Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / applications / utilities / postProcessing / graphics / PV3Readers / PV3blockMeshReader / vtkPV3blockMesh / vtkPV3blockMesh.C
blobda1573bf77c57cc97633b08b012e85693042d98e
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2009-2010 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
11     OpenFOAM is free software: you can redistribute it and/or modify it
12     under the terms of the GNU General Public License as published by
13     the Free Software Foundation, either version 3 of the License, or
14     (at your option) any later version.
16     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19     for more details.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
24 \*---------------------------------------------------------------------------*/
26 #include "vtkPV3blockMesh.H"
27 #include "vtkPV3blockMeshReader.h"
29 // OpenFOAM includes
30 #include "blockMesh.H"
31 #include "Time.H"
32 #include "patchZones.H"
33 #include "OStringStream.H"
35 // VTK includes
36 #include "vtkDataArraySelection.h"
37 #include "vtkMultiBlockDataSet.h"
38 #include "vtkRenderer.h"
39 #include "vtkTextActor.h"
40 #include "vtkTextProperty.h"
42 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
44 defineTypeNameAndDebug(Foam::vtkPV3blockMesh, 0);
46 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
48 void Foam::vtkPV3blockMesh::resetCounters()
50     // Reset mesh part ids and sizes
51     arrayRangeBlocks_.reset();
52     arrayRangeEdges_.reset();
53     arrayRangeCorners_.reset();
57 void Foam::vtkPV3blockMesh::updateInfoBlocks
59     vtkDataArraySelection* arraySelection
62     if (debug)
63     {
64         Info<< "<beg> Foam::vtkPV3blockMesh::updateInfoBlocks"
65             << " [meshPtr=" << (meshPtr_ ? "set" : "NULL") << "]" << endl;
66     }
68     arrayRangeBlocks_.reset( arraySelection->GetNumberOfArrays() );
70     const blockMesh& blkMesh = *meshPtr_;
71     const int nBlocks = blkMesh.size();
72     for (int blockI = 0; blockI < nBlocks; ++blockI)
73     {
74         const blockDescriptor& blockDef = blkMesh[blockI].blockDef();
76         word partName = Foam::name(blockI);
78         // append the (optional) zone name
79         if (!blockDef.zoneName().empty())
80         {
81             partName += " - " + blockDef.zoneName();
82         }
84         // Add blockId and zoneName to GUI list
85         arraySelection->AddArray(partName.c_str());
86     }
88     arrayRangeBlocks_ += nBlocks;
90     if (debug)
91     {
92         // just for debug info
93         getSelectedArrayEntries(arraySelection);
95         Info<< "<end> Foam::vtkPV3blockMesh::updateInfoBlocks" << endl;
96     }
100 void Foam::vtkPV3blockMesh::updateInfoEdges
102     vtkDataArraySelection* arraySelection
105     if (debug)
106     {
107         Info<< "<beg> Foam::vtkPV3blockMesh::updateInfoEdges"
108             << " [meshPtr=" << (meshPtr_ ? "set" : "NULL") << "]" << endl;
109     }
111     arrayRangeEdges_.reset( arraySelection->GetNumberOfArrays() );
113     const blockMesh& blkMesh = *meshPtr_;
114     const curvedEdgeList& edges = blkMesh.edges();
116     const int nEdges = edges.size();
117     forAll(edges, edgeI)
118     {
119         OStringStream ostr;
121         ostr<< edges[edgeI].start() << ":" << edges[edgeI].end() << " - "
122             << edges[edgeI].type();
124         // Add "beg:end - type" to GUI list
125         arraySelection->AddArray(ostr.str().c_str());
126     }
128     arrayRangeEdges_ += nEdges;
130     if (debug)
131     {
132         // just for debug info
133         getSelectedArrayEntries(arraySelection);
135         Info<< "<end> Foam::vtkPV3blockMesh::updateInfoEdges" << endl;
136     }
140 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
142 Foam::vtkPV3blockMesh::vtkPV3blockMesh
144     const char* const FileName,
145     vtkPV3blockMeshReader* reader
148     reader_(reader),
149     dbPtr_(NULL),
150     meshPtr_(NULL),
151     meshRegion_(polyMesh::defaultRegion),
152     meshDir_(polyMesh::meshSubDir),
153     arrayRangeBlocks_("block"),
154     arrayRangeEdges_("edges"),
155     arrayRangeCorners_("corners")
157     if (debug)
158     {
159         Info<< "Foam::vtkPV3blockMesh::vtkPV3blockMesh - "
160             << FileName << endl;
161     }
163     // avoid argList and get rootPath/caseName directly from the file
164     fileName fullCasePath(fileName(FileName).path());
166     if (!isDir(fullCasePath))
167     {
168         return;
169     }
170     if (fullCasePath == ".")
171     {
172         fullCasePath = cwd();
173     }
175     // Set the case as an environment variable - some BCs might use this
176     if (fullCasePath.name().find("processor", 0) == 0)
177     {
178         const fileName globalCase = fullCasePath.path();
180         setEnv("FOAM_CASE", globalCase, true);
181         setEnv("FOAM_CASENAME", globalCase.name(), true);
182     }
183     else
184     {
185         setEnv("FOAM_CASE", fullCasePath, true);
186         setEnv("FOAM_CASENAME", fullCasePath.name(), true);
187     }
189     // look for 'case{region}.OpenFOAM'
190     // could be stringent and insist the prefix match the directory name...
191     // Note: cannot use fileName::name() due to the embedded '{}'
192     string caseName(fileName(FileName).lessExt());
193     string::size_type beg = caseName.find_last_of("/{");
194     string::size_type end = caseName.find('}', beg);
196     if
197     (
198         beg != string::npos && caseName[beg] == '{'
199      && end != string::npos && end == caseName.size()-1
200     )
201     {
202         meshRegion_ = caseName.substr(beg+1, end-beg-1);
204         // some safety
205         if (meshRegion_.empty())
206         {
207             meshRegion_ = polyMesh::defaultRegion;
208         }
210         if (meshRegion_ != polyMesh::defaultRegion)
211         {
212             meshDir_ = meshRegion_/polyMesh::meshSubDir;
213         }
214     }
216     if (debug)
217     {
218         Info<< "fullCasePath=" << fullCasePath << nl
219             << "FOAM_CASE=" << getEnv("FOAM_CASE") << nl
220             << "FOAM_CASENAME=" << getEnv("FOAM_CASENAME") << endl;
221     }
223     // Create time object
224     dbPtr_.reset
225     (
226         new Time
227         (
228             Time::controlDictName,
229             fileName(fullCasePath.path()),
230             fileName(fullCasePath.name())
231         )
232     );
234     dbPtr_().functionObjects().off();
236     updateInfo();
240 // * * * * * * * * * * * * * * * * Destructor  * * * * * * * * * * * * * * * //
242 Foam::vtkPV3blockMesh::~vtkPV3blockMesh()
244     if (debug)
245     {
246         Info<< "<end> Foam::vtkPV3blockMesh::~vtkPV3blockMesh" << endl;
247     }
249     // Hmm. pointNumberTextActors are not getting removed
250     //
251     forAll(pointNumberTextActorsPtrs_, pointI)
252     {
253         pointNumberTextActorsPtrs_[pointI]->Delete();
254     }
255     pointNumberTextActorsPtrs_.clear();
257     delete meshPtr_;
261 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
263 void Foam::vtkPV3blockMesh::updateInfo()
265     if (debug)
266     {
267         Info<< "<beg> Foam::vtkPV3blockMesh::updateInfo"
268             << " [meshPtr=" << (meshPtr_ ? "set" : "NULL") << "] " << endl;
269     }
271     resetCounters();
273     vtkDataArraySelection* blockSelection = reader_->GetBlockSelection();
274     vtkDataArraySelection* edgeSelection = reader_->GetCurvedEdgesSelection();
276     // enable 'internalMesh' on the first call
277     // or preserve the enabled selections
278     stringList enabledParts;
279     stringList enabledEdges;
280     bool firstTime = false;
281     if (!blockSelection->GetNumberOfArrays() && !meshPtr_)
282     {
283         firstTime = true;
284     }
285     else
286     {
287         enabledParts = getSelectedArrayEntries(blockSelection);
288         enabledEdges = getSelectedArrayEntries(edgeSelection);
289     }
291     // Clear current mesh parts list
292     blockSelection->RemoveAllArrays();
293     edgeSelection->RemoveAllArrays();
295     // need a blockMesh
296     updateFoamMesh();
298     // Update mesh parts list
299     updateInfoBlocks( blockSelection );
301     // Update curved edges list
302     updateInfoEdges( edgeSelection );
304     // restore the enabled selections
305     if (!firstTime)
306     {
307         setSelectedArrayEntries(blockSelection, enabledParts);
308         setSelectedArrayEntries(edgeSelection, enabledEdges);
309     }
311     if (debug)
312     {
313         Info<< "<end> Foam::vtkPV3blockMesh::updateInfo" << endl;
314     }
318 void Foam::vtkPV3blockMesh::updateFoamMesh()
320     if (debug)
321     {
322         Info<< "<beg> Foam::vtkPV3blockMesh::updateFoamMesh" << endl;
323     }
325     // Check to see if the OpenFOAM mesh has been created
326     if (!meshPtr_)
327     {
328         if (debug)
329         {
330             Info<< "Creating blockMesh at time=" << dbPtr_().timeName()
331                 << endl;
332         }
334         IOdictionary meshDict
335         (
336             IOobject
337             (
338                 "blockMeshDict",
339                 dbPtr_().constant(),
340                 meshDir_,
341                 dbPtr_(),
342                 IOobject::MUST_READ_IF_MODIFIED,
343                 IOobject::NO_WRITE,
344                 false
345             )
346         );
348         meshPtr_ = new blockMesh(meshDict, meshRegion_);
349     }
352     if (debug)
353     {
354         Info<< "<end> Foam::vtkPV3blockMesh::updateFoamMesh" << endl;
355     }
359 void Foam::vtkPV3blockMesh::Update
361     vtkMultiBlockDataSet* output
364     reader_->UpdateProgress(0.1);
366     // Set up mesh parts selection(s)
367     updateBoolListStatus(blockStatus_, reader_->GetBlockSelection());
369     // Set up curved edges selection(s)
370     updateBoolListStatus(edgeStatus_, reader_->GetCurvedEdgesSelection());
372     reader_->UpdateProgress(0.2);
374     // Update the OpenFOAM mesh
375     updateFoamMesh();
376     reader_->UpdateProgress(0.5);
378     // Convert mesh elemente
379     int blockNo = 0;
381     convertMeshCorners(output, blockNo);
382     convertMeshBlocks(output, blockNo);
383     convertMeshEdges(output, blockNo);
385     reader_->UpdateProgress(0.8);
390 void Foam::vtkPV3blockMesh::CleanUp()
392     reader_->UpdateProgress(1.0);
396 void Foam::vtkPV3blockMesh::renderPointNumbers
398     vtkRenderer* renderer,
399     const bool show
402     // always remove old actors first
404     forAll(pointNumberTextActorsPtrs_, pointI)
405     {
406         renderer->RemoveViewProp(pointNumberTextActorsPtrs_[pointI]);
407         pointNumberTextActorsPtrs_[pointI]->Delete();
408     }
409     pointNumberTextActorsPtrs_.clear();
411     if (show && meshPtr_)
412     {
413         const pointField& cornerPts = meshPtr_->blockPointField();
414         const scalar scaleFactor = meshPtr_->scaleFactor();
416         pointNumberTextActorsPtrs_.setSize(cornerPts.size());
417         forAll(cornerPts, pointI)
418         {
419             vtkTextActor* txt = vtkTextActor::New();
421             txt->SetInput(Foam::name(pointI).c_str());
423             // Set text properties
424             vtkTextProperty* tprop = txt->GetTextProperty();
425             tprop->SetFontFamilyToArial();
426             tprop->BoldOn();
427             tprop->ShadowOff();
428             tprop->SetLineSpacing(1.0);
429             tprop->SetFontSize(14);
430             tprop->SetColor(1.0, 0.0, 1.0);
431             tprop->SetJustificationToCentered();
433             // Set text to use 3-D world co-ordinates
434             txt->GetPositionCoordinate()->SetCoordinateSystemToWorld();
436             txt->GetPositionCoordinate()->SetValue
437             (
438                 cornerPts[pointI].x()*scaleFactor,
439                 cornerPts[pointI].y()*scaleFactor,
440                 cornerPts[pointI].z()*scaleFactor
441             );
443             // Add text to each renderer
444             renderer->AddViewProp(txt);
446             // Maintain a list of text labels added so that they can be
447             // removed later
448             pointNumberTextActorsPtrs_[pointI] = txt;
449         }
450     }
455 void Foam::vtkPV3blockMesh::PrintSelf(ostream& os, vtkIndent indent) const
457 #if 0
458     os  << indent << "Number of nodes: "
459         << (meshPtr_ ? meshPtr_->nPoints() : 0) << "\n";
461     os  << indent << "Number of cells: "
462         << (meshPtr_ ? meshPtr_->nCells() : 0) << "\n";
464     os  << indent << "Number of available time steps: "
465         << (dbPtr_.valid() ? dbPtr_().times().size() : 0) << endl;
466 #endif
469 // ************************************************************************* //