Fixed issue with PLYReader:

Unsupported properties in ply-file will now be skipped instead of read as x-coordinates.

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@157 fdac6126-5c0c-442c-9429-916003d36597
This commit is contained in:
Mike Kremer
2009-06-09 15:47:49 +00:00
parent bfc96ee8ad
commit d1bd070c35
2 changed files with 564 additions and 607 deletions

View File

@@ -39,10 +39,8 @@
* *
\*===========================================================================*/
#define LINE_LEN 4096
//== INCLUDES =================================================================
// OpenMesh
@@ -68,92 +66,75 @@
namespace OpenMesh {
namespace IO {
//=============================================================================
//=== INSTANCIATE =============================================================
_PLYReader_ __PLYReaderInstance;
_PLYReader_& PLYReader() { return __PLYReaderInstance; }
_PLYReader_& PLYReader() {
return __PLYReaderInstance;
}
//=== IMPLEMENTATION ==========================================================
_PLYReader_::_PLYReader_()
{
_PLYReader_::_PLYReader_() {
IOManager().register_module(this);
}
//-----------------------------------------------------------------------------
bool
_PLYReader_::read(const std::string& _filename, BaseImporter& _bi,
Options& _opt)
{
bool _PLYReader_::read(const std::string& _filename, BaseImporter& _bi, Options& _opt) {
std::fstream in(_filename.c_str(), (options_.is_binary() ? std::ios_base::binary | std::ios_base::in
: std::ios_base::in));
if (!in)
{
omerr() << "[PLYReader] : cannot not open file "
<< _filename
<< std::endl;
if (!in) {
omerr() << "[PLYReader] : cannot not open file " << _filename << std::endl;
return false;
}
bool result = read(in, _bi, _opt);
in.close();
return result;
}
//-----------------------------------------------------------------------------
bool
_PLYReader_::read(std::fstream& _in, BaseImporter& _bi, Options& _opt ) const
{
bool _PLYReader_::read(std::fstream& _in, BaseImporter& _bi, Options& _opt) const {
// filter relevant options for reading
bool swap = _opt.check(Options::Swap);
userOptions_ = _opt;
// build options to be returned
_opt.clear();
if (options_.vertex_has_normal() && userOptions_.vertex_has_normal()) _opt += Options::VertexNormal;
if (options_.vertex_has_texcoord() && userOptions_.vertex_has_texcoord()) _opt += Options::VertexTexCoord;
if (options_.vertex_has_color() && userOptions_.vertex_has_color()) _opt += Options::VertexColor;
if (options_.face_has_color() && userOptions_.face_has_color()) _opt += Options::FaceColor;
if (options_.is_binary()) _opt += Options::Binary;
if (options_.vertex_has_normal() && userOptions_.vertex_has_normal())
_opt += Options::VertexNormal;
if (options_.vertex_has_texcoord() && userOptions_.vertex_has_texcoord())
_opt += Options::VertexTexCoord;
if (options_.vertex_has_color() && userOptions_.vertex_has_color())
_opt += Options::VertexColor;
if (options_.face_has_color() && userOptions_.face_has_color())
_opt += Options::FaceColor;
if (options_.is_binary())
_opt += Options::Binary;
// //force user-choice for the alpha value when reading binary
// if ( options_.is_binary() && userOptions_.color_has_alpha() )
// options_ += Options::ColorAlpha;
return (options_.is_binary() ?
read_binary(_in, _bi, swap) :
read_ascii(_in, _bi));
return (options_.is_binary() ? read_binary(_in, _bi, swap) : read_ascii(_in, _bi));
}
//-----------------------------------------------------------------------------
bool
_PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const
{
bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const {
omlog() << "[PLYReader] : read ascii file\n";
@@ -163,8 +144,6 @@ _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const
return false;
}
unsigned int i, j, k, l, idx;
unsigned int nV;
OpenMesh::Vec3f v;
@@ -175,7 +154,6 @@ _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const
BaseImporter::VHandles vhandles;
VertexHandle vh;
_bi.reserve(vertexCount_, 3* vertexCount_ , faceCount_);
if (vertexDimension_ != 3) {
@@ -184,8 +162,7 @@ _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const
}
// read vertices:
for (i=0; i< vertexCount_ && !_in.eof(); ++i)
{
for (i = 0; i < vertexCount_ && !_in.eof(); ++i) {
v[0] = 0.0;
v[1] = 0.0;
v[2] = 0.0;
@@ -208,25 +185,29 @@ _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const
break;
case COLORRED:
if (vertexPropertyMap_[propertyIndex].second == ValueTypeFLOAT32) {
_in >> tmp; c[0] = static_cast<OpenMesh::Vec4i::value_type>(tmp * 255.0f);
_in >> tmp;
c[0] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
} else
_in >> c[0];
break;
case COLORGREEN:
if (vertexPropertyMap_[propertyIndex].second == ValueTypeFLOAT32) {
_in >> tmp; c[1] = static_cast<OpenMesh::Vec4i::value_type>(tmp * 255.0f);
_in >> tmp;
c[1] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
} else
_in >> c[1];
break;
case COLORBLUE:
if (vertexPropertyMap_[propertyIndex].second == ValueTypeFLOAT32) {
_in >> tmp; c[2] = static_cast<OpenMesh::Vec4i::value_type>(tmp * 255.0f);
_in >> tmp;
c[2] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
} else
_in >> c[2];
break;
case COLORALPHA:
if (vertexPropertyMap_[propertyIndex].second == ValueTypeFLOAT32) {
_in >> tmp; c[3] = static_cast<OpenMesh::Vec4i::value_type>(tmp * 255.0f);
_in >> tmp;
c[3] = static_cast<OpenMesh::Vec4i::value_type> (tmp * 255.0f);
} else
_in >> c[3];
break;
@@ -242,13 +223,11 @@ _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const
// faces
// #N <v1> <v2> .. <v(n-1)> [color spec]
for (i=0; i<faceCount_; ++i)
{
for (i = 0; i < faceCount_; ++i) {
// nV = number of Vertices for current face
_in >> nV;
if (nV == 3)
{
if (nV == 3) {
vhandles.resize(3);
_in >> j;
_in >> k;
@@ -257,12 +236,9 @@ _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const
vhandles[0] = VertexHandle(j);
vhandles[1] = VertexHandle(k);
vhandles[2] = VertexHandle(l);
}
else
{
} else {
vhandles.clear();
for (j=0; j<nV; ++j)
{
for (j = 0; j < nV; ++j) {
_in >> idx;
vhandles.push_back(VertexHandle(idx));
}
@@ -276,7 +252,6 @@ _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const
return true;
}
//-----------------------------------------------------------------------------
void _PLYReader_::readValue(ValueType _type, std::fstream& _in, float& _value) const {
@@ -339,9 +314,7 @@ void _PLYReader_::readValue(ValueType _type , std::fstream& _in, int& _value) co
}
}
bool
_PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) const
{
bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) const {
omlog() << "[PLYReader] : read binary file format\n";
@@ -362,8 +335,7 @@ _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) c
_bi.reserve(vertexCount_, 3* vertexCount_ , faceCount_);
// read vertices:
for (i=0; i< vertexCount_ && !_in.eof(); ++i)
{
for (i = 0; i < vertexCount_ && !_in.eof(); ++i) {
v[0] = 0.0;
v[1] = 0.0;
v[2] = 0.0;
@@ -423,13 +395,11 @@ _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) c
_bi.set_color(vh, Vec4uc(c));
}
for (i=0; i<faceCount_; ++i)
{
for (i = 0; i < faceCount_; ++i) {
// Read number of vertices for the current face
readValue(faceIndexType_, _in, nV);
if (nV == 3)
{
if (nV == 3) {
vhandles.resize(3);
readValue(faceEntryType_, _in, j);
readValue(faceEntryType_, _in, k);
@@ -438,12 +408,9 @@ _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) c
vhandles[0] = VertexHandle(j);
vhandles[1] = VertexHandle(k);
vhandles[2] = VertexHandle(l);
}
else
{
} else {
vhandles.clear();
for (j=0; j<nV; ++j)
{
for (j = 0; j < nV; ++j) {
readValue(faceEntryType_, _in, idx);
vhandles.push_back(VertexHandle(idx));
}
@@ -455,19 +422,15 @@ _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap*/) c
return true;
}
//-----------------------------------------------------------------------------
bool _PLYReader_::can_u_read(const std::string& _filename) const
{
bool _PLYReader_::can_u_read(const std::string& _filename) const {
// !!! Assuming BaseReader::can_u_parse( std::string& )
// does not call BaseReader::read_magic()!!!
if (BaseReader::can_u_read(_filename))
{
if (BaseReader::can_u_read(_filename)) {
std::ifstream ifs(_filename.c_str());
if (ifs.is_open() && can_u_read(ifs))
{
if (ifs.is_open() && can_u_read(ifs)) {
ifs.close();
return true;
}
@@ -475,22 +438,15 @@ bool _PLYReader_::can_u_read(const std::string& _filename) const
return false;
}
//-----------------------------------------------------------------------------
std::string get_property_name(std::string _string1, std::string _string2) {
if ( _string1 == "float32" ||
_string1 == "uint8" ||
_string1 == "uchar" ||
_string1 == "float" ||
_string1 == "int32")
if (_string1 == "float32" || _string1 == "uint8" || _string1 == "uchar" || _string1 == "float" || _string1
== "int32")
return _string2;
if ( _string2 == "float32" ||
_string2 == "uint8" ||
_string2 == "uchar" ||
_string2 == "float" ||
_string2 == "int32")
if (_string2 == "float32" || _string2 == "uint8" || _string2 == "uchar" || _string2 == "float" || _string2
== "int32")
return _string1;
std::cerr << "Unsupported entry type" << std::endl;
@@ -516,8 +472,7 @@ _PLYReader_::ValueType get_property_type(std::string _string1 , std::string _str
//-----------------------------------------------------------------------------
bool _PLYReader_::can_u_read(std::istream& _is) const
{
bool _PLYReader_::can_u_read(std::istream& _is) const {
// Clear per file options
options_.cleanup();
@@ -658,6 +613,8 @@ bool _PLYReader_::can_u_read(std::istream& _is) const
options_ += Options::VertexColor;
options_ += Options::ColorAlpha;
} else {
std::pair<VertexProperty, ValueType> entry(UNSUPPORTED, valueType);
vertexPropertyMap_[vertexPropertyCount_] = entry;
std::cerr << "Unsupported property : " << propertyName << std::endl;
}
@@ -690,7 +647,6 @@ bool _PLYReader_::can_u_read(std::istream& _is) const
return true;
}
//=============================================================================
} // namespace IO
} // namespace OpenMesh

View File

@@ -143,7 +143,8 @@ private:
enum VertexProperty {
XCOORD,YCOORD,ZCOORD,
TEXX,TEXY,
COLORRED,COLORGREEN,COLORBLUE,COLORALPHA
COLORRED,COLORGREEN,COLORBLUE,COLORALPHA,
UNSUPPORTED
};