Salome HOME
0022523: [CEA 1096] Add a colomn "biquadratic" in "Mesh Information"
[modules/smesh.git] / src / DriverSTL / DriverSTL_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2014  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 #include "SMDS_FaceOfNodes.hxx"
26 #include "SMDS_IteratorOnIterators.hxx"
27 #include "SMDS_Mesh.hxx"
28 #include "SMDS_MeshElement.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "SMDS_SetIterator.hxx"
31 #include "SMDS_VolumeTool.hxx"
32 #include "SMESH_File.hxx"
33 #include "SMESH_TypeDefs.hxx"
34
35 #include <Basics_Utils.hxx>
36
37 //#include "utilities.h"
38
39 #include <limits>
40
41
42 // definition des constantes 
43 static const int LABEL_SIZE = 80;
44
45 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
46 {
47   myIsAscii = false;
48 }
49
50 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
51 {
52   myIsAscii = theIsAscii;
53 }
54
55 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
56 {
57   Kernel_Utils::Localizer loc;
58
59   Status aResult = DRS_OK;
60
61   if ( !myMesh ) {
62     fprintf(stderr, ">> ERROR : Mesh is null \n");
63     return DRS_FAIL;
64   }
65   findVolumeTriangles();
66   if ( myIsAscii )
67     aResult = writeAscii();
68   else
69     aResult = writeBinary();
70
71   return aResult;
72 }
73 //================================================================================
74 /*!
75  * \brief Destructor deletes temporary faces
76  */
77 //================================================================================
78
79 DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh()
80 {
81   for ( unsigned i = 0; i < myVolumeTrias.size(); ++i )
82     delete myVolumeTrias[i];
83 }
84
85 //================================================================================
86 /*!
87  * \brief Finds free facets of volumes for which faces are missing in the mesh
88  */
89 //================================================================================
90
91 void DriverSTL_W_SMDS_Mesh::findVolumeTriangles()
92 {
93   SMDS_VolumeTool theVolume;
94   SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator();
95   std::vector< const SMDS_MeshNode*> nodes;
96   while ( vIt->more() )
97   {
98     theVolume.Set( vIt->next(), /*ignoreCentralNodes=*/false );
99     for ( int iF = 0; iF < theVolume.NbFaces(); ++iF )
100       if ( theVolume.IsFreeFace( iF ))
101       {
102         const SMDS_MeshNode** n = theVolume.GetFaceNodes(iF);
103         int                 nbN = theVolume.NbFaceNodes(iF);
104         nodes.assign( n, n+nbN );
105         if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*Nomedium=*/false))
106         {
107           if ( nbN == 9 && !theVolume.IsPoly() ) // facet is SMDSEntity_BiQuad_Quadrangle
108           {
109             int nbTria = nbN - 1;
110             for ( int iT = 0; iT < nbTria; ++iT )
111               myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[8], n[0+iT], n[1+iT] ));
112           }
113           else
114           {
115             int nbTria = nbN - 2;
116             for ( int iT = 0; iT < nbTria; ++iT )
117               myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[0], n[1+iT], n[2+iT] ));
118           }
119         }
120       }
121   }
122 }
123
124 //================================================================================
125 /*!
126  * \brief Return iterator on both faces in the mesh and on temporary faces
127  */
128 //================================================================================
129
130 SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const
131 {
132   SMDS_ElemIteratorPtr facesIter = myMesh->elementsIterator(SMDSAbs_Face);
133   SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeTrias.begin(),
134                                                                     myVolumeTrias.end()));
135   typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector;
136   TElemIterVector iters(2);
137   iters[0] = facesIter;
138   iters[1] = tmpTriaIter;
139   
140   typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
141   return SMDS_ElemIteratorPtr( new TItersIter( iters ));
142 }
143
144 // static methods
145
146 static void writeInteger( const Standard_Integer& theVal, SMESH_File& ofile )
147 {
148   union {
149     Standard_Integer i;
150     char c[4];
151   } u;
152
153   u.i = theVal;
154
155   Standard_Integer entier;
156   entier  =  u.c[0] & 0xFF;
157   entier |= (u.c[1] & 0xFF) << 0x08;
158   entier |= (u.c[2] & 0xFF) << 0x10;
159   entier |= (u.c[3] & 0xFF) << 0x18;
160
161   ofile.write( entier );
162 }
163
164 static void writeFloat( const Standard_ShortReal& theVal, SMESH_File& ofile)
165 {
166   union {
167     Standard_ShortReal f;
168     char c[4]; 
169   } u;
170
171   u.f = theVal;
172
173   Standard_Integer entier;
174
175   entier  =  u.c[0] & 0xFF;
176   entier |= (u.c[1] & 0xFF) << 0x08;
177   entier |= (u.c[2] & 0xFF) << 0x10;
178   entier |= (u.c[3] & 0xFF) << 0x18;
179
180   ofile.write( entier );
181 }
182
183 static gp_XYZ getNormale( const SMDS_MeshNode* n1,
184                           const SMDS_MeshNode* n2,
185                           const SMDS_MeshNode* n3)
186 {
187   SMESH_TNodeXYZ xyz1( n1 );
188   SMESH_TNodeXYZ xyz2( n2 );
189   SMESH_TNodeXYZ xyz3( n3 );
190   gp_XYZ q1 = xyz2 - xyz1;
191   gp_XYZ q2 = xyz3 - xyz1;
192   gp_XYZ n  = q1 ^ q2;
193   double len = n.Modulus();
194   if ( len > std::numeric_limits<double>::min() )
195     n /= len;
196
197   return n;
198 }
199
200 //================================================================================
201 /*!
202  * \brief Return nb triangles in a decomposed mesh face
203  *  \retval int - number of triangles
204  */
205 //================================================================================
206
207 static int getNbTriangles( const SMDS_MeshElement* face)
208 {
209   // WARNING: counting triangles must be coherent with getTriangles()
210   switch ( face->GetEntityType() )
211   {
212   case SMDSEntity_BiQuad_Triangle:
213   case SMDSEntity_BiQuad_Quadrangle:
214     return face->NbNodes() - 1;
215   // case SMDSEntity_Triangle:
216   // case SMDSEntity_Quad_Triangle:
217   // case SMDSEntity_Quadrangle:
218   // case SMDSEntity_Quad_Quadrangle:
219   // case SMDSEntity_Polygon:
220   // case SMDSEntity_Quad_Polygon:
221   default:
222     return face->NbNodes() - 2;
223   }
224   return 0;
225 }
226
227 //================================================================================
228 /*!
229  * \brief Decompose a mesh face into triangles
230  *  \retval int - number of triangles
231  */
232 //================================================================================
233
234 static int getTriangles( const SMDS_MeshElement* face,
235                          const SMDS_MeshNode**   nodes)
236 {
237   // WARNING: decomposing into triangles must be coherent with getNbTriangles()
238   int nbTria, i = 0;
239   SMDS_NodeIteratorPtr nIt = face->interlacedNodesIterator();
240   nodes[ i++ ] = nIt->next();
241   nodes[ i++ ] = nIt->next();
242
243   const SMDSAbs_EntityType type = face->GetEntityType();
244   switch ( type )
245   {
246   case SMDSEntity_BiQuad_Triangle:
247   case SMDSEntity_BiQuad_Quadrangle:
248     nbTria = ( type == SMDSEntity_BiQuad_Triangle ) ? 6 : 8;
249     nodes[ i++ ] = face->GetNode( nbTria );
250     while ( i < 3*(nbTria-1) )
251     {
252       nodes[ i++ ] = nodes[ i-2 ];
253       nodes[ i++ ] = nIt->next();
254       nodes[ i++ ] = nodes[ 2 ];
255     }
256     nodes[ i++ ] = nodes[ i-2 ];
257     nodes[ i++ ] = nodes[ 0 ];
258     nodes[ i++ ] = nodes[ 2 ];
259     break;
260   default:
261     // case SMDSEntity_Triangle:
262     // case SMDSEntity_Quad_Triangle:
263     // case SMDSEntity_Quadrangle:
264     // case SMDSEntity_Quad_Quadrangle:
265     // case SMDSEntity_Polygon:
266     // case SMDSEntity_Quad_Polygon:
267     nbTria = face->NbNodes() - 2;
268     nodes[ i++ ] = nIt->next();
269     while ( i < 3*nbTria )
270     {
271       nodes[ i++ ] = nodes[ 0 ];
272       nodes[ i++ ] = nodes[ i-2 ];
273       nodes[ i++ ] = nIt->next();
274     }
275     break;
276   }
277   return nbTria;
278 }
279
280 // private methods
281
282 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
283 {
284   Status aResult = DRS_OK;
285   if ( myFile.empty() ) {
286     fprintf(stderr, ">> ERREOR : invalid file name \n");
287     return DRS_FAIL;
288   }
289
290   SMESH_File aFile( myFile, /*openForReading=*/false );
291   aFile.openForWriting();
292
293   std::string buf("solid\n");
294   aFile.writeRaw( buf.c_str(), buf.size() );
295
296   char sval[128];
297   const SMDS_MeshNode* triaNodes[2048];
298
299   SMDS_ElemIteratorPtr itFaces = getFaces();
300   while ( itFaces->more() )
301   {
302     const SMDS_MeshElement* aFace = itFaces->next();
303     int nbTria = getTriangles( aFace, triaNodes );
304     
305     for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
306     {
307       gp_XYZ normale = getNormale( triaNodes[iN],
308                                    triaNodes[iN+1],
309                                    triaNodes[iN+2] );
310       sprintf (sval,
311                " facet normal % 12e % 12e % 12e\n"
312                "   outer loop\n" ,
313                normale.X(), normale.Y(), normale.Z());
314       aFile.writeRaw ( sval, 70 );
315
316       for ( int jN = 0; jN < 3; ++jN, ++iN )
317       {
318         SMESH_TNodeXYZ node = triaNodes[iN];
319         sprintf (sval,
320                  "     vertex % 12e % 12e % 12e\n",
321                  node.X(), node.Y(), node.Z() );
322         aFile.writeRaw ( sval, 54 );
323       }
324       aFile.writeRaw ("   endloop\n"
325                       " endfacet\n", 21 );
326     } 
327   }
328   aFile.writeRaw ("endsolid\n" , 9 );
329
330   return aResult;
331 }
332
333 //================================================================================
334 /*!
335  * \brief Writes all triangles in binary format
336  *  \return Driver_Mesh::Status - DRS_FAIL if no file name is provided
337  */
338 //================================================================================
339
340 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
341 {
342   Status aResult = DRS_OK;
343
344   if ( myFile.empty() ) {
345     fprintf(stderr, ">> ERREOR : invalid filename \n");
346     return DRS_FAIL;
347   }
348
349   SMESH_File aFile( myFile );
350   aFile.openForWriting();
351
352   // we first count the number of triangles
353   int nbTri = myVolumeTrias.size();
354   {
355     SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
356     while ( itFaces->more() ) {
357       const SMDS_MeshElement* aFace = itFaces->next();
358       nbTri += getNbTriangles( aFace );
359     }
360   }
361   std::string sval( LABEL_SIZE, ' ' );
362   aFile.write( sval.c_str(), LABEL_SIZE );
363
364   // write number of triangles
365   writeInteger( nbTri, aFile );  
366
367   // Loop writing nodes
368
369   int dum=0;
370
371   const SMDS_MeshNode* triaNodes[2048];
372
373   SMDS_ElemIteratorPtr itFaces = getFaces();
374   while ( itFaces->more() )
375   {
376     const SMDS_MeshElement* aFace = itFaces->next();
377     int nbTria = getTriangles( aFace, triaNodes );
378     
379     for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
380     {
381       gp_XYZ normale = getNormale( triaNodes[iN],
382                                    triaNodes[iN+1],
383                                    triaNodes[iN+2] );
384       writeFloat(normale.X(),aFile);
385       writeFloat(normale.Y(),aFile);
386       writeFloat(normale.Z(),aFile);
387
388       for ( int jN = 0; jN < 3; ++jN, ++iN )
389       {
390         const SMDS_MeshNode* node = triaNodes[iN];
391         writeFloat(node->X(),aFile);
392         writeFloat(node->Y(),aFile);
393         writeFloat(node->Z(),aFile);
394       }
395       aFile.writeRaw ( &dum, 2 );
396     }
397   }
398
399   return aResult;
400 }