Back to index

salome-med  6.5.0
MEDMEM_VtkMeshDriver.hxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #ifndef VTK_MESH_DRIVER_HXX
00024 #define VTK_MESH_DRIVER_HXX
00025 
00026 #include <cstring>
00027 
00028 #include "MEDMEM.hxx"
00029 
00030 #include <string>
00031 #include <vector>
00032 #include "MEDMEM_define.hxx"
00033 #include "MEDMEM_GenDriver.hxx"
00034 
00035 #include "MEDMEM_STRING.hxx"
00036 #include "MEDMEM_Exception.hxx"
00037 #include "MEDMEM_Utilities.hxx"
00038 
00039 #include <fstream>
00040 
00041 
00049 namespace MEDMEM {
00050 class GMESH;
00051 class FAMILY;
00052 class GROUP;
00053 class CONNECTIVITY;
00054 class MEDMEM_EXPORT _VTK_BinaryWriter;
00055 
00056 class MEDMEM_EXPORT VTK_MESH_DRIVER : public GENDRIVER
00057 {
00058 protected:
00059 
00060   const GMESH * _ptrMesh;
00061   std::string   _meshName;
00062   mutable std::ofstream *    _vtkFile ;     // The _vtkFile used to write Meshes to _filename
00063   mutable _VTK_BinaryWriter* _binaryFile;
00064 
00065 public :
00066 
00070   VTK_MESH_DRIVER() ;
00074   VTK_MESH_DRIVER(const std::string & fileName, const GMESH * ptrMesh) ;
00078   VTK_MESH_DRIVER(const VTK_MESH_DRIVER & driver) ;
00079 
00083   ~VTK_MESH_DRIVER() ;
00084 
00085   void open() ;
00086   void close() ;
00087 
00088   void openConst() const throw (MEDEXCEPTION);
00089   void closeConst() const throw (MEDEXCEPTION);
00090 
00091   void write( void ) const throw (MEDEXCEPTION) ;
00092   void read ( void ) throw (MEDEXCEPTION) ;
00093 
00099   void   setMeshName(const string & meshName) ;
00103   string getMeshName() const ;
00104 
00105 private:
00106   GENDRIVER * copy ( void ) const;
00107 
00108   template <typename T>
00109   void writeBinary(const T* data, int nbValues) const throw (MEDEXCEPTION);
00110 };
00111 
00112 class MEDMEM_EXPORT _VTK_BinaryWriter
00113 {
00114   std::string _fileName;
00115   int         _binaryFile;
00116 public:
00117   _VTK_BinaryWriter(const std::string file);
00118   bool open(bool append=false) const;
00119   bool close() const;
00120 
00121   template <typename T>
00122   void write(const T* data, int nbValues) const throw (MEDEXCEPTION)
00123   {
00124     // DEBUG
00125 //     if ( sizeof(T) == sizeof(char))
00126 //       cout << data ;
00127 //     else
00128 //       for ( int i=0; i < nbValues;++i )
00129 //         cout << data[i] << " ";
00130     const void* toWrite = (const void* ) data;
00131     T* swappedData = 0;
00132     if ( sizeof(T) != sizeof(char))
00133       {
00134         // inverse bytes
00135         toWrite = (const void* )( swappedData = new T[ nbValues ]);
00136         memcpy( swappedData, data, nbValues * sizeof(T));          
00137         int* intBuf = ((int*) swappedData) - 1;
00138         int* bufEnd = (int*)((char*) swappedData + nbValues * sizeof(T));
00139         while ( ++intBuf < bufEnd )
00140           *intBuf = swapBytes( *intBuf );
00141       }
00142 #ifdef WNT
00143     ssize_t nbWritten = ::_write( _binaryFile, toWrite, nbValues * sizeof(T));
00144 #else
00145     ssize_t nbWritten = ::write( _binaryFile, toWrite, nbValues * sizeof(T));
00146 #endif
00147     if ( swappedData )
00148       delete [] swappedData;
00149     if ( nbWritten < 0 )
00150       throw MEDEXCEPTION(LOCALIZED(STRING("_VTK_BinaryWriter::Failed to write into ")<< _fileName));
00151   }
00152 };
00153 
00154 template <typename T>
00155 void VTK_MESH_DRIVER::writeBinary(const T* data, int nbValues) const throw (MEDEXCEPTION)
00156 {
00157   _binaryFile->write( data, nbValues );
00158 }
00159 
00160 }
00161 
00162 
00163 #endif /* VTK_MESH_DRIVER_HXX */