]> SALOME platform Git repositories - modules/smesh.git/blob - src/DriverSTL/DriverSTL_W_SMDS_Mesh.cxx
Salome HOME
refs #255: 7.5.6. Generation of the boundary particles for particulate boundary model
[modules/smesh.git] / src / DriverSTL / DriverSTL_W_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2016  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\n");
220   aFile.writeRaw( buf.c_str(), buf.size() );
221
222   char sval[128];
223   std::vector< const SMDS_MeshNode* > triaNodes;
224
225   SMESH_MeshAlgos::Triangulate triangulator;
226
227   SMDS_ElemIteratorPtr itFaces = getFaces();
228   while ( itFaces->more() )
229   {
230     const SMDS_MeshElement* aFace = itFaces->next();
231     int nbTria = triangulator.GetTriangles( aFace, triaNodes );
232
233     for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
234     {
235       gp_XYZ normale = getNormale( triaNodes[iN],
236                                    triaNodes[iN+1],
237                                    triaNodes[iN+2] );
238       sprintf (sval,
239                " facet normal % 12e % 12e % 12e\n"
240                "   outer loop\n" ,
241                normale.X(), normale.Y(), normale.Z());
242       aFile.writeRaw ( sval, 70 + strlen( sval + 70 )); // at least 70 but can be more (WIN)
243
244       for ( int jN = 0; jN < 3; ++jN, ++iN )
245       {
246         SMESH_TNodeXYZ node = triaNodes[iN];
247         sprintf (sval,
248                  "     vertex % 12e % 12e % 12e\n",
249                  node.X(), node.Y(), node.Z() );
250         aFile.writeRaw ( sval, 54 + strlen( sval + 54 ));
251       }
252       aFile.writeRaw ("   endloop\n"
253                       " endfacet\n", 21 );
254     }
255   }
256   aFile.writeRaw ("endsolid\n" , 9 );
257
258   return aResult;
259 }
260
261 //================================================================================
262 /*!
263  * \brief Writes all triangles in binary format
264  *  \return Driver_Mesh::Status - DRS_FAIL if no file name is provided
265  */
266 //================================================================================
267
268 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
269 {
270   Status aResult = DRS_OK;
271
272   if ( myFile.empty() ) {
273     fprintf(stderr, ">> ERREOR : invalid filename \n");
274     return DRS_FAIL;
275   }
276
277   SMESH_File aFile( myFile );
278   aFile.openForWriting();
279
280   SMESH_MeshAlgos::Triangulate triangulator;
281
282   // we first count the number of triangles
283   int nbTri = myNbVolumeTrias;
284   {
285     SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
286     while ( itFaces->more() ) {
287       const SMDS_MeshElement* aFace = itFaces->next();
288       nbTri += triangulator.GetNbTriangles( aFace );
289     }
290   }
291   std::string sval( LABEL_SIZE, ' ' );
292   aFile.write( sval.c_str(), LABEL_SIZE );
293
294   // write number of triangles
295   writeInteger( nbTri, aFile );  
296
297   // Loop writing nodes
298
299   int dum=0;
300
301   std::vector< const SMDS_MeshNode* > triaNodes;
302
303   SMDS_ElemIteratorPtr itFaces = getFaces();
304   while ( itFaces->more() )
305   {
306     const SMDS_MeshElement* aFace = itFaces->next();
307     int nbTria = triangulator.GetTriangles( aFace, triaNodes );
308     
309     for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
310     {
311       gp_XYZ normale = getNormale( triaNodes[iN],
312                                    triaNodes[iN+1],
313                                    triaNodes[iN+2] );
314       writeFloat(normale.X(),aFile);
315       writeFloat(normale.Y(),aFile);
316       writeFloat(normale.Z(),aFile);
317
318       for ( int jN = 0; jN < 3; ++jN, ++iN )
319       {
320         const SMDS_MeshNode* node = triaNodes[iN];
321         writeFloat(node->X(),aFile);
322         writeFloat(node->Y(),aFile);
323         writeFloat(node->Z(),aFile);
324       }
325       aFile.writeRaw ( &dum, 2 );
326     }
327   }
328
329   return aResult;
330 }