60 #include <OpenMesh/Core/IO/BinaryHelper.hh>
61 #include <OpenMesh/Core/IO/reader/STLReader.hh>
62 #include <OpenMesh/Core/IO/IOManager.hh>
100 STL_Type file_type = NONE;
102 if ( check_extension( _filename,
"stla" ) )
107 else if ( check_extension( _filename,
"stlb" ) )
112 else if ( check_extension( _filename,
"stl" ) )
114 file_type = check_stl_type(_filename);
122 result = read_stla(_filename, _bi, _opt);
123 _opt -= Options::Binary;
129 result = read_stlb(_filename, _bi, _opt);
130 _opt += Options::Binary;
146 _STLReader_::read(std::istream& _is,
153 if (_opt & Options::Binary)
154 result = read_stlb(_is, _bi, _opt);
156 result = read_stla(_is, _bi, _opt);
165 #ifndef DOXY_IGNORE_THIS
171 CmpVec(
float _eps=FLT_MIN) : eps_(_eps) {}
173 bool operator()(
const Vec3f& _v0,
const Vec3f& _v1 )
const
175 if (fabs(_v0[0] - _v1[0]) <= eps_)
177 if (fabs(_v0[1] - _v1[1]) <= eps_)
179 return (_v0[2] < _v1[2] - eps_);
181 else return (_v0[1] < _v1[1] - eps_);
183 else return (_v0[0] < _v1[0] - eps_);
195 void trimStdString( std::string& _string) {
198 size_t start = _string.find_first_not_of(
" \t\r\n");
199 size_t end = _string.find_last_not_of(
" \t\r\n");
201 if(( std::string::npos == start ) || ( std::string::npos == end))
204 _string = _string.substr( start, end-start+1 );
211 read_stla(
const std::string& _filename,
BaseImporter& _bi, Options& _opt)
const
213 std::fstream in( _filename.c_str(), std::ios_base::in );
217 omerr() <<
"[STLReader] : cannot not open file "
223 bool res = read_stla(in, _bi, _opt);
235 read_stla(std::istream& _in,
BaseImporter& _bi, Options& _opt)
const
241 BaseImporter::VHandles vhandles;
244 std::map<Vec3f, VertexHandle, CmpVec> vMap(comp);
245 std::map<Vec3f, VertexHandle, CmpVec>::iterator vMapIt;
250 std::stringstream strstream;
252 bool facet_normal(
false);
254 while( _in && !_in.eof() ) {
257 std::getline(_in, line);
259 omerr() <<
" Warning! Could not read stream properly!\n";
267 if (line.find(
"facet normal") != std::string::npos) {
272 strstream >> garbage;
275 strstream >> garbage;
285 if ( (line.find(
"outer") != std::string::npos) || (line.find(
"OUTER") != std::string::npos ) ) {
289 for (i=0; i<3; ++i) {
291 std::getline(_in, line);
297 strstream >> garbage;
304 if ((vMapIt=vMap.find(v)) == vMap.end())
307 VertexHandle handle = _bi.add_vertex(v);
308 vhandles.push_back(handle);
313 vhandles.push_back(vMapIt->second);
318 if ((vhandles[0] != vhandles[1]) &&
319 (vhandles[0] != vhandles[2]) &&
320 (vhandles[1] != vhandles[2])) {
323 FaceHandle fh = _bi.add_face(vhandles);
328 if (fh.is_valid() && _opt.face_has_normal())
329 _bi.set_normal(fh, n);
331 _opt -= Options::FaceNormal;
334 facet_normal =
false;
345 read_stlb(
const std::string& _filename,
BaseImporter& _bi, Options& _opt)
const
347 std::fstream in( _filename.c_str(), std::ios_base::in | std::ios_base::binary);
351 omerr() <<
"[STLReader] : cannot not open file "
357 bool res = read_stlb(in, _bi, _opt);
369 read_stlb(std::istream& _in,
BaseImporter& _bi, Options& _opt)
const
375 BaseImporter::VHandles vhandles;
377 std::map<Vec3f, VertexHandle, CmpVec> vMap;
378 std::map<Vec3f, VertexHandle, CmpVec>::iterator vMapIt;
382 if ((
sizeof(
float) != 4) || (
sizeof(
int) != 4)) {
383 omerr() <<
"[STLReader] : wrong type size\n";
388 union {
unsigned int i;
unsigned char c[4]; } endian_test;
390 swapFlag = (endian_test.c[3] == 1);
414 if ((vMapIt=vMap.find(v)) == vMap.end())
417 VertexHandle handle = _bi.add_vertex(v);
418 vhandles.push_back(handle);
423 vhandles.push_back(vMapIt->second);
428 if ((vhandles[0] != vhandles[1]) &&
429 (vhandles[0] != vhandles[2]) &&
430 (vhandles[1] != vhandles[2])) {
431 FaceHandle fh = _bi.add_face(vhandles);
433 if (fh.is_valid() && _opt.face_has_normal())
434 _bi.set_normal(fh, n);
446 _STLReader_::STL_Type
448 check_stl_type(
const std::string& _filename)
const
455 FILE* in = fopen(_filename.c_str(),
"rb");
456 if (!in)
return NONE;
460 union {
unsigned int i;
unsigned char c[4]; } endian_test;
462 bool swapFlag = (endian_test.c[3] == 1);
467 fread(dummy, 1, 80, in);
472 size_t binary_size = 84 + nT*50;
479 file_size += fread(dummy, 1, 100, in);
484 return (binary_size == file_size ? STLB : STLA);
float read_float(FILE *_in, bool _swap=false)
int read_int(FILE *_in, bool _swap=false)
bool register_module(BaseReader *_bl)
Set options for reader/writer modules.
_STLReader_ __STLReaderInstance
Declare the single entity of the STL reader.
_IOManager_ & IOManager()
DrawMode NONE
not a valid draw mode