OpenWalnut  1.2.5
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
WGEGeodeUtils.h
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #ifndef WGEGEODEUTILS_H
26 #define WGEGEODEUTILS_H
27 
28 #include <string>
29 #include <vector>
30 
31 #include <osg/Array>
32 #include <osg/Geode>
33 #include <osg/Geometry>
34 #include <osg/MatrixTransform>
35 #include <osg/PositionAttitudeTransform>
36 #include <osg/Vec3>
37 #include <osgText/Text>
38 
39 #include "../common/math/WLine.h"
40 #include "../common/math/WPlane.h"
41 #include "../common/math/linearAlgebra/WLinearAlgebra.h"
42 #include "../common/WColor.h"
43 #include "../common/datastructures/WColoredVertices.h"
44 #include "WExportWGE.h"
45 #include "WGEGeometryUtils.h"
46 #include "WGESubdividedPlane.h"
47 #include "WGEUtils.h"
48 #include "WTriangleMesh.h"
49 
50 namespace wge
51 {
52  /**
53  * Generates an OSG geode for the bounding box.
54  *
55  * \param bb The axis aligned bounding box to generate a geode from.
56  * \param color The color in which the bounding box should be generated
57  *
58  * \return The OSG geode containing the 12 edges of the box.
59  */
60  osg::ref_ptr< osg::Geode > WGE_EXPORT generateBoundingBoxGeode( const WBoundingBox& bb, const WColor& color );
61 
62  /**
63  * Generates an OSG node for the specified bounding box. It uses solid faces. This actually returns a MatrixTransform node and is especially
64  * useful for shader based raytracing.
65  *
66  * \param bb The axis aligned bounding box
67  * \param color The color in which the bounding box should be generated
68  * \param threeDTexCoords True if 3D texture coordinates should be created.
69  *
70  * \return The OSG node containing the 12 edges of the box.
71  */
72  osg::ref_ptr< osg::Node > WGE_EXPORT generateSolidBoundingBoxNode( const WBoundingBox& bb, const WColor& color, bool threeDTexCoords = true );
73 
74  /**
75  * Creates a osg::Geometry containing an unit cube, having 3D texture coordinates.
76  *
77  * \param color the color to set for all vertices
78  *
79  * \return the geometry
80  */
81  osg::ref_ptr< osg::Geometry > WGE_EXPORT createUnitCube( const WColor& color );
82 
83  /**
84  * Creates a osg::Geometry containing an unit cube as line-strips, having 3D texture coordinates.
85  *
86  * \param color the color to set for all vertices
87  *
88  * \return the geometry
89  */
90  osg::ref_ptr< osg::Geometry > WGE_EXPORT createUnitCubeAsLines( const WColor& color );
91 
92  /**
93  * Extract the vertices and triangles from a WTriangleMesh and save them
94  * into an osg::Geometry. It can use the normals and per-vertex colors of the mesh.
95  *
96  * \param mesh the WTriangleMesh used as input
97  * \param includeNormals When true, calculate the vertex normals and include
98  * them into the geometry.
99  * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
100  * \param lighting if true, a standard lighting is activated for this geometry
101  * \param useMeshColor if true, the mesh color is used. If false, the defaultColor is used.
102  * \return an osg::Geometry containing the mesh
103  * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
104  */
105  osg::ref_ptr< osg::Geometry > convertToOsgGeometry( WTriangleMesh::SPtr mesh,
106  const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
107  bool includeNormals = false,
108  bool lighting = false,
109  bool useMeshColor = true );
110 
111  /**
112  * Extract the vertices and triangles from a WTriangleMesh and save them
113  * into an osg::Geometry. It can use the normals and per-vertex colors of the mesh.
114  * This method additionally uses the specified vertexID-color map to provide additional coloring.
115  *
116  * \param mesh the WTriangleMesh used as input
117  * \param colorMap the map from vertex to color.
118  * \param includeNormals When true, calculate the vertex normals and include
119  * them into the geometry.
120  * \param defaultColor This color is used in case the colorMap does not provide a color for a vertex
121  * \param lighting if true, a standard lighting is activated for this geometry*
122  * \return an osg::Geometry containing the mesh
123  * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
124  */
125  osg::ref_ptr< osg::Geometry > WGE_EXPORT convertToOsgGeometry( WTriangleMesh::SPtr mesh, const WColoredVertices& colorMap,
126  const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
127  bool includeNormals = false,
128  bool lighting = false
129  );
130 
131  /**
132  * Generates a line geode with thickness and color as parameters.
133  *
134  * \param line sequence of points
135  * \param thickness How thick the line strip should be
136  * \param color If present this color is used for the whole line, otherwise local coloring is used
137  *
138  * \return The new assembled geode for this line
139  */
140  osg::ref_ptr< osg::Geode > WGE_EXPORT generateLineStripGeode( const WLine& line,
141  const float thickness = 3.0f,
142  const WColor& color = WColor( 0, 0, 0, 0 ) );
143 
144  /**
145  * helper function to add a label somewhere
146  *
147  * \param position position of the label
148  * \param text text
149  * \return a positionattitudetransfom object containing the label
150  */
151  osg::ref_ptr< osg::PositionAttitudeTransform > WGE_EXPORT addLabel( osg::Vec3 position, std::string text );
152 
153  /**
154  * helper function to add a label with it's position vector
155  *
156  * \param position position of the label
157  * \return a positionattitudetransfom object containing the label
158  */
159  osg::ref_ptr< osg::PositionAttitudeTransform > WGE_EXPORT vector2label( osg::Vec3 position );
160 
161  /**
162  * Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane.
163  *
164  * \param xSize how far the plane from its center along the x-axis should be drawn (both directions)
165  * \param ySize how far the plane from its center along the y-axis should be drawn (both directions)
166  * \param p The plane instance
167  * \param color The color of the plane
168  * \param border If true than a border around each plane is drawn in inverse color of the plane
169  *
170  * \return The new assembled geode for this plane
171  */
172  osg::ref_ptr< osg::Geode > WGE_EXPORT genFinitePlane( double xSize,
173  double ySize,
174  const WPlane& p,
175  const WColor& color = WColor( 0.0, 0.7, 0.7, 1.0 ),
176  bool border = false );
177 
178  /**
179  * Create a coordinate system. The coordinate system is build from cylinders and cones and includes a positive-to-negative
180  * color gradient.
181  *
182  * \param middle osg::Vec3( middleX, middleY, middleZ ) middle points of X, Y, Z
183  * \param sizeX whole lenght of X
184  * \param sizeY whole lenght of Y
185  * \param sizeZ whole lenght of Z
186  *
187  * \return Group Node
188  */
189  osg::ref_ptr< osg::Group > creatCoordinateSystem(
190  osg::Vec3 middle,
191  double sizeX,
192  double sizeY,
193  double sizeZ
194  );
195 
196  /**
197  * Generates a geode out of two vectors and an origin position.
198  *
199  * \param base the origin position. NOT the center.
200  * \param a the first vector spanning the plane
201  * \param b the second vector spanning the plane
202  *
203  * \return the geode
204  */
205  osg::ref_ptr< osg::Geode > WGE_EXPORT genFinitePlane( osg::Vec3 const& base, osg::Vec3 const& a, osg::Vec3 const& b );
206 
207  /**
208  * Generates a plane subdivided into quads.
209  *
210  * \param resX How many quads in x-direction
211  * \param resY How many quads in y-direction
212  * \param spacing Not implement yet
213  *
214  * \return The new uncolored plane geode
215  */
216  osg::ref_ptr< WGESubdividedPlane > WGE_EXPORT genUnitSubdividedPlane( size_t resX, size_t resY, double spacing = 0.01 );
217 
218  /**
219  * For each points in the STL container generate small cubes.
220  *
221  * \param points Center point of the cubes
222  * \param size The size of the cubes
223  * \param color The color of the cubes
224  * \tparam An STL container with WPositions as elements ( don't try it with different than vector, set, list or queue )
225  *
226  * \return Geode with as many cubes as points in the container where each cube is around a certain position.
227  */
228  template< class Container > osg::ref_ptr< osg::Geode > genPointBlobs( boost::shared_ptr< Container > points,
229  double size,
230  const WColor& color = WColor( 1.0, 0.0, 0.0, 1.0 ) );
231 } // end of namespace wge
232 
233 template< class Container > inline osg::ref_ptr< osg::Geode > wge::genPointBlobs( boost::shared_ptr< Container > points,
234  double size,
235  const WColor& color )
236 {
237  osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
238  osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
239  osg::ref_ptr< osg::Geometry > geometry = osg::ref_ptr< osg::Geometry >( new osg::Geometry );
240  osg::ref_ptr< osg::Vec3Array > normals = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
241 
242  for( typename Container::const_iterator point = points->begin(); point != points->end(); ++point )
243  {
244  const WPosition& pos = *point;
245  std::vector< WPosition > corners;
246  corners.reserve( 8 );
247  double halfSize = size / 2.0;
248  corners.push_back( WPosition( pos[0] - halfSize, pos[1] - halfSize, pos[2] - halfSize ) );
249  corners.push_back( WPosition( pos[0] + halfSize, pos[1] - halfSize, pos[2] - halfSize ) );
250  corners.push_back( WPosition( pos[0] + halfSize, pos[1] - halfSize, pos[2] + halfSize ) );
251  corners.push_back( WPosition( pos[0] - halfSize, pos[1] - halfSize, pos[2] + halfSize ) );
252  corners.push_back( WPosition( pos[0] - halfSize, pos[1] + halfSize, pos[2] - halfSize ) );
253  corners.push_back( WPosition( pos[0] + halfSize, pos[1] + halfSize, pos[2] - halfSize ) );
254  corners.push_back( WPosition( pos[0] + halfSize, pos[1] + halfSize, pos[2] + halfSize ) );
255  corners.push_back( WPosition( pos[0] - halfSize, pos[1] + halfSize, pos[2] + halfSize ) );
256 
257  osg::ref_ptr< osg::Vec3Array > ver = generateCuboidQuads( corners );
258  vertices->insert( vertices->end(), ver->begin(), ver->end() );
259  osg::ref_ptr< osg::Vec3Array > nor = generateCuboidQuadNormals( corners );
260  normals->insert( normals->end(), nor->begin(), nor->end() );
261  geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, vertices->size() - ver->size(), ver->size() ) );
262  }
263 
264  geometry->setVertexArray( vertices );
265  colors->push_back( color );
266  geometry->setColorArray( colors );
267  geometry->setColorBinding( osg::Geometry::BIND_OVERALL );
268  geometry->setNormalArray( normals );
269  geometry->setNormalBinding( osg::Geometry::BIND_PER_PRIMITIVE );
270  osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >( new osg::Geode );
271  geode->addDrawable( geometry );
272  return geode;
273 }
274 #endif // WGEGEODEUTILS_H