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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef OPENMESH_KERNEL_OSG_ATTRIBKERNEL_HH
00043 #define OPENMESH_KENREL_OSG_ATTRIBKERNEL_HH
00044
00045
00046
00047
00048 #include <OpenMesh/Core/Utils/GenProg.hh>
00049 #include <OpenMesh/Core/Mesh/Attributes.hh>
00050
00051 #include <OpenMesh/Tools/Kernel_OSG/PropertyT.hh>
00052 #include <OpenMesh/Tools/Kernel_OSG/PropertyKernel.hh>
00053
00054 #include <osg/Geometry>
00055
00056
00057
00058
00059 namespace OpenMesh {
00060 namespace Kernel_OSG {
00061
00062
00063
00064
00065
00067 template <class MeshItems>
00068 class AttribKernelT
00069 : public PropertyKernel< typename MeshItems::Face::IsTriangle >
00070 {
00071 protected:
00072
00073 typedef typename MeshItems::Face::IsTriangle IsTriMesh;
00074 typedef PropertyKernel< IsTriMesh > Base;
00075
00076 typedef typename Base::FPTypesHandle FPTypesHandle;
00077 typedef typename Base::FPLengthsHandle FPLengthsHandle;
00078 typedef typename Base::FIndicesHandle FIndicesHandle;
00079
00080 public:
00081
00082
00083
00084 typedef typename MeshItems::Vertex Vertex;
00085 typedef typename MeshItems::Halfedge Halfedge;
00086 typedef typename MeshItems::Edge Edge;
00087 typedef typename MeshItems::Face Face;
00088
00089 typedef typename MeshItems::Point Point;
00090 typedef typename MeshItems::Normal Normal;
00091 typedef typename MeshItems::Color Color;
00092 typedef typename MeshItems::TexCoord TexCoord;
00093
00094 typedef typename MeshItems::Scalar Scalar;
00095
00096 typedef Attributes::StatusInfo StatusInfo;
00097
00098
00099 enum Attribs {
00100 VAttribs = MeshItems::VAttribs,
00101 HAttribs = MeshItems::HAttribs,
00102 EAttribs = MeshItems::EAttribs,
00103 FAttribs = MeshItems::FAttribs,
00104 };
00105
00106 typedef GenProg::Bool2Type<(bool)(HAttribs & Attributes::PrevHalfedge)>
00107 HasPrevHalfedge;
00108
00109
00110
00111 typedef typename _t2vp< Point >::prop GeoPositions;
00112 typedef typename _t2vn< Normal >::prop GeoNormals;
00113 typedef typename _t2vc< Color >::prop GeoColors;
00114 typedef typename _t2vtc< TexCoord >::prop GeoTexCoords;
00115
00116
00117
00118
00119
00120
00121
00122 AttribKernelT() :
00123
00124 refcount_vnormals_(0),
00125 refcount_vcolors_(0),
00126 refcount_vtexcoords_(0),
00127 refcount_vstatus_(0),
00128 refcount_estatus_(0),
00129 refcount_ecolors_(0),
00130 refcount_hstatus_(0),
00131 refcount_fnormals_(0),
00132 refcount_fcolors_(0),
00133 refcount_fstatus_(0)
00134
00135 {
00136 points_ = add_vpositions( Point(), "v:points" );
00137
00138 face_types_ = add_fptypes();
00139 face_lengths_ = add_fplengths();
00140 face_indices_ = add_findices( face_types_, face_lengths_);
00141
00142 if (VAttribs & Attributes::Normal)
00143 request_vertex_normals();
00144
00145 if (VAttribs & Attributes::Color)
00146 request_vertex_colors();
00147
00148 if (VAttribs & Attributes::TexCoord)
00149 request_vertex_texcoords();
00150
00151 if (VAttribs & Attributes::Status)
00152 request_vertex_status();
00153
00154 if (EAttribs & Attributes::Status)
00155 request_edge_status();
00156
00157 if (EAttribs & Attributes::Color)
00158 request_edge_colors();
00159
00160 if (FAttribs & Attributes::Normal)
00161 request_face_normals();
00162
00163 if (FAttribs & Attributes::Color)
00164 request_face_colors();
00165
00166 if (FAttribs & Attributes::Status)
00167 request_face_status();
00168 }
00169
00170 ~AttribKernelT()
00171 {
00172
00173
00174 }
00175
00176
00177
00178
00179 AttribKernelT( const AttribKernelT& _rhs )
00180 : Base( _rhs )
00181 {
00182 operator=(_rhs);
00183 }
00184
00185 AttribKernelT& operator = ( const AttribKernelT& _rhs )
00186 {
00187
00188 remove_property(points_);
00189 remove_property(vertex_normals_);
00190 remove_property(vertex_colors_);
00191 remove_property(vertex_texcoords_);
00192 remove_property(vertex_status_);
00193 remove_property(halfedge_status_);
00194 remove_property(edge_status_);
00195 remove_property(edge_colors_);
00196 remove_property(face_normals_);
00197 remove_property(face_colors_);
00198 remove_property(face_status_);
00199
00200
00201 BaseKernel::operator=(_rhs);
00202
00203
00204 points_ = _rhs.points_;
00205 vertex_normals_ = _rhs.vertex_normals_;
00206 vertex_colors_ = _rhs.vertex_colors_;
00207 vertex_texcoords_ = _rhs.vertex_texcoords_;
00208 vertex_status_ = _rhs.vertex_status_;
00209 halfedge_status_ = _rhs.halfedge_status_;
00210 edge_status_ = _rhs.edge_status_;
00211 edge_colors_ = _rhs.edge_colors_;
00212 face_normals_ = _rhs.face_normals_;
00213 face_colors_ = _rhs.face_colors_;
00214 face_status_ = _rhs.face_status_;
00215
00216
00217 refcount_vnormals_ = _rhs.refcount_vnormals_;
00218 refcount_vcolors_ = _rhs.refcount_vcolors_;
00219 refcount_vtexcoords_ = _rhs.refcount_vtexcoords_;
00220 refcount_vstatus_ = _rhs.refcount_vstatus_;
00221 refcount_hstatus_ = _rhs.refcount_hstatus_;
00222 refcount_estatus_ = _rhs.refcount_estatus_;
00223 refcount_ecolors_ = _rhs.refcount_ecolors_;
00224 refcount_fnormals_ = _rhs.refcount_fnormals_;
00225 refcount_fcolors_ = _rhs.refcount_fcolors_;
00226 refcount_fstatus_ = _rhs.refcount_fstatus_;
00227
00228 return *this;
00229 }
00230
00231
00232
00233
00234
00235 typename GeoPositions::property_ptr_t osg_vpositions()
00236 { return vpositions(points_).osg_ptr(); }
00237
00238 typename GeoNormals::property_ptr_t osg_vnormals()
00239 { return vnormals(vertex_normals_).osg_ptr(); }
00240
00241 typename GeoColors::property_ptr_t osg_vcolors()
00242 { return vcolors(vertex_colors_).osg_ptr(); }
00243
00244 typename GeoTexCoords::property_ptr_t osg_vtexcoords()
00245 { return vtexcoords(vertex_texcoords_).osg_ptr(); }
00246
00247
00248
00249 typename GeoColors::property_ptr_t osg_ecolors()
00250 { return ecolors(edge_colors_).osg_ptr(); }
00251
00252
00253
00254 GeoPTypes::property_ptr_t osg_ptypes()
00255 { return fptypes( face_types_ ).osg_ptr(); }
00256
00257 GeoPLengths::property_ptr_t osg_plengths()
00258 { return fplengths( face_lengths_ ).osg_ptr(); }
00259
00260 typename GeoIndices::property_ptr_t osg_indices()
00261 { return findices( face_indices_ ).osg_ptr(); }
00262
00263
00264
00265
00266
00267
00268 void set_face_types( FaceHandle _fh, GeoPTypes::value_type _t)
00269 { fptypes( face_types_, _fh ) = _t; }
00270
00271 void set_face_lengths( FaceHandle _fh, GeoPLengths::value_type _l)
00272 { fplengths( face_lengths_, _fh ) = _l; }
00273
00274 void set_face_indices( FaceHandle _fh,
00275 typename GeoIndices::value_type _i)
00276 { findices( face_indices_, _fh ) = _i; }
00277
00278
00279
00280
00281
00282 const Point* points() const
00283 { return vpositions( points_ ).data(); }
00284
00285 const Point& point(VertexHandle _vh) const
00286 { return vpositions( points_, _vh); }
00287
00288 void set_point(VertexHandle _vh, const Point& _p)
00289 { vpositions( points_, _vh ) = _p; }
00290
00291
00292
00293
00294 const Normal* vertex_normals() const {
00295 return vnormals(vertex_normals_).data();
00296 }
00297
00298 const Normal& normal(VertexHandle _vh) const {
00299 return vnormals(vertex_normals_, _vh);
00300 }
00301
00302 void set_normal(VertexHandle _vh, const Normal& _n) {
00303 vnormals(vertex_normals_, _vh) = _n;
00304 }
00305
00306
00307
00308
00309 const Color* vertex_colors() const {
00310 return vcolors(vertex_colors_).data();
00311 }
00312
00313 const Color& color(VertexHandle _vh) const {
00314 return vcolors(vertex_colors_, _vh);
00315 }
00316
00317 void set_color(VertexHandle _vh, const Color& _c) {
00318 vcolors(vertex_colors_, _vh) = _c;
00319 }
00320
00321
00322
00323
00324 const TexCoord* texcoords() const {
00325 return vtexcoords(vertex_texcoords_).data();
00326 }
00327
00328 const TexCoord& texcoord(VertexHandle _vh) const {
00329 return vtexcoords(vertex_texcoords_, _vh);
00330 }
00331
00332 void set_texcoord(VertexHandle _vh, const TexCoord& _t) {
00333 vtexcoords(vertex_texcoords_, _vh) = _t;
00334 }
00335
00336
00337
00338
00339 const StatusInfo& status(VertexHandle _vh) const {
00340 return property(vertex_status_, _vh);
00341 }
00342
00343 StatusInfo& status(VertexHandle _vh) {
00344 return property(vertex_status_, _vh);
00345 }
00346
00347
00348
00349
00350 const StatusInfo& status(HalfedgeHandle _eh) const {
00351 return property(halfedge_status_, _eh);
00352 }
00353
00354 StatusInfo& status(HalfedgeHandle _eh) {
00355 return property(halfedge_status_, _eh);
00356 }
00357
00358
00359
00360
00361 const StatusInfo& status(EdgeHandle _eh) const {
00362 return property(edge_status_, _eh);
00363 }
00364
00365 StatusInfo& status(EdgeHandle _eh) {
00366 return property(edge_status_, _eh);
00367 }
00368
00369
00370
00371 const Color* edge_colors() const {
00372 return ecolors(edge_colors_).data();
00373 }
00374
00375 const Color& color(EdgeHandle _eh) const {
00376 return ecolors(edge_colors_, _eh);
00377 }
00378
00379 void set_color(EdgeHandle _eh, const Color& _c) {
00380 ecolors(edge_colors_, _eh) = _c;
00381 }
00382
00383
00384
00385
00386 const StatusInfo& status(FaceHandle _fh) const {
00387 return property(face_status_, _fh);
00388 }
00389
00390 StatusInfo& status(FaceHandle _fh) {
00391 return property(face_status_, _fh);
00392 }
00393
00394
00395
00396
00397 const Normal& normal(FaceHandle _fh) const {
00398 return property(face_normals_, _fh);
00399 }
00400
00401 void set_normal(FaceHandle _fh, const Normal& _n) {
00402 property(face_normals_, _fh) = _n;
00403 }
00404
00405
00406
00407
00408 const Color& color(FaceHandle _fh) const {
00409 return property(face_colors_, _fh);
00410 }
00411
00412 void set_color(FaceHandle _fh, const Color& _c) {
00413 property(face_colors_, _fh) = _c;
00414 }
00415
00416
00417
00418
00419
00420 void request_vertex_normals() {
00421 if (!refcount_vnormals_++)
00422 vertex_normals_ = add_vnormals( Normal(), "v:normals" );
00423 }
00424
00425 void request_vertex_colors() {
00426 if (!refcount_vcolors_++)
00427 vertex_colors_ = add_vcolors( Color(), "v:colors" );
00428 }
00429
00430 void request_vertex_texcoords() {
00431 if (!refcount_vtexcoords_++)
00432 vertex_texcoords_ = add_vtexcoords( TexCoord(), "v:texcoords" );
00433 }
00434
00435 void request_vertex_status() {
00436 if (!refcount_vstatus_++)
00437 add_property( vertex_status_, "v:status" );
00438 }
00439
00440 void request_halfedge_status() {
00441 if (!refcount_hstatus_++)
00442 add_property( halfedge_status_, "h:status" );
00443 }
00444
00445 void request_edge_status() {
00446 if (!refcount_estatus_++)
00447 add_property( edge_status_, "e:status" );
00448 }
00449
00450 void request_edge_colors() {
00451 if (!refcount_ecolors_++)
00452 edge_colors_ = add_ecolors( Color(), "e:colors" );
00453 }
00454
00455 void request_face_normals() {
00456 if (!refcount_fnormals_++)
00457 add_property( face_normals_, "f:normals" );
00458 }
00459
00460 void request_face_colors() {
00461 if (!refcount_fcolors_++)
00462 add_property( face_colors_, "f:colors" );
00463 }
00464
00465 void request_face_status() {
00466 if (!refcount_fstatus_++)
00467 add_property( face_status_, "f:status" );
00468 }
00469
00470
00471
00472
00473
00474 void release_vertex_normals() {
00475 if ((refcount_vnormals_ > 0) && (! --refcount_vnormals_))
00476 remove_property(vertex_normals_);
00477 }
00478
00479 void release_vertex_colors() {
00480 if ((refcount_vcolors_ > 0) && (! --refcount_vcolors_))
00481 remove_property(vertex_colors_);
00482 }
00483
00484 void release_vertex_texcoords() {
00485 if ((refcount_vtexcoords_ > 0) && (! --refcount_vtexcoords_))
00486 remove_property(vertex_texcoords_);
00487 }
00488
00489 void release_vertex_status() {
00490 if ((refcount_vstatus_ > 0) && (! --refcount_vstatus_))
00491 remove_property(vertex_status_);
00492 }
00493
00494 void release_halfedge_status() {
00495 if ((refcount_hstatus_ > 0) && (! --refcount_hstatus_))
00496 remove_property(halfedge_status_);
00497 }
00498
00499 void release_edge_status() {
00500 if ((refcount_estatus_ > 0) && (! --refcount_estatus_))
00501 remove_property(edge_status_);
00502 }
00503
00504 void release_edge_colors() {
00505 if ((refcount_ecolors_ > 0) && (! --refcount_ecolors_))
00506 remove_property(edge_colors_);
00507 }
00508
00509 void release_face_normals() {
00510 if ((refcount_fnormals_ > 0) && (! --refcount_fnormals_))
00511 remove_property(face_normals_);
00512 }
00513
00514 void release_face_colors() {
00515 if ((refcount_fcolors_ > 0) && (! --refcount_fcolors_))
00516 remove_property(face_colors_);
00517 }
00518
00519 void release_face_status() {
00520 if ((refcount_fstatus_ > 0) && (! --refcount_fstatus_))
00521 remove_property(face_status_);
00522 }
00523
00524
00525
00526
00527 typedef
00528 GenProg::Bool2Type<(bool)(VAttribs & Attributes::Normal)>
00529 HasVertexNormals;
00530
00531 typedef
00532 GenProg::Bool2Type<(bool)(VAttribs & Attributes::Color)>
00533 HasVertexColors;
00534
00535 typedef
00536 GenProg::Bool2Type<(bool)(VAttribs & Attributes::TexCoord)>
00537 HasVertexTexCoords;
00538
00539 typedef
00540 GenProg::Bool2Type<(bool)(VAttribs & Attributes::Status)>
00541 HasVertexStatus;
00542
00543
00544 typedef
00545 GenProg::Bool2Type<(bool)(HAttribs & Attributes::PrevHalfedge)>
00546 HasPrevHalfedge;
00547
00548 typedef
00549 GenProg::Bool2Type<(bool)(HAttribs & Attributes::Status)>
00550 HasHalfedgeStatus;
00551
00552
00553 typedef
00554 GenProg::Bool2Type<(bool)(EAttribs & Attributes::Status)>
00555 HasEdgeStatus;
00556
00557 typedef
00558 GenProg::Bool2Type<(bool)(EAttribs & Attributes::Color)>
00559 HasEdgeColors;
00560
00561
00562 typedef
00563 GenProg::Bool2Type<(bool)(FAttribs & Attributes::Normal)>
00564 HasFaceNormals;
00565
00566 typedef
00567 GenProg::Bool2Type<(bool)(FAttribs & Attributes::Color)>
00568 HasFaceColors;
00569
00570 typedef
00571 GenProg::Bool2Type<(bool)(FAttribs & Attributes::Status)>
00572 HasFaceStatus;
00573
00574
00575
00576
00577 bool has_vertex_normals() const { return vertex_normals_.is_valid(); }
00578 bool has_vertex_colors() const { return vertex_colors_.is_valid(); }
00579 bool has_vertex_texcoords() const { return vertex_texcoords_.is_valid(); }
00580 bool has_vertex_status() const { return vertex_status_.is_valid(); }
00581 bool has_edge_status() const { return edge_status_.is_valid(); }
00582 bool has_halfedge_status() const { return halfedge_status_.is_valid(); }
00583 bool has_edge_colors() const { return edge_colors_.is_valid(); }
00584 bool has_face_normals() const { return face_normals_.is_valid(); }
00585 bool has_face_colors() const { return face_colors_.is_valid(); }
00586 bool has_face_status() const { return face_status_.is_valid(); }
00587
00588 static bool has_prev_halfedge() {
00589 return (HAttribs & Attributes::PrevHalfedge);
00590 }
00591
00592
00593 public:
00594
00595 osg::GeometryPtr createGeometryPtr()
00596 {
00597 using namespace osg;
00598 GeometryPtr geo=Geometry::create();
00599 return bind(geo) ? geo : NullFC;
00600 }
00601
00602
00603 bool bind( osg::GeometryPtr& _geo )
00604 {
00605 using namespace osg;
00606
00607 int Mask =
00608 Geometry::TypesFieldMask |
00609 Geometry::LengthsFieldMask |
00610 Geometry::IndicesFieldMask |
00611 Geometry::PositionsFieldMask;
00612
00613 if ( has_vertex_colors() )
00614 Mask |= Geometry::ColorsFieldMask;
00615 if ( has_vertex_normals() )
00616 Mask |= Geometry::NormalsFieldMask;
00617 if ( has_vertex_texcoords() )
00618 Mask |= Geometry::TexCoordsFieldMask;
00619
00620
00621
00622
00623
00624
00625 beginEditCP( _geo, Mask );
00626 {
00627 addRefCP( osg_ptypes() );
00628 _geo->setTypes ( osg_ptypes() );
00629 addRefCP( osg_plengths() );
00630 _geo->setLengths ( osg_plengths() );
00631 addRefCP( osg_indices() );
00632 _geo->setIndices ( osg_indices() );
00633 addRefCP( osg_vpositions() );
00634 _geo->setPositions( osg_vpositions() );
00635
00636 if ( has_vertex_colors() )
00637 {
00638 addRefCP( osg_vcolors() );
00639 _geo->setColors ( osg_vcolors() );
00640 }
00641 if ( has_vertex_normals() )
00642 {
00643 addRefCP( osg_vnormals() );
00644 _geo->setNormals ( osg_vnormals() );
00645 }
00646 if ( has_vertex_texcoords() )
00647 {
00648 addRefCP( osg_vtexcoords() );
00649 _geo->setTexCoords( osg_vtexcoords() );
00650 }
00651 }
00652 endEditCP (_geo, Mask);
00653
00654 return true;
00655 }
00656
00657 private:
00658
00659 VPropHandleT<Point> points_;
00660 VPropHandleT<Normal> vertex_normals_;
00661 VPropHandleT<Color> vertex_colors_;
00662 VPropHandleT<TexCoord> vertex_texcoords_;
00663 VPropHandleT<StatusInfo> vertex_status_;
00664
00665 FPTypesHandle face_types_;
00666 FPLengthsHandle face_lengths_;
00667 FIndicesHandle face_indices_;
00668
00669 EPropHandleT<StatusInfo> edge_status_;
00670 EPropHandleT<StatusInfo> edge_colors_;
00671 HPropHandleT<StatusInfo> halfedge_status_;
00672
00673 FPropHandleT<Normal> face_normals_;
00674 FPropHandleT<Color> face_colors_;
00675 FPropHandleT<StatusInfo> face_status_;
00676
00677 unsigned int refcount_vnormals_;
00678 unsigned int refcount_vcolors_;
00679 unsigned int refcount_vtexcoords_;
00680 unsigned int refcount_vstatus_;
00681 unsigned int refcount_estatus_;
00682 unsigned int refcount_ecolors_;
00683 unsigned int refcount_hstatus_;
00684 unsigned int refcount_fnormals_;
00685 unsigned int refcount_fcolors_;
00686 unsigned int refcount_fstatus_;
00687
00688 };
00689
00690
00691
00692 }
00693 }
00694
00695 #endif // OPENMESH_KERNEL_OSG_ATTRIBKERNEL_HH defined
00696
00697