Developer Documentation
ResourceManager.cc
1 /*===========================================================================*\
2  * *
3  * OpenVolumeMesh *
4  * Copyright (C) 2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openvolumemesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenVolumeMesh. *
9  * *
10  * OpenVolumeMesh is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenVolumeMesh is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenVolumeMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 #include "ResourceManager.hh"
36 
37 namespace OpenVolumeMesh {
38 
39 ResourceManager::ResourceManager(const ResourceManager &other)
40 {
41  *this = other;
42 }
43 
44 ResourceManager::ResourceManager(ResourceManager &&other)
45 {
46  *this = std::move(other);
47 }
48 
49 ResourceManager& ResourceManager::operator=(const ResourceManager &other)
50 {
51  if (this == &other)
52  return *this;
53 
54  assignAllPropertiesFrom<false>(&other);
55 
56  return *this;
57 }
58 
59 ResourceManager& ResourceManager::operator=(ResourceManager &&other)
60 {
61  if (this == &other)
62  return *this;
63 
64  assignAllPropertiesFrom<true>(&other);
65 
66  return *this;
67 }
68 
69 ResourceManager::~ResourceManager() {
70 
71  // Delete persistent props
72  clearVec(vertex_props_);
73  clearVec(edge_props_);
74  clearVec(halfedge_props_);
75  clearVec(face_props_);
76  clearVec(halfface_props_);
77  clearVec(cell_props_);
78  clearVec(mesh_props_);
79 }
80 
82 
83  resize_props(vertex_props_, _nv);
84 }
85 
87 
88  resize_props(edge_props_, _ne);
89  resize_props(halfedge_props_, _ne*2u);
90 }
91 
93 
94  resize_props(face_props_, _nf);
95  resize_props(halfface_props_, _nf*2u);
96 }
97 
99 
100  resize_props(cell_props_, _nc);
101 }
102 
103 void ResourceManager::vertex_deleted(const VertexHandle& _h) {
104 
105  entity_deleted(vertex_props_, _h);
106 }
107 
108 void ResourceManager::edge_deleted(const EdgeHandle& _h) {
109 
110  entity_deleted(edge_props_, _h);
111  entity_deleted(halfedge_props_, OpenVolumeMeshHandle(_h.idx()*2 + 1));
112  entity_deleted(halfedge_props_, OpenVolumeMeshHandle(_h.idx()*2));
113 }
114 
115 void ResourceManager::face_deleted(const FaceHandle& _h) {
116 
117  entity_deleted(face_props_, _h);
118  entity_deleted(halfface_props_, OpenVolumeMeshHandle(_h.idx()*2 + 1));
119  entity_deleted(halfface_props_, OpenVolumeMeshHandle(_h.idx()*2));
120 }
121 
122 void ResourceManager::cell_deleted(const CellHandle& _h) {
123 
124  entity_deleted(cell_props_, _h);
125 }
126 
127 void ResourceManager::swap_cell_properties(CellHandle _h1, CellHandle _h2){
128 
129  swap_property_elements(cell_props_begin(), cell_props_end(), _h1, _h2);
130 }
131 
132 void ResourceManager::swap_face_properties(FaceHandle _h1, FaceHandle _h2){
133 
134  swap_property_elements(face_props_begin(), face_props_end(), _h1, _h2);
135 }
136 
137 void ResourceManager::swap_halfface_properties(HalfFaceHandle _h1, HalfFaceHandle _h2){
138 
139  swap_property_elements(halfface_props_begin(), halfface_props_end(), _h1, _h2);
140 }
141 
142 void ResourceManager::swap_edge_properties(EdgeHandle _h1, EdgeHandle _h2){
143 
144  swap_property_elements(edge_props_begin(), edge_props_end(), _h1, _h2);
145 }
146 
147 void ResourceManager::swap_halfedge_properties(HalfEdgeHandle _h1, HalfEdgeHandle _h2){
148 
149  swap_property_elements(halfedge_props_begin(), halfedge_props_end(), _h1, _h2);
150 }
151 
152 void ResourceManager::swap_vertex_properties(VertexHandle _h1, VertexHandle _h2){
153 
154  swap_property_elements(vertex_props_begin(), vertex_props_end(), _h1, _h2);
155 }
156 
157 void ResourceManager::release_property(VertexPropHandle _handle) {
158 
159  remove_property(vertex_props_, _handle.uidx());
160 }
161 
162 void ResourceManager::release_property(EdgePropHandle _handle) {
163 
164  remove_property(edge_props_, _handle.uidx());
165 }
166 
167 void ResourceManager::release_property(HalfEdgePropHandle _handle) {
168 
169  remove_property(halfedge_props_, _handle.uidx());
170 }
171 
172 void ResourceManager::release_property(FacePropHandle _handle) {
173 
174  remove_property(face_props_, _handle.uidx());
175 }
176 
177 void ResourceManager::release_property(HalfFacePropHandle _handle) {
178 
179  remove_property(halfface_props_, _handle.uidx());
180 }
181 
182 void ResourceManager::release_property(CellPropHandle _handle) {
183 
184  remove_property(cell_props_, _handle.uidx());
185 }
186 
187 void ResourceManager::release_property(MeshPropHandle _handle) {
188 
189  remove_property(mesh_props_, _handle.idx());
190 }
191 
192 void ResourceManager::delete_multiple_vertex_props(const std::vector<bool>& _tags) {
193 
194  Properties::iterator vp_it = vertex_props_.begin();
195  Properties::iterator vp_end = vertex_props_.end();
196  for(; vp_it != vp_end; ++vp_it) {
197  (*vp_it)->delete_multiple_entries(_tags);
198  }
199 }
200 
201 void ResourceManager::delete_multiple_edge_props(const std::vector<bool>& _tags) {
202 
203  Properties::iterator ep_it = edge_props_.begin();
204  Properties::iterator ep_end = edge_props_.end();
205  for(; ep_it != ep_end; ++ep_it) {
206  (*ep_it)->delete_multiple_entries(_tags);
207  }
208  // Create tags vector for halfedges
209  std::vector<bool> hetags;
210  for(std::vector<bool>::const_iterator t_it = _tags.begin(),
211  t_end = _tags.end(); t_it != t_end; ++t_it) {
212  hetags.push_back(*t_it);
213  hetags.push_back(*t_it);
214  }
215  Properties::iterator hep_it = halfedge_props_.begin();
216  Properties::iterator hep_end = halfedge_props_.end();
217  for(; hep_it != hep_end; ++hep_it) {
218  (*hep_it)->delete_multiple_entries(hetags);
219  }
220 }
221 
222 void ResourceManager::delete_multiple_face_props(const std::vector<bool>& _tags) {
223 
224  Properties::iterator fp_it = face_props_.begin();
225  Properties::iterator fp_end = face_props_.end();
226  for(; fp_it != fp_end; ++fp_it) {
227  (*fp_it)->delete_multiple_entries(_tags);
228  }
229  // Create tags vector for halffaces
230  std::vector<bool> hftags;
231  for(std::vector<bool>::const_iterator t_it = _tags.begin(),
232  t_end = _tags.end(); t_it != t_end; ++t_it) {
233  hftags.push_back(*t_it);
234  hftags.push_back(*t_it);
235  }
236  Properties::iterator hfp_it = halfface_props_.begin();
237  Properties::iterator hfp_end = halfface_props_.end();
238  for(; hfp_it != hfp_end; ++hfp_it) {
239  (*hfp_it)->delete_multiple_entries(hftags);
240  }
241 }
242 
243 void ResourceManager::delete_multiple_cell_props(const std::vector<bool>& _tags) {
244 
245  Properties::iterator cp_it = cell_props_.begin();
246  Properties::iterator cp_end = cell_props_.end();
247  for(; cp_it != cp_end; ++cp_it) {
248  (*cp_it)->delete_multiple_entries(_tags);
249  }
250 }
251 
252 template<bool Move>
253 void ResourceManager::assignProperties(typename std::conditional<Move, Properties&, const Properties&>::type src,
254  Properties &dest)
255 {
256  // If possible, re-use existing properties instead of copying
257  // everything blindly.
258  Properties out;
259  out.reserve(src.size());
260  for (BaseProperty *srcprop: src) {
261  bool found = false;
262  for (auto it = dest.begin(); it != dest.end(); ++it) {
263  auto dstprop = *it;
264  if (dstprop->name() == srcprop->name()
265  && dstprop->internal_type_name() == srcprop->internal_type_name())
266  {
267  out.push_back(dstprop);
268  dest.erase(it);
269  if (Move) {
270  dstprop->move_values_from(srcprop);
271  } else {
272  dstprop->assign_values_from(srcprop);
273  }
274  found = true;
275  break;
276  }
277  }
278  if (!found) {
279  if (Move) {
280  out.push_back(srcprop);
281  } else {
282  out.push_back(srcprop->clone(*this, OpenVolumeMeshHandle(-1)));
283  }
284  }
285  }
286  updatePropHandles(out);
287  dest = std::move(out);
288 }
289 
290 template<bool Move>
291 void ResourceManager::assignAllPropertiesFrom(typename std::conditional<Move, ResourceManager*, const ResourceManager*>::type other)
292 {
293  assignProperties<Move>(other->vertex_props_, vertex_props_);
294  assignProperties<Move>(other->edge_props_, edge_props_);
295  assignProperties<Move>(other->halfedge_props_, halfedge_props_);
296  assignProperties<Move>(other->face_props_, face_props_);
297  assignProperties<Move>(other->halfface_props_, halfface_props_);
298  assignProperties<Move>(other->cell_props_, cell_props_);
299  assignProperties<Move>(other->mesh_props_, mesh_props_);
300 }
301 
302 } // Namespace OpenVolumeMesh
void resize_cprops(size_t _nc)
Change size of stored cell properties.
void resize_eprops(size_t _ne)
Change size of stored edge properties.
size_t uidx() const
return unsigned idx - handle must be valid
void resize_fprops(size_t _nf)
Change size of stored face properties.
void resize_vprops(size_t _nv)
Change size of stored vertex properties.