OpenWalnut  1.3.1
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 
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 > 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 > generateSolidBoundingBoxNode( const WBoundingBox& bb, const WColor& color, bool threeDTexCoords = true );
73 
74  /**
75  * Creates a deprecated_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< deprecated_osg::Geometry > createUnitCube( const WColor& color );
82 
83  /**
84  * Creates a deprecated_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< deprecated_osg::Geometry > createUnitCubeAsLines( const WColor& color );
91 
92  /**
93  * Extract the vertices and triangles from a WTriangleMesh and save them
94  * into an deprecated_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 deprecated_osg::Geometry containing the mesh
103  * \note mesh cannot be const since deprecated_osg::Geometry needs non-const pointers to the contained arrays
104  */
105  osg::ref_ptr< deprecated_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 deprecated_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 deprecated_osg::Geometry containing the mesh
123  * \note mesh cannot be const since deprecated_osg::Geometry needs non-const pointers to the contained arrays
124  */
125  osg::ref_ptr< deprecated_osg::Geometry > 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  * Convert triangle mesh to lines representing it. Draws lines twice (ATM).
133  *
134  * \param mesh The WTriangleMesh used as input.
135  * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
136  * \param useMeshColor If true, the mesh color is used. If false, the defaultColor is used.
137  *
138  * \return an deprecated_osg::Geometry containing the mesh as lines
139  */
140  osg::ref_ptr< deprecated_osg::Geometry > convertToOsgGeometryLines( WTriangleMesh::SPtr mesh,
141  const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
142  bool useMeshColor = true );
143 
144 
145  /**
146  * Generates a line geode with thickness and color as parameters.
147  *
148  * \param line sequence of points
149  * \param thickness How thick the line strip should be
150  * \param color If present this color is used for the whole line, otherwise local coloring is used
151  *
152  * \return The new assembled geode for this line
153  */
154  osg::ref_ptr< osg::Geode > generateLineStripGeode( const WLine& line,
155  const float thickness = 3.0f,
156  const WColor& color = WColor( 0, 0, 0, 0 ) );
157 
158  /**
159  * helper function to add a label somewhere
160  *
161  * \param position position of the label
162  * \param text text
163  * \return a positionattitudetransfom object containing the label
164  */
165  osg::ref_ptr< osg::PositionAttitudeTransform > addLabel( osg::Vec3 position, std::string text );
166 
167  /**
168  * helper function to add a label with it's position vector
169  *
170  * \param position position of the label
171  * \return a positionattitudetransfom object containing the label
172  */
173  osg::ref_ptr< osg::PositionAttitudeTransform > vector2label( osg::Vec3 position );
174 
175  /**
176  * Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane.
177  *
178  * \param xSize how far the plane from its center along the x-axis should be drawn (both directions)
179  * \param ySize how far the plane from its center along the y-axis should be drawn (both directions)
180  * \param p The plane instance
181  * \param color The color of the plane
182  * \param border If true than a border around each plane is drawn in inverse color of the plane
183  *
184  * \return The new assembled geode for this plane
185  */
186  osg::ref_ptr< osg::Geode > genFinitePlane( double xSize,
187  double ySize,
188  const WPlane& p,
189  const WColor& color = WColor( 0.0, 0.7, 0.7, 1.0 ),
190  bool border = false );
191 
192  /**
193  * Create a coordinate system. The coordinate system is build from cylinders and cones and includes a positive-to-negative
194  * color gradient.
195  *
196  * \param middle osg::Vec3( middleX, middleY, middleZ ) middle points of X, Y, Z
197  * \param sizeX whole lenght of X
198  * \param sizeY whole lenght of Y
199  * \param sizeZ whole lenght of Z
200  *
201  * \return Group Node
202  */
203  osg::ref_ptr< osg::Group > creatCoordinateSystem(
204  osg::Vec3 middle,
205  double sizeX,
206  double sizeY,
207  double sizeZ
208  );
209 
210  /**
211  * Generates a geode out of two vectors and an origin position.
212  *
213  * \param base the origin position. NOT the center.
214  * \param a the first vector spanning the plane
215  * \param b the second vector spanning the plane
216  *
217  * \return the geode
218  */
219  osg::ref_ptr< osg::Geode > genFinitePlane( osg::Vec3 const& base, osg::Vec3 const& a, osg::Vec3 const& b );
220 
221  /**
222  * Generates a plane subdivided into quads.
223  *
224  * \param resX How many quads in x-direction
225  * \param resY How many quads in y-direction
226  * \param spacing Not implement yet
227  *
228  * \return The new uncolored plane geode
229  */
230  osg::ref_ptr< WGESubdividedPlane > genUnitSubdividedPlane( size_t resX, size_t resY, double spacing = 0.01 );
231 
232  /**
233  * For each points in the STL container generate small cubes.
234  *
235  * \param points Center point of the cubes
236  * \param size The size of the cubes
237  * \param color The color of the cubes
238  * \tparam An STL container with WPositions as elements ( don't try it with different than vector, set, list or queue )
239  *
240  * \return Geode with as many cubes as points in the container where each cube is around a certain position.
241  */
242  template< class Container > osg::ref_ptr< osg::Geode > genPointBlobs( boost::shared_ptr< Container > points,
243  double size,
244  const WColor& color = WColor( 1.0, 0.0, 0.0, 1.0 ) );
245 } // end of namespace wge
246 
247 template< class Container > inline osg::ref_ptr< osg::Geode > wge::genPointBlobs( boost::shared_ptr< Container > points,
248  double size,
249  const WColor& color )
250 {
251  osg::ref_ptr< osg::Vec3Array > vertices = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
252  osg::ref_ptr< osg::Vec4Array > colors = osg::ref_ptr< osg::Vec4Array >( new osg::Vec4Array );
253  osg::ref_ptr< deprecated_osg::Geometry > geometry = osg::ref_ptr< deprecated_osg::Geometry >( new deprecated_osg::Geometry );
254  osg::ref_ptr< osg::Vec3Array > normals = osg::ref_ptr< osg::Vec3Array >( new osg::Vec3Array );
255 
256  for( typename Container::const_iterator point = points->begin(); point != points->end(); ++point )
257  {
258  const WPosition& pos = *point;
259  std::vector< WPosition > corners;
260  corners.reserve( 8 );
261  double halfSize = size / 2.0;
262  corners.push_back( WPosition( pos[0] - halfSize, pos[1] - halfSize, pos[2] - halfSize ) );
263  corners.push_back( WPosition( pos[0] + halfSize, pos[1] - halfSize, pos[2] - halfSize ) );
264  corners.push_back( WPosition( pos[0] + halfSize, pos[1] - halfSize, pos[2] + halfSize ) );
265  corners.push_back( WPosition( pos[0] - halfSize, pos[1] - halfSize, pos[2] + halfSize ) );
266  corners.push_back( WPosition( pos[0] - halfSize, pos[1] + halfSize, pos[2] - halfSize ) );
267  corners.push_back( WPosition( pos[0] + halfSize, pos[1] + halfSize, pos[2] - halfSize ) );
268  corners.push_back( WPosition( pos[0] + halfSize, pos[1] + halfSize, pos[2] + halfSize ) );
269  corners.push_back( WPosition( pos[0] - halfSize, pos[1] + halfSize, pos[2] + halfSize ) );
270 
271  osg::ref_ptr< osg::Vec3Array > ver = generateCuboidQuads( corners );
272  vertices->insert( vertices->end(), ver->begin(), ver->end() );
273  osg::ref_ptr< osg::Vec3Array > nor = generateCuboidQuadNormals( corners );
274  normals->insert( normals->end(), nor->begin(), nor->end() );
275  geometry->addPrimitiveSet( new osg::DrawArrays( osg::PrimitiveSet::QUADS, vertices->size() - ver->size(), ver->size() ) );
276  }
277 
278  geometry->setVertexArray( vertices );
279  colors->push_back( color );
280  geometry->setColorArray( colors );
281  geometry->setColorBinding( deprecated_osg::Geometry::BIND_OVERALL );
282  geometry->setNormalArray( normals );
283  geometry->setNormalBinding( deprecated_osg::Geometry::BIND_PER_PRIMITIVE );
284  osg::ref_ptr< osg::Geode > geode = osg::ref_ptr< osg::Geode >( new osg::Geode );
285  geode->addDrawable( geometry );
286  return geode;
287 }
288 #endif // WGEGEODEUTILS_H