1 // Copyright (C) 2007-2012 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 myTool;
98 SMDS_VolumeIteratorPtr vIt = myMesh->volumesIterator();
101 myTool.Set( vIt->next() );
102 for ( int iF = 0; iF < myTool.NbFaces(); ++iF )
103 if ( myTool.IsFreeFace( iF ))
105 const SMDS_MeshNode** n = myTool.GetFaceNodes(iF);
106 int nbN = myTool.NbFaceNodes(iF);
107 std::vector< const SMDS_MeshNode*> nodes( n, n+nbN );
108 if ( !myMesh->FindElement( nodes, SMDSAbs_Face, /*Nomedium=*/false))
110 int nbTria = nbN - 2;
111 for ( int iT = 0; iT < nbTria; ++iT )
113 myVolumeTrias.push_back( new SMDS_FaceOfNodes( n[0], n[1+iT], n[2+iT] ));
120 //================================================================================
122 * \brief Return iterator on both faces in the mesh and on temporary faces
124 //================================================================================
126 SMDS_ElemIteratorPtr DriverSTL_W_SMDS_Mesh::getFaces() const
128 SMDS_ElemIteratorPtr facesIter = myMesh->elementsIterator(SMDSAbs_Face);
129 SMDS_ElemIteratorPtr tmpTriaIter( new SMDS_ElementVectorIterator( myVolumeTrias.begin(),
130 myVolumeTrias.end()));
131 typedef std::vector< SMDS_ElemIteratorPtr > TElemIterVector;
132 TElemIterVector iters(2);
133 iters[0] = facesIter;
134 iters[1] = tmpTriaIter;
136 typedef SMDS_IteratorOnIterators<const SMDS_MeshElement *, TElemIterVector> TItersIter;
137 return SMDS_ElemIteratorPtr( new TItersIter( iters ));
142 static void writeInteger( const Standard_Integer& theVal,
152 Standard_Integer entier;
153 entier = u.c[0] & 0xFF;
154 entier |= (u.c[1] & 0xFF) << 0x08;
155 entier |= (u.c[2] & 0xFF) << 0x10;
156 entier |= (u.c[3] & 0xFF) << 0x18;
158 ofile.Write((char *)&entier,sizeof(u.c));
161 static void writeFloat ( const Standard_ShortReal& theVal,
165 Standard_ShortReal f;
171 Standard_Integer entier;
173 entier = u.c[0] & 0xFF;
174 entier |= (u.c[1] & 0xFF) << 0x08;
175 entier |= (u.c[2] & 0xFF) << 0x10;
176 entier |= (u.c[3] & 0xFF) << 0x18;
178 ofile.Write((char *)&entier,sizeof(u.c));
181 static gp_XYZ getNormale( const SMDS_MeshNode* n1,
182 const SMDS_MeshNode* n2,
183 const SMDS_MeshNode* n3)
185 SMESH_TNodeXYZ xyz1( n1 );
186 SMESH_TNodeXYZ xyz2( n2 );
187 SMESH_TNodeXYZ xyz3( n3 );
188 gp_XYZ q1 = xyz2 - xyz1;
189 gp_XYZ q2 = xyz3 - xyz1;
191 double len = n.Modulus();
192 if ( len > std::numeric_limits<double>::min() )
198 //================================================================================
200 * \brief Decompose a mesh face into triangles
201 * \retval int - number of triangles
203 //================================================================================
205 static int getTriangles( const SMDS_MeshElement* face,
206 const SMDS_MeshNode** nodes)
208 // WARNING: implementation must be coherent with counting triangles in writeBinary()
209 int nbN = face->NbCornerNodes();
210 const int nbTria = nbN-2;
211 for ( int i = 0; nbN > 1; --nbN )
213 nodes[ i++ ] = face->GetNode( 0 );
214 nodes[ i++ ] = face->GetNode( nbN-2 );
215 nodes[ i++ ] = face->GetNode( nbN-1 );
222 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeAscii() const
224 Status aResult = DRS_OK;
225 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
226 if ( aFileName.IsEmpty() ) {
227 fprintf(stderr, ">> ERREOR : invalid file name \n");
231 OSD_File aFile = OSD_File(OSD_Path(aFileName));
232 aFile.Build(OSD_WriteOnly,OSD_Protection());
235 TCollection_AsciiString buf = TCollection_AsciiString ("solid\n");
236 aFile.Write (buf,buf.Length());buf.Clear();
238 const SMDS_MeshNode* triaNodes[2048];
240 SMDS_ElemIteratorPtr itFaces = getFaces();
241 while ( itFaces->more() )
243 const SMDS_MeshElement* aFace = itFaces->next();
244 int nbTria = getTriangles( aFace, triaNodes );
246 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
248 gp_XYZ normale = getNormale( triaNodes[iN],
252 buf += " facet normal ";
253 sprintf (sval,"% 12e",normale.X());
256 sprintf (sval,"% 12e",normale.Y());
259 sprintf (sval,"% 12e",normale.Z());
262 aFile.Write (buf,buf.Length());buf.Clear();
263 buf += " outer loop\n";
264 aFile.Write (buf,buf.Length());buf.Clear();
266 for ( int jN = 0; jN < 3; ++jN, ++iN ) {
267 const SMDS_MeshNode* node = triaNodes[iN];
269 sprintf (sval,"% 12e",node->X());
272 sprintf (sval,"% 12e",node->Y());
275 sprintf (sval,"% 12e",node->Z());
278 aFile.Write (buf,buf.Length());buf.Clear();
281 aFile.Write (buf,buf.Length());buf.Clear();
282 buf += " endfacet\n";
283 aFile.Write (buf,buf.Length());buf.Clear();
287 aFile.Write (buf,buf.Length());buf.Clear();
294 Driver_Mesh::Status DriverSTL_W_SMDS_Mesh::writeBinary() const
296 Status aResult = DRS_OK;
297 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
298 if ( aFileName.IsEmpty() ) {
299 fprintf(stderr, ">> ERREOR : invalid filename \n");
303 OSD_File aFile = OSD_File(OSD_Path(aFileName));
304 aFile.Build(OSD_WriteOnly,OSD_Protection());
306 // we first count the number of triangles
307 // WARNING: counting triangles must be coherent with getTriangles()
309 const SMDS_MeshInfo& info = myMesh->GetMeshInfo();
310 nbTri += info.NbTriangles();
311 nbTri += info.NbQuadrangles() * 2;
312 nbTri += myVolumeTrias.size();
313 if ( info.NbPolygons() > 0 )
315 SMDS_FaceIteratorPtr itFaces = myMesh->facesIterator();
316 while ( itFaces->more() )
318 const SMDS_MeshElement* aFace = itFaces->next();
319 if ( aFace->IsPoly() )
320 nbTri += aFace->NbNodes() - 2;
324 // char sval[80]; -- avoid writing not initialized memory
325 TCollection_AsciiString sval(LABEL_SIZE-1,' ');
326 aFile.Write((Standard_Address)sval.ToCString(),LABEL_SIZE);
328 // write number of triangles
329 writeInteger(nbTri,aFile);
331 // Loop writing nodes
335 const SMDS_MeshNode* triaNodes[2048];
337 SMDS_ElemIteratorPtr itFaces = getFaces();
338 while ( itFaces->more() )
340 const SMDS_MeshElement* aFace = itFaces->next();
341 int nbTria = getTriangles( aFace, triaNodes );
343 for ( int iT = 0, iN = 0; iT < nbTria; ++iT )
345 gp_XYZ normale = getNormale( triaNodes[iN],
348 writeFloat(normale.X(),aFile);
349 writeFloat(normale.Y(),aFile);
350 writeFloat(normale.Z(),aFile);
352 for ( int jN = 0; jN < 3; ++jN, ++iN )
354 const SMDS_MeshNode* node = triaNodes[iN];
355 writeFloat(node->X(),aFile);
356 writeFloat(node->Y(),aFile);
357 writeFloat(node->Z(),aFile);
359 aFile.Write (&dum,2);