1 #ifndef VTK_FIELD_DRIVER_HXX
2 #define VTK_FIELD_DRIVER_HXX
8 #include "MEDMEM_define.hxx"
10 #include "MEDMEM_GenDriver.hxx"
11 #include "utilities.h"
13 #include "MEDMEM_STRING.hxx"
14 #include "MEDMEM_Exception.hxx"
15 #include "MEDMEM_Unit.hxx"
16 #include "MEDMEM_Array.hxx"
17 #include "MEDMEM_Support.hxx"
18 //#include "MEDMEM_Field.hxx"
19 #include "MEDMEM_Mesh.hxx"
20 #include "MEDMEM_CellModel.hxx"
22 using namespace MEDMEM ;
29 Generic part : implement open and close methods.
34 template <class T> class FIELD;
35 template <class T> class VTK_FIELD_DRIVER : public GENDRIVER
40 mutable ofstream * _vtkFile ;
46 // all MED cell type ?? Classe de Définition ??
47 // static const medGeometryElement all_cell_type[MED_NBR_GEOMETRIE_MAILLE];
49 // static const char * const all_cell_type_tab [MED_NBR_GEOMETRIE_MAILLE];
54 VTK_FIELD_DRIVER():GENDRIVER(),
55 _ptrField((FIELD<T> *)MED_NULL), _fieldName(""),
56 _fieldNum(MED_INVALID)
58 const char * LOC = "VTK_FIELD_DRIVER::VTK_FIELD_DRIVER() ";
61 _vtkFile = new ofstream();
68 VTK_FIELD_DRIVER(const string & fileName, FIELD<T> * ptrField)
69 : GENDRIVER(fileName,MED_WRONLY),
70 _ptrField((FIELD<T> *) ptrField),
71 _fieldName(fileName),_fieldNum(MED_INVALID)
73 const char * LOC = "VTK_FIELD_DRIVER::VTK_FIELD_DRIVER(const string & fileName, FIELD<T> * ptrField) ";
76 _vtkFile = new ofstream();
84 VTK_FIELD_DRIVER(const VTK_FIELD_DRIVER & fieldDriver):
85 GENDRIVER(fieldDriver),
86 _ptrField(fieldDriver._ptrField),
87 _fieldName(fieldDriver._fieldName),
88 _fieldNum(fieldDriver._fieldNum)
90 _ptrField->addDriver(*this);
91 _vtkFile = new ofstream();
99 const char * LOC ="VTK_FIELD_DRIVER::~VTK_FIELD_DRIVER()";
113 void openConst() const throw (MEDEXCEPTION)
115 const char * LOC = "VTK_FIELD_DRIVER::openConst()" ;
118 MESSAGE(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
120 if ( _fileName == "" )
121 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
122 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
126 if (!(*_vtkFile).is_open())
127 (*_vtkFile).open(_fileName.c_str()) ;
129 // _status = MED_OPENED ;
133 SCRUTE((*_vtkFile).is_open());
139 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
145 void openConstAppend() const throw (MEDEXCEPTION)
147 const char * LOC = "VTK_FIELD_DRIVER::openConstAppend()" ;
150 MESSAGE(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
152 if ( _fileName == "" )
153 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
154 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
158 SCRUTE((*_vtkFile).is_open());
160 if (!(*_vtkFile).is_open())
162 MESSAGE(LOC<<"The file is already close and it is opened with the right option");
163 (*_vtkFile).open(_fileName.c_str(), ofstream::out | ofstream::app) ;
167 MESSAGE(LOC<<"The file is still open, it is closed to make sure that it will be opened with the right option");
171 (*_vtkFile).close() ;
173 _vtkFile->open(_fileName.c_str(), ofstream::out | ofstream::app) ;
176 // _status = MED_OPENED ;
180 SCRUTE((*_vtkFile).is_open());
186 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
192 void open() throw (MEDEXCEPTION)
197 void openAppend() throw (MEDEXCEPTION)
202 void closeConst() const throw (MEDEXCEPTION)
204 const char * LOC = "VTK_FIELD_DRIVER::closeConst() " ;
211 if ((*_vtkFile).is_open())
215 // _status = MED_CLOSED ;
222 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not close file "
234 Set the name of the FIELD asked in file.
236 It could be different than the name of the FIELD object.
238 void setFieldName(const string & fieldName) ;
241 Get the name of the FIELD asked in file.
243 string getFieldName() const ;
246 Return a MEDEXCEPTION : it is the write-only driver.
248 void read ( void ) throw (MEDEXCEPTION) ;
251 Write FIELD in the specified file, with its mesh through its support
252 which has to be on all entities (excluding the faces in 3d and edges
255 void write( void ) const throw (MEDEXCEPTION) ;
258 Write FIELD in the specified file, the mesh is supposed to be
259 written in this file. The field support has to be on all entities
260 (excluding the faces in 3d and edges in 2d).
262 void writeAppend( void ) const throw (MEDEXCEPTION);
265 GENDRIVER * copy ( void ) const ;
270 /*-------------------------*/
271 /* template implementation */
272 /*-------------------------*/
274 /*--------------------- DRIVER PART -------------------------------*/
276 using namespace MEDMEM;
277 template <class T> void VTK_FIELD_DRIVER<T>::setFieldName(const string & fieldName)
279 _fieldName = fieldName;
282 template <class T> string VTK_FIELD_DRIVER<T>::getFieldName() const
287 template <class T> GENDRIVER * VTK_FIELD_DRIVER<T>::copy(void) const
289 VTK_FIELD_DRIVER<T> * myDriver = new VTK_FIELD_DRIVER<T>(*this);
294 template <class T> void VTK_FIELD_DRIVER<T>::read (void)
297 throw MEDEXCEPTION("VTK_FIELD_DRIVER::read : Can't read with a VTK driver because it is write only driver !");
300 template <class T> void VTK_FIELD_DRIVER<T>::write(void) const
303 const char * LOC = "VTK_FIELD_DRIVER::write(void) const " ;
306 // we get the Support and its associated Mesh
308 const SUPPORT * supportField = _ptrField->getSupport();
309 MESH * meshField = supportField->getMesh();
311 // Well we must open vtk file first, because there are
312 // no other driver than MED for VTK that do it !
315 // could we put more than one Mesh ?????
316 (*_vtkFile) << "# vtk DataFile Version 2.0" << endl
317 << "maillage from MedMemory" << endl ;
318 // only ASCII for the moment (binary came later :-)
319 (*_vtkFile) << "ASCII" << endl ;
321 (*_vtkFile) << "DATASET UNSTRUCTURED_GRID" << endl ;
322 // put points (all point are in 3D, so if we are in 1D or 2D, we complete by zero !
323 int SpaceDimension = meshField->getSpaceDimension() ;
324 int NumberOfNodes = meshField->getNumberOfNodes() ;
325 (*_vtkFile) << "POINTS " << NumberOfNodes << " float" << endl ;
326 const double *coordinate = meshField->getCoordinates(MED_FULL_INTERLACE) ;
327 for (int i=0;i<NumberOfNodes;i++) {
328 for (int j=0;j<SpaceDimension;j++)
329 (*_vtkFile) << coordinate[i*SpaceDimension+j] << " " ;
330 if (SpaceDimension==1)
331 (*_vtkFile) << "0 0" ;
332 if (SpaceDimension==2)
334 (*_vtkFile) << endl ;
337 // we put connectivity
338 // how many cells and how many value in connectivity :
339 int cells_types_count = meshField->getNumberOfTypes(MED_CELL) ;
340 // int * cells_count = meshField->get_cells_count() ;
341 // int cells_sum = cells_count[cells_types_count] ;
342 int cells_sum = meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) ;
343 const CELLMODEL * cells_type = meshField->getCellsTypes(MED_CELL) ;
344 // int connectivity_sum = 0 ;
346 //const int * connectivity = meshField->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,MED_ALL_ELEMENTS) ; !! UNUSED VARIABLE !!
347 const int * connectivityIndex = meshField->getConnectivityIndex(MED_NODAL,MED_CELL) ;
349 int connectivity_sum = connectivityIndex[cells_sum]-1 ;
351 (*_vtkFile) << "CELLS " << cells_sum << " " << connectivity_sum+cells_sum << endl ;
352 // we put connectivity
353 for (int i=0;i<cells_types_count;i++) {
354 int *filter = (int*) NULL ; // index in vtk connectivity
355 switch (cells_type[i].getType())
358 filter = new int[1] ;
363 filter = new int[2] ;
372 filter = new int[3] ;
379 filter = new int[4] ;
393 filter = new int[4] ;
396 filter[2] = 3 ; // 3td element in med are 4th in vtk (array begin at 0 !)
397 filter[3] = 2 ; // 4th element in med are 3rd in vtk (array begin at 0 !)
401 filter = new int[5] ;
403 filter[1] = 3 ; // 2nd element in med are 4th in vtk (array begin at 0 !)
405 filter[3] = 1 ; // 4th element in med are 2nd in vtk (array begin at 0 !)
410 filter = new int[6] ;
420 filter = new int[8] ;
448 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getName() ) ) ;
449 int nodes_cell = cells_type[i].getNumberOfNodes();
450 int numberOfCell = meshField->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
451 const int * connectivityArray = meshField->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,cells_type[i].getType());
452 for (int j=0;j<numberOfCell;j++) {
453 (*_vtkFile) << nodes_cell << " " ;
454 for (int k=0;k<nodes_cell;k++)
455 (*_vtkFile) << connectivityArray[j*nodes_cell+filter[k]] - 1 << " " ;
456 (*_vtkFile) << endl ;
461 (*_vtkFile) << endl ;
463 (*_vtkFile) << "CELL_TYPES " << cells_sum << endl ;
464 for (int i=0;i<cells_types_count;i++) {
466 switch (cells_type[i].getType())
534 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getType() ) ) ;
535 int numberOfCell = meshField->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
536 for (int j=0;j<numberOfCell;j++)
537 (*_vtkFile) << vtkType << endl ;
540 // first : field on node
541 // fields is on all node !
543 // second : field on cell
544 // fields is on all cell !
546 int dt = _ptrField->getIterationNumber();
547 int it = _ptrField->getOrderNumber();
550 string nameField = _ptrField->getName();
551 medEntityMesh entitySupport = supportField->getEntity();
552 name << nameField << "_" << dt << "_" << it ;
554 if (!(supportField->isOnAllElements()))
555 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all entities of the mesh !" << entitySupport));
557 if (entitySupport == MED_NODE)
558 (*_vtkFile) << "POINT_DATA " << meshField->getNumberOfNodes() << endl ;
559 else if (entitySupport == MED_CELL)
560 (*_vtkFile) << "CELL_DATA " << meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) << endl ;
562 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all nodes or cells but it's on !" << entitySupport));
564 int NomberOfValue = supportField->getNumberOfElements(MED_ALL_ELEMENTS) ;
565 int NomberOfComponents = _ptrField->getNumberOfComponents() ;
567 med_type_champ fieldType = _ptrField->getValueType() ;
584 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<< name.str() <<" the type is not int or double !"));
588 if (NomberOfComponents==3)
589 (*_vtkFile) << "VECTORS " << name.str() << " int" << endl ;
590 else if (NomberOfComponents<=4)
592 (*_vtkFile) << "SCALARS " << name.str() << " int " << NomberOfComponents << endl ;
593 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
596 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
598 const T * value = _ptrField->getValue(MED_NO_INTERLACE) ;
600 for (int i=0; i<NomberOfValue; i++)
602 for(int j=0; j<NomberOfComponents; j++)
603 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
604 (*_vtkFile) << endl ;
609 template <class T> void VTK_FIELD_DRIVER<T>::writeAppend(void) const
612 const char * LOC = "VTK_FIELD_DRIVER::writeAppend(void) const " ;
615 // we get the Support and its associated Mesh
617 const SUPPORT * supportField = _ptrField->getSupport();
618 MESH * meshField = supportField->getMesh();
620 // Well we must open vtk file first, because there are
621 // no other driver than MED for VTK that do it !
624 // first : field on node
625 // fields is on all node !
627 // second : field on cell
628 // fields is on all cell !
630 int dt = _ptrField->getIterationNumber();
631 int it = _ptrField->getOrderNumber();
634 string nameField = _ptrField->getName();
635 medEntityMesh entitySupport = supportField->getEntity();
636 name << nameField << "_" << dt << "_" << it ;
638 if (!(supportField->isOnAllElements()))
639 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all entities of the mesh !" << entitySupport));
641 if (entitySupport == MED_NODE)
642 (*_vtkFile) << "POINT_DATA " << meshField->getNumberOfNodes() << endl ;
643 else if (entitySupport == MED_CELL)
644 (*_vtkFile) << "CELL_DATA " << meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) << endl ;
646 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all nodes or cells but it's on !" << entitySupport));
648 int NomberOfValue = supportField->getNumberOfElements(MED_ALL_ELEMENTS) ;
649 int NomberOfComponents = _ptrField->getNumberOfComponents() ;
651 med_type_champ fieldType = _ptrField->getValueType() ;
667 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<< name.str() <<" the type is not int or double !"));
671 if (NomberOfComponents==3)
672 (*_vtkFile) << "VECTORS " << name.str() << " int" << endl ;
673 else if (NomberOfComponents<=4)
675 (*_vtkFile) << "SCALARS " << name.str() << " int " << NomberOfComponents << endl ;
676 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
679 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
681 const T * value = _ptrField->getValue(MED_NO_INTERLACE) ;
683 for (int i=0; i<NomberOfValue; i++)
685 for(int j=0; j<NomberOfComponents; j++)
686 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
687 (*_vtkFile) << endl ;
692 #endif /* VTK_FIELD_DRIVER_HXX */