Developer Documentation
unittests_convert_meshes.cc
1 
2 #include <gtest/gtest.h>
3 #include <Unittests/unittests_common.hh>
4 #include <OpenMesh/Core/Utils/PropertyManager.hh>
5 
6 namespace {
7 
8 class OpenMeshConvertTriangleMeshToPoly : public OpenMeshBase {
9 
10  protected:
11 
12  // This function is called before each test is run
13  virtual void SetUp() {
14  mesh_.clear();
15 
16  // Add some vertices
17  Mesh::VertexHandle vhandle[4];
18 
19  vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
20  vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
21  vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
22  vhandle[3] = mesh_.add_vertex(Mesh::Point(1, 0, 0));
23 
24  // Add two faces
25  std::vector<Mesh::VertexHandle> face_vhandles;
26 
27  face_vhandles.push_back(vhandle[2]);
28  face_vhandles.push_back(vhandle[1]);
29  face_vhandles.push_back(vhandle[0]);
30  mesh_.add_face(face_vhandles);
31 
32  face_vhandles.clear();
33 
34  face_vhandles.push_back(vhandle[2]);
35  face_vhandles.push_back(vhandle[0]);
36  face_vhandles.push_back(vhandle[3]);
37  mesh_.add_face(face_vhandles);
38 
39  // Test setup:
40  // 1 === 2
41  // | / |
42  // | / |
43  // | / |
44  // 0 === 3
45  }
46 
47  // This function is called after all tests are through
48  virtual void TearDown() {
49 
50  // Do some final stuff with the member data here...
51  }
52 
53  // Member already defined in OpenMeshBase
54  //Mesh mesh_;
55 };
56 
57 class OpenMeshConvertPolyMeshToTriangle : public OpenMeshBasePoly {
58 
59  protected:
60 
61  // This function is called before each test is run
62  virtual void SetUp() {
63  mesh_.clear();
64 
65  // Add some vertices
66  Mesh::VertexHandle vhandle[4];
67 
68  vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
69  vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
70  vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
71  vhandle[3] = mesh_.add_vertex(Mesh::Point(1, 0, 0));
72 
73  // Add two faces
74  std::vector<Mesh::VertexHandle> face_vhandles;
75 
76  face_vhandles.push_back(vhandle[2]);
77  face_vhandles.push_back(vhandle[1]);
78  face_vhandles.push_back(vhandle[0]);
79  face_vhandles.push_back(vhandle[3]);
80  mesh_.add_face(face_vhandles);
81 
82  // Test setup:
83  // 1 --- 2
84  // | |
85  // | |
86  // | |
87  // 0 --- 3
88  }
89 
90  // This function is called after all tests are through
91  virtual void TearDown() {
92 
93  // Do some final stuff with the member data here...
94  }
95 
96  // Member already defined in OpenMeshBase
97  //Mesh mesh_;
98 };
99 
100 /*
101  * ====================================================================
102  * Define tests below
103  * ====================================================================
104  */
105 
106 /* Checks the converted mesh #faces and #vertices behaviour of adding
107  * vertices and faces to a trimesh after the conversion.
108  */
109 TEST_F(OpenMeshConvertTriangleMeshToPoly, VertexFaceCheck) {
110 
111  EXPECT_EQ(4u, mesh_.n_vertices() ) << "Wrong number of vertices in TriMesh";
112  EXPECT_EQ(2u, mesh_.n_faces() ) << "Wrong number of faces in TriMesh";
113 
114  //convert triMesh to PolyMesh
115  PolyMesh p = static_cast<PolyMesh>(mesh_);
116 
117  // Check setup
118  EXPECT_EQ(4u, p.n_vertices() ) << "Wrong number of vertices in PolyMesh";
119  EXPECT_EQ(2u, p.n_faces() ) << "Wrong number of faces in PolyMesh";
120 
121  //add vertex to original mesh
122  Mesh::VertexHandle vhand = mesh_.add_vertex(Mesh::Point(1, 1, 1));
123 
124  EXPECT_EQ(5u, mesh_.n_vertices() ) << "Wrong number of vertices in TriMesh";
125  EXPECT_EQ(4u, p.n_vertices() ) << "Wrong number of vertices in PolyMesh";
126 
127  Mesh::VertexIter it = mesh_.vertices_begin();
128  //add face to original mesh
129  mesh_.add_face(vhand,(*it),(*++it));
130 
131  EXPECT_EQ(3u, mesh_.n_faces() ) << "Wrong number of faces in TriMesh";
132  EXPECT_EQ(2u, p.n_faces() ) << "Wrong number of faces in PolyMesh";
133 }
134 
135 /* Creates a double property and checks if it works after conversion
136  */
137 TEST_F(OpenMeshConvertTriangleMeshToPoly, VertexPropertyCheckDouble) {
138 
139  // Add a double vertex property
140  OpenMesh::VPropHandleT<double> doubleHandle;
141 
142  EXPECT_FALSE( mesh_.get_property_handle(doubleHandle,"doubleProp") );
143 
144  mesh_.add_property(doubleHandle,"doubleProp");
145 
146  // Fill property
147  double index = 0.0;
148 
149  for ( Mesh::VertexIter v_it = mesh_.vertices_begin() ; v_it != mesh_.vertices_end(); ++v_it ) {
150  mesh_.property(doubleHandle,*v_it) = index;
151  index += 1.0;
152  }
153 
154  EXPECT_TRUE(mesh_.get_property_handle(doubleHandle,"doubleProp"));
155 
156  //convert triMesh to PolyMesh
157  PolyMesh p = static_cast<PolyMesh>(mesh_);
158 
159  EXPECT_TRUE(p.get_property_handle(doubleHandle,"doubleProp"));
160 
161  // Check if it is ok.
162  Mesh::VertexIter v_it = p.vertices_begin();
163  EXPECT_EQ( p.property(doubleHandle,*v_it) , 0.0 ) << "Invalid double value for vertex 0";
164  ++v_it;
165 
166  EXPECT_EQ( p.property(doubleHandle,*v_it) , 1.0 ) << "Invalid double value for vertex 1";
167  ++v_it;
168 
169  EXPECT_EQ( p.property(doubleHandle,*v_it) , 2.0 ) << "Invalid double value for vertex 2";
170  ++v_it;
171 
172  EXPECT_EQ( p.property(doubleHandle,*v_it) , 3.0 ) << "Invalid double value for vertex 3";
173 
174  //check if deletion in the original mesh affects the converted one.
175  mesh_.get_property_handle(doubleHandle,"doubleProp");
176  mesh_.remove_property(doubleHandle);
177  EXPECT_FALSE(mesh_.get_property_handle(doubleHandle,"doubleProp"));
178  EXPECT_TRUE(p.get_property_handle(doubleHandle,"doubleProp"));
179 
180 }
181 
182 /* Checks the converted mesh #faces and #vertices behaviour of adding
183  * vertices and faces to a trimesh after the conversion.
184  */
185 TEST_F(OpenMeshConvertPolyMeshToTriangle, VertexFaceCheck) {
186 
187  EXPECT_EQ(4u, mesh_.n_vertices() ) << "Wrong number of vertices in PolyMesh";
188  EXPECT_EQ(1u, mesh_.n_faces() ) << "Wrong number of faces in PolyMesh";
189 
190  //convert PolyMesh to TriMesh
191  Mesh p = static_cast<Mesh>(mesh_);
192 
193  // Check setup
194  EXPECT_EQ(4u, p.n_vertices() ) << "Wrong number of vertices in TriMesh";
195  EXPECT_EQ(2u, p.n_faces() ) << "Wrong number of faces in TriMesh";
196 
197  //add vertex to original mesh
198  Mesh::VertexHandle vhand = mesh_.add_vertex(Mesh::Point(1, 1, 1));
199 
200  EXPECT_EQ(5u, mesh_.n_vertices() ) << "Wrong number of vertices in PolyMesh";
201  EXPECT_EQ(4u, p.n_vertices() ) << "Wrong number of vertices in TriMesh";
202 
203  Mesh::VertexIter it = mesh_.vertices_begin();
204  //add face to original mesh
205  mesh_.add_face(vhand,(*it),(*++it));
206 
207  EXPECT_EQ(2u, mesh_.n_faces() ) << "Wrong number of faces in PolyMesh";
208  EXPECT_EQ(2u, p.n_faces() ) << "Wrong number of faces in TriMesh";
209 }
210 
211 /* Creates a double property and checks if it works after conversion
212  */
213 TEST_F(OpenMeshConvertPolyMeshToTriangle, VertexPropertyCheckDouble) {
214 
215  // Add a double vertex property
216  OpenMesh::VPropHandleT<double> doubleHandle;
217 
218  EXPECT_FALSE( mesh_.get_property_handle(doubleHandle,"doubleProp") );
219 
220  mesh_.add_property(doubleHandle,"doubleProp");
221 
222  // Fill property
223  double index = 0.0;
224 
225  for ( Mesh::VertexIter v_it = mesh_.vertices_begin() ; v_it != mesh_.vertices_end(); ++v_it ) {
226  mesh_.property(doubleHandle,*v_it) = index;
227  index += 1.0;
228  }
229 
230  EXPECT_TRUE(mesh_.get_property_handle(doubleHandle,"doubleProp"));
231 
232  //convert triMesh to PolyMesh
233  Mesh p = static_cast<Mesh>(mesh_);
234 
235  EXPECT_TRUE(p.get_property_handle(doubleHandle,"doubleProp"));
236 
237  // Check if it is ok.
238  Mesh::VertexIter v_it = p.vertices_begin();
239  EXPECT_EQ( p.property(doubleHandle,*v_it) , 0.0 ) << "Invalid double value for vertex 0";
240  ++v_it;
241 
242  EXPECT_EQ( p.property(doubleHandle,*v_it) , 1.0 ) << "Invalid double value for vertex 1";
243  ++v_it;
244 
245  EXPECT_EQ( p.property(doubleHandle,*v_it) , 2.0 ) << "Invalid double value for vertex 2";
246  ++v_it;
247 
248  EXPECT_EQ( p.property(doubleHandle,*v_it) , 3.0 ) << "Invalid double value for vertex 3";
249 
250  //check if deletion in the original mesh affects the converted one.
251  mesh_.get_property_handle(doubleHandle,"doubleProp");
252  mesh_.remove_property(doubleHandle);
253  EXPECT_FALSE(mesh_.get_property_handle(doubleHandle,"doubleProp"));
254  EXPECT_TRUE(p.get_property_handle(doubleHandle,"doubleProp"));
255 
256 }
257 
258 /* Creates a double property and checks if it works after conversion
259  * especially if edge properties are preserved after triangulation
260  */
261 TEST_F(OpenMeshConvertPolyMeshToTriangle, EdgePropertyCheckDouble) {
262 
263  // Add a double vertex property
264  OpenMesh::EPropHandleT<double> doubleHandle;
265 
266  EXPECT_FALSE( mesh_.get_property_handle(doubleHandle,"doubleProp") );
267 
268  mesh_.add_property(doubleHandle,"doubleProp");
269 
270  // Fill property
271  double index = 0.0;
272 
273  for ( Mesh::EdgeIter v_it = mesh_.edges_begin() ; v_it != mesh_.edges_end(); ++v_it ) {
274  mesh_.property(doubleHandle,*v_it) = index;
275  index += 1.0;
276  }
277 
278  EXPECT_TRUE(mesh_.get_property_handle(doubleHandle,"doubleProp"));
279 
280  //convert triMesh to PolyMesh
281  Mesh p = static_cast<Mesh>(mesh_);
282 
283  EXPECT_TRUE(p.get_property_handle(doubleHandle,"doubleProp"));
284 
285  // Check if it is ok.
286  Mesh::EdgeIter v_it = p.edges_begin();
287 
288  EXPECT_EQ( p.property(doubleHandle,*v_it) , 0.0 ) << "Invalid double value for vertex 0";
289  ++v_it;
290 
291  EXPECT_EQ( p.property(doubleHandle,*v_it) , 1.0 ) << "Invalid double value for vertex 1";
292  ++v_it;
293 
294  EXPECT_EQ( p.property(doubleHandle,*v_it) , 2.0 ) << "Invalid double value for vertex 2";
295  ++v_it;
296 
297  EXPECT_EQ( p.property(doubleHandle,*v_it) , 3.0 ) << "Invalid double value for vertex 3";
298  ++v_it;
299 
300  EXPECT_FALSE( p.is_boundary(*v_it)) << "Invalid Edge after triangulation";
301 
302  //check if deletion in the original mesh affects the converted one.
303  mesh_.get_property_handle(doubleHandle,"doubleProp");
304  mesh_.remove_property(doubleHandle);
305  EXPECT_FALSE(mesh_.get_property_handle(doubleHandle,"doubleProp"));
306  EXPECT_TRUE(p.get_property_handle(doubleHandle,"doubleProp"));
307 
308 }
309 
310 }
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
SmartVertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:235
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136