Salome HOME
22308: EDF 2572 SMESH: Can't import a file with a non ascii character in the path
[modules/smesh.git] / src / DriverSTL / DriverSTL_R_SMDS_Mesh.cxx
1 // Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "DriverSTL_R_SMDS_Mesh.h"
24
25 #include "SMDS_Mesh.hxx"
26 #include "SMDS_MeshElement.hxx"
27 #include "SMDS_MeshNode.hxx"
28 #include "SMESH_File.hxx"
29
30 #include <gp_Pnt.hxx>
31 #include <Basics_Utils.hxx>
32
33 #include <NCollection_DataMap.hxx>
34 #include <Standard_NoMoreObject.hxx>
35
36 namespace
37 {
38   struct Hasher
39   {
40     //=======================================================================
41     //function : HashCode
42     //purpose  :
43     //=======================================================================
44     inline static Standard_Integer HashCode
45     (const gp_Pnt& point,  Standard_Integer Upper)
46     {
47       union
48       {
49         Standard_Real    R[3];
50         Standard_Integer I[6];
51       } U;
52
53       point.Coord( U.R[0], U.R[1], U.R[2] );
54
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);
56     }
57     //=======================================================================
58     //function : IsEqual
59     //purpose  :
60     //=======================================================================
61     inline static Standard_Boolean IsEqual
62     (const gp_Pnt& point1, const gp_Pnt& point2)
63     {
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);
68     }
69   };
70   typedef NCollection_DataMap<gp_Pnt,SMDS_MeshNode*,Hasher> TDataMapOfPntNodePtr;
71
72   const int HEADER_SIZE           = 84;
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;
77 }
78
79 //=======================================================================
80 //function : DriverSTL_R_SMDS_Mesh
81 //purpose  :
82 //=======================================================================
83
84 DriverSTL_R_SMDS_Mesh::DriverSTL_R_SMDS_Mesh()
85 {
86   myIsCreateFaces = true;
87   myIsAscii = Standard_True;
88 }
89
90 //=======================================================================
91 //function : SetIsCreateFaces
92 //purpose  : 
93 //=======================================================================
94
95 void DriverSTL_R_SMDS_Mesh::SetIsCreateFaces( const bool theIsCreate )
96 { myIsCreateFaces = theIsCreate; }
97
98 //=======================================================================
99 //function : Perform
100 //purpose  : 
101 //=======================================================================
102
103 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::Perform()
104 {
105   Kernel_Utils::Localizer loc;
106
107   Status aResult = DRS_OK;
108
109   if ( myFile.empty() ) {
110     fprintf(stderr, ">> ERREOR : invalid file name \n");
111     return DRS_FAIL;
112   }
113
114   SMESH_File file( myFile, /*open=*/false );
115   if ( !file.open() ) {
116     fprintf(stderr, ">> ERROR : cannot open file %s \n", myFile.c_str());
117     return DRS_FAIL;
118   }
119
120   // we skip the header which is in Ascii for both modes
121   const char* data = file;
122   data += HEADER_SIZE;
123
124   // we check 128 characters to detect if we have a non-ascii char
125   myIsAscii = Standard_True;
126   for (int i = 0; i < 128; ++i, ++data) {
127     if ( !isascii( *data ) && data < file.end() ) {
128       myIsAscii = Standard_False;
129       break;
130     }
131   }
132
133   if ( !myMesh ) {
134     fprintf(stderr, ">> ERREOR : cannot create mesh \n");
135     return DRS_FAIL;
136   }
137
138   if ( myIsAscii )
139     aResult = readAscii( file );
140   else
141     aResult = readBinary( file );
142
143   return aResult;
144 }
145
146 // static methods
147
148 static Standard_Real readFloat(SMESH_File& theFile)
149 {
150   union {
151     Standard_Boolean i;
152     Standard_ShortReal f;
153   } u;
154
155   const char* c = theFile;
156   u.i  =  c[0] & 0xFF;
157   u.i |= (c[1] & 0xFF) << 0x08;
158   u.i |= (c[2] & 0xFF) << 0x10;
159   u.i |= (c[3] & 0xFF) << 0x18;
160   theFile += SIZE_OF_FLOAT;
161
162   return u.f;
163 }
164
165 static SMDS_MeshNode* addNode(const gp_Pnt& P,
166                               TDataMapOfPntNodePtr& uniqnodes,
167                               SMDS_Mesh* theMesh)
168 {
169   SMDS_MeshNode* node = 0;
170   if ( uniqnodes.IsBound(P) ) {
171     node = uniqnodes.Find(P);
172   } else {
173     node = theMesh->AddNode(P.X(), P.Y(), P.Z() );
174     uniqnodes.Bind(P,node);
175   }
176   
177   return node;
178 }                                
179
180 static SMDS_MeshNode* readNode(FILE* file,
181                                TDataMapOfPntNodePtr& uniqnodes,
182                                SMDS_Mesh* theMesh)
183 {
184   Standard_ShortReal coord[3];
185   // reading vertex
186   fscanf(file,"%*s %f %f %f\n",&coord[0],&coord[1],&coord[2]);
187
188   gp_Pnt P(coord[0],coord[1],coord[2]);
189   return addNode( P, uniqnodes, theMesh );
190 }
191
192 static SMDS_MeshNode* readNode(SMESH_File& theFile,
193                                TDataMapOfPntNodePtr& uniqnodes,
194                                SMDS_Mesh* theMesh)
195 {
196   gp_Pnt coord;
197   coord.SetX( readFloat(theFile));
198   coord.SetY( readFloat(theFile));
199   coord.SetZ( readFloat(theFile));
200
201   return addNode( coord, uniqnodes, theMesh );
202 }
203
204 //=======================================================================
205 //function : readAscii
206 //purpose  : 
207 //=======================================================================
208
209 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readAscii(SMESH_File& theFile) const
210 {
211   Status aResult = DRS_OK;
212
213   // get the file size
214   long filesize = theFile.size();
215   theFile.close();
216
217   // Open the file 
218   FILE* file = fopen( myFile.c_str(),"r");
219
220   // count the number of lines
221   Standard_Integer nbLines = 0;
222   for (long ipos = 0; ipos < filesize; ++ipos) {
223     if (getc(file) == '\n')
224       nbLines++;
225   }
226
227   // go back to the beginning of the file
228   rewind(file);
229   
230   Standard_Integer nbTri = (nbLines / ASCII_LINES_PER_FACET);
231
232   TDataMapOfPntNodePtr uniqnodes;
233   // skip header
234   while (getc(file) != '\n');
235
236   // main reading
237   for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
238
239     // skipping the facet normal
240     Standard_ShortReal normal[3];
241     fscanf(file,"%*s %*s %f %f %f\n",&normal[0],&normal[1],&normal[2]);
242
243     // skip the keywords "outer loop"
244     fscanf(file,"%*s %*s");
245
246     // reading nodes
247     SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh );
248     SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh );
249     SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh );
250
251     if (myIsCreateFaces)
252       myMesh->AddFace(node1,node2,node3);
253
254     // skip the keywords "endloop"
255     fscanf(file,"%*s");
256
257     // skip the keywords "endfacet"
258     fscanf(file,"%*s");
259   }
260
261   fclose(file);
262   return aResult;
263 }
264
265 //=======================================================================
266 //function : readBinary
267 //purpose  : 
268 //=======================================================================
269
270 Driver_Mesh::Status DriverSTL_R_SMDS_Mesh::readBinary(SMESH_File& file) const
271 {
272   Status aResult = DRS_OK;
273
274   // the size of the file (minus the header size)
275   // must be a multiple of SIZEOF_STL_FACET
276
277   long filesize = file.size();
278
279   if ( (filesize - HEADER_SIZE) % SIZEOF_STL_FACET !=0 
280       // Commented to allow reading small files (ex: 1 face)
281       /*|| (filesize < STL_MIN_FILE_SIZE)*/) {
282     Standard_NoMoreObject::Raise("DriverSTL_R_SMDS_MESH::readBinary (wrong file size)");
283   }
284
285   // don't trust the number of triangles which is coded in the file
286   // sometimes it is wrong, and with this technique we don't need to swap endians for integer
287   Standard_Integer nbTri = ((filesize - HEADER_SIZE) / SIZEOF_STL_FACET);
288
289   // skip the header
290   file += HEADER_SIZE;
291
292   TDataMapOfPntNodePtr uniqnodes;
293   
294   for (Standard_Integer iTri = 0; iTri < nbTri; ++iTri) {
295
296     // ignore normals
297     file += 3 * SIZE_OF_FLOAT;
298
299     // read vertices
300     SMDS_MeshNode* node1 = readNode( file, uniqnodes, myMesh );
301     SMDS_MeshNode* node2 = readNode( file, uniqnodes, myMesh );
302     SMDS_MeshNode* node3 = readNode( file, uniqnodes, myMesh );
303
304     if (myIsCreateFaces)
305       myMesh->AddFace(node1,node2,node3);
306
307     // skip extra bytes
308     file += 2;
309   }
310   return aResult;
311 }