Salome HOME
Copyright update 2022
[modules/smesh.git] / src / DriverSTL / DriverSTL_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2022  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, or (at your option) any later version.
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 #include "DriverSTL_W_SMDS_Mesh.h"
24
25 #ifdef WIN32
26 #define NOMINMAX
27 #endif
28
29 #include <Basics_Utils.hxx>
30
31 #include "SMDS_FaceOfNodes.hxx"
32 #include "SMDS_IteratorOnIterators.hxx"
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_PolygonalFaceOfNodes.hxx"
35 #include "SMDS_SetIterator.hxx"
36 #include "SMDS_VolumeTool.hxx"
37 #include "SMESH_File.hxx"
38 #include "SMESH_MeshAlgos.hxx"
39 #include "SMESH_TypeDefs.hxx"
40
41 #include <limits>
42
43
44 // definition des constantes 
45 static const int LABEL_SIZE = 80;
46
47 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
48 {
49   myIsAscii = false;
50 }
51
52 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
53 {
54   myIsAscii = theIsAscii;
55 }
56
57 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
58 {
59   Kernel_Utils::Localizer loc;
60
61   Status aResult = DRS_OK;
62
63   if ( !myMesh ) {
64     fprintf(stderr, ">> ERROR : Mesh is null \n");
65     return DRS_FAIL;
66   }
67   findVolumeTriangles();
68   if ( myIsAscii )
69     aResult = writeAscii();
70   else
71     aResult = writeBinary();
72
73   return aResult;
74 }
75
76 //================================================================================
77 /*!
78  * \brief Destructor deletes temporary faces
79  */
80 //================================================================================
81
82 DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh()
83 {
84   for ( unsigned i = 0; i < myVolumeFacets.size(); ++i )
85     delete myVolumeFacets[i];
86 }
87
88 //================================================================================
89 /*!
90  * \brief Finds free facets of volumes for which faces are missing in the mesh
91  */
92 //================================================================================
93
94 void DriverSTL_W_SMDS_Mesh::findVolumeTriangles()
95 {
96   myNbVolumeTrias = 0;
97
98   SMDS_VolumeTool theVolume;
99   SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator();
100   std::vector< const SMDS_MeshNode*> nodes;
101   while ( vIt->more() )
102   {
103     theVolume.Set( vIt->next(), /*ignoreCentralNodes=*/false );
104     for ( int iF = 0; iF < theVolume.NbFaces(); ++iF )
105       if ( theVolume.IsFreeFace( iF ))
106       {
107         const SMDS_MeshNode** n = theVolume.GetFaceNodes(iF);
108         int                 nbN = theVolume.NbFaceNodes(iF);
109         nodes.assign( n, n+nbN );
110         if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*noMedium=*/false))
111         {
112           if (( nbN == 9 || nbN == 7 ) &&
113               ( !theVolume.IsPoly() )) // facet is bi-quaratic
114           {
115             int nbTria = nbN - 1;
116             for ( int iT = 0; iT < nbTria; ++iT )
117               myVolumeFacets.push_back( new SMDS_FaceOfNodes( n[8], n[0+iT], n[1+iT] ));
118             myNbVolumeTrias += nbTria;
119           }
120           else
121           {
122             myVolumeFacets.push_back( new SMDS_PolygonalFaceOfNodes( nodes ));
123             myNbVolumeTrias += nbN - 2;
124           }
125         }
126       }
127   }
128 }
129
130 //================================================================================
131 /*!
132  * \brief Return iterator on both faces in the mesh and on temporary faces
133  */
134 //================================================================================
135
136 SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const
137 {
138   SMDS_ElemIteratorPtr facesIter = myMesh->elementsIterator(SMDSAbs_Face);
139   SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeFacets.begin(),
140                                                                     myVolumeFacets.end()));
141   typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector;
142   TElemIterVector iters(2);
143   iters[0] = facesIter;
144   iters[1] = tmpTriaIter;
145   
146   typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
147   return SMDS_ElemIteratorPtr( new TItersIter( iters ));
148 }
149
150 // static methods
151
152 static void writeInteger( const Standard_Integer& theVal, SMESH_File& ofile )
153 {
154   union {
155     Standard_Integer i;
156     char c[4];
157   } u;
158
159   u.i = theVal;
160
161   Standard_Integer entier;
162   entier  =  u.c[0] & 0xFF;
163   entier |= (u.c[1] & 0xFF) << 0x08;
164   entier |= (u.c[2] & 0xFF) << 0x10;
165   entier |= (u.c[3] & 0xFF) << 0x18;
166
167   ofile.write( entier );
168 }
169
170 static void writeFloat( const Standard_ShortReal& theVal, SMESH_File& ofile)
171 {
172   union {
173     Standard_ShortReal f;
174     char c[4];
175   } u;
176
177   u.f = theVal;
178
179   Standard_Integer entier;
180
181   entier  =  u.c[0] & 0xFF;
182   entier |= (u.c[1] & 0xFF) << 0x08;
183   entier |= (u.c[2] & 0xFF) << 0x10;
184   entier |= (u.c[3] & 0xFF) << 0x18;
185
186   ofile.write( entier );
187 }
188
189 static gp_XYZ getNormale( const SMDS_MeshNode* n1,
190                           const SMDS_MeshNode* n2,
191                           const SMDS_MeshNode* n3)
192 {
193   SMESH_TNodeXYZ xyz1( n1 );
194   SMESH_TNodeXYZ xyz2( n2 );
195   SMESH_TNodeXYZ xyz3( n3 );
196   gp_XYZ q1 = xyz2 - xyz1;
197   gp_XYZ q2 = xyz3 - xyz1;
198   gp_XYZ n  = q1 ^ q2;
199   double len = n.Modulus();
200   if ( len > std::numeric_limits<double>::min() )
201     n /= len;
202
203   return n;
204 }
205
206 // private methods
207
208 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
209 {
210   Status aResult = DRS_OK;
211   if ( myFile.empty() ) {
212     fprintf(stderr, ">> ERREOR : invalid file name \n");
213     return DRS_FAIL;
214   }
215
216   SMESH_File aFile( myFile, /*openForReading=*/false );
217   aFile.openForWriting();
218
219   std::string buf("solid ");
220   buf += myName + "\n";
221   aFile.writeRaw( buf.c_str(), buf.size() );
222
223   char sval[128];
224   std::vector< const SMDS_MeshNode* > triaNodes;
225
226   SMESH_MeshAlgos::Triangulate triangulator;
227
228   SMDS_ElemIteratorPtr itFaces = getFaces();
229   while ( itFaces->more() )
230   {
231     const SMDS_MeshElement* aFace = itFaces->next();
232     int nbTria = triangulator.GetTriangles( aFace, triaNodes );
233
234     for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
235     {
236       gp_XYZ normale = getNormale( triaNodes[iN],
237                                    triaNodes[iN+1],
238                                    triaNodes[iN+2] );
239       sprintf (sval,
240                " facet normal % 12e % 12e % 12e\n"
241                "   outer loop\n" ,
242                normale.X(), normale.Y(), normale.Z());
243       aFile.writeRaw ( sval, 70 + strlen( sval + 70 )); // at least 70 but can be more (WIN)
244
245       for ( int jN = 0; jN < 3; ++jN, ++iN )
246       {
247         SMESH_TNodeXYZ node = triaNodes[iN];
248         sprintf (sval,
249                  "     vertex % 12e % 12e % 12e\n",
250                  node.X(), node.Y(), node.Z() );
251         aFile.writeRaw ( sval, 54 + strlen( sval + 54 ));
252       }
253       aFile.writeRaw ("   endloop\n"
254                       " endfacet\n", 21 );
255     }
256   }
257   buf = "endsolid " + myName + "\n";
258   aFile.writeRaw( buf.c_str(), buf.size() );
259
260   return aResult;
261 }
262
263 //================================================================================
264 /*!
265  * \brief Writes all triangles in binary format
266  *  \return Driver_Mesh::Status - DRS_FAIL if no file name is provided
267  */
268 //================================================================================
269
270 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
271 {
272   Status aResult = DRS_OK;
273
274   if ( myFile.empty() ) {
275     fprintf(stderr, ">> ERREOR : invalid filename \n");
276     return DRS_FAIL;
277   }
278
279   SMESH_File aFile( myFile );
280   aFile.openForWriting();
281
282   SMESH_MeshAlgos::Triangulate triangulator;
283
284   // we first count the number of triangles
285   int nbTri = myNbVolumeTrias;
286   {
287     SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
288     while ( itFaces->more() ) {
289       const SMDS_MeshElement* aFace = itFaces->next();
290       nbTri += triangulator.GetNbTriangles( aFace );
291     }
292   }
293   std::string sval( LABEL_SIZE, ' ' );
294   if ( !myName.empty() )
295   {
296     sval = "name: " + myName;
297     sval.resize( LABEL_SIZE, ' ' );
298   }
299   aFile.write( sval.c_str(), LABEL_SIZE );
300
301   // write number of triangles
302   writeInteger( nbTri, aFile );  
303
304   // Loop writing nodes
305
306   int dum=0;
307
308   std::vector< const SMDS_MeshNode* > triaNodes;
309
310   SMDS_ElemIteratorPtr itFaces = getFaces();
311   while ( itFaces->more() )
312   {
313     const SMDS_MeshElement* aFace = itFaces->next();
314     int nbTria = triangulator.GetTriangles( aFace, triaNodes );
315     
316     for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
317     {
318       gp_XYZ normale = getNormale( triaNodes[iN],
319                                    triaNodes[iN+1],
320                                    triaNodes[iN+2] );
321       writeFloat(normale.X(),aFile);
322       writeFloat(normale.Y(),aFile);
323       writeFloat(normale.Z(),aFile);
324
325       for ( int jN = 0; jN < 3; ++jN, ++iN )
326       {
327         const SMDS_MeshNode* node = triaNodes[iN];
328         writeFloat(node->X(),aFile);
329         writeFloat(node->Y(),aFile);
330         writeFloat(node->Z(),aFile);
331       }
332       aFile.writeRaw ( &dum, 2 );
333     }
334   }
335
336   return aResult;
337 }