Developer Documentation

unittests_normal_calculations.cc
1 #include <gtest/gtest.h>
2 #include <Unittests/unittests_common.hh>
3
4 #include <iostream>
5
6 namespace {
7
8 class OpenMeshNormals : public OpenMeshBase {
9
10 protected:
11
12 // This function is called before each test is run
13 virtual void SetUp() {
14 }
15
16 // This function is called after all tests are through
17 virtual void TearDown() {
18
19 // Do some final stuff with the member data here...
20 }
21
22 // Member already defined in OpenMeshBase
23 //Mesh mesh_;
24 };
25
26 class OpenMeshNormalsPolyMesh : public OpenMeshBasePoly {
27
28 protected:
29
30 // This function is called before each test is run
31 virtual void SetUp() {
32
33 // Do some initial stuff with the member data here...
34 }
35
36 // This function is called after all tests are through
37 virtual void TearDown() {
38
39 // Do some final stuff with the member data here...
40 }
41
42 // Member already defined in OpenMeshBase
43 //Mesh mesh_;
44 };
45
46
47
48 /*
49 * ====================================================================
50 * Define tests below
51 * ====================================================================
52 */
53
54 /*
55 * Update normals on a single triangle
56 */
57 TEST_F(OpenMeshNormals, NormalCalculationSingleFaceTriMesh) {
58
59 mesh_.clear();
60
61 // Add some vertices
62 Mesh::VertexHandle vhandle[4];
63
64 vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
65 vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
66 vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 0, 0));
67
68 std::vector<Mesh::VertexHandle> face_vhandles;
69 face_vhandles.push_back(vhandle[0]);
70 face_vhandles.push_back(vhandle[1]);
71 face_vhandles.push_back(vhandle[2]);
72
73 Mesh::FaceHandle fh = mesh_.add_face(face_vhandles);
74
75
76 mesh_.request_vertex_normals();
77 mesh_.request_halfedge_normals();
78 mesh_.request_face_normals();
79
80 mesh_.update_normals();
81
82 EXPECT_EQ( mesh_.normal(fh)[0] ,0.0f );
83 EXPECT_EQ( mesh_.normal(fh)[1] ,0.0f );
84 EXPECT_EQ( mesh_.normal(fh)[2] ,1.0f );
85 }
86
87
88 /*
89 * Update normals on a single triangle
90 */
91 TEST_F(OpenMeshNormalsPolyMesh, NormalCalculationSingleFacePolyMesh) {
92
93 mesh_.clear();
94
95 // Add some vertices
96 PolyMesh::VertexHandle vhandle[4];
97
98 vhandle[0] = mesh_.add_vertex(PolyMesh::Point(0, 1, 0));
99 vhandle[1] = mesh_.add_vertex(PolyMesh::Point(0, 0, 0));
100 vhandle[2] = mesh_.add_vertex(PolyMesh::Point(1, 0, 0));
101
102 std::vector<Mesh::VertexHandle> face_vhandles;
103 face_vhandles.push_back(vhandle[0]);
104 face_vhandles.push_back(vhandle[1]);
105 face_vhandles.push_back(vhandle[2]);
106
107 PolyMesh::FaceHandle fh = mesh_.add_face(face_vhandles);
108
109
110 mesh_.request_vertex_normals();
111 mesh_.request_halfedge_normals();
112 mesh_.request_face_normals();
113
114 mesh_.update_normals();
115
116 EXPECT_EQ( mesh_.normal(fh)[0] ,0.0f );
117 EXPECT_EQ( mesh_.normal(fh)[1] ,0.0f );
118 EXPECT_EQ( mesh_.normal(fh)[2] ,1.0f );
119
120 }
121
122 /*
123 * Collapsing a tetrahedron
124 */
125 TEST_F(OpenMeshNormals, NormalCalculations) {
126
127 mesh_.clear();
128
129 // Add some vertices
130 Mesh::VertexHandle vhandle[4];
131
132 vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
133 vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
134 vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
135 vhandle[3] = mesh_.add_vertex(Mesh::Point(0, 0, 1));
136
137 // Add four faces
138 std::vector<Mesh::VertexHandle> face_vhandles;
139
140 face_vhandles.push_back(vhandle[0]);
141 face_vhandles.push_back(vhandle[1]);
142 face_vhandles.push_back(vhandle[2]);
143 mesh_.add_face(face_vhandles);
144
145 face_vhandles.clear();
146
147 face_vhandles.push_back(vhandle[0]);
148 face_vhandles.push_back(vhandle[2]);
149 face_vhandles.push_back(vhandle[3]);
150 mesh_.add_face(face_vhandles);
151
152 face_vhandles.clear();
153
154 face_vhandles.push_back(vhandle[2]);
155 face_vhandles.push_back(vhandle[1]);
156 face_vhandles.push_back(vhandle[3]);
157 mesh_.add_face(face_vhandles);
158
159 face_vhandles.clear();
160
161 face_vhandles.push_back(vhandle[3]);
162 face_vhandles.push_back(vhandle[1]);
163 face_vhandles.push_back(vhandle[0]);
164 mesh_.add_face(face_vhandles);
165
166 // ===============================================
167 // Setup complete
168 // ===============================================
169
170
171 // Check one Request only vertex normals
172 // Face normals are required for vertex and halfedge normals, so
173 // that prevent access to non existing properties are in place
174
175 mesh_.request_vertex_normals();
176 mesh_.request_halfedge_normals();
177
178 // Check blocks
179 mesh_.update_normals();
180
181 // Request required face normals
182 mesh_.request_face_normals();
183
184 // Automatically compute all normals
185 // As only vertex normals are requested and no face normals, this will compute nothing.
186 mesh_.update_normals();
187
188 // Face normals alone
189 mesh_.update_face_normals();
190
191 // Vertex normals alone (require valid face normals)
192 mesh_.update_vertex_normals();
193
194 // Halfedge normals alone (require valid face normals)
195 mesh_.update_halfedge_normals();
196
197 }
198
199 TEST_F(OpenMeshNormals, NormalCalculations_calc_vertex_normal_fast) {
200
201 mesh_.clear();
202
203 // Add some vertices
204 Mesh::VertexHandle vhandle[4];
205
206 vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
207 vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
208 vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
209 vhandle[3] = mesh_.add_vertex(Mesh::Point(0, 0, 1));
210
211 // Add four faces
212 std::vector<Mesh::VertexHandle> face_vhandles;
213
214 face_vhandles.push_back(vhandle[0]);
215 face_vhandles.push_back(vhandle[1]);
216 face_vhandles.push_back(vhandle[2]);
217 mesh_.add_face(face_vhandles);
218
219 face_vhandles.clear();
220
221 face_vhandles.push_back(vhandle[0]);
222 face_vhandles.push_back(vhandle[2]);
223 face_vhandles.push_back(vhandle[3]);
224 mesh_.add_face(face_vhandles);
225
226 face_vhandles.clear();
227
228 face_vhandles.push_back(vhandle[2]);
229 face_vhandles.push_back(vhandle[1]);
230 face_vhandles.push_back(vhandle[3]);
231 mesh_.add_face(face_vhandles);
232
233 face_vhandles.clear();
234
235 face_vhandles.push_back(vhandle[3]);
236 face_vhandles.push_back(vhandle[1]);
237 face_vhandles.push_back(vhandle[0]);
238 mesh_.add_face(face_vhandles);
239
240 // ===============================================
241 // Setup complete
242 // ===============================================
243
244
245 mesh_.request_vertex_normals();
246 mesh_.request_halfedge_normals();
247 mesh_.request_face_normals();
248
249
250 Mesh::Normal normal;
251
252 mesh_.calc_vertex_normal_fast(vhandle[2],normal);
253
254 }
255
256 TEST_F(OpenMeshNormals, NormalCalculations_calc_vertex_normal_correct) {
257
258 mesh_.clear();
259
260 // Add some vertices
261 Mesh::VertexHandle vhandle[4];
262
263 vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
264 vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
265 vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
266 vhandle[3] = mesh_.add_vertex(Mesh::Point(0, 0, 1));
267
268 // Add four faces
269 std::vector<Mesh::VertexHandle> face_vhandles;
270
271 face_vhandles.push_back(vhandle[0]);
272 face_vhandles.push_back(vhandle[1]);
273 face_vhandles.push_back(vhandle[2]);
274 mesh_.add_face(face_vhandles);
275
276 face_vhandles.clear();
277
278 face_vhandles.push_back(vhandle[0]);
279 face_vhandles.push_back(vhandle[2]);
280 face_vhandles.push_back(vhandle[3]);
281 mesh_.add_face(face_vhandles);
282
283 face_vhandles.clear();
284
285 face_vhandles.push_back(vhandle[2]);
286 face_vhandles.push_back(vhandle[1]);
287 face_vhandles.push_back(vhandle[3]);
288 mesh_.add_face(face_vhandles);
289
290 face_vhandles.clear();
291
292 face_vhandles.push_back(vhandle[3]);
293 face_vhandles.push_back(vhandle[1]);
294 face_vhandles.push_back(vhandle[0]);
295 mesh_.add_face(face_vhandles);
296
297
298 // ===============================================
299 // Setup complete
300 // ===============================================
301
302
303 mesh_.request_vertex_normals();
304 mesh_.request_halfedge_normals();
305 mesh_.request_face_normals();
306
307 Mesh::Normal normal;
308
309
310 mesh_.calc_vertex_normal_correct(vhandle[2],normal);
311
312 }
313
314 TEST_F(OpenMeshNormals, NormalCalculations_calc_vertex_normal_loop) {
315
316 mesh_.clear();
317
318 // Add some vertices
319 Mesh::VertexHandle vhandle[4];
320
321 vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
322 vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
323 vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
324 vhandle[3] = mesh_.add_vertex(Mesh::Point(0, 0, 1));
325
326 // Add four faces
327 std::vector<Mesh::VertexHandle> face_vhandles;
328
329 face_vhandles.push_back(vhandle[0]);
330 face_vhandles.push_back(vhandle[1]);
331 face_vhandles.push_back(vhandle[2]);
332 mesh_.add_face(face_vhandles);
333
334 face_vhandles.clear();
335
336 face_vhandles.push_back(vhandle[0]);
337 face_vhandles.push_back(vhandle[2]);
338 face_vhandles.push_back(vhandle[3]);
339 mesh_.add_face(face_vhandles);
340
341 face_vhandles.clear();
342
343 face_vhandles.push_back(vhandle[2]);
344 face_vhandles.push_back(vhandle[1]);
345 face_vhandles.push_back(vhandle[3]);
346 mesh_.add_face(face_vhandles);
347
348 face_vhandles.clear();
349
350 face_vhandles.push_back(vhandle[3]);
351 face_vhandles.push_back(vhandle[1]);
352 face_vhandles.push_back(vhandle[0]);
353 mesh_.add_face(face_vhandles);
354
355 // ===============================================
356 // Setup complete
357 // ===============================================
358
359
360 mesh_.request_vertex_normals();
361 mesh_.request_halfedge_normals();
362 mesh_.request_face_normals();
363
364 Mesh::Normal normal;
365
366 mesh_.calc_vertex_normal_loop(vhandle[2],normal);
367
368 }
369
370
371
372
373
374
375
376
377
378 }
OpenMesh::PolyMeshT::calc_vertex_normal_loop
void calc_vertex_normal_loop(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:443
OpenMesh::PolyMeshT::Point
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
OpenMesh::PolyMeshT::add_vertex
VertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:235
OpenMesh::PolyMeshT::VertexHandle
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
OpenMesh::PolyMeshT::update_vertex_normals
void update_vertex_normals()
Update normal vectors for all vertices.
Definition: PolyMeshT_impl.hh:466
OpenMeshBase
Definition: unittests_common.hh:26
OpenMesh::PolyMeshT::calc_vertex_normal_correct
void calc_vertex_normal_correct(VertexHandle _vh, Normal &_n) const
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:415
OpenMesh::PolyMeshT::Normal
Kernel::Normal Normal
Normal type.
Definition: PolyMeshT.hh:114
OpenMesh::PolyMeshT::update_halfedge_normals
void update_halfedge_normals(const double _feature_angle=0.8)
Update normal vectors for all halfedges.
Definition: PolyMeshT_impl.hh:292
OpenMesh::PolyMeshT::update_face_normals
void update_face_normals()
Update normal vectors for all faces.
Definition: PolyMeshT_impl.hh:277
OpenMeshBasePoly
Definition: unittests_common.hh:50
OpenMesh::PolyMeshT::update_normals
void update_normals()
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:259
OpenMesh::PolyMeshT::calc_vertex_normal_fast
void calc_vertex_normal_fast(VertexHandle _vh, Normal &_n) const
Definition: PolyMeshT_impl.hh:405