OpenMesh

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

- File List
- File Members

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