OpenWalnut 1.3.1
|
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 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 > 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 > 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 > 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 > 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 > 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 * Convert triangle mesh to lines representing it. Draws lines twice (ATM). 00133 * 00134 * \param mesh The WTriangleMesh used as input. 00135 * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh. 00136 * \param useMeshColor If true, the mesh color is used. If false, the defaultColor is used. 00137 * 00138 * \return an osg::Geometry containing the mesh as lines 00139 */ 00140 osg::ref_ptr< osg::Geometry > convertToOsgGeometryLines( WTriangleMesh::SPtr mesh, 00141 const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ), 00142 bool useMeshColor = true ); 00143 00144 00145 /** 00146 * Generates a line geode with thickness and color as parameters. 00147 * 00148 * \param line sequence of points 00149 * \param thickness How thick the line strip should be 00150 * \param color If present this color is used for the whole line, otherwise local coloring is used 00151 * 00152 * \return The new assembled geode for this line 00153 */ 00154 osg::ref_ptr< osg::Geode > generateLineStripGeode( const WLine& line, 00155 const float thickness = 3.0f, 00156 const WColor& color = WColor( 0, 0, 0, 0 ) ); 00157 00158 /** 00159 * helper function to add a label somewhere 00160 * 00161 * \param position position of the label 00162 * \param text text 00163 * \return a positionattitudetransfom object containing the label 00164 */ 00165 osg::ref_ptr< osg::PositionAttitudeTransform > addLabel( osg::Vec3 position, std::string text ); 00166 00167 /** 00168 * helper function to add a label with it's position vector 00169 * 00170 * \param position position of the label 00171 * \return a positionattitudetransfom object containing the label 00172 */ 00173 osg::ref_ptr< osg::PositionAttitudeTransform > vector2label( osg::Vec3 position ); 00174 00175 /** 00176 * Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane. 00177 * 00178 * \param xSize how far the plane from its center along the x-axis should be drawn (both directions) 00179 * \param ySize how far the plane from its center along the y-axis should be drawn (both directions) 00180 * \param p The plane instance 00181 * \param color The color of the plane 00182 * \param border If true than a border around each plane is drawn in inverse color of the plane 00183 * 00184 * \return The new assembled geode for this plane 00185 */ 00186 osg::ref_ptr< osg::Geode > genFinitePlane( double xSize, 00187 double ySize, 00188 const WPlane& p, 00189 const WColor& color = WColor( 0.0, 0.7, 0.7, 1.0 ), 00190 bool border = false ); 00191 00192 /** 00193 * Create a coordinate system. The coordinate system is build from cylinders and cones and includes a positive-to-negative 00194 * color gradient. 00195 * 00196 * \param middle osg::Vec3( middleX, middleY, middleZ ) middle points of X, Y, Z 00197 * \param sizeX whole lenght of X 00198 * \param sizeY whole lenght of Y 00199 * \param sizeZ whole lenght of Z 00200 * 00201 * \return Group Node 00202 */ 00203 osg::ref_ptr< osg::Group > creatCoordinateSystem( 00204 osg::Vec3 middle, 00205 double sizeX, 00206 double sizeY, 00207 double sizeZ 00208 ); 00209 00210 /** 00211 * Generates a geode out of two vectors and an origin position. 00212 * 00213 * \param base the origin position. NOT the center. 00214 * \param a the first vector spanning the plane 00215 * \param b the second vector spanning the plane 00216 * 00217 * \return the geode 00218 */ 00219 osg::ref_ptr< osg::Geode > genFinitePlane( osg::Vec3 const& base, osg::Vec3 const& a, osg::Vec3 const& b ); 00220 00221 /** 00222 * Generates a plane subdivided into quads. 00223 * 00224 * \param resX How many quads in x-direction 00225 * \param resY How many quads in y-direction 00226 * \param spacing Not implement yet 00227 * 00228 * \return The new uncolored plane geode 00229 */ 00230 osg::ref_ptr< WGESubdividedPlane > genUnitSubdividedPlane( size_t resX, size_t resY, double spacing = 0.01 ); 00231 00232 /** 00233 * For each points in the STL container generate small cubes. 00234 * 00235 * \param points Center point of the cubes 00236 * \param size The size of the cubes 00237 * \param color The color of the cubes 00238 * \tparam An STL container with WPositions as elements ( don't try it with different than vector, set, list or queue ) 00239 * 00240 * \return Geode with as many cubes as points in the container where each cube is around a certain position. 00241 */ 00242 template< class Container > osg::ref_ptr< osg::Geode > genPointBlobs( boost::shared_ptr< Container > points, 00243 double size, 00244 const WColor& color = WColor( 1.0, 0.0, 0.0, 1.0 ) ); 00245 } // end of namespace wge 00246 00247 template< class Container > inline osg::ref_ptr< osg::Geode > wge::genPointBlobs( boost::shared_ptr< Container > points, 00248 double size, 00249 const WColor& color ) 00250 { 00251 osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array ); 00252 osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array ); 00253 osg::ref_ptr< osg::Geometry > geometry = osg::ref_ptr< osg::Geometry >( new osg::Geometry ); 00254 osg::ref_ptr< osg::Vec3Array > normals = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array ); 00255 00256 for( typename Container::const_iterator point = points->begin(); point != points->end(); ++point ) 00257 { 00258 const WPosition& pos = *point; 00259 std::vector< WPosition > corners; 00260 corners.reserve( 8 ); 00261 double halfSize = size / 2.0; 00262 corners.push_back( WPosition( pos[0] - halfSize, pos[1] - halfSize, pos[2] - halfSize ) ); 00263 corners.push_back( WPosition( pos[0] + halfSize, pos[1] - halfSize, pos[2] - halfSize ) ); 00264 corners.push_back( WPosition( pos[0] + halfSize, pos[1] - halfSize, pos[2] + halfSize ) ); 00265 corners.push_back( WPosition( pos[0] - halfSize, pos[1] - halfSize, pos[2] + halfSize ) ); 00266 corners.push_back( WPosition( pos[0] - halfSize, pos[1] + halfSize, pos[2] - halfSize ) ); 00267 corners.push_back( WPosition( pos[0] + halfSize, pos[1] + halfSize, pos[2] - halfSize ) ); 00268 corners.push_back( WPosition( pos[0] + halfSize, pos[1] + halfSize, pos[2] + halfSize ) ); 00269 corners.push_back( WPosition( pos[0] - halfSize, pos[1] + halfSize, pos[2] + halfSize ) ); 00270 00271 osg::ref_ptr< osg::Vec3Array > ver = generateCuboidQuads( corners ); 00272 vertices->insert( vertices->end(), ver->begin(), ver->end() ); 00273 osg::ref_ptr< osg::Vec3Array > nor = generateCuboidQuadNormals( corners ); 00274 normals->insert( normals->end(), nor->begin(), nor->end() ); 00275 geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, vertices->size() - ver->size(), ver->size() ) ); 00276 } 00277 00278 geometry->setVertexArray( vertices ); 00279 colors->push_back( color ); 00280 geometry->setColorArray( colors ); 00281 geometry->setColorBinding( osg::Geometry::BIND_OVERALL ); 00282 geometry->setNormalArray( normals ); 00283 geometry->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE ); 00284 osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >( new osg::Geode ); 00285 geode->addDrawable( geometry ); 00286 return geode; 00287 } 00288 #endif // WGEGEODEUTILS_H