From f5e5aa83b19cc478a1691391a2112bf45055476f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20M=C3=B6bius?= Date: Fri, 24 Sep 2021 11:10:47 +0200 Subject: [PATCH] Fixed OBJ reader not reading materials which start with a leading space --- src/OpenMesh/Core/IO/reader/OBJReader.cc | 11 +++--- src/Unittests/TestFiles/CubeCol.mtl | 9 +++++ src/Unittests/TestFiles/CubeCol.obj | 28 ++++++++++++++++ src/Unittests/unittests_read_write_OBJ.cc | 41 +++++++++++++++++++++++ 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 src/Unittests/TestFiles/CubeCol.mtl create mode 100644 src/Unittests/TestFiles/CubeCol.obj diff --git a/src/OpenMesh/Core/IO/reader/OBJReader.cc b/src/OpenMesh/Core/IO/reader/OBJReader.cc index bdefcd7d..6fe6141e 100644 --- a/src/OpenMesh/Core/IO/reader/OBJReader.cc +++ b/src/OpenMesh/Core/IO/reader/OBJReader.cc @@ -188,23 +188,24 @@ read_material(std::fstream& _in) stream >> keyWrd; - if( ( isspace(line[0]) && line[0] != '\t' ) || line[0] == '#' ) + if (keyWrd == "newmtl") // begin new material definition { + // If we are in a material definition (Already reading a material) + // And a material name has been set + // And the current material definition is valid + // Then Store the current material in our lookup table if (indef && !key.empty() && mat.is_valid()) { materials_[key] = mat; mat.cleanup(); } - } - else if (keyWrd == "newmtl") // begin new material definition - { stream >> key; indef = true; } else if (keyWrd == "Kd") // diffuse color - { + { stream >> f1; stream >> f2; stream >> f3; if( !stream.fail() ) diff --git a/src/Unittests/TestFiles/CubeCol.mtl b/src/Unittests/TestFiles/CubeCol.mtl new file mode 100644 index 00000000..bcbb1b20 --- /dev/null +++ b/src/Unittests/TestFiles/CubeCol.mtl @@ -0,0 +1,9 @@ +newmtl cube +Ns 10.0000 +Ni 1.000 +d 1.0000 +illum 2 +#Ambient, Diffuse, Specular colors + Ka 1.0000 0.7000 0.1000 + Kd 0.1250 0.2500 0.5000 + Ks 1.0000 0.6000 0.4000 diff --git a/src/Unittests/TestFiles/CubeCol.obj b/src/Unittests/TestFiles/CubeCol.obj new file mode 100644 index 00000000..b6087333 --- /dev/null +++ b/src/Unittests/TestFiles/CubeCol.obj @@ -0,0 +1,28 @@ +# cube.obj +# +o cube +mtllib CubeCol.mtl + +v -0.500000 -0.500000 0.500000 +v 0.500000 -0.500000 0.500000 +v -0.500000 0.500000 0.500000 +v 0.500000 0.500000 0.500000 +v -0.500000 0.500000 -0.500000 +v 0.500000 0.500000 -0.500000 +v -0.500000 -0.500000 -0.500000 +v 0.500000 -0.500000 -0.500000 + +g cube +usemtl cube +f 1 2 3 +f 3 2 4 +f 3 4 5 +f 5 4 6 +f 5 6 7 +f 7 6 8 +f 7 8 1 +f 1 8 2 +f 2 8 4 +f 4 8 6 +f 7 1 5 +f 5 1 3 \ No newline at end of file diff --git a/src/Unittests/unittests_read_write_OBJ.cc b/src/Unittests/unittests_read_write_OBJ.cc index c779daa0..51671e9f 100644 --- a/src/Unittests/unittests_read_write_OBJ.cc +++ b/src/Unittests/unittests_read_write_OBJ.cc @@ -623,7 +623,48 @@ TEST_F(OpenMeshReadWriteOBJ, FaceTexCoordTest) { } +/* + * Load, save and load a simple obj + */ +TEST_F(OpenMeshReadWriteOBJ, ReadOBJMTL) { + mesh_.clear(); + mesh_.request_face_colors(); + + OpenMesh::IO::Options options; + options += OpenMesh::IO::Options::FaceColor; + bool ok = OpenMesh::IO::read_mesh(mesh_, "CubeCol.obj", options); + + EXPECT_TRUE(ok) << "Unable to load CubeCol.obj"; + + OpenMesh::FaceHandle fh0 = mesh_.face_handle(0); + OpenMesh::FaceHandle fh1 = mesh_.face_handle(1); + OpenMesh::FaceHandle fh2 = mesh_.face_handle(2); + OpenMesh::FaceHandle fh3 = mesh_.face_handle(3); + + + EXPECT_TRUE(fh0.is_valid()) << "fh0 should be valid"; + EXPECT_TRUE(fh1.is_valid()) << "fh1 should be valid"; + EXPECT_TRUE(fh2.is_valid()) << "fh2 should be valid"; + EXPECT_TRUE(fh3.is_valid()) << "fh3 should be valid"; + + EXPECT_FLOAT_EQ(32, mesh_.color(fh0)[0] ) << "Wrong vertex color at face 0 component 0"; + EXPECT_FLOAT_EQ(64 , mesh_.color(fh0)[1] ) << "Wrong vertex color at face 0 component 1"; + EXPECT_FLOAT_EQ(128, mesh_.color(fh0)[2] ) << "Wrong vertex color at face 0 component 2"; + + EXPECT_FLOAT_EQ(32, mesh_.color(fh1)[0] ) << "Wrong vertex color at face 1 component 0"; + EXPECT_FLOAT_EQ(64, mesh_.color(fh1)[1] ) << "Wrong vertex color at face 1 component 1"; + EXPECT_FLOAT_EQ(128, mesh_.color(fh1)[2] ) << "Wrong vertex color at face 1 component 2"; + + EXPECT_FLOAT_EQ(32, mesh_.color(fh2)[0] ) << "Wrong vertex color at face 2 component 0"; + EXPECT_FLOAT_EQ(64, mesh_.color(fh2)[1] ) << "Wrong vertex color at face 2 component 1"; + EXPECT_FLOAT_EQ(128, mesh_.color(fh2)[2] ) << "Wrong vertex color at face 2 component 2"; + + EXPECT_FLOAT_EQ(32, mesh_.color(fh3)[0] ) << "Wrong vertex color at face 3 component 0"; + EXPECT_FLOAT_EQ(64, mesh_.color(fh3)[1] ) << "Wrong vertex color at face 3 component 1"; + EXPECT_FLOAT_EQ(128, mesh_.color(fh3)[2] ) << "Wrong vertex color at face 3 component 2"; + +}