Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

AttribKernelT.hh

00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2005 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *                                                                           *
00009  *                                License                                    *
00010  *                                                                           *
00011  *  This library is free software; you can redistribute it and/or modify it  *
00012  *  under the terms of the GNU Library General Public License as published   *
00013  *  by the Free Software Foundation, version 2.                              *
00014  *                                                                           *
00015  *  This library is distributed in the hope that it will be useful, but      *
00016  *  WITHOUT ANY WARRANTY; without even the implied warranty of               *
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        *
00018  *  Library General Public License for more details.                         *
00019  *                                                                           *
00020  *  You should have received a copy of the GNU Library General Public        *
00021  *  License along with this library; if not, write to the Free Software      *
00022  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                *
00023  *                                                                           *
00024 \*===========================================================================*/
00025 
00026 #ifndef OPENMESH_KERNEL_OSG_ATTRIBKERNEL_HH
00027 #define OPENMESH_KENREL_OSG_ATTRIBKERNEL_HH
00028 
00029 
00030 //== INCLUDES =================================================================
00031 
00032 #include <OpenMesh/Core/Utils/GenProg.hh>
00033 #include <OpenMesh/Core/Attributes/Attributes.hh>
00034 // --------------------
00035 #include <OpenMesh/Tools/Kernel_OSG/PropertyT.hh>
00036 #include <OpenMesh/Tools/Kernel_OSG/PropertyKernel.hh>
00037 // --------------------
00038 #include <OpenSG/OSGGeometry.h>
00039 
00040 
00041 //== NAMESPACES ===============================================================
00042 
00043 namespace OpenMesh {
00044 namespace Kernel_OSG {
00045 
00046 //== CLASS DEFINITION =========================================================
00047 
00048 
00049 
00051 template <class MeshItems>
00052 class AttribKernelT 
00053   : public PropertyKernel< typename MeshItems::Face::IsTriangle >
00054 {
00055 protected:
00056 
00057   typedef typename MeshItems::Face::IsTriangle IsTriMesh;
00058   typedef PropertyKernel< IsTriMesh > Base;
00059 
00060   typedef typename Base::FPTypesHandle         FPTypesHandle;
00061   typedef typename Base::FPLengthsHandle       FPLengthsHandle;
00062   typedef typename Base::FIndicesHandle        FIndicesHandle;
00063 
00064 public:
00065 
00066   //---------------------------------------------------------------- item types
00067 
00068   typedef typename MeshItems::Vertex             Vertex;
00069   typedef typename MeshItems::Halfedge           Halfedge;
00070   typedef typename MeshItems::Edge               Edge;
00071   typedef typename MeshItems::Face               Face;
00072 
00073   typedef typename MeshItems::Point              Point;
00074   typedef typename MeshItems::Normal             Normal;
00075   typedef typename MeshItems::Color              Color;
00076   typedef typename MeshItems::TexCoord1D         TexCoord1D;
00077   typedef typename MeshItems::TexCoord2D         TexCoord2D;
00078   typedef typename MeshItems::TexCoord3D         TexCoord3D;
00079 
00080   typedef typename MeshItems::Scalar             Scalar;
00081 
00082   typedef Attributes::StatusInfo                 StatusInfo;
00083 
00084 
00085   enum Attribs {
00086     VAttribs = MeshItems::VAttribs,
00087     HAttribs = MeshItems::HAttribs,
00088     EAttribs = MeshItems::EAttribs,
00089     FAttribs = MeshItems::FAttribs,
00090   };
00091 
00092   typedef GenProg::Bool2Type<(bool)(HAttribs & Attributes::PrevHalfedge)> 
00093     HasPrevHalfedge;
00094 
00095   //
00096 
00097   typedef typename _t2vp< Point >::prop                 GeoPositions;
00098   typedef typename _t2vn< Normal >::prop                GeoNormals;
00099   typedef typename _t2vc< Color  >::prop                GeoColors;
00100   typedef typename _t2vtc< TexCoord2D >::prop           GeoTexCoords;
00101 
00102 //   typedef typename Base::GeoPTypes                      GeoPTypes;
00103 //   typedef typename Base::GeoPLengths                    GeoPLengths;
00104 //   typedef typename Base::GeoIndices                     GeoIndices;
00105   
00106   //-------------------------------------------------- constructor / destructor
00107 
00108   AttribKernelT() :
00109 
00110     refcount_vnormals_(0),
00111     refcount_vcolors_(0),
00112     refcount_vtexcoords2D_(0),
00113     refcount_vstatus_(0),
00114     refcount_estatus_(0),
00115     refcount_hstatus_(0),
00116     refcount_fnormals_(0),
00117     refcount_fcolors_(0),
00118     refcount_fstatus_(0)
00119 
00120   {
00121     points_       = add_vpositions( Point(), "v:points" );
00122 
00123     face_types_   = add_fptypes();
00124     face_lengths_ = add_fplengths();
00125     face_indices_ = add_findices( face_types_, face_lengths_);
00126 
00127     if (VAttribs & Attributes::Normal)
00128       request_vertex_normals();
00129 
00130     if (VAttribs & Attributes::Color)
00131       request_vertex_colors();
00132 
00133     if (VAttribs & Attributes::TexCoord2D)
00134       request_vertex_texcoords2D();
00135 
00136     if (VAttribs & Attributes::Status)
00137       request_vertex_status();
00138 
00139     if (EAttribs & Attributes::Status)
00140       request_edge_status();
00141 
00142     if (FAttribs & Attributes::Normal)
00143       request_face_normals();
00144 
00145     if (FAttribs & Attributes::Color)
00146       request_face_colors();
00147 
00148     if (FAttribs & Attributes::Status)
00149       request_face_status();
00150   }
00151 
00152   ~AttribKernelT() 
00153   {
00154     // should remove properties, but this will be done in
00155     // BaseKernel's destructor anyway...
00156   }
00157 
00158 
00159   // ------------------------------------------------------- copy & assignement
00160 
00161   AttribKernelT( const AttribKernelT& _rhs )
00162     : Base( _rhs )
00163   {
00164     operator=(_rhs);
00165   }
00166 
00167   AttribKernelT& operator = ( const AttribKernelT& _rhs )
00168   {
00169     // remove old properties
00170     remove_property(points_);
00171     remove_property(vertex_normals_);
00172     remove_property(vertex_colors_);
00173     remove_property(vertex_texcoords2D_);
00174     remove_property(vertex_status_);
00175     remove_property(halfedge_status_);
00176     remove_property(edge_status_);
00177     remove_property(face_normals_);
00178     remove_property(face_colors_);
00179     remove_property(face_status_);
00180 
00181     // parent deep-copies properties
00182     BaseKernel::operator=(_rhs);
00183 
00184     // copy property handles
00185     points_            = _rhs.points_;
00186     vertex_normals_    = _rhs.vertex_normals_;
00187     vertex_colors_     = _rhs.vertex_colors_;
00188     vertex_texcoords2D_  = _rhs.vertex_texcoords2D_;
00189     vertex_status_     = _rhs.vertex_status_;
00190     halfedge_status_   = _rhs.halfedge_status_;
00191     edge_status_       = _rhs.edge_status_;
00192     face_normals_      = _rhs.face_normals_;
00193     face_colors_       = _rhs.face_colors_;
00194     face_status_       = _rhs.face_status_;
00195 
00196     // copy ref-counts
00197     refcount_vnormals_   = _rhs.refcount_vnormals_;
00198     refcount_vcolors_    = _rhs.refcount_vcolors_;
00199     refcount_vtexcoords2D_ = _rhs.refcount_vtexcoords2D_;
00200     refcount_vstatus_    = _rhs.refcount_vstatus_;
00201     refcount_hstatus_    = _rhs.refcount_hstatus_;
00202     refcount_estatus_    = _rhs.refcount_estatus_;
00203     refcount_fnormals_   = _rhs.refcount_fnormals_;
00204     refcount_fcolors_    = _rhs.refcount_fcolors_;
00205     refcount_fstatus_    = _rhs.refcount_fstatus_;
00206 
00207     return *this;
00208   }
00209 
00210   //------------------------------------------------------------ osg properties
00211 
00212   //------------------------------ vertex property
00213 
00214   typename GeoPositions::property_ptr_t osg_vpositions()
00215   { return vpositions(points_).osg_ptr(); }
00216 
00217   typename GeoNormals::property_ptr_t osg_vnormals() 
00218   { return vnormals(vertex_normals_).osg_ptr(); }
00219 
00220   typename GeoColors::property_ptr_t osg_vcolors() 
00221   { return vcolors(vertex_colors_).osg_ptr(); }
00222 
00223   typename GeoTexCoords::property_ptr_t osg_vtexcoords() 
00224   { return vtexcoords(vertex_texcoords2D_).osg_ptr(); }
00225 
00226   //------------------------------ face property
00227 
00228   GeoPTypes::property_ptr_t osg_ptypes() 
00229   { return fptypes( face_types_ ).osg_ptr(); }
00230 
00231   GeoPLengths::property_ptr_t osg_plengths() 
00232   { return fplengths( face_lengths_ ).osg_ptr(); }
00233 
00234   typename GeoIndices::property_ptr_t osg_indices() 
00235   { return findices( face_indices_ ).osg_ptr(); }
00236 
00237 
00238   //---------------------------------------- set osg geo property
00239   
00240   //------------------------------ face property
00241 
00242   void set_face_types( FaceHandle _fh, GeoPTypes::value_type _t)
00243   { fptypes( face_types_, _fh ) = _t; }
00244 
00245   void set_face_lengths( FaceHandle _fh, GeoPLengths::value_type _l) 
00246   { fplengths( face_lengths_, _fh ) = _l; }
00247 
00248   void set_face_indices( FaceHandle _fh, 
00249                          typename GeoIndices::value_type _i) 
00250   { findices( face_indices_, _fh ) = _i; }
00251 
00252   //--------------------------------------------------------- set/get properties
00253 
00254   //---------------------------------------- points
00255 
00256   const Point* points() const 
00257   { return vpositions( points_ ).data(); }
00258 
00259   const Point& point(VertexHandle _vh) const 
00260   { return vpositions( points_, _vh); }
00261 
00262   void set_point(VertexHandle _vh, const Point& _p) 
00263   { vpositions( points_, _vh ) = _p; }
00264 
00265 
00266   //---------------------------------------- vertex normals
00267 
00268   const Normal* vertex_normals() const {
00269     return vnormals(vertex_normals_).data(); 
00270   }
00271 
00272   const Normal& normal(VertexHandle _vh) const {
00273     return vnormals(vertex_normals_, _vh);
00274   }
00275 
00276   void set_normal(VertexHandle _vh, const Normal& _n) {
00277     vnormals(vertex_normals_, _vh) = _n;
00278   }
00279 
00280 
00281   //---------------------------------------- vertex colors
00282 
00283   const Color* vertex_colors() const {
00284     return vcolors(vertex_colors_).data(); 
00285   }
00286 
00287   const Color& color(VertexHandle _vh) const {
00288     return vcolors(vertex_colors_, _vh);
00289   }
00290 
00291   void set_color(VertexHandle _vh, const Color& _c) {
00292     vcolors(vertex_colors_, _vh) = _c;
00293   }
00294 
00295 
00296   //---------------------------------------- vertex texcoords
00297 
00298   const TexCoord2D* texcoords2D() const {
00299     return vtexcoords(vertex_texcoords2D_).data(); 
00300   }
00301 
00302   const TexCoord2D& texcoord2D(VertexHandle _vh) const {
00303     return vtexcoords(vertex_texcoords2D_, _vh);
00304   }
00305 
00306   void set_texcoord2D(VertexHandle _vh, const TexCoord2D& _t) {
00307     vtexcoords(vertex_texcoords2D_, _vh) = _t;
00308   }
00309 
00310 
00311   //---------------------------------------- vertex status
00312 
00313   const StatusInfo& status(VertexHandle _vh) const {
00314     return property(vertex_status_, _vh);
00315   }
00316 
00317   StatusInfo& status(VertexHandle _vh) {
00318     return property(vertex_status_, _vh);
00319   }
00320 
00321 
00322   //---------------------------------------- edge status
00323 
00324   const StatusInfo& status(HalfedgeHandle _eh) const {
00325     return property(halfedge_status_, _eh);
00326   }
00327 
00328   StatusInfo& status(HalfedgeHandle _eh) {
00329     return property(halfedge_status_, _eh);
00330   }
00331 
00332 
00333   //---------------------------------------- edge status
00334 
00335   const StatusInfo& status(EdgeHandle _eh) const {
00336     return property(edge_status_, _eh);
00337   }
00338 
00339   StatusInfo& status(EdgeHandle _eh) {
00340     return property(edge_status_, _eh);
00341   }
00342 
00343 
00344   //---------------------------------------- face status
00345 
00346   const StatusInfo& status(FaceHandle _fh) const {
00347     return property(face_status_, _fh);
00348   }
00349 
00350   StatusInfo& status(FaceHandle _fh) {
00351     return property(face_status_, _fh);
00352   }
00353 
00354 
00355   //---------------------------------------- face normals
00356 
00357   const Normal& normal(FaceHandle _fh) const {
00358     return property(face_normals_, _fh);
00359   }
00360 
00361   void set_normal(FaceHandle _fh, const Normal& _n) {
00362     property(face_normals_, _fh) = _n;
00363   }
00364 
00365 
00366   //---------------------------------------- face colors
00367 
00368   const Color& color(FaceHandle _fh) const {
00369     return property(face_colors_, _fh);
00370   }
00371 
00372   void set_color(FaceHandle _fh, const Color& _c) {
00373     property(face_colors_, _fh) = _c;
00374   }
00375 
00376 
00377 
00378   //------------------------------------------------ request / alloc properties
00379 
00380   void request_vertex_normals() {
00381     if (!refcount_vnormals_++)  
00382       vertex_normals_ = add_vnormals( Normal(), "v:normals" );
00383   }
00384 
00385   void request_vertex_colors() {
00386     if (!refcount_vcolors_++)
00387       vertex_colors_ = add_vcolors( Color(), "v:colors" );
00388   }
00389 
00390   void request_vertex_texcoords2D() {
00391     if (!refcount_vtexcoords2D_++)
00392       vertex_texcoords2D_ = add_vtexcoords( TexCoord2D(), "v:texcoords" );
00393   }
00394 
00395   void request_vertex_status() {
00396     if (!refcount_vstatus_++)
00397       add_property( vertex_status_, "v:status" );
00398   }
00399 
00400   void request_halfedge_status() {
00401     if (!refcount_hstatus_++)
00402       add_property( halfedge_status_, "h:status" );
00403   }
00404 
00405   void request_edge_status() {
00406     if (!refcount_estatus_++)
00407       add_property( edge_status_, "e:status" );
00408   }
00409 
00410   void request_face_normals() {
00411     if (!refcount_fnormals_++)
00412       add_property( face_normals_, "f:normals" );
00413   }
00414 
00415   void request_face_colors() {
00416     if (!refcount_fcolors_++)
00417       add_property( face_colors_, "f:colors" );
00418   }
00419 
00420   void request_face_status() {
00421     if (!refcount_fstatus_++)
00422       add_property( face_status_, "f:status" );
00423   }
00424 
00425 
00426 
00427   //------------------------------------------------- release / free properties
00428 
00429   void release_vertex_normals() {
00430     if ((refcount_vnormals_ > 0) && (! --refcount_vnormals_))
00431       remove_property(vertex_normals_);
00432   }
00433 
00434   void release_vertex_colors() {
00435     if ((refcount_vcolors_ > 0) && (! --refcount_vcolors_))
00436       remove_property(vertex_colors_);
00437   }
00438 
00439   void release_vertex_texcoords2D() {
00440     if ((refcount_vtexcoords2D_ > 0) && (! --refcount_vtexcoords2D_))
00441       remove_property(vertex_texcoords2D_);
00442   }
00443 
00444   void release_vertex_status() {
00445     if ((refcount_vstatus_ > 0) && (! --refcount_vstatus_))
00446       remove_property(vertex_status_);
00447   }
00448 
00449   void release_halfedge_status() {
00450     if ((refcount_hstatus_ > 0) && (! --refcount_hstatus_))
00451       remove_property(halfedge_status_);
00452   }
00453 
00454   void release_edge_status() {
00455     if ((refcount_estatus_ > 0) && (! --refcount_estatus_))
00456       remove_property(edge_status_);
00457   }
00458 
00459   void release_face_normals() {
00460     if ((refcount_fnormals_ > 0) && (! --refcount_fnormals_))
00461       remove_property(face_normals_);
00462   }
00463 
00464   void release_face_colors() {
00465     if ((refcount_fcolors_ > 0) && (! --refcount_fcolors_))
00466       remove_property(face_colors_);
00467   }
00468 
00469   void release_face_status() {
00470     if ((refcount_fstatus_ > 0) && (! --refcount_fstatus_))
00471       remove_property(face_status_);
00472   }
00473 
00474 
00475   //----------------------------------------------- static check for properties
00476 
00477   typedef 
00478   GenProg::Bool2Type<(bool)(VAttribs & Attributes::Normal)> 
00479   HasVertexNormals;
00480 
00481   typedef 
00482   GenProg::Bool2Type<(bool)(VAttribs & Attributes::Color)> 
00483   HasVertexColors;
00484 
00485   typedef
00486   GenProg::Bool2Type<(bool)(VAttribs & Attributes::TexCoord2D)> 
00487   HasVertexTexCoords;
00488 
00489   typedef
00490   GenProg::Bool2Type<(bool)(VAttribs & Attributes::Status)> 
00491   HasVertexStatus;
00492 
00493 
00494   typedef
00495   GenProg::Bool2Type<(bool)(HAttribs & Attributes::PrevHalfedge)> 
00496   HasPrevHalfedge;
00497 
00498   typedef
00499   GenProg::Bool2Type<(bool)(HAttribs & Attributes::Status)> 
00500   HasHalfedgeStatus;
00501 
00502 
00503   typedef
00504   GenProg::Bool2Type<(bool)(EAttribs & Attributes::Status)> 
00505   HasEdgeStatus;
00506 
00507 
00508   typedef 
00509   GenProg::Bool2Type<(bool)(FAttribs & Attributes::Normal)> 
00510   HasFaceNormals;
00511 
00512   typedef 
00513   GenProg::Bool2Type<(bool)(FAttribs & Attributes::Color)> 
00514   HasFaceColors;
00515 
00516   typedef
00517   GenProg::Bool2Type<(bool)(FAttribs & Attributes::Status)> 
00518   HasFaceStatus;
00519 
00520 
00521   //---------------------------------------------- dynamic check for properties
00522 
00523   bool has_vertex_normals()   const { return vertex_normals_.is_valid();   }
00524   bool has_vertex_colors()    const { return vertex_colors_.is_valid();    }
00525   bool has_vertex_texcoords2D() const { return vertex_texcoords2D_.is_valid(); }
00526   bool has_vertex_status()    const { return vertex_status_.is_valid();    }
00527   bool has_edge_status()      const { return edge_status_.is_valid();      }
00528   bool has_halfedge_status()  const { return halfedge_status_.is_valid();  }
00529   bool has_face_normals()     const { return face_normals_.is_valid();     }
00530   bool has_face_colors()      const { return face_colors_.is_valid();      }
00531   bool has_face_status()      const { return face_status_.is_valid();      }
00532 
00533   static bool has_prev_halfedge() {
00534     return (HAttribs & Attributes::PrevHalfedge); 
00535   }
00536 
00537 
00538 public:
00539 
00540   osg::GeometryPtr createGeometryPtr()
00541   {
00542     using namespace osg;
00543     GeometryPtr geo=Geometry::create();
00544     return bind(geo) ? geo : NullFC;    
00545   }
00546 
00547   // create new geometry core from mesh
00548   bool bind( osg::GeometryPtr& _geo )
00549   {
00550     using namespace osg;
00551 
00552     int Mask = 
00553       Geometry::TypesFieldMask     |
00554       Geometry::LengthsFieldMask   |
00555       Geometry::IndicesFieldMask   |
00556       Geometry::PositionsFieldMask;
00557 
00558     if ( has_vertex_colors() )
00559       Mask |= Geometry::ColorsFieldMask;
00560     if ( has_vertex_normals() )
00561       Mask |= Geometry::NormalsFieldMask;
00562     if ( has_vertex_texcoords2D() )
00563       Mask |= Geometry::TexCoordsFieldMask;
00564 
00565 //     std::clog << "#ptypes   : " << osg_ptypes()->getSize()     << std::endl;
00566 //     std::clog << "#plengths : " << osg_plengths()->getSize()   << std::endl;
00567 //     std::clog << "#indices  : " << osg_indices()->getSize()    << std::endl;
00568 //     std::clog << "#points   : " << osg_vpositions()->getSize() << std::endl;
00569 
00570     beginEditCP( _geo, Mask );
00571     {
00572       addRefCP( osg_ptypes() );
00573       _geo->setTypes    ( osg_ptypes() );
00574       addRefCP( osg_plengths() );
00575       _geo->setLengths  ( osg_plengths() );
00576       addRefCP( osg_indices() );
00577       _geo->setIndices  ( osg_indices() );
00578       addRefCP( osg_vpositions() );
00579       _geo->setPositions( osg_vpositions() );
00580 
00581       if ( has_vertex_colors() )
00582       {
00583         addRefCP( osg_vcolors() );
00584         _geo->setColors   ( osg_vcolors() );
00585       }
00586       if ( has_vertex_normals() )
00587       {
00588         addRefCP( osg_vnormals() );
00589         _geo->setNormals  ( osg_vnormals() );
00590       }
00591       if ( has_vertex_texcoords2D() )
00592       {
00593         addRefCP( osg_vtexcoords() );
00594         _geo->setTexCoords( osg_vtexcoords() );
00595       }
00596     }
00597     endEditCP  (_geo, Mask);
00598 
00599     return true;      
00600   }
00601 
00602 private:
00603   
00604   VPropHandleT<Point>         points_;
00605   VPropHandleT<Normal>        vertex_normals_;
00606   VPropHandleT<Color>         vertex_colors_;
00607   VPropHandleT<TexCoord2D>    vertex_texcoords2D_;
00608   VPropHandleT<StatusInfo>    vertex_status_;
00609 
00610   FPTypesHandle               face_types_;
00611   FPLengthsHandle             face_lengths_;
00612   FIndicesHandle              face_indices_;
00613 
00614   EPropHandleT<StatusInfo>    edge_status_;
00615   HPropHandleT<StatusInfo>    halfedge_status_;
00616 
00617   FPropHandleT<Normal>        face_normals_;
00618   FPropHandleT<Color>         face_colors_;
00619   FPropHandleT<StatusInfo>    face_status_;
00620 
00621   unsigned int  refcount_vnormals_;
00622   unsigned int  refcount_vcolors_;
00623   unsigned int  refcount_vtexcoords2D_;
00624   unsigned int  refcount_vstatus_;
00625   unsigned int  refcount_estatus_;
00626   unsigned int  refcount_hstatus_;
00627   unsigned int  refcount_fnormals_;
00628   unsigned int  refcount_fcolors_;
00629   unsigned int  refcount_fstatus_;
00630 
00631 };
00632 
00633 
00634 //=============================================================================
00635 } // namespace Kernel_OSG
00636 } // namespace OpenMesh
00637 //=============================================================================
00638 #endif // OPENMESH_KERNEL_OSG_ATTRIBKERNEL_HH defined
00639 //=============================================================================
00640 

acg pic Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .