WGEGeodeUtils.h

00001 //---------------------------------------------------------------------------
00002 //
00003 // Project: OpenWalnut ( http://www.openwalnut.org )
00004 //
00005 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
00006 // For more information see http://www.openwalnut.org/copying
00007 //
00008 // This file is part of OpenWalnut.
00009 //
00010 // OpenWalnut is free software: you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as published by
00012 // the Free Software Foundation, either version 3 of the License, or
00013 // (at your option) any later version.
00014 //
00015 // OpenWalnut is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 // GNU Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public License
00021 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
00022 //
00023 //---------------------------------------------------------------------------
00024 
00025 #ifndef WGEGEODEUTILS_H
00026 #define WGEGEODEUTILS_H
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 #include <osg/Array>
00032 #include <osg/Geode>
00033 #include <osg/Geometry>
00034 #include <osg/MatrixTransform>
00035 #include <osg/PositionAttitudeTransform>
00036 #include <osg/Vec3>
00037 #include <osgText/Text>
00038 
00039 #include "../common/math/WLine.h"
00040 #include "../common/math/WPlane.h"
00041 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
00042 #include "../common/WColor.h"
00043 #include "../common/datastructures/WColoredVertices.h"
00044 #include "WExportWGE.h"
00045 #include "WGEGeometryUtils.h"
00046 #include "WGESubdividedPlane.h"
00047 #include "WGEUtils.h"
00048 #include "WTriangleMesh.h"
00049 
00050 namespace wge
00051 {
00052     /**
00053      * Generates an OSG geode for the bounding box.
00054      *
00055      * \param bb The axis aligned bounding box to generate a geode from.
00056      * \param color The color in which the bounding box should be generated
00057      *
00058      * \return The OSG geode containing the 12 edges of the box.
00059      */
00060     osg::ref_ptr< osg::Geode > WGE_EXPORT generateBoundingBoxGeode( const WBoundingBox& bb, const WColor& color );
00061 
00062     /**
00063      * Generates an OSG node for the specified bounding box. It uses solid faces. This actually returns a MatrixTransform node and is especially
00064      * useful for shader based raytracing.
00065      *
00066      * \param bb The axis aligned bounding box
00067      * \param color The color in which the bounding box should be generated
00068      * \param threeDTexCoords True if 3D texture coordinates should be created.
00069      *
00070      * \return The OSG node containing the 12 edges of the box.
00071      */
00072     osg::ref_ptr< osg::Node > WGE_EXPORT generateSolidBoundingBoxNode( const WBoundingBox& bb, const WColor& color, bool threeDTexCoords = true );
00073 
00074     /**
00075      * Creates a osg::Geometry containing an unit cube, having 3D texture coordinates.
00076      *
00077      * \param color the color to set for all vertices
00078      *
00079      * \return the geometry
00080      */
00081     osg::ref_ptr< osg::Geometry > WGE_EXPORT createUnitCube( const WColor& color );
00082 
00083     /**
00084      * Creates a osg::Geometry containing an unit cube as line-strips, having 3D texture coordinates.
00085      *
00086      * \param color the color to set for all vertices
00087      *
00088      * \return the geometry
00089      */
00090     osg::ref_ptr< osg::Geometry > WGE_EXPORT createUnitCubeAsLines( const WColor& color );
00091 
00092     /**
00093      * Extract the vertices and triangles from a WTriangleMesh and save them
00094      * into an osg::Geometry. It can use the normals and per-vertex colors of the mesh.
00095      *
00096      * \param mesh the WTriangleMesh used as input
00097      * \param includeNormals When true, calculate the vertex normals and include
00098      *                       them into the geometry.
00099      * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
00100      * \param lighting if true, a standard lighting is activated for this geometry
00101      * \param useMeshColor if true, the mesh color is used. If false, the defaultColor is used.
00102      * \return an osg::Geometry containing the mesh
00103      * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
00104      */
00105     osg::ref_ptr< osg::Geometry > convertToOsgGeometry( WTriangleMesh::SPtr mesh,
00106                                                         const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
00107                                                         bool includeNormals = false,
00108                                                         bool lighting = false,
00109                                                         bool useMeshColor = true );
00110 
00111     /**
00112      * Extract the vertices and triangles from a WTriangleMesh and save them
00113      * into an osg::Geometry. It can use the normals and per-vertex colors of the mesh.
00114      * This method additionally uses the specified vertexID-color map to provide additional coloring.
00115      *
00116      * \param mesh the WTriangleMesh used as input
00117      * \param colorMap the map from vertex to color.
00118      * \param includeNormals When true, calculate the vertex normals and include
00119      *                       them into the geometry.
00120      * \param defaultColor This color is used in case the colorMap does not provide a color for a vertex
00121      * \param lighting if true, a standard lighting is activated for this geometry*
00122      * \return an osg::Geometry containing the mesh
00123      * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
00124      */
00125     osg::ref_ptr< osg::Geometry > WGE_EXPORT convertToOsgGeometry( WTriangleMesh::SPtr mesh, const WColoredVertices& colorMap,
00126                                                                    const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
00127                                                                    bool includeNormals = false,
00128                                                                    bool lighting = false
00129                                                                    );
00130 
00131     /**
00132      * Generates a line geode with thickness and color as parameters.
00133      *
00134      * \param line sequence of points
00135      * \param thickness How thick the line strip should be
00136      * \param color If present this color is used for the whole line, otherwise local coloring is used
00137      *
00138      * \return The new assembled geode for this line
00139      */
00140     osg::ref_ptr< osg::Geode > WGE_EXPORT generateLineStripGeode( const WLine& line,
00141                                                                   const float thickness = 3.0f,
00142                                                                   const WColor& color = WColor( 0, 0, 0, 0 ) );
00143 
00144     /**
00145      * helper function to add a label somewhere
00146      *
00147      * \param position position of the label
00148      * \param text text
00149      * \return a positionattitudetransfom object containing the label
00150      */
00151     osg::ref_ptr< osg::PositionAttitudeTransform > WGE_EXPORT addLabel( osg::Vec3 position, std::string text );
00152 
00153     /**
00154      * helper function to add a label with it's position vector
00155      *
00156      * \param position position of the label
00157      * \return a positionattitudetransfom object containing the label
00158      */
00159     osg::ref_ptr< osg::PositionAttitudeTransform > WGE_EXPORT vector2label( osg::Vec3 position );
00160 
00161     /**
00162      * Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane.
00163      *
00164      * \param xSize how far the plane from its center along the x-axis should be drawn (both directions)
00165      * \param ySize how far the plane from its center along the y-axis should be drawn (both directions)
00166      * \param p The plane instance
00167      * \param color The color of the plane
00168      * \param border If true than a border around each plane is drawn in inverse color of the plane
00169      *
00170      * \return The new assembled geode for this plane
00171      */
00172     osg::ref_ptr< osg::Geode > WGE_EXPORT genFinitePlane( double xSize,
00173                                                           double ySize,
00174                                                           const WPlane& p,
00175                                                           const WColor& color = WColor( 0.0, 0.7, 0.7, 1.0 ),
00176                                                           bool border = false );
00177 
00178     /**
00179      * Create a coordinate system. The coordinate system is build from cylinders and cones and includes a positive-to-negative
00180      * color gradient.
00181      *
00182      * \param middle osg::Vec3( middleX, middleY, middleZ ) middle points of X, Y, Z
00183      * \param sizeX whole lenght of X
00184      * \param sizeY whole lenght of Y
00185      * \param sizeZ whole lenght of Z
00186      *
00187      * \return Group Node
00188      */
00189     osg::ref_ptr< osg::Group > creatCoordinateSystem(
00190         osg::Vec3 middle,
00191         double sizeX,
00192         double sizeY,
00193         double sizeZ
00194     );
00195 
00196     /**
00197      * Generates a geode out of two vectors and an origin position.
00198      *
00199      * \param base the origin position. NOT the center.
00200      * \param a the first vector spanning the plane
00201      * \param b the second vector spanning the plane
00202      *
00203      * \return the geode
00204      */
00205     osg::ref_ptr< osg::Geode > WGE_EXPORT genFinitePlane( osg::Vec3 const& base, osg::Vec3 const& a, osg::Vec3 const& b );
00206 
00207     /**
00208      * Generates a plane subdivided into quads.
00209      *
00210      * \param resX How many quads in x-direction
00211      * \param resY How many quads in y-direction
00212      * \param spacing Not implement yet
00213      *
00214      * \return The new uncolored plane geode
00215      */
00216     osg::ref_ptr< WGESubdividedPlane > WGE_EXPORT genUnitSubdividedPlane( size_t resX, size_t resY, double spacing = 0.01 );
00217 
00218     /**
00219      * For each points in the STL container generate small cubes.
00220      *
00221      * \param points Center point of the cubes
00222      * \param size The size of the cubes
00223      * \param color The color of the cubes
00224      * \tparam An STL container with WPositions as elements ( don't try it with different than vector, set, list or queue )
00225      *
00226      * \return Geode with as many cubes as points in the container where each cube is around a certain position.
00227      */
00228     template< class Container > osg::ref_ptr< osg::Geode > genPointBlobs( boost::shared_ptr< Container > points,
00229                                                                           double size,
00230                                                                           const WColor& color = WColor( 1.0, 0.0, 0.0, 1.0 ) );
00231 } // end of namespace wge
00232 
00233 template< class Container > inline osg::ref_ptr< osg::Geode > wge::genPointBlobs( boost::shared_ptr< Container > points,
00234                                                                                   double size,
00235                                                                                   const WColor& color )
00236 {
00237     osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
00238     osg::ref_ptr< osg::Vec4Array > colors   = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
00239     osg::ref_ptr< osg::Geometry >  geometry = osg::ref_ptr< osg::Geometry >( new osg::Geometry );
00240     osg::ref_ptr< osg::Vec3Array > normals  = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
00241 
00242     for( typename Container::const_iterator point = points->begin(); point != points->end(); ++point )
00243     {
00244         const WPosition& pos = *point;
00245         std::vector< WPosition > corners;
00246         corners.reserve( 8 );
00247         double halfSize = size / 2.0;
00248         corners.push_back( WPosition( pos[0] - halfSize, pos[1] - halfSize, pos[2] - halfSize ) );
00249         corners.push_back( WPosition( pos[0] + halfSize, pos[1] - halfSize, pos[2] - halfSize ) );
00250         corners.push_back( WPosition( pos[0] + halfSize, pos[1] - halfSize, pos[2] + halfSize ) );
00251         corners.push_back( WPosition( pos[0] - halfSize, pos[1] - halfSize, pos[2] + halfSize ) );
00252         corners.push_back( WPosition( pos[0] - halfSize, pos[1] + halfSize, pos[2] - halfSize ) );
00253         corners.push_back( WPosition( pos[0] + halfSize, pos[1] + halfSize, pos[2] - halfSize ) );
00254         corners.push_back( WPosition( pos[0] + halfSize, pos[1] + halfSize, pos[2] + halfSize ) );
00255         corners.push_back( WPosition( pos[0] - halfSize, pos[1] + halfSize, pos[2] + halfSize ) );
00256 
00257         osg::ref_ptr< osg::Vec3Array > ver = generateCuboidQuads( corners );
00258         vertices->insert( vertices->end(), ver->begin(), ver->end() );
00259         osg::ref_ptr< osg::Vec3Array > nor = generateCuboidQuadNormals( corners );
00260         normals->insert( normals->end(), nor->begin(), nor->end() );
00261         geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, vertices->size() - ver->size(), ver->size() ) );
00262     }
00263 
00264     geometry->setVertexArray( vertices );
00265     colors->push_back( color );
00266     geometry->setColorArray( colors );
00267     geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
00268     geometry->setNormalArray( normals );
00269     geometry->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE );
00270     osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >( new osg::Geode );
00271     geode->addDrawable( geometry );
00272     return geode;
00273 }
00274 #endif  // WGEGEODEUTILS_H
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends