1 // Copyright (C) 2007-2010 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
25 //=======================================================================
28 //=======================================================================
29 inline Standard_Integer HashCode
30 (const gp_Pnt& point, Standard_Integer Upper)
35 Standard_Integer I[6];
38 point.Coord(U.R[0],U.R[1],U.R[2]);
40 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);
42 static Standard_Real tab1[3];
43 static Standard_Real tab2[3];
44 //=======================================================================
47 //=======================================================================
48 inline Standard_Boolean IsEqual
49 (const gp_Pnt& point1, const gp_Pnt& point2)
51 point1.Coord(tab1[0],tab1[1],tab1[2]);
52 point2.Coord(tab2[0],tab2[1],tab2[2]);
53 return (memcmp(tab1,tab2,sizeof(tab1)) == 0);
55 #include "DriverSTL_R_SMDS_Mesh.h"
57 #include "SMDS_Mesh.hxx"
58 #include "SMDS_MeshElement.hxx"
59 #include "SMDS_MeshNode.hxx"
61 #include <OSD_Path.hxx>
62 #include <OSD_File.hxx>
63 #include <OSD_FromWhere.hxx>
64 #include <OSD_Protection.hxx>
65 #include <OSD_SingleProtection.hxx>
66 #include <Standard_NoMoreObject.hxx>
68 #include "utilities.h"
70 static const int HEADER_SIZE = 84;
71 static const int SIZEOF_STL_FACET = 50;
72 //static const int STL_MIN_FILE_SIZE = 284;
73 static const int ASCII_LINES_PER_FACET = 7;
76 //typedef NCollection_BaseCollection<SMDS_MeshNodePtr> DriverSTL_ColOfNodePtr;
79 #include <NCollection_DataMap.hxx>
80 typedef NCollection_DataMap<gp_Pnt,SMDS_MeshNode*> DriverSTL_DataMapOfPntNodePtr;
81 //=======================================================================
82 //function : DriverSTL_R_SMDS_Mesh
84 //=======================================================================
86 DriverSTL_R_SMDS_Mesh::DriverSTL_R_SMDS_Mesh()
88 myIsCreateFaces = true;
89 myIsAscii = Standard_True;
92 //=======================================================================
93 //function : SetIsCreateFaces
95 //=======================================================================
97 void DriverSTL_R_SMDS_Mesh::SetIsCreateFaces( const bool theIsCreate )
98 { myIsCreateFaces = theIsCreate; }
100 //=======================================================================
103 //=======================================================================
105 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::Perform()
107 Status aResult = DRS_OK;
109 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
110 if ( aFileName.IsEmpty() ) {
111 fprintf(stderr, ">> ERREOR : invalid file name \n");
116 Standard_IStream is(&fic);
117 if (!fic.open(aFileName.ToCString(),ios::in)) {
118 fprintf(stderr, ">> ERROR : cannot open file %s \n", aFileName.ToCString());
123 OSD_Path aPath( aFileName );
124 OSD_File file = OSD_File( aPath );
125 file.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
126 unsigned char str[128];
127 Standard_Integer lread,i;
128 Standard_Address ach;
129 ach = (Standard_Address)str;
130 // we skip the header which is in Ascii for both modes
131 file.Read(ach,HEADER_SIZE,lread);
133 // we read 128 characters to detect if we have a non-ascii char
134 file.Read(ach,sizeof(str),lread);
136 myIsAscii = Standard_True;
137 for (i = 0; i < lread; ++i) {
139 myIsAscii = Standard_False;
147 fprintf(stderr, ">> ERREOR : cannot create mesh \n");
152 aResult = readAscii();
154 aResult = readBinary();
161 static Standard_Real readFloat(OSD_File& theFile)
165 Standard_ShortReal f;
169 Standard_Address adr;
170 adr = (Standard_Address)c;
171 Standard_Integer lread;
172 theFile.Read(adr,4,lread);
174 u.i |= (c[1] & 0xFF) << 0x08;
175 u.i |= (c[2] & 0xFF) << 0x10;
176 u.i |= (c[3] & 0xFF) << 0x18;
181 static SMDS_MeshNode* addNode(const gp_Pnt& P,
182 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
185 SMDS_MeshNode* node = 0;
186 if ( uniqnodes.IsBound(P) ) {
187 node = uniqnodes.Find(P);
189 node = theMesh->AddNode(P.X(), P.Y(), P.Z() );
190 uniqnodes.Bind(P,node);
196 static SMDS_MeshNode* readNode(FILE* file,
197 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
200 Standard_ShortReal coord[3];
202 fscanf(file,"%*s %f %f %f\n",&coord[0],&coord[1],&coord[2]);
204 gp_Pnt P(coord[0],coord[1],coord[2]);
205 return addNode( P, uniqnodes, theMesh );
208 static SMDS_MeshNode* readNode(OSD_File& theFile,
209 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
212 Standard_ShortReal coord[3];
213 coord[0] = readFloat(theFile);
214 coord[1] = readFloat(theFile);
215 coord[2] = readFloat(theFile);
217 gp_Pnt P(coord[0],coord[1],coord[2]);
218 return addNode( P, uniqnodes, theMesh );
221 //=======================================================================
222 //function : readAscii
224 //=======================================================================
226 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii() const
228 Status aResult = DRS_OK;
230 Standard_Integer nbLines = 0;
233 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
234 FILE* file = fopen(aFileName.ToCString(),"r");
235 fseek(file,0L,SEEK_END);
237 long filesize = ftell(file);
239 file = fopen(aFileName.ToCString(),"r");
241 // count the number of lines
242 for (ipos = 0; ipos < filesize; ++ipos) {
243 if (getc(file) == '\n')
247 // go back to the beginning of the file
249 // file = fopen(aFileName.ToCString(),"r");
252 Standard_Integer nbTri = (nbLines / ASCII_LINES_PER_FACET);
254 DriverSTL_DataMapOfPntNodePtr uniqnodes;
256 while (getc(file) != '\n');
259 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
261 // skipping the facet normal
262 Standard_ShortReal normal[3];
263 fscanf(file,"%*s %*s %f %f %f\n",&normal[0],&normal[1],&normal[2]);
265 // skip the keywords "outer loop"
266 fscanf(file,"%*s %*s");
269 SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh );
270 SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh );
271 SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh );
274 myMesh->AddFace(node1,node2,node3);
276 // skip the keywords "endloop"
279 // skip the keywords "endfacet"
287 //=======================================================================
288 //function : readBinary
290 //=======================================================================
292 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary() const
294 Status aResult = DRS_OK;
297 Standard_Address adr;
298 adr = (Standard_Address)buftest;
300 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
301 OSD_File aFile = OSD_File(OSD_Path( aFileName ));
302 aFile.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
304 // the size of the file (minus the header size)
305 // must be a multiple of SIZEOF_STL_FACET
308 Standard_Integer filesize = aFile.Size();
310 if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET !=0
311 // Commented to allow reading small files (ex: 1 face)
312 /*|| (filesize < STL_MIN_FILE_SIZE)*/) {
313 Standard_NoMoreObject::Raise("DriverSTL_R_SMDS_MESH::readBinary (wrong file size)");
316 // don't trust the number of triangles which is coded in the file
317 // sometimes it is wrong, and with this technique we don't need to swap endians for integer
318 Standard_Integer nbTri = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);
321 aFile.Seek(HEADER_SIZE,OSD_FromBeginning);
323 DriverSTL_DataMapOfPntNodePtr uniqnodes;
324 Standard_Integer lread;
326 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
334 SMDS_MeshNode* node1 = readNode( aFile, uniqnodes, myMesh );
335 SMDS_MeshNode* node2 = readNode( aFile, uniqnodes, myMesh );
336 SMDS_MeshNode* node3 = readNode( aFile, uniqnodes, myMesh );
339 myMesh->AddFace(node1,node2,node3);
342 aFile.Read(adr,2,lread);