OpenVolumeMesh
 All Classes Functions Variables Typedefs 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: 217 $ *
38  * $Date: 2012-07-18 15:04:05 +0200 (Wed, 18 Jul 2012) $ *
39  * $LastChangedBy: kremer $ *
40  * *
41 \*===========================================================================*/
42 
43 #ifndef OPENVOLUMEMESHPROPERTY_HH
44 #define OPENVOLUMEMESHPROPERTY_HH
45 
46 //== INCLUDES =================================================================
47 
48 #include <vector>
49 #include <string>
50 #include <iostream>
51 #include <algorithm>
52 #include <numeric>
53 #include <cassert>
54 
55 #include "OpenVolumeMeshBaseProperty.hh"
56 #include "OpenVolumeMeshHandle.hh"
57 
58 namespace OpenVolumeMesh {
59 
60 //== CLASS DEFINITION =========================================================
61 
69 template<class T>
71 public:
72 
73  template <class PropT, class HandleT> friend class PropertyPtr;
74 
75  typedef T Value;
76  typedef std::vector<T> vector_type;
77  typedef T value_type;
78  typedef typename vector_type::reference reference;
79  typedef typename vector_type::const_reference const_reference;
80 
81 public:
82 
84  OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const T _def = T()) :
86  def_(_def) {
87  }
88 
92  data_(_rhs.data_),
93  def_(_rhs.def_) {
94  }
95 
96 public:
97  // inherited from OpenVolumeMeshBaseProperty
98  virtual void reserve(size_t _n) {
99  data_.reserve(_n);
100  }
101  virtual void resize(size_t _n) {
102  data_.resize(_n, def_);
103  }
104  virtual void clear() {
105  data_.clear();
106  vector_type().swap(data_);
107  }
108  virtual void push_back() {
109  data_.push_back(def_);
110  }
111  virtual void swap(size_t _i0, size_t _i1) {
112  std::swap(data_[_i0], data_[_i1]);
113  }
114 
115  void delete_element(size_t _idx) {
116  data_.erase(data_.begin() + _idx);
117  }
118 
119 public:
120 
121  virtual size_t n_elements() const {
122  return data_.size();
123  }
124  virtual size_t element_size() const {
125  return sizeof(T);
126  }
127 
128 #ifndef DOXY_IGNORE_THIS
129  struct plus {
130  size_t operator ()(size_t _b, const T& /*_v*/) {
131  return _b + sizeof(T);
132  }
133  };
134 #endif
135 
136  virtual size_t size_of() const {
139  return std::accumulate(data_.begin(), data_.end(), 0, plus());
140  }
141 
142  virtual size_t size_of(size_t _n_elem) const {
143  return this->OpenVolumeMeshBaseProperty::size_of(_n_elem);
144  }
145 
146  // Function to serialize a property
147  virtual void serialize(std::ostream& _ostr) const {
148  OpenVolumeMeshBaseProperty::serialize(_ostr);
149  for(typename vector_type::const_iterator it = data_.begin();
150  it != data_.end(); ++it) {
151  _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  value_type val;
159  _istr >> val;
160  data_[i] = val;
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[](int _idx) {
184  assert(size_t(_idx) < data_.size());
185  return data_[_idx];
186  }
187 
189  const_reference operator[](int _idx) const {
190  assert(size_t(_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 
279  void delete_element(size_t _idx) {
280  data_.erase(data_.begin() + _idx);
281  }
282 
283 public:
284 
285  virtual size_t n_elements() const {
286  return data_.size();
287  }
288  virtual size_t element_size() const {
290  }
291  virtual size_t size_of() const {
292  return size_of(n_elements());
293  }
294  virtual size_t size_of(size_t _n_elem) const {
295  return _n_elem / 8 + ((_n_elem % 8) != 0);
296  }
297 
298 public:
299 
301  reference operator[](int _idx) {
302  assert(size_t(_idx) < data_.size());
303  return data_[_idx];
304  }
305 
307  const_reference operator[](int _idx) const {
308  assert(size_t(_idx) < data_.size());
309  return data_[_idx];
310  }
311 
315  *this);
316  return p;
317  }
318 
319  vector_type::const_iterator begin() const { return data_.begin(); }
320 
321  vector_type::iterator begin() { return data_.begin(); }
322 
323  vector_type::const_iterator end() const { return data_.end(); }
324 
325  vector_type::iterator end() { return data_.end(); }
326 
327 protected:
328 
330  virtual void delete_multiple_entries(const std::vector<bool>& _tags) {
331 
332  assert(_tags.size() == data_.size());
333  vector_type new_data;
334  vector_type::iterator d_it = data_.begin();
335  std::vector<bool>::const_iterator t_it = _tags.begin();
336  std::vector<bool>::const_iterator t_end = _tags.end();
337  for(; t_it != t_end; ++t_it, ++d_it) {
338  if(!*t_it) {
339  new_data.push_back(*d_it);
340  }
341  }
342  data_.swap(new_data);
343  }
344 
345 private:
346 
347  vector_type data_;
348 
349  const bool def_;
350 };
351 
352 //-----------------------------------------------------------------------------
353 
354 
358 template<>
360 public:
361 
362  template <class PropT, class HandleT> friend class PropertyPtr;
363 
364  typedef std::string Value;
365  typedef std::vector<std::string> vector_type;
366  typedef std::string value_type;
367  typedef vector_type::reference reference;
368  typedef vector_type::const_reference const_reference;
369 
370 public:
371 
372  OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const std::string _def = std::string()) :
374  def_(_def) {
375  }
376 
377 public:
378  // inherited from OpenVolumeMeshBaseProperty
379 
380  virtual void reserve(size_t _n) {
381  data_.reserve(_n);
382  }
383  virtual void resize(size_t _n) {
384  data_.resize(_n, def_);
385  }
386  virtual void clear() {
387  data_.clear();
388  vector_type().swap(data_);
389  }
390  virtual void push_back() {
391  data_.push_back(def_);
392  }
393  virtual void swap(size_t _i0, size_t _i1) {
394  std::swap(data_[_i0], data_[_i1]);
395  }
396 
397  virtual void delete_element(size_t _idx) {
398  data_.erase(data_.begin() + _idx);
399  }
400 
401 public:
402 
403  virtual size_t n_elements() const {
404  return data_.size();
405  }
406  virtual size_t element_size() const {
408  }
409  virtual size_t size_of() const {
410  return sizeof(data_);
411  }
412 
413  virtual size_t size_of(size_t /* _n_elem */) const {
415  }
416 
417  virtual void stats(std::ostream& _ostr) const {
418  for(vector_type::const_iterator it = data_.begin();
419  it != data_.end(); ++it) {
420  _ostr << *it << " ";
421  }
422  }
423 
424 public:
425 
426  const value_type* data() const {
427  if (data_.empty())
428  return 0;
429 
430  return (value_type*) &data_[0];
431  }
432 
434  reference operator[](int _idx) {
435  assert(size_t(_idx) < data_.size());
436  return ((value_type*) &data_[0])[_idx];
437  }
438 
440  const_reference operator[](int _idx) const {
441  assert(size_t(_idx) < data_.size());
442  return ((value_type*) &data_[0])[_idx];
443  }
444 
447  value_type> (*this);
448  return p;
449  }
450 
451  vector_type::const_iterator begin() const { return data_.begin(); }
452 
453  vector_type::iterator begin() { return data_.begin(); }
454 
455  vector_type::const_iterator end() const { return data_.end(); }
456 
457  vector_type::iterator end() { return data_.end(); }
458 
459 protected:
460 
462  virtual void delete_multiple_entries(const std::vector<bool>& _tags) {
463 
464  assert(_tags.size() == data_.size());
465  vector_type new_data;
466  vector_type::iterator d_it = data_.begin();
467  std::vector<bool>::const_iterator t_it = _tags.begin();
468  std::vector<bool>::const_iterator t_end = _tags.end();
469  for(; t_it != t_end; ++t_it, ++d_it) {
470  if(!*t_it) {
471  new_data.push_back(*d_it);
472  }
473  }
474  data_.swap(new_data);
475  }
476 
477 private:
478 
479  vector_type data_;
480 
481  const std::string def_;
482 };
483 
484 } // Namespace OpenVolumeMesh
485 
486 //=============================================================================
487 #endif // OPENVOLUMEMESHPROPERTY_HH defined
488 //=============================================================================