unittests_read_write_OFF.cc 8.53 KB
Newer Older
1 2
#include <gtest/gtest.h>
#include <Unittests/unittests_common.hh>
3
#include <cstdio>
4 5


6 7
namespace {

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
class OpenMeshReadWriteOFF : public OpenMeshBase {

    protected:

        // This function is called before each test is run
        virtual void SetUp() {

            // Do some initial stuff with the member data here...
        }

        // This function is called after all tests are through
        virtual void TearDown() {

            // Do some final stuff with the member data here...
        }

    // Member already defined in OpenMeshBase
    //Mesh mesh_;
};

/*
 * ====================================================================
 * Define tests below
 * ====================================================================
 */

/*
 * Just load a simple mesh file in obj format and count whether
 * the right number of entities has been loaded.
 */
TEST_F(OpenMeshReadWriteOFF, LoadSimpleOFFFile) {

    mesh_.clear();

    bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.off");

    EXPECT_TRUE(ok);

    EXPECT_EQ(7526u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
    EXPECT_EQ(22572u, mesh_.n_edges()) << "The number of loaded edges is not correct!";
    EXPECT_EQ(15048u, mesh_.n_faces()) << "The number of loaded faces is not correct!";
}


TEST_F(OpenMeshReadWriteOFF, WriteAndReadVertexColorsToAndFromOFFFile) {

    mesh_.clear();

    mesh_.request_vertex_colors();

    mesh_.add_vertex( Mesh::Point(0,0,1) );
    mesh_.add_vertex( Mesh::Point(0,1,0) );
    mesh_.add_vertex( Mesh::Point(0,1,1) );
    mesh_.add_vertex( Mesh::Point(1,0,1) );

    // using the default color type Vec3uc from DefaultTraits in Traits.hh
    Mesh::Color testColor(255, 128, 64);

    // setting colors (different from black)
    for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit)
68
      mesh_.set_color(*vit, testColor);
69 70 71 72

    // check if the colors are correctly setted
    int count = 0;
    for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit) {
73
      Mesh::Color color = mesh_.color(*vit);
74 75 76 77 78 79 80 81 82 83
      if ( color[0] != testColor[0] || color[1] != testColor[1] || color[2] != testColor[2] )
        ++ count;
    }

    EXPECT_EQ(0, count) << "Vertices have the wrong color!";

    // write the mesh_
    OpenMesh::IO::Options opt(OpenMesh::IO::Options::VertexColor);
    OpenMesh::IO::write_mesh(mesh_, "temp.off", opt);
    OpenMesh::IO::read_mesh(mesh_, "temp.off", opt);
84
    remove("temp.off");
85 86 87 88

    // check if vertices still have the same color
    count = 0;
    for (Mesh::VertexIter vit = mesh_.vertices_begin(), vitend = mesh_.vertices_end(); vit != vitend; ++vit) {
89
      Mesh::Color color = mesh_.color(*vit);
90 91 92 93 94 95 96 97 98
      if ( color[0] != testColor[0] || color[1] != testColor[1] || color[2] != testColor[2] )
        ++ count;
    }

    EXPECT_EQ(0, count) << "Vertices should have the same color after writing and reading the OFF file!";

    mesh_.release_vertex_colors();
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
TEST_F(OpenMeshReadWriteOFF, WriteAndReadFloatVertexColorsToAndFromOFFFile) {

    mesh_.clear();

    mesh_.request_vertex_colors();

    OpenMesh::IO::Options opt(OpenMesh::IO::Options::VertexColor);

    bool ok = OpenMesh::IO::read_mesh(mesh_, "meshlab.ply", opt);

    EXPECT_TRUE(ok) << "meshlab.ply could not be read!";

    opt.clear();
    opt += OpenMesh::IO::Options::VertexColor;
    opt += OpenMesh::IO::Options::ColorFloat;

    // write the mesh_
    ok = OpenMesh::IO::write_mesh(mesh_, "cube_floating.off", opt);
    EXPECT_TRUE(ok) << "cube_floating.off could not be written!";
    mesh_.clear();
    ok = OpenMesh::IO::read_mesh(mesh_, "cube_floating.off", opt);
    EXPECT_TRUE(ok) << "cube_floating.off could not be read!";

    EXPECT_EQ(8u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
    EXPECT_EQ(18u , mesh_.n_edges())    << "The number of loaded edges is not correct!";
    EXPECT_EQ(12u , mesh_.n_faces())    << "The number of loaded faces is not correct!";

    EXPECT_EQ(0,   mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
    EXPECT_EQ(0,   mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
    EXPECT_EQ(255,   mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";

    EXPECT_EQ(0,   mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
    EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
    EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";

    EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
    EXPECT_EQ(0,   mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
    EXPECT_EQ(255,   mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";

    EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
    EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
    EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";

    EXPECT_FALSE(opt.vertex_has_normal()) << "Wrong user opt are returned!";
    EXPECT_FALSE(opt.vertex_has_texcoord()) << "Wrong user opt are returned!";
    EXPECT_TRUE(opt.vertex_has_color()) << "Wrong user opt are returned!";
    EXPECT_TRUE(opt.color_is_float()) << "Wrong user opt are returned!";

    mesh_.release_vertex_colors();
}

TEST_F(OpenMeshReadWriteOFF, WriteAndReadBinaryFloatVertexColorsToAndFromOFFFile) {

    mesh_.clear();

    mesh_.request_vertex_colors();

    OpenMesh::IO::Options opt(OpenMesh::IO::Options::VertexColor);

    bool ok = OpenMesh::IO::read_mesh(mesh_, "meshlab.ply", opt);

    EXPECT_TRUE(ok) << "meshlab.ply could not be read!";

    opt.clear();
    opt += OpenMesh::IO::Options::VertexColor;
    opt += OpenMesh::IO::Options::Binary;
    opt += OpenMesh::IO::Options::ColorFloat;

    // write the mesh_
    ok = OpenMesh::IO::write_mesh(mesh_, "cube_floating_binary.off", opt);
    EXPECT_TRUE(ok) << "cube_floating_binary.off could not be written!";
    mesh_.clear();
    opt.clear();
    opt += OpenMesh::IO::Options::VertexColor;
    opt += OpenMesh::IO::Options::Binary;
    opt += OpenMesh::IO::Options::ColorFloat;
    ok = OpenMesh::IO::read_mesh(mesh_, "cube_floating_binary.off", opt);
    EXPECT_TRUE(ok) << "cube_floating_binary.off could not be read!";

    EXPECT_EQ(8u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
    EXPECT_EQ(18u , mesh_.n_edges())    << "The number of loaded edges is not correct!";
    EXPECT_EQ(12u , mesh_.n_faces())    << "The number of loaded faces is not correct!";

    EXPECT_EQ(0,   mesh_.color(mesh_.vertex_handle(0))[0] ) << "Wrong vertex color at vertex 0 component 0";
    EXPECT_EQ(0,   mesh_.color(mesh_.vertex_handle(0))[1] ) << "Wrong vertex color at vertex 0 component 1";
    EXPECT_EQ(255,   mesh_.color(mesh_.vertex_handle(0))[2] ) << "Wrong vertex color at vertex 0 component 2";

    EXPECT_EQ(0,   mesh_.color(mesh_.vertex_handle(3))[0] ) << "Wrong vertex color at vertex 3 component 0";
    EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(3))[1] ) << "Wrong vertex color at vertex 3 component 1";
    EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(3))[2] ) << "Wrong vertex color at vertex 3 component 2";

    EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(4))[0] ) << "Wrong vertex color at vertex 4 component 0";
    EXPECT_EQ(0,   mesh_.color(mesh_.vertex_handle(4))[1] ) << "Wrong vertex color at vertex 4 component 1";
    EXPECT_EQ(255,   mesh_.color(mesh_.vertex_handle(4))[2] ) << "Wrong vertex color at vertex 4 component 2";

    EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[0] ) << "Wrong vertex color at vertex 7 component 0";
    EXPECT_EQ(0, mesh_.color(mesh_.vertex_handle(7))[1] ) << "Wrong vertex color at vertex 7 component 1";
    EXPECT_EQ(255, mesh_.color(mesh_.vertex_handle(7))[2] ) << "Wrong vertex color at vertex 7 component 2";

    EXPECT_FALSE(opt.vertex_has_normal()) << "Wrong user opt are returned!";
    EXPECT_FALSE(opt.vertex_has_texcoord()) << "Wrong user opt are returned!";
    EXPECT_FALSE(opt.face_has_color()) << "Wrong user opt are returned!";
    EXPECT_TRUE(opt.vertex_has_color()) << "Wrong user opt are returned!";
    EXPECT_TRUE(opt.color_is_float()) << "Wrong user opt are returned!";
    EXPECT_TRUE(opt.is_binary()) << "Wrong user opt are returned!";

    mesh_.release_vertex_colors();
}
207
}