1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "DriverSTL_W_SMDS_Mesh.h"
29 #include <Basics_Utils.hxx>
31 #include "SMDS_FaceOfNodes.hxx"
32 #include "SMDS_IteratorOnIterators.hxx"
33 #include "SMDS_Mesh.hxx"
34 #include "SMDS_MeshElement.hxx"
35 #include "SMDS_MeshNode.hxx"
36 #include "SMDS_SetIterator.hxx"
37 #include "SMDS_VolumeTool.hxx"
38 #include "SMESH_File.hxx"
39 #include "SMESH_TypeDefs.hxx"
41 //#include "utilities.h"
46 // definition des constantes
47 static const int LABEL_SIZE = 80;
49 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
54 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
56 myIsAscii = theIsAscii;
59 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
61 Kernel_Utils::Localizer loc;
63 Status aResult = DRS_OK;
66 fprintf(stderr, ">> ERROR : Mesh is null \n");
69 findVolumeTriangles();
71 aResult = writeAscii();
73 aResult = writeBinary();
77 //================================================================================
79 * \brief Destructor deletes temporary faces
81 //================================================================================
83 DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh()
85 for ( unsigned i = 0; i < myVolumeTrias.size(); ++i )
86 delete myVolumeTrias[i];
89 //================================================================================
91 * \brief Finds free facets of volumes for which faces are missing in the mesh
93 //================================================================================
95 void DriverSTL_W_SMDS_Mesh::findVolumeTriangles()
97 SMDS_VolumeTool theVolume;
98 SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator();
99 std::vector< const SMDS_MeshNode*> nodes;
100 while ( vIt->more() )
102 theVolume.Set( vIt->next(), /*ignoreCentralNodes=*/false );
103 for ( int iF = 0; iF < theVolume.NbFaces(); ++iF )
104 if ( theVolume.IsFreeFace( iF ))
106 const SMDS_MeshNode** n = theVolume.GetFaceNodes(iF);
107 int nbN = theVolume.NbFaceNodes(iF);
108 nodes.assign( n, n+nbN );
109 if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*Nomedium=*/false))
111 if ( nbN == 9 && !theVolume.IsPoly() ) // facet is SMDSEntity_BiQuad_Quadrangle
113 int nbTria = nbN - 1;
114 for ( int iT = 0; iT < nbTria; ++iT )
115 myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[8], n[0+iT], n[1+iT] ));
119 int nbTria = nbN - 2;
120 for ( int iT = 0; iT < nbTria; ++iT )
121 myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[0], n[1+iT], n[2+iT] ));
128 //================================================================================
130 * \brief Return iterator on both faces in the mesh and on temporary faces
132 //================================================================================
134 SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const
136 SMDS_ElemIteratorPtr facesIter = myMesh->elementsIterator(SMDSAbs_Face);
137 SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeTrias.begin(),
138 myVolumeTrias.end()));
139 typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector;
140 TElemIterVector iters(2);
141 iters[0] = facesIter;
142 iters[1] = tmpTriaIter;
144 typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
145 return SMDS_ElemIteratorPtr( new TItersIter( iters ));
150 static void writeInteger( const Standard_Integer& theVal, SMESH_File& ofile )
159 Standard_Integer entier;
160 entier = u.c[0] & 0xFF;
161 entier |= (u.c[1] & 0xFF) << 0x08;
162 entier |= (u.c[2] & 0xFF) << 0x10;
163 entier |= (u.c[3] & 0xFF) << 0x18;
165 ofile.write( entier );
168 static void writeFloat( const Standard_ShortReal& theVal, SMESH_File& ofile)
171 Standard_ShortReal f;
177 Standard_Integer entier;
179 entier = u.c[0] & 0xFF;
180 entier |= (u.c[1] & 0xFF) << 0x08;
181 entier |= (u.c[2] & 0xFF) << 0x10;
182 entier |= (u.c[3] & 0xFF) << 0x18;
184 ofile.write( entier );
187 static gp_XYZ getNormale( const SMDS_MeshNode* n1,
188 const SMDS_MeshNode* n2,
189 const SMDS_MeshNode* n3)
191 SMESH_TNodeXYZ xyz1( n1 );
192 SMESH_TNodeXYZ xyz2( n2 );
193 SMESH_TNodeXYZ xyz3( n3 );
194 gp_XYZ q1 = xyz2 - xyz1;
195 gp_XYZ q2 = xyz3 - xyz1;
197 double len = n.Modulus();
198 if ( len > std::numeric_limits<double>::min() )
204 //================================================================================
206 * \brief Return nb triangles in a decomposed mesh face
207 * \retval int - number of triangles
209 //================================================================================
211 static int getNbTriangles( const SMDS_MeshElement* face)
213 // WARNING: counting triangles must be coherent with getTriangles()
214 switch ( face->GetEntityType() )
216 case SMDSEntity_BiQuad_Triangle:
217 case SMDSEntity_BiQuad_Quadrangle:
218 return face->NbNodes() - 1;
219 // case SMDSEntity_Triangle:
220 // case SMDSEntity_Quad_Triangle:
221 // case SMDSEntity_Quadrangle:
222 // case SMDSEntity_Quad_Quadrangle:
223 // case SMDSEntity_Polygon:
224 // case SMDSEntity_Quad_Polygon:
226 return face->NbNodes() - 2;
231 //================================================================================
233 * \brief Decompose a mesh face into triangles
234 * \retval int - number of triangles
236 //================================================================================
238 static int getTriangles( const SMDS_MeshElement* face,
239 const SMDS_MeshNode** nodes)
241 // WARNING: decomposing into triangles must be coherent with getNbTriangles()
243 SMDS_NodeIteratorPtr nIt = face->interlacedNodesIterator();
244 nodes[ i++ ] = nIt->next();
245 nodes[ i++ ] = nIt->next();
247 const SMDSAbs_EntityType type = face->GetEntityType();
250 case SMDSEntity_BiQuad_Triangle:
251 case SMDSEntity_BiQuad_Quadrangle:
252 nbTria = ( type == SMDSEntity_BiQuad_Triangle ) ? 6 : 8;
253 nodes[ i++ ] = face->GetNode( nbTria );
254 while ( i < 3*(nbTria-1) )
256 nodes[ i++ ] = nodes[ i-2 ];
257 nodes[ i++ ] = nIt->next();
258 nodes[ i++ ] = nodes[ 2 ];
260 nodes[ i++ ] = nodes[ i-2 ];
261 nodes[ i++ ] = nodes[ 0 ];
262 nodes[ i++ ] = nodes[ 2 ];
265 // case SMDSEntity_Triangle:
266 // case SMDSEntity_Quad_Triangle:
267 // case SMDSEntity_Quadrangle:
268 // case SMDSEntity_Quad_Quadrangle:
269 // case SMDSEntity_Polygon:
270 // case SMDSEntity_Quad_Polygon:
271 nbTria = face->NbNodes() - 2;
272 nodes[ i++ ] = nIt->next();
273 while ( i < 3*nbTria )
275 nodes[ i++ ] = nodes[ 0 ];
276 nodes[ i++ ] = nodes[ i-2 ];
277 nodes[ i++ ] = nIt->next();
286 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
288 Status aResult = DRS_OK;
289 if ( myFile.empty() ) {
290 fprintf(stderr, ">> ERREOR : invalid file name \n");
294 SMESH_File aFile( myFile, /*openForReading=*/false );
295 aFile.openForWriting();
297 std::string buf("solid\n");
298 aFile.writeRaw( buf.c_str(), buf.size() );
301 const SMDS_MeshNode* triaNodes[2048];
303 SMDS_ElemIteratorPtr itFaces = getFaces();
304 while ( itFaces->more() )
306 const SMDS_MeshElement* aFace = itFaces->next();
307 int nbTria = getTriangles( aFace, triaNodes );
309 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
311 gp_XYZ normale = getNormale( triaNodes[iN],
315 " facet normal % 12e % 12e % 12e\n"
317 normale.X(), normale.Y(), normale.Z());
318 aFile.writeRaw ( sval, 70 + strlen( sval + 70 )); // at least 70 but can be more (WIN)
320 for ( int jN = 0; jN < 3; ++jN, ++iN )
322 SMESH_TNodeXYZ node = triaNodes[iN];
324 " vertex % 12e % 12e % 12e\n",
325 node.X(), node.Y(), node.Z() );
326 aFile.writeRaw ( sval, 54 + strlen( sval + 54 ));
328 aFile.writeRaw (" endloop\n"
332 aFile.writeRaw ("endsolid\n" , 9 );
337 //================================================================================
339 * \brief Writes all triangles in binary format
340 * \return Driver_Mesh::Status - DRS_FAIL if no file name is provided
342 //================================================================================
344 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
346 Status aResult = DRS_OK;
348 if ( myFile.empty() ) {
349 fprintf(stderr, ">> ERREOR : invalid filename \n");
353 SMESH_File aFile( myFile );
354 aFile.openForWriting();
356 // we first count the number of triangles
357 int nbTri = myVolumeTrias.size();
359 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
360 while ( itFaces->more() ) {
361 const SMDS_MeshElement* aFace = itFaces->next();
362 nbTri += getNbTriangles( aFace );
365 std::string sval( LABEL_SIZE, ' ' );
366 aFile.write( sval.c_str(), LABEL_SIZE );
368 // write number of triangles
369 writeInteger( nbTri, aFile );
371 // Loop writing nodes
375 const SMDS_MeshNode* triaNodes[2048];
377 SMDS_ElemIteratorPtr itFaces = getFaces();
378 while ( itFaces->more() )
380 const SMDS_MeshElement* aFace = itFaces->next();
381 int nbTria = getTriangles( aFace, triaNodes );
383 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
385 gp_XYZ normale = getNormale( triaNodes[iN],
388 writeFloat(normale.X(),aFile);
389 writeFloat(normale.Y(),aFile);
390 writeFloat(normale.Z(),aFile);
392 for ( int jN = 0; jN < 3; ++jN, ++iN )
394 const SMDS_MeshNode* node = triaNodes[iN];
395 writeFloat(node->X(),aFile);
396 writeFloat(node->Y(),aFile);
397 writeFloat(node->Z(),aFile);
399 aFile.writeRaw ( &dum, 2 );