1 // Copyright (C) 2007-2014 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"
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"
35 #include <Basics_Utils.hxx>
37 //#include "utilities.h"
42 // definition des constantes
43 static const int LABEL_SIZE = 80;
45 DriverSTL_W_SMDS_Mesh::DriverSTL_W_SMDS_Mesh()
50 void DriverSTL_W_SMDS_Mesh::SetIsAscii( const bool theIsAscii )
52 myIsAscii = theIsAscii;
55 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::Perform()
57 Kernel_Utils::Localizer loc;
59 Status aResult = DRS_OK;
62 fprintf(stderr, ">> ERROR : Mesh is null \n");
65 findVolumeTriangles();
67 aResult = writeAscii();
69 aResult = writeBinary();
73 //================================================================================
75 * \brief Destructor deletes temporary faces
77 //================================================================================
79 DriverSTL_W_SMDS_Mesh::~DriverSTL_W_SMDS_Mesh()
81 for ( unsigned i = 0; i < myVolumeTrias.size(); ++i )
82 delete myVolumeTrias[i];
85 //================================================================================
87 * \brief Finds free facets of volumes for which faces are missing in the mesh
89 //================================================================================
91 void DriverSTL_W_SMDS_Mesh::findVolumeTriangles()
93 SMDS_VolumeTool theVolume;
94 SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator();
95 std::vector< const SMDS_MeshNode*> nodes;
98 theVolume.Set( vIt->next(), /*ignoreCentralNodes=*/false );
99 for ( int iF = 0; iF < theVolume.NbFaces(); ++iF )
100 if ( theVolume.IsFreeFace( iF ))
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))
107 if ( nbN == 9 && !theVolume.IsPoly() ) // facet is SMDSEntity_BiQuad_Quadrangle
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] ));
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] ));
124 //================================================================================
126 * \brief Return iterator on both faces in the mesh and on temporary faces
128 //================================================================================
130 SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const
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;
140 typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
141 return SMDS_ElemIteratorPtr( new TItersIter( iters ));
146 static void writeInteger( const Standard_Integer& theVal, SMESH_File& ofile )
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;
161 ofile.write( entier );
164 static void writeFloat( const Standard_ShortReal& theVal, SMESH_File& ofile)
167 Standard_ShortReal f;
173 Standard_Integer entier;
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;
180 ofile.write( entier );
183 static gp_XYZ getNormale( const SMDS_MeshNode* n1,
184 const SMDS_MeshNode* n2,
185 const SMDS_MeshNode* n3)
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;
193 double len = n.Modulus();
194 if ( len > std::numeric_limits<double>::min() )
200 //================================================================================
202 * \brief Return nb triangles in a decomposed mesh face
203 * \retval int - number of triangles
205 //================================================================================
207 static int getNbTriangles( const SMDS_MeshElement* face)
209 // WARNING: counting triangles must be coherent with getTriangles()
210 switch ( face->GetEntityType() )
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:
222 return face->NbNodes() - 2;
227 //================================================================================
229 * \brief Decompose a mesh face into triangles
230 * \retval int - number of triangles
232 //================================================================================
234 static int getTriangles( const SMDS_MeshElement* face,
235 const SMDS_MeshNode** nodes)
237 // WARNING: decomposing into triangles must be coherent with getNbTriangles()
239 SMDS_NodeIteratorPtr nIt = face->interlacedNodesIterator();
240 nodes[ i++ ] = nIt->next();
241 nodes[ i++ ] = nIt->next();
243 const SMDSAbs_EntityType type = face->GetEntityType();
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) )
252 nodes[ i++ ] = nodes[ i-2 ];
253 nodes[ i++ ] = nIt->next();
254 nodes[ i++ ] = nodes[ 2 ];
256 nodes[ i++ ] = nodes[ i-2 ];
257 nodes[ i++ ] = nodes[ 0 ];
258 nodes[ i++ ] = nodes[ 2 ];
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 )
271 nodes[ i++ ] = nodes[ 0 ];
272 nodes[ i++ ] = nodes[ i-2 ];
273 nodes[ i++ ] = nIt->next();
282 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
284 Status aResult = DRS_OK;
285 if ( myFile.empty() ) {
286 fprintf(stderr, ">> ERREOR : invalid file name \n");
290 SMESH_File aFile( myFile, /*openForReading=*/false );
291 aFile.openForWriting();
293 std::string buf("solid\n");
294 aFile.writeRaw( buf.c_str(), buf.size() );
297 const SMDS_MeshNode* triaNodes[2048];
299 SMDS_ElemIteratorPtr itFaces = getFaces();
300 while ( itFaces->more() )
302 const SMDS_MeshElement* aFace = itFaces->next();
303 int nbTria = getTriangles( aFace, triaNodes );
305 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
307 gp_XYZ normale = getNormale( triaNodes[iN],
311 " facet normal % 12e % 12e % 12e\n"
313 normale.X(), normale.Y(), normale.Z());
314 aFile.writeRaw ( sval, 70 );
316 for ( int jN = 0; jN < 3; ++jN, ++iN )
318 SMESH_TNodeXYZ node = triaNodes[iN];
320 " vertex % 12e % 12e % 12e\n",
321 node.X(), node.Y(), node.Z() );
322 aFile.writeRaw ( sval, 54 );
324 aFile.writeRaw (" endloop\n"
328 aFile.writeRaw ("endsolid\n" , 9 );
333 //================================================================================
335 * \brief Writes all triangles in binary format
336 * \return Driver_Mesh::Status - DRS_FAIL if no file name is provided
338 //================================================================================
340 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
342 Status aResult = DRS_OK;
344 if ( myFile.empty() ) {
345 fprintf(stderr, ">> ERREOR : invalid filename \n");
349 SMESH_File aFile( myFile );
350 aFile.openForWriting();
352 // we first count the number of triangles
353 int nbTri = myVolumeTrias.size();
355 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
356 while ( itFaces->more() ) {
357 const SMDS_MeshElement* aFace = itFaces->next();
358 nbTri += getNbTriangles( aFace );
361 std::string sval( LABEL_SIZE, ' ' );
362 aFile.write( sval.c_str(), LABEL_SIZE );
364 // write number of triangles
365 writeInteger( nbTri, aFile );
367 // Loop writing nodes
371 const SMDS_MeshNode* triaNodes[2048];
373 SMDS_ElemIteratorPtr itFaces = getFaces();
374 while ( itFaces->more() )
376 const SMDS_MeshElement* aFace = itFaces->next();
377 int nbTria = getTriangles( aFace, triaNodes );
379 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
381 gp_XYZ normale = getNormale( triaNodes[iN],
384 writeFloat(normale.X(),aFile);
385 writeFloat(normale.Y(),aFile);
386 writeFloat(normale.Z(),aFile);
388 for ( int jN = 0; jN < 3; ++jN, ++iN )
390 const SMDS_MeshNode* node = triaNodes[iN];
391 writeFloat(node->X(),aFile);
392 writeFloat(node->Y(),aFile);
393 writeFloat(node->Z(),aFile);
395 aFile.writeRaw ( &dum, 2 );