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 MEDMEM ;
27 Generic part : implement open and close methods.
32 template <class T> class FIELD;
33 template <class T> class VTK_FIELD_DRIVER : public GENDRIVER
38 mutable ofstream * _vtkFile ;
44 // all MED cell type ?? Classe de Définition ??
45 // static const medGeometryElement all_cell_type[MED_NBR_GEOMETRIE_MAILLE];
47 // static const char * const all_cell_type_tab [MED_NBR_GEOMETRIE_MAILLE];
52 VTK_FIELD_DRIVER():GENDRIVER(),
53 _ptrField((FIELD<T> *)MED_NULL), _fieldName(""),
54 _fieldNum(MED_INVALID)
56 const char * LOC = "VTK_FIELD_DRIVER::VTK_FIELD_DRIVER() ";
59 _vtkFile = new ofstream();
66 VTK_FIELD_DRIVER(const string & fileName, FIELD<T> * ptrField)
67 : GENDRIVER(fileName,MED_WRONLY),
68 _ptrField((FIELD<T> *) ptrField),
69 _fieldName(fileName),_fieldNum(MED_INVALID)
71 const char * LOC = "VTK_FIELD_DRIVER::VTK_FIELD_DRIVER(const string & fileName, FIELD<T> * ptrField) ";
74 _vtkFile = new ofstream();
82 VTK_FIELD_DRIVER(const VTK_FIELD_DRIVER & fieldDriver):
83 GENDRIVER(fieldDriver),
84 _ptrField(fieldDriver._ptrField),
85 _fieldName(fieldDriver._fieldName),
86 _fieldNum(fieldDriver._fieldNum)
88 _ptrField->addDriver(*this);
89 _vtkFile = new ofstream();
97 const char * LOC ="VTK_FIELD_DRIVER::~VTK_FIELD_DRIVER()";
111 void openConst() const throw (MEDEXCEPTION)
113 const char * LOC = "VTK_FIELD_DRIVER::openConst()" ;
116 MESSAGE(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
118 if ( _fileName == "" )
119 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
120 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
124 if (!(*_vtkFile).is_open())
125 (*_vtkFile).open(_fileName.c_str()) ;
127 // _status = MED_OPENED ;
131 SCRUTE((*_vtkFile).is_open());
137 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
143 void openConstAppend() const throw (MEDEXCEPTION)
145 const char * LOC = "VTK_FIELD_DRIVER::openConstAppend()" ;
148 MESSAGE(LOC<<" : _fileName.c_str : "<< _fileName.c_str()<<",mode : "<< _accessMode);
150 if ( _fileName == "" )
151 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC)
152 << "_fileName is |\"\"|, please set a correct fileName before calling open()"
156 SCRUTE((*_vtkFile).is_open());
158 if (!(*_vtkFile).is_open())
160 MESSAGE(LOC<<"The file is already close and it is opened with the right option");
161 (*_vtkFile).open(_fileName.c_str(), ofstream::out | ofstream::app) ;
165 MESSAGE(LOC<<"The file is still open, it is closed to make sure that it will be opened with the right option");
169 (*_vtkFile).close() ;
171 _vtkFile->open(_fileName.c_str(), ofstream::out | ofstream::app) ;
174 // _status = MED_OPENED ;
178 SCRUTE((*_vtkFile).is_open());
184 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "
190 void open() throw (MEDEXCEPTION)
195 void openAppend() throw (MEDEXCEPTION)
200 void closeConst() const throw (MEDEXCEPTION)
202 const char * LOC = "VTK_FIELD_DRIVER::closeConst() " ;
209 if ((*_vtkFile).is_open())
213 // _status = MED_CLOSED ;
220 throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not close file "
232 Set the name of the FIELD asked in file.
234 It could be different than the name of the FIELD object.
236 void setFieldName(const string & fieldName) ;
239 Get the name of the FIELD asked in file.
241 string getFieldName() const ;
244 Return a MEDEXCEPTION : it is the write-only driver.
246 void read ( void ) throw (MEDEXCEPTION) ;
249 Write FIELD in the specified file, with its mesh through its support
250 which has to be on all entities (excluding the faces in 3d and edges
253 void write( void ) const throw (MEDEXCEPTION) ;
256 Write FIELD in the specified file, the mesh is supposed to be
257 written in this file. The field support has to be on all entities
258 (excluding the faces in 3d and edges in 2d).
260 void writeAppend( void ) const throw (MEDEXCEPTION);
263 GENDRIVER * copy ( void ) const ;
268 /*-------------------------*/
269 /* template implementation */
270 /*-------------------------*/
272 /*--------------------- DRIVER PART -------------------------------*/
274 using namespace MEDMEM;
275 template <class T> void VTK_FIELD_DRIVER<T>::setFieldName(const string & fieldName)
277 _fieldName = fieldName;
280 template <class T> string VTK_FIELD_DRIVER<T>::getFieldName() const
285 template <class T> GENDRIVER * VTK_FIELD_DRIVER<T>::copy(void) const
287 VTK_FIELD_DRIVER<T> * myDriver = new VTK_FIELD_DRIVER<T>(*this);
292 template <class T> void VTK_FIELD_DRIVER<T>::read (void)
295 throw MEDEXCEPTION("VTK_FIELD_DRIVER::read : Can't read with a VTK driver because it is write only driver !");
298 template <class T> void VTK_FIELD_DRIVER<T>::write(void) const
301 const char * LOC = "VTK_FIELD_DRIVER::write(void) const " ;
304 // we get the Support and its associated Mesh
306 const SUPPORT * supportField = _ptrField->getSupport();
307 MESH * meshField = supportField->getMesh();
309 // Well we must open vtk file first, because there are
310 // no other driver than MED for VTK that do it !
313 // could we put more than one Mesh ?????
314 (*_vtkFile) << "# vtk DataFile Version 2.0" << endl
315 << "maillage from MedMemory" << endl ;
316 // only ASCII for the moment (binary came later :-)
317 (*_vtkFile) << "ASCII" << endl ;
319 (*_vtkFile) << "DATASET UNSTRUCTURED_GRID" << endl ;
320 // put points (all point are in 3D, so if we are in 1D or 2D, we complete by zero !
321 int SpaceDimension = meshField->getSpaceDimension() ;
322 int NumberOfNodes = meshField->getNumberOfNodes() ;
323 (*_vtkFile) << "POINTS " << NumberOfNodes << " float" << endl ;
324 const double *coordinate = meshField->getCoordinates(MED_FULL_INTERLACE) ;
325 for (int i=0;i<NumberOfNodes;i++) {
326 for (int j=0;j<SpaceDimension;j++)
327 (*_vtkFile) << coordinate[i*SpaceDimension+j] << " " ;
328 if (SpaceDimension==1)
329 (*_vtkFile) << "0 0" ;
330 if (SpaceDimension==2)
332 (*_vtkFile) << endl ;
335 // we put connectivity
336 // how many cells and how many value in connectivity :
337 int cells_types_count = meshField->getNumberOfTypes(MED_CELL) ;
338 // int * cells_count = meshField->get_cells_count() ;
339 // int cells_sum = cells_count[cells_types_count] ;
340 int cells_sum = meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) ;
341 const CELLMODEL * cells_type = meshField->getCellsTypes(MED_CELL) ;
342 // int connectivity_sum = 0 ;
344 //const int * connectivity = meshField->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,MED_ALL_ELEMENTS) ; !! UNUSED VARIABLE !!
345 const int * connectivityIndex = meshField->getConnectivityIndex(MED_NODAL,MED_CELL) ;
347 int connectivity_sum = connectivityIndex[cells_sum]-1 ;
349 (*_vtkFile) << "CELLS " << cells_sum << " " << connectivity_sum+cells_sum << endl ;
350 // we put connectivity
351 for (int i=0;i<cells_types_count;i++) {
352 int *filter = (int*) NULL ; // index in vtk connectivity
353 switch (cells_type[i].getType())
356 filter = new int[1] ;
361 filter = new int[2] ;
370 filter = new int[3] ;
377 filter = new int[4] ;
391 filter = new int[4] ;
394 filter[2] = 3 ; // 3td element in med are 4th in vtk (array begin at 0 !)
395 filter[3] = 2 ; // 4th element in med are 3rd in vtk (array begin at 0 !)
399 filter = new int[5] ;
401 filter[1] = 3 ; // 2nd element in med are 4th in vtk (array begin at 0 !)
403 filter[3] = 1 ; // 4th element in med are 2nd in vtk (array begin at 0 !)
408 filter = new int[6] ;
418 filter = new int[8] ;
446 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getName() ) ) ;
447 int nodes_cell = cells_type[i].getNumberOfNodes();
448 int numberOfCell = meshField->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
449 const int * connectivityArray = meshField->getConnectivity(MED_FULL_INTERLACE,MED_NODAL,MED_CELL,cells_type[i].getType());
450 for (int j=0;j<numberOfCell;j++) {
451 (*_vtkFile) << nodes_cell << " " ;
452 for (int k=0;k<nodes_cell;k++)
453 (*_vtkFile) << connectivityArray[j*nodes_cell+filter[k]] - 1 << " " ;
454 (*_vtkFile) << endl ;
459 (*_vtkFile) << endl ;
461 (*_vtkFile) << "CELL_TYPES " << cells_sum << endl ;
462 for (int i=0;i<cells_types_count;i++) {
464 switch (cells_type[i].getType())
532 throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": MED element type not supported yet : " << cells_type[i].getType() ) ) ;
533 int numberOfCell = meshField->getNumberOfElements(MED_CELL,cells_type[i].getType()) ;
534 for (int j=0;j<numberOfCell;j++)
535 (*_vtkFile) << vtkType << endl ;
538 // first : field on node
539 // fields is on all node !
541 // second : field on cell
542 // fields is on all cell !
544 int dt = _ptrField->getIterationNumber();
545 int it = _ptrField->getOrderNumber();
548 string nameField = _ptrField->getName();
549 medEntityMesh entitySupport = supportField->getEntity();
550 name << nameField << "_" << dt << "_" << it ;
552 if (!(supportField->isOnAllElements()))
553 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all entities of the mesh !" << entitySupport));
555 if (entitySupport == MED_NODE)
556 (*_vtkFile) << "POINT_DATA " << meshField->getNumberOfNodes() << endl ;
557 else if (entitySupport == MED_CELL)
558 (*_vtkFile) << "CELL_DATA " << meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) << endl ;
560 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));
562 int NomberOfValue = supportField->getNumberOfElements(MED_ALL_ELEMENTS) ;
563 int NomberOfComponents = _ptrField->getNumberOfComponents() ;
565 med_type_champ fieldType = _ptrField->getValueType() ;
573 MESSAGE("MED_INT32");
574 if (NomberOfComponents==3)
575 (*_vtkFile) << "VECTORS " << name.str() << " int" << endl ;
576 else if (NomberOfComponents<=4)
578 (*_vtkFile) << "SCALARS " << name.str() << " int " << NomberOfComponents << endl ;
579 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
582 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
584 //const int * value = ((FIELD<int>*)myField)->getValue(MED_NO_INTERLACE) ;
585 const int * value = ((FIELD<int>*)_ptrField)->getValue(MED_NO_INTERLACE) ;
586 for (int i=0; i<NomberOfValue; i++)
588 for(int j=0; j<NomberOfComponents; j++)
589 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
590 (*_vtkFile) << endl ;
595 MESSAGE("MED_REEL64");
596 if (NomberOfComponents==3)
597 (*_vtkFile) << "VECTORS " << name.str() << " float" << endl ;
598 else if (NomberOfComponents<=4)
600 (*_vtkFile) << "SCALARS " << name.str() << " float " << NomberOfComponents << endl ;
601 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
604 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
606 const double * value = ((FIELD<double>*)_ptrField)->getValue(MED_NO_INTERLACE) ;
607 for (int i=0; i<NomberOfValue; i++)
609 for(int j=0; j<NomberOfComponents; j++)
610 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
611 (*_vtkFile) << endl ;
616 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<< name.str() <<" the type is not int or double !"));
623 template <class T> void VTK_FIELD_DRIVER<T>::writeAppend(void) const
626 const char * LOC = "VTK_FIELD_DRIVER::writeAppend(void) const " ;
629 // we get the Support and its associated Mesh
631 const SUPPORT * supportField = _ptrField->getSupport();
632 MESH * meshField = supportField->getMesh();
634 // Well we must open vtk file first, because there are
635 // no other driver than MED for VTK that do it !
638 // first : field on node
639 // fields is on all node !
641 // second : field on cell
642 // fields is on all cell !
644 int dt = _ptrField->getIterationNumber();
645 int it = _ptrField->getOrderNumber();
648 string nameField = _ptrField->getName();
649 medEntityMesh entitySupport = supportField->getEntity();
650 name << nameField << "_" << dt << "_" << it ;
652 if (!(supportField->isOnAllElements()))
653 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all entities of the mesh !" << entitySupport));
655 if (entitySupport == MED_NODE)
656 (*_vtkFile) << "POINT_DATA " << meshField->getNumberOfNodes() << endl ;
657 else if (entitySupport == MED_CELL)
658 (*_vtkFile) << "CELL_DATA " << meshField->getNumberOfElements(MED_CELL,MED_ALL_ELEMENTS) << endl ;
660 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));
662 int NomberOfValue = supportField->getNumberOfElements(MED_ALL_ELEMENTS) ;
663 int NomberOfComponents = _ptrField->getNumberOfComponents() ;
665 med_type_champ fieldType = _ptrField->getValueType() ;
673 MESSAGE("MED_INT32");
674 if (NomberOfComponents==3)
675 (*_vtkFile) << "VECTORS " << name.str() << " int" << endl ;
676 else if (NomberOfComponents<=4)
678 (*_vtkFile) << "SCALARS " << name.str() << " int " << NomberOfComponents << endl ;
679 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
682 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
684 //const int * value = ((FIELD<int>*)myField)->getValue(MED_NO_INTERLACE) ;
685 const int * value = ((FIELD<int>*)_ptrField)->getValue(MED_NO_INTERLACE) ;
686 for (int i=0; i<NomberOfValue; i++)
688 for(int j=0; j<NomberOfComponents; j++)
689 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
690 (*_vtkFile) << endl ;
695 MESSAGE("MED_REEL64");
696 if (NomberOfComponents==3)
697 (*_vtkFile) << "VECTORS " << name.str() << " float" << endl ;
698 else if (NomberOfComponents<=4)
700 (*_vtkFile) << "SCALARS " << name.str() << " float " << NomberOfComponents << endl ;
701 (*_vtkFile) << "LOOKUP_TABLE default" << endl ;
704 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));
706 const double * value = ((FIELD<double>*)_ptrField)->getValue(MED_NO_INTERLACE) ;
707 for (int i=0; i<NomberOfValue; i++)
709 for(int j=0; j<NomberOfComponents; j++)
710 (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
711 (*_vtkFile) << endl ;
716 throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<< name.str() <<" the type is not int or double !"));
723 #endif /* VTK_FIELD_DRIVER_HXX */