1 #include "MEDMEM_VtkMedDriver.hxx"
3 #include "MEDMEM_Med.hxx"
5 VTK_MED_DRIVER::VTK_MED_DRIVER(): GENDRIVER(),
6 _ptrMed((MED * const)MED_NULL),
9 // What about _id in Gendriver ?
14 VTK_MED_DRIVER::VTK_MED_DRIVER(const string & fileName, MED * const ptrMed):
15 GENDRIVER(fileName,MED_EN::MED_RDWR), _ptrMed(ptrMed), _vtkFile(MED_INVALID)
17 // What about _id in Gendriver ?
21 //REM : As t'on besoin du champ _status : _vtkFile <-> _status ? Oui
24 void VTK_MED_DRIVER::open() {
26 const char * LOC ="VTK_MED_DRIVER::open() : ";
29 // REFLECHIR SUR CE TEST PAR RAPPORT A L'OUVERTURE/FERMETURE
30 // if ( _vtkFile != MED_INVALID )
31 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
32 // << "_vtkFile is already in use, please close the file |"
33 // << _fileName << "| before calling open()"
37 if ( _status != MED_CLOSED )
38 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
39 << "_status is not closed, please close the file |"
40 << _fileName << "| before calling open()"
44 if ( _fileName == "" )
45 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
46 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
49 _vtkFile.open(filename.c_str()) ; // ? if error ????
50 _status = MED_OPENED ;
52 // if (_vtkFile > 0) _status=MED_OPENED;
54 // _status = MED_CLOSED;
55 // _vtkFile = MED_INVALID;
56 // throw MED_EXCEPTION (LOCALIZED( STRING(LOC)
57 // << "Can't open |" << _fileName
58 // << "|, _vtkFile : " << _vtkFile
67 void VTK_MED_DRIVER::close() {
69 const char * LOC = "MED_MED_DRIVER::close() : ";
72 if ( _status == MED_CLOSED)
73 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << ": the file |"
74 << _fileName << "| is already closed"
78 // if ( _vtkFile == MED_INVALID )
79 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "_vtkFile invalid, but the file |"
80 // << _fileName << "| seems to be openned !"
86 // _vtkFile = MED_INVALID;
88 // if (err != MED_VALID)
89 // throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "the file |"
90 // << _fileName << "| couldn't be closed"
98 void VTK_MED_DRIVER::write() {
100 const char * LOC = "MED_MED_DRIVER::write() : ";
103 // could we put more than one Mesh ?????
104 _vtkFile << "# vtk DataFile Version 2.0" << endl
105 << "maillage SALOLME" << endl ;
106 // only ASCII for the moment (binary came latest :-)
107 _vtkFile << "ASCII" << endl ;
109 int NumberOfMeshes = _ptrMED->getNumberOfMeshes() ;
110 string * MeshName = new string[NumberOfMeshes] ;
111 _ptrMED->getMeshNames(MeshName) ;
112 // In fact, we must take care of all supports
113 // We restrict Field on all nodes or cells
114 for (int i=0; i<NumberOfMeshes; i++) {
115 MESH * myMesh = _ptrMED->getMesh(MeshName[i]) ;
117 // get all field which values are on this mesh => revoir api de Med !!!
118 _vtkFile << "NODE" << endl ;
119 // first : field on node
120 for (int j=0; j<NumberOfFields; j++) {
121 FIELD_ * myField = _ptrMED->getField() ;
122 if (myField->getSupport()->getEntity()!=MED_NODE)
123 if (myField->getSupport()->isOnAllElements())
124 writeField(myField) ;
127 _vtkFile << "CELL" << endl ;
128 // second : field on cell
129 for (int j=0; j<NumberOfFields; j++) {
130 FIELD_ * myField = _ptrMED->getField() ;
131 if (myField->getSupport()->getEntity()!=MED_CELL)
132 if (myField->getSupport()->isOnAllElements())
133 writeField(myField) ;
142 void VTK_MED_DRIVER::writeMesh(MESH * myMesh) {
144 const char * LOC = "MED_MED_DRIVER::writeMesh() : ";
147 _vtkFile << "DATASET UNSTRUCTURED_GRID" << endl ;
148 // put points (all point are in 3D, so if we are in 1D or 2D, we complete by zero !
149 int SpaceDimension = myMesh->getSpaceDimension() ;
150 int NumberOfNodes = myMesh->getNumberOfNodes() ;
151 _vtkFile << "POINTS " << NumberOfNodes << " float" << endl ;
152 double *coordinate = myMesh->getCoordinates(MED_FULL_ENTERLACE) ;
153 if (SpaceDimension<3) { // 1D or 2D
154 coordinate_z = new double[NumberOfNodes] ;
156 for (int i=0;i<NumberOfNodes;i++)
157 coordinate_z[i] = 0.0 ;
158 if (SpaceDimension==1)
159 coordinate_y = coordinate_z ; // only one array of zero !
161 coordinate_y = coordinate_x + NumberOfNodes ;
163 coordinate_y = coordinate_x + NumberOfNodes ;
164 coordinate_z = coordinate_y + NumberOfNodes ;
166 for (int i=0;i<NumberOfNodes;i++)
167 for (int j=0;j<SpaceDimension;j++)
168 _vtkFile << coordinate[i*SpaceDimension+j] << " " ;
169 if (SpaceDimension==1)
171 if (SpaceDimension==2)
175 // we put connectivity
176 // how many cells and how many value in connectivity :
177 int cells_types_count = myMesh->getNumberOfTypes(MED_CELL) ;
178 int * cells_count = myMesh->get_cells_count() ;
179 int cells_sum = cells_count[cells_types_count] ;
180 CellModel * cells_type = myMesh->get_cells_type() ;
181 int connectivity_sum = 0 ;
183 int * connectivity = myMesh->getConnectivity(MED_FULL_ENTERLACE,MED_CELL,MED_ALL_ELEMENTS) ;
184 int * connectivityIndex = myMesh->getConnectivityIndex(MED_CELL) ;
186 for (int i=0;i<cells_types_count;i++) {
187 int nodes_cell = cells_type[i].getNumberOfNodes();
188 connectivity_sum+= (cells_count[i+1]-cells_count[i])*(nodes_cell + 1);
189 // we add 1 because we put nodes count in vtk file !
191 _vtkFile << "CELLS " << cells_sum << " " << connectivity_sum << endl ;
192 // we put connectivity
193 for (int i=0;i<cells_types_count;i++) {
194 int *filter = (int*) NULL ; // index in vtk connectivity
195 switch (cells_type[i].get_type())
198 filter = new int[1] ;
203 filter = new int[2] ;
212 filter = new int[3] ;
219 filter = new int[4] ;
233 filter = new int[4] ;
236 filter[2] = 3 ; // 3td element in med are 4th in vtk (array begin at 0 !)
237 filter[3] = 2 ; // 4th element in med are 3rd in vtk (array begin at 0 !)
241 filter = new int[5] ;
243 filter[1] = 3 ; // 2nd element in med are 4th in vtk (array begin at 0 !)
245 filter[3] = 1 ; // 4th element in med are 2nd in vtk (array begin at 0 !)
250 filter = new int[6] ;
260 filter = new int[8] ;
288 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].get_type() ) ) ;
289 int nodes_cell = cells_type[i].get_NumberOfNodes();
290 for (int j=0;j<cells_count[i+1]-cells_count[i];j++) {
291 _vtkFile << nodes_cell << " " ;
292 for (int k=0;k<nodes_cell;k++)
293 _vtkFile << (myMesh->get_nodal_connectivity(i+1))[j*nodes_cell+filter[k]] - 1 << " " ;
299 _vtkFile << "CELL_TYPES " << cells_sum << endl ;
300 for (int i=0;i<cells_types_count;i++) {
302 switch (cells_type[i].get_type())
370 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].get_type() ) ) ;
371 for (int j=0;j<cells_count[i+1]-cells_count[i];j++)
372 _vtkFile << vtkType << endl ;
377 // add a constant field on all node to test !
378 // _vtkFile << "POINT_DATA " << NumberOfNodes << endl ;
379 // _vtkFile << "SCALARS example_scalaire float 1" << endl ;
380 // _vtkFile << "LOOKUP_TABLE default" << endl ;
381 // for (int i=0;i<NumberOfNodes;i++)
382 // _vtkFile << i << endl ;
390 void VTK_MED_DRIVER::writeField(FIELD * myField) {
392 const char * LOC = "MED_MED_DRIVER::writeField() : ";