Developer Documentation
OpenVolumeMeshProperty.hh
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 /*===========================================================================*\
36  * *
37  * $Revision$ *
38  * $Date$ *
39  * $LastChangedBy$ *
40  * *
41 \*===========================================================================*/
42 
43 #ifndef OPENVOLUMEMESHPROPERTY_HH
44 #define OPENVOLUMEMESHPROPERTY_HH
45 
46 //== INCLUDES =================================================================
47 
48 #include <cassert>
49 #include <istream>
50 #include <ostream>
51 #include <numeric>
52 #include <string>
53 #include <vector>
54 
55 #include "OpenVolumeMeshBaseProperty.hh"
56 
57 #include "Serializers.hh"
58 
59 namespace OpenVolumeMesh {
60 
61 //== CLASS DEFINITION =========================================================
62 
70 template<class T>
72 public:
73 
74  template <class PropT, class HandleT> friend class PropertyPtr;
75 
76  typedef T Value;
77  typedef std::vector<T> vector_type;
78  typedef T value_type;
79  typedef typename vector_type::reference reference;
80  typedef typename vector_type::const_reference const_reference;
81 
82 public:
83 
85  OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const T _def = T()) :
87  def_(_def) {
88  }
89 
93  data_(_rhs.data_),
94  def_(_rhs.def_) {
95  }
96 
97 public:
98  // inherited from OpenVolumeMeshBaseProperty
99  virtual void reserve(size_t _n) {
100  data_.reserve(_n);
101  }
102  virtual void resize(size_t _n) {
103  data_.resize(_n, def_);
104  }
105  virtual void clear() {
106  data_.clear();
107  vector_type().swap(data_);
108  }
109  virtual void push_back() {
110  data_.push_back(def_);
111  }
112  virtual void swap(size_t _i0, size_t _i1) {
113  std::swap(data_[_i0], data_[_i1]);
114  }
115 
116  void delete_element(size_t _idx) {
117  data_.erase(data_.begin() + _idx);
118  }
119 
120 public:
121 
122  virtual size_t n_elements() const {
123  return data_.size();
124  }
125  virtual size_t element_size() const {
126  return sizeof(T);
127  }
128 
129 #ifndef DOXY_IGNORE_THIS
130  struct plus {
131  size_t operator ()(size_t _b, const T& /*_v*/) {
132  return _b + sizeof(T);
133  }
134  };
135 #endif
136 
137  virtual size_t size_of() const {
140  return std::accumulate(data_.begin(), data_.end(), 0, plus());
141  }
142 
143  virtual size_t size_of(size_t _n_elem) const {
144  return this->OpenVolumeMeshBaseProperty::size_of(_n_elem);
145  }
146 
147  // Function to serialize a property
148  virtual void serialize(std::ostream& _ostr) const {
149  for(typename vector_type::const_iterator it = data_.begin();
150  it != data_.end(); ++it) {
151  OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
152  }
153  }
154 
155  // Function to deserialize a property
156  virtual void deserialize(std::istream& _istr) {
157  for(unsigned int i = 0; i < n_elements(); ++i) {
158  OpenVolumeMesh::deserialize(_istr, data_[i]);
159  }
160  }
161 
162 public:
163  // data access interface
164 
166  const T* data() const {
167 
168  if (data_.empty())
169  return 0;
170 
171  return &data_[0];
172  }
173 
175  vector_type& data_vector() {
176 
177  return data_;
178  }
179 
181  reference operator[](int _idx) {
182  assert(size_t(_idx) < data_.size());
183  return data_[_idx];
184  }
185 
187  const_reference operator[](int _idx) const {
188  assert(size_t(_idx) < data_.size());
189  return data_[_idx];
190  }
191 
195  return p;
196  }
197 
198  typename vector_type::const_iterator begin() const { return data_.begin(); }
199 
200  typename vector_type::iterator begin() { return data_.begin(); }
201 
202  typename vector_type::const_iterator end() const { return data_.end(); }
203 
204  typename vector_type::iterator end() { return data_.end(); }
205 
206 protected:
207 
209  virtual void delete_multiple_entries(const std::vector<bool>& _tags) {
210 
211  assert(_tags.size() == data_.size());
212  vector_type new_data;
213  typename vector_type::iterator d_it = data_.begin();
214  std::vector<bool>::const_iterator t_it = _tags.begin();
215  std::vector<bool>::const_iterator t_end = _tags.end();
216  for(; t_it != t_end; ++t_it, ++d_it) {
217  if(!*t_it) {
218  new_data.push_back(*d_it);
219  }
220  }
221  data_.swap(new_data);
222  }
223 
224 private:
225 
226  vector_type data_;
227 
228  const T def_;
229 };
230 
231 //-----------------------------------------------------------------------------
232 
233 
237 template<>
239 public:
240 
241  template <class PropT, class HandleT> friend class PropertyPtr;
242 
243  typedef std::vector<bool> vector_type;
244  typedef bool value_type;
245  typedef vector_type::reference reference;
246  typedef vector_type::const_reference const_reference;
247 
248 public:
249 
250  OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const bool _def = bool()) :
252  def_(_def) {
253  }
254 
255 public:
256  // inherited from OpenVolumeMeshBaseProperty
257 
258  virtual void reserve(size_t _n) {
259  data_.reserve(_n);
260  }
261  virtual void resize(size_t _n) {
262  data_.resize(_n, def_);
263  }
264  virtual void clear() {
265  data_.clear();
266  vector_type().swap(data_);
267  }
268  virtual void push_back() {
269  data_.push_back(def_);
270  }
271  virtual void swap(size_t _i0, size_t _i1) {
272  bool t(data_[_i0]);
273  data_[_i0] = data_[_i1];
274  data_[_i1] = t;
275  }
276 
277  void delete_element(size_t _idx) {
278  data_.erase(data_.begin() + _idx);
279  }
280 
281 public:
282 
283  virtual size_t n_elements() const {
284  return data_.size();
285  }
286  virtual size_t element_size() const {
288  }
289  virtual size_t size_of() const {
290  return size_of(n_elements());
291  }
292  virtual size_t size_of(size_t _n_elem) const {
293  return _n_elem / 8 + ((_n_elem % 8) != 0);
294  }
295 
296  // Function to serialize a property
297  virtual void serialize(std::ostream& _ostr) const {
298  for(vector_type::const_iterator it = data_.begin();
299  it != data_.end(); ++it) {
300  OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
301  }
302  }
303 
304  // Function to deserialize a property
305  virtual void deserialize(std::istream& _istr) {
306  for(unsigned int i = 0; i < n_elements(); ++i) {
307  value_type val;
308  OpenVolumeMesh::deserialize(_istr, val);
309  data_[i] = val;
310  }
311  }
312 
313 public:
314 
316  reference operator[](int _idx) {
317  assert(size_t(_idx) < data_.size());
318  return data_[_idx];
319  }
320 
322  const_reference operator[](int _idx) const {
323  assert(size_t(_idx) < data_.size());
324  return data_[_idx];
325  }
326 
330  *this);
331  return p;
332  }
333 
334  vector_type::const_iterator begin() const { return data_.begin(); }
335 
336  vector_type::iterator begin() { return data_.begin(); }
337 
338  vector_type::const_iterator end() const { return data_.end(); }
339 
340  vector_type::iterator end() { return data_.end(); }
341 
342 protected:
343 
345  virtual void delete_multiple_entries(const std::vector<bool>& _tags) {
346 
347  assert(_tags.size() == data_.size());
348  vector_type new_data;
349  vector_type::iterator d_it = data_.begin();
350  std::vector<bool>::const_iterator t_it = _tags.begin();
351  std::vector<bool>::const_iterator t_end = _tags.end();
352  for(; t_it != t_end; ++t_it, ++d_it) {
353  if(!*t_it) {
354  new_data.push_back(*d_it);
355  }
356  }
357  data_.swap(new_data);
358  }
359 
360 private:
361 
362  vector_type data_;
363 
364  const bool def_;
365 };
366 
367 //-----------------------------------------------------------------------------
368 
369 
373 template<>
375 public:
376 
377  template <class PropT, class HandleT> friend class PropertyPtr;
378 
379  typedef std::string Value;
380  typedef std::vector<std::string> vector_type;
381  typedef std::string value_type;
382  typedef vector_type::reference reference;
383  typedef vector_type::const_reference const_reference;
384 
385 public:
386 
387  OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const std::string& _def = "") :
389  def_(_def) {
390  }
391 
392 public:
393  // inherited from OpenVolumeMeshBaseProperty
394 
395  virtual void reserve(size_t _n) {
396  data_.reserve(_n);
397  }
398  virtual void resize(size_t _n) {
399  data_.resize(_n, def_);
400  }
401  virtual void clear() {
402  data_.clear();
403  vector_type().swap(data_);
404  }
405  virtual void push_back() {
406  data_.push_back(def_);
407  }
408  virtual void swap(size_t _i0, size_t _i1) {
409  std::swap(data_[_i0], data_[_i1]);
410  }
411 
412  virtual void delete_element(size_t _idx) {
413  data_.erase(data_.begin() + _idx);
414  }
415 
416 public:
417 
418  virtual size_t n_elements() const {
419  return data_.size();
420  }
421  virtual size_t element_size() const {
423  }
424  virtual size_t size_of() const {
425  return sizeof(data_);
426  }
427 
428  virtual size_t size_of(size_t /* _n_elem */) const {
430  }
431 
432  virtual void stats(std::ostream& _ostr) const {
433  for(vector_type::const_iterator it = data_.begin();
434  it != data_.end(); ++it) {
435  _ostr << *it << " ";
436  }
437  }
438 
439  // Function to serialize a property
440  virtual void serialize(std::ostream& _ostr) const {
441  for(vector_type::const_iterator it = data_.begin();
442  it != data_.end(); ++it) {
443  OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
444  }
445  }
446 
447  // Function to deserialize a property
448  virtual void deserialize(std::istream& _istr) {
449  for(unsigned int i = 0; i < n_elements(); ++i) {
450  OpenVolumeMesh::deserialize(_istr, data_[i]);
451  }
452  }
453 
454 public:
455 
456  const value_type* data() const {
457  if (data_.empty())
458  return 0;
459 
460  return (value_type*) &data_[0];
461  }
462 
464  reference operator[](int _idx) {
465  assert(size_t(_idx) < data_.size());
466  return ((value_type*) &data_[0])[_idx];
467  }
468 
470  const_reference operator[](int _idx) const {
471  assert(size_t(_idx) < data_.size());
472  return ((value_type*) &data_[0])[_idx];
473  }
474 
477  value_type> (*this);
478  return p;
479  }
480 
481  vector_type::const_iterator begin() const { return data_.begin(); }
482 
483  vector_type::iterator begin() { return data_.begin(); }
484 
485  vector_type::const_iterator end() const { return data_.end(); }
486 
487  vector_type::iterator end() { return data_.end(); }
488 
489 protected:
490 
492  virtual void delete_multiple_entries(const std::vector<bool>& _tags) {
493 
494  assert(_tags.size() == data_.size());
495  vector_type new_data;
496  vector_type::iterator d_it = data_.begin();
497  std::vector<bool>::const_iterator t_it = _tags.begin();
498  std::vector<bool>::const_iterator t_end = _tags.end();
499  for(; t_it != t_end; ++t_it, ++d_it) {
500  if(!*t_it) {
501  new_data.push_back(*d_it);
502  }
503  }
504  data_.swap(new_data);
505  }
506 
507 private:
508 
509  vector_type data_;
510 
511  const std::string def_;
512 };
513 
514 } // Namespace OpenVolumeMesh
515 
516 //=============================================================================
517 #endif // OPENVOLUMEMESHPROPERTY_HH defined
518 //=============================================================================
OpenVolumeMeshPropertyT(const std::string &_name="<unknown>", const T _def=T())
Default constructor.
virtual size_t n_elements() const
Number of elements in property.
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
OpenVolumeMeshPropertyT< T > * clone() const
Make a copy of self.
virtual void resize(size_t _n)
Resize storage to hold n elements.
virtual size_t size_of() const
Return size of property in bytes.
virtual size_t size_of() const
Return size of property in bytes.
void delete_element(size_t _idx)
Erase an element of the vector.
OpenVolumeMeshPropertyT< bool > * clone() const
Make a copy of self.
virtual void clear()
Clear all elements and free memory.
virtual void reserve(size_t _n)
Reserve memory for n elements.
const_reference operator[](int _idx) const
Const access the i&#39;th element. No range check is performed!
const T * data() const
Get pointer to array (does not work for T==bool)
reference operator[](int _idx)
Access the i&#39;th element. No range check is performed!
virtual size_t n_elements() const
Number of elements in property.
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
OpenVolumeMeshPropertyT< value_type > * clone() const
Return a deep copy of self.
void delete_element(size_t _idx)
Erase an element of the vector.
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
reference operator[](int _idx)
Access the i&#39;th element. No range check is performed!
virtual size_t size_of() const
Return size of property in bytes.
virtual void push_back()
Extend the number of elements by one.
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
virtual void delete_element(size_t _idx)
Erase an element of the vector.
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
virtual void clear()
Clear all elements and free memory.
Default property class for any type T.
virtual void resize(size_t _n)
Resize storage to hold n elements.
virtual size_t size_of(size_t _n_elem) const
vector_type & data_vector()
Get reference to property vector (be careful, improper usage, e.g. resizing, may crash) ...
const_reference operator[](int _idx) const
Const access to the i&#39;th element. No range check is performed!
OpenVolumeMeshPropertyT(const OpenVolumeMeshPropertyT &_rhs)
Copy constructor.
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
const_reference operator[](int _idx) const
Const access to the i&#39;th element. No range check is performed!
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
static const size_t UnknownSize
Indicates an error when a size is returned by a member.
virtual size_t n_elements() const
Number of elements in property.
virtual void reserve(size_t _n)
Reserve memory for n elements.
virtual void clear()
Clear all elements and free memory.
virtual void push_back()
Extend the number of elements by one.
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
STL namespace.
virtual size_t size_of() const
Return size of property in bytes.
reference operator[](int _idx)
Access the i&#39;th element. No range check is performed!
virtual void resize(size_t _n)
Resize storage to hold n elements.
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
virtual void reserve(size_t _n)
Reserve memory for n elements.
virtual void push_back()
Extend the number of elements by one.
virtual size_t size_of(size_t _n_elem) const