Commit bcd590e1 authored by Mike Kremer's avatar Mike Kremer

PLY format now supports texture coordinates (actually written/read per vertex...

PLY format now supports texture coordinates (actually written/read per vertex and not per face (halfedge))

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@158 fdac6126-5c0c-442c-9429-916003d36597
parent d1bd070c
...@@ -113,16 +113,21 @@ bool _PLYReader_::read(std::fstream& _in, BaseImporter& _bi, Options& _opt) cons ...@@ -113,16 +113,21 @@ bool _PLYReader_::read(std::fstream& _in, BaseImporter& _bi, Options& _opt) cons
// build options to be returned // build options to be returned
_opt.clear(); _opt.clear();
if (options_.vertex_has_normal() && userOptions_.vertex_has_normal()) if (options_.vertex_has_normal() && userOptions_.vertex_has_normal()) {
_opt += Options::VertexNormal; _opt += Options::VertexNormal;
if (options_.vertex_has_texcoord() && userOptions_.vertex_has_texcoord()) }
if (options_.vertex_has_texcoord() && userOptions_.vertex_has_texcoord()) {
_opt += Options::VertexTexCoord; _opt += Options::VertexTexCoord;
if (options_.vertex_has_color() && userOptions_.vertex_has_color()) }
if (options_.vertex_has_color() && userOptions_.vertex_has_color()) {
_opt += Options::VertexColor; _opt += Options::VertexColor;
if (options_.face_has_color() && userOptions_.face_has_color()) }
if (options_.face_has_color() && userOptions_.face_has_color()) {
_opt += Options::FaceColor; _opt += Options::FaceColor;
if (options_.is_binary()) }
if (options_.is_binary()) {
_opt += Options::Binary; _opt += Options::Binary;
}
// //force user-choice for the alpha value when reading binary // //force user-choice for the alpha value when reading binary
// if ( options_.is_binary() && userOptions_.color_has_alpha() ) // if ( options_.is_binary() && userOptions_.color_has_alpha() )
...@@ -148,7 +153,7 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const { ...@@ -148,7 +153,7 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const {
unsigned int nV; unsigned int nV;
OpenMesh::Vec3f v; OpenMesh::Vec3f v;
std::string trash; std::string trash;
// OpenMesh::Vec2f t; OpenMesh::Vec2f t;
OpenMesh::Vec4i c; OpenMesh::Vec4i c;
float tmp; float tmp;
BaseImporter::VHandles vhandles; BaseImporter::VHandles vhandles;
...@@ -167,6 +172,9 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const { ...@@ -167,6 +172,9 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const {
v[1] = 0.0; v[1] = 0.0;
v[2] = 0.0; v[2] = 0.0;
t[0] = 0.0;
t[1] = 0.0;
c[0] = 0; c[0] = 0;
c[1] = 0; c[1] = 0;
c[2] = 0; c[2] = 0;
...@@ -183,6 +191,12 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const { ...@@ -183,6 +191,12 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const {
case ZCOORD: case ZCOORD:
_in >> v[2]; _in >> v[2];
break; break;
case TEXX:
_in >> t[0];
break;
case TEXY:
_in >> t[1];
break;
case COLORRED: case COLORRED:
if (vertexPropertyMap_[propertyIndex].second == ValueTypeFLOAT32) { if (vertexPropertyMap_[propertyIndex].second == ValueTypeFLOAT32) {
_in >> tmp; _in >> tmp;
...@@ -218,6 +232,7 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const { ...@@ -218,6 +232,7 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const {
} }
vh = _bi.add_vertex(v); vh = _bi.add_vertex(v);
_bi.set_texcoord(vh, t);
_bi.set_color(vh, Vec4uc(c)); _bi.set_color(vh, Vec4uc(c));
} }
...@@ -254,66 +269,6 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const { ...@@ -254,66 +269,6 @@ bool _PLYReader_::read_ascii(std::fstream& _in, BaseImporter& _bi) const {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void _PLYReader_::readValue(ValueType _type, std::fstream& _in, float& _value) const {
switch (_type) {
case ValueTypeFLOAT32:
case ValueTypeFLOAT:
float32_t tmp;
restore(_in, tmp, options_.check(Options::MSB));
_value = tmp;
break;
default:
_value = 0.0;
std::cerr << "unsupported conversion type to float: " << _type << std::endl;
break;
}
}
void _PLYReader_::readValue(ValueType _type, std::fstream& _in, unsigned int& _value) const {
int32_t tmp_int32_t;
uint8_t tmp_uchar;
switch (_type) {
case ValueTypeINT:
case ValueTypeINT32:
restore(_in, tmp_int32_t, options_.check(Options::MSB));
_value = tmp_int32_t;
break;
case ValueTypeUCHAR:
restore(_in, tmp_uchar, options_.check(Options::MSB));
_value = tmp_uchar;
break;
default:
_value = 0;
std::cerr << "unsupported conversion type to int: " << _type << std::endl;
break;
}
}
void _PLYReader_::readValue(ValueType _type, std::fstream& _in, int& _value) const {
int32_t tmp_int32_t;
uint8_t tmp_uchar;
switch (_type) {
case ValueTypeINT:
case ValueTypeINT32:
restore(_in, tmp_int32_t, options_.check(Options::MSB));
_value = tmp_int32_t;
break;
case ValueTypeUCHAR:
restore(_in, tmp_uchar, options_.check(Options::MSB));
_value = tmp_uchar;
break;
default:
_value = 0;
std::cerr << "unsupported conversion type to int: " << _type << std::endl;
break;
}
}
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"; omlog() << "[PLYReader] : read binary file format\n";
...@@ -326,10 +281,11 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap ...@@ -326,10 +281,11 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap
unsigned int i, j, k, l, idx; unsigned int i, j, k, l, idx;
unsigned int nV; unsigned int nV;
OpenMesh::Vec3f v; OpenMesh::Vec3f v; // Vertex
OpenMesh::Vec2f t; // TexCoords
BaseImporter::VHandles vhandles; BaseImporter::VHandles vhandles;
VertexHandle vh; VertexHandle vh;
OpenMesh::Vec4i c; OpenMesh::Vec4i c; // Color
float tmp; float tmp;
_bi.reserve(vertexCount_, 3* vertexCount_ , faceCount_); _bi.reserve(vertexCount_, 3* vertexCount_ , faceCount_);
...@@ -340,6 +296,9 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap ...@@ -340,6 +296,9 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap
v[1] = 0.0; v[1] = 0.0;
v[2] = 0.0; v[2] = 0.0;
t[0] = 0.0;
t[1] = 0.0;
c[0] = 0; c[0] = 0;
c[1] = 0; c[1] = 0;
c[2] = 0; c[2] = 0;
...@@ -356,6 +315,12 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap ...@@ -356,6 +315,12 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap
case ZCOORD: case ZCOORD:
readValue(vertexPropertyMap_[propertyIndex].second, _in, v[2]); readValue(vertexPropertyMap_[propertyIndex].second, _in, v[2]);
break; break;
case TEXX:
readValue(vertexPropertyMap_[propertyIndex].second, _in, t[0]);
break;
case TEXY:
readValue(vertexPropertyMap_[propertyIndex].second, _in, t[1]);
break;
case COLORRED: case COLORRED:
if (vertexPropertyMap_[propertyIndex].second == ValueTypeFLOAT32) { if (vertexPropertyMap_[propertyIndex].second == ValueTypeFLOAT32) {
readValue(vertexPropertyMap_[propertyIndex].second, _in, tmp); readValue(vertexPropertyMap_[propertyIndex].second, _in, tmp);
...@@ -392,6 +357,7 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap ...@@ -392,6 +357,7 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap
} }
vh = _bi.add_vertex(v); vh = _bi.add_vertex(v);
_bi.set_texcoord(vh, t);
_bi.set_color(vh, Vec4uc(c)); _bi.set_color(vh, Vec4uc(c));
} }
...@@ -425,9 +391,75 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap ...@@ -425,9 +391,75 @@ bool _PLYReader_::read_binary(std::fstream& _in, BaseImporter& _bi, bool /*_swap
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void _PLYReader_::readValue(ValueType _type, std::fstream& _in, float& _value) const {
switch (_type) {
case ValueTypeFLOAT32:
case ValueTypeFLOAT:
float32_t tmp;
restore(_in, tmp, options_.check(Options::MSB));
_value = tmp;
break;
default:
_value = 0.0;
std::cerr << "unsupported conversion type to float: " << _type << std::endl;
break;
}
}
void _PLYReader_::readValue(ValueType _type, std::fstream& _in, unsigned int& _value) const {
int32_t tmp_int32_t;
uint8_t tmp_uchar;
switch (_type) {
case ValueTypeINT:
case ValueTypeINT32:
restore(_in, tmp_int32_t, options_.check(Options::MSB));
_value = tmp_int32_t;
break;
case ValueTypeUCHAR:
restore(_in, tmp_uchar, options_.check(Options::MSB));
_value = tmp_uchar;
break;
default:
_value = 0;
std::cerr << "unsupported conversion type to int: " << _type << std::endl;
break;
}
}
void _PLYReader_::readValue(ValueType _type, std::fstream& _in, int& _value) const {
int32_t tmp_int32_t;
uint8_t tmp_uchar;
switch (_type) {
case ValueTypeINT:
case ValueTypeINT32:
restore(_in, tmp_int32_t, options_.check(Options::MSB));
_value = tmp_int32_t;
break;
case ValueTypeUCHAR:
restore(_in, tmp_uchar, options_.check(Options::MSB));
_value = tmp_uchar;
break;
default:
_value = 0;
std::cerr << "unsupported conversion type to int: " << _type << std::endl;
break;
}
}
//------------------------------------------------------------------------------
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& ) // !!! Assuming BaseReader::can_u_parse( std::string& )
// does not call BaseReader::read_magic()!!! // does not call BaseReader::read_magic()!!!
if (BaseReader::can_u_read(_filename)) { if (BaseReader::can_u_read(_filename)) {
std::ifstream ifs(_filename.c_str()); std::ifstream ifs(_filename.c_str());
if (ifs.is_open() && can_u_read(ifs)) { if (ifs.is_open() && can_u_read(ifs)) {
...@@ -595,6 +627,14 @@ bool _PLYReader_::can_u_read(std::istream& _is) const { ...@@ -595,6 +627,14 @@ bool _PLYReader_::can_u_read(std::istream& _is) const {
std::pair<VertexProperty, ValueType> entry(ZCOORD, valueType); std::pair<VertexProperty, ValueType> entry(ZCOORD, valueType);
vertexPropertyMap_[vertexPropertyCount_] = entry; vertexPropertyMap_[vertexPropertyCount_] = entry;
vertexDimension_++; vertexDimension_++;
} else if (propertyName == "u" || propertyName == "s") {
std::pair<VertexProperty, ValueType> entry(TEXX, valueType);
vertexPropertyMap_[vertexPropertyCount_] = entry;
options_ += Options::VertexTexCoord;
} else if (propertyName == "v" || propertyName == "t") {
std::pair<VertexProperty, ValueType> entry(TEXY, valueType);
vertexPropertyMap_[vertexPropertyCount_] = entry;
options_ += Options::VertexTexCoord;
} else if (propertyName == "red") { } else if (propertyName == "red") {
std::pair<VertexProperty, ValueType> entry(COLORRED, valueType); std::pair<VertexProperty, ValueType> entry(COLORRED, valueType);
vertexPropertyMap_[vertexPropertyCount_] = entry; vertexPropertyMap_[vertexPropertyCount_] = entry;
......
...@@ -4,10 +4,10 @@ ...@@ -4,10 +4,10 @@
* Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen * * Copyright (C) 2001-2009 by Computer Graphics Group, RWTH Aachen *
* www.openmesh.org * * www.openmesh.org *
* * * *
*---------------------------------------------------------------------------* *---------------------------------------------------------------------------*
* This file is part of OpenMesh. * * This file is part of OpenMesh. *
* * * *
* OpenMesh is free software: you can redistribute it and/or modify * * OpenMesh is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as * * it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 3 of * * published by the Free Software Foundation, either version 3 of *
* the License, or (at your option) any later version with the * * the License, or (at your option) any later version with the *
...@@ -30,10 +30,10 @@ ...@@ -30,10 +30,10 @@
* License along with OpenMesh. If not, * * License along with OpenMesh. If not, *
* see <http://www.gnu.org/licenses/>. * * see <http://www.gnu.org/licenses/>. *
* * * *
\*===========================================================================*/ \*===========================================================================*/
/*===========================================================================*\ /*===========================================================================*\
* * * *
* $Revision$ * * $Revision$ *
* $Date$ * * $Date$ *
* * * *
...@@ -125,9 +125,9 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -125,9 +125,9 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const
unsigned int i, j, nV, nF; unsigned int i, j, nV, nF;
Vec3f v, n; Vec3f v, n;
Vec2f t;
OpenMesh::Vec3f c; OpenMesh::Vec3f c;
OpenMesh::Vec4f cA; OpenMesh::Vec4f cA;
OpenMesh::Vec2f t;
VertexHandle vh; VertexHandle vh;
std::vector<VertexHandle> vhandles; std::vector<VertexHandle> vhandles;
...@@ -140,6 +140,11 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -140,6 +140,11 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const
_out << "property float32 y" << std::endl; _out << "property float32 y" << std::endl;
_out << "property float32 z" << std::endl; _out << "property float32 z" << std::endl;
if ( _opt.vertex_has_texcoord() ){
_out << "property float32 u" << std::endl;
_out << "property float32 v" << std::endl;
}
if ( _opt.vertex_has_color() ){ if ( _opt.vertex_has_color() ){
_out << "property int32 red" << std::endl; _out << "property int32 red" << std::endl;
_out << "property int32 green" << std::endl; _out << "property int32 green" << std::endl;
...@@ -162,6 +167,12 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -162,6 +167,12 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const
//Vertex //Vertex
_out << v[0] << " " << v[1] << " " << v[2]; _out << v[0] << " " << v[1] << " " << v[2];
// Vertex TexCoords
if ( _opt.vertex_has_texcoord() ) {
t = _be.texcoord(vh);
_out << " " << t[0] << " " << t[1];
}
// VertexColor // VertexColor
if ( _opt.vertex_has_color() ) { if ( _opt.vertex_has_color() ) {
//with alpha //with alpha
...@@ -177,16 +188,16 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -177,16 +188,16 @@ write_ascii(std::fstream& _out, BaseExporter& _be, Options _opt) const
_out << "\n"; _out << "\n";
} }
// faces (indices starting at 0) // faces (indices starting at 0)
if (_be.is_triangle_mesh()) if (_be.is_triangle_mesh())
{ {
for (i=0, nF=_be.n_faces(); i<nF; ++i) for (i=0, nF=_be.n_faces(); i<nF; ++i)
{ {
_be.get_vhandles(FaceHandle(i), vhandles); _be.get_vhandles(FaceHandle(i), vhandles);
_out << 3 << " "; _out << 3 << " ";
_out << vhandles[0].idx() << " "; _out << vhandles[0].idx() << " ";
_out << vhandles[1].idx() << " "; _out << vhandles[1].idx() << " ";
_out << vhandles[2].idx(); _out << vhandles[2].idx();
// //face color // //face color
...@@ -297,7 +308,7 @@ void _PLYWriter_::writeValue(ValueType _type, std::fstream& _out, float value) c ...@@ -297,7 +308,7 @@ void _PLYWriter_::writeValue(ValueType _type, std::fstream& _out, float value) c
} }
} }
bool bool
_PLYWriter_:: _PLYWriter_::
write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const
{ {
...@@ -310,7 +321,6 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -310,7 +321,6 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const
VertexHandle vh; VertexHandle vh;
std::vector<VertexHandle> vhandles; std::vector<VertexHandle> vhandles;
//writing header //writing header
_out << "ply" << std::endl; _out << "ply" << std::endl;
_out << "format "; _out << "format ";
...@@ -328,6 +338,11 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -328,6 +338,11 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const
_out << "property float32 y" << std::endl; _out << "property float32 y" << std::endl;
_out << "property float32 z" << std::endl; _out << "property float32 z" << std::endl;
if ( _opt.vertex_has_texcoord() ){
_out << "property float32 u" << std::endl;
_out << "property float32 v" << std::endl;
}
if ( _opt.vertex_has_color() ){ if ( _opt.vertex_has_color() ){
_out << "property int32 red" << std::endl; _out << "property int32 red" << std::endl;
_out << "property int32 green" << std::endl; _out << "property int32 green" << std::endl;
...@@ -340,18 +355,25 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -340,18 +355,25 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const
_out << "element face " << _be.n_faces() << std::endl; _out << "element face " << _be.n_faces() << std::endl;
_out << "property list uchar int32 vertex_indices" << std::endl; _out << "property list uchar int32 vertex_indices" << std::endl;
_out << "end_header" << std::endl; _out << "end_header" << std::endl;
// vertex data (point, normals, texcoords) // vertex data (point, normals, texcoords)
for (i=0, nV=_be.n_vertices(); i<nV; ++i) for (i=0, nV=_be.n_vertices(); i<nV; ++i)
{ {
vh = VertexHandle(i); vh = VertexHandle(i);
v = _be.point(vh); v = _be.point(vh);
//vertex //vertex
writeValue(ValueTypeFLOAT, _out, v[0]); writeValue(ValueTypeFLOAT, _out, v[0]);
writeValue(ValueTypeFLOAT, _out, v[1]); writeValue(ValueTypeFLOAT, _out, v[1]);
writeValue(ValueTypeFLOAT, _out, v[2]); writeValue(ValueTypeFLOAT, _out, v[2]);
// Vertex TexCoords
if ( _opt.vertex_has_texcoord() ) {
t = _be.texcoord(vh);
writeValue(ValueTypeFLOAT, _out, t[0]);
writeValue(ValueTypeFLOAT, _out, t[1]);
}
// vertex color // vertex color
if ( _opt.vertex_has_color() ) { if ( _opt.vertex_has_color() ) {
c = _be.colorA(vh); c = _be.colorA(vh);
...@@ -382,7 +404,7 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -382,7 +404,7 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const
// writeValue(_out, c[0]); // writeValue(_out, c[0]);
// writeValue(_out, c[1]); // writeValue(_out, c[1]);
// writeValue(_out, c[2]); // writeValue(_out, c[2]);
// //
// if ( _opt.color_has_alpha() ) // if ( _opt.color_has_alpha() )
// writeValue(_out, c[3]); // writeValue(_out, c[3]);
// } // }
...@@ -404,7 +426,7 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const ...@@ -404,7 +426,7 @@ write_binary(std::fstream& _out, BaseExporter& _be, Options _opt) const
// writeValue(_out, c[0]); // writeValue(_out, c[0]);
// writeValue(_out, c[1]); // writeValue(_out, c[1]);
// writeValue(_out, c[2]); // writeValue(_out, c[2]);
// //
// if ( _opt.color_has_alpha() ) // if ( _opt.color_has_alpha() )
// writeValue(_out, c[3]); // writeValue(_out, c[3]);
// } // }
...@@ -427,7 +449,7 @@ binary_size(BaseExporter& _be, Options _opt) const ...@@ -427,7 +449,7 @@ binary_size(BaseExporter& _be, Options _opt) const
size_t _3floats(3*sizeof(float)); size_t _3floats(3*sizeof(float));
size_t _3ui(3*sizeof(unsigned int)); size_t _3ui(3*sizeof(unsigned int));
size_t _4ui(4*sizeof(unsigned int)); size_t _4ui(4*sizeof(unsigned int));
if ( !_opt.is_binary() ) if ( !_opt.is_binary() )
return 0; return 0;
else else
...@@ -442,7 +464,7 @@ binary_size(BaseExporter& _be, Options _opt) const ...@@ -442,7 +464,7 @@ binary_size(BaseExporter& _be, Options _opt) const
header += 1; // N header += 1; // N
data += _be.n_vertices() * _3floats; data += _be.n_vertices() * _3floats;
} }
if ( _opt.vertex_has_color() && _be.has_vertex_colors() ) if ( _opt.vertex_has_color() && _be.has_vertex_colors() )
{ {
header += 1; // C header += 1; // C
...@@ -473,7 +495,7 @@ binary_size(BaseExporter& _be, Options _opt) const ...@@ -473,7 +495,7 @@ binary_size(BaseExporter& _be, Options _opt) const
} }
} }
// face colors // face colors
if ( _opt.face_has_color() && _be.has_face_colors() ){ if ( _opt.face_has_color() && _be.has_face_colors() ){
if ( _opt.color_has_alpha() ) if ( _opt.color_has_alpha() )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment