Back to index

salome-paravis  6.5.0
vtkMedUtilities.h
Go to the documentation of this file.
00001 // Copyright (C) 2010-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #ifndef _vtkMedUtilities_h_
00021 #define _vtkMedUtilities_h_
00022 
00023 #include "vtkObject.h"
00024 #include "vtkSmartPointer.h"
00025 #include "vtkMultiBlockDataSet.h"
00026 #include "vtkStringArray.h"
00027 #include "vtkMedSetGet.h"
00028 #include "vtkMed.h"
00029 #include "vtkMedReader.h"
00030 
00031 #include <utility>
00032 #include <string>
00033 #include <vector>
00034 #include <list>
00035 #include <set>
00036 #include <map>
00037 
00038 class vtkDataArray;
00039 class vtkMedMesh;
00040 class vtkMedFamily;
00041 class vtkMedGroup;
00042 class vtkIdList;
00043 class vtkMutableDirectedGraph;
00044 class vtkInformationIntegerKey;
00045 class vtkInformationObjectBaseKey;
00046 
00047 //BTX
00048 class vtkMedComputeStep
00049 {
00050 public :
00051   med_int IterationIt;
00052   med_int TimeIt;
00053   med_float TimeOrFrequency;
00054 
00055   vtkMedComputeStep()
00056     {
00057     this->IterationIt = MED_NO_IT;
00058     this->TimeIt = MED_NO_DT;
00059     this->TimeOrFrequency = MED_UNDEF_DT;
00060     }
00061 };
00062 
00063 bool operator==(const vtkMedComputeStep& cs0, const vtkMedComputeStep& cs1);
00064 bool operator!=(const vtkMedComputeStep& cs0, const vtkMedComputeStep& cs1);
00065 bool operator<(const vtkMedComputeStep& cs0, const vtkMedComputeStep& cs1);
00066 
00067 class vtkMedEntity
00068 {
00069 public :
00070 
00071   vtkMedEntity() : EntityType(MED_NODE),
00072                      GeometryType(MED_NONE)
00073     {
00074     }
00075 
00076   vtkMedEntity(med_entity_type type, med_geometry_type geometry) :
00077       EntityType(type),
00078       GeometryType(geometry)
00079     {
00080     }
00081 
00082   ~vtkMedEntity()
00083     {
00084     }
00085 
00086   vtkMedEntity(const vtkMedEntity& entity) :
00087       EntityType(entity.EntityType),
00088       GeometryType(entity.GeometryType)
00089     {
00090     this->GeometryName = entity.GeometryName;
00091     }
00092 
00093   void operator=(const vtkMedEntity& entity)
00094     {
00095     this->EntityType = entity.EntityType;
00096     this->GeometryType = entity.GeometryType;
00097     this->GeometryName = entity.GeometryName;
00098     }
00099 
00100   med_entity_type EntityType;
00101   med_geometry_type GeometryType;
00102   std::string GeometryName;
00103 };
00104 
00105 bool operator==(const vtkMedEntity& cs0, const vtkMedEntity& cs1);
00106 bool operator!=(const vtkMedEntity& cs0, const vtkMedEntity& cs1);
00107 bool operator<(const vtkMedEntity& cs0, const vtkMedEntity& cs1);
00108 //ETX
00109 
00110 class VTK_EXPORT vtkMedUtilities
00111 {
00112 public:
00113   static vtkInformationIntegerKey* ELNO();
00114   static vtkInformationIntegerKey* ELGA();
00115   static vtkInformationStringVectorKey* BLOCK_NAME();
00116   static vtkInformationObjectBaseKey* STRUCT_ELEMENT();
00117   static vtkInformationIntegerKey* STRUCT_ELEMENT_INDEX();
00118 
00119   // Description:
00120   // return an array to store the coordinates of nodes.
00121   // the  type of the elements is the same as the one in the med file
00122   static vtkDataArray* NewCoordArray();
00123 
00124   // Description:
00125   // returns an array to store data of a given type.
00126   // the type corresponds to med types.
00127   static vtkDataArray* NewArray(med_field_type type);
00128   static vtkAbstractArray* NewArray(med_attribute_type type);
00129 
00130   //BTX
00131   enum
00132   {
00133     OnPoint, OnCell
00134   };
00135   //ETX
00136 
00137   //BTX
00138   // Description:
00139   // returns a name for the given med_geometry_type
00140   static const char* GeometryName(med_geometry_type geometry);
00141 
00142   // Description:
00143   // returns a name for the given med_geometry_type
00144   static const char* EntityName(med_entity_type type);
00145 
00146   // Description:
00147   // returns a name for the given med_connectivity_mode
00148   static const char* ConnectivityName(med_connectivity_mode conn);
00149 
00150   static const std::string SimplifyName(const char* medName);
00151 
00152   static const std::string FamilyKey(const char* meshName, int pointOrCell,
00153       const char* familyName);
00154   static const std::string GroupKey(const char* meshName, int pointOrCell,
00155       const char* groupName);
00156 
00157   static const std::string EntityKey(const vtkMedEntity&);
00158 
00159   static int GetNumberOfPoint(med_geometry_type geometry);
00160   static int GetDimension(med_geometry_type geometry);
00161 
00162   // returns the VTK cell type (as described in the vtkCellType.h file)
00163   // corresponding to the given med_geometry_type
00164   static int GetVTKCellType(med_geometry_type geometry);
00165 
00166   // returns the number of sub entity : the number of faces for cells,
00167   // the number of edges for faces, the number of nodes for edges
00168   static int GetNumberOfSubEntity(med_geometry_type geometry);
00169 
00170   // returns the number of Nodes
00171   static int GetNumberOfNodes(med_geometry_type geometry);
00172   //ETX
00173 
00174   static med_entity_type GetSubType(med_entity_type type);
00175   static med_geometry_type GetSubGeometry(med_geometry_type geometry,
00176       int index);
00177 
00178   static int GetParentNodeIndex(med_geometry_type parentGeometry,
00179       int subEntityIndex, int subEntityNodeIndex);
00180 
00181   // Description :
00182   // Project the ids gathered in the sub entity to the parent entity.
00183   // used for MED_DESC connectivity.
00184   // Rem : no check is performed, and do not work for
00185   // MED_POLYHEDRE and MED_POLYGON
00186   static void ProjectConnectivity(med_geometry_type parentGeometry,
00187       vtkIdList* parentIds, vtkIdList* subEntityIds, int subEntityIndex,
00188       bool invert);
00189 
00190   static char Separator;
00191 
00192   static const char* NoGroupName;
00193   static const char* OnCellName;
00194   static const char* OnPointName;
00195 
00196   //BTX
00197   static void SplitGroupKey(const char* name, vtkstd::string& mesh,
00198       vtkstd::string& entity, vtkstd::string& group);
00199 
00200   static std::string GetModeKey(int index, double frequency, int maxindex);
00201   static int  GetModeFromKey(const char*, int& index, double& frequency);
00202 
00203   static int MedToVTKIndex(int vtktype, int node);
00204 
00205   static vtkMultiBlockDataSet* GetParent(vtkMultiBlockDataSet* root,
00206                                   vtkStringArray* path);
00207 
00208   // Description:
00209   // Format the id list so that it respects the VTK format for polyhedrons :
00210   // numfaces, npts_face0, pt0, ... npts_face1, pt1 ....
00211   static int  FormatPolyhedronForVTK(vtkMedFamilyOnEntityOnProfile*,
00212                                vtkIdType, vtkIdList*);
00213 
00214   static int SizeOf(med_attribute_type type);
00215   //ETX
00216 };
00217 
00218 //BTX
00219 
00220 template<class T>
00221 class vtkObjectVector: public std::vector<vtkSmartPointer<T> >
00222 {
00223 };
00224 
00225 template<class T>
00226 class vtkMedComputeStepMap: public
00227     std::map<med_int, std::map<med_int, vtkSmartPointer<T> > >
00228 {
00229 public :
00230   void  AddObject(const vtkMedComputeStep& cs, T* obj)
00231     {
00232     (*this)[cs.TimeIt][cs.IterationIt] = obj;
00233     this->TimeIt[cs.TimeOrFrequency] = cs.TimeIt;
00234     }
00235 
00236   T* GetObject(const vtkMedComputeStep& cs)
00237     {
00238     if(this->find(cs.TimeIt) == this->end())
00239       return NULL;
00240 
00241     std::map<med_int, vtkSmartPointer<T> >& itmap = (*this)[cs.TimeIt];
00242 
00243     if(itmap.find(cs.IterationIt) == itmap.end())
00244       return NULL;
00245 
00246     return itmap[cs.IterationIt];
00247     }
00248 
00249   med_int GetNumberOfObject()
00250     {
00251     med_int nb = 0;
00252     typename vtkMedComputeStepMap<T>::iterator it = this->begin();
00253     while(it != this->end())
00254       {
00255       nb += it->second.size();
00256       it++;
00257       }
00258     return nb;
00259     }
00260 
00261   T* GetObject(med_int id)
00262     {
00263     med_int nb = 0;
00264     if(id < 0)
00265       return NULL;
00266 
00267     typename vtkMedComputeStepMap<T>::iterator it = this->begin();
00268     while(it != this->end())
00269       {
00270       std::map<med_int, vtkSmartPointer<T> >& itmap = it->second;
00271       nb += itmap.size();
00272       if(id < nb)
00273         {
00274         typename std::map<med_int, vtkSmartPointer<T> >::iterator iterationit =
00275             itmap.begin();
00276         for(int ii=0; ii<nb-id-1; ii++)
00277           iterationit++;
00278         return iterationit->second;
00279         }
00280       it++;
00281       }
00282     return NULL;
00283     }
00284 
00285   T* FindObject(const vtkMedComputeStep& cs, int strategy)
00286     {
00287     // first test if the given compute step is present
00288     T* obj = this->GetObject(cs);
00289     if(obj != NULL)
00290       return obj;
00291 
00292     if(this->size() == 0)
00293       return NULL;
00294 
00295     // let us first find the iterator that corresponds to the given time
00296     med_int timeit = this->FindTimeIterator(cs.TimeOrFrequency, cs.TimeIt);
00297 
00298     std::map<med_int, vtkSmartPointer<T> >& itmap =
00299         (*this)[timeit];
00300 
00301     if(itmap.size() == 0)
00302       return NULL;
00303 
00304     if(strategy == vtkMedReader::PhysicalTime
00305        || strategy == vtkMedReader::Modes)
00306       {
00307       // in this strategies, we return the last iteration for each time.
00308       return itmap.rbegin()->second;
00309       }
00310     else if(strategy == vtkMedReader::Iteration)
00311       {
00312       // in this case, we look for the real iteration
00313       typename std::map<med_int, vtkSmartPointer<T> >::iterator iterationit
00314           = itmap.lower_bound(cs.IterationIt);
00315 
00316       // if this is not exactly the same step and if this is not the first
00317       // step, rool back one step to choose the one just before the asked time.
00318       if(iterationit->first != cs.IterationIt && iterationit != itmap.begin())
00319         iterationit--;
00320 
00321       // the time iterator asked for is higher than all times,
00322       // let us pick the last one.
00323       if(iterationit == itmap.end())
00324         iterationit--;
00325 
00326       return iterationit->second;
00327       }
00328     }
00329 
00330   void  GatherTimes(std::set<med_float>& times)
00331     {
00332     typename std::map<med_float, med_int>::iterator it
00333         = this->TimeIt.begin();
00334     while(it != this->TimeIt.end())
00335       {
00336       times.insert(it->first);
00337       it++;
00338       }
00339     }
00340 
00341   void  GatherIterations(med_float time, std::set<med_int>& iterations)
00342     {
00343     med_int timeit = this->FindTimeIterator(time, -1);
00344     if(timeit == -1)
00345       return;
00346 
00347     std::map<med_int, vtkSmartPointer<T> >& itmap =
00348         (*this)[timeit];
00349 
00350     typename std::map<med_int, vtkSmartPointer<T> >::iterator it =
00351         itmap.begin();
00352 
00353     while(it != itmap.end())
00354       {
00355       iterations.insert(it->first);
00356       it++;
00357       }
00358     }
00359 
00360 protected :
00361 
00362   med_int FindTimeIterator(med_float time, med_int defaultit)
00363     {
00364     if(this->TimeIt.size() == 0)
00365       return defaultit;
00366 
00367     typename std::map<med_float, med_int>::iterator it
00368         = this->TimeIt.lower_bound(time);
00369 
00370     // if this is not exactly the same step and if this is not the first step,
00371     // rool back one step to choose the one just before the asked time.
00372     if(it->first != time && it != this->TimeIt.begin())
00373       it--;
00374 
00375     // if the time iterator asked for is higher than all times,
00376     // let us pick the last one.
00377     if(it == this->TimeIt.end())
00378       it--;
00379 
00380     return it->second;
00381     }
00382 
00383   std::map<med_float, med_int> TimeIt;
00384 };
00385 
00386 template<class T>
00387 class vtkList: public std::list<T>
00388 {
00389 };
00390 
00391 template<class T1, class T2>
00392 struct IsSameTraits
00393 {
00394   static const bool IsSame()
00395   {
00396     return false;
00397   }
00398 };
00399 
00400 template<class T1>
00401 struct IsSameTraits<T1, T1>
00402 {
00403   static const bool IsSame()
00404   {
00405     return true;
00406   }
00407 };
00408 
00409 #define PRINT_IVAR(os, indent, name) \
00410   os << indent << #name << " : "  << name << endl;
00411 
00412 #define PRINT_STRING(os, indent, name) \
00413   os << indent << #name << " : "  << ( name ? name : "(void)") << endl;
00414 
00415 #define PRINT_OBJECT(os, indent, name) \
00416   os << indent << #name << " : " ;\
00417   if(name != NULL) \
00418   {\
00419     os << endl;\
00420     name->PrintSelf(os, indent.GetNextIndent());\
00421   }\
00422   else os << "(NULL)" << endl;
00423 
00424 #define PRINT_VECTOR(os, indent, name, size) \
00425 {\
00426   os << indent << #name << " : (";\
00427   for(vtkIdType _index = 0; _index<size; _index++)\
00428     {\
00429     os << name[_index];\
00430     if(_index < size-1)\
00431       os << ", ";\
00432     }\
00433   os << ")" << endl;\
00434 }
00435 
00436 #define PRINT_OBJECT_VECTOR(os, indent, name) \
00437 {\
00438   os << indent << #name;\
00439   os << endl;\
00440   vtkIdType _size = name->size();\
00441   for(vtkIdType _index = 0; _index < _size; _index++)\
00442   {\
00443   os << indent << #name << _index << " : " << endl;\
00444   if(name->at(_index) != NULL)\
00445     name->at(_index)->PrintSelf(os, indent.GetNextIndent());\
00446   else\
00447     os << indent.GetNextIndent() << "(NULL)" << endl;\
00448   }\
00449 }
00450 
00451 #define PRINT_STRING_VECTOR(os, indent, name)\
00452 {\
00453   os << indent << #name << ": ";\
00454   for(int _comp = 0; _comp<this->name->size(); _comp++)\
00455     {\
00456     os << this->name->at(_comp)->GetString();\
00457     if(_comp < this->name->size()-1)\
00458       os << ", ";\
00459     }\
00460   os << endl;\
00461 }
00462 
00463 #define PRINT_MED_STRING(os, indent, name)\
00464   os << indent << #name << ": " << this->name->GetString() << endl; \
00465 
00466 //ETX
00467 
00468 #endif