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 #include "DriverSTL_R_SMDS_Mesh.h"
24 #include "SMDS_Mesh.hxx"
25 #include "SMDS_MeshElement.hxx"
26 #include "SMDS_MeshNode.hxx"
28 #include <OSD_Path.hxx>
29 #include <OSD_File.hxx>
30 #include <OSD_FromWhere.hxx>
31 #include <OSD_Protection.hxx>
32 #include <OSD_SingleProtection.hxx>
33 #include <NCollection_DataMap.hxx>
34 #include <Standard_NoMoreObject.hxx>
36 #include "utilities.h"
38 static const int HEADER_SIZE = 84;
39 static const int SIZEOF_STL_FACET = 50;
40 //static const int STL_MIN_FILE_SIZE = 284;
41 static const int ASCII_LINES_PER_FACET = 7;
43 static Standard_Real tab1[3];
44 static Standard_Real tab2[3];
46 typedef NCollection_DataMap<gp_Pnt,SMDS_MeshNode*> DriverSTL_DataMapOfPntNodePtr;
47 //typedef NCollection_BaseCollection<SMDS_MeshNodePtr> DriverSTL_ColOfNodePtr;
49 //=======================================================================
52 //=======================================================================
53 inline Standard_Integer HashCode
54 (const gp_Pnt& point, const Standard_Integer Upper)
59 Standard_Integer I[6];
62 point.Coord(U.R[0],U.R[1],U.R[2]);
64 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);
67 //=======================================================================
70 //=======================================================================
71 inline Standard_Boolean IsEqual
72 (const gp_Pnt& point1, const gp_Pnt& point2)
74 point1.Coord(tab1[0],tab1[1],tab1[2]);
75 point2.Coord(tab2[0],tab2[1],tab2[2]);
76 return (memcmp(tab1,tab2,sizeof(tab1)) == 0);
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 Status aResult = DRS_OK;
107 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
108 if ( aFileName.IsEmpty() ) {
109 fprintf(stderr, ">> ERREOR : invalid file name \n");
114 Standard_IStream is(&fic);
115 if (!fic.open(aFileName.ToCString(),ios::in)) {
116 fprintf(stderr, ">> ERROR : cannot open file %s \n", aFileName.ToCString());
121 OSD_Path aPath( aFileName );
122 OSD_File file = OSD_File( aPath );
123 file.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
124 unsigned char str[128];
125 Standard_Integer lread,i;
126 Standard_Address ach;
127 ach = (Standard_Address)str;
128 // we skip the header which is in Ascii for both modes
129 file.Read(ach,HEADER_SIZE,lread);
131 // we read 128 characters to detect if we have a non-ascii char
132 file.Read(ach,sizeof(str),lread);
134 myIsAscii = Standard_True;
135 for (i = 0; i < lread; ++i) {
137 myIsAscii = Standard_False;
145 fprintf(stderr, ">> ERREOR : cannot create mesh \n");
150 aResult = readAscii();
152 aResult = readBinary();
159 static Standard_Real readFloat(OSD_File& theFile)
163 Standard_ShortReal f;
167 Standard_Address adr;
168 adr = (Standard_Address)c;
169 Standard_Integer lread;
170 theFile.Read(adr,4,lread);
172 u.i |= (c[1] & 0xFF) << 0x08;
173 u.i |= (c[2] & 0xFF) << 0x10;
174 u.i |= (c[3] & 0xFF) << 0x18;
179 static SMDS_MeshNode* addNode(const gp_Pnt& P,
180 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
183 SMDS_MeshNode* node = 0;
184 if ( uniqnodes.IsBound(P) ) {
185 node = uniqnodes.Find(P);
187 node = theMesh->AddNode(P.X(), P.Y(), P.Z() );
188 uniqnodes.Bind(P,node);
194 static SMDS_MeshNode* readNode(FILE* file,
195 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
198 Standard_ShortReal coord[3];
200 fscanf(file,"%*s %f %f %f\n",&coord[0],&coord[1],&coord[2]);
202 gp_Pnt P(coord[0],coord[1],coord[2]);
203 return addNode( P, uniqnodes, theMesh );
206 static SMDS_MeshNode* readNode(OSD_File& theFile,
207 DriverSTL_DataMapOfPntNodePtr& uniqnodes,
210 Standard_ShortReal coord[3];
211 coord[0] = readFloat(theFile);
212 coord[1] = readFloat(theFile);
213 coord[2] = readFloat(theFile);
215 gp_Pnt P(coord[0],coord[1],coord[2]);
216 return addNode( P, uniqnodes, theMesh );
219 //=======================================================================
220 //function : readAscii
222 //=======================================================================
224 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii() const
226 Status aResult = DRS_OK;
228 Standard_Integer nbLines = 0;
231 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
232 FILE* file = fopen(aFileName.ToCString(),"r");
233 fseek(file,0L,SEEK_END);
235 long filesize = ftell(file);
237 file = fopen(aFileName.ToCString(),"r");
239 // count the number of lines
240 for (ipos = 0; ipos < filesize; ++ipos) {
241 if (getc(file) == '\n')
245 // go back to the beginning of the file
247 // file = fopen(aFileName.ToCString(),"r");
250 Standard_Integer nbTri = (nbLines / ASCII_LINES_PER_FACET);
252 DriverSTL_DataMapOfPntNodePtr uniqnodes;
254 while (getc(file) != '\n');
257 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
259 // skipping the facet normal
260 Standard_ShortReal normal[3];
261 fscanf(file,"%*s %*s %f %f %f\n",&normal[0],&normal[1],&normal[2]);
263 // skip the keywords "outer loop"
264 fscanf(file,"%*s %*s");
267 SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh );
268 SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh );
269 SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh );
272 myMesh->AddFace(node1,node2,node3);
274 // skip the keywords "endloop"
277 // skip the keywords "endfacet"
285 //=======================================================================
286 //function : readBinary
288 //=======================================================================
290 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary() const
292 Status aResult = DRS_OK;
295 Standard_Address adr;
296 adr = (Standard_Address)buftest;
298 TCollection_AsciiString aFileName( (char *)myFile.c_str() );
299 OSD_File aFile = OSD_File(OSD_Path( aFileName ));
300 aFile.Open(OSD_ReadOnly,OSD_Protection(OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD));
302 // the size of the file (minus the header size)
303 // must be a multiple of SIZEOF_STL_FACET
306 Standard_Integer filesize = aFile.Size();
308 if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET !=0
309 // Commented to allow reading small files (ex: 1 face)
310 /*|| (filesize < STL_MIN_FILE_SIZE)*/) {
311 Standard_NoMoreObject::Raise("DriverSTL_R_SMDS_MESH::readBinary (wrong file size)");
314 // don't trust the number of triangles which is coded in the file
315 // sometimes it is wrong, and with this technique we don't need to swap endians for integer
316 Standard_Integer nbTri = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);
319 aFile.Seek(HEADER_SIZE,OSD_FromBeginning);
321 DriverSTL_DataMapOfPntNodePtr uniqnodes;
322 Standard_Integer lread;
324 for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
332 SMDS_MeshNode* node1 = readNode( aFile, uniqnodes, myMesh );
333 SMDS_MeshNode* node2 = readNode( aFile, uniqnodes, myMesh );
334 SMDS_MeshNode* node3 = readNode( aFile, uniqnodes, myMesh );
337 myMesh->AddFace(node1,node2,node3);
340 aFile.Read(adr,2,lread);