Salome HOME
fe69a70e29a3d207d489298a944e47568dde09c0
[modules/med.git] / src / MEDMEM / MEDMEM_VtkMeshDriver.hxx
1 // Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #ifndef VTK_MESH_DRIVER_HXX
24 #define VTK_MESH_DRIVER_HXX
25
26 #include <cstring>
27
28 #include "MEDMEM.hxx"
29
30 #include <string>
31 #include <vector>
32 #include "MEDMEM_define.hxx"
33 #include "MEDMEM_GenDriver.hxx"
34
35 #include "MEDMEM_STRING.hxx"
36 #include "MEDMEM_Exception.hxx"
37 #include "MEDMEM_Utilities.hxx"
38
39 #include <fstream>
40
41 #ifndef WIN32
42 # include <unistd.h>
43 #endif
44
45
46 /*!
47
48   Driver Vtk for MESH (only for writing).
49
50   Generic part : implement open and close methods.
51
52 */
53 namespace MEDMEM {
54 class GMESH;
55 class FAMILY;
56 class GROUP;
57 class CONNECTIVITY;
58 class MEDMEM_EXPORT _VTK_BinaryWriter;
59
60 class MEDMEM_EXPORT VTK_MESH_DRIVER : public GENDRIVER
61 {
62 protected:
63
64   const GMESH * _ptrMesh;
65   std::string   _meshName;
66   mutable std::ofstream *    _vtkFile ;     // The _vtkFile used to write Meshes to _filename
67   mutable _VTK_BinaryWriter* _binaryFile;
68
69 public :
70
71   /*!
72     Constructor.
73   */
74   VTK_MESH_DRIVER() ;
75   /*!
76     Constructor.
77   */
78   VTK_MESH_DRIVER(const std::string & fileName, const GMESH * ptrMesh) ;
79   /*!
80     Copy constructor.
81   */
82   VTK_MESH_DRIVER(const VTK_MESH_DRIVER & driver) ;
83
84   /*!
85     Destructor.
86   */
87   ~VTK_MESH_DRIVER() ;
88
89   void open() ;
90   void close() ;
91
92   void openConst() const throw (MEDEXCEPTION);
93   void closeConst() const throw (MEDEXCEPTION);
94
95   void write( void ) const throw (MEDEXCEPTION) ;
96   void read ( void ) throw (MEDEXCEPTION) ;
97
98   /*!
99     Set the name of the MESH asked in file.
100
101     It could be different than the name of the MESH object.
102   */
103   void   setMeshName(const string & meshName) ;
104   /*!
105     Get the name of the MESH asked in file.
106   */
107   string getMeshName() const ;
108
109 private:
110   GENDRIVER * copy ( void ) const;
111
112   template <typename T>
113   void writeBinary(const T* data, int nbValues) const throw (MEDEXCEPTION);
114 };
115
116 class MEDMEM_EXPORT _VTK_BinaryWriter
117 {
118   std::string _fileName;
119   int         _binaryFile;
120 public:
121   _VTK_BinaryWriter(const std::string file);
122   bool open(bool append=false) const;
123   bool close() const;
124
125   template <typename T>
126   void write(const T* data, int nbValues) const throw (MEDEXCEPTION)
127   {
128     // DEBUG
129 //     if ( sizeof(T) == sizeof(char))
130 //       cout << data ;
131 //     else
132 //       for ( int i=0; i < nbValues;++i )
133 //         cout << data[i] << " ";
134     const void* toWrite = (const void* ) data;
135     T* swappedData = 0;
136     if ( sizeof(T) != sizeof(char))
137       {
138         // inverse bytes
139         toWrite = (const void* )( swappedData = new T[ nbValues ]);
140         memcpy( swappedData, data, nbValues * sizeof(T));          
141         int* intBuf = ((int*) swappedData) - 1;
142         int* bufEnd = (int*)((char*) swappedData + nbValues * sizeof(T));
143         while ( ++intBuf < bufEnd )
144           *intBuf = swapBytes( *intBuf );
145       }
146 #ifdef WIN32
147     ssize_t nbWritten = ::_write( _binaryFile, toWrite, nbValues * sizeof(T));
148 #else
149     ssize_t nbWritten = ::write( _binaryFile, toWrite, nbValues * sizeof(T));
150 #endif
151     if ( swappedData )
152       delete [] swappedData;
153     if ( nbWritten < 0 )
154       throw MEDEXCEPTION(LOCALIZED(STRING("_VTK_BinaryWriter::Failed to write into ")<< _fileName));
155   }
156 };
157
158 template <typename T>
159 void VTK_MESH_DRIVER::writeBinary(const T* data, int nbValues) const throw (MEDEXCEPTION)
160 {
161   _binaryFile->write( data, nbValues );
162 }
163
164 }
165
166
167 #endif /* VTK_MESH_DRIVER_HXX */