1 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
22 //=======================================================================
25 //=======================================================================
26 inline Standard_Integer HashCode
27 (const gp_Pnt& point, Standard_Integer Upper)
32 Standard_Integer I[6];
35 point.Coord(U.R[0],U.R[1],U.R[2]);
37 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);
39 static Standard_Real tab1[3];
40 static Standard_Real tab2[3];
41 //=======================================================================
44 //=======================================================================
45 inline Standard_Boolean IsEqual
46 (const gp_Pnt& point1, const gp_Pnt& point2)
48 point1.Coord(tab1[0],tab1[1],tab1[2]);
49 point2.Coord(tab2[0],tab2[1],tab2[2]);
50 return (memcmp(tab1,tab2,sizeof(tab1)) == 0);
52 #include "DriverSTL_R_SMDS_Mesh.h"
54 #include "SMDS_Mesh.hxx"
55 #include "SMDS_MeshElement.hxx"
56 #include "SMDS_MeshNode.hxx"
58 #include <OSD_Path.hxx>
59 #include <OSD_File.hxx>
60 #include <OSD_FromWhere.hxx>
61 #include <OSD_Protection.hxx>
62 #include <OSD_SingleProtection.hxx>
63 #include <Standard_NoMoreObject.hxx>
65 #include "utilities.h"
67 static const int HEADER_SIZE = 84;
68 static const int SIZEOF_STL_FACET = 50;
69 //static const int STL_MIN_FILE_SIZE = 284;
70 static const int ASCII_LINES_PER_FACET = 7;
73 //typedef NCollection_BaseCollection<SMDS_MeshNodePtr> DriverSTL_ColOfNodePtr;
76 #include <NCollection_DataMap.hxx>
77 typedef NCollection_DataMap<gp_Pnt,SMDS_MeshNode*> DriverSTL_DataMapOfPntNodePtr;
78 //=======================================================================
79 //function : DriverSTL_R_SMDS_Mesh
81 //=======================================================================
83 DriverSTL_R_SMDS_Mesh::DriverSTL_R_SMDS_Mesh()
85 myIsCreateFaces = true;
86 myIsAscii = Standard_True;
89 //=======================================================================
90 //function : SetIsCreateFaces
92 //=======================================================================
94 void DriverSTL_R_SMDS_Mesh::SetIsCreateFaces( const bool theIsCreate )
95 { myIsCreateFaces = theIsCreate; }
97 //=======================================================================
100 //=======================================================================
102 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::Perform()
104 Status aResult = DRS_OK;
106 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
107 if ( aFileName.IsEmpty() ) {
108 fprintf(stderr, ">> ERREOR : invalid file name \n");
113 Standard_IStream is(&fic);
114 if (!fic.open(aFileName.ToCString(),ios::in)) {
115 fprintf(stderr, ">> ERROR : cannot open file %s \n", aFileName.ToCString());
120 OSD_Path aPath( aFileName );
121 OSD_File file = OSD_File( aPath );
122 file.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
123 unsigned char str[128];
124 Standard_Integer lread,i;
125 Standard_Address ach;
126 ach = (Standard_Address)str;
127 // we skip the header which is in Ascii for both modes
128 file.Read(ach,HEADER_SIZE,lread);
130 // we read 128 characters to detect if we have a non-ascii char
131 file.Read(ach,sizeof(str),lread);
133 myIsAscii = Standard_True;
134 for (i = 0; i < lread; ++i) {
136 myIsAscii = Standard_False;
144 fprintf(stderr, ">> ERREOR : cannot create mesh \n");
149 aResult = readAscii();
151 aResult = readBinary();
158 static Standard_Real readFloat(OSD_File& theFile)
162 Standard_ShortReal f;
166 Standard_Address adr;
167 adr = (Standard_Address)c;
168 Standard_Integer lread;
169 theFile.Read(adr,4,lread);
171 u.i |= (c[1] & 0xFF) << 0x08;
172 u.i |= (c[2] & 0xFF) << 0x10;
173 u.i |= (c[3] & 0xFF) << 0x18;
178 static SMDS_MeshNode* addNode(const gp_Pnt& P,
179 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
182 SMDS_MeshNode* node = 0;
183 if ( uniqnodes.IsBound(P) ) {
184 node = uniqnodes.Find(P);
186 node = theMesh->AddNode(P.X(), P.Y(), P.Z() );
187 uniqnodes.Bind(P,node);
193 static SMDS_MeshNode* readNode(FILE* file,
194 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
197 Standard_ShortReal coord[3];
199 fscanf(file,"%*s %f %f %f\n",&coord[0],&coord[1],&coord[2]);
201 gp_Pnt P(coord[0],coord[1],coord[2]);
202 return addNode( P, uniqnodes, theMesh );
205 static SMDS_MeshNode* readNode(OSD_File& theFile,
206 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
209 Standard_ShortReal coord[3];
210 coord[0] = readFloat(theFile);
211 coord[1] = readFloat(theFile);
212 coord[2] = readFloat(theFile);
214 gp_Pnt P(coord[0],coord[1],coord[2]);
215 return addNode( P, uniqnodes, theMesh );
218 //=======================================================================
219 //function : readAscii
221 //=======================================================================
223 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii() const
225 Status aResult = DRS_OK;
227 Standard_Integer nbLines = 0;
230 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
231 FILE* file = fopen(aFileName.ToCString(),"r");
232 fseek(file,0L,SEEK_END);
234 long filesize = ftell(file);
236 file = fopen(aFileName.ToCString(),"r");
238 // count the number of lines
239 for (ipos = 0; ipos < filesize; ++ipos) {
240 if (getc(file) == '\n')
244 // go back to the beginning of the file
246 // file = fopen(aFileName.ToCString(),"r");
249 Standard_Integer nbTri = (nbLines / ASCII_LINES_PER_FACET);
251 DriverSTL_DataMapOfPntNodePtr uniqnodes;
253 while (getc(file) != '\n');
256 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
258 // skipping the facet normal
259 Standard_ShortReal normal[3];
260 fscanf(file,"%*s %*s %f %f %f\n",&normal[0],&normal[1],&normal[2]);
262 // skip the keywords "outer loop"
263 fscanf(file,"%*s %*s");
266 SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh );
267 SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh );
268 SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh );
271 myMesh->AddFace(node1,node2,node3);
273 // skip the keywords "endloop"
276 // skip the keywords "endfacet"
284 //=======================================================================
285 //function : readBinary
287 //=======================================================================
289 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary() const
291 Status aResult = DRS_OK;
294 Standard_Address adr;
295 adr = (Standard_Address)buftest;
297 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
298 OSD_File aFile = OSD_File(OSD_Path( aFileName ));
299 aFile.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
301 // the size of the file (minus the header size)
302 // must be a multiple of SIZEOF_STL_FACET
305 Standard_Integer filesize = aFile.Size();
307 if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET !=0
308 // Commented to allow reading small files (ex: 1 face)
309 /*|| (filesize < STL_MIN_FILE_SIZE)*/) {
310 Standard_NoMoreObject::Raise("DriverSTL_R_SMDS_MESH::readBinary (wrong file size)");
313 // don't trust the number of triangles which is coded in the file
314 // sometimes it is wrong, and with this technique we don't need to swap endians for integer
315 Standard_Integer nbTri = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);
318 aFile.Seek(HEADER_SIZE,OSD_FromBeginning);
320 DriverSTL_DataMapOfPntNodePtr uniqnodes;
321 Standard_Integer lread;
323 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
331 SMDS_MeshNode* node1 = readNode( aFile, uniqnodes, myMesh );
332 SMDS_MeshNode* node2 = readNode( aFile, uniqnodes, myMesh );
333 SMDS_MeshNode* node3 = readNode( aFile, uniqnodes, myMesh );
336 myMesh->AddFace(node1,node2,node3);
339 aFile.Read(adr,2,lread);