1 // Copyright (C) 2007-2013 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.
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
26 #include "DriverSTL_W_SMDS_Mesh.h"
28 #include "SMDS_FaceOfNodes.hxx"
29 #include "SMDS_IteratorOnIterators.hxx"
30 #include "SMDS_Mesh.hxx"
31 #include "SMDS_MeshElement.hxx"
32 #include "SMDS_MeshNode.hxx"
33 #include "SMDS_SetIterator.hxx"
34 #include "SMDS_VolumeTool.hxx"
35 #include "SMESH_TypeDefs.hxx"
37 #include <OSD_File.hxx>
38 #include <OSD_Path.hxx>
39 #include <OSD_Protection.hxx>
40 #include <TCollection_AsciiString.hxx>
42 #include <Basics_Utils.hxx>
44 #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, OSD_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((char *)&entier,sizeof(u.c));
168 static void writeFloat ( const Standard_ShortReal& theVal,
172 Standard_ShortReal f;
178 Standard_Integer entier;
180 entier = u.c[0] & 0xFF;
181 entier |= (u.c[1] & 0xFF) << 0x08;
182 entier |= (u.c[2] & 0xFF) << 0x10;
183 entier |= (u.c[3] & 0xFF) << 0x18;
185 ofile.Write((char *)&entier,sizeof(u.c));
188 static gp_XYZ getNormale( const SMDS_MeshNode* n1,
189 const SMDS_MeshNode* n2,
190 const SMDS_MeshNode* n3)
192 SMESH_TNodeXYZ xyz1( n1 );
193 SMESH_TNodeXYZ xyz2( n2 );
194 SMESH_TNodeXYZ xyz3( n3 );
195 gp_XYZ q1 = xyz2 - xyz1;
196 gp_XYZ q2 = xyz3 - xyz1;
198 double len = n.Modulus();
199 if ( len > std::numeric_limits<double>::min() )
205 //================================================================================
207 * \brief Return nb triangles in a decomposed mesh face
208 * \retval int - number of triangles
210 //================================================================================
212 static int getNbTriangles( const SMDS_MeshElement* face)
214 // WARNING: counting triangles must be coherent with getTriangles()
215 switch ( face->GetEntityType() )
217 case SMDSEntity_BiQuad_Triangle:
218 case SMDSEntity_BiQuad_Quadrangle:
219 return face->NbNodes() - 1;
220 // case SMDSEntity_Triangle:
221 // case SMDSEntity_Quad_Triangle:
222 // case SMDSEntity_Quadrangle:
223 // case SMDSEntity_Quad_Quadrangle:
224 // case SMDSEntity_Polygon:
225 // case SMDSEntity_Quad_Polygon:
227 return face->NbNodes() - 2;
232 //================================================================================
234 * \brief Decompose a mesh face into triangles
235 * \retval int - number of triangles
237 //================================================================================
239 static int getTriangles( const SMDS_MeshElement* face,
240 const SMDS_MeshNode** nodes)
242 // WARNING: decomposing into triangles must be coherent with getNbTriangles()
244 SMDS_NodeIteratorPtr nIt = face->interlacedNodesIterator();
245 nodes[ i++ ] = nIt->next();
246 nodes[ i++ ] = nIt->next();
248 const SMDSAbs_EntityType type = face->GetEntityType();
251 case SMDSEntity_BiQuad_Triangle:
252 case SMDSEntity_BiQuad_Quadrangle:
253 nbTria = ( type == SMDSEntity_BiQuad_Triangle ) ? 6 : 8;
254 nodes[ i++ ] = face->GetNode( nbTria );
255 while ( i < 3*(nbTria-1) )
257 nodes[ i++ ] = nodes[ i-2 ];
258 nodes[ i++ ] = nIt->next();
259 nodes[ i++ ] = nodes[ 2 ];
261 nodes[ i++ ] = nodes[ i-2 ];
262 nodes[ i++ ] = nodes[ 0 ];
263 nodes[ i++ ] = nodes[ 2 ];
266 // case SMDSEntity_Triangle:
267 // case SMDSEntity_Quad_Triangle:
268 // case SMDSEntity_Quadrangle:
269 // case SMDSEntity_Quad_Quadrangle:
270 // case SMDSEntity_Polygon:
271 // case SMDSEntity_Quad_Polygon:
272 nbTria = face->NbNodes() - 2;
273 nodes[ i++ ] = nIt->next();
274 while ( i < 3*nbTria )
276 nodes[ i++ ] = nodes[ 0 ];
277 nodes[ i++ ] = nodes[ i-2 ];
278 nodes[ i++ ] = nIt->next();
287 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
289 Status aResult = DRS_OK;
290 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
291 if ( aFileName.IsEmpty() ) {
292 fprintf(stderr, ">> ERREOR : invalid file name \n");
296 OSD_File aFile = OSD_File(OSD_Path(aFileName));
297 aFile.Build(OSD_WriteOnly,OSD_Protection());
300 TCollection_AsciiString buf = TCollection_AsciiString ("solid\n");
301 aFile.Write (buf,buf.Length());buf.Clear();
303 const SMDS_MeshNode* triaNodes[2048];
305 SMDS_ElemIteratorPtr itFaces = getFaces();
306 while ( itFaces->more() )
308 const SMDS_MeshElement* aFace = itFaces->next();
309 int nbTria = getTriangles( aFace, triaNodes );
311 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
313 gp_XYZ normale = getNormale( triaNodes[iN],
317 buf += " facet normal ";
318 sprintf (sval,"% 12e",normale.X());
321 sprintf (sval,"% 12e",normale.Y());
324 sprintf (sval,"% 12e",normale.Z());
327 aFile.Write (buf,buf.Length());buf.Clear();
328 buf += " outer loop\n";
329 aFile.Write (buf,buf.Length());buf.Clear();
331 for ( int jN = 0; jN < 3; ++jN, ++iN ) {
332 const SMDS_MeshNode* node = triaNodes[iN];
334 sprintf (sval,"% 12e",node->X());
337 sprintf (sval,"% 12e",node->Y());
340 sprintf (sval,"% 12e",node->Z());
343 aFile.Write (buf,buf.Length());buf.Clear();
346 aFile.Write (buf,buf.Length());buf.Clear();
347 buf += " endfacet\n";
348 aFile.Write (buf,buf.Length());buf.Clear();
352 aFile.Write (buf,buf.Length());buf.Clear();
359 //================================================================================
361 * \brief Writes all triangles in binary format
362 * \return Driver_Mesh::Status - DRS_FAIL if no file name is provided
364 //================================================================================
366 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
368 Status aResult = DRS_OK;
369 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
370 if ( aFileName.IsEmpty() ) {
371 fprintf(stderr, ">> ERREOR : invalid filename \n");
375 OSD_File aFile = OSD_File(OSD_Path(aFileName));
376 aFile.Build(OSD_WriteOnly,OSD_Protection());
378 // we first count the number of triangles
379 int nbTri = myVolumeTrias.size();
381 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
382 while ( itFaces->more() ) {
383 const SMDS_MeshElement* aFace = itFaces->next();
384 nbTri += getNbTriangles( aFace );
387 // char sval[80]; -- avoid writing not initialized memory
388 TCollection_AsciiString sval(LABEL_SIZE-1,' ');
389 aFile.Write((Standard_Address)sval.ToCString(),LABEL_SIZE);
391 // write number of triangles
392 writeInteger(nbTri,aFile);
394 // Loop writing nodes
398 const SMDS_MeshNode* triaNodes[2048];
400 SMDS_ElemIteratorPtr itFaces = getFaces();
401 while ( itFaces->more() )
403 const SMDS_MeshElement* aFace = itFaces->next();
404 int nbTria = getTriangles( aFace, triaNodes );
406 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
408 gp_XYZ normale = getNormale( triaNodes[iN],
411 writeFloat(normale.X(),aFile);
412 writeFloat(normale.Y(),aFile);
413 writeFloat(normale.Z(),aFile);
415 for ( int jN = 0; jN < 3; ++jN, ++iN )
417 const SMDS_MeshNode* node = triaNodes[iN];
418 writeFloat(node->X(),aFile);
419 writeFloat(node->Y(),aFile);
420 writeFloat(node->Z(),aFile);
422 aFile.Write (&dum,2);