OpenMesh

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BaseKernel.hh
1 /*===========================================================================*\
2 * *
3 * OpenMesh *
4 * Copyright (C) 2001-2012 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: 840 $ *
38 * $Date: 2013-07-22 17:06:21 +0200 (Mo, 22. Jul 2013) $ *
39 * *
40 \*===========================================================================*/
41
42
43 //=============================================================================
44 //
45 // CLASS BaseKernel
46 //
47 //=============================================================================
48
49
50 #ifndef OPENMESH_BASE_KERNEL_HH
51 #define OPENMESH_BASE_KERNEL_HH
52
53
54 //== INCLUDES =================================================================
55
56
57 #include <OpenMesh/Core/System/config.h>
58 // --------------------
59 #include <vector>
60 #include <string>
61 #include <algorithm>
62 // --------------------
63 #include <OpenMesh/Core/Utils/PropertyContainer.hh>
64
65
66 //== NAMESPACES ===============================================================
67
68
69 namespace OpenMesh {
70
71
72 //== CLASS DEFINITION =========================================================
73
90
91 class OPENMESHDLLEXPORT BaseKernel
92 {
93 public: //-------------------------------------------- constructor / destructor
94
95 BaseKernel() {}
96 virtual ~BaseKernel() {
97 vprops_.clear();
98 eprops_.clear();
99 hprops_.clear();
100 fprops_.clear();
101 }
102
103
104 public: //-------------------------------------------------- add new properties
105
107
109
129 template <class T>
130 void add_property( VPropHandleT<T>& _ph, const std::string& _name="<vprop>")
131 {
132 _ph = VPropHandleT<T>( vprops_.add(T(), _name) );
133 vprops_.resize(n_vertices());
134 }
135
136 template <class T>
137 void add_property( HPropHandleT<T>& _ph, const std::string& _name="<hprop>")
138 {
139 _ph = HPropHandleT<T>( hprops_.add(T(), _name) );
140 hprops_.resize(n_halfedges());
141 }
142
143 template <class T>
144 void add_property( EPropHandleT<T>& _ph, const std::string& _name="<eprop>")
145 {
146 _ph = EPropHandleT<T>( eprops_.add(T(), _name) );
147 eprops_.resize(n_edges());
148 }
149
150 template <class T>
151 void add_property( FPropHandleT<T>& _ph, const std::string& _name="<fprop>")
152 {
153 _ph = FPropHandleT<T>( fprops_.add(T(), _name) );
154 fprops_.resize(n_faces());
155 }
156
157 template <class T>
158 void add_property( MPropHandleT<T>& _ph, const std::string& _name="<mprop>")
159 {
160 _ph = MPropHandleT<T>( mprops_.add(T(), _name) );
161 mprops_.resize(1);
162 }
163
165
166
167 public: //--------------------------------------------------- remove properties
168
170
171
179 template <typename T>
180 void remove_property(VPropHandleT<T>& _ph)
181 {
182 if (_ph.is_valid())
183 vprops_.remove(_ph);
184 _ph.reset();
185 }
186
187 template <typename T>
188 void remove_property(HPropHandleT<T>& _ph)
189 {
190 if (_ph.is_valid())
191 hprops_.remove(_ph);
192 _ph.reset();
193 }
194
195 template <typename T>
196 void remove_property(EPropHandleT<T>& _ph)
197 {
198 if (_ph.is_valid())
199 eprops_.remove(_ph);
200 _ph.reset();
201 }
202
203 template <typename T>
204 void remove_property(FPropHandleT<T>& _ph)
205 {
206 if (_ph.is_valid())
207 fprops_.remove(_ph);
208 _ph.reset();
209 }
210
211 template <typename T>
212 void remove_property(MPropHandleT<T>& _ph)
213 {
214 if (_ph.is_valid())
215 mprops_.remove(_ph);
216 _ph.reset();
217 }
218
220
221 public: //------------------------------------------------ get handle from name
222
224
225
234 template <class T>
235 bool get_property_handle(VPropHandleT<T>& _ph,
236 const std::string& _name) const
237 {
238 return (_ph = VPropHandleT<T>(vprops_.handle(T(), _name))).is_valid();
239 }
240
241 template <class T>
242 bool get_property_handle(HPropHandleT<T>& _ph,
243 const std::string& _name) const
244 {
245 return (_ph = HPropHandleT<T>(hprops_.handle(T(), _name))).is_valid();
246 }
247
248 template <class T>
249 bool get_property_handle(EPropHandleT<T>& _ph,
250 const std::string& _name) const
251 {
252 return (_ph = EPropHandleT<T>(eprops_.handle(T(), _name))).is_valid();
253 }
254
255 template <class T>
256 bool get_property_handle(FPropHandleT<T>& _ph,
257 const std::string& _name) const
258 {
259 return (_ph = FPropHandleT<T>(fprops_.handle(T(), _name))).is_valid();
260 }
261
262 template <class T>
263 bool get_property_handle(MPropHandleT<T>& _ph,
264 const std::string& _name) const
265 {
266 return (_ph = MPropHandleT<T>(mprops_.handle(T(), _name))).is_valid();
267 }
268
270
271 public: //--------------------------------------------------- access properties
272
274
275
285 template <class T>
286 PropertyT<T>& property(VPropHandleT<T> _ph) {
287 return vprops_.property(_ph);
288 }
289 template <class T>
290 const PropertyT<T>& property(VPropHandleT<T> _ph) const {
291 return vprops_.property(_ph);
292 }
293
294 template <class T>
295 PropertyT<T>& property(HPropHandleT<T> _ph) {
296 return hprops_.property(_ph);
297 }
298 template <class T>
299 const PropertyT<T>& property(HPropHandleT<T> _ph) const {
300 return hprops_.property(_ph);
301 }
302
303 template <class T>
304 PropertyT<T>& property(EPropHandleT<T> _ph) {
305 return eprops_.property(_ph);
306 }
307 template <class T>
308 const PropertyT<T>& property(EPropHandleT<T> _ph) const {
309 return eprops_.property(_ph);
310 }
311
312 template <class T>
313 PropertyT<T>& property(FPropHandleT<T> _ph) {
314 return fprops_.property(_ph);
315 }
316 template <class T>
317 const PropertyT<T>& property(FPropHandleT<T> _ph) const {
318 return fprops_.property(_ph);
319 }
320
321 template <class T>
322 PropertyT<T>& mproperty(MPropHandleT<T> _ph) {
323 return mprops_.property(_ph);
324 }
325 template <class T>
326 const PropertyT<T>& mproperty(MPropHandleT<T> _ph) const {
327 return mprops_.property(_ph);
328 }
329
331
332 public: //-------------------------------------------- access property elements
333
335
336
340 template <class T>
341 typename VPropHandleT<T>::reference
342 property(VPropHandleT<T> _ph, VertexHandle _vh) {
343 return vprops_.property(_ph)[_vh.idx()];
344 }
345
346 template <class T>
347 typename VPropHandleT<T>::const_reference
348 property(VPropHandleT<T> _ph, VertexHandle _vh) const {
349 return vprops_.property(_ph)[_vh.idx()];
350 }
351
352
353 template <class T>
354 typename HPropHandleT<T>::reference
355 property(HPropHandleT<T> _ph, HalfedgeHandle _hh) {
356 return hprops_.property(_ph)[_hh.idx()];
357 }
358
359 template <class T>
360 typename HPropHandleT<T>::const_reference
361 property(HPropHandleT<T> _ph, HalfedgeHandle _hh) const {
362 return hprops_.property(_ph)[_hh.idx()];
363 }
364
365
366 template <class T>
367 typename EPropHandleT<T>::reference
368 property(EPropHandleT<T> _ph, EdgeHandle _eh) {
369 return eprops_.property(_ph)[_eh.idx()];
370 }
371
372 template <class T>
373 typename EPropHandleT<T>::const_reference
374 property(EPropHandleT<T> _ph, EdgeHandle _eh) const {
375 return eprops_.property(_ph)[_eh.idx()];
376 }
377
378
379 template <class T>
380 typename FPropHandleT<T>::reference
381 property(FPropHandleT<T> _ph, FaceHandle _fh) {
382 return fprops_.property(_ph)[_fh.idx()];
383 }
384
385 template <class T>
386 typename FPropHandleT<T>::const_reference
387 property(FPropHandleT<T> _ph, FaceHandle _fh) const {
388 return fprops_.property(_ph)[_fh.idx()];
389 }
390
391
392 template <class T>
393 typename MPropHandleT<T>::reference
394 property(MPropHandleT<T> _ph) {
395 return mprops_.property(_ph)[0];
396 }
397
398 template <class T>
399 typename MPropHandleT<T>::const_reference
400 property(MPropHandleT<T> _ph) const {
401 return mprops_.property(_ph)[0];
402 }
403
405
406
407 public: //------------------------------------------------ copy property
408
415 template <class T>
416 void copy_property(VPropHandleT<T>& _ph, VertexHandle _vh_from, VertexHandle _vh_to) {
417 if(_vh_from.is_valid() && _vh_to.is_valid())
418 vprops_.property(_ph)[_vh_to.idx()] = vprops_.property(_ph)[_vh_from.idx()];
419 }
420
427 template <class T>
428 void copy_property(HPropHandleT<T> _ph, HalfedgeHandle _hh_from, HalfedgeHandle _hh_to) {
429 if(_hh_from.is_valid() && _hh_to.is_valid())
430 hprops_.property(_ph)[_hh_to.idx()] = hprops_.property(_ph)[_hh_from.idx()];
431 }
432
439 template <class T>
440 void copy_property(EPropHandleT<T> _ph, EdgeHandle _eh_from, EdgeHandle _eh_to) {
441 if(_eh_from.is_valid() && _eh_to.is_valid())
442 eprops_.property(_ph)[_eh_to.idx()] = eprops_.property(_ph)[_eh_from.idx()];
443 }
444
451 template <class T>
452 void copy_property(FPropHandleT<T> _ph, FaceHandle _fh_from, FaceHandle _fh_to) {
453 if(_fh_from.is_valid() && _fh_to.is_valid())
454 fprops_.property(_ph)[_fh_to.idx()] = fprops_.property(_ph)[_fh_from.idx()];
455 }
456
457
458 public:
459 //------------------------------------------------ copy all properties
460
468 void copy_all_properties(VertexHandle _vh_from, VertexHandle _vh_to, bool _copyBuildIn = false) {
469
470 for( PropertyContainer::iterator p_it = vprops_.begin();
471 p_it != vprops_.end(); ++p_it) {
472
473 // Copy all properties, if build in is true
474 // Otherwise, copy only properties without build in specifier
475 if ( _copyBuildIn || (*p_it)->name().substr(0,2) != "v:")
476 (*p_it)->copy(_vh_from.idx(), _vh_to.idx());
477
478 }
479 }
480
487 void copy_all_properties(HalfedgeHandle _hh_from, HalfedgeHandle _hh_to, bool _copyBuildIn = false) {
488
489 for( PropertyContainer::iterator p_it = hprops_.begin();
490 p_it != hprops_.end(); ++p_it) {
491
492 // Copy all properties, if build in is true
493 // Otherwise, copy only properties without build in specifier
494 if ( _copyBuildIn || (*p_it)->name().substr(0,2) != "h:")
495 (*p_it)->copy(_hh_from.idx(), _hh_to.idx());
496
497 }
498 }
499
506 void copy_all_properties(EdgeHandle _eh_from, EdgeHandle _eh_to, bool _copyBuildIn = false) {
507 for( PropertyContainer::iterator p_it = eprops_.begin();
508 p_it != eprops_.end(); ++p_it) {
509
510 // Copy all properties, if build in is true
511 // Otherwise, copy only properties without build in specifier
512 if ( _copyBuildIn || (*p_it)->name().substr(0,2) != "e:")
513 (*p_it)->copy(_eh_from.idx(), _eh_to.idx());
514
515 }
516 }
517
525 void copy_all_properties(FaceHandle _fh_from, FaceHandle _fh_to, bool _copyBuildIn = false) {
526
527 for( PropertyContainer::iterator p_it = fprops_.begin();
528 p_it != fprops_.end(); ++p_it) {
529
530 // Copy all properties, if build in is true
531 // Otherwise, copy only properties without build in specifier
532 if ( _copyBuildIn || (*p_it)->name().substr(0,2) != "f:")
533 (*p_it)->copy(_fh_from.idx(), _fh_to.idx());
534 }
535
536 }
537
538 protected: //------------------------------------------------- low-level access
539
540 public: // used by non-native kernel and MeshIO, should be protected
541
542 size_t n_vprops(void) const { return vprops_.size(); }
543
544 size_t n_eprops(void) const { return eprops_.size(); }
545
546 size_t n_hprops(void) const { return hprops_.size(); }
547
548 size_t n_fprops(void) const { return fprops_.size(); }
549
550 size_t n_mprops(void) const { return mprops_.size(); }
551
552 BaseProperty* _get_vprop( const std::string& _name)
553 { return vprops_.property(_name); }
554
555 BaseProperty* _get_eprop( const std::string& _name)
556 { return eprops_.property(_name); }
557
558 BaseProperty* _get_hprop( const std::string& _name)
559 { return hprops_.property(_name); }
560
561 BaseProperty* _get_fprop( const std::string& _name)
562 { return fprops_.property(_name); }
563
564 BaseProperty* _get_mprop( const std::string& _name)
565 { return mprops_.property(_name); }
566
567 const BaseProperty* _get_vprop( const std::string& _name) const
568 { return vprops_.property(_name); }
569
570 const BaseProperty* _get_eprop( const std::string& _name) const
571 { return eprops_.property(_name); }
572
573 const BaseProperty* _get_hprop( const std::string& _name) const
574 { return hprops_.property(_name); }
575
576 const BaseProperty* _get_fprop( const std::string& _name) const
577 { return fprops_.property(_name); }
578
579 const BaseProperty* _get_mprop( const std::string& _name) const
580 { return mprops_.property(_name); }
581
582 BaseProperty& _vprop( size_t _idx ) { return vprops_._property( _idx ); }
583 BaseProperty& _eprop( size_t _idx ) { return eprops_._property( _idx ); }
584 BaseProperty& _hprop( size_t _idx ) { return hprops_._property( _idx ); }
585 BaseProperty& _fprop( size_t _idx ) { return fprops_._property( _idx ); }
586 BaseProperty& _mprop( size_t _idx ) { return mprops_._property( _idx ); }
587
588 const BaseProperty& _vprop( size_t _idx ) const
589 { return vprops_._property( _idx ); }
590 const BaseProperty& _eprop( size_t _idx ) const
591 { return eprops_._property( _idx ); }
592 const BaseProperty& _hprop( size_t _idx ) const
593 { return hprops_._property( _idx ); }
594 const BaseProperty& _fprop( size_t _idx ) const
595 { return fprops_._property( _idx ); }
596 const BaseProperty& _mprop( size_t _idx ) const
597 { return mprops_._property( _idx ); }
598
599 size_t _add_vprop( BaseProperty* _bp ) { return vprops_._add( _bp ); }
600 size_t _add_eprop( BaseProperty* _bp ) { return eprops_._add( _bp ); }
601 size_t _add_hprop( BaseProperty* _bp ) { return hprops_._add( _bp ); }
602 size_t _add_fprop( BaseProperty* _bp ) { return fprops_._add( _bp ); }
603 size_t _add_mprop( BaseProperty* _bp ) { return mprops_._add( _bp ); }
604
605 protected: // low-level access non-public
606
607 BaseProperty& _vprop( BaseHandle _h )
608 { return vprops_._property( _h.idx() ); }
609 BaseProperty& _eprop( BaseHandle _h )
610 { return eprops_._property( _h.idx() ); }
611 BaseProperty& _hprop( BaseHandle _h )
612 { return hprops_._property( _h.idx() ); }
613 BaseProperty& _fprop( BaseHandle _h )
614 { return fprops_._property( _h.idx() ); }
615 BaseProperty& _mprop( BaseHandle _h )
616 { return mprops_._property( _h.idx() ); }
617
618 const BaseProperty& _vprop( BaseHandle _h ) const
619 { return vprops_._property( _h.idx() ); }
620 const BaseProperty& _eprop( BaseHandle _h ) const
621 { return eprops_._property( _h.idx() ); }
622 const BaseProperty& _hprop( BaseHandle _h ) const
623 { return hprops_._property( _h.idx() ); }
624 const BaseProperty& _fprop( BaseHandle _h ) const
625 { return fprops_._property( _h.idx() ); }
626 const BaseProperty& _mprop( BaseHandle _h ) const
627 { return mprops_._property( _h.idx() ); }
628
629
630 public: //----------------------------------------------------- element numbers
631
632
633 virtual size_t n_vertices() const { return 0; }
634 virtual size_t n_halfedges() const { return 0; }
635 virtual size_t n_edges() const { return 0; }
636 virtual size_t n_faces() const { return 0; }
637
638
639 protected: //------------------------------------------- synchronize properties
640
641 void vprops_reserve(size_t _n) const { vprops_.reserve(_n); }
642 void vprops_resize(size_t _n) const { vprops_.resize(_n); }
643 void vprops_clear() {
644 vprops_.clear();
645 }
646 void vprops_swap(unsigned int _i0, unsigned int _i1) const {
647 vprops_.swap(_i0, _i1);
648 }
649
650 void hprops_reserve(size_t _n) const { hprops_.reserve(_n); }
651 void hprops_resize(size_t _n) const { hprops_.resize(_n); }
652 void hprops_clear() {
653 hprops_.clear();
654 }
655 void hprops_swap(unsigned int _i0, unsigned int _i1) const {
656 hprops_.swap(_i0, _i1);
657 }
658
659 void eprops_reserve(size_t _n) const { eprops_.reserve(_n); }
660 void eprops_resize(size_t _n) const { eprops_.resize(_n); }
661 void eprops_clear() {
662 eprops_.clear();
663 }
664 void eprops_swap(unsigned int _i0, unsigned int _i1) const {
665 eprops_.swap(_i0, _i1);
666 }
667
668 void fprops_reserve(size_t _n) const { fprops_.reserve(_n); }
669 void fprops_resize(size_t _n) const { fprops_.resize(_n); }
670 void fprops_clear() {
671 fprops_.clear();
672 }
673 void fprops_swap(unsigned int _i0, unsigned int _i1) const {
674 fprops_.swap(_i0, _i1);
675 }
676
677 void mprops_resize(size_t _n) const { mprops_.resize(_n); }
678 void mprops_clear() {
679 mprops_.clear();
680 }
681
682 public:
683
684 void property_stats(std::ostream& _ostr = std::clog) const;
685
686 void vprop_stats( std::string& _string ) const;
687 void hprop_stats( std::string& _string ) const;
688 void eprop_stats( std::string& _string ) const;
689 void fprop_stats( std::string& _string ) const;
690 void mprop_stats( std::string& _string ) const;
691
692 void vprop_stats(std::ostream& _ostr = std::clog) const;
693 void hprop_stats(std::ostream& _ostr = std::clog) const;
694 void eprop_stats(std::ostream& _ostr = std::clog) const;
695 void fprop_stats(std::ostream& _ostr = std::clog) const;
696 void mprop_stats(std::ostream& _ostr = std::clog) const;
697
698 public:
699
700 typedef PropertyContainer::iterator prop_iterator;
701 typedef PropertyContainer::const_iterator const_prop_iterator;
702
703 prop_iterator vprops_begin() { return vprops_.begin(); }
704 prop_iterator vprops_end() { return vprops_.end(); }
705 const_prop_iterator vprops_begin() const { return vprops_.begin(); }
706 const_prop_iterator vprops_end() const { return vprops_.end(); }
707
708 prop_iterator eprops_begin() { return eprops_.begin(); }
709 prop_iterator eprops_end() { return eprops_.end(); }
710 const_prop_iterator eprops_begin() const { return eprops_.begin(); }
711 const_prop_iterator eprops_end() const { return eprops_.end(); }
712
713 prop_iterator hprops_begin() { return hprops_.begin(); }
714 prop_iterator hprops_end() { return hprops_.end(); }
715 const_prop_iterator hprops_begin() const { return hprops_.begin(); }
716 const_prop_iterator hprops_end() const { return hprops_.end(); }
717
718 prop_iterator fprops_begin() { return fprops_.begin(); }
719 prop_iterator fprops_end() { return fprops_.end(); }
720 const_prop_iterator fprops_begin() const { return fprops_.begin(); }
721 const_prop_iterator fprops_end() const { return fprops_.end(); }
722
723 prop_iterator mprops_begin() { return mprops_.begin(); }
724 prop_iterator mprops_end() { return mprops_.end(); }
725 const_prop_iterator mprops_begin() const { return mprops_.begin(); }
726 const_prop_iterator mprops_end() const { return mprops_.end(); }
727
728 private:
729
730 PropertyContainer vprops_;
731 PropertyContainer hprops_;
732 PropertyContainer eprops_;
733 PropertyContainer fprops_;
734 PropertyContainer mprops_;
735 };
736
737
738 //=============================================================================
739 } // namespace OpenMesh
740 //=============================================================================
741 #endif // OPENMESH_BASE_KERNEL_HH defined
742 //=============================================================================