1 // Copyright (C) 2010-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #ifndef _vtkMedUtilities_h_
21 #define _vtkMedUtilities_h_
23 #include "vtkObject.h"
24 #include "vtkSmartPointer.h"
25 #include "vtkMultiBlockDataSet.h"
26 #include "vtkStringArray.h"
27 #include "vtkMedSetGet.h"
29 #include "vtkMedReader.h"
43 class vtkMutableDirectedGraph;
44 class vtkInformationIntegerKey;
45 class vtkInformationObjectBaseKey;
48 class vtkMedComputeStep
53 med_float TimeOrFrequency;
57 this->IterationIt = MED_NO_IT;
58 this->TimeIt = MED_NO_DT;
59 this->TimeOrFrequency = MED_UNDEF_DT;
63 bool operator==(const vtkMedComputeStep& cs0, const vtkMedComputeStep& cs1);
64 bool operator!=(const vtkMedComputeStep& cs0, const vtkMedComputeStep& cs1);
65 bool operator<(const vtkMedComputeStep& cs0, const vtkMedComputeStep& cs1);
71 vtkMedEntity() : EntityType(MED_NODE),
72 GeometryType(MED_NONE)
76 vtkMedEntity(med_entity_type type, med_geometry_type geometry) :
78 GeometryType(geometry)
86 vtkMedEntity(const vtkMedEntity& entity) :
87 EntityType(entity.EntityType),
88 GeometryType(entity.GeometryType)
90 this->GeometryName = entity.GeometryName;
93 void operator=(const vtkMedEntity& entity)
95 this->EntityType = entity.EntityType;
96 this->GeometryType = entity.GeometryType;
97 this->GeometryName = entity.GeometryName;
100 med_entity_type EntityType;
101 med_geometry_type GeometryType;
102 std::string GeometryName;
105 bool operator==(const vtkMedEntity& cs0, const vtkMedEntity& cs1);
106 bool operator!=(const vtkMedEntity& cs0, const vtkMedEntity& cs1);
107 bool operator<(const vtkMedEntity& cs0, const vtkMedEntity& cs1);
110 class VTK_EXPORT vtkMedUtilities
113 static vtkInformationIntegerKey* ELNO();
114 static vtkInformationIntegerKey* ELGA();
115 static vtkInformationStringVectorKey* BLOCK_NAME();
116 static vtkInformationObjectBaseKey* STRUCT_ELEMENT();
117 static vtkInformationIntegerKey* STRUCT_ELEMENT_INDEX();
120 // return an array to store the coordinates of nodes.
121 // the type of the elements is the same as the one in the med file
122 static vtkDataArray* NewCoordArray();
125 // returns an array to store data of a given type.
126 // the type corresponds to med types.
127 static vtkDataArray* NewArray(med_field_type type);
128 static vtkAbstractArray* NewArray(med_attribute_type type);
139 // returns a name for the given med_geometry_type
140 static const char* GeometryName(med_geometry_type geometry);
143 // returns a name for the given med_geometry_type
144 static const char* EntityName(med_entity_type type);
147 // returns a name for the given med_connectivity_mode
148 static const char* ConnectivityName(med_connectivity_mode conn);
150 static const std::string SimplifyName(const char* medName);
152 static const std::string FamilyKey(const char* meshName, int pointOrCell,
153 const char* familyName);
154 static const std::string GroupKey(const char* meshName, int pointOrCell,
155 const char* groupName);
157 static const std::string EntityKey(const vtkMedEntity&);
159 static int GetNumberOfPoint(med_geometry_type geometry);
160 static int GetDimension(med_geometry_type geometry);
162 // returns the VTK cell type (as described in the vtkCellType.h file)
163 // corresponding to the given med_geometry_type
164 static int GetVTKCellType(med_geometry_type geometry);
166 // returns the number of sub entity : the number of faces for cells,
167 // the number of edges for faces, the number of nodes for edges
168 static int GetNumberOfSubEntity(med_geometry_type geometry);
170 // returns the number of Nodes
171 static int GetNumberOfNodes(med_geometry_type geometry);
174 static med_entity_type GetSubType(med_entity_type type);
175 static med_geometry_type GetSubGeometry(med_geometry_type geometry,
178 static int GetParentNodeIndex(med_geometry_type parentGeometry,
179 int subEntityIndex, int subEntityNodeIndex);
182 // Project the ids gathered in the sub entity to the parent entity.
183 // used for MED_DESC connectivity.
184 // Rem : no check is performed, and do not work for
185 // MED_POLYHEDRE and MED_POLYGON
186 static void ProjectConnectivity(med_geometry_type parentGeometry,
187 vtkIdList* parentIds, vtkIdList* subEntityIds, int subEntityIndex,
190 static char Separator;
192 static const char* NoGroupName;
193 static const char* OnCellName;
194 static const char* OnPointName;
197 static void SplitGroupKey(const char* name, vtkstd::string& mesh,
198 vtkstd::string& entity, vtkstd::string& group);
200 static std::string GetModeKey(int index, double frequency, int maxindex);
201 static int GetModeFromKey(const char*, int& index, double& frequency);
203 static int MedToVTKIndex(int vtktype, int node);
205 static vtkMultiBlockDataSet* GetParent(vtkMultiBlockDataSet* root,
206 vtkStringArray* path);
209 // Format the id list so that it respects the VTK format for polyhedrons :
210 // numfaces, npts_face0, pt0, ... npts_face1, pt1 ....
211 static int FormatPolyhedronForVTK(vtkMedFamilyOnEntityOnProfile*,
212 vtkIdType, vtkIdList*);
214 static int SizeOf(med_attribute_type type);
221 class vtkObjectVector: public std::vector<vtkSmartPointer<T> >
226 class vtkMedComputeStepMap: public
227 std::map<med_int, std::map<med_int, vtkSmartPointer<T> > >
230 void AddObject(const vtkMedComputeStep& cs, T* obj)
232 (*this)[cs.TimeIt][cs.IterationIt] = obj;
233 this->TimeIt[cs.TimeOrFrequency] = cs.TimeIt;
236 T* GetObject(const vtkMedComputeStep& cs)
238 if(this->find(cs.TimeIt) == this->end())
241 std::map<med_int, vtkSmartPointer<T> >& itmap = (*this)[cs.TimeIt];
243 if(itmap.find(cs.IterationIt) == itmap.end())
246 return itmap[cs.IterationIt];
249 med_int GetNumberOfObject()
252 typename vtkMedComputeStepMap<T>::iterator it = this->begin();
253 while(it != this->end())
255 nb += it->second.size();
261 T* GetObject(med_int id)
267 typename vtkMedComputeStepMap<T>::iterator it = this->begin();
268 while(it != this->end())
270 std::map<med_int, vtkSmartPointer<T> >& itmap = it->second;
274 typename std::map<med_int, vtkSmartPointer<T> >::iterator iterationit =
276 for(int ii=0; ii<nb-id-1; ii++)
278 return iterationit->second;
285 T* FindObject(const vtkMedComputeStep& cs, int strategy)
287 // first test if the given compute step is present
288 T* obj = this->GetObject(cs);
292 if(this->size() == 0)
295 // let us first find the iterator that corresponds to the given time
296 med_int timeit = this->FindTimeIterator(cs.TimeOrFrequency, cs.TimeIt);
298 std::map<med_int, vtkSmartPointer<T> >& itmap =
301 if(itmap.size() == 0)
304 if(strategy == vtkMedReader::PhysicalTime
305 || strategy == vtkMedReader::Modes)
307 // in this strategies, we return the last iteration for each time.
308 return itmap.rbegin()->second;
310 else if(strategy == vtkMedReader::Iteration)
312 // in this case, we look for the real iteration
313 typename std::map<med_int, vtkSmartPointer<T> >::iterator iterationit
314 = itmap.lower_bound(cs.IterationIt);
316 // if this is not exactly the same step and if this is not the first
317 // step, rool back one step to choose the one just before the asked time.
318 if(iterationit->first != cs.IterationIt && iterationit != itmap.begin())
321 // the time iterator asked for is higher than all times,
322 // let us pick the last one.
323 if(iterationit == itmap.end())
326 return iterationit->second;
330 void GatherTimes(std::set<med_float>& times)
332 typename std::map<med_float, med_int>::iterator it
333 = this->TimeIt.begin();
334 while(it != this->TimeIt.end())
336 times.insert(it->first);
341 void GatherIterations(med_float time, std::set<med_int>& iterations)
343 med_int timeit = this->FindTimeIterator(time, -1);
347 std::map<med_int, vtkSmartPointer<T> >& itmap =
350 typename std::map<med_int, vtkSmartPointer<T> >::iterator it =
353 while(it != itmap.end())
355 iterations.insert(it->first);
362 med_int FindTimeIterator(med_float time, med_int defaultit)
364 if(this->TimeIt.size() == 0)
367 typename std::map<med_float, med_int>::iterator it
368 = this->TimeIt.lower_bound(time);
370 // if this is not exactly the same step and if this is not the first step,
371 // rool back one step to choose the one just before the asked time.
372 if(it->first != time && it != this->TimeIt.begin())
375 // if the time iterator asked for is higher than all times,
376 // let us pick the last one.
377 if(it == this->TimeIt.end())
383 std::map<med_float, med_int> TimeIt;
387 class vtkList: public std::list<T>
391 template<class T1, class T2>
394 static const bool IsSame()
401 struct IsSameTraits<T1, T1>
403 static const bool IsSame()
409 #define PRINT_IVAR(os, indent, name) \
410 os << indent << #name << " : " << name << endl;
412 #define PRINT_STRING(os, indent, name) \
413 os << indent << #name << " : " << ( name ? name : "(void)") << endl;
415 #define PRINT_OBJECT(os, indent, name) \
416 os << indent << #name << " : " ;\
420 name->PrintSelf(os, indent.GetNextIndent());\
422 else os << "(NULL)" << endl;
424 #define PRINT_VECTOR(os, indent, name, size) \
426 os << indent << #name << " : (";\
427 for(vtkIdType _index = 0; _index<size; _index++)\
436 #define PRINT_OBJECT_VECTOR(os, indent, name) \
438 os << indent << #name;\
440 vtkIdType _size = name->size();\
441 for(vtkIdType _index = 0; _index < _size; _index++)\
443 os << indent << #name << _index << " : " << endl;\
444 if(name->at(_index) != NULL)\
445 name->at(_index)->PrintSelf(os, indent.GetNextIndent());\
447 os << indent.GetNextIndent() << "(NULL)" << endl;\
451 #define PRINT_STRING_VECTOR(os, indent, name)\
453 os << indent << #name << ": ";\
454 for(int _comp = 0; _comp<this->name->size(); _comp++)\
456 os << this->name->at(_comp)->GetString();\
457 if(_comp < this->name->size()-1)\
463 #define PRINT_MED_STRING(os, indent, name)\
464 os << indent << #name << ": " << this->name->GetString() << endl; \