OpenMesh
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Property.hh
1 /*===========================================================================*\
2  * *
3  * OpenMesh *
4  * Copyright (C) 2001-2011 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: 537 $ *
38  * $Date: 2012-02-24 11:11:01 +0100 (Fr, 24 Feb 2012) $ *
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 
117 public:
118 
119  virtual void set_persistent( bool _yn )
120  { check_and_set_persistent<T>( _yn ); }
121 
122  virtual size_t n_elements() const { return data_.size(); }
123  virtual size_t element_size() const { return IO::size_of<T>(); }
124 
125 #ifndef DOXY_IGNORE_THIS
126  struct plus {
127  size_t operator () ( size_t _b, const T& _v )
128  { return _b + IO::size_of<T>(_v); }
129  };
130 #endif
131 
132  virtual size_t size_of(void) const
133  {
134  if (element_size() != IO::UnknownSize)
135  return this->BaseProperty::size_of(n_elements());
136  return std::accumulate(data_.begin(), data_.end(), 0, plus());
137  }
138 
139  virtual size_t size_of(size_t _n_elem) const
140  { return this->BaseProperty::size_of(_n_elem); }
141 
142  virtual size_t store( std::ostream& _ostr, bool _swap ) const
143  {
144  if ( IO::is_streamable<vector_type>() )
145  return IO::store(_ostr, data_, _swap );
146  size_t bytes = 0;
147  for (size_t i=0; i<n_elements(); ++i)
148  bytes += IO::store( _ostr, data_[i], _swap );
149  return bytes;
150  }
151 
152  virtual size_t restore( std::istream& _istr, bool _swap )
153  {
154  if ( IO::is_streamable<vector_type>() )
155  return IO::restore(_istr, data_, _swap );
156  size_t bytes = 0;
157  for (size_t i=0; i<n_elements(); ++i)
158  bytes += IO::restore( _istr, data_[i], _swap );
159  return bytes;
160  }
161 
162 public: // data access interface
163 
165  const T* data() const {
166 
167  if( data_.empty() )
168  return 0;
169 
170  return &data_[0];
171  }
172 
174  vector_type& data_vector() {
175 
176  return data_;
177  }
178 
180  reference operator[](int _idx)
181  {
182  assert( size_t(_idx) < data_.size() );
183  return data_[_idx];
184  }
185 
187  const_reference operator[](int _idx) const
188  {
189  assert( size_t(_idx) < data_.size());
190  return data_[_idx];
191  }
192 
195  {
196  PropertyT<T>* p = new PropertyT<T>( *this );
197  return p;
198  }
199 
200 
201 private:
202 
203  vector_type data_;
204 };
205 
206 //-----------------------------------------------------------------------------
207 
208 
213 template <>
214 class PropertyT<bool> : public BaseProperty
215 {
216 public:
217 
218  typedef std::vector<bool> vector_type;
219  typedef bool value_type;
220  typedef vector_type::reference reference;
221  typedef vector_type::const_reference const_reference;
222 
223 public:
224 
225  PropertyT(const std::string& _name = "<unknown>")
226  : BaseProperty(_name)
227  { }
228 
229 public: // inherited from BaseProperty
230 
231  virtual void reserve(size_t _n) { data_.reserve(_n); }
232  virtual void resize(size_t _n) { data_.resize(_n); }
233  virtual void clear() { data_.clear(); vector_type().swap(data_); }
234  virtual void push_back() { data_.push_back(bool()); }
235  virtual void swap(size_t _i0, size_t _i1)
236  { bool t(data_[_i0]); data_[_i0]=data_[_i1]; data_[_i1]=t; }
237 
238 public:
239 
240  virtual void set_persistent( bool _yn )
241  {
242  check_and_set_persistent<bool>( _yn );
243  }
244 
245  virtual size_t n_elements() const { return data_.size(); }
246  virtual size_t element_size() const { return UnknownSize; }
247  virtual size_t size_of() const { return size_of( n_elements() ); }
248  virtual size_t size_of(size_t _n_elem) const
249  {
250  return _n_elem / 8 + ((_n_elem % 8)!=0);
251  }
252 
253  size_t store( std::ostream& _ostr, bool /* _swap */ ) const
254  {
255  size_t bytes = 0;
256 
257  size_t N = data_.size() / 8;
258  size_t R = data_.size() % 8;
259 
260  size_t idx; // element index
261  size_t bidx;
262  unsigned char bits; // bitset
263 
264  for (bidx=idx=0; idx < N; ++idx, bidx+=8)
265  {
266  bits = !!data_[bidx]
267  | (!!data_[bidx+1] << 1)
268  | (!!data_[bidx+2] << 2)
269  | (!!data_[bidx+3] << 3)
270  | (!!data_[bidx+4] << 4)
271  | (!!data_[bidx+5] << 5)
272  | (!!data_[bidx+6] << 6)
273  | (!!data_[bidx+7] << 7);
274  _ostr << bits;
275  }
276  bytes = N;
277 
278  if (R)
279  {
280  bits = 0;
281  for (idx=0; idx < R; ++idx)
282  bits |= !!data_[bidx+idx] << idx;
283  _ostr << bits;
284  ++bytes;
285  }
286 
287  std::cout << std::endl;
288 
289  assert( bytes == size_of() );
290 
291  return bytes;
292  }
293 
294  size_t restore( std::istream& _istr, bool /* _swap */ )
295  {
296  size_t bytes = 0;
297 
298  size_t N = data_.size() / 8;
299  size_t R = data_.size() % 8;
300 
301  size_t idx; // element index
302  size_t bidx; //
303  unsigned char bits; // bitset
304 
305  for (bidx=idx=0; idx < N; ++idx, bidx+=8)
306  {
307  _istr >> bits;
308  data_[bidx+0] = !!(bits & 0x01);
309  data_[bidx+1] = !!(bits & 0x02);
310  data_[bidx+2] = !!(bits & 0x04);
311  data_[bidx+3] = !!(bits & 0x08);
312  data_[bidx+4] = !!(bits & 0x10);
313  data_[bidx+5] = !!(bits & 0x20);
314  data_[bidx+6] = !!(bits & 0x40);
315  data_[bidx+7] = !!(bits & 0x80);
316  }
317  bytes = N;
318 
319  if (R)
320  {
321  _istr >> bits;
322  for (idx=0; idx < R; ++idx)
323  data_[bidx+idx] = !!(bits & (1<<idx));
324  ++bytes;
325  }
326 
327  std::cout << std::endl;
328 
329  return bytes;
330  }
331 
332 
333 public:
334 
336  vector_type& data_vector() {
337 
338  return data_;
339  }
340 
342  reference operator[](int _idx)
343  {
344  assert( size_t(_idx) < data_.size() );
345  return data_[_idx];
346  }
347 
349  const_reference operator[](int _idx) const
350  {
351  assert( size_t(_idx) < data_.size());
352  return data_[_idx];
353  }
354 
357  {
358  PropertyT<bool>* p = new PropertyT<bool>( *this );
359  return p;
360  }
361 
362 
363 private:
364 
365  vector_type data_;
366 };
367 
368 
369 //-----------------------------------------------------------------------------
370 
371 
374 template <>
375 class PropertyT<std::string> : public BaseProperty
376 {
377 public:
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  PropertyT(const std::string& _name = "<unknown>")
388  : BaseProperty(_name)
389  { }
390 
391 public: // inherited from BaseProperty
392 
393  virtual void reserve(size_t _n) { data_.reserve(_n); }
394  virtual void resize(size_t _n) { data_.resize(_n); }
395  virtual void clear() { data_.clear(); vector_type().swap(data_); }
396  virtual void push_back() { data_.push_back(std::string()); }
397  virtual void swap(size_t _i0, size_t _i1) {
398  std::swap(data_[_i0], data_[_i1]);
399  }
400 
401 public:
402 
403  virtual void set_persistent( bool _yn )
404  { check_and_set_persistent<std::string>( _yn ); }
405 
406  virtual size_t n_elements() const { return data_.size(); }
407  virtual size_t element_size() const { return UnknownSize; }
408  virtual size_t size_of() const
409  { return IO::size_of( data_ ); }
410 
411  virtual size_t size_of(size_t /* _n_elem */) const
412  { return UnknownSize; }
413 
415  size_t store( std::ostream& _ostr, bool _swap ) const
416  { return IO::store( _ostr, data_, _swap ); }
417 
418  size_t restore( std::istream& _istr, bool _swap )
419  { return IO::restore( _istr, data_, _swap ); }
420 
421 public:
422 
423  const value_type* data() const {
424  if( data_.empty() )
425  return 0;
426 
427  return (value_type*) &data_[0];
428  }
429 
431  reference operator[](int _idx) {
432  assert( size_t(_idx) < data_.size());
433  return ((value_type*) &data_[0])[_idx];
434  }
435 
437  const_reference operator[](int _idx) const {
438  assert( size_t(_idx) < data_.size());
439  return ((value_type*) &data_[0])[_idx];
440  }
441 
444  return p;
445  }
446 
447 
448 private:
449 
450  vector_type data_;
451 
452 };
453 
455 template <class T>
457 {
458  typedef T Value;
459  typedef std::vector<T> vector_type;
460  typedef T value_type;
461  typedef typename vector_type::reference reference;
462  typedef typename vector_type::const_reference const_reference;
463 
464  explicit BasePropHandleT(int _idx=-1) : BaseHandle(_idx) {}
465 };
466 
467 
471 template <class T>
472 struct VPropHandleT : public BasePropHandleT<T>
473 {
474  typedef T Value;
475  typedef T value_type;
476 
477  explicit VPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
478  explicit VPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
479 };
480 
481 
485 template <class T>
486 struct HPropHandleT : public BasePropHandleT<T>
487 {
488  typedef T Value;
489  typedef T value_type;
490 
491  explicit HPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
492  explicit HPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
493 };
494 
495 
499 template <class T>
500 struct EPropHandleT : public BasePropHandleT<T>
501 {
502  typedef T Value;
503  typedef T value_type;
504 
505  explicit EPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
506  explicit EPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
507 };
508 
509 
513 template <class T>
514 struct FPropHandleT : public BasePropHandleT<T>
515 {
516  typedef T Value;
517  typedef T value_type;
518 
519  explicit FPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
520  explicit FPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
521 };
522 
523 
527 template <class T>
528 struct MPropHandleT : public BasePropHandleT<T>
529 {
530  typedef T Value;
531  typedef T value_type;
532 
533  explicit MPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
534  explicit MPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
535 };
536 
537 } // namespace OpenMesh
538 //=============================================================================
539 #endif // OPENMESH_PROPERTY_HH defined
540 //=============================================================================

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