1 // Copyright (C) 2007-2008 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
24 //=======================================================================
27 //=======================================================================
28 inline Standard_Integer HashCode
29 (const gp_Pnt& point, Standard_Integer Upper)
34 Standard_Integer I[6];
37 point.Coord(U.R[0],U.R[1],U.R[2]);
39 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);
41 static Standard_Real tab1[3];
42 static Standard_Real tab2[3];
43 //=======================================================================
46 //=======================================================================
47 inline Standard_Boolean IsEqual
48 (const gp_Pnt& point1, const gp_Pnt& point2)
50 point1.Coord(tab1[0],tab1[1],tab1[2]);
51 point2.Coord(tab2[0],tab2[1],tab2[2]);
52 return (memcmp(tab1,tab2,sizeof(tab1)) == 0);
54 #include "DriverSTL_R_SMDS_Mesh.h"
56 #include "SMDS_Mesh.hxx"
57 #include "SMDS_MeshElement.hxx"
58 #include "SMDS_MeshNode.hxx"
60 #include <OSD_Path.hxx>
61 #include <OSD_File.hxx>
62 #include <OSD_FromWhere.hxx>
63 #include <OSD_Protection.hxx>
64 #include <OSD_SingleProtection.hxx>
65 #include <Standard_NoMoreObject.hxx>
67 #include "utilities.h"
69 static const int HEADER_SIZE = 84;
70 static const int SIZEOF_STL_FACET = 50;
71 //static const int STL_MIN_FILE_SIZE = 284;
72 static const int ASCII_LINES_PER_FACET = 7;
75 //typedef NCollection_BaseCollection<SMDS_MeshNodePtr> DriverSTL_ColOfNodePtr;
78 #include <NCollection_DataMap.hxx>
79 typedef NCollection_DataMap<gp_Pnt,SMDS_MeshNode*> DriverSTL_DataMapOfPntNodePtr;
80 //=======================================================================
81 //function : DriverSTL_R_SMDS_Mesh
83 //=======================================================================
85 DriverSTL_R_SMDS_Mesh::DriverSTL_R_SMDS_Mesh()
87 myIsCreateFaces = true;
88 myIsAscii = Standard_True;
91 //=======================================================================
92 //function : SetIsCreateFaces
94 //=======================================================================
96 void DriverSTL_R_SMDS_Mesh::SetIsCreateFaces( const bool theIsCreate )
97 { myIsCreateFaces = theIsCreate; }
99 //=======================================================================
102 //=======================================================================
104 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::Perform()
106 Status aResult = DRS_OK;
108 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
109 if ( aFileName.IsEmpty() ) {
110 fprintf(stderr, ">> ERREOR : invalid file name \n");
115 Standard_IStream is(&fic);
116 if (!fic.open(aFileName.ToCString(),ios::in)) {
117 fprintf(stderr, ">> ERROR : cannot open file %s \n", aFileName.ToCString());
122 OSD_Path aPath( aFileName );
123 OSD_File file = OSD_File( aPath );
124 file.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
125 unsigned char str[128];
126 Standard_Integer lread,i;
127 Standard_Address ach;
128 ach = (Standard_Address)str;
129 // we skip the header which is in Ascii for both modes
130 file.Read(ach,HEADER_SIZE,lread);
132 // we read 128 characters to detect if we have a non-ascii char
133 file.Read(ach,sizeof(str),lread);
135 myIsAscii = Standard_True;
136 for (i = 0; i < lread; ++i) {
138 myIsAscii = Standard_False;
146 fprintf(stderr, ">> ERREOR : cannot create mesh \n");
151 aResult = readAscii();
153 aResult = readBinary();
160 static Standard_Real readFloat(OSD_File& theFile)
164 Standard_ShortReal f;
168 Standard_Address adr;
169 adr = (Standard_Address)c;
170 Standard_Integer lread;
171 theFile.Read(adr,4,lread);
173 u.i |= (c[1] & 0xFF) << 0x08;
174 u.i |= (c[2] & 0xFF) << 0x10;
175 u.i |= (c[3] & 0xFF) << 0x18;
180 static SMDS_MeshNode* addNode(const gp_Pnt& P,
181 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
184 SMDS_MeshNode* node = 0;
185 if ( uniqnodes.IsBound(P) ) {
186 node = uniqnodes.Find(P);
188 node = theMesh->AddNode(P.X(), P.Y(), P.Z() );
189 uniqnodes.Bind(P,node);
195 static SMDS_MeshNode* readNode(FILE* file,
196 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
199 Standard_ShortReal coord[3];
201 fscanf(file,"%*s %f %f %f\n",&coord[0],&coord[1],&coord[2]);
203 gp_Pnt P(coord[0],coord[1],coord[2]);
204 return addNode( P, uniqnodes, theMesh );
207 static SMDS_MeshNode* readNode(OSD_File& theFile,
208 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
211 Standard_ShortReal coord[3];
212 coord[0] = readFloat(theFile);
213 coord[1] = readFloat(theFile);
214 coord[2] = readFloat(theFile);
216 gp_Pnt P(coord[0],coord[1],coord[2]);
217 return addNode( P, uniqnodes, theMesh );
220 //=======================================================================
221 //function : readAscii
223 //=======================================================================
225 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii() const
227 Status aResult = DRS_OK;
229 Standard_Integer nbLines = 0;
232 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
233 FILE* file = fopen(aFileName.ToCString(),"r");
234 fseek(file,0L,SEEK_END);
236 long filesize = ftell(file);
238 file = fopen(aFileName.ToCString(),"r");
240 // count the number of lines
241 for (ipos = 0; ipos < filesize; ++ipos) {
242 if (getc(file) == '\n')
246 // go back to the beginning of the file
248 // file = fopen(aFileName.ToCString(),"r");
251 Standard_Integer nbTri = (nbLines / ASCII_LINES_PER_FACET);
253 DriverSTL_DataMapOfPntNodePtr 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() const
293 Status aResult = DRS_OK;
296 Standard_Address adr;
297 adr = (Standard_Address)buftest;
299 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
300 OSD_File aFile = OSD_File(OSD_Path( aFileName ));
301 aFile.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
303 // the size of the file (minus the header size)
304 // must be a multiple of SIZEOF_STL_FACET
307 Standard_Integer filesize = aFile.Size();
309 if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET !=0
310 // Commented to allow reading small files (ex: 1 face)
311 /*|| (filesize < STL_MIN_FILE_SIZE)*/) {
312 Standard_NoMoreObject::Raise("DriverSTL_R_SMDS_MESH::readBinary (wrong file size)");
315 // don't trust the number of triangles which is coded in the file
316 // sometimes it is wrong, and with this technique we don't need to swap endians for integer
317 Standard_Integer nbTri = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);
320 aFile.Seek(HEADER_SIZE,OSD_FromBeginning);
322 DriverSTL_DataMapOfPntNodePtr uniqnodes;
323 Standard_Integer lread;
325 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
333 SMDS_MeshNode* node1 = readNode( aFile, uniqnodes, myMesh );
334 SMDS_MeshNode* node2 = readNode( aFile, uniqnodes, myMesh );
335 SMDS_MeshNode* node3 = readNode( aFile, uniqnodes, myMesh );
338 myMesh->AddFace(node1,node2,node3);
341 aFile.Read(adr,2,lread);