diff --git a/src/OpenMesh/Core/IO/importer/BaseImporter.hh b/src/OpenMesh/Core/IO/importer/BaseImporter.hh index 300accfc..afcfd6ca 100644 --- a/src/OpenMesh/Core/IO/importer/BaseImporter.hh +++ b/src/OpenMesh/Core/IO/importer/BaseImporter.hh @@ -106,6 +106,9 @@ public: // add texture coordinates per face, _vh references the first texcoord virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector& _face_texcoords) = 0; + // add texture 3d coordinates per face, _vh references the first texcoord + virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector& _face_texcoords) = 0; + // Set the texture index for a face virtual void set_face_texindex( FaceHandle _fh, int _texId ) = 0; @@ -133,6 +136,12 @@ public: // set vertex texture coordinate virtual void set_texcoord(HalfedgeHandle _heh, const Vec2f& _texcoord) = 0; + // set 3d vertex texture coordinate + virtual void set_texcoord(VertexHandle _vh, const Vec3f& _texcoord) = 0; + + // set 3d vertex texture coordinate + virtual void set_texcoord(HalfedgeHandle _heh, const Vec3f& _texcoord) = 0; + // set edge color virtual void set_color(EdgeHandle _eh, const Vec3uc& _color) = 0; diff --git a/src/OpenMesh/Core/IO/importer/ImporterT.hh b/src/OpenMesh/Core/IO/importer/ImporterT.hh index f3c1f655..aed9a909 100644 --- a/src/OpenMesh/Core/IO/importer/ImporterT.hh +++ b/src/OpenMesh/Core/IO/importer/ImporterT.hh @@ -90,6 +90,7 @@ public: typedef typename Mesh::Normal Normal; typedef typename Mesh::Color Color; typedef typename Mesh::TexCoord2D TexCoord2D; + typedef typename Mesh::TexCoord3D TexCoord3D; typedef std::vector VHandles; @@ -217,6 +218,19 @@ public: mesh_.set_texcoord2D(_heh, vector_cast(_texcoord)); } + virtual void set_texcoord(VertexHandle _vh, const Vec3f& _texcoord) + { + if (mesh_.has_vertex_texcoords3D()) + mesh_.set_texcoord3D(_vh, vector_cast(_texcoord)); + } + + virtual void set_texcoord(HalfedgeHandle _heh, const Vec3f& _texcoord) + { + if (mesh_.has_halfedge_texcoords3D()) + mesh_.set_texcoord3D(_heh, vector_cast(_texcoord)); + } + + // edge attributes virtual void set_color(EdgeHandle _eh, const Vec4uc& _color) @@ -292,6 +306,23 @@ public: } } + virtual void add_face_texcoords( FaceHandle _fh, VertexHandle _vh, const std::vector& _face_texcoords) + { + // get first halfedge handle + HalfedgeHandle cur_heh = mesh_.halfedge_handle(_fh); + HalfedgeHandle end_heh = mesh_.prev_halfedge_handle(cur_heh); + + // find start heh + while( mesh_.to_vertex_handle(cur_heh) != _vh && cur_heh != end_heh ) + cur_heh = mesh_.next_halfedge_handle( cur_heh); + + for(unsigned int i=0; i<_face_texcoords.size(); ++i) + { + set_texcoord( cur_heh, _face_texcoords[i]); + cur_heh = mesh_.next_halfedge_handle( cur_heh); + } + } + virtual void set_face_texindex( FaceHandle _fh, int _texId ) { if ( mesh_.has_face_texture_index() ) { mesh_.set_texture_index(_fh , _texId); diff --git a/src/OpenMesh/Core/IO/reader/OBJReader.cc b/src/OpenMesh/Core/IO/reader/OBJReader.cc index a9247c35..3e8f8e39 100644 --- a/src/OpenMesh/Core/IO/reader/OBJReader.cc +++ b/src/OpenMesh/Core/IO/reader/OBJReader.cc @@ -290,13 +290,13 @@ read(std::istream& _in, BaseImporter& _bi, Options& _opt) std::string line; std::string keyWrd; - float x, y, z, u, v; + float x, y, z, u, v, w; float r, g, b; BaseImporter::VHandles vhandles; std::vector normals; std::vector colors; - std::vector texcoords; - std::vector face_texcoords; + std::vector texcoords3d, face_texcoords3d; + std::vector texcoords, face_texcoords; std::vector vertexHandles; std::string matname; @@ -413,11 +413,16 @@ read(std::istream& _in, BaseImporter& _bi, Options& _opt) // Can be used for both! fileOptions += Options::VertexTexCoord; fileOptions += Options::FaceTexCoord; + + // try to read the w component as it is optional + stream >> w; + if ( !stream.fail() ) + texcoords3d.push_back(OpenMesh::Vec3f(u, v, w)); + } }else{ - - omerr() << "Only single 2D texture coordinate per vertex" + omerr() << "Only single 2D or 3D texture coordinate per vertex" << "allowed!" << std::endl; return false; } @@ -553,6 +558,8 @@ read(std::istream& _in, BaseImporter& _bi, Options& _opt) if (!texcoords.empty() && (unsigned int) (value - 1) < texcoords.size()) { // Obj counts from 1 and not zero .. array counts from zero therefore -1 _bi.set_texcoord(vhandles.back(), texcoords[value - 1]); + if(!texcoords3d.empty() && (unsigned int) (value -1) < texcoords3d.size()) + _bi.set_texcoord(vhandles.back(), texcoords3d[value - 1]); } else { omerr() << "Error setting Texture coordinates" << std::endl; } @@ -563,6 +570,8 @@ read(std::istream& _in, BaseImporter& _bi, Options& _opt) if (!texcoords.empty() && (unsigned int) (value - 1) < texcoords.size()) { face_texcoords.push_back( texcoords[value-1] ); + if(!texcoords3d.empty() && (unsigned int) (value -1) < texcoords3d.size()) + face_texcoords3d.push_back( texcoords3d[value-1] ); } else { omerr() << "Error setting Texture coordinates" << std::endl; } @@ -609,7 +618,10 @@ read(std::istream& _in, BaseImporter& _bi, Options& _opt) fh = _bi.add_face(faceVertices); if (!vhandles.empty() && fh.is_valid() ) + { _bi.add_face_texcoords(fh, vhandles[0], face_texcoords); + _bi.add_face_texcoords(fh, vhandles[0], face_texcoords3d); + } if ( !matname.empty() ) { diff --git a/src/Unittests/TestFiles/cube-minimal-texCoords3d.obj b/src/Unittests/TestFiles/cube-minimal-texCoords3d.obj new file mode 100644 index 00000000..54f42a35 --- /dev/null +++ b/src/Unittests/TestFiles/cube-minimal-texCoords3d.obj @@ -0,0 +1,39 @@ +g cube +v 0.0 0.0 0.0 +v 0.0 0.0 1.0 +v 0.0 1.0 0.0 +v 0.0 1.0 1.0 +v 1.0 0.0 0.0 +v 1.0 0.0 1.0 +v 1.0 1.0 0.0 +v 1.0 1.0 1.0 +vn 0.0 0.0 1.0 +vn 0.0 0.0 -1.0 +vn 0.0 1.0 0.0 +vn 0.0 -1.0 0.0 +vn 1.0 0.0 0.0 +vn -1.0 0.0 0.0 +vt 1.0 1.0 1.0 +vt 2.0 2.0 2.0 +vt 3.0 3.0 3.0 +vt 4.0 4.0 4.0 +vt 5.0 5.0 5.0 +vt 6.0 6.0 6.0 +vt 7.0 7.0 7.0 +vt 8.0 8.0 8.0 +vt 9.0 9.0 9.0 +vt 10.0 10.0 10.0 +vt 11.0 11.0 11.0 +vt 12.0 12.0 12.0 +f 1/1/2 7/1/2 5/1/2 +f 1/2/2 3/2/2 7/2/2 +f 1/3/6 4/3/6 3/3/6 +f 1/4/6 2/4/6 4/4/6 +f 3/5/3 8/5/3 7/5/3 +f 3/6/3 4/6/3 8/6/3 +f 5/7/5 7/7/5 8/7/5 +f 5/8/5 8/8/5 6/8/5 +f 1/9/4 5/9/4 6/9/4 +f 1/10/4 6/10/4 2/10/4 +f 2/11/1 6/11/1 8/11/1 +f 2/12/1 8/12/1 4/12/1 diff --git a/src/Unittests/unittests_read_write_OBJ.cc b/src/Unittests/unittests_read_write_OBJ.cc index a1416f8e..2b502d0f 100644 --- a/src/Unittests/unittests_read_write_OBJ.cc +++ b/src/Unittests/unittests_read_write_OBJ.cc @@ -202,6 +202,51 @@ TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJCheckTexCoords) { mesh_.release_halfedge_texcoords2D(); } +/* + * Just load a obj file of a cube and checks the 3d halfedge texCoords + */ +TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJCheckTexCoords3d) { + + mesh_.clear(); + + mesh_.request_halfedge_texcoords3D(); + + OpenMesh::IO::Options options; + options += OpenMesh::IO::Options::FaceTexCoord; + + std::string file_name = "cube-minimal-texCoords3d.obj"; + + bool ok = OpenMesh::IO::read_mesh(mesh_, file_name,options); + + EXPECT_TRUE(ok) << file_name; + + EXPECT_EQ(1, mesh_.texcoord3D(mesh_.halfedge_handle(0))[0] ) << "Wrong texCoord at halfedge 0 component 0"; + EXPECT_EQ(1, mesh_.texcoord3D(mesh_.halfedge_handle(0))[1] ) << "Wrong texCoord at halfedge 0 component 1"; + EXPECT_EQ(1, mesh_.texcoord3D(mesh_.halfedge_handle(0))[2] ) << "Wrong texCoord at halfedge 0 component 2"; + + EXPECT_EQ(3, mesh_.texcoord3D(mesh_.halfedge_handle(10))[0] ) << "Wrong texCoord at halfedge 1 component 0"; + EXPECT_EQ(3, mesh_.texcoord3D(mesh_.halfedge_handle(10))[1] ) << "Wrong texCoord at halfedge 1 component 1"; + EXPECT_EQ(3, mesh_.texcoord3D(mesh_.halfedge_handle(10))[2] ) << "Wrong texCoord at halfedge 1 component 2"; + + EXPECT_EQ(6, mesh_.texcoord3D(mesh_.halfedge_handle(19))[0] ) << "Wrong texCoord at halfedge 4 component 0"; + EXPECT_EQ(6, mesh_.texcoord3D(mesh_.halfedge_handle(19))[1] ) << "Wrong texCoord at halfedge 4 component 1"; + EXPECT_EQ(6, mesh_.texcoord3D(mesh_.halfedge_handle(19))[2] ) << "Wrong texCoord at halfedge 4 component 2"; + + EXPECT_EQ(7, mesh_.texcoord3D(mesh_.halfedge_handle(24))[0] ) << "Wrong texCoord at halfedge 7 component 0"; + EXPECT_EQ(7, mesh_.texcoord3D(mesh_.halfedge_handle(24))[1] ) << "Wrong texCoord at halfedge 7 component 1"; + EXPECT_EQ(7, mesh_.texcoord3D(mesh_.halfedge_handle(24))[2] ) << "Wrong texCoord at halfedge 7 component 2"; + + EXPECT_EQ(9, mesh_.texcoord3D(mesh_.halfedge_handle(30))[0] ) << "Wrong texCoord at halfedge 9 component 0"; + EXPECT_EQ(9, mesh_.texcoord3D(mesh_.halfedge_handle(30))[1] ) << "Wrong texCoord at halfedge 9 component 1"; + EXPECT_EQ(9, mesh_.texcoord3D(mesh_.halfedge_handle(30))[2] ) << "Wrong texCoord at halfedge 9 component 2"; + + EXPECT_EQ(12, mesh_.texcoord3D(mesh_.halfedge_handle(35))[0] ) << "Wrong texCoord at halfedge 11 component 0"; + EXPECT_EQ(12, mesh_.texcoord3D(mesh_.halfedge_handle(35))[1] ) << "Wrong texCoord at halfedge 11 component 1"; + EXPECT_EQ(12, mesh_.texcoord3D(mesh_.halfedge_handle(35))[2] ) << "Wrong texCoord at halfedge 11 component 2"; + + mesh_.request_halfedge_texcoords3D(); +} + /* * Just load a obj file of a square with a material */