Developer Documentation
unittests_propertymanager.cc
1 #include <gtest/gtest.h>
2 #include <Unittests/unittests_common.hh>
3 #include <OpenMesh/Core/Utils/PropertyManager.hh>
4 
5 #include <iostream>
6 
7 namespace {
8 
9 class OpenMeshPropertyManager : public OpenMeshBase {
10 
11  protected:
12 
13  // This function is called before each test is run
14  virtual void SetUp() {
15  }
16 
17  // This function is called after all tests are through
18  virtual void TearDown() {
19 
20  // Do some final stuff with the member data here...
21  }
22 
23  // Member already defined in OpenMeshBase
24  //Mesh mesh_;
25 };
26 
27 /*
28  * ====================================================================
29  * General Tests
30  * ====================================================================
31  */
32 
33 /*
34  * Collapsing a tetrahedron
35  */
36 TEST_F(OpenMeshPropertyManager, set_range_bool) {
37 
38  mesh_.clear();
39 
40  // Add some vertices
41  Mesh::VertexHandle vhandle[4];
42 
43  vhandle[0] = mesh_.add_vertex(Mesh::Point(0, 0, 0));
44  vhandle[1] = mesh_.add_vertex(Mesh::Point(0, 1, 0));
45  vhandle[2] = mesh_.add_vertex(Mesh::Point(1, 1, 0));
46  vhandle[3] = mesh_.add_vertex(Mesh::Point(0, 0, 1));
47 
48  // Add two faces
49  std::vector<Mesh::VertexHandle> face_vhandles;
50 
51  face_vhandles.push_back(vhandle[0]);
52  face_vhandles.push_back(vhandle[1]);
53  face_vhandles.push_back(vhandle[2]);
54  mesh_.add_face(face_vhandles);
55 
56  face_vhandles.clear();
57 
58  face_vhandles.push_back(vhandle[0]);
59  face_vhandles.push_back(vhandle[2]);
60  face_vhandles.push_back(vhandle[3]);
61  mesh_.add_face(face_vhandles);
62 
63  {
65  OpenMesh::VPropHandleT<bool>, Mesh> pm_v_bool(mesh_, "pm_v_bool");
66  pm_v_bool.set_range(mesh_.vertices_begin(), mesh_.vertices_end(), false);
67  for (int i = 0; i < 4; ++i)
68  ASSERT_FALSE(pm_v_bool[vhandle[i]]);
69  pm_v_bool.set_range(mesh_.vertices_begin(), mesh_.vertices_end(), true);
70  for (int i = 0; i < 4; ++i)
71  ASSERT_TRUE(pm_v_bool[vhandle[i]]);
72 
74  OpenMesh::EPropHandleT<bool>, Mesh> pm_e_bool(mesh_, "pm_e_bool");
75  pm_e_bool.set_range(mesh_.edges_begin(), mesh_.edges_end(), false);
76  for (Mesh::EdgeIter e_it = mesh_.edges_begin(), f_end = mesh_.edges_end();
77  e_it != f_end; ++e_it)
78  ASSERT_FALSE(pm_e_bool[*e_it]);
79  pm_e_bool.set_range(mesh_.edges_begin(), mesh_.edges_end(), true);
80  for (Mesh::EdgeIter e_it = mesh_.edges_begin(), f_end = mesh_.edges_end();
81  e_it != f_end; ++e_it)
82  ASSERT_TRUE(pm_e_bool[*e_it]);
83 
85  OpenMesh::FPropHandleT<bool>, Mesh> pm_f_bool(mesh_, "pm_f_bool");
86  pm_f_bool.set_range(mesh_.faces_begin(), mesh_.faces_end(), false);
87  for (Mesh::FaceIter f_it = mesh_.faces_begin(), f_end = mesh_.faces_end();
88  f_it != f_end; ++f_it)
89  ASSERT_FALSE(pm_f_bool[*f_it]);
90  pm_f_bool.set_range(mesh_.faces_begin(), mesh_.faces_end(), true);
91  for (Mesh::FaceIter f_it = mesh_.faces_begin(), f_end = mesh_.faces_end();
92  f_it != f_end; ++f_it)
93  ASSERT_TRUE(pm_f_bool[*f_it]);
94  }
95 
96 #if (defined(_MSC_VER) && (_MSC_VER >= 1900)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
97  /*
98  * Same thing again, this time with C++11 ranges.
99  */
100  {
102  OpenMesh::VPropHandleT<bool>, Mesh> pm_v_bool(mesh_, "pm_v_bool2");
103  pm_v_bool.set_range(mesh_.vertices(), false);
104  for (int i = 0; i < 4; ++i)
105  ASSERT_FALSE(pm_v_bool[vhandle[i]]);
106  pm_v_bool.set_range(mesh_.vertices(), true);
107  for (int i = 0; i < 4; ++i)
108  ASSERT_TRUE(pm_v_bool[vhandle[i]]);
109 
111  OpenMesh::EPropHandleT<bool>, Mesh> pm_e_bool(mesh_, "pm_e_bool2");
112  pm_e_bool.set_range(mesh_.edges(), false);
113  for (Mesh::EdgeIter e_it = mesh_.edges_begin(), f_end = mesh_.edges_end();
114  e_it != f_end; ++e_it)
115  ASSERT_FALSE(pm_e_bool[*e_it]);
116  pm_e_bool.set_range(mesh_.edges(), true);
117  for (Mesh::EdgeIter e_it = mesh_.edges_begin(), f_end = mesh_.edges_end();
118  e_it != f_end; ++e_it)
119  ASSERT_TRUE(pm_e_bool[*e_it]);
120 
122  OpenMesh::FPropHandleT<bool>, Mesh> pm_f_bool(mesh_, "pm_f_bool2");
123  pm_f_bool.set_range(mesh_.faces(), false);
124  for (Mesh::FaceIter f_it = mesh_.faces_begin(), f_end = mesh_.faces_end();
125  f_it != f_end; ++f_it)
126  ASSERT_FALSE(pm_f_bool[*f_it]);
127  pm_f_bool.set_range(mesh_.faces(), true);
128  for (Mesh::FaceIter f_it = mesh_.faces_begin(), f_end = mesh_.faces_end();
129  f_it != f_end; ++f_it)
130  ASSERT_TRUE(pm_f_bool[*f_it]);
131  }
132 #endif
133 }
134 
135 /*
136  * ====================================================================
137  * C++11 Specific Tests
138  * ====================================================================
139  */
140 #if (defined(_MSC_VER) && (_MSC_VER >= 1900)) || __cplusplus > 199711L || defined(__GXX_EXPERIMENTAL_CXX0X__)
141 
142 template<typename PropHandle, typename Mesh>
143 bool has_property(const Mesh& _mesh, const std::string& _name) {
144  auto dummy_handle = PropHandle{};
145  return _mesh.get_property_handle(dummy_handle, _name);
146 }
147 
148 /*
149  * Temporary property
150  */
151 TEST_F(OpenMeshPropertyManager, cpp11_temp_property) {
152  using handle_type = OpenMesh::VPropHandleT<int>;
153  const auto prop_name = "pm_v_test_property";
154  ASSERT_FALSE(has_property<handle_type>(mesh_, prop_name));
155 
156  {
157  auto vprop = OpenMesh::makePropertyManagerFromNew<handle_type>(mesh_, prop_name);
158  static_cast<void>(vprop); // Unused variable
159  ASSERT_TRUE(has_property<handle_type>(mesh_, prop_name));
160  }
161 
162  ASSERT_FALSE(has_property<handle_type>(mesh_, prop_name));
163 }
164 
165 /*
166  * Two temporary properties on a mesh using the same name and type. The second
167  * (inner) one shadows the first (outer) one instead of aliasing.
168  */
169 TEST_F(OpenMeshPropertyManager, cpp11_temp_property_shadowing) {
170  auto vh = mesh_.add_vertex({0,0,0}); // Dummy vertex to attach properties to
171 
172  using handle_type = OpenMesh::VPropHandleT<int>;
173  const auto prop_name = "pm_v_test_property";
174 
175  auto outer_prop = OpenMesh::makePropertyManagerFromNew<handle_type>(mesh_, prop_name);
176  outer_prop[vh] = 100;
177  ASSERT_EQ(100, outer_prop[vh]);
178 
179  {
180  // inner_prop uses same type and name as outer_prop
181  auto inner_prop = OpenMesh::makePropertyManagerFromNew<handle_type>(mesh_, prop_name);
182  inner_prop[vh] = 200;
183  ASSERT_EQ(200, inner_prop[vh]);
184  // End of scope: inner_prop is removed from mesh_
185  }
186 
187  // Ensure outer_prop still exists and its data has not been overwritten by inner_prop
188  ASSERT_TRUE(has_property<handle_type>(mesh_, prop_name));
189  ASSERT_EQ(100, outer_prop[vh]);
190 }
191 
192 /*
193  * In sequence:
194  * - add a persistent property to a mesh
195  * - retrieve an existing property of a mesh and modify it
196  * - obtain a non-owning property handle
197  * - attempt to obtain a non-owning handle to a non-existing property (throws)
198  */
199 TEST_F(OpenMeshPropertyManager, cpp11_persistent_and_non_owning_properties) {
200  auto vh = mesh_.add_vertex({0,0,0}); // Dummy vertex to attach properties to
201 
202  using handle_type = OpenMesh::VPropHandleT<int>;
203  const auto prop_name = "pm_v_test_property";
204 
205  ASSERT_FALSE(has_property<handle_type>(mesh_, prop_name));
206 
207  {
208  auto prop = OpenMesh::makePropertyManagerFromExistingOrNew<handle_type>(mesh_, prop_name);
209  prop[vh] = 100;
210  // End of scope, property persists
211  }
212 
213  ASSERT_TRUE(has_property<handle_type>(mesh_, prop_name));
214 
215  {
216  // Since a property of the same name and type already exists, this refers to the existing property.
217  auto prop = OpenMesh::makePropertyManagerFromExistingOrNew<handle_type>(mesh_, prop_name);
218  ASSERT_EQ(100, prop[vh]);
219  prop[vh] = 200;
220  // End of scope, property persists
221  }
222 
223  ASSERT_TRUE(has_property<handle_type>(mesh_, prop_name));
224 
225  {
226  // Acquire non-owning handle to the property, knowing it exists
227  auto prop = OpenMesh::makePropertyManagerFromExisting<handle_type>(mesh_, prop_name);
228  ASSERT_EQ(200, prop[vh]);
229  }
230 
231  ASSERT_TRUE(has_property<handle_type>(mesh_, prop_name));
232 
233  {
234  // Attempt to acquire non-owning handle for a non-existing property
235  ASSERT_THROW(OpenMesh::makePropertyManagerFromExisting<handle_type>(mesh_, "wrong_property_name"), std::runtime_error);
236  }
237 
238  ASSERT_TRUE(has_property<handle_type>(mesh_, prop_name));
239 }
240 
241 #endif
242 
243 }
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:139
VertexHandle add_vertex(const Point &_p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:236
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:115