]> SALOME platform Git repositories - modules/med.git/blob - src/MEDMEM/MEDMEM_VtkMeshDriver.cxx
Salome HOME
Merge from BR_V5_DEV 16Feb09
[modules/med.git] / src / MEDMEM / MEDMEM_VtkMeshDriver.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "MEDMEM_VtkMeshDriver.hxx"
23
24 #include "MEDMEM_DriversDef.hxx"
25
26 #include "MEDMEM_Family.hxx"
27 #include "MEDMEM_Group.hxx"
28 #include "MEDMEM_Coordinate.hxx"
29 #include "MEDMEM_Connectivity.hxx"
30 #include "MEDMEM_Mesh.hxx"
31 #include "MEDMEM_CellModel.hxx"
32 #include "MEDMEM_Grid.hxx"
33
34 #include <sstream>
35
36 using namespace std;
37 using namespace MEDMEM;
38 using namespace MED_EN;
39
40 #define MED_NULL     NULL
41
42 VTK_MESH_DRIVER::VTK_MESH_DRIVER(): GENDRIVER(VTK_DRIVER), 
43                                     _ptrMesh((MESH * const)MED_NULL)
44 {
45   _vtkFile = new ofstream();
46   // What about _id in Gendriver ?
47   // _driverType ???
48 }
49
50 VTK_MESH_DRIVER::VTK_MESH_DRIVER(const string & fileName,
51                                  MESH * ptrMesh) :
52   GENDRIVER(fileName, WRONLY, VTK_DRIVER),
53   _ptrMesh(ptrMesh)
54 {
55
56   // Send an exception because a VTK_MESH_DRIVER object cannot be instantied
57   // from a file and there is no read for that kind of driver
58
59   //  throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "This driver is only used to write in VTK format So thie object can not be instantied using a file!"));
60
61   //  _ptrMesh->addDriver(*this); // OU RECUPERER L'ID.
62   MESSAGE_MED("VTK_MESH_DRIVER::VTK_MESH_DRIVER(const string & fileName, MESH * ptrMesh) : "
63           << "WARNING this driver is only used to write in VTK format So the object can not be instantied using a file!");
64
65   _vtkFile = new ofstream(); 
66 }
67
68 VTK_MESH_DRIVER::VTK_MESH_DRIVER(const VTK_MESH_DRIVER & driver): 
69   GENDRIVER(driver),
70   _ptrMesh(driver._ptrMesh),
71   _meshName(driver._meshName)
72 {
73   // next string commented by skl for bug NPAL14840
74   //_ptrMesh->addDriver(*this);
75   _vtkFile = new ofstream();
76 }
77
78 VTK_MESH_DRIVER::~VTK_MESH_DRIVER()
79 {
80   const char* LOC = "VTK_MESH_DRIVER::~VTK_MESH_DRIVER()";
81   BEGIN_OF_MED(LOC);
82
83   close();
84
85   SCRUTE_MED(_vtkFile);
86
87   delete _vtkFile ;
88
89   SCRUTE_MED(_vtkFile);
90
91   END_OF_MED(LOC);
92 }
93
94 void VTK_MESH_DRIVER::openConst() const throw (MEDEXCEPTION)
95 {
96   const char * LOC = "VTK_MESH_DRIVER::openConst()" ;
97   BEGIN_OF_MED(LOC);
98
99   MESSAGE_MED(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
100
101   if ( _fileName == "" )
102     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
103                                      << "_fileName is |\"\"|, please set a correct fileName before calling open()"
104                                      )
105                           );
106
107   if (!(*_vtkFile).is_open())
108     (*_vtkFile).open(_fileName.c_str()) ; 
109 //    if (*_vtkFile)
110 //      _status = MED_OPENED ;
111 //    else
112
113
114   SCRUTE_MED((*_vtkFile).is_open());
115   SCRUTE_MED(_vtkFile);
116
117
118
119   if (!(*_vtkFile))
120     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
121                                      << _fileName)
122                           );
123   END_OF_MED(LOC);
124 }
125
126 void VTK_MESH_DRIVER::open() {
127   openConst() ;
128 }
129
130 void VTK_MESH_DRIVER::closeConst() const throw (MEDEXCEPTION)
131 {
132   const char * LOC = "VTK_MESH_DRIVER::closeConst() " ;
133   BEGIN_OF_MED(LOC);
134
135   SCRUTE_MED(_vtkFile);
136   SCRUTE_MED(*_vtkFile);
137
138
139   if ((*_vtkFile).is_open())
140     (*_vtkFile).close();
141   
142 //    if (*_vtkFile)
143 //      _status = MED_CLOSED ;
144 //    else
145
146   SCRUTE_MED(_vtkFile);
147   SCRUTE_MED(*_vtkFile);
148   SCRUTE_MED(_vtkFile->is_open());
149
150   if ( (*_vtkFile) && _vtkFile->is_open() )
151     throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not close file "
152                                      << _fileName)
153                           );
154
155   END_OF_MED(LOC);
156 }
157
158 void VTK_MESH_DRIVER::close() {
159   closeConst() ;
160 }
161
162 void    VTK_MESH_DRIVER::setMeshName(const string & meshName) { _meshName = meshName; };
163 string  VTK_MESH_DRIVER::getMeshName() const { return _meshName; };
164
165 void VTK_MESH_DRIVER::read(void) throw (MEDEXCEPTION)
166 {
167   const char * LOC = "VTK_MESH_DRIVER::read() : " ;
168   BEGIN_OF_MED(LOC);
169
170   // Send an exception
171
172   throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "This driver is only used to write in VTK format !"));
173
174   END_OF_MED(LOC);
175 }
176
177
178 void VTK_MESH_DRIVER::write(void) const
179   throw (MEDEXCEPTION)
180
181   const char * LOC = "void VTK_MESH_DRIVER::write(void) const : ";
182   BEGIN_OF_MED(LOC);
183
184   // Well we must open vtk file first, because there are
185   // no other driver than MED for VTK that do it !
186
187   openConst();
188
189   int SpaceDimension = _ptrMesh->getSpaceDimension() ;
190   int NumberOfNodes = _ptrMesh->getNumberOfNodes() ;
191   if ( SpaceDimension < 1 )
192     throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "Mesh is empty"));
193
194   (*_vtkFile) << "# vtk DataFile Version 2.0" << endl 
195            << "maillage from MedMemory"  << endl ;
196   // only ASCII for the moment (binary came later :-)
197   (*_vtkFile) << "ASCII" << endl ;
198
199
200   (*_vtkFile) << "DATASET UNSTRUCTURED_GRID" << endl ;
201   // put points (all point are in 3D, so if we are in 1D or 2D, we complete by zero !
202   (*_vtkFile) << "POINTS " << NumberOfNodes << " float" << endl ;
203   const double *coordinate = _ptrMesh->getCoordinates(MED_FULL_INTERLACE) ;
204   for (int i=0;i<NumberOfNodes;i++) {
205     for (int j=0;j<SpaceDimension;j++)
206       (*_vtkFile) << coordinate[i*SpaceDimension+j] << " " ;
207     if (SpaceDimension==1) 
208       (*_vtkFile) << "0 0" ;
209     if (SpaceDimension==2) 
210       (*_vtkFile) << "0" ;
211     (*_vtkFile) << endl ;
212   }
213
214   // we put connectivity
215   // how many cells and how many value in connectivity :
216   int cells_types_count = _ptrMesh->getNumberOfTypes(MED_CELL) ;
217   //  int * cells_count = _ptrMesh->get_cells_count() ;
218   //  int cells_sum = cells_count[cells_types_count] ;
219   int cells_sum = _ptrMesh->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) ;
220   const CELLMODEL * cells_type = _ptrMesh->getCellsTypes(MED_CELL) ;
221   //  int connectivity_sum = 0 ;
222
223   //const int * connectivity = _ptrMesh->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,MED_ALL_ELEMENTS) ; !! UNUSED VARIABLE !!
224   const int * connectivityIndex = _ptrMesh->getConnectivityIndex(MED_NODAL,MED_CELL) ;
225
226   int connectivity_sum =  connectivityIndex[cells_sum]-1 ;
227
228   (*_vtkFile) << "CELLS " << cells_sum << " " << connectivity_sum+cells_sum << endl ;
229   // we put connectivity
230   for (int i=0;i<cells_types_count;i++) {
231     int *filter = (int*) NULL ; // index in vtk connectivity
232     switch (cells_type[i].getType())
233       {
234       case MED_POINT1  : {
235         filter = new int[1] ;
236         filter[0] = 0 ;
237         break ;
238       }
239       case MED_SEG2    : {
240         filter = new int[2] ;
241         filter[0] = 0 ;
242         filter[1] = 1 ;
243         break ;
244       }
245       case MED_SEG3    : {  
246         break ;
247       }
248       case MED_TRIA3   : {
249         filter = new int[3] ;
250         filter[0] = 0 ;
251         filter[1] = 1 ;
252         filter[2] = 2 ;
253         break ;
254       }
255       case MED_QUAD4   : {
256         filter = new int[4] ;
257         filter[0] = 0 ;
258         filter[1] = 1 ;
259         filter[2] = 2 ;
260         filter[3] = 3 ;
261         break ;
262       }
263       case MED_TRIA6   : {
264         break ;
265       }
266       case MED_QUAD8   : {
267         break ;
268       }
269       case MED_TETRA4  : {
270         filter = new int[4] ;
271         filter[0] = 0 ;
272         filter[1] = 1 ;
273         filter[2] = 3 ;  // 3td element in med are 4th in vtk (array begin at 0 !)
274         filter[3] = 2 ;  // 4th element in med are 3rd in vtk (array begin at 0 !)
275         break ;
276       }
277       case MED_PYRA5   : {
278         filter = new int[5] ;
279         filter[0] = 0 ;
280         filter[1] = 3 ;  // 2nd element in med are 4th in vtk (array begin at 0 !)
281         filter[2] = 2 ;
282         filter[3] = 1 ;  // 4th element in med are 2nd in vtk (array begin at 0 !)
283         filter[4] = 4 ;
284         break ;
285       }
286       case MED_PENTA6  : {
287         filter = new int[6] ;
288         filter[0] = 0 ;
289         filter[1] = 1 ;
290         filter[2] = 2 ;
291         filter[3] = 3 ;
292         filter[4] = 4 ;
293         filter[5] = 5 ;
294         break ;
295       }
296       case MED_HEXA8   : {
297         filter = new int[8] ;
298         filter[0] = 0 ;
299         filter[1] = 3 ;
300         filter[2] = 2 ;
301         filter[3] = 1 ;
302         filter[4] = 4 ;
303         filter[5] = 7 ;
304         filter[6] = 6 ;
305         filter[7] = 5 ;
306         break ;
307       }
308       case MED_TETRA10 : {
309         break ;
310       }
311       case MED_PYRA13  : {
312         break ;
313       }
314       case MED_PENTA15 : {
315         break ;
316       }
317       case MED_HEXA20  : {
318         break ;
319       }
320       default : { 
321         break ;
322       }
323       }
324     if (filter==NULL) 
325       throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getName() ) ) ;
326     int nodes_cell = cells_type[i].getNumberOfNodes();
327     int numberOfCell = _ptrMesh->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
328     const int * connectivityArray = _ptrMesh->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,cells_type[i].getType());
329     for (int j=0;j<numberOfCell;j++) {
330       (*_vtkFile) << nodes_cell << " " ;
331       for (int k=0;k<nodes_cell;k++)
332         (*_vtkFile) << connectivityArray[j*nodes_cell+filter[k]] - 1 << " " ;
333       (*_vtkFile) << endl ;
334     }
335     if (filter != NULL)
336       delete[] filter ;
337   }
338   (*_vtkFile) << endl ;
339   // we put cells type
340   (*_vtkFile) << "CELL_TYPES " << cells_sum << endl ;
341   for (int i=0;i<cells_types_count;i++) {
342     int vtkType = 0 ;
343     switch (cells_type[i].getType())
344       {
345       case MED_POINT1  : {
346         vtkType = 1 ;
347         break ;
348       }
349       case MED_SEG2    : {
350         vtkType = 3 ;
351         break ;
352       }
353       case MED_SEG3    : {  
354         vtkType = 0 ;
355         break ;
356       }
357       case MED_TRIA3   : {
358         vtkType = 5 ;
359         break ;
360       }
361       case MED_QUAD4   : {
362         vtkType = 9 ;
363         break ;
364       }
365       case MED_TRIA6   : {
366         vtkType = 0 ;
367         break ;
368       }
369       case MED_QUAD8   : {
370         vtkType = 0 ;
371         break ;
372       }
373       case MED_TETRA4  : {
374         vtkType = 10 ;
375         break ;
376       }
377       case MED_PYRA5   : {
378         vtkType = 14 ;
379         break ;
380       }
381       case MED_PENTA6  : {
382         vtkType = 13 ;
383         break ;
384       }
385       case MED_HEXA8   : {
386         vtkType = 12 ;
387         break ;
388       }
389       case MED_TETRA10 : {
390         vtkType = 0 ;
391         break ;
392       }
393       case MED_PYRA13  : {
394         vtkType = 0 ;
395         break ;
396       }
397       case MED_PENTA15 : {
398         vtkType = 0 ;
399         break ;
400       }
401       case MED_HEXA20  : {
402         vtkType = 0 ;
403         break ;
404       }
405       default : { 
406         vtkType = 0 ;
407         break ;
408       }
409       }
410     if (vtkType == 0)
411       throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getType() ) ) ;
412     int numberOfCell = _ptrMesh->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
413     for (int j=0;j<numberOfCell;j++)
414       (*_vtkFile) << vtkType << endl ;
415   }
416
417   END_OF_MED(LOC);
418
419
420 GENDRIVER * VTK_MESH_DRIVER::copy(void) const
421 {
422   return new VTK_MESH_DRIVER(*this);
423 }