1 
2 #include <gtest/gtest.h>
3 #include <Unittests/unittests_common.hh>
4 
5 
6 namespace {
7 
8 class OpenMeshReadWriteSTL : public OpenMeshBase {
9 
10     protected:
11 
12         // This function is called before each test is run
SetUp()13         virtual void SetUp() {
14 
15             // Do some initial stuff with the member data here...
16         }
17 
18         // This function is called after all tests are through
TearDown()19         virtual void TearDown() {
20 
21             // Do some final stuff with the member data here...
22         }
23 
24     // Member already defined in OpenMeshBase
25     //Mesh mesh_;
26 };
27 
28 /*
29  * ====================================================================
30  * Define tests below
31  * ====================================================================
32  */
33 
34 /*
35  * Just load a simple mesh file in stla format and count whether
36  * the right number of entities has been loaded.
37  */
TEST_F(OpenMeshReadWriteSTL,LoadSimpleSTLFile)38 TEST_F(OpenMeshReadWriteSTL, LoadSimpleSTLFile) {
39 
40     mesh_.clear();
41 
42     bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.stl");
43 
44     EXPECT_TRUE(ok);
45 
46     EXPECT_EQ(7526u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
47     EXPECT_EQ(22572u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
48     EXPECT_EQ(15048u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
49 }
50 
51 
52 /*
53  * Just load a simple mesh file in stla format and count whether
54  * the right number of entities has been loaded. Also check facet normals.
55  */
TEST_F(OpenMeshReadWriteSTL,LoadSimpleSTLFileWithNormals)56 TEST_F(OpenMeshReadWriteSTL, LoadSimpleSTLFileWithNormals) {
57 
58     mesh_.clear();
59     mesh_.request_face_normals();
60 
61     OpenMesh::IO::Options opt;
62     opt += OpenMesh::IO::Options::FaceNormal;
63 
64     bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1.stl", opt);
65 
66     EXPECT_TRUE(ok);
67 
68     EXPECT_TRUE(opt.face_has_normal());
69     EXPECT_FALSE(opt.vertex_has_normal());
70 
71     EXPECT_NEAR(-0.038545f, mesh_.normal(mesh_.face_handle(0))[0], 0.0001 ) << "Wrong face normal at face 0 component 0";
72     EXPECT_NEAR(-0.004330f, mesh_.normal(mesh_.face_handle(0))[1], 0.0001 ) << "Wrong face normal at face 0 component 1";
73     EXPECT_NEAR(0.999247f, mesh_.normal(mesh_.face_handle(0))[2], 0.0001 ) << "Wrong face normal at face 0 component 2";
74 
75     EXPECT_EQ(7526u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
76     EXPECT_EQ(22572u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
77     EXPECT_EQ(15048u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
78 
79     mesh_.release_face_normals();
80 }
81 
82 
83 /*
84  * Just load a simple mesh file in stlb format and count whether
85  * the right number of entities has been loaded.
86  */
TEST_F(OpenMeshReadWriteSTL,LoadSimpleSTLBinaryFile)87 TEST_F(OpenMeshReadWriteSTL, LoadSimpleSTLBinaryFile) {
88 
89     mesh_.clear();
90 
91     bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1Binary.stl");
92 
93     EXPECT_TRUE(ok);
94 
95     EXPECT_EQ(7526u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
96     EXPECT_EQ(22572u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
97     EXPECT_EQ(15048u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
98 }
99 
100 
101 /*
102  * Just load a simple mesh file in stlb format and count whether
103  * the right number of entities has been loaded. Also check facet normals.
104  */
TEST_F(OpenMeshReadWriteSTL,LoadSimpleSTLBinaryFileWithNormals)105 TEST_F(OpenMeshReadWriteSTL, LoadSimpleSTLBinaryFileWithNormals) {
106 
107     mesh_.clear();
108     mesh_.request_face_normals();
109 
110     OpenMesh::IO::Options opt;
111     opt += OpenMesh::IO::Options::FaceNormal;
112     opt += OpenMesh::IO::Options::Binary;
113 
114     bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1Binary.stl", opt);
115 
116     EXPECT_TRUE(ok);
117 
118     EXPECT_TRUE(opt.is_binary());
119     EXPECT_TRUE(opt.face_has_normal());
120     EXPECT_FALSE(opt.vertex_has_normal());
121 
122     EXPECT_NEAR(-0.038545f, mesh_.normal(mesh_.face_handle(0))[0], 0.0001 ) << "Wrong face normal at face 0 component 0";
123     EXPECT_NEAR(-0.004330f, mesh_.normal(mesh_.face_handle(0))[1], 0.0001 ) << "Wrong face normal at face 0 component 1";
124     EXPECT_NEAR(0.999247f, mesh_.normal(mesh_.face_handle(0))[2], 0.0001 ) << "Wrong face normal at face 0 component 2";
125 
126     EXPECT_EQ(7526u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
127     EXPECT_EQ(22572u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
128     EXPECT_EQ(15048u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
129 
130     mesh_.release_face_normals();
131 }
132 
133 /*
134  * Read and Write stl binary file
135  */
TEST_F(OpenMeshReadWriteSTL,ReadWriteSimpleSTLBinaryFile)136 TEST_F(OpenMeshReadWriteSTL, ReadWriteSimpleSTLBinaryFile) {
137 
138     mesh_.clear();
139 
140     OpenMesh::IO::Options opt;
141     opt += OpenMesh::IO::Options::Binary;
142 
143     bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1Binary.stl");
144 
145     EXPECT_TRUE(ok);
146 
147     const char* filename = "cube1Binary_openmeshWriteTestFile.stl";
148 
149     ok = OpenMesh::IO::write_mesh(mesh_, filename, opt);
150 
151     EXPECT_TRUE(ok);
152 
153     ok = OpenMesh::IO::read_mesh(mesh_, filename, opt);
154 
155     EXPECT_TRUE(ok);
156 
157     EXPECT_EQ(7526u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
158     EXPECT_EQ(22572u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
159     EXPECT_EQ(15048u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
160 
161     remove(filename);
162 }
163 
164 /*
165  * Just load a simple mesh file in stlb format rewrite and load it again and count whether
166  * the right number of entities has been loaded. Also check facet normals.
167  */
TEST_F(OpenMeshReadWriteSTL,ReadWriteSimpleSTLBinaryFileWithNormals)168 TEST_F(OpenMeshReadWriteSTL, ReadWriteSimpleSTLBinaryFileWithNormals) {
169 
170     mesh_.clear();
171     mesh_.request_face_normals();
172 
173     OpenMesh::IO::Options opt;
174     opt += OpenMesh::IO::Options::FaceNormal;
175     opt += OpenMesh::IO::Options::Binary;
176 
177     bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1Binary.stl", opt);
178 
179     EXPECT_TRUE(ok);
180 
181     const char* filename = "cube1BinaryNormal_openmeshWriteTestFile.stl";
182 
183     ok = OpenMesh::IO::write_mesh(mesh_, filename, opt);
184 
185     EXPECT_TRUE(ok);
186 
187     ok = OpenMesh::IO::read_mesh(mesh_, filename, opt);
188 
189     EXPECT_TRUE(ok);
190 
191     EXPECT_TRUE(opt.is_binary());
192     EXPECT_TRUE(opt.face_has_normal());
193     EXPECT_FALSE(opt.vertex_has_normal());
194 
195     EXPECT_NEAR(-0.038545f, mesh_.normal(mesh_.face_handle(0))[0], 0.0001 ) << "Wrong face normal at face 0 component 0";
196     EXPECT_NEAR(-0.004330f, mesh_.normal(mesh_.face_handle(0))[1], 0.0001 ) << "Wrong face normal at face 0 component 1";
197     EXPECT_NEAR(0.999247f, mesh_.normal(mesh_.face_handle(0))[2], 0.0001 ) << "Wrong face normal at face 0 component 2";
198 
199     EXPECT_EQ(7526u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
200     EXPECT_EQ(22572u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
201     EXPECT_EQ(15048u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
202 
203     mesh_.release_face_normals();
204     remove(filename);
205 }
206 
207 /*
208  * Just load a simple mesh file in stlb format rewrite and load it again and count whether
209  * the right number of entities has been loaded. Also check facet normals.
210  */
TEST_F(OpenMeshReadWriteSTL,ReadWriteSimpleSTLAsciiFileWithNormals)211 TEST_F(OpenMeshReadWriteSTL, ReadWriteSimpleSTLAsciiFileWithNormals) {
212 
213     mesh_.clear();
214     mesh_.request_face_normals();
215 
216     OpenMesh::IO::Options opt;
217     opt += OpenMesh::IO::Options::FaceNormal;
218 
219     bool ok = OpenMesh::IO::read_mesh(mesh_, "cube1Binary.stl", opt);
220 
221     EXPECT_TRUE(ok);
222     opt.clear();
223     opt += OpenMesh::IO::Options::FaceNormal;
224     const char* filename = "cube1Normal_openmeshWriteTestFile.stl";
225 
226     ok = OpenMesh::IO::write_mesh(mesh_, filename, opt);
227 
228     EXPECT_TRUE(ok);
229 
230     opt.clear();
231     opt += OpenMesh::IO::Options::FaceNormal;
232     ok = OpenMesh::IO::read_mesh(mesh_, filename, opt);
233 
234     EXPECT_TRUE(ok);
235 
236     EXPECT_FALSE(opt.is_binary());
237     EXPECT_TRUE(opt.face_has_normal());
238     EXPECT_FALSE(opt.vertex_has_normal());
239 
240     EXPECT_NEAR(-0.038545f, mesh_.normal(mesh_.face_handle(0))[0], 0.0001 ) << "Wrong face normal at face 0 component 0";
241     EXPECT_NEAR(-0.004330f, mesh_.normal(mesh_.face_handle(0))[1], 0.0001 ) << "Wrong face normal at face 0 component 1";
242     EXPECT_NEAR(0.999247f, mesh_.normal(mesh_.face_handle(0))[2], 0.0001 ) << "Wrong face normal at face 0 component 2";
243 
244     EXPECT_EQ(7526u  , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
245     EXPECT_EQ(22572u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
246     EXPECT_EQ(15048u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
247 
248     mesh_.release_face_normals();
249     remove(filename);
250 }
251 
252 
253 }
254