unittests_mc_decimater.cc 5.6 KB
Newer Older
1 2 3 4 5 6 7

#include <gtest/gtest.h>
#include <Unittests/unittests_common.hh>
#include <OpenMesh/Tools/Decimater/McDecimaterT.hh>
#include <OpenMesh/Tools/Decimater/ModQuadricT.hh>
#include <OpenMesh/Tools/Decimater/ModNormalFlippingT.hh>

8 9
namespace {

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
class OpenMeshMultipleChoiceDecimater : 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
 * ====================================================================
 */

/*
 */
TEST_F(OpenMeshMultipleChoiceDecimater, DecimateMesh) {

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

  typedef OpenMesh::Decimater::McDecimaterT< Mesh >  Decimater;
45 46
  typedef OpenMesh::Decimater::ModQuadricT< Mesh >::Handle HModQuadric;
  typedef OpenMesh::Decimater::ModNormalFlippingT< Mesh >::Handle HModNormal;
47 48 49 50 51

  Decimater decimaterDBG(mesh_);
  HModQuadric hModQuadricDBG;
  decimaterDBG.add( hModQuadricDBG );
  decimaterDBG.initialize();
Jan Möbius's avatar
Jan Möbius committed
52
  size_t removedVertices = 0;
53 54 55
  removedVertices = decimaterDBG.decimate_to(5000);
                    decimaterDBG.mesh().garbage_collection();

56
  EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
57 58 59 60 61
  EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
  EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
  EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
}

Matthias Möller's avatar
Matthias Möller committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75
TEST_F(OpenMeshMultipleChoiceDecimater, DecimateMeshToFaceVerticesLimit) {

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

  ASSERT_TRUE(ok);

  typedef OpenMesh::Decimater::McDecimaterT< Mesh >  Decimater;
  typedef OpenMesh::Decimater::ModQuadricT< Mesh >::Handle HModQuadric;
  typedef OpenMesh::Decimater::ModNormalFlippingT< Mesh >::Handle HModNormal;

  Decimater decimaterDBG(mesh_);
  HModQuadric hModQuadricDBG;
  decimaterDBG.add( hModQuadricDBG );
  decimaterDBG.initialize();
Jan Möbius's avatar
Jan Möbius committed
76
  size_t removedVertices = 0;
Matthias Möller's avatar
Matthias Möller committed
77 78 79
  removedVertices = decimaterDBG.decimate_to_faces(5000, 8000);
                    decimaterDBG.mesh().garbage_collection();

80
  EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
Matthias Möller's avatar
Matthias Möller committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
  EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
  EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
}

TEST_F(OpenMeshMultipleChoiceDecimater, DecimateMeshToFaceFaceLimit) {

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

  ASSERT_TRUE(ok);

  typedef OpenMesh::Decimater::McDecimaterT< Mesh >  Decimater;
  typedef OpenMesh::Decimater::ModQuadricT< Mesh >::Handle HModQuadric;
  typedef OpenMesh::Decimater::ModNormalFlippingT< Mesh >::Handle HModNormal;

  Decimater decimaterDBG(mesh_);
  HModQuadric hModQuadricDBG;
  decimaterDBG.add( hModQuadricDBG );
  decimaterDBG.initialize();
Jan Möbius's avatar
Jan Möbius committed
100
  size_t removedVertices = 0;
Matthias Möller's avatar
Matthias Möller committed
101 102 103
  removedVertices = decimaterDBG.decimate_to_faces(4500, 9996);
                    decimaterDBG.mesh().garbage_collection();

104
  EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
Matthias Möller's avatar
Matthias Möller committed
105 106 107 108
  EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
  EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
  EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
}
109 110 111 112 113 114

class UnittestObserver : public OpenMesh::Decimater::Observer
{
    size_t notifies_;
    size_t all_steps_;
public:
Jan Möbius's avatar
Jan Möbius committed
115
    explicit UnittestObserver(size_t _steps) :Observer(_steps), notifies_(0), all_steps_(0) {}
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

    void notify(size_t _step)
    {
        ++notifies_;
        all_steps_ = _step;
    }
    bool abort() const
    {
        return all_steps_ >= 2526u;
    }

    size_t countedNotifies()
    {
        return notifies_;
    }
};

TEST_F(OpenMeshMultipleChoiceDecimater, DecimateMeshStoppedByObserver) {

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

    ASSERT_TRUE(ok);

    typedef OpenMesh::Decimater::McDecimaterT< Mesh >  Decimater;
    typedef OpenMesh::Decimater::ModQuadricT< Mesh >::Handle HModQuadric;

    Decimater decimaterDBG(mesh_);
    HModQuadric hModQuadricDBG;
    decimaterDBG.add(hModQuadricDBG);

    decimaterDBG.module(hModQuadricDBG).unset_max_err();

    decimaterDBG.initialize();
    UnittestObserver obs(2);
    decimaterDBG.set_observer(&obs);
    size_t removedVertices = 0;
    removedVertices = decimaterDBG.decimate_to_faces(0, 0);
    decimaterDBG.mesh().garbage_collection();

    EXPECT_TRUE(obs.abort()) << "Observer did not abort the decimater!";
    EXPECT_EQ(obs.countedNotifies(), 2526u / 2u) << "Observer did not get the right amount of notifications!";

    EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
    EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
    EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
    EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
}
163
}