2009-02-06 13:37:46 +00:00
|
|
|
/*===========================================================================*\
|
|
|
|
|
* *
|
|
|
|
|
* OpenMesh *
|
2011-01-26 09:21:12 +00:00
|
|
|
* Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
|
2009-02-06 13:37:46 +00:00
|
|
|
* www.openmesh.org *
|
|
|
|
|
* *
|
2009-11-17 13:54:16 +00:00
|
|
|
*---------------------------------------------------------------------------*
|
2009-06-04 08:46:29 +00:00
|
|
|
* This file is part of OpenMesh. *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
2009-11-17 13:54:16 +00:00
|
|
|
* OpenMesh is free software: you can redistribute it and/or modify *
|
2009-06-04 08:46:29 +00:00
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of *
|
|
|
|
|
* the License, or (at your option) any later version with the *
|
|
|
|
|
* following exceptions: *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
2009-06-04 08:46:29 +00:00
|
|
|
* If other files instantiate templates or use macros *
|
|
|
|
|
* or inline functions from this file, or you compile this file and *
|
|
|
|
|
* link it with other files to produce an executable, this file does *
|
|
|
|
|
* not by itself cause the resulting executable to be covered by the *
|
|
|
|
|
* GNU Lesser General Public License. This exception does not however *
|
|
|
|
|
* invalidate any other reasons why the executable file might be *
|
|
|
|
|
* covered by the GNU Lesser General Public License. *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
2009-06-04 08:46:29 +00:00
|
|
|
* OpenMesh is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* GNU Lesser General Public License for more details. *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
2009-06-04 08:46:29 +00:00
|
|
|
* You should have received a copy of the GNU LesserGeneral Public *
|
|
|
|
|
* License along with OpenMesh. If not, *
|
|
|
|
|
* see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
* *
|
2009-11-17 13:54:16 +00:00
|
|
|
\*===========================================================================*/
|
2009-06-04 08:46:29 +00:00
|
|
|
|
|
|
|
|
/*===========================================================================*\
|
2009-11-17 13:54:16 +00:00
|
|
|
* *
|
2009-06-04 08:46:29 +00:00
|
|
|
* $Revision$ *
|
|
|
|
|
* $Date$ *
|
2009-02-06 13:37:46 +00:00
|
|
|
* *
|
|
|
|
|
\*===========================================================================*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//== INCLUDES =================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// STL
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
|
|
|
|
|
|
// OpenMesh
|
|
|
|
|
#include <OpenMesh/Core/System/config.h>
|
|
|
|
|
#include <OpenMesh/Core/IO/BinaryHelper.hh>
|
|
|
|
|
#include <OpenMesh/Core/IO/reader/STLReader.hh>
|
|
|
|
|
#include <OpenMesh/Core/IO/IOManager.hh>
|
|
|
|
|
#include <OpenMesh/Core/System/omstream.hh>
|
|
|
|
|
#include <OpenMesh/Core/IO/importer/BaseImporter.hh>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=== NAMESPACES ==============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace OpenMesh {
|
|
|
|
|
namespace IO {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=== INSTANCIATE =============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// register the STLReader singleton with MeshReader
|
|
|
|
|
_STLReader_ __STLReaderInstance;
|
|
|
|
|
_STLReader_& STLReader() { return __STLReaderInstance; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=== IMPLEMENTATION ==========================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_STLReader_::
|
2009-11-17 13:54:16 +00:00
|
|
|
_STLReader_()
|
2009-02-06 13:37:46 +00:00
|
|
|
: eps_(FLT_MIN)
|
|
|
|
|
{
|
2009-11-17 13:54:16 +00:00
|
|
|
IOManager().register_module(this);
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
bool
|
2009-02-06 13:37:46 +00:00
|
|
|
_STLReader_::
|
|
|
|
|
read(const std::string& _filename, BaseImporter& _bi, Options& _opt)
|
|
|
|
|
{
|
|
|
|
|
bool result = false;
|
|
|
|
|
|
|
|
|
|
STL_Type file_type = NONE;
|
|
|
|
|
|
|
|
|
|
if ( check_extension( _filename, "stla" ) )
|
|
|
|
|
{
|
|
|
|
|
file_type = STLA;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if ( check_extension( _filename, "stlb" ) )
|
|
|
|
|
{
|
|
|
|
|
file_type = STLB;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if ( check_extension( _filename, "stl" ) )
|
|
|
|
|
{
|
|
|
|
|
file_type = check_stl_type(_filename);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (file_type)
|
|
|
|
|
{
|
|
|
|
|
case STLA:
|
|
|
|
|
{
|
|
|
|
|
result = read_stla(_filename, _bi);
|
|
|
|
|
_opt -= Options::Binary;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
case STLB:
|
|
|
|
|
{
|
|
|
|
|
result = read_stlb(_filename, _bi);
|
|
|
|
|
_opt += Options::Binary;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2009-11-17 13:54:16 +00:00
|
|
|
|
|
|
|
|
default:
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
result = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
bool
|
|
|
|
|
_STLReader_::read(std::istream& _is,
|
|
|
|
|
BaseImporter& _bi,
|
|
|
|
|
Options& _opt)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
omerr() << "[OMReader] : STL Streams are not supported " << std::endl;
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef DOXY_IGNORE_THIS
|
|
|
|
|
|
|
|
|
|
class CmpVec
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
CmpVec(float _eps=FLT_MIN) : eps_(_eps) {}
|
|
|
|
|
|
|
|
|
|
bool operator()( const Vec3f& _v0, const Vec3f& _v1 ) const
|
|
|
|
|
{
|
2009-11-17 13:54:16 +00:00
|
|
|
if (fabs(_v0[0] - _v1[0]) <= eps_)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
2009-11-17 13:54:16 +00:00
|
|
|
if (fabs(_v0[1] - _v1[1]) <= eps_)
|
2009-02-06 13:37:46 +00:00
|
|
|
{
|
|
|
|
|
return (_v0[2] < _v1[2] - eps_);
|
|
|
|
|
}
|
|
|
|
|
else return (_v0[1] < _v1[1] - eps_);
|
|
|
|
|
}
|
|
|
|
|
else return (_v0[0] < _v1[0] - eps_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
float eps_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
bool
|
2009-02-06 13:37:46 +00:00
|
|
|
_STLReader_::
|
|
|
|
|
read_stla(const std::string& _filename, BaseImporter& _bi) const
|
|
|
|
|
{
|
|
|
|
|
omlog() << "[STLReader] : read ascii file\n";
|
|
|
|
|
|
|
|
|
|
FILE* in = fopen(_filename.c_str(), "r");
|
|
|
|
|
if (!in)
|
|
|
|
|
{
|
2009-11-17 13:54:16 +00:00
|
|
|
omerr() << "[STLReader] : cannot not open file "
|
2009-02-06 13:37:46 +00:00
|
|
|
<< _filename
|
|
|
|
|
<< std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char line[100], *p;
|
|
|
|
|
unsigned int i;
|
|
|
|
|
OpenMesh::Vec3f v;
|
|
|
|
|
unsigned int cur_idx(0);
|
|
|
|
|
BaseImporter::VHandles vhandles;
|
|
|
|
|
|
|
|
|
|
CmpVec comp(eps_);
|
|
|
|
|
std::map<Vec3f, VertexHandle, CmpVec> vMap(comp);
|
|
|
|
|
std::map<Vec3f, VertexHandle, CmpVec>::iterator vMapIt;
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
while (in && !feof(in) && fgets(line, 100, in))
|
|
|
|
|
{
|
|
|
|
|
for (p=line; isspace(*p) && *p!='\0'; ++p) {}; // skip white-space
|
|
|
|
|
|
|
|
|
|
if ((strncmp(p, "outer", 5) == 0) || (strncmp(p, "OUTER", 5) == 0))
|
2009-11-17 13:54:16 +00:00
|
|
|
{
|
2009-02-06 13:37:46 +00:00
|
|
|
vhandles.clear();
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
for (i=0; i<3; ++i)
|
|
|
|
|
{
|
|
|
|
|
fgets(line, 100, in);
|
|
|
|
|
for (p=line; isspace(*p) && *p!='\0'; ++p) {}; // skip white-space
|
|
|
|
|
sscanf(p+6, "%f %f %f", &v[0], &v[1], &v[2]);
|
|
|
|
|
|
|
|
|
|
// has vector been referenced before?
|
|
|
|
|
if ((vMapIt=vMap.find(v)) == vMap.end())
|
|
|
|
|
{
|
|
|
|
|
// No : add vertex and remember idx/vector mapping
|
|
|
|
|
_bi.add_vertex(v);
|
|
|
|
|
vhandles.push_back(VertexHandle(cur_idx));
|
|
|
|
|
vMap[v] = VertexHandle(cur_idx++);
|
|
|
|
|
}
|
2009-11-17 13:54:16 +00:00
|
|
|
else
|
2009-02-06 13:37:46 +00:00
|
|
|
// Yes : get index from map
|
|
|
|
|
vhandles.push_back(vMapIt->second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add face only if it is not degenerated
|
|
|
|
|
if ((vhandles[0] != vhandles[1]) &&
|
|
|
|
|
(vhandles[0] != vhandles[2]) &&
|
|
|
|
|
(vhandles[1] != vhandles[2]))
|
|
|
|
|
_bi.add_face(vhandles);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fclose(in);
|
|
|
|
|
|
2011-07-13 15:10:37 +00:00
|
|
|
return true;
|
2009-02-06 13:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
bool
|
2009-02-06 13:37:46 +00:00
|
|
|
_STLReader_::
|
|
|
|
|
read_stlb(const std::string& _filename, BaseImporter& _bi) const
|
|
|
|
|
{
|
|
|
|
|
omlog() << "[STLReader] : read binary file\n";
|
|
|
|
|
|
|
|
|
|
FILE* in = fopen(_filename.c_str(), "rb");
|
|
|
|
|
if (!in)
|
|
|
|
|
{
|
2009-11-17 13:54:16 +00:00
|
|
|
omerr() << "[STLReader] : cannot not open file "
|
2009-02-06 13:37:46 +00:00
|
|
|
<< _filename
|
|
|
|
|
<< std::endl;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char dummy[100];
|
|
|
|
|
bool swapFlag;
|
|
|
|
|
unsigned int i, nT;
|
|
|
|
|
OpenMesh::Vec3f v;
|
|
|
|
|
unsigned int cur_idx(0);
|
|
|
|
|
BaseImporter::VHandles vhandles;
|
|
|
|
|
|
|
|
|
|
std::map<Vec3f, VertexHandle, CmpVec> vMap;
|
|
|
|
|
std::map<Vec3f, VertexHandle, CmpVec>::iterator vMapIt;
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// check size of types
|
|
|
|
|
if ((sizeof(float) != 4) || (sizeof(int) != 4)) {
|
|
|
|
|
omerr() << "[STLReader] : wrong type size\n";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// determine endian mode
|
|
|
|
|
union { unsigned int i; unsigned char c[4]; } endian_test;
|
|
|
|
|
endian_test.i = 1;
|
|
|
|
|
swapFlag = (endian_test.c[3] == 1);
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// read number of triangles
|
|
|
|
|
fread(dummy, 1, 80, in);
|
|
|
|
|
nT = read_int(in, swapFlag);
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// read triangles
|
|
|
|
|
while (nT)
|
|
|
|
|
{
|
|
|
|
|
vhandles.clear();
|
|
|
|
|
|
|
|
|
|
// skip triangle normal
|
|
|
|
|
fread(dummy, 1, 12, in);
|
|
|
|
|
|
|
|
|
|
// triangle's vertices
|
|
|
|
|
for (i=0; i<3; ++i)
|
|
|
|
|
{
|
|
|
|
|
v[0] = read_float(in, swapFlag);
|
|
|
|
|
v[1] = read_float(in, swapFlag);
|
|
|
|
|
v[2] = read_float(in, swapFlag);
|
|
|
|
|
|
|
|
|
|
// has vector been referenced before?
|
|
|
|
|
if ((vMapIt=vMap.find(v)) == vMap.end())
|
|
|
|
|
{
|
|
|
|
|
// No : add vertex and remember idx/vector mapping
|
|
|
|
|
_bi.add_vertex(v);
|
|
|
|
|
vhandles.push_back(VertexHandle(cur_idx));
|
|
|
|
|
vMap[v] = VertexHandle(cur_idx++);
|
|
|
|
|
}
|
2009-11-17 13:54:16 +00:00
|
|
|
else
|
2009-02-06 13:37:46 +00:00
|
|
|
// Yes : get index from map
|
|
|
|
|
vhandles.push_back(vMapIt->second);
|
|
|
|
|
}
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
// Add face only if it is not degenerated
|
|
|
|
|
if ((vhandles[0] != vhandles[1]) &&
|
|
|
|
|
(vhandles[0] != vhandles[2]) &&
|
|
|
|
|
(vhandles[1] != vhandles[2]))
|
|
|
|
|
_bi.add_face(vhandles);
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
fread(dummy, 1, 2, in);
|
|
|
|
|
--nT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_STLReader_::STL_Type
|
|
|
|
|
_STLReader_::
|
|
|
|
|
check_stl_type(const std::string& _filename) const
|
|
|
|
|
{
|
|
|
|
|
// assume it's binary stl, then file size is known from #triangles
|
|
|
|
|
// if size matches, it's really binary
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// open file
|
|
|
|
|
FILE* in = fopen(_filename.c_str(), "rb");
|
|
|
|
|
if (!in) return NONE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// determine endian mode
|
|
|
|
|
union { unsigned int i; unsigned char c[4]; } endian_test;
|
|
|
|
|
endian_test.i = 1;
|
|
|
|
|
bool swapFlag = (endian_test.c[3] == 1);
|
|
|
|
|
|
2009-11-17 13:54:16 +00:00
|
|
|
|
2009-02-06 13:37:46 +00:00
|
|
|
// read number of triangles
|
|
|
|
|
char dummy[100];
|
|
|
|
|
fread(dummy, 1, 80, in);
|
2012-03-20 09:32:02 +00:00
|
|
|
size_t nT = read_int(in, swapFlag);
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// compute file size from nT
|
2012-03-20 09:32:02 +00:00
|
|
|
size_t binary_size = 84 + nT*50;
|
2009-02-06 13:37:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// get actual file size
|
2012-03-20 09:32:02 +00:00
|
|
|
size_t file_size(0);
|
2009-02-06 13:37:46 +00:00
|
|
|
rewind(in);
|
|
|
|
|
while (!feof(in))
|
|
|
|
|
file_size += fread(dummy, 1, 100, in);
|
|
|
|
|
fclose(in);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if sizes match -> it's STLB
|
|
|
|
|
return (binary_size == file_size ? STLB : STLA);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
} // namespace IO
|
|
|
|
|
} // namespace OpenMesh
|
|
|
|
|
//=============================================================================
|