1 #ifndef VTK_FIELD_DRIVER_HXX
2 #define VTK_FIELD_DRIVER_HXX
6 #include "MEDMEM_define.hxx"
8 #include "MEDMEM_GenDriver.hxx"
11 #include "MEDMEM_STRING.hxx"
12 #include "MEDMEM_Exception.hxx"
13 #include "MEDMEM_Unit.hxx"
14 #include "MEDMEM_Array.hxx"
15 #include "MEDMEM_Support.hxx"
16 //#include "MEDMEM_Field.hxx"
17 #include "MEDMEM_Mesh.hxx"
18 #include "MEDMEM_CellModel.hxx"
20 //using namespace MED_FR ;
22 template <class T> class FIELD;
28 Generic part : implement open and close methods.
32 template <class T> class VTK_FIELD_DRIVER : public GENDRIVER
37 mutable ofstream * _vtkFile ;
43 // all MED cell type ?? Classe de Définition ??
44 // static const medGeometryElement all_cell_type[MED_NBR_GEOMETRIE_MAILLE];
46 // static const char * const all_cell_type_tab [MED_NBR_GEOMETRIE_MAILLE];
51 VTK_FIELD_DRIVER():GENDRIVER(),
52 _ptrField((FIELD<T> *)MED_NULL), _fieldName(""),
53 _fieldNum(MED_INVALID)
55 const char * LOC = "VTK_FIELD_DRIVER::VTK_FIELD_DRIVER() ";
58 _vtkFile = new ofstream();
65 VTK_FIELD_DRIVER(const string & fileName, FIELD<T> * ptrField)
66 : GENDRIVER(fileName,MED_WRONLY),
67 _ptrField((FIELD<T> *) ptrField),
68 _fieldName(fileName),_fieldNum(MED_INVALID)
70 const char * LOC = "VTK_FIELD_DRIVER::VTK_FIELD_DRIVER(const string & fileName, FIELD<T> * ptrField) ";
73 _vtkFile = new ofstream();
81 VTK_FIELD_DRIVER(const VTK_FIELD_DRIVER & fieldDriver):
82 GENDRIVER(fieldDriver),
83 _ptrField(fieldDriver._ptrField),
84 _fieldName(fieldDriver._fieldName),
85 _fieldNum(fieldDriver._fieldNum)
87 _ptrField->addDriver(*this);
88 _vtkFile = new ofstream();
96 const char * LOC ="VTK_FIELD_DRIVER::~VTK_FIELD_DRIVER()";
110 void openConst() const throw (MEDEXCEPTION)
112 const char * LOC = "VTK_FIELD_DRIVER::openConst()" ;
115 MESSAGE(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
117 if ( _fileName == "" )
118 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
119 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
123 if (!(*_vtkFile).is_open())
124 (*_vtkFile).open(_fileName.c_str()) ;
126 // _status = MED_OPENED ;
130 SCRUTE((*_vtkFile).is_open());
136 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
142 void openConstAppend() const throw (MEDEXCEPTION)
144 const char * LOC = "VTK_FIELD_DRIVER::openConstAppend()" ;
147 MESSAGE(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
149 if ( _fileName == "" )
150 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
151 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
155 SCRUTE((*_vtkFile).is_open());
157 if (!(*_vtkFile).is_open())
159 MESSAGE(LOC<<"The file is already close and it is opened with the right option");
160 (*_vtkFile).open(_fileName.c_str(), ofstream::out | ofstream::app) ;
164 MESSAGE(LOC<<"The file is still open, it is closed to make sure that it will be opened with the right option");
168 (*_vtkFile).close() ;
170 _vtkFile->open(_fileName.c_str(), ofstream::out | ofstream::app) ;
173 // _status = MED_OPENED ;
177 SCRUTE((*_vtkFile).is_open());
183 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
189 void open() throw (MEDEXCEPTION)
194 void openAppend() throw (MEDEXCEPTION)
199 void closeConst() const throw (MEDEXCEPTION)
201 const char * LOC = "VTK_FIELD_DRIVER::closeConst() " ;
208 if ((*_vtkFile).is_open())
212 // _status = MED_CLOSED ;
219 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not close file "
231 Set the name of the FIELD asked in file.
233 It could be different than the name of the FIELD object.
235 void setFieldName(const string & fieldName) ;
238 Get the name of the FIELD asked in file.
240 string getFieldName() const ;
243 Return a MEDEXCEPTION : it is the write-only driver.
245 void read ( void ) throw (MEDEXCEPTION) ;
248 Write FIELD in the specified file, with its mesh through its support
249 which has to be on all entities (excluding the faces in 3d and edges
252 void write( void ) const throw (MEDEXCEPTION) ;
255 Write FIELD in the specified file, the mesh is supposed to be
256 written in this file. The field support has to be on all entities
257 (excluding the faces in 3d and edges in 2d).
259 void writeAppend( void ) const throw (MEDEXCEPTION);
262 GENDRIVER * copy ( void ) const ;
266 /*-------------------------*/
267 /* template implementation */
268 /*-------------------------*/
270 /*--------------------- DRIVER PART -------------------------------*/
272 template <class T> void VTK_FIELD_DRIVER<T>::setFieldName(const string & fieldName)
274 _fieldName = fieldName;
277 template <class T> string VTK_FIELD_DRIVER<T>::getFieldName() const
282 template <class T> GENDRIVER * VTK_FIELD_DRIVER<T>::copy(void) const
284 VTK_FIELD_DRIVER<T> * myDriver = new VTK_FIELD_DRIVER<T>(*this);
289 template <class T> void VTK_FIELD_DRIVER<T>::read (void)
292 throw MEDEXCEPTION("VTK_FIELD_DRIVER::read : Can't read with a VTK driver because it is write only driver !");
295 template <class T> void VTK_FIELD_DRIVER<T>::write(void) const
298 const char * LOC = "VTK_FIELD_DRIVER::write(void) const " ;
301 // we get the Support and its associated Mesh
303 const SUPPORT * supportField = _ptrField->getSupport();
304 MESH * meshField = supportField->getMesh();
306 // Well we must open vtk file first, because there are
307 // no other driver than MED for VTK that do it !
310 // could we put more than one Mesh ?????
311 (*_vtkFile) << "# vtk DataFile Version 2.0" << endl
312 << "maillage from MedMemory" << endl ;
313 // only ASCII for the moment (binary came later :-)
314 (*_vtkFile) << "ASCII" << endl ;
316 (*_vtkFile) << "DATASET UNSTRUCTURED_GRID" << endl ;
317 // put points (all point are in 3D, so if we are in 1D or 2D, we complete by zero !
318 int SpaceDimension = meshField->getSpaceDimension() ;
319 int NumberOfNodes = meshField->getNumberOfNodes() ;
320 (*_vtkFile) << "POINTS " << NumberOfNodes << " float" << endl ;
321 const double *coordinate = meshField->getCoordinates(MED_FULL_INTERLACE) ;
322 for (int i=0;i<NumberOfNodes;i++) {
323 for (int j=0;j<SpaceDimension;j++)
324 (*_vtkFile) << coordinate[i*SpaceDimension+j] << " " ;
325 if (SpaceDimension==1)
326 (*_vtkFile) << "0 0" ;
327 if (SpaceDimension==2)
329 (*_vtkFile) << endl ;
332 // we put connectivity
333 // how many cells and how many value in connectivity :
334 int cells_types_count = meshField->getNumberOfTypes(MED_CELL) ;
335 // int * cells_count = meshField->get_cells_count() ;
336 // int cells_sum = cells_count[cells_types_count] ;
337 int cells_sum = meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) ;
338 const CELLMODEL * cells_type = meshField->getCellsTypes(MED_CELL) ;
339 // int connectivity_sum = 0 ;
341 //const int * connectivity = meshField->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,MED_ALL_ELEMENTS) ; !! UNUSED VARIABLE !!
342 const int * connectivityIndex = meshField->getConnectivityIndex(MED_NODAL,MED_CELL) ;
344 int connectivity_sum = connectivityIndex[cells_sum]-1 ;
346 (*_vtkFile) << "CELLS " << cells_sum << " " << connectivity_sum+cells_sum << endl ;
347 // we put connectivity
348 for (int i=0;i<cells_types_count;i++) {
349 int *filter = (int*) NULL ; // index in vtk connectivity
350 switch (cells_type[i].getType())
353 filter = new int[1] ;
358 filter = new int[2] ;
367 filter = new int[3] ;
374 filter = new int[4] ;
388 filter = new int[4] ;
391 filter[2] = 3 ; // 3td element in med are 4th in vtk (array begin at 0 !)
392 filter[3] = 2 ; // 4th element in med are 3rd in vtk (array begin at 0 !)
396 filter = new int[5] ;
398 filter[1] = 3 ; // 2nd element in med are 4th in vtk (array begin at 0 !)
400 filter[3] = 1 ; // 4th element in med are 2nd in vtk (array begin at 0 !)
405 filter = new int[6] ;
415 filter = new int[8] ;
443 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getName() ) ) ;
444 int nodes_cell = cells_type[i].getNumberOfNodes();
445 int numberOfCell = meshField->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
446 const int * connectivityArray = meshField->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,cells_type[i].getType());
447 for (int j=0;j<numberOfCell;j++) {
448 (*_vtkFile) << nodes_cell << " " ;
449 for (int k=0;k<nodes_cell;k++)
450 (*_vtkFile) << connectivityArray[j*nodes_cell+filter[k]] - 1 << " " ;
451 (*_vtkFile) << endl ;
456 (*_vtkFile) << endl ;
458 (*_vtkFile) << "CELL_TYPES " << cells_sum << endl ;
459 for (int i=0;i<cells_types_count;i++) {
461 switch (cells_type[i].getType())
529 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getType() ) ) ;
530 int numberOfCell = meshField->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
531 for (int j=0;j<numberOfCell;j++)
532 (*_vtkFile) << vtkType << endl ;
535 // first : field on node
536 // fields is on all node !
538 // second : field on cell
539 // fields is on all cell !
541 int dt = _ptrField->getIterationNumber();
542 int it = _ptrField->getOrderNumber();
545 string nameField = _ptrField->getName();
546 medEntityMesh entitySupport = supportField->getEntity();
547 name << nameField << "_" << dt << "_" << it ;
549 if (!(supportField->isOnAllElements()))
550 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all entities of the mesh !" << entitySupport));
552 if (entitySupport == MED_NODE)
553 (*_vtkFile) << "POINT_DATA " << meshField->getNumberOfNodes() << endl ;
554 else if (entitySupport == MED_CELL)
555 (*_vtkFile) << "CELL_DATA " << meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) << endl ;
557 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));
559 int NomberOfValue = supportField->getNumberOfElements(MED_ALL_ELEMENTS) ;
560 int NomberOfComponents = _ptrField->getNumberOfComponents() ;
562 med_type_champ fieldType = _ptrField->getValueType() ;
570 MESSAGE("MED_INT32");
571 if (NomberOfComponents==3)
572 (*_vtkFile) << "VECTORS " << name.str() << " int" << endl ;
573 else if (NomberOfComponents<=4)
575 (*_vtkFile) << "SCALARS " << name.str() << " int " << NomberOfComponents << endl ;
576 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
579 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
581 //const int * value = ((FIELD<int>*)myField)->getValue(MED_NO_INTERLACE) ;
582 const int * value = ((FIELD<int>*)_ptrField)->getValue(MED_NO_INTERLACE) ;
583 for (int i=0; i<NomberOfValue; i++)
585 for(int j=0; j<NomberOfComponents; j++)
586 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
587 (*_vtkFile) << endl ;
592 MESSAGE("MED_REEL64");
593 if (NomberOfComponents==3)
594 (*_vtkFile) << "VECTORS " << name.str() << " float" << endl ;
595 else if (NomberOfComponents<=4)
597 (*_vtkFile) << "SCALARS " << name.str() << " float " << NomberOfComponents << endl ;
598 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
601 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
603 const double * value = ((FIELD<double>*)_ptrField)->getValue(MED_NO_INTERLACE) ;
604 for (int i=0; i<NomberOfValue; i++)
606 for(int j=0; j<NomberOfComponents; j++)
607 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
608 (*_vtkFile) << endl ;
613 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<< name.str() <<" the type is not int or double !"));
620 template <class T> void VTK_FIELD_DRIVER<T>::writeAppend(void) const
623 const char * LOC = "VTK_FIELD_DRIVER::writeAppend(void) const " ;
626 // we get the Support and its associated Mesh
628 const SUPPORT * supportField = _ptrField->getSupport();
629 MESH * meshField = supportField->getMesh();
631 // Well we must open vtk file first, because there are
632 // no other driver than MED for VTK that do it !
635 // first : field on node
636 // fields is on all node !
638 // second : field on cell
639 // fields is on all cell !
641 int dt = _ptrField->getIterationNumber();
642 int it = _ptrField->getOrderNumber();
645 string nameField = _ptrField->getName();
646 medEntityMesh entitySupport = supportField->getEntity();
647 name << nameField << "_" << dt << "_" << it ;
649 if (!(supportField->isOnAllElements()))
650 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all entities of the mesh !" << entitySupport));
652 if (entitySupport == MED_NODE)
653 (*_vtkFile) << "POINT_DATA " << meshField->getNumberOfNodes() << endl ;
654 else if (entitySupport == MED_CELL)
655 (*_vtkFile) << "CELL_DATA " << meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) << endl ;
657 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));
659 int NomberOfValue = supportField->getNumberOfElements(MED_ALL_ELEMENTS) ;
660 int NomberOfComponents = _ptrField->getNumberOfComponents() ;
662 med_type_champ fieldType = _ptrField->getValueType() ;
670 MESSAGE("MED_INT32");
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 int * value = ((FIELD<int>*)myField)->getValue(MED_NO_INTERLACE) ;
682 const int * value = ((FIELD<int>*)_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 MESSAGE("MED_REEL64");
693 if (NomberOfComponents==3)
694 (*_vtkFile) << "VECTORS " << name.str() << " float" << endl ;
695 else if (NomberOfComponents<=4)
697 (*_vtkFile) << "SCALARS " << name.str() << " float " << NomberOfComponents << endl ;
698 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
701 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
703 const double * value = ((FIELD<double>*)_ptrField)->getValue(MED_NO_INTERLACE) ;
704 for (int i=0; i<NomberOfValue; i++)
706 for(int j=0; j<NomberOfComponents; j++)
707 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
708 (*_vtkFile) << endl ;
713 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<< name.str() <<" the type is not int or double !"));
720 #endif /* VTK_FIELD_DRIVER_HXX */