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

AttribKernelT.hh

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

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