Initial commit for version 2.0.x patch release
[OpenFOAM-2.0.x.git] / applications / utilities / postProcessing / graphics / PV3Readers / vtkPV3Readers / vtkPV3Readers.C
blob9a7e305a0264271d5f331ceabe1729fbab85c9b8
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 2004-2011 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 Description
25     Misc helper methods and utilities
27 \*---------------------------------------------------------------------------*/
29 #include "vtkPV3Readers.H"
31 // OpenFOAM includes
32 #include "IFstream.H"
34 // VTK includes
35 #include "vtkDataArraySelection.h"
36 #include "vtkDataSet.h"
37 #include "vtkMultiBlockDataSet.h"
38 #include "vtkInformation.h"
40 // * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
42 defineTypeNameAndDebug(Foam::vtkPV3Readers, 0);
44 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
46 namespace Foam
48     //! \cond fileScope
49     //  Extract up to the first non-word characters
50     inline word getFirstWord(const char* str)
51     {
52         if (str)
53         {
54             label n = 0;
55             while (str[n] && word::valid(str[n]))
56             {
57                 ++n;
58             }
59             return word(str, n, true);
60         }
61         else
62         {
63             return word::null;
64         }
66     }
67     //! \endcond
69 } // End namespace Foam
72 // * * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * //
74 void Foam::vtkPV3Readers::AddToBlock
76     vtkMultiBlockDataSet* output,
77     vtkDataSet* dataset,
78     const partInfo& selector,
79     const label datasetNo,
80     const std::string& datasetName
83     const int blockNo = selector.block();
85     vtkDataObject* blockDO = output->GetBlock(blockNo);
86     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
88     if (!block)
89     {
90         if (blockDO)
91         {
92             FatalErrorIn("Foam::vtkPV3Readers::AddToBlock")
93                 << "Block already has a vtkDataSet assigned to it"
94                 << endl;
95             return;
96         }
98         block = vtkMultiBlockDataSet::New();
99         output->SetBlock(blockNo, block);
100         block->Delete();
101     }
103     if (debug)
104     {
105         Info<< "block[" << blockNo << "] has "
106             << block->GetNumberOfBlocks()
107             <<  " datasets prior to adding set " << datasetNo
108             <<  " with name: " << datasetName << endl;
109     }
111     block->SetBlock(datasetNo, dataset);
113     // name the block when assigning dataset 0
114     if (datasetNo == 0)
115     {
116         output->GetMetaData(blockNo)->Set
117         (
118             vtkCompositeDataSet::NAME(),
119             selector.name()
120         );
121     }
123     if (datasetName.size())
124     {
125         block->GetMetaData(datasetNo)->Set
126         (
127             vtkCompositeDataSet::NAME(),
128             datasetName.c_str()
129         );
130     }
134 vtkDataSet* Foam::vtkPV3Readers::GetDataSetFromBlock
136     vtkMultiBlockDataSet* output,
137     const partInfo& selector,
138     const label datasetNo
141     const int blockNo = selector.block();
143     vtkDataObject* blockDO = output->GetBlock(blockNo);
144     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
146     if (block)
147     {
148         return vtkDataSet::SafeDownCast(block->GetBlock(datasetNo));
149     }
151     return 0;
155 // ununsed at the moment
156 Foam::label Foam::vtkPV3Readers::GetNumberOfDataSets
158     vtkMultiBlockDataSet* output,
159     const partInfo& selector
162     const int blockNo = selector.block();
164     vtkDataObject* blockDO = output->GetBlock(blockNo);
165     vtkMultiBlockDataSet* block = vtkMultiBlockDataSet::SafeDownCast(blockDO);
166     if (block)
167     {
168         return block->GetNumberOfBlocks();
169     }
171     return 0;
175 // Foam::word Foam::vtkPV3Readers::getPartName(int partId)
176 // {
177 //     return getFirstWord(reader_->GetPartArrayName(partId));
178 // }
181 Foam::wordHashSet Foam::vtkPV3Readers::getSelected
183     vtkDataArraySelection* select
186     int nElem = select->GetNumberOfArrays();
187     wordHashSet selections(2*nElem);
189     for (int elemI=0; elemI < nElem; ++elemI)
190     {
191         if (select->GetArraySetting(elemI))
192         {
193             selections.insert(getFirstWord(select->GetArrayName(elemI)));
194         }
195     }
197     return selections;
201 Foam::wordHashSet Foam::vtkPV3Readers::getSelected
203     vtkDataArraySelection* select,
204     const partInfo& selector
207     int nElem = select->GetNumberOfArrays();
208     wordHashSet selections(2*nElem);
210     for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
211     {
212         if (select->GetArraySetting(elemI))
213         {
214             selections.insert(getFirstWord(select->GetArrayName(elemI)));
215         }
216     }
218     return selections;
222 Foam::stringList Foam::vtkPV3Readers::getSelectedArrayEntries
224     vtkDataArraySelection* select
227     stringList selections(select->GetNumberOfArrays());
228     label nElem = 0;
230     forAll(selections, elemI)
231     {
232         if (select->GetArraySetting(elemI))
233         {
234             selections[nElem++] = select->GetArrayName(elemI);
235         }
236     }
237     selections.setSize(nElem);
240     if (debug)
241     {
242         label nElem = select->GetNumberOfArrays();
243         Info<< "available(";
244         for (int elemI = 0; elemI < nElem; ++elemI)
245         {
246             Info<< " \"" << select->GetArrayName(elemI) << "\"";
247         }
248         Info<< " )\nselected(";
250         forAll(selections, elemI)
251         {
252             Info<< " " << selections[elemI];
253         }
254         Info<< " )\n";
255     }
257     return selections;
261 Foam::stringList Foam::vtkPV3Readers::getSelectedArrayEntries
263     vtkDataArraySelection* select,
264     const partInfo& selector
267     stringList selections(selector.size());
268     label nElem = 0;
270     for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
271     {
272         if (select->GetArraySetting(elemI))
273         {
274             selections[nElem++] = select->GetArrayName(elemI);
275         }
276     }
277     selections.setSize(nElem);
280     if (debug)
281     {
282         Info<< "available(";
283         for (int elemI = selector.start(); elemI < selector.end(); ++elemI)
284         {
285             Info<< " \"" << select->GetArrayName(elemI) << "\"";
286         }
287         Info<< " )\nselected(";
289         forAll(selections, elemI)
290         {
291             Info<< " " << selections[elemI];
292         }
293         Info<< " )\n";
294     }
296     return selections;
300 void Foam::vtkPV3Readers::setSelectedArrayEntries
302     vtkDataArraySelection* select,
303     const stringList& selections
306     const int nElem = select->GetNumberOfArrays();
307     select->DisableAllArrays();
309     // Loop through entries, setting values from selectedEntries
310     for (int elemI=0; elemI < nElem; ++elemI)
311     {
312         string arrayName(select->GetArrayName(elemI));
314         forAll(selections, elemI)
315         {
316             if (selections[elemI] == arrayName)
317             {
318                 select->EnableArray(arrayName.c_str());
319                 break;
320             }
321         }
322     }
326 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
329 // ************************************************************************* //