OpenMesh
OpenMesh/Tools/Kernel_OSG/VectorAdapter.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 // ----------------------------------------------------------------------------
00043 
00044 #ifndef OPENMESH_KERNEL_OSG_VECTORADAPTER_HH
00045 #define OPENMESH_KERNEL_OSG_VECTORADAPTER_HH
00046 
00047 
00048 //== INCLUDES =================================================================
00049 
00050 #include <osg/Geometry>
00051 #include <OpenMesh/Core/Utils/vector_cast.hh>
00052 
00053 //== NAMESPACES ===============================================================
00054 
00055 namespace OpenMesh {
00056 
00057 //== CLASS DEFINITION =========================================================
00058 
00059 // ----------------------------------------------------------------- class ----
00060 
00061 #define OSG_VECTOR_TRAITS( VecType ) \
00062   template <> struct vector_traits< VecType > { \
00063     typedef VecType                vector_type; \
00064     typedef vector_type::ValueType value_type;  \
00065     typedef GenProg::Int2Type< vector_type::_iSize > typed_size; \
00066     \
00067     static const size_t size_ = vector_type::_iSize; \
00068     static size_t size() { return size_; } \
00069   }
00070 
00072 OSG_VECTOR_TRAITS( osg::Pnt4f );
00074 OSG_VECTOR_TRAITS( osg::Pnt3f );
00076 OSG_VECTOR_TRAITS( osg::Pnt2f );
00077 
00079 OSG_VECTOR_TRAITS( osg::Vec4f );
00081 OSG_VECTOR_TRAITS( osg::Vec3f );
00083 OSG_VECTOR_TRAITS( osg::Vec2f );
00084 
00086 OSG_VECTOR_TRAITS( osg::Pnt4d );
00088 OSG_VECTOR_TRAITS( osg::Pnt3d );
00090 OSG_VECTOR_TRAITS( osg::Pnt2d );
00091 
00093 OSG_VECTOR_TRAITS( osg::Vec4d );
00095 OSG_VECTOR_TRAITS( osg::Vec3d );
00096 
00098 OSG_VECTOR_TRAITS( osg::Vec4ub );
00099 
00100 
00101 // ----------------------------------------------------------------------------
00102 
00103 
00104 #define OSG_COLOR_TRAITS( VecType, N ) \
00105   template <> struct vector_traits< VecType > { \
00106     typedef VecType                vector_type; \
00107     typedef vector_type::ValueType value_type;  \
00108     typedef GenProg::Int2Type< N > typed_size; \
00109     \
00110     static const size_t size_ = N; \
00111     static size_t size() { return size_; } \
00112   }
00113 
00114 
00116 OSG_COLOR_TRAITS( osg::Color3ub, 3 );
00118 OSG_COLOR_TRAITS( osg::Color4ub, 4 );
00120 OSG_COLOR_TRAITS( osg::Color3f,  3 );
00122 OSG_COLOR_TRAITS( osg::Color4f,  4 );
00123 
00124 #undef OSG_VECTOR_TRAITS
00125 
00126 
00127 // ----------------------------------------
00128 #if 1
00129 #define PNT2VEC_CASTER( DST, SRC ) \
00130   template <> struct vector_caster< DST, SRC > { \
00131     typedef DST   dst_t; \
00132     typedef SRC   src_t; \
00133     typedef const dst_t& return_type; \
00134     inline static return_type cast( const src_t& _src ) {\
00135       return _src.subZero(); \
00136     } \
00137   }
00138 
00140 PNT2VEC_CASTER( osg::Vec3f, osg::Pnt3f );
00141 
00143 PNT2VEC_CASTER( osg::Vec4f, osg::Pnt4f );
00144 
00146 PNT2VEC_CASTER( osg::Vec3d, osg::Pnt3d );
00147 
00149 PNT2VEC_CASTER( osg::Vec4d, osg::Pnt4d );
00150 
00151 #undef PNT2VEC
00152 #else
00153  
00154   template <> 
00155   struct vector_caster< osg::Vec3f, osg::Pnt3f > 
00156   {
00157     typedef osg::Vec3f   dst_t;
00158     typedef osg::Pnt3f   src_t;
00159 
00160     typedef const dst_t& return_type;
00161     inline static return_type cast( const src_t& _src ) 
00162     {
00163       std::cout << "casting Pnt3f to Vec3f\n";
00164       return _src.subZero();
00165     }
00166   };
00167 
00168 #endif
00169 // ----------------------------------------
00170 
00172 
00173 inline
00174 osg::Vec3f::ValueType dot( const osg::Vec3f &_v1, const osg::Vec3f &_v2 )
00175 { return _v1.dot(_v2); }
00176 
00177 
00178 inline
00179 osg::Vec3f::ValueType dot( const osg::Vec3f &_v1, const osg::Pnt3f &_v2 )
00180 { return _v1.dot(_v2); }
00181 
00182 
00183 inline
00184 osg::Vec2f::ValueType dot( const osg::Vec2f &_v1, const osg::Vec2f &_v2 )
00185 { return _v1.dot(_v2); }
00186 
00187 
00188 inline
00189 osg::Vec3f cross( const osg::Vec3f &_v1, const osg::Vec3f &_v2 )
00190 { return _v1.cross(_v2); }
00192 
00193 //=============================================================================
00194 } // namespace OpenMesh
00195 //=============================================================================
00196 #endif // OPENMESH_VECTORADAPTER_HH defined
00197 //=============================================================================
00198 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines