Salome HOME
Fix for Bug IPAL8945
[modules/visu.git] / src / CONVERTOR / VISU_Convertor.hxx
1 //  VISU CONVERTOR :
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
6 //  This library is free software; you can redistribute it and/or 
7 //  modify it under the terms of the GNU Lesser General Public 
8 //  License as published by the Free Software Foundation; either 
9 //  version 2.1 of the License. 
10 // 
11 //  This library is distributed in the hope that it will be useful, 
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of 
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
14 //  Lesser General Public License for more details. 
15 // 
16 //  You should have received a copy of the GNU Lesser General Public 
17 //  License along with this library; if not, write to the Free Software 
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //  File   : VISU_Convertor.hxx
24 //  Author : Alexey PETROV
25 //  Module : VISU
26
27 #ifndef VISU_Convertor_HeaderFile
28 #define VISU_Convertor_HeaderFile
29
30 /*! 
31   \file VISU_Convertor.hxx
32   \brief The file contains definitions for basic classes of the VISU CONVERTER package
33 */
34
35 #include "VISU_IDMapper.hxx"
36 #include "VISU_ConvertorDef.hxx"
37
38 #include "MED_Vector.hxx"
39
40 #include <map>
41 #include <set>
42 #include <utility>
43 #include <string>
44 #include <stdexcept>
45
46 namespace VISU
47 {
48   using MED::TVector;
49
50   //---------------------------------------------------------------
51   typedef std::string TName;
52
53   typedef TVector<TName> TNames;
54
55   //---------------------------------------------------------------
56   //! Define a basic class for all MED entites which can be identified by its number
57   struct TIntId: virtual TBaseStructure
58   {
59     vtkIdType myId;
60
61     TIntId(): myId(0)
62     {}
63   };
64
65
66   //---------------------------------------------------------------
67   typedef std::map<TEntity,PMeshOnEntity> TMeshOnEntityMap;
68   typedef std::map<TName,PGroup> TGroupMap;
69
70   //! Define a basic class which corresponds to MED MESH entity
71   /*!
72     This class in its turn contains map of TMeshOnEntity and TGroup substructures,
73     also it keeps name and dimention of corresponding MED MESH entity.
74   */
75   struct TMesh: virtual TBaseStructure
76   {
77     TMeshOnEntityMap myMeshOnEntityMap; //!< Contains corresponding meshes for MED ENTITIES
78     TGroupMap myGroupMap; //!< Contains map of bounded MED GROUPS
79     TName myName; //! Name of the corresponding MED MESH
80     int myDim; //! Dimension of the corresponding MED MESH
81
82     TMesh(): myDim(0)
83     {}
84   };
85   typedef std::map<std::string,PMesh> TMeshMap;
86
87
88   //---------------------------------------------------------------
89   //! Define a basic class which corresponds to MED PROFILE entity
90   struct TSubProfile: virtual TBaseStructure
91   {};
92
93
94   //---------------------------------------------------------------
95   //! Define a containerfor MED PROFILE entities which belongs to the same MED ENTITY
96   struct TProfile: virtual TNamedIDMapper
97   {};
98
99
100   //---------------------------------------------------------------
101   bool
102   operator<(const PSubProfile& theLeft, const PSubProfile& theRight);
103
104   typedef std::set<PSubProfile> TProfileKey;
105   typedef std::map<TProfileKey,PProfile> TProfileMap;
106
107
108   //---------------------------------------------------------------
109   //! Define a basic class for MED GAUSS entity
110   struct TGauss: virtual TBaseStructure
111   {};
112
113
114   //---------------------------------------------------------------
115   //! Define a container for mesh generated from MED GAUSS and corresponding MED PROFILE
116   struct TGaussSubMesh: virtual TBaseStructure
117   {
118     PSubProfile mySubProfile; //!< Keeps reference on what submesh the Gauss Points are located
119   };
120
121   
122   //---------------------------------------------------------------
123   //! Define a container for all TGaussSubMesh that belongs to the same MED ENTITY
124   struct TGaussMesh: virtual TGaussPtsIDMapper
125   {};
126
127
128   //---------------------------------------------------------------
129   bool
130   operator<(const PGaussSubMesh& theLeft, const PGaussSubMesh& theRight);
131
132   typedef std::set<PGaussSubMesh> TGaussKey;
133   typedef std::map<TGaussKey,PGaussMesh> TGaussMeshMap;
134
135
136   //---------------------------------------------------------------
137
138   typedef std::map<TName,PFamily> TFamilyMap;
139   typedef std::map<TName,PField> TFieldMap;
140
141   //! Define a basic class which corresponds to MED ENTITY
142   /*!
143     This class in its turn contains map of TGaussMesh and TProfile substructures,
144     also it keeps corresponding map of MED FAMILIES and FIELDS.
145   */
146   struct TMeshOnEntity: virtual TNamedIDMapper
147   {
148     TGaussMeshMap myGaussMeshMap; //!< Contains map of Gauss mesh which exist on it
149     TProfileMap myProfileMap; //!< Contains map of Profile mesh which exist on it
150
151     TFamilyMap myFamilyMap; //!< Contains map of MED FAMILIES which belongs to it
152     TFieldMap myFieldMap; //!< Contains map of MED FIELDS which belongs to it
153
154     TName myMeshName; //!< Contains name of the MED MESH where the it belongs to.
155     TEntity myEntity; //!< Referes to MED ENTITY where the it belongs to.
156   };
157
158
159   //---------------------------------------------------------------
160   //! Define a basic class for MED FAMILY entity
161   struct TFamily: virtual TIntId,
162                   virtual TIDMapper
163   {
164     TEntity myEntity; //!< Referes to MED ENTITY where the TFamily belongs to.
165     TName myName; //!< Contains name of the corresponding MED FAMILY
166   };
167
168
169   //---------------------------------------------------------------
170   typedef std::set<PFamily> TFamilySet;
171
172   //! Define a basic class for MED GROUP entity
173   struct TGroup: virtual TIDMapper
174   {
175     TFamilySet myFamilySet;
176   };
177
178
179   //---------------------------------------------------------------
180   typedef std::map<vtkIdType,PValForTime> TValField;
181   typedef std::pair<float,float> TMinMax;
182
183   //! Define a basic class for MED FIELD entity
184   struct TField: virtual TIntId
185   {
186     TEntity myEntity; //!< Referes to MED ENTITY where it belongs to.
187     TName myName; //!< Contains name of the corresponding MED FIELD
188     TName myMeshName; //!< Contains name of the MED MESH where it belongs to.
189     TValField myValField; //!< Contains sequence of values for corresponding MED TIMESTAMPS
190     TNames myCompNames; //!< Contains names of components of the MED FIELD
191     TNames myUnitNames; //!< Contains names of units of the MED FIELD
192     vtkIdType myNbComp; //!< Keeps number of components for the MED FIELD
193
194     //! Calculate min/max values for each of the MED FIELD components among all its timestamps
195     /*!
196       Numeration of the components starts from 1.
197       Zero component contains min/max value for modulus of corresponding vector
198     */
199     virtual
200     TMinMax 
201     GetMinMax(vtkIdType theCompID) = 0;
202     
203     bool myIsMinMaxInitilized; //!< Is the min / max values are calculated
204
205     TField(): 
206       myNbComp(0),
207       myIsMinMaxInitilized(false)
208     {}
209   };
210  
211
212   //---------------------------------------------------------------
213   typedef std::pair<double,std::string> TTime;
214
215   //! Define a basic class for MED TIMESTAMP entity
216   struct TValForTime: virtual TIntId
217   {
218     TEntity myEntity; //!< Referes to MED ENTITY where it belongs to.
219     TName myMeshName; //!< Contains name of the MED MESH where it belongs to.
220     TName myFieldName; //!< Contains name of the MED FIELD where it belongs to.
221     TTime myTime;
222
223     PProfile myProfile; //!< Contains corresponding MED PROFILE where the MED TIEMSTMAP attached to
224     PGaussMesh myGaussMesh;
225   };
226
227
228   //---------------------------------------------------------------
229   //! The utility function allows to write vtkUnstructuredGrid to a file with defined name
230   void 
231   WriteToFile(vtkUnstructuredGrid* theDataSet, 
232               const std::string& theFileName);
233 };
234
235
236 //---------------------------------------------------------------
237 //! This class defines interface to read VTK interpretation of MED entities
238 /*!
239   It is the main class of the VISU CONVERTER package.
240   Following MED entities can be retrived from any MED file:
241   - mesh from corresponding MED ENTITIES;
242   - MED TIMESTAMPS;
243   - MED FAMILIES;
244   - MED GROUPS.
245   The class produce its work in two main steps:
246   1. Perfrom parsing of MED file to get known what MED entities are pressent in it
247   2. Get VTK representation for any existing MED entity
248   Also, it can perform some additional work to calculate expected amount of memory to build defined VTK representation
249   
250 */
251 class VISU_Convertor
252 {
253 protected:
254   std::string myName;
255   VISU::TMeshMap myMeshMap;
256   int myIsDone;
257
258 public:
259   virtual 
260   ~VISU_Convertor()
261   {};
262   
263   //! Get brief name of the corresponding source MED file
264   virtual
265   const std::string& 
266   GetName(){ return myName;}
267
268   //! Let known whether the source MED file parsed or not
269   virtual
270   int
271   IsDone() const { return myIsDone; }
272
273   //! Defines what subtype of vtkDataSet is used for MED to VTK mapping
274   typedef VISU::TVTKOutput TOutput;
275
276   //! This method perform first parsing of MED file to get known what MED entities are pressent in it
277   virtual
278   VISU_Convertor* 
279   Build() = 0;
280
281   //! This method perform first parsing of MED file to get known what MED mesh entities are pressent in it
282   virtual
283   VISU_Convertor* 
284   BuildEntities() = 0;
285
286   //! This method perform first parsing of MED file to get known what MED fields are pressent in it
287   virtual
288   VISU_Convertor* 
289   BuildFields() = 0;
290
291   //! This min /max calculation over existing MED fields
292   virtual
293   VISU_Convertor* 
294   BuildMinMax() = 0;
295
296   //! This method perform first parsing of MED file to get known what MED groups are pressent in it
297   virtual
298   VISU_Convertor* 
299   BuildGroups() = 0;
300
301   //! This method allow to get known what MED entities are present is the MED file
302   virtual
303   const VISU::TMeshMap& 
304   GetMeshMap();
305
306   //! Get amount of memory to build VTK representations for all existing MED entities
307   virtual
308   float
309   GetSize() = 0;
310
311   //! Get mesh for corresponding MED ENTITY
312   virtual
313   VISU::PNamedIDMapper 
314   GetMeshOnEntity(const std::string& theMeshName, 
315                   const VISU::TEntity& theEntity) = 0;
316   
317   //! Get amount of memory to build mesh for corresponding MED ENTITY
318   virtual
319   float
320   GetMeshOnEntitySize(const std::string& theMeshName, 
321                       const VISU::TEntity& theEntity) = 0;
322   
323   //! Get mesh for corresponding MED FAMILY
324   virtual 
325   VISU::PIDMapper 
326   GetFamilyOnEntity(const std::string& theMeshName, 
327                     const VISU::TEntity& theEntity,
328                     const std::string& theFamilyName) = 0;
329
330   //! Get amount of memory to build mesh for corresponding MED FAMILY
331   virtual 
332   float 
333   GetFamilyOnEntitySize(const std::string& theMeshName, 
334                         const VISU::TEntity& theEntity,
335                         const std::string& theFamilyName) = 0;
336
337   //! Get mesh for corresponding MED GROUP
338   virtual
339   VISU::PIDMapper
340   GetMeshOnGroup(const std::string& theMeshName, 
341                  const std::string& theGroupName) = 0;
342   
343   //! Get amount of memory to build mesh for corresponding MED GROUP
344   virtual
345   float
346   GetMeshOnGroupSize(const std::string& theMeshName, 
347                      const std::string& theGroupName) = 0;
348   
349   //! Get mesh with attached values for corresponding MED TIMESTAMP
350   virtual
351   VISU::PIDMapper 
352   GetTimeStampOnMesh(const std::string& theMeshName, 
353                      const VISU::TEntity& theEntity,
354                      const std::string& theFieldName,
355                      int theStampsNum) = 0;
356
357   //! Get Gauss Points mesh with attached values for corresponding MED TIMESTAMP
358   virtual
359   VISU::PGaussPtsIDMapper 
360   GetTimeStampOnGaussPts(const std::string& theMeshName, 
361                          const VISU::TEntity& theEntity,
362                          const std::string& theFieldName,
363                          int theStampsNum) = 0;
364    
365   //! Get amount of memory to build mesh for corresponding MED TIMESTAMP
366   virtual 
367   float
368   GetTimeStampSize(const std::string& theMeshName, 
369                    const VISU::TEntity& theEntity,
370                    const std::string& theFieldName,
371                    int theStampsNum) = 0;
372     
373   //! Get amount of memory to build all MED TIMESTAMPS for corresponding MED FIELD
374   virtual 
375   float
376   GetFieldOnMeshSize(const std::string& theMeshName, 
377                      const VISU::TEntity& theEntity,
378                      const std::string& theFieldName) = 0;
379   
380   //! Find MED FIELD container
381   virtual
382   const VISU::PField 
383   GetField(const std::string& theMeshName, 
384            VISU::TEntity theEntity, 
385            const std::string& theFieldName) = 0;
386   
387   //! Find MED TIMESTAMP container
388   virtual
389   const VISU::PValForTime 
390   GetTimeStamp(const std::string& theMeshName, 
391                const VISU::TEntity& theEntity,
392                const std::string& theFieldName,
393                int theStampsNum) = 0;
394     
395   //! Allow to generate pretty name for MED TIMESTAMP
396   static 
397   std::string 
398   GenerateName(const VISU::TTime& aTime);
399
400   static 
401   std::string 
402   GenerateName(const std::string& theName, unsigned int theTimeId);
403 };
404
405 extern "C"
406 {
407   //! Instatiate proper VISU_Convertor subclass
408   VISU_Convertor* 
409   CreateConvertor(const std::string& theFileName);
410 };
411
412
413 #endif