Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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  virtual void copy(size_t _src_idx, size_t _dst_idx) {
116  data_[_dst_idx] = data_[_src_idx];
117  }
118  void delete_element(size_t _idx) {
119  data_.erase(data_.begin() + _idx);
120  }
121 
122 public:
123 
124  virtual size_t n_elements() const {
125  return data_.size();
126  }
127  virtual size_t element_size() const {
128  return sizeof(T);
129  }
130 
131 #ifndef DOXY_IGNORE_THIS
132  struct plus {
133  size_t operator ()(size_t _b, const T& /*_v*/) {
134  return _b + sizeof(T);
135  }
136  };
137 #endif
138 
139  virtual size_t size_of() const {
142  return std::accumulate(data_.begin(), data_.end(), size_t(0), plus());
143  }
144 
145  virtual size_t size_of(size_t _n_elem) const {
146  return this->OpenVolumeMeshBaseProperty::size_of(_n_elem);
147  }
148 
149  // Function to serialize a property
150  virtual void serialize(std::ostream& _ostr) const {
151  for(typename vector_type::const_iterator it = data_.begin();
152  it != data_.end(); ++it) {
153  OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
154  }
155  }
156 
157  // Function to deserialize a property
158  virtual void deserialize(std::istream& _istr) {
159  for(unsigned int i = 0; i < n_elements(); ++i) {
160  OpenVolumeMesh::deserialize(_istr, data_[i]);
161  }
162  }
163 
164 public:
165  // data access interface
166 
168  const T* data() const {
169 
170  if (data_.empty())
171  return 0;
172 
173  return &data_[0];
174  }
175 
177  vector_type& data_vector() {
178 
179  return data_;
180  }
181 
183  reference operator[](size_t _idx) {
184  assert(_idx < data_.size());
185  return data_[_idx];
186  }
187 
189  const_reference operator[](size_t _idx) const {
190  assert(_idx < data_.size());
191  return data_[_idx];
192  }
193 
197  return p;
198  }
199 
200  typename vector_type::const_iterator begin() const { return data_.begin(); }
201 
202  typename vector_type::iterator begin() { return data_.begin(); }
203 
204  typename vector_type::const_iterator end() const { return data_.end(); }
205 
206  typename vector_type::iterator end() { return data_.end(); }
207 
208 protected:
209 
211  virtual void delete_multiple_entries(const std::vector<bool>& _tags) {
212 
213  assert(_tags.size() == data_.size());
214  vector_type new_data;
215  typename vector_type::iterator d_it = data_.begin();
216  std::vector<bool>::const_iterator t_it = _tags.begin();
217  std::vector<bool>::const_iterator t_end = _tags.end();
218  for(; t_it != t_end; ++t_it, ++d_it) {
219  if(!*t_it) {
220  new_data.push_back(*d_it);
221  }
222  }
223  data_.swap(new_data);
224  }
225 
226 private:
227 
228  vector_type data_;
229 
230  const T def_;
231 };
232 
233 //-----------------------------------------------------------------------------
234 
235 
239 template<>
241 public:
242 
243  template <class PropT, class HandleT> friend class PropertyPtr;
244 
245  typedef std::vector<bool> vector_type;
246  typedef bool value_type;
247  typedef vector_type::reference reference;
248  typedef vector_type::const_reference const_reference;
249 
250 public:
251 
252  OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const bool _def = bool()) :
254  def_(_def) {
255  }
256 
257 public:
258  // inherited from OpenVolumeMeshBaseProperty
259 
260  virtual void reserve(size_t _n) {
261  data_.reserve(_n);
262  }
263  virtual void resize(size_t _n) {
264  data_.resize(_n, def_);
265  }
266  virtual void clear() {
267  data_.clear();
268  vector_type().swap(data_);
269  }
270  virtual void push_back() {
271  data_.push_back(def_);
272  }
273  virtual void swap(size_t _i0, size_t _i1) {
274  bool t(data_[_i0]);
275  data_[_i0] = data_[_i1];
276  data_[_i1] = t;
277  }
278  virtual void copy(size_t _src_idx, size_t _dst_idx) {
279  data_[_dst_idx] = data_[_src_idx];
280  }
281 
282  void delete_element(size_t _idx) {
283  data_.erase(data_.begin() + _idx);
284  }
285 
286 public:
287 
288  virtual size_t n_elements() const {
289  return data_.size();
290  }
291  virtual size_t element_size() const {
293  }
294  virtual size_t size_of() const {
295  return size_of(n_elements());
296  }
297  virtual size_t size_of(size_t _n_elem) const {
298  return _n_elem / 8 + ((_n_elem % 8) != 0);
299  }
300 
301  // Function to serialize a property
302  virtual void serialize(std::ostream& _ostr) const {
303  for(vector_type::const_iterator it = data_.begin();
304  it != data_.end(); ++it) {
305  OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
306  }
307  }
308 
309  // Function to deserialize a property
310  virtual void deserialize(std::istream& _istr) {
311  for(unsigned int i = 0; i < n_elements(); ++i) {
312  value_type val;
313  OpenVolumeMesh::deserialize(_istr, val);
314  data_[i] = val;
315  }
316  }
317 
318 public:
319 
321  reference operator[](size_t _idx) {
322  assert(_idx < data_.size());
323  return data_[_idx];
324  }
325 
327  const_reference operator[](size_t _idx) const {
328  assert(_idx < data_.size());
329  return data_[_idx];
330  }
331 
335  *this);
336  return p;
337  }
338 
339  vector_type::const_iterator begin() const { return data_.begin(); }
340 
341  vector_type::iterator begin() { return data_.begin(); }
342 
343  vector_type::const_iterator end() const { return data_.end(); }
344 
345  vector_type::iterator end() { return data_.end(); }
346 
347 protected:
348 
350  virtual void delete_multiple_entries(const std::vector<bool>& _tags) {
351 
352  assert(_tags.size() == data_.size());
353  vector_type new_data;
354  vector_type::iterator d_it = data_.begin();
355  std::vector<bool>::const_iterator t_it = _tags.begin();
356  std::vector<bool>::const_iterator t_end = _tags.end();
357  for(; t_it != t_end; ++t_it, ++d_it) {
358  if(!*t_it) {
359  new_data.push_back(*d_it);
360  }
361  }
362  data_.swap(new_data);
363  }
364 
365 private:
366 
367  vector_type data_;
368 
369  const bool def_;
370 };
371 
372 //-----------------------------------------------------------------------------
373 
374 
378 template<>
380 public:
381 
382  template <class PropT, class HandleT> friend class PropertyPtr;
383 
384  typedef std::string Value;
385  typedef std::vector<std::string> vector_type;
386  typedef std::string value_type;
387  typedef vector_type::reference reference;
388  typedef vector_type::const_reference const_reference;
389 
390 public:
391 
392  OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const std::string& _def = "") :
394  def_(_def) {
395  }
396 
397 public:
398  // inherited from OpenVolumeMeshBaseProperty
399 
400  virtual void reserve(size_t _n) {
401  data_.reserve(_n);
402  }
403  virtual void resize(size_t _n) {
404  data_.resize(_n, def_);
405  }
406  virtual void clear() {
407  data_.clear();
408  vector_type().swap(data_);
409  }
410  virtual void push_back() {
411  data_.push_back(def_);
412  }
413  virtual void swap(size_t _i0, size_t _i1) {
414  std::swap(data_[_i0], data_[_i1]);
415  }
416  virtual void copy(size_t _src_idx, size_t _dst_idx) {
417  data_[_dst_idx] = data_[_src_idx];
418  }
419  virtual void delete_element(size_t _idx) {
420  data_.erase(data_.begin() + _idx);
421  }
422 
423 public:
424 
425  virtual size_t n_elements() const {
426  return data_.size();
427  }
428  virtual size_t element_size() const {
430  }
431  virtual size_t size_of() const {
432  return sizeof(data_);
433  }
434 
435  virtual size_t size_of(size_t /* _n_elem */) const {
437  }
438 
439  virtual void stats(std::ostream& _ostr) const {
440  for(vector_type::const_iterator it = data_.begin();
441  it != data_.end(); ++it) {
442  _ostr << *it << " ";
443  }
444  }
445 
446  // Function to serialize a property
447  virtual void serialize(std::ostream& _ostr) const {
448  for(vector_type::const_iterator it = data_.begin();
449  it != data_.end(); ++it) {
450  OpenVolumeMesh::serialize(_ostr, *it) << std::endl;
451  }
452  }
453 
454  // Function to deserialize a property
455  virtual void deserialize(std::istream& _istr) {
456  for(unsigned int i = 0; i < n_elements(); ++i) {
457  OpenVolumeMesh::deserialize(_istr, data_[i]);
458  }
459  }
460 
461 public:
462 
463  const value_type* data() const {
464  if (data_.empty())
465  return 0;
466 
467  return (value_type*) &data_[0];
468  }
469 
471  reference operator[](size_t _idx) {
472  assert(_idx < data_.size());
473  return ((value_type*) &data_[0])[_idx];
474  }
475 
477  const_reference operator[](size_t _idx) const {
478  assert(_idx < data_.size());
479  return ((value_type*) &data_[0])[_idx];
480  }
481 
484  value_type> (*this);
485  return p;
486  }
487 
488  vector_type::const_iterator begin() const { return data_.begin(); }
489 
490  vector_type::iterator begin() { return data_.begin(); }
491 
492  vector_type::const_iterator end() const { return data_.end(); }
493 
494  vector_type::iterator end() { return data_.end(); }
495 
496 protected:
497 
499  virtual void delete_multiple_entries(const std::vector<bool>& _tags) {
500 
501  assert(_tags.size() == data_.size());
502  vector_type new_data;
503  vector_type::iterator d_it = data_.begin();
504  std::vector<bool>::const_iterator t_it = _tags.begin();
505  std::vector<bool>::const_iterator t_end = _tags.end();
506  for(; t_it != t_end; ++t_it, ++d_it) {
507  if(!*t_it) {
508  new_data.push_back(*d_it);
509  }
510  }
511  data_.swap(new_data);
512  }
513 
514 private:
515 
516  vector_type data_;
517 
518  const std::string def_;
519 };
520 
521 } // Namespace OpenVolumeMesh
522 
523 //=============================================================================
524 #endif // OPENVOLUMEMESHPROPERTY_HH defined
525 //=============================================================================
virtual void clear()
Clear all elements and free memory.
virtual void reserve(size_t _n)
Reserve memory for n elements.
reference operator[](size_t _idx)
Access the i'th element. No range check is performed!
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
OpenVolumeMeshPropertyT< T > * clone() const
Make a copy of self.
const_reference operator[](size_t _idx) const
Const access the i'th element. No range check is performed!
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
virtual size_t size_of() const
Return size of property in bytes.
virtual size_t size_of() const
Return size of property in bytes.
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
OpenVolumeMeshPropertyT(const std::string &_name="<unknown>", const T _def=T())
Default constructor.
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 clear()
Clear all elements and free memory.
reference operator[](size_t _idx)
Access the i'th element. No range check is performed!
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.
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
virtual size_t n_elements() const
Number of elements in property.
OpenVolumeMeshPropertyT(const OpenVolumeMeshPropertyT &_rhs)
Copy constructor.
const_reference operator[](size_t _idx) const
Const access to the i'th element. No range check is performed!
OpenVolumeMeshPropertyT< bool > * clone() const
Make a copy of self.
virtual size_t size_of(size_t _n_elem) const
OpenVolumeMeshPropertyT< value_type > * clone() const
Return a deep copy of self.
virtual size_t n_elements() const
Number of elements in property.
vector_type & data_vector()
Get reference to property vector (be careful, improper usage, e.g. resizing, may crash) ...
static const size_t UnknownSize
Indicates an error when a size is returned by a member.
virtual size_t size_of() const
Return size of property in bytes.
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
virtual void reserve(size_t _n)
Reserve memory for n elements.
virtual size_t n_elements() const
Number of elements in property.
const_reference operator[](size_t _idx) const
Const access to the i'th element. No range check is performed!
Default property class for any type T.
virtual void push_back()
Extend the number of elements by one.
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
virtual size_t size_of(size_t _n_elem) const
virtual void resize(size_t _n)
Resize storage to hold n elements.
virtual void reserve(size_t _n)
Reserve memory for n elements.
STL namespace.
reference operator[](size_t _idx)
Access the i'th element. No range check is performed!
const T * data() const
Get pointer to array (does not work for T==bool)
virtual void resize(size_t _n)
Resize storage to hold n elements.
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
virtual size_t size_of() const
Return size of property in bytes.
void delete_element(size_t _idx)
Erase an element of the vector.
virtual void push_back()
Extend the number of elements by one.
virtual void delete_element(size_t _idx)
Erase an element of the vector.
virtual void clear()
Clear all elements and free memory.
virtual void push_back()
Extend the number of elements by one.