OpenMesh
OpenMesh/Tools/Kernel_OSG/PropertyT.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------* 
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         * 
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/ 
00034 
00035 /*===========================================================================*\
00036  *                                                                           *             
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 #ifndef OPENMESH_KERNEL_OSG_PROPERTYT_HH
00043 #define OPENMESH_KERNEL_OSG_PROPERTYT_HH
00044 
00045 
00046 //== INCLUDES =================================================================
00047 
00048 #include <OpenMesh/Core/Mesh/Attributes.hh>
00049 #include <OpenMesh/Core/Mesh/BaseKernel.hh>
00050 #include <OpenMesh/Core/Utils/GenProg.hh>
00051 #include <OpenMesh/Core/Utils/Property.hh>
00052 //
00053 #include <osg/Geometry>
00054 //
00055 #include <stdexcept>
00056 #include <vector>
00057 
00058 
00059 //== NAMESPACES ===============================================================
00060 
00061 namespace OpenMesh {
00062 namespace Kernel_OSG {
00063 
00064 
00065 //== CLASS DEFINITION =========================================================
00066 
00067 
00068 // ----------------------------------------------------------------------------
00069 
00084 template <typename GeoProperty>
00085 class oPropertyT : public BaseProperty
00086 {
00087 public:
00088 
00089   // Type of the encapsulated OpenSG Geometry Property
00090   typedef GeoProperty                                    property_t;
00091   typedef typename property_t::PtrType                   property_ptr_t;
00092 
00093   typedef typename property_t::StoredFieldType           field_t;
00094   typedef typename field_t::StoredType                   element_t;
00095   typedef typename field_t::StoredType                   value_type;
00096 
00097 public:
00098 
00099   //
00100   oPropertyT( property_ptr_t _geo_prop, 
00101               const std::string& _name = "<unknown>" ) 
00102     : BaseProperty(_name), data_( _geo_prop )
00103   { 
00104     osg_init_check();
00105   }
00106 
00107   //
00108   oPropertyT( const std::string& _name = "<unknown>" )
00109     : BaseProperty(_name), data_(NULL)
00110   {
00111     data_ = property_t::create();
00112     
00113     // make sure data_ is not null. In that case most probably
00114     // osg::osgInit() hasn't been executed!
00115     osg_init_check();
00116   }
00117 
00119   virtual ~oPropertyT() 
00120   { }
00121 
00122 public:
00123 
00124   oPropertyT& operator = (const oPropertyT& _rhs )
00125   {
00126     // Shallow copy! Remember, data_ is a osg pointer type, and the assign
00127     // operator makes a shallow copy!
00128     data_ = _rhs.data_;
00129     return *this;
00130 
00131   }
00132 
00133   
00134 public: // interface BaseProperty
00135 
00136   virtual void reserve(size_t _n) { data_->getField().reserve( _n );  }
00137   virtual void resize(size_t _n)  { data_->resize( _n ); }
00138   virtual void push_back()        { data_->resize( data_->size()+1 ); }
00139   virtual void swap(size_t _i0, size_t _i1)
00140   { std::swap( data_->getField()[_i0], data_->getField()[_i1] ); }
00141 
00142   virtual oPropertyT<property_t>* clone() const
00143   {
00144     oPropertyT<property_t> *dolly = new oPropertyT<property_t>();
00145     if (n_elements() > 0)
00146     {
00147       // OSGGeoProperty does not provide a deep copy
00148       dolly->resize(n_elements());
00149       element_t *begin = const_cast<element_t*>(data());
00150       element_t *end   = begin+n_elements();
00151       element_t *dst   = const_cast<element_t*>(dolly->data());
00152       std::copy( begin, end, dst );
00153     }
00154     return dolly;
00155   }
00156 
00157 public:
00158 
00159   virtual void set_persistent( bool _yn )
00160   {
00161     check_and_set_persistent<element_t>(_yn);
00162   }
00163 
00164   virtual size_t       n_elements() const
00165   { return data_==osg::NullFC ? UnknownSize : data_->getSize(); }
00166 
00167   virtual size_t       element_size() const
00168   { return UnknownSize; }
00169   
00170   virtual size_t store( std::ostream& _ostr, bool _swap ) const
00171   { return 0; }
00172 
00173   virtual size_t restore( std::istream& _istr, bool _swap )      
00174   { return 0; }
00175 
00176   
00177 public: // OpenSG GeoPropertyInterface compatibility
00178 
00179   void clear(void) { data_->clear(); }
00180 
00181 
00182 public: // access to OpenSG GeoProperty
00183 
00184   property_ptr_t& osg_ptr() 
00185   { return data_; }
00186 
00187   const property_ptr_t& osg_ptr() const
00188   { return data_; }
00189 
00190 
00191   const element_t *data() const  
00192   { return &( (*this)[ 0 ] ); }
00193 
00194   element_t& operator[](size_t idx) 
00195   { return data_->getField()[ idx ]; }
00196 
00197   const element_t& operator[](size_t idx) const 
00198   { return data_->getField()[ idx ]; }
00199 
00200 
00201 protected:
00202 
00203   property_ptr_t  data_;
00204 
00205 
00206 private:
00207 
00208   void osg_init_check(void)
00209   {
00210     // make sure data_ is not null. In that case most probably
00211     // osg::osgInit() hasn't been executed!
00212     if ( data_ == osg::NullFC )
00213       throw std::logic_error("OpenSG Runtime Environment is not initialized: " \
00214                              "Use osg::osgInit()");
00215   }
00216 
00217   oPropertyT( const oPropertyT& );
00218 };
00219 
00220 // ----------------------------------------------------------------- class ----
00221 
00222 
00223 // ------------------------------------------------------------ properties ----
00224 
00226 namespace VP {
00227 
00228   // ---------------------------------------- Positions
00230 
00231 
00232   typedef oPropertyT< osg::GeoPositions2d > GeoPositions2d;
00233   typedef oPropertyT< osg::GeoPositions2f > GeoPositions2f;
00234   typedef oPropertyT< osg::GeoPositions3d > GeoPositions3d;
00235   typedef oPropertyT< osg::GeoPositions3f > GeoPositions3f;
00236   typedef oPropertyT< osg::GeoPositions4d > GeoPositions4d;
00237   typedef oPropertyT< osg::GeoPositions4f > GeoPositions4f;
00239 
00240   // ---------------------------------------- Normals
00242 
00243 
00244   typedef oPropertyT< osg::GeoNormals3f > GeoNormals3f;
00246 
00247   // ---------------------------------------- TexCoords
00249 
00250 
00251   typedef oPropertyT< osg::GeoTexCoords1f > GeoTexCoords1f;
00252   typedef oPropertyT< osg::GeoTexCoords2f > GeoTexCoords2f;
00253   typedef oPropertyT< osg::GeoTexCoords3f > GeoTexCoords3f;
00255 
00256   // ---------------------------------------- Colors
00258 
00259 
00260   typedef oPropertyT< osg::GeoColors3f  > GeoColors3f;
00261   typedef oPropertyT< osg::GeoColors3ub > GeoColors3ub;
00262   typedef oPropertyT< osg::GeoColors4f  > GeoColors4f;
00263   typedef oPropertyT< osg::GeoColors4ub > GeoColors4ub;
00265 
00266 } // namespace VP
00267 
00268 
00270 namespace FP {
00271 
00272   // ---------------------------------------- Types
00274   typedef oPropertyT< osg::GeoPTypesUI8 > GeoPTypesUI8;
00275 
00276   // ---------------------------------------- Lengths
00278   typedef oPropertyT< osg::GeoPLengthsUI32 > GeoPLengthsUI32;
00279 
00280   // ---------------------------------------- Indices
00281 
00282   typedef oPropertyT< osg::GeoIndicesUI32 >  _GeoIndicesUI32;
00283 
00285   template < typename IsTriMesh >
00286   class GeoIndicesUI32 : public _GeoIndicesUI32
00287   {
00288   public: // ---------------------------------------- typedefs
00289 
00290     typedef _GeoIndicesUI32                      inherited_t;
00291     typedef typename inherited_t::property_ptr_t property_ptr_t;
00292 
00293   public: // ---------------------------------------- ctor/dtor
00294 
00295     GeoIndicesUI32( property_ptr_t     _geo_prop,
00296                     GeoPTypesUI8&      _types,
00297                     GeoPLengthsUI32&   _lengths)
00298       : inherited_t( _geo_prop ), types_(_types), length_(_lengths)
00299     { }
00300 
00301     GeoIndicesUI32( GeoPTypesUI8&      _types,
00302                     GeoPLengthsUI32&   _lengths)
00303       : inherited_t(), types_(_types), length_(_lengths)
00304     { }
00305 
00306     virtual ~GeoIndicesUI32() 
00307     { }
00308 
00309   public: // ---------------------------------------- inherited
00310     
00311     void swap(size_t _i0, size_t _i1) { _swap( _i0, _i1, IsTriMesh() ); }
00312     virtual void reserve(size_t _n)   { _reserve( _n, IsTriMesh() ); }
00313     virtual void resize(size_t _n)    { _resize( _n, IsTriMesh() ); }
00314 
00315   protected: // ------------------------------------- swap
00316 
00317     void _swap(size_t _i0, size_t _i1, GenProg::False )
00318     {
00319       omerr() << "Unsupported mesh type!" << std::endl;
00320       assert(0);
00321     }
00322     
00323     void _swap(size_t _i0, size_t _i1, GenProg::True )
00324     {
00325       size_t j0 = _i0 + _i0 + _i0;
00326       size_t j1 = _i1 + _i1 + _i1;
00327 
00328       inherited_t::swap(   j0,   j1 );
00329       inherited_t::swap( ++j0, ++j1 );
00330       inherited_t::swap( ++j0, ++j1 );
00331     }
00332 
00333     virtual void _reserve(size_t _n, GenProg::True )
00334     { inherited_t::reserve( _n + _n + _n ); }
00335 
00336     virtual void _reserve(size_t _n, GenProg::False )
00337     { assert( false ); }
00338 
00339     virtual void _resize(size_t _n, GenProg::True )
00340     { inherited_t::resize( _n + _n + _n ); }
00341 
00342     virtual void _resize(size_t _n, GenProg::False )
00343     { assert( false ); }
00344 
00345 
00346   protected:
00347 
00348     GeoPTypesUI8    &types_;
00349     GeoPLengthsUI32 &length_;
00350 
00351   };
00352 
00353 } // namespace FP
00354 
00355 
00356 // ----------------------------------------------------------------------------
00357 
00358 #ifndef DOXY_IGNORE_THIS
00359 
00360 template <typename T> struct _t2vp;
00361 template <> struct _t2vp< osg::Pnt2f > 
00362 { typedef osg::GeoPositions2f type; typedef VP::GeoPositions2f prop; };
00363 
00364 template <> struct _t2vp< osg::Pnt3f > 
00365 { typedef osg::GeoPositions3f type; typedef VP::GeoPositions3f prop; };
00366 
00367 template <> struct _t2vp< osg::Pnt4f >
00368 { typedef osg::GeoPositions4f type; typedef VP::GeoPositions4f prop; };
00369 
00370 template <> struct _t2vp< osg::Pnt2d >
00371 { typedef osg::GeoPositions2d type; typedef VP::GeoPositions2d prop; };
00372 template <> struct _t2vp< osg::Pnt3d >
00373 { typedef osg::GeoPositions3d type; typedef VP::GeoPositions3d prop; };
00374 template <> struct _t2vp< osg::Pnt4d >
00375 { typedef osg::GeoPositions4d type; typedef VP::GeoPositions4d prop; };
00376 
00377 template <typename T> struct _t2vn;
00378 template <> struct _t2vn< osg::Vec3f > 
00379 { typedef osg::GeoNormals3f   type; typedef VP::GeoNormals3f   prop; };
00380 
00381 template <typename T> struct _t2vc;
00382 template <> struct _t2vc< osg::Color3f >  
00383 { typedef osg::GeoColors3f  type;   typedef VP::GeoColors3f    prop; };
00384 
00385 template <> struct _t2vc< osg::Color4f >
00386 { typedef osg::GeoColors4f  type;   typedef VP::GeoColors4f    prop; };
00387 
00388 template <> struct _t2vc< osg::Color3ub >
00389 { typedef osg::GeoColors3ub  type;   typedef VP::GeoColors3ub    prop; };
00390 
00391 template <> struct _t2vc< osg::Color4ub >
00392 { typedef osg::GeoColors4ub  type;   typedef VP::GeoColors3ub    prop; };
00393 
00394 template <typename T> struct _t2vtc;
00395 template <> struct _t2vtc< osg::Vec2f > 
00396 { typedef osg::GeoTexCoords2f type;  typedef VP::GeoTexCoords2f  prop; };
00397 
00398 template <> struct _t2vtc< osg::Vec3f >
00399 { typedef osg::GeoTexCoords3f type;  typedef VP::GeoTexCoords3f  prop; };
00400 
00401 #endif
00402 
00403 //=============================================================================
00404 } // namespace Kernel_OSG
00405 } // namespace OpenMesh
00406 //=============================================================================
00407 #endif // OPENMESH_PROPERTYT_HH defined
00408 //=============================================================================
00409 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines