00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #ifndef OPENMESH_PROPERTY_HH
00027 #define OPENMESH_PROPERTY_HH
00028
00029
00030
00031
00032
00033 #include <OpenMesh/Core/System/config.h>
00034 #include <OpenMesh/Core/Mesh/Handles.hh>
00035 #include <OpenMesh/Core/Utils/BaseProperty.hh>
00036 #include <vector>
00037 #include <string>
00038 #include <algorithm>
00039
00040
00041
00042
00043 namespace OpenMesh {
00044
00045
00046
00065
00066
00067
00068
00069
00070 template <class T>
00071 class PropertyT : public BaseProperty
00072 {
00073 public:
00074
00075 typedef T Value;
00076 typedef std::vector<T> vector_type;
00077 typedef T value_type;
00078 typedef typename vector_type::reference reference;
00079 typedef typename vector_type::const_reference const_reference;
00080
00081 public:
00082
00084 PropertyT(const std::string& _name = "<unknown>")
00085 : BaseProperty(_name)
00086 {}
00087
00089 PropertyT(const PropertyT & _rhs)
00090 : BaseProperty( _rhs ), data_( _rhs.data_ ) {}
00091
00092 public:
00093
00094 virtual void reserve(size_t _n) { data_.reserve(_n); }
00095 virtual void resize(size_t _n) { data_.resize(_n); }
00096 virtual void push_back() { data_.push_back(T()); }
00097 virtual void swap(size_t _i0, size_t _i1)
00098 { std::swap(data_[_i0], data_[_i1]); }
00099
00100 public:
00101
00102 virtual void set_persistent( bool _yn )
00103 { check_and_set_persistent<T>( _yn ); }
00104
00105 virtual size_t n_elements() const { return data_.size(); }
00106 virtual size_t element_size() const { return IO::size_of<T>(); }
00107
00108 #ifndef DOXY_IGNORE_THIS
00109 struct plus {
00110 size_t operator () ( size_t _b, const T& _v )
00111 { return _b + IO::size_of<T>(_v); }
00112 };
00113 #endif
00114
00115 virtual size_t size_of(void) const
00116 {
00117 if (element_size() != IO::UnknownSize)
00118 return this->BaseProperty::size_of(n_elements());
00119 return std::accumulate(data_.begin(), data_.end(), 0, plus());
00120 }
00121
00122 virtual size_t size_of(size_t _n_elem) const
00123 { return this->BaseProperty::size_of(_n_elem); }
00124
00125 virtual size_t store( std::ostream& _ostr, bool _swap ) const
00126 {
00127 if ( IO::is_streamable<vector_type>() )
00128 return IO::store(_ostr, data_, _swap );
00129 size_t bytes = 0;
00130 for (size_t i=0; i<n_elements(); ++i)
00131 bytes += IO::store( _ostr, data_[i], _swap );
00132 return bytes;
00133 }
00134
00135 virtual size_t restore( std::istream& _istr, bool _swap )
00136 {
00137 if ( IO::is_streamable<vector_type>() )
00138 return IO::restore(_istr, data_, _swap );
00139 size_t bytes = 0;
00140 for (size_t i=0; i<n_elements(); ++i)
00141 bytes += IO::restore( _istr, data_[i], _swap );
00142 return bytes;
00143 }
00144
00145 public:
00146
00148 const T* data() const {
00149
00150 if( data_.empty() )
00151 return 0;
00152
00153 return &data_[0];
00154 }
00155
00157 reference operator[](int _idx)
00158 {
00159 assert( size_t(_idx) < data_.size() );
00160 return data_[_idx];
00161 }
00162
00164 const_reference operator[](int _idx) const
00165 {
00166 assert( size_t(_idx) < data_.size());
00167 return data_[_idx];
00168 }
00169
00171 PropertyT<T>* clone() const
00172 {
00173 PropertyT<T>* p = new PropertyT<T>( *this );
00174 return p;
00175 }
00176
00177
00178 private:
00179
00180 vector_type data_;
00181 };
00182
00183
00184
00185
00191 template <>
00192 class PropertyT<bool> : public BaseProperty
00193 {
00194 public:
00195
00196 typedef std::vector<bool> vector_type;
00197 typedef bool value_type;
00198 typedef vector_type::reference reference;
00199 typedef vector_type::const_reference const_reference;
00200
00201 public:
00202
00203 PropertyT(const std::string& _name = "<unknown>")
00204 : BaseProperty(_name)
00205 { }
00206
00207 public:
00208
00209 virtual void reserve(size_t _n) { data_.reserve(_n); }
00210 virtual void resize(size_t _n) { data_.resize(_n); }
00211 virtual void push_back() { data_.push_back(bool()); }
00212 virtual void swap(size_t _i0, size_t _i1)
00213 { bool t(data_[_i0]); data_[_i0]=data_[_i1]; data_[_i1]=t; }
00214
00215 public:
00216
00217 virtual void set_persistent( bool _yn )
00218 {
00219 check_and_set_persistent<bool>( _yn );
00220 }
00221
00222 virtual size_t n_elements() const { return data_.size(); }
00223 virtual size_t element_size() const { return UnknownSize; }
00224 virtual size_t size_of() const { return size_of( n_elements() ); }
00225 virtual size_t size_of(size_t _n_elem) const
00226 {
00227 return _n_elem / 8 + ((_n_elem % 8)!=0);
00228 }
00229
00230 size_t store( std::ostream& _ostr, bool ) const
00231 {
00232 size_t bytes = 0;
00233
00234 size_t N = data_.size() / 8;
00235 size_t R = data_.size() % 8;
00236
00237 size_t idx;
00238 size_t bidx;
00239 unsigned char bits;
00240
00241 for (bidx=idx=0; idx < N; ++idx, bidx+=8)
00242 {
00243 bits = !!data_[bidx]
00244 | (!!data_[bidx+1] << 1)
00245 | (!!data_[bidx+2] << 2)
00246 | (!!data_[bidx+3] << 3)
00247 | (!!data_[bidx+4] << 4)
00248 | (!!data_[bidx+5] << 5)
00249 | (!!data_[bidx+6] << 6)
00250 | (!!data_[bidx+7] << 7);
00251 _ostr << bits;
00252 }
00253 bytes = N;
00254
00255 if (R)
00256 {
00257 bits = 0;
00258 for (idx=0; idx < R; ++idx)
00259 bits |= !!data_[bidx+idx] << idx;
00260 _ostr << bits;
00261 ++bytes;
00262 }
00263
00264 std::cout << std::endl;
00265
00266 assert( bytes == size_of() );
00267
00268 return bytes;
00269 }
00270
00271 size_t restore( std::istream& _istr, bool )
00272 {
00273 size_t bytes = 0;
00274
00275 size_t N = data_.size() / 8;
00276 size_t R = data_.size() % 8;
00277
00278 size_t idx;
00279 size_t bidx;
00280 unsigned char bits;
00281
00282 for (bidx=idx=0; idx < N; ++idx, bidx+=8)
00283 {
00284 _istr >> bits;
00285 data_[bidx+0] = !!(bits & 0x01);
00286 data_[bidx+1] = !!(bits & 0x02);
00287 data_[bidx+2] = !!(bits & 0x04);
00288 data_[bidx+3] = !!(bits & 0x08);
00289 data_[bidx+4] = !!(bits & 0x10);
00290 data_[bidx+5] = !!(bits & 0x20);
00291 data_[bidx+6] = !!(bits & 0x40);
00292 data_[bidx+7] = !!(bits & 0x80);
00293 }
00294 bytes = N;
00295
00296 if (R)
00297 {
00298 _istr >> bits;
00299 for (idx=0; idx < R; ++idx)
00300 data_[bidx+idx] = !!(bits & (1<<idx));
00301 ++bytes;
00302 }
00303
00304 std::cout << std::endl;
00305
00306 return bytes;
00307 }
00308
00309
00310 public:
00311
00313 reference operator[](int _idx)
00314 {
00315 assert( size_t(_idx) < data_.size() );
00316 return data_[_idx];
00317 }
00318
00320 const_reference operator[](int _idx) const
00321 {
00322 assert( size_t(_idx) < data_.size());
00323 return data_[_idx];
00324 }
00325
00327 PropertyT<bool>* clone() const
00328 {
00329 PropertyT<bool>* p = new PropertyT<bool>( *this );
00330 return p;
00331 }
00332
00333
00334 private:
00335
00336 vector_type data_;
00337 };
00338
00339
00340
00341
00342
00347 template <>
00348 class PropertyT<std::string> : public BaseProperty
00349 {
00350 public:
00351
00352 typedef std::string Value;
00353 typedef std::vector<std::string> vector_type;
00354 typedef std::string value_type;
00355 typedef vector_type::reference reference;
00356 typedef vector_type::const_reference const_reference;
00357
00358 public:
00359
00360 PropertyT(const std::string& _name = "<unknown>")
00361 : BaseProperty(_name)
00362 { }
00363
00364 public:
00365
00366 virtual void reserve(size_t _n) { data_.reserve(_n); }
00367 virtual void resize(size_t _n) { data_.resize(_n); }
00368 virtual void push_back() { data_.push_back(std::string()); }
00369 virtual void swap(size_t _i0, size_t _i1) {
00370 std::swap(data_[_i0], data_[_i1]);
00371 }
00372
00373 public:
00374
00375 virtual void set_persistent( bool _yn )
00376 { check_and_set_persistent<std::string>( _yn ); }
00377
00378 virtual size_t n_elements() const { return data_.size(); }
00379 virtual size_t element_size() const { return UnknownSize; }
00380 virtual size_t size_of() const
00381 { return IO::size_of( data_ ); }
00382
00383 virtual size_t size_of(size_t ) const
00384 { return UnknownSize; }
00385
00387 size_t store( std::ostream& _ostr, bool _swap ) const
00388 { return IO::store( _ostr, data_, _swap ); }
00389
00390 size_t restore( std::istream& _istr, bool _swap )
00391 { return IO::restore( _istr, data_, _swap ); }
00392
00393 public:
00394
00395 const value_type* data() const {
00396 if( data_.empty() )
00397 return 0;
00398
00399 return (value_type*) &data_[0];
00400 }
00401
00403 reference operator[](int _idx) {
00404 assert( size_t(_idx) < data_.size());
00405 return ((value_type*) &data_[0])[_idx];
00406 }
00407
00409 const_reference operator[](int _idx) const {
00410 assert( size_t(_idx) < data_.size());
00411 return ((value_type*) &data_[0])[_idx];
00412 }
00413
00414 PropertyT<value_type>* clone() const {
00415 PropertyT<value_type>* p = new PropertyT<value_type>( *this );
00416 return p;
00417 }
00418
00419
00420 private:
00421
00422 vector_type data_;
00423
00424 };
00425
00427 template <class T>
00428 struct BasePropHandleT : public BaseHandle
00429 {
00430 typedef T Value;
00431 typedef std::vector<T> vector_type;
00432 typedef T value_type;
00433 typedef typename vector_type::reference reference;
00434 typedef typename vector_type::const_reference const_reference;
00435
00436 explicit BasePropHandleT(int _idx=-1) : BaseHandle(_idx) {}
00437 };
00438
00439
00443 template <class T>
00444 struct VPropHandleT : public BasePropHandleT<T>
00445 {
00446 typedef T Value;
00447 typedef T value_type;
00448
00449 explicit VPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
00450 explicit VPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
00451 };
00452
00453
00457 template <class T>
00458 struct HPropHandleT : public BasePropHandleT<T>
00459 {
00460 typedef T Value;
00461 typedef T value_type;
00462
00463 explicit HPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
00464 explicit HPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
00465 };
00466
00467
00471 template <class T>
00472 struct EPropHandleT : public BasePropHandleT<T>
00473 {
00474 typedef T Value;
00475 typedef T value_type;
00476
00477 explicit EPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
00478 explicit EPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
00479 };
00480
00481
00485 template <class T>
00486 struct FPropHandleT : public BasePropHandleT<T>
00487 {
00488 typedef T Value;
00489 typedef T value_type;
00490
00491 explicit FPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
00492 explicit FPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
00493 };
00494
00495
00499 template <class T>
00500 struct MPropHandleT : public BasePropHandleT<T>
00501 {
00502 typedef T Value;
00503 typedef T value_type;
00504
00505 explicit MPropHandleT(int _idx=-1) : BasePropHandleT<T>(_idx) {}
00506 explicit MPropHandleT(const BasePropHandleT<T>& _b) : BasePropHandleT<T>(_b) {}
00507 };
00508
00509 }
00510
00511 #endif // OPENMESH_PROPERTY_HH defined
00512