OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Property.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2015 by Computer Graphics Group, RWTH Aachen *
5  * www.openmesh.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenMesh. *
9  * *
10  * OpenMesh 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  * OpenMesh 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 OpenMesh. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 1196 $ *
38  * $Date: 2015-01-14 16:41:22 +0100 (Mi, 14 Jan 2015) $ *
39  * *
40 \*===========================================================================*/
41 
42 #ifndef OPENMESH_PROPERTY_HH
43 #define OPENMESH_PROPERTY_HH
44 
45 
46 //== INCLUDES =================================================================
47 
48 
49 #include <OpenMesh/Core/System/config.h>
50 #include <OpenMesh/Core/Mesh/Handles.hh>
51 #include <OpenMesh/Core/Utils/BaseProperty.hh>
52 #include <vector>
53 #include <string>
54 #include <algorithm>
55 
56 
57 //== NAMESPACES ===============================================================
58 
59 namespace OpenMesh {
60 
61 //== CLASS DEFINITION =========================================================
62 
81 // TODO: it might be possible to define Property using kind of a runtime info
82 // structure holding the size of T. Then reserve, swap, resize, etc can be written
83 // in pure malloc() style w/o virtual overhead. Template member function proved per
84 // element access to the properties, asserting dynamic_casts in debug
85 
86 template <class T>
87 class PropertyT : public BaseProperty
88 {
89 public:
90 
91  typedef T Value;
92  typedef std::vector<T> vector_type;
93  typedef T value_type;
94  typedef typename vector_type::reference reference;
95  typedef typename vector_type::const_reference const_reference;
96 
97 public:
98 
100  PropertyT(const std::string& _name = "<unknown>")
101  : BaseProperty(_name)
102  {}
103 
105  PropertyT(const PropertyT & _rhs)
106  : BaseProperty( _rhs ), data_( _rhs.data_ ) {}
107 
108 public: // inherited from BaseProperty
109 
110  virtual void reserve(size_t _n) { data_.reserve(_n); }
111  virtual void resize(size_t _n) { data_.resize(_n); }
112  virtual void clear() { data_.clear(); vector_type().swap(data_); }
113  virtual void push_back() { data_.push_back(T()); }
114  virtual void swap(size_t _i0, size_t _i1)
115  { std::swap(data_[_i0], data_[_i1]); }
116  virtual void copy(size_t _i0, size_t _i1)
117  { data_[_i1] = data_[_i0]; }
118 
119 public:
120 
121  virtual void set_persistent( bool _yn )
122  { check_and_set_persistent<T>( _yn ); }
123 
124  virtual size_t n_elements() const { return data_.size(); }
125  virtual size_t element_size() const { return IO::size_of<T>(); }
126 
127 #ifndef DOXY_IGNORE_THIS
128  struct plus {
129  size_t operator () ( size_t _b, const T& _v )
130  { return _b + IO::size_of<T>(_v); }
131  };
132 #endif
133 
134  virtual size_t size_of(void) const
135  {
136  if (element_size() != IO::UnknownSize)
137  return this->BaseProperty::size_of(n_elements());
138  return std::accumulate(data_.begin(), data_.end(), size_t(0), plus());
139  }
140 
141  virtual size_t size_of(size_t _n_elem) const
142  { return this->BaseProperty::size_of(_n_elem); }
143 
144  virtual size_t store( std::ostream& _ostr, bool _swap ) const
145  {
146  if ( IO::is_streamable<vector_type>() )
147  return IO::store(_ostr, data_, _swap );
148  size_t bytes = 0;
149  for (size_t i=0; i<n_elements(); ++i)
150  bytes += IO::store( _ostr, data_[i], _swap );
151  return bytes;
152  }
153 
154  virtual size_t restore( std::istream& _istr, bool _swap )
155  {
156  if ( IO::is_streamable<vector_type>() )
157  return IO::restore(_istr, data_, _swap );
158  size_t bytes = 0;
159  for (size_t i=0; i<n_elements(); ++i)
160  bytes += IO::restore( _istr, data_[i], _swap );
161  return bytes;
162  }
163 
164 public: // data access interface
165 
167  const T* data() const {
168 
169  if( data_.empty() )
170  return 0;
171 
172  return &data_[0];
173  }
174 
176  vector_type& data_vector() {
177  return data_;
178  }
179 
181  const vector_type& data_vector() const {
182  return data_;
183  }
184 
186  reference operator[](int _idx)
187  {
188  assert( size_t(_idx) < data_.size() );
189  return data_[_idx];
190  }
191 
193  const_reference operator[](int _idx) const
194  {
195  assert( size_t(_idx) < data_.size());
196  return data_[_idx];
197  }
198 
201  {
202  PropertyT<T>* p = new PropertyT<T>( *this );
203  return p;
204  }
205 
206 
207 private:
208 
209  vector_type data_;
210 };
211 
212 //-----------------------------------------------------------------------------
213 
214 
219 template <>
220 class PropertyT<bool> : public BaseProperty
221 {
222 public:
223 
224  typedef std::vector<bool> vector_type;
225  typedef bool value_type;
226  typedef vector_type::reference reference;
227  typedef vector_type::const_reference const_reference;
228 
229 public:
230 
231  PropertyT(const std::string& _name = "<unknown>")
232  : BaseProperty(_name)
233  { }
234 
235 public: // inherited from BaseProperty
236 
237  virtual void reserve(size_t _n) { data_.reserve(_n); }
238  virtual void resize(size_t _n) { data_.resize(_n); }
239  virtual void clear() { data_.clear(); vector_type().swap(data_); }
240  virtual void push_back() { data_.push_back(bool()); }
241  virtual void swap(size_t _i0, size_t _i1)
242  { bool t(data_[_i0]); data_[_i0]=data_[_i1]; data_[_i1]=t; }
243  virtual void copy(size_t _i0, size_t _i1)
244  { data_[_i1] = data_[_i0]; }
245 
246 public:
247 
248  virtual void set_persistent( bool _yn )
249  {
250  check_and_set_persistent<bool>( _yn );
251  }
252 
253  virtual size_t n_elements() const { return data_.size(); }
254  virtual size_t element_size() const { return UnknownSize; }
255  virtual size_t size_of() const { return size_of( n_elements() ); }
256  virtual size_t size_of(size_t _n_elem) const
257  {
258  return _n_elem / 8 + ((_n_elem % 8)!=0);
259  }
260 
261  size_t store( std::ostream& _ostr, bool /* _swap */ ) const
262  {
263  size_t bytes = 0;
264 
265  size_t N = data_.size() / 8;
266  size_t R = data_.size() % 8;
267 
268  size_t idx; // element index
269  size_t bidx;
270  unsigned char bits; // bitset
271 
272  for (bidx=idx=0; idx < N; ++idx, bidx+=8)
273  {
274  bits = !!data_[bidx]
275  | (!!data_[bidx+1] << 1)
276  | (!!data_[bidx+2] << 2)
277  | (!!data_[bidx+3] << 3)
278  | (!!data_[bidx+4] << 4)
279  | (!!data_[bidx+5] << 5)
280  | (!!data_[bidx+6] << 6)
281  | (!!data_[bidx+7] << 7);
282  _ostr << bits;
283  }
284  bytes = N;
285 
286  if (R)
287  {
288  bits = 0;
289  for (idx=0; idx < R; ++idx)
290  bits |= !!data_[bidx+idx] << idx;
291  _ostr << bits;
292  ++bytes;
293  }
294 
295  std::cout << std::endl;
296 
297  assert( bytes == size_of() );
298 
299  return bytes;
300  }
301 
302  size_t restore( std::istream& _istr, bool /* _swap */ )
303  {
304  size_t bytes = 0;
305 
306  size_t N = data_.size() / 8;
307  size_t R = data_.size() % 8;
308 
309  size_t idx; // element index
310  size_t bidx; //
311  unsigned char bits; // bitset
312 
313  for (bidx=idx=0; idx < N; ++idx, bidx+=8)
314  {
315  _istr >> bits;
316  data_[bidx+0] = !!(bits & 0x01);
317  data_[bidx+1] = !!(bits & 0x02);
318  data_[bidx+2] = !!(bits & 0x04);
319  data_[bidx+3] = !!(bits & 0x08);
320  data_[bidx+4] = !!(bits & 0x10);
321  data_[bidx+5] = !!(bits & 0x20);
322  data_[bidx+6] = !!(bits & 0x40);
323  data_[bidx+7] = !!(bits & 0x80);
324  }
325  bytes = N;
326 
327  if (R)
328  {
329  _istr >> bits;
330  for (idx=0; idx < R; ++idx)
331  data_[bidx+idx] = !!(bits & (1<<idx));
332  ++bytes;
333  }
334 
335  std::cout << std::endl;
336 
337  return bytes;
338  }
339 
340 
341 public:
342 
344  vector_type& data_vector() {
345  return data_;
346  }
347 
349  const vector_type& data_vector() const {
350  return data_;
351  }
352 
354  reference operator[](int _idx)
355  {
356  assert( size_t(_idx) < data_.size() );
357  return data_[_idx];
358  }
359 
361  const_reference operator[](int _idx) const
362  {
363  assert( size_t(_idx) < data_.size());
364  return data_[_idx];
365  }
366 
369  {
370  PropertyT<bool>* p = new PropertyT<bool>( *this );
371  return p;
372  }
373 
374 
375 private:
376 
377  vector_type data_;
378 };
379 
380 
381 //-----------------------------------------------------------------------------
382 
383 
386 template <>
387 class PropertyT<std::string> : public BaseProperty
388 {
389 public:
390 
391  typedef std::string Value;
392  typedef std::vector<std::string> vector_type;
393  typedef std::string value_type;
394  typedef vector_type::reference reference;
395  typedef vector_type::const_reference const_reference;
396 
397 public:
398 
399  PropertyT(const std::string& _name = "<unknown>")
400  : BaseProperty(_name)
401  { }
402 
403 public: // inherited from BaseProperty
404 
405  virtual void reserve(size_t _n) { data_.reserve(_n); }
406  virtual void resize(size_t _n) { data_.resize(_n); }
407  virtual void clear() { data_.clear(); vector_type().swap(data_); }
408  virtual void push_back() { data_.push_back(std::string()); }
409  virtual void swap(size_t _i0, size_t _i1) {
410  std::swap(data_[_i0], data_[_i1]);
411  }
412  virtual void copy(size_t _i0, size_t _i1)
413  { data_[_i1] = data_[_i0]; }
414 
415 public:
416 
417  virtual void set_persistent( bool _yn )
418  { check_and_set_persistent<std::string>( _yn ); }
419 
420  virtual size_t n_elements() const { return data_.size(); }
421  virtual size_t element_size() const { return UnknownSize; }
422  virtual size_t size_of() const
423  { return IO::size_of( data_ ); }
424 
425  virtual size_t size_of(size_t /* _n_elem */) const
426  { return UnknownSize; }
427 
429  size_t store( std::ostream& _ostr, bool _swap ) const
430  { return IO::store( _ostr, data_, _swap ); }
431 
432  size_t restore( std::istream& _istr, bool _swap )
433  { return IO::restore( _istr, data_, _swap ); }
434 
435 public:
436 
437  const value_type* data() const {
438  if( data_.empty() )
439  return 0;
440 
441  return (value_type*) &data_[0];
442  }
443 
445  reference operator[](int _idx) {
446  assert( size_t(_idx) < data_.size());
447  return ((value_type*) &data_[0])[_idx];
448  }
449 
451  const_reference operator[](int _idx) const {
452  assert( size_t(_idx) < data_.size());
453  return ((value_type*) &data_[0])[_idx];
454  }
455 
458  return p;
459  }
460 private:
461 
462  vector_type data_;
463 
464 };
465 
467 template <class T>
469 {
470  typedef T Value;
471  typedef std::vector<T> vector_type;
472  typedef T value_type;
473  typedef typename vector_type::reference reference;
474  typedef typename vector_type::const_reference const_reference;
475 
476  explicit BasePropHandleT(int _idx=-1) : BaseHandle(_idx) {}
477 };
478 
479 
483 template <class T>
484 struct VPropHandleT : public BasePropHandleT<T>
485 {
486  typedef T Value;
487  typedef T value_type;
488 
489  explicit VPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
490  explicit VPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
491 };
492 
493 
497 template <class T>
498 struct HPropHandleT : public BasePropHandleT<T>
499 {
500  typedef T Value;
501  typedef T value_type;
502 
503  explicit HPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
504  explicit HPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
505 };
506 
507 
511 template <class T>
512 struct EPropHandleT : public BasePropHandleT<T>
513 {
514  typedef T Value;
515  typedef T value_type;
516 
517  explicit EPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
518  explicit EPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
519 };
520 
521 
525 template <class T>
526 struct FPropHandleT : public BasePropHandleT<T>
527 {
528  typedef T Value;
529  typedef T value_type;
530 
531  explicit FPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
532  explicit FPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
533 };
534 
535 
539 template <class T>
540 struct MPropHandleT : public BasePropHandleT<T>
541 {
542  typedef T Value;
543  typedef T value_type;
544 
545  explicit MPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
546  explicit MPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
547 };
548 
549 } // namespace OpenMesh
550 //=============================================================================
551 #endif // OPENMESH_PROPERTY_HH defined
552 //=============================================================================
size_t restore(std::istream &_istr, bool _swap)
Restore self from a binary block.
Definition: Property.hh:432
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: Property.hh:241
virtual size_t size_of() const
Return size of property in bytes.
Definition: Property.hh:255
vector_type & data_vector()
Get reference to property vector (be careful, improper usage, e.g. resizing, may crash OpenMesh!!!) ...
Definition: Property.hh:176
const_reference operator[](int _idx) const
Const access the i'th element. No range check is performed!
Definition: Property.hh:451
Handle representing an edge property.
Definition: Property.hh:512
Base class for all handle types.
Definition: Handles.hh:60
virtual void set_persistent(bool _yn)
Enable or disable persistency.
Definition: Property.hh:121
const vector_type & data_vector() const
Const access to property vector.
Definition: Property.hh:349
virtual void copy(size_t _i0, size_t _i1)
Copy one element to another.
Definition: Property.hh:412
virtual size_t restore(std::istream &_istr, bool _swap)
Restore self from a binary block.
Definition: Property.hh:154
Default property class for any type T.
Definition: Property.hh:87
virtual void clear()
Clear all elements and free memory.
Definition: Property.hh:239
virtual size_t store(std::ostream &_ostr, bool _swap) const
Store self as one binary block.
Definition: Property.hh:144
virtual size_t size_of() const
Return size of property in bytes.
Definition: Property.hh:422
static const size_t UnknownSize
Indicates an error when a size is returned by a member.
Definition: BaseProperty.hh:63
Handle representing a halfedge property.
Definition: Property.hh:498
size_t store(std::ostream &_ostr, bool) const
Store self as one binary block.
Definition: Property.hh:261
Base property handle.
Definition: Property.hh:468
size_t size_of(const T &_v)
Binary read a short from _is and perform byte swapping if _swap is true.
Definition: StoreRestore.hh:87
virtual void push_back()
Extend the number of elements by one.
Definition: Property.hh:113
virtual void push_back()
Extend the number of elements by one.
Definition: Property.hh:408
virtual size_t size_of(size_t _n_elem) const
Estimated size of property if it has _n_elem elements.
Definition: Property.hh:256
virtual size_t n_elements() const
Number of elements in property.
Definition: Property.hh:420
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: Property.hh:405
virtual void set_persistent(bool _yn)
Enable or disable persistency.
Definition: Property.hh:248
Property specialization for bool type.
Definition: Property.hh:220
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: Property.hh:406
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition: Property.hh:254
virtual void copy(size_t _i0, size_t _i1)
Copy one element to another.
Definition: Property.hh:116
virtual void clear()
Clear all elements and free memory.
Definition: Property.hh:112
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: Property.hh:238
virtual size_t n_elements() const
Number of elements in property.
Definition: Property.hh:124
const_reference operator[](int _idx) const
Const access to the i'th element. No range check is performed!
Definition: Property.hh:361
virtual void copy(size_t _i0, size_t _i1)
Copy one element to another.
Definition: Property.hh:243
const vector_type & data_vector() const
Const access to property vector.
Definition: Property.hh:181
size_t store(std::ostream &_ostr, bool _swap) const
Store self as one binary block. Max. length of a string is 65535 bytes.
Definition: Property.hh:429
const_reference operator[](int _idx) const
Const access to the i'th element. No range check is performed!
Definition: Property.hh:193
Abstract class defining the basic interface of a dynamic property.
Definition: BaseProperty.hh:58
virtual void set_persistent(bool _yn)
Enable or disable persistency.
Definition: Property.hh:417
reference operator[](int _idx)
Access the i'th element. No range check is performed!
Definition: Property.hh:445
virtual size_t n_elements() const
Number of elements in property.
Definition: Property.hh:253
PropertyT(const PropertyT &_rhs)
Copy constructor.
Definition: Property.hh:105
PropertyT< T > * clone() const
Make a copy of self.
Definition: Property.hh:200
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition: Property.hh:421
reference operator[](int _idx)
Access the i'th element. No range check is performed!
Definition: Property.hh:186
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: Property.hh:237
Handle representing a face property.
Definition: Property.hh:526
STL namespace.
virtual size_t size_of(size_t _n_elem) const
Estimated size of property if it has _n_elem elements.
Definition: Property.hh:141
reference operator[](int _idx)
Access the i'th element. No range check is performed!
Definition: Property.hh:354
const T * data() const
Get pointer to array (does not work for T==bool)
Definition: Property.hh:167
PropertyT(const std::string &_name="<unknown>")
Default constructor.
Definition: Property.hh:100
virtual size_t size_of(void) const
Return size of property in bytes.
Definition: Property.hh:134
PropertyT< bool > * clone() const
Make a copy of self.
Definition: Property.hh:368
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition: Property.hh:125
virtual void clear()
Clear all elements and free memory.
Definition: Property.hh:407
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition: Property.hh:110
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: Property.hh:114
BaseProperty(const std::string &_name="<unknown>")
Default constructor.
Definition: BaseProperty.hh:81
virtual size_t size_of(size_t) const
Estimated size of property if it has _n_elem elements.
Definition: Property.hh:425
virtual void push_back()
Extend the number of elements by one.
Definition: Property.hh:240
Handle representing a vertex property.
Definition: Property.hh:484
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition: Property.hh:111
Contains all the mesh ingredients like the polygonal mesh, the triangle mesh, different mesh kernels ...
Definition: MeshItems.hh:56
vector_type & data_vector()
Get reference to property vector (be careful, improper usage, e.g. resizing, may crash OpenMesh!!!) ...
Definition: Property.hh:344
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition: Property.hh:409
Handle representing a mesh property.
Definition: Property.hh:540
virtual size_t size_of() const
Return size of property in bytes.
Definition: BaseProperty.hh:138
size_t restore(std::istream &_istr, bool)
Restore self from a binary block.
Definition: Property.hh:302
PropertyT< value_type > * clone() const
Return a deep copy of self.
Definition: Property.hh:456

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .