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

PropertyT.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_PROPERTYT_HH
00027 #define OPENMESH_KERNEL_OSG_PROPERTYT_HH
00028 
00029 
00030 //== INCLUDES =================================================================
00031 
00032 #include <OpenMesh/Core/Attributes/Attributes.hh>
00033 #include <OpenMesh/Core/Mesh/Kernels/Common/BaseKernel.hh>
00034 #include <OpenMesh/Core/Utils/GenProg.hh>
00035 #include <OpenMesh/Core/Utils/Property.hh>
00036 //
00037 #include <OpenSG/OSGGeometry.h>
00038 //
00039 #include <stdexcept>
00040 #include <vector>
00041 
00042 
00043 //== NAMESPACES ===============================================================
00044 
00045 namespace OpenMesh {
00046 namespace Kernel_OSG {
00047 
00048 
00049 //== CLASS DEFINITION =========================================================
00050 
00051 
00052 // ----------------------------------------------------------------------------
00053 
00068 template <typename GeoProperty>
00069 class oPropertyT : public BaseProperty
00070 {
00071 public:
00072 
00073   // Type of the encapsulated OpenSG Geometry Property
00074   typedef GeoProperty                                    property_t;
00075   typedef typename property_t::PtrType                   property_ptr_t;
00076 
00077   typedef typename property_t::StoredFieldType           field_t;
00078   typedef typename field_t::StoredType                   element_t;
00079   typedef typename field_t::StoredType                   value_type;
00080 
00081 public:
00082 
00083   //
00084   oPropertyT( property_ptr_t _geo_prop, 
00085               const std::string& _name = "<unknown>" ) 
00086     : BaseProperty(_name), data_( _geo_prop )
00087   { 
00088     osg_init_check();
00089   }
00090 
00091   //
00092   oPropertyT( const std::string& _name = "<unknown>" )
00093     : BaseProperty(_name), data_(NULL)
00094   {
00095     data_ = property_t::create();
00096     
00097     // make sure data_ is not null. In that case most probably
00098     // osg::osgInit() hasn't been executed!
00099     osg_init_check();
00100   }
00101 
00103   virtual ~oPropertyT() 
00104   { }
00105 
00106 public:
00107 
00108   oPropertyT& operator = (const oPropertyT& _rhs )
00109   {
00110     // Shallow copy! Remember, data_ is a osg pointer type, and the assign
00111     // operator makes a shallow copy!
00112     data_ = _rhs.data_;
00113     return *this;
00114 
00115   }
00116 
00117   
00118 public: // interface BaseProperty
00119 
00120   virtual void free_mem()         {}
00121   virtual void reserve(size_t _n) { data_->getField().reserve( _n );  }
00122   virtual void resize(size_t _n)  { data_->resize( _n ); }
00123   virtual void push_back()        { data_->resize( data_->size()+1 ); }
00124   virtual void swap(size_t _i0, size_t _i1)
00125   { std::swap( data_->getField()[_i0], data_->getField()[_i1] ); }
00126 
00127   virtual oPropertyT<property_t>* clone() const
00128   {
00129     oPropertyT<property_t> *dolly = new oPropertyT<property_t>();
00130     if (n_elements() > 0)
00131     {
00132       // OSGGeoProperty does not provide a deep copy
00133       dolly->resize(n_elements());
00134       element_t *begin = const_cast<element_t*>(data());
00135       element_t *end   = begin+n_elements();
00136       element_t *dst   = const_cast<element_t*>(dolly->data());
00137       std::copy( begin, end, dst );
00138     }
00139     return dolly;
00140   }
00141 
00142 public:
00143 
00144   virtual void set_persistent( bool _yn )
00145   {
00146     check_and_set_persistent<element_t>(_yn);
00147   }
00148 
00149   virtual size_t       n_elements() const
00150   { return data_==osg::NullFC ? UnknownSize : data_->getSize(); }
00151 
00152   virtual size_t       element_size() const
00153   { return UnknownSize; }
00154   
00155   virtual size_t store( std::ostream& _ostr, bool _swap ) const
00156   { return 0; }
00157 
00158   virtual size_t restore( std::istream& _istr, bool _swap )      
00159   { return 0; }
00160 
00161   
00162 public: // OpenSG GeoPropertyInterface compatibility
00163 
00164   void clear(void) { data_->clear(); }
00165 
00166 
00167 public: // access to OpenSG GeoProperty
00168 
00169   property_ptr_t& osg_ptr() 
00170   { return data_; }
00171 
00172   const property_ptr_t& osg_ptr() const
00173   { return data_; }
00174 
00175 
00176   const element_t *data() const  
00177   { return &( (*this)[ 0 ] ); }
00178 
00179   element_t& operator[](size_t idx) 
00180   { return data_->getField()[ idx ]; }
00181 
00182   const element_t& operator[](size_t idx) const 
00183   { return data_->getField()[ idx ]; }
00184 
00185 
00186 protected:
00187 
00188   property_ptr_t  data_;
00189 
00190 
00191 private:
00192 
00193   void osg_init_check(void)
00194   {
00195     // make sure data_ is not null. In that case most probably
00196     // osg::osgInit() hasn't been executed!
00197     if ( data_ == osg::NullFC )
00198       throw std::logic_error("OpenSG Runtime Environment is not initialized: " \
00199                              "Use osg::osgInit()");
00200   }
00201 
00202   oPropertyT( const oPropertyT& );
00203 };
00204 
00205 // ----------------------------------------------------------------- class ----
00206 
00207 
00208 // ------------------------------------------------------------ properties ----
00209 
00211 namespace VP {
00212 
00213   // ---------------------------------------- Positions
00215 
00216 
00217   typedef oPropertyT< osg::GeoPositions2d > GeoPositions2d;
00218   typedef oPropertyT< osg::GeoPositions2f > GeoPositions2f;
00219   typedef oPropertyT< osg::GeoPositions3d > GeoPositions3d;
00220   typedef oPropertyT< osg::GeoPositions3f > GeoPositions3f;
00221   typedef oPropertyT< osg::GeoPositions4d > GeoPositions4d;
00222   typedef oPropertyT< osg::GeoPositions4f > GeoPositions4f;
00224 
00225   // ---------------------------------------- Normals
00227 
00228 
00229   typedef oPropertyT< osg::GeoNormals3f > GeoNormals3f;
00231 
00232   // ---------------------------------------- TexCoords
00234 
00235 
00236   typedef oPropertyT< osg::GeoTexCoords1f > GeoTexCoords1f;
00237   typedef oPropertyT< osg::GeoTexCoords2f > GeoTexCoords2f;
00238   typedef oPropertyT< osg::GeoTexCoords3f > GeoTexCoords3f;
00240 
00241   // ---------------------------------------- Colors
00243 
00244 
00245   typedef oPropertyT< osg::GeoColors3f  > GeoColors3f;
00246   typedef oPropertyT< osg::GeoColors3ub > GeoColors3ub;
00247   typedef oPropertyT< osg::GeoColors4f  > GeoColors4f;
00248   typedef oPropertyT< osg::GeoColors4ub > GeoColors4ub;
00250 
00251 } // namespace VP
00252 
00253 
00255 namespace FP {
00256 
00257   // ---------------------------------------- Types
00259   typedef oPropertyT< osg::GeoPTypesUI8 > GeoPTypesUI8;
00260 
00261   // ---------------------------------------- Lengths
00263   typedef oPropertyT< osg::GeoPLengthsUI32 > GeoPLengthsUI32;
00264 
00265   // ---------------------------------------- Indices
00266 
00267   typedef oPropertyT< osg::GeoIndicesUI32 >  _GeoIndicesUI32;
00268 
00270   template < typename IsTriMesh >
00271   class GeoIndicesUI32 : public _GeoIndicesUI32
00272   {
00273   public: // ---------------------------------------- typedefs
00274 
00275     typedef _GeoIndicesUI32                      inherited_t;
00276     typedef typename inherited_t::property_ptr_t property_ptr_t;
00277 
00278   public: // ---------------------------------------- ctor/dtor
00279 
00280     GeoIndicesUI32( property_ptr_t     _geo_prop,
00281                     GeoPTypesUI8&      _types,
00282                     GeoPLengthsUI32&   _lengths)
00283       : inherited_t( _geo_prop ), types_(_types), length_(_lengths)
00284     { }
00285 
00286     GeoIndicesUI32( GeoPTypesUI8&      _types,
00287                     GeoPLengthsUI32&   _lengths)
00288       : inherited_t(), types_(_types), length_(_lengths)
00289     { }
00290 
00291     virtual ~GeoIndicesUI32() 
00292     { }
00293 
00294   public: // ---------------------------------------- inherited
00295     
00296     void swap(size_t _i0, size_t _i1) { _swap( _i0, _i1, IsTriMesh() ); }
00297     virtual void reserve(size_t _n)   { _reserve( _n, IsTriMesh() ); }
00298     virtual void resize(size_t _n)    { _resize( _n, IsTriMesh() ); }
00299 
00300   protected: // ------------------------------------- swap
00301 
00302     void _swap(size_t _i0, size_t _i1, GenProg::False )
00303     {
00304       omerr() << "Unsupported mesh type!" << std::endl;
00305       assert(0);
00306     }
00307     
00308     void _swap(size_t _i0, size_t _i1, GenProg::True )
00309     {
00310       size_t j0 = _i0 + _i0 + _i0;
00311       size_t j1 = _i1 + _i1 + _i1;
00312 
00313       inherited_t::swap(   j0,   j1 );
00314       inherited_t::swap( ++j0, ++j1 );
00315       inherited_t::swap( ++j0, ++j1 );
00316     }
00317 
00318     virtual void _reserve(size_t _n, GenProg::True )
00319     { inherited_t::reserve( _n + _n + _n ); }
00320 
00321     virtual void _reserve(size_t _n, GenProg::False )
00322     { assert( false ); }
00323 
00324     virtual void _resize(size_t _n, GenProg::True )
00325     { inherited_t::resize( _n + _n + _n ); }
00326 
00327     virtual void _resize(size_t _n, GenProg::False )
00328     { assert( false ); }
00329 
00330 
00331   protected:
00332 
00333     GeoPTypesUI8    &types_;
00334     GeoPLengthsUI32 &length_;
00335 
00336   };
00337 
00338 } // namespace FP
00339 
00340 
00341 // ----------------------------------------------------------------------------
00342 
00343 #ifndef DOXY_IGNORE_THIS
00344 
00345 template <typename T> struct _t2vp;
00346 template <> struct _t2vp< osg::Pnt2f > 
00347 { typedef osg::GeoPositions2f type; typedef VP::GeoPositions2f prop; };
00348 
00349 template <> struct _t2vp< osg::Pnt3f > 
00350 { typedef osg::GeoPositions3f type; typedef VP::GeoPositions3f prop; };
00351 
00352 template <> struct _t2vp< osg::Pnt4f >
00353 { typedef osg::GeoPositions4f type; typedef VP::GeoPositions4f prop; };
00354 
00355 template <> struct _t2vp< osg::Pnt2d >
00356 { typedef osg::GeoPositions2d type; typedef VP::GeoPositions2d prop; };
00357 template <> struct _t2vp< osg::Pnt3d >
00358 { typedef osg::GeoPositions3d type; typedef VP::GeoPositions3d prop; };
00359 template <> struct _t2vp< osg::Pnt4d >
00360 { typedef osg::GeoPositions4d type; typedef VP::GeoPositions4d prop; };
00361 
00362 template <typename T> struct _t2vn;
00363 template <> struct _t2vn< osg::Vec3f > 
00364 { typedef osg::GeoNormals3f   type; typedef VP::GeoNormals3f   prop; };
00365 
00366 template <typename T> struct _t2vc;
00367 template <> struct _t2vc< osg::Color3f >  
00368 { typedef osg::GeoColors3f  type;   typedef VP::GeoColors3f    prop; };
00369 
00370 template <> struct _t2vc< osg::Color4f >
00371 { typedef osg::GeoColors4f  type;   typedef VP::GeoColors4f    prop; };
00372 
00373 template <> struct _t2vc< osg::Color3ub >
00374 { typedef osg::GeoColors3ub  type;   typedef VP::GeoColors3ub    prop; };
00375 
00376 template <> struct _t2vc< osg::Color4ub >
00377 { typedef osg::GeoColors4ub  type;   typedef VP::GeoColors3ub    prop; };
00378 
00379 template <typename T> struct _t2vtc;
00380 template <> struct _t2vtc< osg::Vec2f > 
00381 { typedef osg::GeoTexCoords2f type;  typedef VP::GeoTexCoords2f  prop; };
00382 
00383 template <> struct _t2vtc< osg::Vec3f >
00384 { typedef osg::GeoTexCoords3f type;  typedef VP::GeoTexCoords3f  prop; };
00385 
00386 #endif
00387 
00388 //=============================================================================
00389 } // namespace Kernel_OSG
00390 } // namespace OpenMesh
00391 //=============================================================================
00392 #endif // OPENMESH_PROPERTYT_HH defined
00393 //=============================================================================
00394 

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