1 #include "MEDMEM_VtkMeshDriver.hxx"
3 #include "MEDMEM_DriversDef.hxx"
5 #include "MEDMEM_Family.hxx"
6 #include "MEDMEM_Group.hxx"
7 #include "MEDMEM_Coordinate.hxx"
8 #include "MEDMEM_Connectivity.hxx"
9 #include "MEDMEM_Mesh.hxx"
10 #include "MEDMEM_CellModel.hxx"
11 #include "MEDMEM_Grid.hxx"
16 using namespace MEDMEM;
17 using namespace MED_EN;
19 VTK_MESH_DRIVER::VTK_MESH_DRIVER(): GENDRIVER(),
20 _ptrMesh((MESH * const)MED_NULL)
22 _vtkFile = new ofstream();
23 // What about _id in Gendriver ?
27 VTK_MESH_DRIVER::VTK_MESH_DRIVER(const string & fileName,
29 GENDRIVER(fileName,MED_WRONLY),
32 const char * LOC = "VTK_MESH_DRIVER::VTK_MESH_DRIVER(const string & fileName, MESH * ptrMesh) : " ;
34 // Send an exception because a VTK_MESH_DRIVER object cannot be instantied
35 // from a file and there is no read for that kind of driver
37 // 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!"));
39 // _ptrMesh->addDriver(*this); // OU RECUPERER L'ID.
40 MESSAGE(LOC << "WARNING this driver is only used to write in VTK format So the object can not be instantied using a file!");
42 _vtkFile = new ofstream();
45 VTK_MESH_DRIVER::VTK_MESH_DRIVER(const VTK_MESH_DRIVER & driver):
47 _ptrMesh(driver._ptrMesh),
48 _meshName(driver._meshName)
50 _ptrMesh->addDriver(*this);
51 _vtkFile = new ofstream();
54 VTK_MESH_DRIVER::~VTK_MESH_DRIVER()
56 const char * LOC ="VTK_MESH_DRIVER::~VTK_MESH_DRIVER()";
70 void VTK_MESH_DRIVER::openConst() const throw (MEDEXCEPTION)
72 const char * LOC = "VTK_MESH_DRIVER::openConst()" ;
75 MESSAGE(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
77 if ( _fileName == "" )
78 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
79 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
83 if (!(*_vtkFile).is_open())
84 (*_vtkFile).open(_fileName.c_str()) ;
86 // _status = MED_OPENED ;
90 SCRUTE((*_vtkFile).is_open());
96 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
102 void VTK_MESH_DRIVER::open() {
106 void VTK_MESH_DRIVER::closeConst() const throw (MEDEXCEPTION)
108 const char * LOC = "VTK_MESH_DRIVER::closeConst() " ;
115 if ((*_vtkFile).is_open())
119 // _status = MED_CLOSED ;
126 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not close file "
133 void VTK_MESH_DRIVER::close() {
137 void VTK_MESH_DRIVER::setMeshName(const string & meshName) { _meshName = meshName; };
138 string VTK_MESH_DRIVER::getMeshName() const { return _meshName; };
140 void VTK_MESH_DRIVER::read(void) throw (MEDEXCEPTION)
142 const char * LOC = "VTK_MESH_DRIVER::read() : " ;
147 throw MEDEXCEPTION(LOCALIZED(STRING(LOC) << "This driver is only used to write in VTK format !"));
153 void VTK_MESH_DRIVER::write(void) const
156 const char * LOC = "void VTK_MESH_DRIVER::write(void) const : ";
159 // Well we must open vtk file first, because there are
160 // no other driver than MED for VTK that do it !
164 (*_vtkFile) << "# vtk DataFile Version 2.0" << endl
165 << "maillage from MedMemory" << endl ;
166 // only ASCII for the moment (binary came later :-)
167 (*_vtkFile) << "ASCII" << endl ;
170 (*_vtkFile) << "DATASET UNSTRUCTURED_GRID" << endl ;
171 // put points (all point are in 3D, so if we are in 1D or 2D, we complete by zero !
172 int SpaceDimension = _ptrMesh->getSpaceDimension() ;
173 int NumberOfNodes = _ptrMesh->getNumberOfNodes() ;
174 (*_vtkFile) << "POINTS " << NumberOfNodes << " float" << endl ;
175 const double *coordinate = _ptrMesh->getCoordinates(MED_FULL_INTERLACE) ;
176 for (int i=0;i<NumberOfNodes;i++) {
177 for (int j=0;j<SpaceDimension;j++)
178 (*_vtkFile) << coordinate[i*SpaceDimension+j] << " " ;
179 if (SpaceDimension==1)
180 (*_vtkFile) << "0 0" ;
181 if (SpaceDimension==2)
183 (*_vtkFile) << endl ;
186 // we put connectivity
187 // how many cells and how many value in connectivity :
188 int cells_types_count = _ptrMesh->getNumberOfTypes(MED_CELL) ;
189 // int * cells_count = _ptrMesh->get_cells_count() ;
190 // int cells_sum = cells_count[cells_types_count] ;
191 int cells_sum = _ptrMesh->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) ;
192 const CELLMODEL * cells_type = _ptrMesh->getCellsTypes(MED_CELL) ;
193 // int connectivity_sum = 0 ;
195 //const int * connectivity = _ptrMesh->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,MED_ALL_ELEMENTS) ; !! UNUSED VARIABLE !!
196 const int * connectivityIndex = _ptrMesh->getConnectivityIndex(MED_NODAL,MED_CELL) ;
198 int connectivity_sum = connectivityIndex[cells_sum]-1 ;
200 (*_vtkFile) << "CELLS " << cells_sum << " " << connectivity_sum+cells_sum << endl ;
201 // we put connectivity
202 for (int i=0;i<cells_types_count;i++) {
203 int *filter = (int*) NULL ; // index in vtk connectivity
204 switch (cells_type[i].getType())
207 filter = new int[1] ;
212 filter = new int[2] ;
221 filter = new int[3] ;
228 filter = new int[4] ;
242 filter = new int[4] ;
245 filter[2] = 3 ; // 3td element in med are 4th in vtk (array begin at 0 !)
246 filter[3] = 2 ; // 4th element in med are 3rd in vtk (array begin at 0 !)
250 filter = new int[5] ;
252 filter[1] = 3 ; // 2nd element in med are 4th in vtk (array begin at 0 !)
254 filter[3] = 1 ; // 4th element in med are 2nd in vtk (array begin at 0 !)
259 filter = new int[6] ;
269 filter = new int[8] ;
297 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getName() ) ) ;
298 int nodes_cell = cells_type[i].getNumberOfNodes();
299 int numberOfCell = _ptrMesh->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
300 const int * connectivityArray = _ptrMesh->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,cells_type[i].getType());
301 for (int j=0;j<numberOfCell;j++) {
302 (*_vtkFile) << nodes_cell << " " ;
303 for (int k=0;k<nodes_cell;k++)
304 (*_vtkFile) << connectivityArray[j*nodes_cell+filter[k]] - 1 << " " ;
305 (*_vtkFile) << endl ;
310 (*_vtkFile) << endl ;
312 (*_vtkFile) << "CELL_TYPES " << cells_sum << endl ;
313 for (int i=0;i<cells_types_count;i++) {
315 switch (cells_type[i].getType())
383 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getType() ) ) ;
384 int numberOfCell = _ptrMesh->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
385 for (int j=0;j<numberOfCell;j++)
386 (*_vtkFile) << vtkType << endl ;
393 GENDRIVER * VTK_MESH_DRIVER::copy(void) const
395 return new VTK_MESH_DRIVER(*this);