1#include "unittests_common.hh"
2#include <gtest/gtest-typed-test.h>
4#include <OpenVolumeMesh/FileManager/FileManager.hh>
5#include <OpenVolumeMesh/IO/ovmb_read.hh>
6#include <OpenVolumeMesh/IO/ovmb_write.hh>
8using namespace OpenVolumeMesh;
10using namespace Geometry;
15 template<
typename MeshT>
16 bool readFile(
const char *filename,
18 bool _topo_check =
true,
19 bool _bottom_up =
true)
const
21 if constexpr(Binary) {
22 std::string ovmb_filename = filename;
25 options.topology_check = _topo_check;
26 options.bottom_up_incidences = _bottom_up;
27 auto result = OpenVolumeMesh::IO::ovmb_read(ovmb_filename.c_str(), mesh, options);
28 return result == OpenVolumeMesh::IO::ReadResult::Ok;
31 return fileManager.
readFile(filename, mesh, _topo_check, _bottom_up);
34 template<
typename MeshT>
35 bool writeFile(
const char *filename,
36 MeshT const &mesh)
const
38 if constexpr(Binary) {
39 std::string ovmb_filename = filename;
41 auto result = OpenVolumeMesh::IO::ovmb_write(ovmb_filename.c_str(), mesh);
42 return result == OpenVolumeMesh::IO::WriteResult::Ok;
45 return fileManager.
writeFile(filename, mesh);
48 bool isHexahedralMesh(
const char *filename)
const
50 if constexpr(Binary) {
51 std::string ovmb_filename = filename;
53 std::ifstream stream(ovmb_filename, std::ios::binary);
54 auto result = OpenVolumeMesh::IO::make_ovmb_reader(
57 OpenVolumeMesh::IO::g_default_property_codecs);
58 auto topo_type = result->topo_type();
59 return topo_type.has_value() &&
60 topo_type.value() == OpenVolumeMesh::IO::TopoType::Hexahedral;
69template<
typename use_binary>
74 using LoadSave<use_binary::value>::readFile;
75 using LoadSave<use_binary::value>::writeFile;
76 using LoadSave<use_binary::value>::isHexahedralMesh;
79template<
typename use_binary>
84 using LoadSave<use_binary::value>::readFile;
85 using LoadSave<use_binary::value>::writeFile;
86 using LoadSave<use_binary::value>::isHexahedralMesh;
89using TrueFalse = testing::Types<std::true_type, std::false_type>;
97 ASSERT_TRUE(this->readFile(
"Cylinder.ovm", mesh));
100 EXPECT_EQ(399u, mesh.n_vertices());
101 EXPECT_EQ(1070u, mesh.n_edges());
102 EXPECT_EQ(960u, mesh.n_faces());
103 EXPECT_EQ(288u, mesh.n_cells());
104 auto v0 = mesh.vertex(
VH(0));
105 EXPECT_DOUBLE_EQ(v0[0], 4.97203);
106 EXPECT_DOUBLE_EQ(v0[1], 0.504645);
107 EXPECT_DOUBLE_EQ(v0[2], 0.913112);
114 ASSERT_TRUE(this->readFile(
"NonManifold.ovm", mesh));
116 EXPECT_EQ(12u, mesh.n_vertices());
117 EXPECT_EQ(20u, mesh.n_edges());
118 EXPECT_EQ(11u, mesh.n_faces());
119 EXPECT_EQ(2u, mesh.n_cells());
126 ASSERT_TRUE(this->isHexahedralMesh(
"Cylinder.ovm"));
127 ASSERT_TRUE(this->readFile(
"Cylinder.ovm", mesh));
129 EXPECT_EQ(399u, mesh.n_vertices());
130 EXPECT_EQ(1070u, mesh.n_edges());
131 EXPECT_EQ(960u, mesh.n_faces());
132 EXPECT_EQ(288u, mesh.n_cells());
139 ASSERT_TRUE(this->readFile(
"Cylinder.ovm", mesh));
141 EXPECT_EQ(399u, mesh.n_vertices());
142 EXPECT_EQ(1070u, mesh.n_edges());
143 EXPECT_EQ(960u, mesh.n_faces());
144 EXPECT_EQ(288u, mesh.n_cells());
147 ASSERT_TRUE(this->writeFile(
"Cylinder.copy.ovm", mesh));
151 ASSERT_TRUE(this->readFile(
"Cylinder.copy.ovm", mesh));
153 EXPECT_EQ(399u, mesh.n_vertices());
154 EXPECT_EQ(1070u, mesh.n_edges());
155 EXPECT_EQ(960u, mesh.n_faces());
156 EXPECT_EQ(288u, mesh.n_cells());
157 auto v0 = mesh.vertex(
VH(0));
158 EXPECT_DOUBLE_EQ(v0[0], 4.97203);
159 EXPECT_DOUBLE_EQ(v0[1], 0.504645);
160 EXPECT_DOUBLE_EQ(v0[2], 0.913112);
167 ASSERT_TRUE(this->readFile(
"Cylinder.ovm", mesh));
169 EXPECT_EQ(399u, mesh.n_vertices());
170 EXPECT_EQ(1070u, mesh.n_edges());
171 EXPECT_EQ(960u, mesh.n_faces());
172 EXPECT_EQ(288u, mesh.n_cells());
178 for(
unsigned int i = 0; i < mesh.n_halffaces(); ++i) {
181 for(
unsigned int i = 0; i < mesh.n_vertices(); ++i) {
182 vprop[VertexHandle(i)] = i;
185 mesh.set_persistent(hfprop);
186 mesh.set_persistent(vprop);
189 ASSERT_TRUE(this->writeFile(
"Cylinder.copy.ovm", mesh));
196 ASSERT_TRUE(this->readFile(
"Cylinder.copy.ovm", mesh));
198 EXPECT_EQ(399u, mesh.n_vertices());
199 EXPECT_EQ(1070u, mesh.n_edges());
200 EXPECT_EQ(960u, mesh.n_faces());
201 EXPECT_EQ(288u, mesh.n_cells());
209 for(
unsigned int i = 0; i < mesh.n_halffaces(); ++i) {
212 for(
unsigned int i = 0; i < mesh.n_vertices(); ++i) {
213 EXPECT_EQ(i, vprop2[VertexHandle(i)]);
221 ASSERT_TRUE(this->readFile(
"Cylinder.ovm", mesh));
223 EXPECT_EQ(399u, mesh.n_vertices());
224 EXPECT_EQ(1070u, mesh.n_edges());
225 EXPECT_EQ(960u, mesh.n_faces());
226 EXPECT_EQ(288u, mesh.n_cells());
232 for(
unsigned int i = 0; i < mesh.n_halffaces(); ++i) {
235 for(
unsigned int i = 0; i < mesh.n_vertices(); ++i) {
236 vprop[VertexHandle(i)] =
Vec2i(i, i);
239 mesh.set_persistent(hfprop);
240 mesh.set_persistent(vprop);
243 ASSERT_TRUE(this->writeFile(
"Cylinder.copy.ovm", mesh));
249 ASSERT_TRUE(this->readFile(
"Cylinder.copy.ovm", mesh,
true,
false));
251 EXPECT_EQ(399u, mesh.n_vertices());
252 EXPECT_EQ(1070u, mesh.n_edges());
253 EXPECT_EQ(960u, mesh.n_faces());
254 EXPECT_EQ(288u, mesh.n_cells());
262 for(
unsigned int i = 0; i < mesh.n_halffaces(); ++i) {
264 EXPECT_DOUBLE_EQ((
double)i/2.0, hfprop2[hfh][0]);
265 EXPECT_DOUBLE_EQ((
double)i/2.0, hfprop2[hfh][1]);
266 EXPECT_DOUBLE_EQ((
double)i/2.0, hfprop2[hfh][2]);
268 for(
unsigned int i = 0; i < mesh.n_vertices(); ++i) {
270 EXPECT_EQ((
int)i, vprop2[vh][0]);
271 EXPECT_EQ((
int)i, vprop2[vh][1]);
279 ASSERT_TRUE(this->readFile(
"Cylinder.ovm", mesh));
281 EXPECT_EQ(399u, mesh.n_vertices());
282 EXPECT_EQ(1070u, mesh.n_edges());
283 EXPECT_EQ(960u, mesh.n_faces());
284 EXPECT_EQ(288u, mesh.n_cells());
290 for(
unsigned int i = 0; i < mesh.n_halffaces(); ++i) {
292 hfprop[hfh] =
Vec3d((
double)i/2.0, (
double)i/2.0, (
double)i/2.0);
294 for(
unsigned int i = 0; i < mesh.n_vertices(); ++i) {
296 vprop[vh] =
Vec2i(i, i);
299 mesh.set_persistent(hfprop);
300 mesh.set_persistent(vprop);
302 std::ofstream ofs1(
"hfVecPropTest");
303 std::ofstream ofs2(
"vVecPropTest");
305 hfprop.serialize(ofs1);
306 vprop.serialize(ofs2);
314 for(
unsigned int i = 0; i < mesh.n_halffaces(); ++i) {
318 for(
unsigned int i = 0; i < mesh.n_vertices(); ++i) {
319 vprop[VertexHandle(i)] =
Vec2i(2*i, 2*i);
322 std::ifstream ifs1(
"hfVecPropTest");
323 std::ifstream ifs2(
"vVecPropTest");
325 hfprop.deserialize(ifs1);
326 vprop.deserialize(ifs2);
331 for(
unsigned int i = 0; i < mesh.n_halffaces(); ++i) {
333 EXPECT_DOUBLE_EQ((
double)i/2.0, hfprop[hfh][0]);
334 EXPECT_DOUBLE_EQ((
double)i/2.0, hfprop[hfh][1]);
335 EXPECT_DOUBLE_EQ((
double)i/2.0, hfprop[hfh][2]);
337 for(
unsigned int i = 0; i < mesh.n_vertices(); ++i) {
339 EXPECT_EQ((
int)i, vprop[vh][0]);
340 EXPECT_EQ((
int)i, vprop[vh][1]);
348 ASSERT_TRUE(this->readFile(
"Cube_with_props.ovm", mesh,
true,
false));
350 EXPECT_EQ(8u, mesh.n_vertices());
351 EXPECT_EQ(12u, mesh.n_edges());
352 EXPECT_EQ(6u, mesh.n_faces());
353 EXPECT_EQ(1u, mesh.n_cells());
367 ASSERT_TRUE(this->readFile(
"Cube_with_props.ovm", mesh,
true,
false));
369 EXPECT_EQ(8u, mesh.n_vertices());
370 EXPECT_EQ(12u, mesh.n_edges());
371 EXPECT_EQ(6u, mesh.n_faces());
372 EXPECT_EQ(1u, mesh.n_cells());
381 ASSERT_TRUE(this->writeFile(
"Cube_with_props.copy.ovm", mesh));
385 ASSERT_TRUE(this->readFile(
"Cube_with_props.copy.ovm", mesh,
true,
false));
387 EXPECT_EQ(8u, mesh.n_vertices());
388 EXPECT_EQ(12u, mesh.n_edges());
389 EXPECT_EQ(6u, mesh.n_faces());
390 EXPECT_EQ(1u, mesh.n_cells());
Read/Write mesh data from/to files.
bool readFile(const std::string &_filename, MeshT &_mesh, bool _topologyCheck=true, bool _computeBottomUpIncidences=true) const
Read a mesh from a file.
bool writeFile(const std::string &_filename, const MeshT &_mesh) const
Write a mesh to a file.
bool isHexahedralMesh(const std::string &_filename) const
Test whether given file contains a hexahedral mesh.