56#include <OpenMesh/Core/IO/BinaryHelper.hh>
57#include <OpenMesh/Core/IO/reader/STLReader.hh>
58#include <OpenMesh/Core/IO/IOManager.hh>
62 #define strnicmp _strnicmp
64 #define strnicmp strncasecmp
103 STL_Type file_type = NONE;
105 if ( check_extension( _filename,
"stla" ) )
110 else if ( check_extension( _filename,
"stlb" ) )
115 else if ( check_extension( _filename,
"stl" ) )
117 file_type = check_stl_type(_filename);
125 result = read_stla(_filename, _bi, _opt);
126 _opt -= Options::Binary;
132 result = read_stlb(_filename, _bi, _opt);
133 _opt += Options::Binary;
149_STLReader_::read(std::istream& _is,
156 if (_opt & Options::Binary)
157 result = read_stlb(_is, _bi, _opt);
159 result = read_stla(_is, _bi, _opt);
168#ifndef DOXY_IGNORE_THIS
174 explicit CmpVec(
float _eps=FLT_MIN) : eps_(_eps) {}
176 bool operator()(
const Vec3f& _v0,
const Vec3f& _v1 )
const
178 if (fabs(_v0[0] - _v1[0]) <= eps_)
180 if (fabs(_v0[1] - _v1[1]) <= eps_)
182 return (_v0[2] < _v1[2] - eps_);
184 else return (_v0[1] < _v1[1] - eps_);
186 else return (_v0[0] < _v1[0] - eps_);
198void trimStdString( std::string& _string) {
201 size_t start = _string.find_first_not_of(
" \t\r\n");
202 size_t end = _string.find_last_not_of(
" \t\r\n");
204 if(( std::string::npos == start ) || ( std::string::npos == end))
207 _string = _string.substr( start, end-start+1 );
214read_stla(
const std::string& _filename,
BaseImporter& _bi, Options& _opt)
const
216 std::fstream in( _filename.c_str(), std::ios_base::in );
220 omerr() <<
"[STLReader] : cannot not open file "
226 bool res = read_stla(in, _bi, _opt);
238read_stla(std::istream& _in,
BaseImporter& _bi, Options& _opt)
const
244 BaseImporter::VHandles vhandles;
247 std::map<Vec3f, VertexHandle, CmpVec> vMap(comp);
248 std::map<Vec3f, VertexHandle, CmpVec>::iterator vMapIt;
253 std::stringstream strstream;
255 bool facet_normal(
false);
257 while( _in && !_in.eof() ) {
260 std::getline(_in, line);
262 omerr() <<
" Warning! Could not read stream properly!\n";
270 if (line.find(
"facet normal") != std::string::npos) {
275 strstream >> garbage;
278 strstream >> garbage;
288 if ( (line.find(
"outer") != std::string::npos) || (line.find(
"OUTER") != std::string::npos ) ) {
292 for (i=0; i<3; ++i) {
294 std::getline(_in, line);
300 strstream >> garbage;
307 if ((vMapIt=vMap.find(v)) == vMap.end())
310 VertexHandle handle = _bi.add_vertex(v);
311 vhandles.push_back(handle);
316 vhandles.push_back(vMapIt->second);
321 if ((vhandles[0] != vhandles[1]) &&
322 (vhandles[0] != vhandles[2]) &&
323 (vhandles[1] != vhandles[2])) {
326 FaceHandle fh = _bi.add_face(vhandles);
331 if (fh.is_valid() && _opt.face_has_normal())
332 _bi.set_normal(fh, n);
334 _opt -= Options::FaceNormal;
337 facet_normal =
false;
348read_stlb(
const std::string& _filename,
BaseImporter& _bi, Options& _opt)
const
350 std::fstream in( _filename.c_str(), std::ios_base::in | std::ios_base::binary);
354 omerr() <<
"[STLReader] : cannot not open file "
360 bool res = read_stlb(in, _bi, _opt);
372read_stlb(std::istream& _in,
BaseImporter& _bi, Options& _opt)
const
378 BaseImporter::VHandles vhandles;
380 std::map<Vec3f, VertexHandle, CmpVec> vMap;
381 std::map<Vec3f, VertexHandle, CmpVec>::iterator vMapIt;
385 if ((
sizeof(
float) != 4) || (
sizeof(
int) != 4)) {
386 omerr() <<
"[STLReader] : wrong type size\n";
391 union {
unsigned int i;
unsigned char c[4]; } endian_test;
393 swapFlag = (endian_test.c[3] == 1);
417 if ((vMapIt=vMap.find(v)) == vMap.end())
420 VertexHandle handle = _bi.add_vertex(v);
421 vhandles.push_back(handle);
426 vhandles.push_back(vMapIt->second);
431 if ((vhandles[0] != vhandles[1]) &&
432 (vhandles[0] != vhandles[2]) &&
433 (vhandles[1] != vhandles[2])) {
434 FaceHandle fh = _bi.add_face(vhandles);
436 if (fh.is_valid() && _opt.face_has_normal())
437 _bi.set_normal(fh, n);
451check_stl_type(
const std::string& _filename)
const
457 FILE* in = fopen(_filename.c_str(),
"rb");
458 if (!in)
return NONE;
461 union {
unsigned int i;
unsigned char c[4]; } endian_test;
463 bool swapFlag = (endian_test.c[3] == 1);
468 fread(dummy, 1, 80, in);
478 file_size += fread(dummy, 1, 100, in);
482 return (binary_size == file_size ? STLB : STLA);
Set options for reader/writer modules.
bool register_module(BaseReader *_bl)
DrawMode NONE
not a valid draw mode
_STLReader_ __STLReaderInstance
Declare the single entity of the STL reader.
size_t binary_size(const Mesh &_mesh, const std::string &_ext, Options _opt=Options::Default)
Get binary size of data.
float read_float(FILE *_in, bool _swap=false)
int read_int(FILE *_in, bool _swap=false)
_IOManager_ & IOManager()