0029399: Optimize reading of floating point values from text strings -- base dtoa.c
[occt.git] / src / RWStl / RWStl_Reader.cxx
CommitLineData
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>
21#include <Message_ProgressSentry.hxx>
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
33IMPLEMENT_STANDARD_RTTIEXT(RWStl_Reader, Standard_Transient)
34
35namespace
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
42 //! Auxiliary tool for merging nodes during STL reading.
43 class MergeNodeTool
44 {
45 public:
46
47 //! Constructor
48 MergeNodeTool (RWStl_Reader* theReader)
49 : myReader (theReader),
50 myMap (1024, new NCollection_IncAllocator (1024 * 1024))
51 {
52 }
53
54 //! Add new triangle
55 int AddNode (double theX, double theY, double theZ)
56 {
57 // use existing node if found at the same point
58 gp_XYZ aPnt (theX, theY, theZ);
59
60 Standard_Integer anIndex = -1;
61 if (myMap.Find (aPnt, anIndex))
62 {
63 return anIndex;
64 }
65
66 anIndex = myReader->AddNode (aPnt);
67 myMap.Bind (aPnt, anIndex);
68 return anIndex;
69 }
70
71 public:
72
73 static Standard_Boolean IsEqual (const gp_XYZ& thePnt1, const gp_XYZ& thePnt2)
74 {
75 return (thePnt1 - thePnt2).SquareModulus() < Precision::SquareConfusion();
76 }
77
78 static Standard_Integer HashCode (const gp_XYZ& thePnt, Standard_Integer theUpper)
79 {
80 return ::HashCode (thePnt.X() * M_LN10 + thePnt.Y() * M_PI + thePnt.Z() * M_E, theUpper);
81 }
82
83 private:
84 RWStl_Reader* myReader;
85 NCollection_DataMap<gp_XYZ, Standard_Integer, MergeNodeTool> myMap;
86 };
87
88 //! Read a Little Endian 32 bits float
89 inline static float readStlFloat (const char* theData)
90 {
91 #if OCCT_BINARY_FILE_DO_INVERSE
92 // on big-endian platform, map values byte-per-byte
93 union
94 {
95 uint32_t i;
96 float f;
97 } bidargum;
98 bidargum.i = theData[0] & 0xFF;
99 bidargum.i |= (theData[1] & 0xFF) << 0x08;
100 bidargum.i |= (theData[2] & 0xFF) << 0x10;
101 bidargum.i |= (theData[3] & 0xFF) << 0x18;
102 return bidargum.f;
103 #else
104 // on little-endian platform, use plain cast
105 return *reinterpret_cast<const float*>(theData);
106 #endif
107 }
108
109 //! Read a Little Endian 32 bits float
110 inline static gp_XYZ readStlFloatVec3 (const char* theData)
111 {
112 return gp_XYZ (readStlFloat (theData),
113 readStlFloat (theData + sizeof(float)),
114 readStlFloat (theData + sizeof(float) * 2));
115 }
116
117}
118
119//==============================================================================
120//function : Read
121//purpose :
122//==============================================================================
123
124Standard_Boolean RWStl_Reader::Read (const char* theFile,
125 const Handle(Message_ProgressIndicator)& theProgress)
126{
127 std::filebuf aBuf;
128 OSD_OpenStream (aBuf, theFile, std::ios::in | std::ios::binary);
129 if (!aBuf.is_open())
130 {
131 return Standard_False;
132 }
133
134 Standard_IStream aStream (&aBuf);
1d949423 135
136 // get length of file to feed progress indicator in Ascii mode
137 aStream.seekg (0, aStream.end);
138 std::streampos theEnd = aStream.tellg();
139 aStream.seekg (0, aStream.beg);
140
22e70738 141 // binary STL files cannot be shorter than 134 bytes
142 // (80 bytes header + 4 bytes facet count + 50 bytes for one facet);
143 // thus assume files shorter than 134 as Ascii without probing
144 // (probing may bring stream to fail state if EOF is reached)
145 bool isAscii = ((size_t)theEnd < THE_STL_MIN_FILE_SIZE || IsAscii (aStream));
146
147 while (aStream.good())
4178b353 148 {
22e70738 149 if (isAscii)
1d949423 150 {
151 if (!ReadAscii (aStream, theEnd, theProgress))
152 {
153 break;
154 }
155 }
156 else
157 {
158 if (!ReadBinary (aStream, theProgress))
159 {
160 break;
161 }
162 }
163 aStream >> std::ws; // skip any white spaces
4178b353 164 }
22e70738 165 return ! aStream.fail();
4178b353 166}
167
168//==============================================================================
169//function : IsAscii
170//purpose :
171//==============================================================================
172
173Standard_Boolean RWStl_Reader::IsAscii (Standard_IStream& theStream)
174{
175 // read first 134 bytes to detect file format
176 char aBuffer[THE_STL_MIN_FILE_SIZE];
177 std::streamsize aNbRead = theStream.read (aBuffer, THE_STL_MIN_FILE_SIZE).gcount();
22e70738 178 if (! theStream)
4178b353 179 {
180 Message::DefaultMessenger()->Send ("Error: Cannot read file", Message_Fail);
22e70738 181 return true;
4178b353 182 }
183
184 // put back the read symbols
185 for (std::streamsize aByteIter = aNbRead; aByteIter > 0; --aByteIter)
186 {
187 theStream.unget();
188 }
189
190 // if file is shorter than size of binary file with 1 facet, it must be ascii
191 if (aNbRead < std::streamsize(THE_STL_MIN_FILE_SIZE))
192 {
193 return true;
194 }
195
196 // otherwise, detect binary format by presence of non-ascii symbols in first 128 bytes
197 // (note that binary STL file may start with the same bytes "solid " as Ascii one)
198 for (Standard_Integer aByteIter = 0; aByteIter < aNbRead; ++aByteIter)
199 {
200 if ((unsigned char )aBuffer[aByteIter] > (unsigned char )'~')
201 {
202 return false;
203 }
204 }
205 return true;
206}
207
208// adapted from Standard_CString.cxx
209#ifdef __APPLE__
210 // There are a lot of *_l functions availalbe on Mac OS X - we use them
211 #define SAVE_TL()
212#elif defined(_MSC_VER)
213 // MSVCRT has equivalents with slightly different syntax
214 #define SAVE_TL()
215 #define sscanf_l(theBuffer, theLocale, theFormat, ...) _sscanf_s_l(theBuffer, theFormat, theLocale, __VA_ARGS__)
216#else
217 // glibc provides only limited xlocale implementation:
218 // strtod_l/strtol_l/strtoll_l functions with explicitly specified locale
219 // and newlocale/uselocale/freelocale to switch locale within current thread only.
220 // So we switch to C locale temporarily
221 #define SAVE_TL() Standard_CLocaleSentry aLocaleSentry;
222 #define sscanf_l(theBuffer, theLocale, theFormat, ...) sscanf(theBuffer, theFormat, __VA_ARGS__)
223#endif
224
225// Macro to get 64-bit position of the file from streampos
226#if defined(_MSC_VER)
227 #define GETPOS(aPos) aPos.seekpos()
228#else
229 #define GETPOS(aPos) ((int64_t)aPos)
230#endif
231
232static inline bool str_starts_with (const char* theStr, const char* theWord, int theN)
233{
234 while (isspace (*theStr) && *theStr != '\0') theStr++;
235 return !strncmp (theStr, theWord, theN);
236}
237
238//==============================================================================
239//function : ReadAscii
240//purpose :
241//==============================================================================
242Standard_Boolean RWStl_Reader::ReadAscii (Standard_IStream& theStream,
243 const std::streampos theUntilPos,
244 const Handle(Message_ProgressIndicator)& theProgress)
245{
246 // use method seekpos() to get true 64-bit offset to enable
247 // handling of large files (VS 2010 64-bit)
248 const int64_t aStartPos = GETPOS(theStream.tellg());
22e70738 249 // Note: 1 is added to theUntilPos to be sure to read the last symbol (relevant for files without EOL at the end)
250 const int64_t aEndPos = (theUntilPos > 0 ? 1 + GETPOS(theUntilPos) : std::numeric_limits<int64_t>::max());
4178b353 251
252 // skip header "solid ..."
71c810df 253 theStream.ignore ((std::streamsize)(aEndPos - aStartPos), '\n');
4178b353 254 if (!theStream)
255 {
256 Message::DefaultMessenger()->Send ("Error: premature end of file", Message_Fail);
257 return false;
258 }
259
260 MergeNodeTool aMergeTool (this);
261 Standard_CLocaleSentry::clocale_t aLocale = Standard_CLocaleSentry::GetCLocale();
262 (void)aLocale; // to avoid warning on GCC where it is actually not used
263 SAVE_TL() // for GCC only, set C locale globally
264
265 // report progress every 1 MiB of read data
266 const int aStepB = 1024 * 1024;
71c810df 267 const Standard_Integer aNbSteps = 1 + Standard_Integer((GETPOS(theUntilPos) - aStartPos) / aStepB);
4178b353 268 Message_ProgressSentry aPSentry (theProgress, "Reading text STL file", 0, aNbSteps, 1);
269
270 int64_t aProgressPos = aStartPos + aStepB;
271 const int64_t LINELEN = 1024;
272 int aNbLine = 1;
273 char aLine1[LINELEN], aLine2[LINELEN], aLine3[LINELEN];
274 while (aPSentry.More())
275 {
276 if (GETPOS(theStream.tellg()) > aProgressPos)
277 {
278 aPSentry.Next();
279 aProgressPos += aStepB;
280 }
281
282 char facet[LINELEN], outer[LINELEN];
71c810df 283 theStream.getline (facet, (std::streamsize)std::min (LINELEN, aEndPos - GETPOS(theStream.tellg()))); // "facet normal nx ny nz"
4178b353 284 if (str_starts_with (facet, "endsolid", 8))
285 {
286 // end of STL code
287 break;
288 }
71c810df 289 theStream.getline (outer, (std::streamsize)std::min (LINELEN, aEndPos - GETPOS(theStream.tellg()))); // "outer loop"
4178b353 290 if (!str_starts_with (facet, "facet", 5) || !str_starts_with (outer, "outer", 5))
291 {
292 TCollection_AsciiString aStr ("Error: unexpected format of facet at line ");
293 aStr += aNbLine + 1;
294 Message::DefaultMessenger()->Send (aStr, Message_Fail);
295 return false;
296 }
297
71c810df 298 theStream.getline (aLine1, (std::streamsize)std::min (LINELEN, aEndPos - GETPOS(theStream.tellg())));
299 theStream.getline (aLine2, (std::streamsize)std::min (LINELEN, aEndPos - GETPOS(theStream.tellg())));
300 theStream.getline (aLine3, (std::streamsize)std::min (LINELEN, aEndPos - GETPOS(theStream.tellg())));
4178b353 301
302 // stop reading if end of file is reached;
303 // note that well-formatted file never ends by the vertex line
304 if (theStream.eof() || GETPOS(theStream.tellg()) >= aEndPos)
305 {
306 break;
307 }
308
309 if (!theStream)
310 {
311 Message::DefaultMessenger()->Send ("Error: premature end of file", Message_Fail);
312 return false;
313 }
314 aNbLine += 5;
315
316 Standard_Real x1, y1, z1, x2, y2, z2, x3, y3, z3;
317 Standard_Integer aReadCount = // read 3 lines "vertex x y z"
318 sscanf_l (aLine1, aLocale, "%*s %lf %lf %lf", &x1, &y1, &z1) +
319 sscanf_l (aLine2, aLocale, "%*s %lf %lf %lf", &x2, &y2, &z2) +
320 sscanf_l (aLine3, aLocale, "%*s %lf %lf %lf", &x3, &y3, &z3);
321 if (aReadCount != 9)
322 {
323 TCollection_AsciiString aStr ("Error: cannot read vertex co-ordinates at line ");
324 aStr += aNbLine;
325 Message::DefaultMessenger()->Send(aStr, Message_Fail);
326 return false;
327 }
328
329 // add triangle
330 int n1 = aMergeTool.AddNode (x1, y1, z1);
331 int n2 = aMergeTool.AddNode (x2, y2, z2);
332 int n3 = aMergeTool.AddNode (x3, y3, z3);
333 if (n1 != n2 && n2 != n3 && n3 != n1)
334 {
335 AddTriangle (n1, n2, n3);
336 }
337
71c810df 338 theStream.ignore ((std::streamsize)(aEndPos - GETPOS(theStream.tellg())), '\n'); // skip "endloop"
339 theStream.ignore ((std::streamsize)(aEndPos - GETPOS(theStream.tellg())), '\n'); // skip "endfacet"
4178b353 340
341 aNbLine += 2;
342 }
343
344 return aPSentry.More();
345}
346
347//==============================================================================
348//function : readStlBinary
349//purpose :
350//==============================================================================
351
352Standard_Boolean RWStl_Reader::ReadBinary (Standard_IStream& theStream,
353 const Handle(Message_ProgressIndicator)& theProgress)
354{
355/*
356 // the size of the file (minus the header size)
357 // must be a multiple of SIZEOF_STL_FACET
358 if ((theFileLen - THE_STL_HEADER_SIZE) % THE_STL_SIZEOF_FACET != 0
359 || (theFileLen < THE_STL_MIN_FILE_SIZE))
360 {
361 Message::DefaultMessenger()->Send ("Error: Corrupted binary STL file (inconsistent file size)!", Message_Fail);
362 return Standard_False;
363 }
364 const Standard_Integer aNbFacets = Standard_Integer((theFileLen - THE_STL_HEADER_SIZE) / THE_STL_SIZEOF_FACET);
365*/
366
367 // read file header at first
368 char aHeader[THE_STL_HEADER_SIZE + 1];
369 if (theStream.read (aHeader, THE_STL_HEADER_SIZE).gcount() != std::streamsize(THE_STL_HEADER_SIZE))
370 {
371 Message::DefaultMessenger()->Send ("Error: Corrupted binary STL file!", Message_Fail);
372 return false;
373 }
374
375 // number of facets is stored as 32-bit integer at position 80
376 const Standard_Integer aNbFacets = *(int32_t*)(aHeader + 80);
377
378 MergeNodeTool aMergeTool (this);
379
380 // don't trust the number of triangles which is coded in the file
381 // sometimes it is wrong, and with this technique we don't need to swap endians for integer
382 Message_ProgressSentry aPSentry (theProgress, "Reading binary STL file", 0, aNbFacets, 1);
383 Standard_Integer aNbRead = 0;
384
385 // allocate buffer for 80 triangles
386 const int THE_CHUNK_NBFACETS = 80;
387 char aBuffer[THE_STL_SIZEOF_FACET * THE_CHUNK_NBFACETS];
388
389 // normal + 3 nodes + 2 extra bytes
390 const size_t aVec3Size = sizeof(float) * 3;
391 const size_t aFaceDataLen = aVec3Size * 4 + 2;
392 const char* aBufferPtr = aBuffer;
393 Standard_Integer aNbFacesInBuffer = 0;
394 for (Standard_Integer aNbFacetRead = 0; aNbFacetRead < aNbFacets && aPSentry.More();
395 ++aNbFacetRead, ++aNbRead, --aNbFacesInBuffer, aBufferPtr += aFaceDataLen, aPSentry.Next())
396 {
397 // read more data
398 if (aNbFacesInBuffer <= 0)
399 {
400 aNbFacesInBuffer = Min (THE_CHUNK_NBFACETS, aNbFacets - aNbFacetRead);
401 const std::streamsize aDataToRead = aNbFacesInBuffer * aFaceDataLen;
402 if (theStream.read (aBuffer, aDataToRead).gcount() != aDataToRead)
403 {
404 Message::DefaultMessenger()->Send ("Error: read filed", Message_Fail);
405 return false;
406 }
407 aBufferPtr = aBuffer;
408 }
409
410 // get points from buffer
411// readStlFloatVec3 (aBufferPtr); // skip normal
412 gp_XYZ aP1 = readStlFloatVec3 (aBufferPtr + aVec3Size);
413 gp_XYZ aP2 = readStlFloatVec3 (aBufferPtr + aVec3Size * 2);
414 gp_XYZ aP3 = readStlFloatVec3 (aBufferPtr + aVec3Size * 3);
415
416 // add triangle
417 int n1 = aMergeTool.AddNode (aP1.X(), aP1.Y(), aP1.Z());
418 int n2 = aMergeTool.AddNode (aP2.X(), aP2.Y(), aP2.Z());
419 int n3 = aMergeTool.AddNode (aP3.X(), aP3.Y(), aP3.Z());
420 if (n1 != n2 && n2 != n3 && n3 != n1)
421 {
422 AddTriangle (n1, n2, n3);
423 }
424 }
425
426 return true;
427}