Back to index

salome-med  6.5.0
Public Member Functions | Protected Attributes | Private Member Functions
MEDMEM::VTK_FIELD_DRIVER< T > Class Template Reference

#include <MEDMEM_VtkFieldDriver.hxx>

Collaboration diagram for MEDMEM::VTK_FIELD_DRIVER< T >:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 VTK_FIELD_DRIVER ()
 Constructor.
template<class INTERLACING_TAG >
 VTK_FIELD_DRIVER (const std::string &fileName, const FIELD< T, INTERLACING_TAG > *ptrField)
 Constructor.
 VTK_FIELD_DRIVER (const VTK_FIELD_DRIVER &fieldDriver)
 Copy constructor.
 ~VTK_FIELD_DRIVER ()
 Destructor.
void openConst (bool append=false) const throw (MEDEXCEPTION)
void openConstAppend () const throw (MEDEXCEPTION)
void open () throw (MEDEXCEPTION)
void openAppend () throw (MEDEXCEPTION)
void closeConst () const throw (MEDEXCEPTION)
void close ()
void setFieldName (const string &fieldName)
 Set the name of the FIELD asked in file.
string getFieldName () const
 Get the name of the FIELD asked in file.
void read (void) throw (MEDEXCEPTION)
 Return a MEDEXCEPTION : it is the write-only driver.
void write (void) const throw (MEDEXCEPTION)
 Write FIELD in the specified file, with its mesh through its support which has to be on all entities (excluding the faces in 3d and edges in 2d).
void writeAppend (void) const throw (MEDEXCEPTION)
 Write FIELD in the specified file, the mesh is supposed to be written in this file.

Protected Attributes

const FIELD< T > * _ptrField
std::string _fieldName
int _fieldNum
ofstream * _vtkFile
_VTK_BinaryWriter * _binaryFile

Private Member Functions

GENDRIVER * copy (void) const

Detailed Description

template<class T>
class MEDMEM::VTK_FIELD_DRIVER< T >

Definition at line 61 of file MEDMEM_VtkFieldDriver.hxx.


Constructor & Destructor Documentation

template<class T>
MEDMEM::VTK_FIELD_DRIVER< T >::VTK_FIELD_DRIVER ( ) [inline]

Constructor.

Definition at line 77 of file MEDMEM_VtkFieldDriver.hxx.

                    :GENDRIVER(VTK_DRIVER),
                     _ptrField(0), _fieldName(""), _fieldNum(MED_EN::MED_INVALID),
                     _vtkFile(0), _binaryFile(0)
  {
    const char * LOC = "VTK_FIELD_DRIVER::VTK_FIELD_DRIVER() ";
    BEGIN_OF_MED(LOC);
    END_OF_MED(LOC);
  }
template<class T>
template<class INTERLACING_TAG >
MEDMEM::VTK_FIELD_DRIVER< T >::VTK_FIELD_DRIVER ( const std::string &  fileName,
const FIELD< T, INTERLACING_TAG > *  ptrField 
) [inline]

Constructor.

Definition at line 89 of file MEDMEM_VtkFieldDriver.hxx.

                                                              :
    GENDRIVER(fileName, MED_EN::WRONLY, VTK_DRIVER),
    _ptrField((const FIELD<T> *) ptrField), _fieldName(fileName),_fieldNum(MED_EN::MED_INVALID),
    _vtkFile(0), _binaryFile(0)
  {
    const char* LOC = "VTK_FIELD_DRIVER::VTK_FIELD_DRIVER(const string & fileName, FIELD<T> * ptrField) ";
    BEGIN_OF_MED(LOC);
    END_OF_MED(LOC);
  }
template<class T>
MEDMEM::VTK_FIELD_DRIVER< T >::VTK_FIELD_DRIVER ( const VTK_FIELD_DRIVER< T > &  fieldDriver) [inline]

Copy constructor.

Definition at line 103 of file MEDMEM_VtkFieldDriver.hxx.

                                                        :
    GENDRIVER(fieldDriver),
    _ptrField(fieldDriver._ptrField),
    _fieldName(fieldDriver._fieldName),
    _fieldNum(fieldDriver._fieldNum),
    _vtkFile(0), _binaryFile(0)
  {
  }
template<class T>
MEDMEM::VTK_FIELD_DRIVER< T >::~VTK_FIELD_DRIVER ( ) [inline]

Destructor.

Definition at line 115 of file MEDMEM_VtkFieldDriver.hxx.

  {
    const char* LOC = "VTK_FIELD_DRIVER::~VTK_FIELD_DRIVER()";
    BEGIN_OF_MED(LOC);

    close();

    if ( _vtkFile )    delete _vtkFile ;
    if ( _binaryFile ) delete _binaryFile;

    _vtkFile = 0;
    _binaryFile = 0;

    END_OF_MED(LOC);
  }

Here is the call graph for this function:


Member Function Documentation

template<class T>
void MEDMEM::VTK_FIELD_DRIVER< T >::close ( void  ) [inline]

Definition at line 226 of file MEDMEM_VtkFieldDriver.hxx.

               {
    closeConst() ;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T>
void MEDMEM::VTK_FIELD_DRIVER< T >::closeConst ( ) const throw (MEDEXCEPTION) [inline]

Definition at line 203 of file MEDMEM_VtkFieldDriver.hxx.

  {
    const char * LOC = "VTK_FIELD_DRIVER::closeConst() " ;
    BEGIN_OF_MED(LOC);

    if ( _vtkFile )
      {
        if ((*_vtkFile).is_open())
          (*_vtkFile).close();

        if ( (*_vtkFile) && _vtkFile->is_open() )
          throw MED_EXCEPTION( LOCALIZED( STRING(LOC) << "Could not close file "<< _fileName));
      }
    if ( _binaryFile )
      {
        _binaryFile->close();
        delete _binaryFile;
        _binaryFile = 0;
      }

    END_OF_MED(LOC);
  }

Here is the caller graph for this function:

template<class T >
GENDRIVER * MEDMEM::VTK_FIELD_DRIVER< T >::copy ( void  ) const [private]

Definition at line 282 of file MEDMEM_VtkFieldDriver.hxx.

{
  VTK_FIELD_DRIVER<T> * myDriver = new VTK_FIELD_DRIVER<T>(*this);

  return myDriver ;
}
template<class T >
string MEDMEM::VTK_FIELD_DRIVER< T >::getFieldName ( ) const

Get the name of the FIELD asked in file.

Definition at line 277 of file MEDMEM_VtkFieldDriver.hxx.

{
  return _fieldName;
}

Here is the caller graph for this function:

template<class T>
void MEDMEM::VTK_FIELD_DRIVER< T >::open ( void  ) throw (MEDEXCEPTION) [inline]

Definition at line 193 of file MEDMEM_VtkFieldDriver.hxx.

  {
    openConst() ;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T>
void MEDMEM::VTK_FIELD_DRIVER< T >::openAppend ( void  ) throw (MEDEXCEPTION) [inline]

Definition at line 198 of file MEDMEM_VtkFieldDriver.hxx.

  {
    openConst(true) ;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T>
void MEDMEM::VTK_FIELD_DRIVER< T >::openConst ( bool  append = false) const throw (MEDEXCEPTION) [inline]

Definition at line 131 of file MEDMEM_VtkFieldDriver.hxx.

  {
    const char * LOC = "VTK_FIELD_DRIVER::openConst()" ;
    BEGIN_OF_MED(LOC);

  if ( _fileName == "" )
    throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) 
                                     << "_fileName is |\"\"|, please set a correct fileName before calling open()"));

  if ( DRIVERFACTORY::getVtkBinaryFormatForWriting() )
    {
      if ( _vtkFile )
        {
          closeConst();
          delete _vtkFile;
          _vtkFile = 0;
        }
      if ( !_binaryFile )
        _binaryFile = new _VTK_BinaryWriter( _fileName );
      else
        _binaryFile->close();
      if (!_binaryFile->open(append))
        {
          delete _binaryFile;
          _binaryFile = 0;
          throw MED_EXCEPTION( LOCALIZED( STRING(LOC) << "Could not open file "<< _fileName));
        }
    }
  else
    {
      if ( _binaryFile )
        {
          _binaryFile->close();
          delete _binaryFile;
          _binaryFile = 0;
        }
 
      if (!_vtkFile )
        _vtkFile = new ofstream();
      else
        (*_vtkFile).close();

      if ( append )
        (*_vtkFile).open(_fileName.c_str(), ofstream::out | ofstream::app);
      else
        (*_vtkFile).open(_fileName.c_str()); 

      if (!(*_vtkFile))
        {
          delete _vtkFile;
          _vtkFile = 0;
          throw MED_EXCEPTION ( LOCALIZED( STRING(LOC) << "Could not open file "<< _fileName));
        }
    }
    END_OF_MED(LOC);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T>
void MEDMEM::VTK_FIELD_DRIVER< T >::openConstAppend ( ) const throw (MEDEXCEPTION) [inline]

Definition at line 188 of file MEDMEM_VtkFieldDriver.hxx.

  {
    openConst(true);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T >
void MEDMEM::VTK_FIELD_DRIVER< T >::read ( void  ) throw (MEDEXCEPTION)

Return a MEDEXCEPTION : it is the write-only driver.

Definition at line 289 of file MEDMEM_VtkFieldDriver.hxx.

{
  throw MEDEXCEPTION("VTK_FIELD_DRIVER::read : Can't read with a VTK driver because it is write only driver !");
}

Here is the caller graph for this function:

template<class T >
void MEDMEM::VTK_FIELD_DRIVER< T >::setFieldName ( const string &  fieldName)

Set the name of the FIELD asked in file.

It could be different than the name of the FIELD object.

Definition at line 272 of file MEDMEM_VtkFieldDriver.hxx.

Here is the caller graph for this function:

template<class T >
void MEDMEM::VTK_FIELD_DRIVER< T >::write ( void  ) const throw (MEDEXCEPTION)

Write FIELD in the specified file, with its mesh through its support which has to be on all entities (excluding the faces in 3d and edges in 2d).

Write.

Definition at line 301 of file MEDMEM_VtkFieldDriver.hxx.

{
  const char * LOC = "VTK_FIELD_DRIVER::write(void) const " ;
  BEGIN_OF_MED(LOC);

  // we get the Support and its associated Mesh

  const SUPPORT * supportField = _ptrField->getSupport();
  const GMESH * meshField = supportField->getMesh();
  if (! meshField )
    throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": mesh was not read before writing")) ;

  // first, write mesh
  {
    VTK_MESH_DRIVER meshDriver( _fileName, meshField );
    meshDriver.write();
  }

  // write field
  writeAppend();
}

Here is the call graph for this function:

Here is the caller graph for this function:

template<class T >
void MEDMEM::VTK_FIELD_DRIVER< T >::writeAppend ( void  ) const throw (MEDEXCEPTION)

Write FIELD in the specified file, the mesh is supposed to be written in this file.

Write append.

The field support has to be on all entities (excluding the faces in 3d and edges in 2d).

Definition at line 330 of file MEDMEM_VtkFieldDriver.hxx.

{
  const char * LOC = "VTK_FIELD_DRIVER::writeAppend(void) const " ;
  BEGIN_OF_MED(LOC);

  // we get the Support and its associated Mesh

  const SUPPORT * supportField = _ptrField->getSupport();
  const GMESH * meshField = supportField->getMesh();
  MED_EN::medEntityMesh entitySupport = supportField->getEntity();

  if (! meshField )
    throw MEDEXCEPTION(LOCALIZED(STRING(LOC)<<": mesh was not read before writing")) ;

  if ( _ptrField->getGaussPresence() )
    throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which use Gauss Points !" << entitySupport));

  if (!(supportField->isOnAllElements()))
    throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" which is not on all entities of the mesh !" << entitySupport));


  // Well we must open vtk file first, because there are
  // no other driver than MED for VTK that do it !
  openConstAppend() ;

  // first : field on node
  // fields is on all node !

  // second : field on cell
  // fields is on all cell !

  int dt = _ptrField->getIterationNumber();
  int it = _ptrField->getOrderNumber();

  ostringstream name ;
  string nameField = _ptrField->getName();
  name << nameField << "_" << dt << "_" << it ;

  // BEGIN issue 0020610: [CEA 371] VTK field driver : save many fields.
  // POINT_DATA and CELL_DATA must encounters once
  STRING dataStr;
  if (entitySupport == MED_EN::MED_NODE)
    dataStr << "POINT_DATA " << meshField->getNumberOfNodes() ;
  else if (entitySupport == MED_EN::MED_CELL)
    dataStr << "CELL_DATA " << meshField->getNumberOfElements(MED_EN::MED_CELL,MED_EN::MED_ALL_ELEMENTS);
  else
    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));

  // check if dataStr is already present in the file
  bool toWriteDataStr = true;
#ifdef WNT
  int vtkFile = ::_open (_fileName.c_str(), _O_RDONLY|_O_BINARY);
#else
  int vtkFile = ::open (_fileName.c_str(), O_RDONLY);
#endif
  if ( vtkFile > 0 )
  {
#ifdef WNT
    ssize_t fileSize = ::_lseek( vtkFile, 0, SEEK_END); ::lseek( vtkFile, 0, SEEK_SET);
    char* buf = new char[ fileSize ];
    ::_read (vtkFile, buf, fileSize );
#else
    ssize_t fileSize = ::lseek( vtkFile, 0, SEEK_END); ::lseek( vtkFile, 0, SEEK_SET);
    char* buf = new char[ fileSize ];
    ::read (vtkFile, buf, fileSize );
#endif
    char *vtkData = buf, *vtkDataEnd = buf+fileSize-dataStr.size();
    while ( ++vtkData < vtkDataEnd && toWriteDataStr )
      toWriteDataStr = ( strncmp( dataStr.data(), vtkData, dataStr.size()) != 0 );
    delete [] buf;
#ifdef WNT
    ::_close (vtkFile);
#else
    ::close (vtkFile);
#endif
  }
  std::ostringstream vtkFileStr; // to collect labels
  if ( _binaryFile )
    vtkFileStr << endl;
  if ( toWriteDataStr )
    vtkFileStr << dataStr << endl;
  // END issue 0020610: [CEA 371] VTK field driver : save many fields

  int NomberOfValue = supportField->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS) ;
  int NomberOfComponents =  _ptrField->getNumberOfComponents() ;

  MED_EN::med_type_champ fieldType = _ptrField->getValueType() ;

  SCRUTE_MED(name.str());
  SCRUTE_MED(fieldType);

  std::string typeStr;
  switch (fieldType)
    {
    case MED_EN::MED_INT32 :
      {
        typeStr = " int"; break ;
      }
    case MED_EN::MED_REEL64 :
      {
        typeStr = " float"; break ;
      }
    default :
      {
        throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<< name.str() <<" the type is not int or double !"));
      }
    }

  if (NomberOfComponents==3)
    vtkFileStr << "VECTORS " << name.str() << typeStr << endl ;
  else if (NomberOfComponents<=4)
    {
      vtkFileStr << "SCALARS " << name.str() << typeStr << " " << NomberOfComponents << endl ;
      vtkFileStr << "LOOKUP_TABLE default" << endl ;
    }
  else
    throw MED_EXCEPTION(LOCALIZED(STRING(LOC) << "Could not write field "<<_ptrField->getName()<<" there are more than 4 components !"));

  const T * value ;
  MEDMEM_Array_ * tmpArray = 0;
  if ( _ptrField->getInterlacingType() == MED_EN::MED_FULL_INTERLACE )
    {
      value = _ptrField->getValue();
    }
  else if ( _ptrField->getInterlacingType() == MED_EN::MED_NO_INTERLACE_BY_TYPE )
    {
      MEDMEM_Array_ * ptrArray = _ptrField->getArray();
      MEDMEM_Array<T,NoInterlaceByTypeNoGaussPolicy> * temp = dynamic_cast<MEDMEM_Array<T,NoInterlaceByTypeNoGaussPolicy> * >  ( ptrArray );
      MEDMEM_Array<T,NoInterlaceNoGaussPolicy> * array = ArrayConvert2No( *temp );
      tmpArray = array;
      value = array->getPtr();
    }
  else
    {
      MEDMEM_Array_ * ptrArray = _ptrField->getArray();
      MEDMEM_Array<T,NoInterlaceNoGaussPolicy> * temp = dynamic_cast<MEDMEM_Array<T,NoInterlaceNoGaussPolicy> * >  ( ptrArray );
      MEDMEM_Array<T,FullInterlaceNoGaussPolicy> * array = ArrayConvert( *temp );
      tmpArray = array;
      value = array->getPtr();
    }

  if ( _vtkFile ) // ASCII
    {
      (*_vtkFile) << vtkFileStr.str();
      for (int i=0; i<NomberOfValue; i++)
        {
          for(int j=0; j<NomberOfComponents; j++)
            (*_vtkFile) << value[j*NomberOfValue+i] << " " ;
          (*_vtkFile) << endl ;
        }
    }
  else // BINARY
    {
      std::string str = vtkFileStr.str();
      _binaryFile->write( str.data(), str.size() );
      // Though type "double" is available in VTK but actually it does not work
      // (at least paraview shows wrong values)
      if ( fieldType == MED_EN::MED_REEL64 )
        {
          vector<float> floatValue(NomberOfValue * NomberOfComponents );
          for ( unsigned i = 0; i < floatValue.size(); ++i )
            floatValue[i]=float( value[i] );
          _binaryFile->write( &floatValue[0], NomberOfValue * NomberOfComponents );
        }
      else
        {
          _binaryFile->write( value, NomberOfValue * NomberOfComponents );
        }
    }

  if ( _ptrField->getInterlacingType() != MED_EN::MED_FULL_INTERLACE )
    delete tmpArray;

  closeConst();

  END_OF_MED(LOC);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

template<class T>
_VTK_BinaryWriter* MEDMEM::VTK_FIELD_DRIVER< T >::_binaryFile [mutable, protected]

Definition at line 70 of file MEDMEM_VtkFieldDriver.hxx.

template<class T>
std::string MEDMEM::VTK_FIELD_DRIVER< T >::_fieldName [protected]

Definition at line 66 of file MEDMEM_VtkFieldDriver.hxx.

template<class T>
int MEDMEM::VTK_FIELD_DRIVER< T >::_fieldNum [protected]

Definition at line 67 of file MEDMEM_VtkFieldDriver.hxx.

template<class T>
const FIELD<T>* MEDMEM::VTK_FIELD_DRIVER< T >::_ptrField [protected]

Definition at line 65 of file MEDMEM_VtkFieldDriver.hxx.

template<class T>
ofstream* MEDMEM::VTK_FIELD_DRIVER< T >::_vtkFile [mutable, protected]

Definition at line 69 of file MEDMEM_VtkFieldDriver.hxx.


The documentation for this class was generated from the following file: