Developer Documentation
unittests_decimater.cc
1 
2 #include <gtest/gtest.h>
3 #include <Unittests/unittests_common.hh>
6 
7 namespace {
8 
9 class OpenMeshDecimater : public OpenMeshBase {
10 
11  protected:
12 
13  // This function is called before each test is run
14  virtual void SetUp() {
15 
16  // Do some initial stuff with the member data here...
17  }
18 
19  // This function is called after all tests are through
20  virtual void TearDown() {
21 
22  // Do some final stuff with the member data here...
23  }
24 
25  // Member already defined in OpenMeshBase
26  //Mesh mesh_;
27 };
28 
29 /*
30  * ====================================================================
31  * Define tests below
32  * ====================================================================
33  */
34 
35 /*
36  */
37 TEST_F(OpenMeshDecimater, DecimateMesh) {
38 
39  bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.off");
40 
41  ASSERT_TRUE(ok);
42 
45 
46  Decimater decimaterDBG(mesh_);
47  HModQuadric hModQuadricDBG;
48  decimaterDBG.add( hModQuadricDBG );
49  decimaterDBG.initialize();
50  size_t removedVertices = 0;
51  removedVertices = decimaterDBG.decimate_to(5000);
52  decimaterDBG.mesh().garbage_collection();
53 
54  EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
55  EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
56  EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
57  EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
58 }
59 
60 TEST_F(OpenMeshDecimater, DecimateMeshToFaceVerticesLimit) {
61 
62  bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.off");
63 
64  ASSERT_TRUE(ok);
65 
68 
69  Decimater decimaterDBG(mesh_);
70  HModQuadric hModQuadricDBG;
71  decimaterDBG.add( hModQuadricDBG );
72  decimaterDBG.initialize();
73  size_t removedVertices = 0;
74  removedVertices = decimaterDBG.decimate_to_faces(5000, 8000);
75  decimaterDBG.mesh().garbage_collection();
76 
77  EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
78  EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
79  EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
80  EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
81 }
82 
83 TEST_F(OpenMeshDecimater, DecimateMeshToFaceFaceLimit) {
84 
85  bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.off");
86 
87  ASSERT_TRUE(ok);
88 
91 
92  Decimater decimaterDBG(mesh_);
93  HModQuadric hModQuadricDBG;
94  decimaterDBG.add( hModQuadricDBG );
95  decimaterDBG.initialize();
96  size_t removedVertices = 0;
97  removedVertices = decimaterDBG.decimate_to_faces(4500, 9996);
98  decimaterDBG.mesh().garbage_collection();
99 
100  EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
101  EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
102  EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
103  EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
104 }
105 
106 TEST_F(OpenMeshDecimater, DecimateMeshExampleFromDoc) {
107 
108  bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.off");
109 
110  ASSERT_TRUE(ok);
111 
112  typedef OpenMesh::Decimater::DecimaterT< Mesh > Decimater;
114 
115  Decimater decimaterDBG(mesh_);
116  HModQuadric hModQuadricDBG;
117  decimaterDBG.add( hModQuadricDBG );
118 
119  decimaterDBG.module( hModQuadricDBG ).unset_max_err();
120 
121  decimaterDBG.initialize();
122  size_t removedVertices = 0;
123  removedVertices = decimaterDBG.decimate_to_faces(4500, 9996);
124  decimaterDBG.mesh().garbage_collection();
125 
126  EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
127  EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
128  EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
129  EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
130 }
131 
132 class UnittestObserver : public OpenMesh::Decimater::Observer
133 {
134  size_t notifies_;
135  size_t all_steps_;
136 public:
137  UnittestObserver(size_t _steps) :Observer(_steps), notifies_(0), all_steps_(0) {}
138 
139  void notify(size_t _step)
140  {
141  ++notifies_;
142  all_steps_ = _step;
143  }
144  bool abort() const
145  {
146  return all_steps_ >= 2526u;
147  }
148 
149  size_t countedNotifies()
150  {
151  return notifies_;
152  }
153 };
154 
155 TEST_F(OpenMeshDecimater, DecimateMeshStoppedByObserver) {
156 
157  bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.off");
158 
159  ASSERT_TRUE(ok);
160 
161  typedef OpenMesh::Decimater::DecimaterT< Mesh > Decimater;
163 
164  Decimater decimaterDBG(mesh_);
165  HModQuadric hModQuadricDBG;
166  decimaterDBG.add(hModQuadricDBG);
167 
168  decimaterDBG.module(hModQuadricDBG).unset_max_err();
169 
170  decimaterDBG.initialize();
171  UnittestObserver obs(2);
172  decimaterDBG.set_observer(&obs);
173  size_t removedVertices = 0;
174  removedVertices = decimaterDBG.decimate_to_faces(0, 0);
175  decimaterDBG.mesh().garbage_collection();
176 
177  EXPECT_TRUE(obs.abort()) << "Observer did not abort the decimater!";
178  EXPECT_EQ(obs.countedNotifies(), 2526u / 2u) << "Observer did not get the right amount of notifications!";
179 
180  EXPECT_EQ(2526u, removedVertices) << "The number of remove vertices is not correct!";
181  EXPECT_EQ(5000u, mesh_.n_vertices()) << "The number of vertices after decimation is not correct!";
182  EXPECT_EQ(14994u, mesh_.n_edges()) << "The number of edges after decimation is not correct!";
183  EXPECT_EQ(9996u, mesh_.n_faces()) << "The number of faces after decimation is not correct!";
184 }
185 
186 
187 }
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:104
Mesh decimation module computing collapse priority based on error quadrics.
Definition: ModQuadricT.hh:83