diff options
author | Marius Kintel <marius@kintel.net> | 2013-02-02 22:25:13 (GMT) |
---|---|---|
committer | Marius Kintel <marius@kintel.net> | 2013-02-02 22:25:13 (GMT) |
commit | e5448f5e69f6d59256ba1816acf2647e808e10db (patch) | |
tree | 565c87e051833daa1c0eed7ffe45533fcf05c2fb /src/import.cc | |
parent | eb30d42b5dd3ed5bb37336ed46a59eabaca81e06 (diff) | |
parent | 18e3a7d9ad8e052d626ac250a9c9d5f36f29a54d (diff) |
Merge pull request #270 from openscad/misc_updates
Big Endian binary STL fix + misc updates
Diffstat (limited to 'src/import.cc')
-rw-r--r-- | src/import.cc | 80 |
1 files changed, 54 insertions, 26 deletions
diff --git a/src/import.cc b/src/import.cc index 6a6d925..8980448 100644 --- a/src/import.cc +++ b/src/import.cc @@ -52,6 +52,9 @@ namespace fs = boost::filesystem; using namespace boost::assign; // bring 'operator+=()' into scope #include "boosty.h" +#include <boost/detail/endian.hpp> +#include <boost/cstdint.hpp> + class ImportModule : public AbstractModule { public: @@ -112,6 +115,47 @@ AbstractNode *ImportModule::evaluate(const Context *ctx, const ModuleInstantiati return node; } +#define STL_FACET_NUMBYTES 4*3*4+2 +// as there is no 'float32_t' standard, we assume the systems 'float' +// is a 'binary32' aka 'single' standard IEEE 32-bit floating point type +union stl_facet { + uint8_t data8[ STL_FACET_NUMBYTES ]; + uint32_t data32[4*3]; + struct facet_data { + float i, j, k; + float x1, y1, z1; + float x2, y2, z2; + float x3, y3, z3; + uint16_t attribute_byte_count; + } data; +}; + +void uint32_byte_swap( uint32_t &x ) +{ +#if defined(__GNUC__) || defined(__clang__) + x = __builtin_bswap32( x ); +#elif defined(_MSC_VER) + x = _byteswap_ulong( x ); +#else + uint32_t b1 = ( 0x000000FF & x ) << 24; + uint32_t b2 = ( 0x0000FF00 & x ) << 8; + uint32_t b3 = ( 0x00FF0000 & x ) >> 8; + uint32_t b4 = ( 0xFF000000 & x ) >> 24; + x = b1 | b2 | b3 | b4; +#endif +} + +void read_stl_facet( std::ifstream &f, stl_facet &facet ) +{ + f.read( (char*)facet.data8, STL_FACET_NUMBYTES ); +#ifdef BOOST_BIG_ENDIAN + for ( int i = 0; i < 12; i++ ) { + uint32_byte_swap( facet.data32[ i ] ); + } + // we ignore attribute byte count +#endif +} + PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const { PolySet *p = NULL; @@ -137,9 +181,11 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const int file_size = f.tellg(); f.seekg(80); if (!f.eof()) { - int facenum = 0; - // FIXME: Assumes little endian - f.read((char *)&facenum, sizeof(int)); + uint32_t facenum = 0; + f.read((char *)&facenum, sizeof(uint32_t)); +#ifdef BOOST_BIG_ENDIAN + uint32_byte_swap( facenum ); +#endif if (file_size == 80 + 4 + 50*facenum) binary = true; } f.seekg(0); @@ -186,32 +232,14 @@ PolySet *ImportNode::evaluate_polyset(class PolySetEvaluator *) const else { f.ignore(80-5+4); - // FIXME: Assumes little endian while (1) { -#ifdef _MSC_VER -#pragma pack(push,1) -#endif - struct { - float i, j, k; - float x1, y1, z1; - float x2, y2, z2; - float x3, y3, z3; - unsigned short acount; - } -#ifdef __GNUC__ - __attribute__ ((packed)) -#endif - stldata; -#ifdef _MSC_VER -#pragma pack(pop) -#endif - - f.read((char*)&stldata, sizeof(stldata)); + stl_facet facet; + read_stl_facet( f, facet ); if (f.eof()) break; p->append_poly(); - p->append_vertex(stldata.x1, stldata.y1, stldata.z1); - p->append_vertex(stldata.x2, stldata.y2, stldata.z2); - p->append_vertex(stldata.x3, stldata.y3, stldata.z3); + p->append_vertex(facet.data.x1, facet.data.y1, facet.data.z1); + p->append_vertex(facet.data.x2, facet.data.y2, facet.data.z2); + p->append_vertex(facet.data.x3, facet.data.y3, facet.data.z3); } } } |