- added the functionality to read meshes from istreams with the STLReader
git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@704 fdac6126-5c0c-442c-9429-916003d36597
This commit is contained in:
@@ -124,6 +124,63 @@ double read_double(FILE* _in, bool _swap)
|
|||||||
return dc.d;
|
return dc.d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
short int read_short(std::istream& _in, bool _swap)
|
||||||
|
{
|
||||||
|
union u1 { short int s; unsigned char c[2]; } sc;
|
||||||
|
_in.read((char*)sc.c, 2);
|
||||||
|
if (_swap) std::swap(sc.c[0], sc.c[1]);
|
||||||
|
return sc.s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
int read_int(std::istream& _in, bool _swap)
|
||||||
|
{
|
||||||
|
union u2 { int i; unsigned char c[4]; } ic;
|
||||||
|
_in.read((char*)ic.c, 4);
|
||||||
|
if (_swap) {
|
||||||
|
std::swap(ic.c[0], ic.c[3]);
|
||||||
|
std::swap(ic.c[1], ic.c[2]);
|
||||||
|
}
|
||||||
|
return ic.i;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
float read_float(std::istream& _in, bool _swap)
|
||||||
|
{
|
||||||
|
union u3 { float f; unsigned char c[4]; } fc;
|
||||||
|
_in.read((char*)fc.c, 4);
|
||||||
|
if (_swap) {
|
||||||
|
std::swap(fc.c[0], fc.c[3]);
|
||||||
|
std::swap(fc.c[1], fc.c[2]);
|
||||||
|
}
|
||||||
|
return fc.f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
double read_double(std::istream& _in, bool _swap)
|
||||||
|
{
|
||||||
|
union u4 { double d; unsigned char c[8]; } dc;
|
||||||
|
_in.read((char*)dc.c, 8);
|
||||||
|
if (_swap) {
|
||||||
|
std::swap(dc.c[0], dc.c[7]);
|
||||||
|
std::swap(dc.c[1], dc.c[6]);
|
||||||
|
std::swap(dc.c[2], dc.c[5]);
|
||||||
|
std::swap(dc.c[3], dc.c[4]);
|
||||||
|
}
|
||||||
|
return dc.d;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|||||||
@@ -96,6 +96,22 @@ float read_float(FILE* _in, bool _swap=false);
|
|||||||
\c _swap is true */
|
\c _swap is true */
|
||||||
double read_double(FILE* _in, bool _swap=false);
|
double read_double(FILE* _in, bool _swap=false);
|
||||||
|
|
||||||
|
/** Binary read a \c short from \c _is and perform byte swapping if
|
||||||
|
\c _swap is true */
|
||||||
|
short int read_short(std::istream& _in, bool _swap=false);
|
||||||
|
|
||||||
|
/** Binary read an \c int from \c _is and perform byte swapping if
|
||||||
|
\c _swap is true */
|
||||||
|
int read_int(std::istream& _in, bool _swap=false);
|
||||||
|
|
||||||
|
/** Binary read a \c float from \c _is and perform byte swapping if
|
||||||
|
\c _swap is true */
|
||||||
|
float read_float(std::istream& _in, bool _swap=false);
|
||||||
|
|
||||||
|
/** Binary read a \c double from \c _is and perform byte swapping if
|
||||||
|
\c _swap is true */
|
||||||
|
double read_double(std::istream& _in, bool _swap=false);
|
||||||
|
|
||||||
|
|
||||||
/** Binary write a \c short to \c _os and perform byte swapping if
|
/** Binary write a \c short to \c _os and perform byte swapping if
|
||||||
\c _swap is true */
|
\c _swap is true */
|
||||||
|
|||||||
@@ -144,9 +144,14 @@ _STLReader_::read(std::istream& _is,
|
|||||||
Options& _opt)
|
Options& _opt)
|
||||||
{
|
{
|
||||||
|
|
||||||
omerr() << "[OMReader] : STL Streams are not supported " << std::endl;
|
bool result = false;
|
||||||
|
|
||||||
return false;
|
if (_opt & Options::Binary)
|
||||||
|
result = read_stlb(_is, _bi);
|
||||||
|
else
|
||||||
|
result = read_stla(_is, _bi);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -313,9 +318,111 @@ read_stla(const std::string& _filename, BaseImporter& _bi) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool
|
||||||
|
_STLReader_::
|
||||||
|
read_stla(std::istream& _in, BaseImporter& _bi) const
|
||||||
|
{
|
||||||
|
omlog() << "[STLReader] : read ascii stream\n";
|
||||||
|
|
||||||
|
|
||||||
|
unsigned int i;
|
||||||
|
OpenMesh::Vec3f v;
|
||||||
|
OpenMesh::Vec3f n;
|
||||||
|
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;
|
||||||
|
|
||||||
|
std::string line;
|
||||||
|
|
||||||
|
//bool normal = false;
|
||||||
|
|
||||||
|
while( _in && !_in.eof() ) {
|
||||||
|
|
||||||
|
// Get one line
|
||||||
|
std::getline(_in, line);
|
||||||
|
if ( _in.bad() ){
|
||||||
|
omerr() << " Warning! Could not read stream properly!\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Trim Both leading and trailing spaces
|
||||||
|
trimStdString(line);
|
||||||
|
|
||||||
|
// Normal found?
|
||||||
|
if (line.find("facet normal") != std::string::npos) {
|
||||||
|
std::stringstream strstream(line);
|
||||||
|
|
||||||
|
std::string garbage;
|
||||||
|
|
||||||
|
// facet
|
||||||
|
strstream >> garbage;
|
||||||
|
|
||||||
|
// normal
|
||||||
|
strstream >> garbage;
|
||||||
|
|
||||||
|
strstream >> n[0];
|
||||||
|
strstream >> n[1];
|
||||||
|
strstream >> n[2];
|
||||||
|
|
||||||
|
//normal = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Detected a triangle
|
||||||
|
if ( (line.find("outer") != std::string::npos) || (line.find("OUTER") != std::string::npos ) ) {
|
||||||
|
|
||||||
|
vhandles.clear();
|
||||||
|
|
||||||
|
for (i=0; i<3; ++i) {
|
||||||
|
// Get one vertex
|
||||||
|
std::getline(_in, line);
|
||||||
|
trimStdString(line);
|
||||||
|
|
||||||
|
std::stringstream strstream(line);
|
||||||
|
|
||||||
|
std::string garbage;
|
||||||
|
strstream >> garbage;
|
||||||
|
|
||||||
|
strstream >> v[0];
|
||||||
|
strstream >> v[1];
|
||||||
|
strstream >> 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++);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
// 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])) {
|
||||||
|
|
||||||
|
|
||||||
|
FaceHandle fh = _bi.add_face(vhandles);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//normal = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
bool
|
bool
|
||||||
_STLReader_::
|
_STLReader_::
|
||||||
@@ -403,9 +510,84 @@ read_stlb(const std::string& _filename, BaseImporter& _bi) const
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
|
bool
|
||||||
|
_STLReader_::
|
||||||
|
read_stlb(std::istream& _in, BaseImporter& _bi) const
|
||||||
|
{
|
||||||
|
omlog() << "[STLReader] : read binary stream\n";
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
// read number of triangles
|
||||||
|
//fread(dummy, 1, 80, in);
|
||||||
|
_in.read(dummy, 80);
|
||||||
|
nT = read_int(_in, swapFlag);
|
||||||
|
|
||||||
|
// read triangles
|
||||||
|
while (nT)
|
||||||
|
{
|
||||||
|
vhandles.clear();
|
||||||
|
|
||||||
|
// skip triangle normal
|
||||||
|
_in.read(dummy, 12);
|
||||||
|
|
||||||
|
// 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++);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
_in.read(dummy, 2);
|
||||||
|
--nT;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------------
|
||||||
|
|
||||||
_STLReader_::STL_Type
|
_STLReader_::STL_Type
|
||||||
_STLReader_::
|
_STLReader_::
|
||||||
|
|||||||
@@ -121,7 +121,9 @@ private:
|
|||||||
STL_Type check_stl_type(const std::string& _filename) const;
|
STL_Type check_stl_type(const std::string& _filename) const;
|
||||||
|
|
||||||
bool read_stla(const std::string& _filename, BaseImporter& _bi) const;
|
bool read_stla(const std::string& _filename, BaseImporter& _bi) const;
|
||||||
|
bool read_stla(std::istream& _in, BaseImporter& _bi) const;
|
||||||
bool read_stlb(const std::string& _filename, BaseImporter& _bi) const;
|
bool read_stlb(const std::string& _filename, BaseImporter& _bi) const;
|
||||||
|
bool read_stlb(std::istream& _in, BaseImporter& _bi) const;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
Reference in New Issue
Block a user