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
AttribKernelT.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 #ifndef OPENMESH_KERNEL_OSG_ATTRIBKERNEL_HH
43 #define OPENMESH_KENREL_OSG_ATTRIBKERNEL_HH
44
45
46 //== INCLUDES =================================================================
47
48 #include <OpenMesh/Core/Utils/GenProg.hh>
49 #include <OpenMesh/Core/Mesh/Attributes.hh>
50 // --------------------
51 #include <OpenMesh/Tools/Kernel_OSG/PropertyT.hh>
52 #include <OpenMesh/Tools/Kernel_OSG/PropertyKernel.hh>
53 // --------------------
54 #include <osg/Geometry>
55
56
57 //== NAMESPACES ===============================================================
58
59 namespace OpenMesh {
60 namespace Kernel_OSG {
61
62 //== CLASS DEFINITION =========================================================
63
64
65
67 template <class MeshItems>
68 class AttribKernelT
69 : public PropertyKernel< typename MeshItems::Face::IsTriangle >
70 {
71 protected:
72
73 typedef typename MeshItems::Face::IsTriangle IsTriMesh;
74 typedef PropertyKernel< IsTriMesh > Base;
75
76 typedef typename Base::FPTypesHandle FPTypesHandle;
77 typedef typename Base::FPLengthsHandle FPLengthsHandle;
78 typedef typename Base::FIndicesHandle FIndicesHandle;
79
80 public:
81
82 //---------------------------------------------------------------- item types
83
84 typedef typename MeshItems::Vertex Vertex;
85 typedef typename MeshItems::Halfedge Halfedge;
86 typedef typename MeshItems::Edge Edge;
87 typedef typename MeshItems::Face Face;
88
89 typedef typename MeshItems::Point Point;
90 typedef typename MeshItems::Normal Normal;
91 typedef typename MeshItems::Color Color;
92 typedef typename MeshItems::TexCoord TexCoord;
93
94 typedef typename MeshItems::Scalar Scalar;
95
96 typedef Attributes::StatusInfo StatusInfo;
97
98
99 enum Attribs {
100 VAttribs = MeshItems::VAttribs,
101 HAttribs = MeshItems::HAttribs,
102 EAttribs = MeshItems::EAttribs,
103 FAttribs = MeshItems::FAttribs,
104 };
105
106 typedef GenProg::Bool2Type<(bool)(HAttribs & Attributes::PrevHalfedge)>
107 HasPrevHalfedge;
108
109 //
110
111 typedef typename _t2vp< Point >::prop GeoPositions;
112 typedef typename _t2vn< Normal >::prop GeoNormals;
113 typedef typename _t2vc< Color >::prop GeoColors;
114 typedef typename _t2vtc< TexCoord >::prop GeoTexCoords;
115
116 // typedef typename Base::GeoPTypes GeoPTypes;
117 // typedef typename Base::GeoPLengths GeoPLengths;
118 // typedef typename Base::GeoIndices GeoIndices;
119
120 //-------------------------------------------------- constructor / destructor
121
122 AttribKernelT() :
123
124 refcount_vnormals_(0),
125 refcount_vcolors_(0),
126 refcount_vtexcoords_(0),
127 refcount_vstatus_(0),
128 refcount_estatus_(0),
129 refcount_ecolors_(0),
130 refcount_hstatus_(0),
131 refcount_fnormals_(0),
132 refcount_fcolors_(0),
133 refcount_fstatus_(0)
134
135 {
136 points_ = add_vpositions( Point(), "v:points" );
137
138 face_types_ = add_fptypes();
139 face_lengths_ = add_fplengths();
140 face_indices_ = add_findices( face_types_, face_lengths_);
141
142 if (VAttribs & Attributes::Normal)
143 request_vertex_normals();
144
145 if (VAttribs & Attributes::Color)
146 request_vertex_colors();
147
148 if (VAttribs & Attributes::TexCoord)
149 request_vertex_texcoords();
150
151 if (VAttribs & Attributes::Status)
152 request_vertex_status();
153
154 if (EAttribs & Attributes::Status)
155 request_edge_status();
156
157 if (EAttribs & Attributes::Color)
158 request_edge_colors();
159
160 if (FAttribs & Attributes::Normal)
161 request_face_normals();
162
163 if (FAttribs & Attributes::Color)
164 request_face_colors();
165
166 if (FAttribs & Attributes::Status)
167 request_face_status();
168 }
169
170 ~AttribKernelT()
171 {
172 // should remove properties, but this will be done in
173 // BaseKernel's destructor anyway...
174 }
175
176
177 // ------------------------------------------------------- copy & assignement
178
179 AttribKernelT( const AttribKernelT& _rhs )
180 : Base( _rhs )
181 {
182 operator=(_rhs);
183 }
184
185 AttribKernelT& operator = ( const AttribKernelT& _rhs )
186 {
187 // remove old properties
188 remove_property(points_);
189 remove_property(vertex_normals_);
190 remove_property(vertex_colors_);
191 remove_property(vertex_texcoords_);
192 remove_property(vertex_status_);
193 remove_property(halfedge_status_);
194 remove_property(edge_status_);
195 remove_property(edge_colors_);
196 remove_property(face_normals_);
197 remove_property(face_colors_);
198 remove_property(face_status_);
199
200 // parent deep-copies properties
201 BaseKernel::operator=(_rhs);
202
203 // copy property handles
204 points_ = _rhs.points_;
205 vertex_normals_ = _rhs.vertex_normals_;
206 vertex_colors_ = _rhs.vertex_colors_;
207 vertex_texcoords_ = _rhs.vertex_texcoords_;
208 vertex_status_ = _rhs.vertex_status_;
209 halfedge_status_ = _rhs.halfedge_status_;
210 edge_status_ = _rhs.edge_status_;
211 edge_colors_ = _rhs.edge_colors_;
212 face_normals_ = _rhs.face_normals_;
213 face_colors_ = _rhs.face_colors_;
214 face_status_ = _rhs.face_status_;
215
216 // copy ref-counts
217 refcount_vnormals_ = _rhs.refcount_vnormals_;
218 refcount_vcolors_ = _rhs.refcount_vcolors_;
219 refcount_vtexcoords_ = _rhs.refcount_vtexcoords_;
220 refcount_vstatus_ = _rhs.refcount_vstatus_;
221 refcount_hstatus_ = _rhs.refcount_hstatus_;
222 refcount_estatus_ = _rhs.refcount_estatus_;
223 refcount_ecolors_ = _rhs.refcount_ecolors_;
224 refcount_fnormals_ = _rhs.refcount_fnormals_;
225 refcount_fcolors_ = _rhs.refcount_fcolors_;
226 refcount_fstatus_ = _rhs.refcount_fstatus_;
227
228 return *this;
229 }
230
231 //------------------------------------------------------------ osg properties
232
233 //------------------------------ vertex property
234
235 typename GeoPositions::property_ptr_t osg_vpositions()
236 { return vpositions(points_).osg_ptr(); }
237
238 typename GeoNormals::property_ptr_t osg_vnormals()
239 { return vnormals(vertex_normals_).osg_ptr(); }
240
241 typename GeoColors::property_ptr_t osg_vcolors()
242 { return vcolors(vertex_colors_).osg_ptr(); }
243
244 typename GeoTexCoords::property_ptr_t osg_vtexcoords()
245 { return vtexcoords(vertex_texcoords_).osg_ptr(); }
246
247 //------------------------------ edge property
248
249 typename GeoColors::property_ptr_t osg_ecolors()
250 { return ecolors(edge_colors_).osg_ptr(); }
251
252 //------------------------------ face property
253
254 GeoPTypes::property_ptr_t osg_ptypes()
255 { return fptypes( face_types_ ).osg_ptr(); }
256
257 GeoPLengths::property_ptr_t osg_plengths()
258 { return fplengths( face_lengths_ ).osg_ptr(); }
259
260 typename GeoIndices::property_ptr_t osg_indices()
261 { return findices( face_indices_ ).osg_ptr(); }
262
263
264 //---------------------------------------- set osg geo property
265
266 //------------------------------ face property
267
268 void set_face_types( FaceHandle _fh, GeoPTypes::value_type _t)
269 { fptypes( face_types_, _fh ) = _t; }
270
271 void set_face_lengths( FaceHandle _fh, GeoPLengths::value_type _l)
272 { fplengths( face_lengths_, _fh ) = _l; }
273
274 void set_face_indices( FaceHandle _fh,
275 typename GeoIndices::value_type _i)
276 { findices( face_indices_, _fh ) = _i; }
277
278 //--------------------------------------------------------- set/get properties
279
280 //---------------------------------------- points
281
282 const Point* points() const
283 { return vpositions( points_ ).data(); }
284
285 const Point& point(VertexHandle _vh) const
286 { return vpositions( points_, _vh); }
287
288 void set_point(VertexHandle _vh, const Point& _p)
289 { vpositions( points_, _vh ) = _p; }
290
291
292 //---------------------------------------- vertex normals
293
294 const Normal* vertex_normals() const {
295 return vnormals(vertex_normals_).data();
296 }
297
298 const Normal& normal(VertexHandle _vh) const {
299 return vnormals(vertex_normals_, _vh);
300 }
301
302 void set_normal(VertexHandle _vh, const Normal& _n) {
303 vnormals(vertex_normals_, _vh) = _n;
304 }
305
306
307 //---------------------------------------- vertex colors
308
309 const Color* vertex_colors() const {
310 return vcolors(vertex_colors_).data();
311 }
312
313 const Color& color(VertexHandle _vh) const {
314 return vcolors(vertex_colors_, _vh);
315 }
316
317 void set_color(VertexHandle _vh, const Color& _c) {
318 vcolors(vertex_colors_, _vh) = _c;
319 }
320
321
322 //---------------------------------------- vertex texcoords
323
324 const TexCoord* texcoords() const {
325 return vtexcoords(vertex_texcoords_).data();
326 }
327
328 const TexCoord& texcoord(VertexHandle _vh) const {
329 return vtexcoords(vertex_texcoords_, _vh);
330 }
331
332 void set_texcoord(VertexHandle _vh, const TexCoord& _t) {
333 vtexcoords(vertex_texcoords_, _vh) = _t;
334 }
335
336
337 //---------------------------------------- vertex status
338
339 const StatusInfo& status(VertexHandle _vh) const {
340 return property(vertex_status_, _vh);
341 }
342
343 StatusInfo& status(VertexHandle _vh) {
344 return property(vertex_status_, _vh);
345 }
346
347
348 //---------------------------------------- halfedge status
349
350 const StatusInfo& status(HalfedgeHandle _eh) const {
351 return property(halfedge_status_, _eh);
352 }
353
354 StatusInfo& status(HalfedgeHandle _eh) {
355 return property(halfedge_status_, _eh);
356 }
357
358
359 //---------------------------------------- edge status
360
361 const StatusInfo& status(EdgeHandle _eh) const {
362 return property(edge_status_, _eh);
363 }
364
365 StatusInfo& status(EdgeHandle _eh) {
366 return property(edge_status_, _eh);
367 }
368
369 //---------------------------------------- edge colors
370
371 const Color* edge_colors() const {
372 return ecolors(edge_colors_).data();
373 }
374
375 const Color& color(EdgeHandle _eh) const {
376 return ecolors(edge_colors_, _eh);
377 }
378
379 void set_color(EdgeHandle _eh, const Color& _c) {
380 ecolors(edge_colors_, _eh) = _c;
381 }
382
383
384 //---------------------------------------- face status
385
386 const StatusInfo& status(FaceHandle _fh) const {
387 return property(face_status_, _fh);
388 }
389
390 StatusInfo& status(FaceHandle _fh) {
391 return property(face_status_, _fh);
392 }
393
394
395 //---------------------------------------- face normals
396
397 const Normal& normal(FaceHandle _fh) const {
398 return property(face_normals_, _fh);
399 }
400
401 void set_normal(FaceHandle _fh, const Normal& _n) {
402 property(face_normals_, _fh) = _n;
403 }
404
405
406 //---------------------------------------- face colors
407
408 const Color& color(FaceHandle _fh) const {
409 return property(face_colors_, _fh);
410 }
411
412 void set_color(FaceHandle _fh, const Color& _c) {
413 property(face_colors_, _fh) = _c;
414 }
415
416
417
418 //------------------------------------------------ request / alloc properties
419
420 void request_vertex_normals() {
421 if (!refcount_vnormals_++)
422 vertex_normals_ = add_vnormals( Normal(), "v:normals" );
423 }
424
425 void request_vertex_colors() {
426 if (!refcount_vcolors_++)
427 vertex_colors_ = add_vcolors( Color(), "v:colors" );
428 }
429
430 void request_vertex_texcoords() {
431 if (!refcount_vtexcoords_++)
432 vertex_texcoords_ = add_vtexcoords( TexCoord(), "v:texcoords" );
433 }
434
435 void request_vertex_status() {
436 if (!refcount_vstatus_++)
437 add_property( vertex_status_, "v:status" );
438 }
439
440 void request_halfedge_status() {
441 if (!refcount_hstatus_++)
442 add_property( halfedge_status_, "h:status" );
443 }
444
445 void request_edge_status() {
446 if (!refcount_estatus_++)
447 add_property( edge_status_, "e:status" );
448 }
449
450 void request_edge_colors() {
451 if (!refcount_ecolors_++)
452 edge_colors_ = add_ecolors( Color(), "e:colors" );
453 }
454
455 void request_face_normals() {
456 if (!refcount_fnormals_++)
457 add_property( face_normals_, "f:normals" );
458 }
459
460 void request_face_colors() {
461 if (!refcount_fcolors_++)
462 add_property( face_colors_, "f:colors" );
463 }
464
465 void request_face_status() {
466 if (!refcount_fstatus_++)
467 add_property( face_status_, "f:status" );
468 }
469
470
471
472 //------------------------------------------------- release / free properties
473
474 void release_vertex_normals() {
475 if ((refcount_vnormals_ > 0) && (! --refcount_vnormals_))
476 remove_property(vertex_normals_);
477 }
478
479 void release_vertex_colors() {
480 if ((refcount_vcolors_ > 0) && (! --refcount_vcolors_))
481 remove_property(vertex_colors_);
482 }
483
484 void release_vertex_texcoords() {
485 if ((refcount_vtexcoords_ > 0) && (! --refcount_vtexcoords_))
486 remove_property(vertex_texcoords_);
487 }
488
489 void release_vertex_status() {
490 if ((refcount_vstatus_ > 0) && (! --refcount_vstatus_))
491 remove_property(vertex_status_);
492 }
493
494 void release_halfedge_status() {
495 if ((refcount_hstatus_ > 0) && (! --refcount_hstatus_))
496 remove_property(halfedge_status_);
497 }
498
499 void release_edge_status() {
500 if ((refcount_estatus_ > 0) && (! --refcount_estatus_))
501 remove_property(edge_status_);
502 }
503
504 void release_edge_colors() {
505 if ((refcount_ecolors_ > 0) && (! --refcount_ecolors_))
506 remove_property(edge_colors_);
507 }
508
509 void release_face_normals() {
510 if ((refcount_fnormals_ > 0) && (! --refcount_fnormals_))
511 remove_property(face_normals_);
512 }
513
514 void release_face_colors() {
515 if ((refcount_fcolors_ > 0) && (! --refcount_fcolors_))
516 remove_property(face_colors_);
517 }
518
519 void release_face_status() {
520 if ((refcount_fstatus_ > 0) && (! --refcount_fstatus_))
521 remove_property(face_status_);
522 }
523
524
525 //----------------------------------------------- static check for properties
526
527 typedef
528 GenProg::Bool2Type<(bool)(VAttribs & Attributes::Normal)>
529 HasVertexNormals;
530
531 typedef
532 GenProg::Bool2Type<(bool)(VAttribs & Attributes::Color)>
533 HasVertexColors;
534
535 typedef
536 GenProg::Bool2Type<(bool)(VAttribs & Attributes::TexCoord)>
537 HasVertexTexCoords;
538
539 typedef
540 GenProg::Bool2Type<(bool)(VAttribs & Attributes::Status)>
541 HasVertexStatus;
542
543
544 typedef
545 GenProg::Bool2Type<(bool)(HAttribs & Attributes::PrevHalfedge)>
546 HasPrevHalfedge;
547
548 typedef
549 GenProg::Bool2Type<(bool)(HAttribs & Attributes::Status)>
550 HasHalfedgeStatus;
551
552
553 typedef
554 GenProg::Bool2Type<(bool)(EAttribs & Attributes::Status)>
555 HasEdgeStatus;
556
557 typedef
558 GenProg::Bool2Type<(bool)(EAttribs & Attributes::Color)>
559 HasEdgeColors;
560
561
562 typedef
563 GenProg::Bool2Type<(bool)(FAttribs & Attributes::Normal)>
564 HasFaceNormals;
565
566 typedef
567 GenProg::Bool2Type<(bool)(FAttribs & Attributes::Color)>
568 HasFaceColors;
569
570 typedef
571 GenProg::Bool2Type<(bool)(FAttribs & Attributes::Status)>
572 HasFaceStatus;
573
574
575 //---------------------------------------------- dynamic check for properties
576
577 bool has_vertex_normals() const { return vertex_normals_.is_valid(); }
578 bool has_vertex_colors() const { return vertex_colors_.is_valid(); }
579 bool has_vertex_texcoords() const { return vertex_texcoords_.is_valid(); }
580 bool has_vertex_status() const { return vertex_status_.is_valid(); }
581 bool has_edge_status() const { return edge_status_.is_valid(); }
582 bool has_halfedge_status() const { return halfedge_status_.is_valid(); }
583 bool has_edge_colors() const { return edge_colors_.is_valid(); }
584 bool has_face_normals() const { return face_normals_.is_valid(); }
585 bool has_face_colors() const { return face_colors_.is_valid(); }
586 bool has_face_status() const { return face_status_.is_valid(); }
587
588 static bool has_prev_halfedge() {
589 return (HAttribs & Attributes::PrevHalfedge);
590 }
591
592
593 public:
594
595 osg::GeometryPtr createGeometryPtr()
596 {
597 using namespace osg;
598 GeometryPtr geo=Geometry::create();
599 return bind(geo) ? geo : NullFC;
600 }
601
602 // create new geometry core from mesh
603 bool bind( osg::GeometryPtr& _geo )
604 {
605 using namespace osg;
606
607 int Mask =
608 Geometry::TypesFieldMask |
609 Geometry::LengthsFieldMask |
610 Geometry::IndicesFieldMask |
611 Geometry::PositionsFieldMask;
612
613 if ( has_vertex_colors() )
614 Mask |= Geometry::ColorsFieldMask;
615 if ( has_vertex_normals() )
616 Mask |= Geometry::NormalsFieldMask;
617 if ( has_vertex_texcoords() )
618 Mask |= Geometry::TexCoordsFieldMask;
619
620 // std::clog << "#ptypes : " << osg_ptypes()->getSize() << std::endl;
621 // std::clog << "#plengths : " << osg_plengths()->getSize() << std::endl;
622 // std::clog << "#indices : " << osg_indices()->getSize() << std::endl;
623 // std::clog << "#points : " << osg_vpositions()->getSize() << std::endl;
624
625 beginEditCP( _geo, Mask );
626 {
627 addRefCP( osg_ptypes() );
628 _geo->setTypes ( osg_ptypes() );
629 addRefCP( osg_plengths() );
630 _geo->setLengths ( osg_plengths() );
631 addRefCP( osg_indices() );
632 _geo->setIndices ( osg_indices() );
633 addRefCP( osg_vpositions() );
634 _geo->setPositions( osg_vpositions() );
635
636 if ( has_vertex_colors() )
637 {
638 addRefCP( osg_vcolors() );
639 _geo->setColors ( osg_vcolors() );
640 }
641 if ( has_vertex_normals() )
642 {
643 addRefCP( osg_vnormals() );
644 _geo->setNormals ( osg_vnormals() );
645 }
646 if ( has_vertex_texcoords() )
647 {
648 addRefCP( osg_vtexcoords() );
649 _geo->setTexCoords( osg_vtexcoords() );
650 }
651 }
652 endEditCP (_geo, Mask);
653
654 return true;
655 }
656
657 private:
658
659 VPropHandleT<Point> points_;
660 VPropHandleT<Normal> vertex_normals_;
661 VPropHandleT<Color> vertex_colors_;
662 VPropHandleT<TexCoord> vertex_texcoords_;
663 VPropHandleT<StatusInfo> vertex_status_;
664
665 FPTypesHandle face_types_;
666 FPLengthsHandle face_lengths_;
667 FIndicesHandle face_indices_;
668
669 EPropHandleT<StatusInfo> edge_status_;
670 EPropHandleT<StatusInfo> edge_colors_;
671 HPropHandleT<StatusInfo> halfedge_status_;
672
673 FPropHandleT<Normal> face_normals_;
674 FPropHandleT<Color> face_colors_;
675 FPropHandleT<StatusInfo> face_status_;
676
677 unsigned int refcount_vnormals_;
678 unsigned int refcount_vcolors_;
679 unsigned int refcount_vtexcoords_;
680 unsigned int refcount_vstatus_;
681 unsigned int refcount_estatus_;
682 unsigned int refcount_ecolors_;
683 unsigned int refcount_hstatus_;
684 unsigned int refcount_fnormals_;
685 unsigned int refcount_fcolors_;
686 unsigned int refcount_fstatus_;
687
688 };
689
690
691 //=============================================================================
692 } // namespace Kernel_OSG
693 } // namespace OpenMesh
694 //=============================================================================
695 #endif // OPENMESH_KERNEL_OSG_ATTRIBKERNEL_HH defined
696 //=============================================================================
697