4178b353 |
1 | // Created: 2016-05-01 |
2 | // Author: Andrey Betenev |
3 | // Copyright: Open CASCADE 2016 |
4 | // |
5 | // This file is part of Open CASCADE Technology software library. |
6 | // |
7 | // This library is free software; you can redistribute it and/or modify it under |
8 | // the terms of the GNU Lesser General Public License version 2.1 as published |
9 | // by the Free Software Foundation, with special exception defined in the file |
10 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
11 | // distribution for complete text of the license and disclaimer of any warranty. |
12 | // |
13 | // Alternatively, this file may be used under the terms of Open CASCADE |
14 | // commercial license or contractual agreement. |
15 | |
16 | #include <RWStl_Reader.hxx> |
17 | |
18 | #include <gp_XY.hxx> |
19 | #include <Message.hxx> |
20 | #include <Message_Messenger.hxx> |
7e785937 |
21 | #include <Message_ProgressScope.hxx> |
4178b353 |
22 | #include <NCollection_DataMap.hxx> |
23 | #include <NCollection_IncAllocator.hxx> |
24 | #include <FSD_BinaryFile.hxx> |
25 | #include <OSD_OpenFile.hxx> |
26 | #include <OSD_Timer.hxx> |
27 | #include <Precision.hxx> |
28 | #include <Standard_CLocaleSentry.hxx> |
29 | |
30 | #include <algorithm> |
31 | #include <limits> |
32 | |
33 | IMPLEMENT_STANDARD_RTTIEXT(RWStl_Reader, Standard_Transient) |
34 | |
35 | namespace |
36 | { |
37 | // Binary STL sizes |
38 | static const size_t THE_STL_HEADER_SIZE = 84; |
39 | static const size_t THE_STL_SIZEOF_FACET = 50; |
40 | static const size_t THE_STL_MIN_FILE_SIZE = THE_STL_HEADER_SIZE + THE_STL_SIZEOF_FACET; |
41 | |
51ee6a7d |
42 | // The length of buffer to read (in bytes) |
43 | static const size_t THE_BUFFER_SIZE = 1024; |
44 | |
4178b353 |
45 | //! Auxiliary tool for merging nodes during STL reading. |
46 | class MergeNodeTool |
47 | { |
48 | public: |
49 | |
50 | //! Constructor |
51 | MergeNodeTool (RWStl_Reader* theReader) |
52 | : myReader (theReader), |
53 | myMap (1024, new NCollection_IncAllocator (1024 * 1024)) |
54 | { |
55 | } |
56 | |
57 | //! Add new triangle |
58 | int AddNode (double theX, double theY, double theZ) |
59 | { |
60 | // use existing node if found at the same point |
61 | gp_XYZ aPnt (theX, theY, theZ); |
62 | |
63 | Standard_Integer anIndex = -1; |
64 | if (myMap.Find (aPnt, anIndex)) |
65 | { |
66 | return anIndex; |
67 | } |
68 | |
69 | anIndex = myReader->AddNode (aPnt); |
70 | myMap.Bind (aPnt, anIndex); |
71 | return anIndex; |
72 | } |
73 | |
74 | public: |
75 | |
76 | static Standard_Boolean IsEqual (const gp_XYZ& thePnt1, const gp_XYZ& thePnt2) |
77 | { |
78 | return (thePnt1 - thePnt2).SquareModulus() < Precision::SquareConfusion(); |
79 | } |
80 | |
2b2be3fb |
81 | //! Computes a hash code for the point, in the range [1, theUpperBound] |
82 | //! @param thePoint the point which hash code is to be computed |
83 | //! @param theUpperBound the upper bound of the range a computing hash code must be within |
84 | //! @return a computed hash code, in the range [1, theUpperBound] |
85 | static Standard_Integer HashCode (const gp_XYZ& thePoint, const Standard_Integer theUpperBound) |
4178b353 |
86 | { |
2b2be3fb |
87 | return ::HashCode (thePoint.X() * M_LN10 + thePoint.Y() * M_PI + thePoint.Z() * M_E, theUpperBound); |
4178b353 |
88 | } |
89 | |
90 | private: |
91 | RWStl_Reader* myReader; |
92 | NCollection_DataMap<gp_XYZ, Standard_Integer, MergeNodeTool> myMap; |
93 | }; |
94 | |
95 | //! Read a Little Endian 32 bits float |
96 | inline static float readStlFloat (const char* theData) |
97 | { |
98 | #if OCCT_BINARY_FILE_DO_INVERSE |
99 | // on big-endian platform, map values byte-per-byte |
100 | union |
101 | { |
102 | uint32_t i; |
103 | float f; |
104 | } bidargum; |
105 | bidargum.i = theData[0] & 0xFF; |
106 | bidargum.i |= (theData[1] & 0xFF) << 0x08; |
107 | bidargum.i |= (theData[2] & 0xFF) << 0x10; |
108 | bidargum.i |= (theData[3] & 0xFF) << 0x18; |
109 | return bidargum.f; |
110 | #else |
111 | // on little-endian platform, use plain cast |
112 | return *reinterpret_cast<const float*>(theData); |
113 | #endif |
114 | } |
115 | |
116 | //! Read a Little Endian 32 bits float |
117 | inline static gp_XYZ readStlFloatVec3 (const char* theData) |
118 | { |
119 | return gp_XYZ (readStlFloat (theData), |
120 | readStlFloat (theData + sizeof(float)), |
121 | readStlFloat (theData + sizeof(float) * 2)); |
122 | } |
123 | |
124 | } |
125 | |
126 | //============================================================================== |
127 | //function : Read |
128 | //purpose : |
129 | //============================================================================== |
130 | |
131 | Standard_Boolean RWStl_Reader::Read (const char* theFile, |
7e785937 |
132 | const Message_ProgressRange& theProgress) |
4178b353 |
133 | { |
134 | std::filebuf aBuf; |
135 | OSD_OpenStream (aBuf, theFile, std::ios::in | std::ios::binary); |
136 | if (!aBuf.is_open()) |
137 | { |
138 | return Standard_False; |
139 | } |
140 | |
141 | Standard_IStream aStream (&aBuf); |
1d949423 |
142 | |
143 | // get length of file to feed progress indicator in Ascii mode |
144 | aStream.seekg (0, aStream.end); |
145 | std::streampos theEnd = aStream.tellg(); |
146 | aStream.seekg (0, aStream.beg); |
147 | |
22e70738 |
148 | // binary STL files cannot be shorter than 134 bytes |
149 | // (80 bytes header + 4 bytes facet count + 50 bytes for one facet); |
150 | // thus assume files shorter than 134 as Ascii without probing |
151 | // (probing may bring stream to fail state if EOF is reached) |
152 | bool isAscii = ((size_t)theEnd < THE_STL_MIN_FILE_SIZE || IsAscii (aStream)); |
153 | |
c37bd936 |
154 | Standard_ReadLineBuffer aBuffer (THE_BUFFER_SIZE); |
155 | |
7e785937 |
156 | // Note: here we are trying to handle rare but realistic case of |
157 | // STL files which are composed of several STL data blocks |
158 | // running translation in cycle. |
159 | // For this reason use infinite (logarithmic) progress scale, |
160 | // but in special mode so that the first cycle will take ~ 70% of it |
161 | Message_ProgressScope aPS (theProgress, NULL, 1, true); |
22e70738 |
162 | while (aStream.good()) |
4178b353 |
163 | { |
22e70738 |
164 | if (isAscii) |
1d949423 |
165 | { |
7e785937 |
166 | if (!ReadAscii (aStream, aBuffer, theEnd, aPS.Next(2))) |
1d949423 |
167 | { |
168 | break; |
169 | } |
170 | } |
171 | else |
172 | { |
7e785937 |
173 | if (!ReadBinary (aStream, aPS.Next(2))) |
1d949423 |
174 | { |
175 | break; |
176 | } |
177 | } |
178 | aStream >> std::ws; // skip any white spaces |
4178b353 |
179 | } |
22e70738 |
180 | return ! aStream.fail(); |
4178b353 |
181 | } |
182 | |
183 | //============================================================================== |
184 | //function : IsAscii |
185 | //purpose : |
186 | //============================================================================== |
187 | |
188 | Standard_Boolean RWStl_Reader::IsAscii (Standard_IStream& theStream) |
189 | { |
190 | // read first 134 bytes to detect file format |
191 | char aBuffer[THE_STL_MIN_FILE_SIZE]; |
192 | std::streamsize aNbRead = theStream.read (aBuffer, THE_STL_MIN_FILE_SIZE).gcount(); |
22e70738 |
193 | if (! theStream) |
4178b353 |
194 | { |
a87b1b37 |
195 | Message::SendFail ("Error: Cannot read file"); |
22e70738 |
196 | return true; |
4178b353 |
197 | } |
198 | |
199 | // put back the read symbols |
200 | for (std::streamsize aByteIter = aNbRead; aByteIter > 0; --aByteIter) |
201 | { |
202 | theStream.unget(); |
203 | } |
204 | |
205 | // if file is shorter than size of binary file with 1 facet, it must be ascii |
206 | if (aNbRead < std::streamsize(THE_STL_MIN_FILE_SIZE)) |
207 | { |
208 | return true; |
209 | } |
210 | |
211 | // otherwise, detect binary format by presence of non-ascii symbols in first 128 bytes |
212 | // (note that binary STL file may start with the same bytes "solid " as Ascii one) |
213 | for (Standard_Integer aByteIter = 0; aByteIter < aNbRead; ++aByteIter) |
214 | { |
215 | if ((unsigned char )aBuffer[aByteIter] > (unsigned char )'~') |
216 | { |
217 | return false; |
218 | } |
219 | } |
220 | return true; |
221 | } |
222 | |
223 | // adapted from Standard_CString.cxx |
224 | #ifdef __APPLE__ |
225 | // There are a lot of *_l functions availalbe on Mac OS X - we use them |
226 | #define SAVE_TL() |
227 | #elif defined(_MSC_VER) |
228 | // MSVCRT has equivalents with slightly different syntax |
229 | #define SAVE_TL() |
230 | #define sscanf_l(theBuffer, theLocale, theFormat, ...) _sscanf_s_l(theBuffer, theFormat, theLocale, __VA_ARGS__) |
231 | #else |
232 | // glibc provides only limited xlocale implementation: |
233 | // strtod_l/strtol_l/strtoll_l functions with explicitly specified locale |
234 | // and newlocale/uselocale/freelocale to switch locale within current thread only. |
235 | // So we switch to C locale temporarily |
236 | #define SAVE_TL() Standard_CLocaleSentry aLocaleSentry; |
237 | #define sscanf_l(theBuffer, theLocale, theFormat, ...) sscanf(theBuffer, theFormat, __VA_ARGS__) |
238 | #endif |
239 | |
04232180 |
240 | // Macro to get 64-bit position of the file from std::streampos |
6b1800cb |
241 | #if defined(_MSC_VER) && _MSC_VER < 1700 |
04232180 |
242 | // In MSVC 2010, cast of std::streampos to 64-bit int is implemented incorrectly; |
6b1800cb |
243 | // work-around (relevant for files larger than 4 GB) is to use internal function seekpos(). |
244 | // Since MSVC 15.8, seekpos() is deprecated and is said to always return 0. |
4178b353 |
245 | #define GETPOS(aPos) aPos.seekpos() |
246 | #else |
247 | #define GETPOS(aPos) ((int64_t)aPos) |
248 | #endif |
249 | |
d45b7860 |
250 | # if defined(_MSC_VER) && ! defined(strncasecmp) |
251 | # define strncasecmp _strnicmp |
252 | # endif |
253 | |
4178b353 |
254 | static inline bool str_starts_with (const char* theStr, const char* theWord, int theN) |
255 | { |
256 | while (isspace (*theStr) && *theStr != '\0') theStr++; |
d45b7860 |
257 | return !strncasecmp (theStr, theWord, theN); |
4178b353 |
258 | } |
259 | |
07bbde45 |
260 | static bool ReadVertex (const char* theStr, double& theX, double& theY, double& theZ) |
261 | { |
262 | const char *aStr = theStr; |
263 | |
264 | // skip 'vertex' |
265 | while (isspace ((unsigned char)*aStr) || isalpha ((unsigned char)*aStr)) |
266 | ++aStr; |
267 | |
268 | // read values |
269 | char *aEnd; |
270 | theX = Strtod (aStr, &aEnd); |
271 | theY = Strtod (aStr = aEnd, &aEnd); |
272 | theZ = Strtod (aStr = aEnd, &aEnd); |
273 | |
274 | return aEnd != aStr; |
275 | } |
276 | |
4178b353 |
277 | //============================================================================== |
278 | //function : ReadAscii |
279 | //purpose : |
280 | //============================================================================== |
281 | Standard_Boolean RWStl_Reader::ReadAscii (Standard_IStream& theStream, |
c37bd936 |
282 | Standard_ReadLineBuffer& theBuffer, |
4178b353 |
283 | const std::streampos theUntilPos, |
7e785937 |
284 | const Message_ProgressRange& theProgress) |
4178b353 |
285 | { |
286 | // use method seekpos() to get true 64-bit offset to enable |
287 | // handling of large files (VS 2010 64-bit) |
288 | const int64_t aStartPos = GETPOS(theStream.tellg()); |
51ee6a7d |
289 | size_t aLineLen = 0; |
290 | const char* aLine; |
4178b353 |
291 | |
292 | // skip header "solid ..." |
c37bd936 |
293 | aLine = theBuffer.ReadLine (theStream, aLineLen); |
51ee6a7d |
294 | if (aLine == NULL) |
4178b353 |
295 | { |
a87b1b37 |
296 | Message::SendFail ("Error: premature end of file"); |
4178b353 |
297 | return false; |
298 | } |
299 | |
300 | MergeNodeTool aMergeTool (this); |
301 | Standard_CLocaleSentry::clocale_t aLocale = Standard_CLocaleSentry::GetCLocale(); |
302 | (void)aLocale; // to avoid warning on GCC where it is actually not used |
303 | SAVE_TL() // for GCC only, set C locale globally |
304 | |
305 | // report progress every 1 MiB of read data |
306 | const int aStepB = 1024 * 1024; |
71c810df |
307 | const Standard_Integer aNbSteps = 1 + Standard_Integer((GETPOS(theUntilPos) - aStartPos) / aStepB); |
7e785937 |
308 | Message_ProgressScope aPS (theProgress, "Reading text STL file", aNbSteps); |
4178b353 |
309 | int64_t aProgressPos = aStartPos + aStepB; |
4178b353 |
310 | int aNbLine = 1; |
51ee6a7d |
311 | |
7e785937 |
312 | while (aPS.More()) |
4178b353 |
313 | { |
314 | if (GETPOS(theStream.tellg()) > aProgressPos) |
315 | { |
7e785937 |
316 | aPS.Next(); |
4178b353 |
317 | aProgressPos += aStepB; |
318 | } |
319 | |
c37bd936 |
320 | aLine = theBuffer.ReadLine (theStream, aLineLen); // "facet normal nx ny nz" |
51ee6a7d |
321 | if (aLine == NULL) |
322 | { |
a87b1b37 |
323 | Message::SendFail ("Error: premature end of file"); |
51ee6a7d |
324 | return false; |
325 | } |
326 | if (str_starts_with (aLine, "endsolid", 8)) |
4178b353 |
327 | { |
328 | // end of STL code |
329 | break; |
330 | } |
51ee6a7d |
331 | if (!str_starts_with (aLine, "facet", 5)) |
4178b353 |
332 | { |
a87b1b37 |
333 | Message::SendFail (TCollection_AsciiString ("Error: unexpected format of facet at line ") + (aNbLine + 1)); |
4178b353 |
334 | return false; |
335 | } |
336 | |
c37bd936 |
337 | aLine = theBuffer.ReadLine (theStream, aLineLen); // "outer loop" |
51ee6a7d |
338 | if (aLine == NULL || !str_starts_with (aLine, "outer", 5)) |
4178b353 |
339 | { |
a87b1b37 |
340 | Message::SendFail (TCollection_AsciiString ("Error: unexpected format of facet at line ") + (aNbLine + 1)); |
51ee6a7d |
341 | return false; |
4178b353 |
342 | } |
343 | |
51ee6a7d |
344 | gp_XYZ aVertex[3]; |
345 | Standard_Boolean isEOF = false; |
346 | for (Standard_Integer i = 0; i < 3; i++) |
4178b353 |
347 | { |
c37bd936 |
348 | aLine = theBuffer.ReadLine (theStream, aLineLen); |
51ee6a7d |
349 | if (aLine == NULL) |
350 | { |
351 | isEOF = true; |
352 | break; |
353 | } |
354 | gp_XYZ aReadVertex; |
355 | if (!ReadVertex (aLine, aReadVertex.ChangeCoord (1), aReadVertex.ChangeCoord (2), aReadVertex.ChangeCoord (3))) |
356 | { |
a87b1b37 |
357 | Message::SendFail (TCollection_AsciiString ("Error: cannot read vertex co-ordinates at line ") + aNbLine); |
51ee6a7d |
358 | return false; |
359 | } |
360 | aVertex[i] = aReadVertex; |
4178b353 |
361 | } |
4178b353 |
362 | |
51ee6a7d |
363 | // stop reading if end of file is reached; |
364 | // note that well-formatted file never ends by the vertex line |
365 | if (isEOF) |
4178b353 |
366 | { |
51ee6a7d |
367 | break; |
4178b353 |
368 | } |
369 | |
51ee6a7d |
370 | aNbLine += 5; |
371 | |
4178b353 |
372 | // add triangle |
51ee6a7d |
373 | int n1 = aMergeTool.AddNode (aVertex[0].X(), aVertex[0].Y(), aVertex[0].Z()); |
374 | int n2 = aMergeTool.AddNode (aVertex[1].X(), aVertex[1].Y(), aVertex[1].Z()); |
375 | int n3 = aMergeTool.AddNode (aVertex[2].X(), aVertex[2].Y(), aVertex[2].Z()); |
4178b353 |
376 | if (n1 != n2 && n2 != n3 && n3 != n1) |
377 | { |
378 | AddTriangle (n1, n2, n3); |
379 | } |
380 | |
c37bd936 |
381 | theBuffer.ReadLine (theStream, aLineLen); // skip "endloop" |
382 | theBuffer.ReadLine (theStream, aLineLen); // skip "endfacet" |
4178b353 |
383 | |
384 | aNbLine += 2; |
385 | } |
386 | |
7e785937 |
387 | return aPS.More(); |
4178b353 |
388 | } |
389 | |
390 | //============================================================================== |
391 | //function : readStlBinary |
392 | //purpose : |
393 | //============================================================================== |
394 | |
395 | Standard_Boolean RWStl_Reader::ReadBinary (Standard_IStream& theStream, |
7e785937 |
396 | const Message_ProgressRange& theProgress) |
4178b353 |
397 | { |
398 | /* |
399 | // the size of the file (minus the header size) |
400 | // must be a multiple of SIZEOF_STL_FACET |
401 | if ((theFileLen - THE_STL_HEADER_SIZE) % THE_STL_SIZEOF_FACET != 0 |
402 | || (theFileLen < THE_STL_MIN_FILE_SIZE)) |
403 | { |
a87b1b37 |
404 | Message::SendFail ("Error: Corrupted binary STL file (inconsistent file size)"); |
4178b353 |
405 | return Standard_False; |
406 | } |
407 | const Standard_Integer aNbFacets = Standard_Integer((theFileLen - THE_STL_HEADER_SIZE) / THE_STL_SIZEOF_FACET); |
408 | */ |
409 | |
410 | // read file header at first |
411 | char aHeader[THE_STL_HEADER_SIZE + 1]; |
412 | if (theStream.read (aHeader, THE_STL_HEADER_SIZE).gcount() != std::streamsize(THE_STL_HEADER_SIZE)) |
413 | { |
a87b1b37 |
414 | Message::SendFail ("Error: Corrupted binary STL file"); |
4178b353 |
415 | return false; |
416 | } |
417 | |
418 | // number of facets is stored as 32-bit integer at position 80 |
419 | const Standard_Integer aNbFacets = *(int32_t*)(aHeader + 80); |
420 | |
421 | MergeNodeTool aMergeTool (this); |
422 | |
423 | // don't trust the number of triangles which is coded in the file |
424 | // sometimes it is wrong, and with this technique we don't need to swap endians for integer |
7e785937 |
425 | Message_ProgressScope aPS (theProgress, "Reading binary STL file", aNbFacets); |
4178b353 |
426 | Standard_Integer aNbRead = 0; |
427 | |
428 | // allocate buffer for 80 triangles |
429 | const int THE_CHUNK_NBFACETS = 80; |
430 | char aBuffer[THE_STL_SIZEOF_FACET * THE_CHUNK_NBFACETS]; |
431 | |
432 | // normal + 3 nodes + 2 extra bytes |
433 | const size_t aVec3Size = sizeof(float) * 3; |
434 | const size_t aFaceDataLen = aVec3Size * 4 + 2; |
435 | const char* aBufferPtr = aBuffer; |
436 | Standard_Integer aNbFacesInBuffer = 0; |
7e785937 |
437 | for (Standard_Integer aNbFacetRead = 0; aNbFacetRead < aNbFacets && aPS.More(); |
438 | ++aNbFacetRead, ++aNbRead, --aNbFacesInBuffer, aBufferPtr += aFaceDataLen, aPS.Next()) |
4178b353 |
439 | { |
440 | // read more data |
441 | if (aNbFacesInBuffer <= 0) |
442 | { |
443 | aNbFacesInBuffer = Min (THE_CHUNK_NBFACETS, aNbFacets - aNbFacetRead); |
444 | const std::streamsize aDataToRead = aNbFacesInBuffer * aFaceDataLen; |
445 | if (theStream.read (aBuffer, aDataToRead).gcount() != aDataToRead) |
446 | { |
a87b1b37 |
447 | Message::SendFail ("Error: binary STL read failed"); |
4178b353 |
448 | return false; |
449 | } |
450 | aBufferPtr = aBuffer; |
451 | } |
452 | |
453 | // get points from buffer |
454 | // readStlFloatVec3 (aBufferPtr); // skip normal |
455 | gp_XYZ aP1 = readStlFloatVec3 (aBufferPtr + aVec3Size); |
456 | gp_XYZ aP2 = readStlFloatVec3 (aBufferPtr + aVec3Size * 2); |
457 | gp_XYZ aP3 = readStlFloatVec3 (aBufferPtr + aVec3Size * 3); |
458 | |
459 | // add triangle |
460 | int n1 = aMergeTool.AddNode (aP1.X(), aP1.Y(), aP1.Z()); |
461 | int n2 = aMergeTool.AddNode (aP2.X(), aP2.Y(), aP2.Z()); |
462 | int n3 = aMergeTool.AddNode (aP3.X(), aP3.Y(), aP3.Z()); |
463 | if (n1 != n2 && n2 != n3 && n3 != n1) |
464 | { |
465 | AddTriangle (n1, n2, n3); |
466 | } |
467 | } |
468 | |
7e785937 |
469 | return aPS.More(); |
4178b353 |
470 | } |