Developer Documentation

unittests_eigen3_type.cc
1
2 #ifdef ENABLE_EIGEN3_TEST
3
4 #include <gtest/gtest.h>
5 #include <Unittests/unittests_common.hh>
6 #include <iostream>
7
8 #include <OpenMesh/Core/Mesh/Traits.hh>
9 #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
10
11 #include <OpenMesh/Core/Geometry/EigenVectorT.hh>
12
13 struct EigenTraits : OpenMesh::DefaultTraits {
14 using Point = Eigen::Vector3d;
15 using Normal = Eigen::Vector3d;
16
17 using TexCoord2D = Eigen::Vector2d;
18 };
19
20 using EigenTriMesh = OpenMesh::TriMesh_ArrayKernelT<EigenTraits>;
21
22 namespace {
23
24
25 class OpenMeshEigenTest : public testing::Test {
26
27 protected:
28
29 // This function is called before each test is run
30 virtual void SetUp() {
31
32 // Do some initial stuff with the member data here...
33 }
34
35 // This function is called after all tests are through
36 virtual void TearDown() {
37
38
39 }
40
41 EigenTriMesh mesh_;
42 };
43
44 TEST_F(OpenMeshEigenTest, Test_external_vectorize) {
45
46
47 EigenTriMesh::Normal normal;
48
49 vectorize(normal,2);
50
51 EXPECT_EQ(normal[0],2.0f ) << "Wrong x value";
52 EXPECT_EQ(normal[1],2.0f ) << "Wrong y value";
53 EXPECT_EQ(normal[2],2.0f ) << "Wrong z value";
54
55
56 }
57
58 TEST_F(OpenMeshEigenTest, Test_external_norm) {
59
60
61 EigenTriMesh::Normal normal(1,0,0);
62
63 EigenTriMesh::Scalar result = norm(normal);
64
65 EXPECT_EQ(result,1.0f ) << "Wrong norm";
66
67 normal = EigenTriMesh::Normal(2,0,0);
68
69 result = norm(normal);
70
71 EXPECT_EQ(result,2.0f ) << "Wrong norm";
72 }
73
74 TEST_F(OpenMeshEigenTest, Test_external_sqrnorm) {
75
76
77 EigenTriMesh::Normal normal(1,0,0);
78
79 EigenTriMesh::Scalar result = sqrnorm(normal);
80
81 EXPECT_EQ(result,1.0f ) << "Wrong norm";
82
83 normal = EigenTriMesh::Normal(2,0,0);
84
85 result = sqrnorm(normal);
86
87 EXPECT_EQ(result,4.0f ) << "Wrong norm";
88 }
89
90 TEST_F(OpenMeshEigenTest, Test_external_normalize) {
91
92
93 EigenTriMesh::Normal normal(2,0,0);
94
95 normalize(normal);
96
97 EXPECT_EQ(norm(normal),1.0f ) << "Wrong norm after normalization";
98
99 normal = EigenTriMesh::Normal(2,6,9);
100
101 normalize(normal);
102
103 EXPECT_EQ(norm(normal),1.0f ) << "Wrong norm after normalization";
104
105 }
106
107 TEST_F(OpenMeshEigenTest, Test_external_cross_Product) {
108
109
110 EigenTriMesh::Normal normal1(1,0,0);
111 EigenTriMesh::Normal normal2(1,1,0);
112
113 EigenTriMesh::Normal result = cross(normal1,normal2);
114
115 EXPECT_EQ(result[0],0.0f ) << "Wrong result x direction";
116 EXPECT_EQ(result[1],0.0f ) << "Wrong result y direction";
117 EXPECT_EQ(result[2],1.0f ) << "Wrong result z direction";
118 }
119
120 TEST_F(OpenMeshEigenTest, Test_external_dot_Product) {
121
122
123 EigenTriMesh::Normal normal1(1,0,0);
124 EigenTriMesh::Normal normal2(1,1,0);
125 EigenTriMesh::Normal normal3(1,1,1);
126 EigenTriMesh::Normal normal4(2,4,6);
127
128 EigenTriMesh::Scalar result = dot(normal1,normal2);
129 EigenTriMesh::Scalar result1 = dot(normal3,normal4);
130
131 EXPECT_EQ(result,1.0f ) << "Wrong dot product";
132 EXPECT_EQ(result1,12.0f ) << "Wrong dot product";
133
134 }
135
136
137 TEST_F(OpenMeshEigenTest, Test_Basic_setup) {
138
139 // Add some vertices
140 EigenTriMesh::VertexHandle vhandle[4];
141
142 vhandle[0] = mesh_.add_vertex(EigenTriMesh::Point(0, 0, 0));
143 vhandle[1] = mesh_.add_vertex(EigenTriMesh::Point(0, 1, 0));
144 vhandle[2] = mesh_.add_vertex(EigenTriMesh::Point(1, 1, 0));
145 vhandle[3] = mesh_.add_vertex(EigenTriMesh::Point(1, 0, 0));
146
147 // Add two faces
148 std::vector<EigenTriMesh::VertexHandle> face_vhandles;
149
150 face_vhandles.push_back(vhandle[2]);
151 face_vhandles.push_back(vhandle[1]);
152 face_vhandles.push_back(vhandle[0]);
153
154 mesh_.add_face(face_vhandles);
155
156 face_vhandles.clear();
157
158 face_vhandles.push_back(vhandle[2]);
159 face_vhandles.push_back(vhandle[0]);
160 face_vhandles.push_back(vhandle[3]);
161 mesh_.add_face(face_vhandles);
162
163
164 EXPECT_EQ(mesh_.n_faces(),2u) << "Wrong number of faces";
165
166 }
167
168 TEST_F(OpenMeshEigenTest, test_normal_computation) {
169
170 // Add some vertices
171 EigenTriMesh::VertexHandle vhandle[4];
172
173 mesh_.request_vertex_normals();
174 mesh_.request_face_normals();
175
176 vhandle[0] = mesh_.add_vertex(EigenTriMesh::Point(0, 0, 0));
177 vhandle[1] = mesh_.add_vertex(EigenTriMesh::Point(0, 1, 0));
178 vhandle[2] = mesh_.add_vertex(EigenTriMesh::Point(1, 1, 0));
179 vhandle[3] = mesh_.add_vertex(EigenTriMesh::Point(1, 0, 0));
180
181 // Add two faces
182 std::vector<EigenTriMesh::VertexHandle> face_vhandles;
183
184 face_vhandles.push_back(vhandle[2]);
185 face_vhandles.push_back(vhandle[1]);
186 face_vhandles.push_back(vhandle[0]);
187
188 EigenTriMesh::FaceHandle face1 = mesh_.add_face(face_vhandles);
189
190 face_vhandles.clear();
191
192 face_vhandles.push_back(vhandle[2]);
193 face_vhandles.push_back(vhandle[0]);
194 face_vhandles.push_back(vhandle[3]);
195 EigenTriMesh::FaceHandle face2 = mesh_.add_face(face_vhandles);
196
197 mesh_.update_face_normals();
198
199
200 EXPECT_EQ(mesh_.n_faces(),2) << "Wrong number of faces";
201
202 EigenTriMesh::Normal normal = mesh_.normal(face1);
203
204 EXPECT_EQ(normal[0],0.0f ) << "Wrong normal x direction";
205 EXPECT_EQ(normal[1],0.0f ) << "Wrong normal y direction";
206 EXPECT_EQ(normal[2],1.0f ) << "Wrong normal z direction";
207
208 normal = mesh_.normal(face2);
209
210 EXPECT_EQ(normal[0],0.0f ) << "Wrong normal x direction";
211 EXPECT_EQ(normal[1],0.0f ) << "Wrong normal y direction";
212 EXPECT_EQ(normal[2],1.0f ) << "Wrong normal z direction";
213
214 }
215
216 /* Just load a simple mesh file in obj format and count whether
217 * the right number of entities has been loaded. Afterwards recompute
218 * normals
219 */
220 TEST_F(OpenMeshEigenTest, LoadSimpleOFFFile) {
221
222 mesh_.clear();
223
224 bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.off");
225
226 EXPECT_TRUE(ok);
227
228 EXPECT_EQ(7526u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
229 EXPECT_EQ(22572u, mesh_.n_edges()) << "The number of loaded edges is not correct!";
230 EXPECT_EQ(15048u, mesh_.n_faces()) << "The number of loaded faces is not correct!";
231
232 mesh_.update_normals();
233 }
234
235 }
236
237 #endif
OpenMesh::TriMesh_ArrayKernelT
Definition: PolyMesh_ArrayKernelT.hh:73
OpenMesh::PolyMeshT::add_vertex
VertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:235
OpenMesh::DefaultTraits
Definition: Traits.hh:121
Traits.hh
OpenMesh::Attributes::TexCoord2D
Add 2D texture coordinates (vertices, halfedges)
Definition: Attributes.hh:87
OpenMesh::PolyMeshT::update_face_normals
void update_face_normals()
Update normal vectors for all faces.
Definition: PolyMeshT_impl.hh:277
OpenMesh::IO::read_mesh
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition: MeshIO.hh:112
OpenMesh::PolyMeshT::update_normals
void update_normals()
Compute normals for all primitives.
Definition: PolyMeshT_impl.hh:259