OpenVolumeMesh
Loading...
Searching...
No Matches
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
59namespace OpenVolumeMesh {
60
61//== CLASS DEFINITION =========================================================
62
69
70template<class T>
71class OpenVolumeMeshPropertyT: public OpenVolumeMeshBaseProperty {
72public:
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
82public:
83
85 OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const T _def = T()) :
86 OpenVolumeMeshBaseProperty(_name),
87 def_(_def) {
88 }
89
92 OpenVolumeMeshBaseProperty(_rhs),
93 data_(_rhs.data_),
94 def_(_rhs.def_) {
95 }
96
97public:
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
120public:
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
162public:
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
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
206protected:
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
224private:
225
226 vector_type data_;
227
228 const T def_;
229};
230
231//-----------------------------------------------------------------------------
232
233
237template<>
238class OpenVolumeMeshPropertyT<bool> : public OpenVolumeMeshBaseProperty {
239public:
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
248public:
249
250 OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const bool _def = bool()) :
251 OpenVolumeMeshBaseProperty(_name),
252 def_(_def) {
253 }
254
255public:
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
281public:
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
313public:
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
328 OpenVolumeMeshPropertyT<bool>* clone() const {
329 OpenVolumeMeshPropertyT<bool>* p = new OpenVolumeMeshPropertyT<bool> (
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
342protected:
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
360private:
361
362 vector_type data_;
363
364 const bool def_;
365};
366
367//-----------------------------------------------------------------------------
368
369
373template<>
374class OpenVolumeMeshPropertyT<std::string> : public OpenVolumeMeshBaseProperty {
375public:
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
385public:
386
387 OpenVolumeMeshPropertyT(const std::string& _name = "<unknown>", const std::string& _def = "") :
388 OpenVolumeMeshBaseProperty(_name),
389 def_(_def) {
390 }
391
392public:
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
416public:
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
454public:
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
475 OpenVolumeMeshPropertyT<value_type>* clone() const {
476 OpenVolumeMeshPropertyT<value_type>* p = new OpenVolumeMeshPropertyT<
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
489protected:
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
507private:
508
509 vector_type data_;
510
511 const std::string def_;
512};
513
514} // Namespace OpenVolumeMesh
515
516//=============================================================================
517#endif // OPENVOLUMEMESHPROPERTY_HH defined
518//=============================================================================
static const size_t UnknownSize
Indicates an error when a size is returned by a member.
Definition OpenVolumeMeshBaseProperty.hh:70
virtual size_t size_of() const
Return size of property in bytes.
Definition OpenVolumeMeshBaseProperty.hh:130
OpenVolumeMeshPropertyT< bool > * clone() const
Make a copy of self.
Definition OpenVolumeMeshProperty.hh:328
void delete_element(size_t _idx)
Erase an element of the vector.
Definition OpenVolumeMeshProperty.hh:277
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition OpenVolumeMeshProperty.hh:261
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition OpenVolumeMeshProperty.hh:258
const_reference operator[](int _idx) const
Const access to the i'th element. No range check is performed!
Definition OpenVolumeMeshProperty.hh:322
virtual size_t size_of(size_t _n_elem) const
Definition OpenVolumeMeshProperty.hh:292
virtual size_t size_of() const
Return size of property in bytes.
Definition OpenVolumeMeshProperty.hh:289
virtual size_t n_elements() const
Number of elements in property.
Definition OpenVolumeMeshProperty.hh:283
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition OpenVolumeMeshProperty.hh:271
virtual void push_back()
Extend the number of elements by one.
Definition OpenVolumeMeshProperty.hh:268
virtual void clear()
Clear all elements and free memory.
Definition OpenVolumeMeshProperty.hh:264
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition OpenVolumeMeshProperty.hh:286
reference operator[](int _idx)
Access the i'th element. No range check is performed!
Definition OpenVolumeMeshProperty.hh:316
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
Definition OpenVolumeMeshProperty.hh:345
OpenVolumeMeshPropertyT< value_type > * clone() const
Return a deep copy of self.
Definition OpenVolumeMeshProperty.hh:475
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition OpenVolumeMeshProperty.hh:398
virtual size_t size_of() const
Return size of property in bytes.
Definition OpenVolumeMeshProperty.hh:424
virtual void delete_element(size_t _idx)
Erase an element of the vector.
Definition OpenVolumeMeshProperty.hh:412
virtual size_t size_of(size_t) const
Definition OpenVolumeMeshProperty.hh:428
virtual void push_back()
Extend the number of elements by one.
Definition OpenVolumeMeshProperty.hh:405
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition OpenVolumeMeshProperty.hh:408
const_reference operator[](int _idx) const
Const access the i'th element. No range check is performed!
Definition OpenVolumeMeshProperty.hh:470
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
Definition OpenVolumeMeshProperty.hh:492
reference operator[](int _idx)
Access the i'th element. No range check is performed!
Definition OpenVolumeMeshProperty.hh:464
virtual void clear()
Clear all elements and free memory.
Definition OpenVolumeMeshProperty.hh:401
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition OpenVolumeMeshProperty.hh:421
virtual size_t n_elements() const
Number of elements in property.
Definition OpenVolumeMeshProperty.hh:418
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition OpenVolumeMeshProperty.hh:395
OpenVolumeMeshPropertyT< T > * clone() const
Make a copy of self.
Definition OpenVolumeMeshProperty.hh:193
virtual void clear()
Clear all elements and free memory.
Definition OpenVolumeMeshProperty.hh:105
void delete_element(size_t _idx)
Erase an element of the vector.
Definition OpenVolumeMeshProperty.hh:116
virtual void resize(size_t _n)
Resize storage to hold n elements.
Definition OpenVolumeMeshProperty.hh:102
virtual void reserve(size_t _n)
Reserve memory for n elements.
Definition OpenVolumeMeshProperty.hh:99
virtual void delete_multiple_entries(const std::vector< bool > &_tags)
Delete multiple entries in list.
Definition OpenVolumeMeshProperty.hh:209
OpenVolumeMeshPropertyT(const std::string &_name="<unknown>", const T _def=T())
Default constructor.
Definition OpenVolumeMeshProperty.hh:85
const_reference operator[](int _idx) const
Const access to the i'th element. No range check is performed!
Definition OpenVolumeMeshProperty.hh:187
reference operator[](int _idx)
Access the i'th element. No range check is performed!
Definition OpenVolumeMeshProperty.hh:181
virtual void swap(size_t _i0, size_t _i1)
Let two elements swap their storage place.
Definition OpenVolumeMeshProperty.hh:112
OpenVolumeMeshPropertyT(const OpenVolumeMeshPropertyT &_rhs)
Copy constructor.
Definition OpenVolumeMeshProperty.hh:91
virtual size_t element_size() const
Size of one element in bytes or UnknownSize if not known.
Definition OpenVolumeMeshProperty.hh:125
virtual void push_back()
Extend the number of elements by one.
Definition OpenVolumeMeshProperty.hh:109
virtual size_t size_of() const
Return size of property in bytes.
Definition OpenVolumeMeshProperty.hh:137
virtual size_t n_elements() const
Number of elements in property.
Definition OpenVolumeMeshProperty.hh:122
vector_type & data_vector()
Get reference to property vector (be careful, improper usage, e.g. resizing, may crash)
Definition OpenVolumeMeshProperty.hh:175
const T * data() const
Get pointer to array (does not work for T==bool)
Definition OpenVolumeMeshProperty.hh:166
virtual size_t size_of(size_t _n_elem) const
Definition OpenVolumeMeshProperty.hh:143
Definition OpenVolumeMeshProperty.hh:130