OpenWalnut  1.4.0
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/Drawable>
00035 #include <osg/MatrixTransform>
00036 #include <osg/PositionAttitudeTransform>
00037 #include <osg/Vec3>
00038 #include <osgText/Text>
00039 
00040 #include "../common/math/WLine.h"
00041 #include "../common/math/WPlane.h"
00042 #include "../common/math/linearAlgebra/WPosition.h"
00043 #include "../common/WColor.h"
00044 #include "../common/datastructures/WColoredVertices.h"
00045 
00046 #include "WGEGeometryUtils.h"
00047 #include "WGESubdividedPlane.h"
00048 #include "WGEUtils.h"
00049 #include "WTriangleMesh.h"
00050 
00051 namespace wge
00052 {
00053     /**
00054      * Generates an OSG geode for the bounding box.
00055      *
00056      * \param bb The axis aligned bounding box to generate a geode from.
00057      * \param color The color in which the bounding box should be generated
00058      *
00059      * \return The OSG geode containing the 12 edges of the box.
00060      */
00061     osg::ref_ptr< osg::Geode > generateBoundingBoxGeode( const WBoundingBox& bb, const WColor& color );
00062 
00063     /**
00064      * Generates an OSG node for the specified bounding box. It uses solid faces. This actually returns a MatrixTransform node and is especially
00065      * useful for shader based raytracing.
00066      *
00067      * \param bb The axis aligned bounding box
00068      * \param color The color in which the bounding box should be generated
00069      * \param threeDTexCoords True if 3D texture coordinates should be created.
00070      *
00071      * \return The OSG node containing the 12 edges of the box.
00072      */
00073     osg::ref_ptr< osg::Node > generateSolidBoundingBoxNode( const WBoundingBox& bb, const WColor& color, bool threeDTexCoords = true );
00074 
00075     /**
00076      * Creates a osg::Geometry containing an unit cube, having 3D texture coordinates.
00077      *
00078      * \param color the color to set for all vertices
00079      *
00080      * \return the geometry
00081      */
00082     osg::ref_ptr< osg::Geometry > createUnitCube( const WColor& color );
00083 
00084     /**
00085      * Create an arbitrary cube and insert it into the given geometry. You will get a cube with normals, tex coords and a single color. This
00086      * function is very restrictive with respect to the given geode. It is not recommended to use it in combination with an geometry you already
00087      * filled with something. It is designed to be used to fill many cubes into one geometry.
00088      *
00089      * \param position position of the lower,left,front vertex
00090      * \param size size in x,y,z direction
00091      * \param color a color
00092      * \param geometry the geometry where to add the cube. It can be completely empty (no arrays). If you use an invalid (in the following
00093      * terms) geometry here, the graphics will fail. Ensure that you have a color array, tex array, normal array and vertex array, where the
00094      * color array is used per primitive set and normals/tex are bound per vertex. The vertices/tex/normals need to be vec3 arrays. The color
00095      * array needs to be a vec4 array.
00096      *
00097      * \return the drawable.
00098      */
00099     void createCube( osg::ref_ptr< osg::Geometry > geometry, const WPosition& position, const WPosition& size, const WColor& color );
00100 
00101     /**
00102      * Create an arbitrary cube.
00103      *
00104      * \param position position of the lower,left,front vertex
00105      * \param size size in x,y,z direction
00106      * \param color a color
00107      *
00108      * \return the drawable.
00109      */
00110     osg::ref_ptr< osg::Geometry > createCube( const WPosition& position, const WPosition& size, const WColor& color );
00111 
00112     /**
00113      * Creates a osg::Geometry containing an unit cube as line-strips, having 3D texture coordinates.
00114      *
00115      * \param color the color to set for all vertices
00116      *
00117      * \return the geometry
00118      */
00119     osg::ref_ptr< osg::Geometry > createUnitCubeAsLines( const WColor& color );
00120 
00121     /**
00122      * Extract the vertices and triangles from a WTriangleMesh and save them
00123      * into an osg::Geometry. It can use the normals and per-vertex colors of the mesh.
00124      *
00125      * \param mesh the WTriangleMesh used as input
00126      * \param includeNormals When true, calculate the vertex normals and include
00127      *                       them into the geometry.
00128      * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
00129      * \param lighting if true, a standard lighting is activated for this geometry
00130      * \param useMeshColor if true, the mesh color is used. If false, the defaultColor is used.
00131      * \return an osg::Geometry containing the mesh
00132      * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
00133      */
00134     osg::ref_ptr< osg::Geometry > convertToOsgGeometry( WTriangleMesh::SPtr mesh,
00135                                                         const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
00136                                                         bool includeNormals = false,
00137                                                         bool lighting = false,
00138                                                         bool useMeshColor = true );
00139 
00140 
00141     /**
00142      * Extract the vertices and triangles from a WTriangleMesh and save them
00143      * into an osg::Geometry in order to produce a flat shaded rendering.
00144      *
00145      * \param mesh the WTriangleMesh used as input
00146      * \param includeNormals When true, calculate the triangle normals and include
00147      *                       them into the geometry.
00148      * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
00149      * \param lighting if true, a standard lighting is activated for this geometry
00150      * \param useMeshColor if true, the mesh color is used. If false, the defaultColor is used.
00151      * \return an osg::Geometry containing the mesh
00152      * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
00153      */
00154     osg::ref_ptr< osg::Geometry > convertToOsgGeometryFlatShaded( WTriangleMesh::SPtr mesh,
00155                                                                         const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
00156                                                                         bool includeNormals = false,
00157                                                                         bool lighting = false,
00158                                                                         bool useMeshColor = true );
00159     /**
00160      * Extract the vertices and triangles from a WTriangleMesh and save them
00161      * into an osg::Geometry. It can use the normals and per-vertex colors of the mesh.
00162      * This method additionally uses the specified vertexID-color map to provide additional coloring.
00163      *
00164      * \param mesh the WTriangleMesh used as input
00165      * \param colorMap the map from vertex to color.
00166      * \param includeNormals When true, calculate the vertex normals and include
00167      *                       them into the geometry.
00168      * \param defaultColor This color is used in case the colorMap does not provide a color for a vertex
00169      * \param lighting if true, a standard lighting is activated for this geometry*
00170      * \return an osg::Geometry containing the mesh
00171      * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
00172      */
00173     osg::ref_ptr< osg::Geometry > convertToOsgGeometry( WTriangleMesh::SPtr mesh, const WColoredVertices& colorMap,
00174                                                                    const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
00175                                                                    bool includeNormals = false,
00176                                                                    bool lighting = false
00177                                                                    );
00178 
00179 
00180     /**
00181      * Convert triangle mesh to lines representing it. Draws lines twice (ATM).
00182      *
00183      * \param mesh The WTriangleMesh used as input.
00184      * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
00185      * \param useMeshColor If true, the mesh color is used. If false, the defaultColor is used.
00186      *
00187      * \return an osg::Geometry containing the mesh as lines
00188      */
00189     osg::ref_ptr< osg::Geometry > convertToOsgGeometryLines( WTriangleMesh::SPtr mesh,
00190                                                              const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
00191                                                              bool useMeshColor = true );
00192 
00193     /**
00194      * helper function to add a label somewhere
00195      *
00196      * \param position position of the label
00197      * \param text text
00198      * \return a positionattitudetransfom object containing the label
00199      */
00200     osg::ref_ptr< osg::PositionAttitudeTransform > addLabel( osg::Vec3 position, std::string text );
00201 
00202     /**
00203      * helper function to add a label with it's position vector
00204      *
00205      * \param position position of the label
00206      * \return a positionattitudetransfom object containing the label
00207      */
00208     osg::ref_ptr< osg::PositionAttitudeTransform > vector2label( osg::Vec3 position );
00209 
00210     /**
00211      * Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane.
00212      *
00213      * \param xSize how far the plane from its center along the x-axis should be drawn (both directions)
00214      * \param ySize how far the plane from its center along the y-axis should be drawn (both directions)
00215      * \param p The plane instance
00216      * \param color The color of the plane
00217      * \param border If true than a border around each plane is drawn in inverse color of the plane
00218      *
00219      * \return The new assembled geode for this plane
00220      */
00221     osg::ref_ptr< osg::Geode > genFinitePlane( double xSize,
00222                                                           double ySize,
00223                                                           const WPlane& p,
00224                                                           const WColor& color = WColor( 0.0, 0.7, 0.7, 1.0 ),
00225                                                           bool border = false );
00226 
00227     /**
00228      * Create a coordinate system. The coordinate system is build from cylinders and cones and includes a positive-to-negative
00229      * color gradient.
00230      *
00231      * \param middle osg::Vec3( middleX, middleY, middleZ ) middle points of X, Y, Z
00232      * \param sizeX whole lenght of X
00233      * \param sizeY whole lenght of Y
00234      * \param sizeZ whole lenght of Z
00235      *
00236      * \return Group Node
00237      */
00238     osg::ref_ptr< osg::Group > creatCoordinateSystem(
00239         osg::Vec3 middle,
00240         double sizeX,
00241         double sizeY,
00242         double sizeZ
00243     );
00244 
00245     /**
00246      * Generates a geode out of two vectors and an origin position.
00247      *
00248      * \param base the origin position. NOT the center.
00249      * \param a the first vector spanning the plane
00250      * \param b the second vector spanning the plane
00251      * \param color the color to use.
00252      *
00253      * \return the geode
00254      */
00255     osg::ref_ptr< osg::Geode > genFinitePlane( osg::Vec3 const& base, osg::Vec3 const& a, osg::Vec3 const& b,
00256                                                const WColor& color = defaultColor::WHITE );
00257 } // end of namespace wge
00258 
00259 #endif  // WGEGEODEUTILS_H