1 // Copyright (C) 2007-2016 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_R_SMDS_Mesh.h"
25 #include <Basics_Utils.hxx>
28 #include <NCollection_DataMap.hxx>
29 #include <Standard_NoMoreObject.hxx>
31 #include "SMDS_Mesh.hxx"
32 #include "SMDS_MeshElement.hxx"
33 #include "SMDS_MeshNode.hxx"
34 #include "SMESH_File.hxx"
40 //=======================================================================
43 //=======================================================================
44 inline static Standard_Integer HashCode
45 (const gp_Pnt& point, Standard_Integer Upper)
50 Standard_Integer I[6];
53 point.Coord( U.R[0], U.R[1], U.R[2] );
55 return ::HashCode(U.I[0]/23+U.I[1]/19+U.I[2]/17+U.I[3]/13+U.I[4]/11+U.I[5]/7,Upper);
57 //=======================================================================
60 //=======================================================================
61 inline static Standard_Boolean IsEqual
62 (const gp_Pnt& point1, const gp_Pnt& point2)
64 static Standard_Real tab1[3], tab2[3];
65 point1.Coord(tab1[0],tab1[1],tab1[2]);
66 point2.Coord(tab2[0],tab2[1],tab2[2]);
67 return (memcmp(tab1,tab2,sizeof(tab1)) == 0);
70 typedef NCollection_DataMap<gp_Pnt,SMDS_MeshNode*,Hasher> TDataMapOfPntNodePtr;
72 const int HEADER_SIZE = 84; // 80 chars + int
73 const int SIZEOF_STL_FACET = 50;
74 const int ASCII_LINES_PER_FACET = 7;
75 const int SIZE_OF_FLOAT = 4;
76 // const int STL_MIN_FILE_SIZE = 284;
79 //=======================================================================
80 //function : DriverSTL_R_SMDS_Mesh
82 //=======================================================================
84 DriverSTL_R_SMDS_Mesh::DriverSTL_R_SMDS_Mesh()
86 myIsCreateFaces = true;
87 myIsAscii = Standard_True;
90 //=======================================================================
91 //function : SetIsCreateFaces
93 //=======================================================================
95 void DriverSTL_R_SMDS_Mesh::SetIsCreateFaces( const bool theIsCreate )
96 { myIsCreateFaces = theIsCreate; }
98 //=======================================================================
101 //=======================================================================
103 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::Perform()
105 Kernel_Utils::Localizer loc;
107 Status aResult = DRS_OK;
109 if ( myFile.empty() ) {
110 fprintf(stderr, ">> ERREOR : invalid file name \n");
114 SMESH_File file( myFile, /*open=*/false );
115 if ( !file.open() ) {
116 fprintf(stderr, ">> ERROR : cannot open file %s \n", myFile.c_str());
117 if ( file.error().empty() )
118 fprintf(stderr, ">> ERROR : %s \n", file.error().c_str());
122 // we skip the header which is in Ascii for both modes
123 const char* data = file;
126 // we check 128 characters to detect if we have a non-ascii char
127 myIsAscii = Standard_True;
128 for (int i = 0; i < 128; ++i, ++data) {
129 if ( !isascii( *data ) && data < file.end() ) {
130 myIsAscii = Standard_False;
136 fprintf(stderr, ">> ERREOR : cannot create mesh \n");
141 aResult = readAscii( file );
143 aResult = readBinary( file );
150 static Standard_Real readFloat(SMESH_File& theFile)
157 const char* c = theFile;
160 u.i |= (c[1] & 0xFF) << 0x08;
161 u.i |= (c[2] & 0xFF) << 0x10;
162 u.i |= (c[3] & 0xFF) << 0x18;
163 theFile += SIZE_OF_FLOAT;
168 static SMDS_MeshNode* addNode(const gp_Pnt& P,
169 TDataMapOfPntNodePtr& uniqnodes,
172 SMDS_MeshNode* node = 0;
173 if ( uniqnodes.IsBound(P) ) {
174 node = uniqnodes.Find(P);
176 node = theMesh->AddNode(P.X(), P.Y(), P.Z() );
177 uniqnodes.Bind(P,node);
183 static SMDS_MeshNode* readNode(FILE* file,
184 TDataMapOfPntNodePtr& uniqnodes,
187 Standard_ShortReal coord[3];
189 fscanf(file,"%*s %f %f %f\n",&coord[0],&coord[1],&coord[2]);
191 gp_Pnt P(coord[0],coord[1],coord[2]);
192 return addNode( P, uniqnodes, theMesh );
195 static SMDS_MeshNode* readNode(SMESH_File& theFile,
196 TDataMapOfPntNodePtr& uniqnodes,
200 coord.SetX( readFloat(theFile));
201 coord.SetY( readFloat(theFile));
202 coord.SetZ( readFloat(theFile));
204 return addNode( coord, uniqnodes, theMesh );
207 //=======================================================================
208 //function : readAscii
210 //=======================================================================
212 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii(SMESH_File& theFile) const
214 Status aResult = DRS_OK;
217 if ( strncmp( "solid ", theFile, strlen("solid ")) == 0 ) // not empty
219 const char * header = theFile;
220 std::string& name = const_cast<std::string&>( myName );
221 for ( header += strlen("solid "); !iscntrl( *header ); ++header )
222 name.push_back( *header );
224 std::string::iterator i = name.begin();
225 while ( i != name.end() && isspace( *i )) ++i;
226 name.erase( name.begin(), i );
228 size_t n = name.size();
229 while ( n > 0 && isspace( name[ n - 1 ] )) --n;
234 long filesize = theFile.size();
238 FILE* file = fopen( myFile.c_str(),"r");
240 // count the number of lines
241 Standard_Integer nbLines = 0;
242 for (long ipos = 0; ipos < filesize; ++ipos)
244 if (getc(file) == '\n')
248 // go back to the beginning of the file
251 Standard_Integer nbTri = (nbLines / ASCII_LINES_PER_FACET);
253 TDataMapOfPntNodePtr uniqnodes;
255 while (getc(file) != '\n');
258 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri)
260 // skipping the facet normal
261 Standard_ShortReal normal[3];
262 fscanf(file,"%*s %*s %f %f %f\n",&normal[0],&normal[1],&normal[2]);
264 // skip the keywords "outer loop"
265 fscanf(file,"%*s %*s");
268 SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh );
269 SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh );
270 SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh );
273 myMesh->AddFace(node1,node2,node3);
275 // skip the keywords "endloop"
278 // skip the keywords "endfacet"
286 //=======================================================================
287 //function : readBinary
289 //=======================================================================
291 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary(SMESH_File& file) const
293 Status aResult = DRS_OK;
295 // the size of the file (minus the header size)
296 // must be a multiple of SIZEOF_STL_FACET
298 long filesize = file.size();
300 if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET != 0
301 // Commented to allow reading small files (ex: 1 face)
302 /*|| (filesize < STL_MIN_FILE_SIZE)*/) {
303 Standard_NoMoreObject::Raise("DriverSTL_R_SMDS_MESH::readBinary (wrong file size)");
306 // don't trust the number of triangles which is coded in the file
307 // sometimes it is wrong, and with this technique we don't need to swap endians for integer
308 Standard_Integer nbTri = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);
311 if ( strncmp( "name: ", file, strlen("name: ")) == 0 ) // name present
313 const char * header = file;
314 std::string& name = const_cast<std::string&>( myName );
315 header += strlen("name: ");
316 name.assign( header, HEADER_SIZE - strlen("name: ") - 4);
318 size_t n = name.size();
319 while ( n > 0 && isspace( name[ n - 1 ] )) --n;
326 TDataMapOfPntNodePtr uniqnodes;
328 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
331 file += 3 * SIZE_OF_FLOAT;
334 SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh );
335 SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh );
336 SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh );
339 myMesh->AddFace(node1,node2,node3);