OpenWalnut  1.5.0dev
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/Drawable>
35 #include <osg/MatrixTransform>
36 #include <osg/PositionAttitudeTransform>
37 #include <osg/Vec3>
38 #include <osgText/Text>
39 
40 #include "../common/math/WLine.h"
41 #include "../common/math/WPlane.h"
42 #include "../common/math/linearAlgebra/WPosition.h"
43 #include "../common/WColor.h"
44 #include "../common/datastructures/WColoredVertices.h"
45 
46 #include "WGEGeometryUtils.h"
47 #include "WGESubdividedPlane.h"
48 #include "WGEUtils.h"
49 #include "WTriangleMesh.h"
50 
51 namespace wge
52 {
53  /**
54  * Generates an OSG geode for the bounding box.
55  *
56  * \param bb The axis aligned bounding box to generate a geode from.
57  * \param color The color in which the bounding box should be generated
58  *
59  * \return The OSG geode containing the 12 edges of the box.
60  */
61  osg::ref_ptr< osg::Geode > generateBoundingBoxGeode( const WBoundingBox& bb, const WColor& color );
62 
63  /**
64  * Generates an OSG node for the specified bounding box. It uses solid faces. This actually returns a MatrixTransform node and is especially
65  * useful for shader based raytracing.
66  *
67  * \param bb The axis aligned bounding box
68  * \param color The color in which the bounding box should be generated
69  * \param threeDTexCoords True if 3D texture coordinates should be created.
70  *
71  * \return The OSG node containing the 12 edges of the box.
72  */
73  osg::ref_ptr< osg::Node > generateSolidBoundingBoxNode( const WBoundingBox& bb, const WColor& color, bool threeDTexCoords = true );
74 
75  /**
76  * Creates a osg::Geometry containing an unit cube, having 3D texture coordinates.
77  *
78  * \param color the color to set for all vertices
79  *
80  * \return the geometry
81  */
82  osg::ref_ptr< osg::Geometry > createUnitCube( const WColor& color );
83 
84  /**
85  * 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
86  * function is very restrictive with respect to the given geode. It is not recommended to use it in combination with an geometry you already
87  * filled with something. It is designed to be used to fill many cubes into one geometry.
88  *
89  * \param position position of the lower,left,front vertex
90  * \param size size in x,y,z direction
91  * \param color a color
92  * \param geometry the geometry where to add the cube. It can be completely empty (no arrays). If you use an invalid (in the following
93  * terms) geometry here, the graphics will fail. Ensure that you have a color array, tex array, normal array and vertex array, where the
94  * 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
95  * array needs to be a vec4 array.
96  */
97  void createCube( osg::ref_ptr< osg::Geometry > geometry, const WPosition& position, const WPosition& size, const WColor& color );
98 
99  /**
100  * Create an arbitrary cube.
101  *
102  * \param position position of the lower,left,front vertex
103  * \param size size in x,y,z direction
104  * \param color a color
105  *
106  * \return the drawable.
107  */
108  osg::ref_ptr< osg::Geometry > createCube( const WPosition& position, const WPosition& size, const WColor& color );
109 
110  /**
111  * Creates a osg::Geometry containing an unit cube as line-strips, having 3D texture coordinates.
112  *
113  * \param color the color to set for all vertices
114  * \param asLines force the cube to be made only of lines, no line strips.
115  *
116  * \return the geometry
117  */
118  osg::ref_ptr< osg::Geometry > createUnitCubeAsLines( const WColor& color, bool asLines = false );
119 
120  /**
121  * Extract the vertices and triangles from a WTriangleMesh and save them
122  * into an osg::Geometry. It can use the normals and per-vertex colors of the mesh.
123  *
124  * \param mesh the WTriangleMesh used as input
125  * \param includeNormals When true, calculate the vertex normals and include
126  * them into the geometry.
127  * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
128  * \param lighting if true, a standard lighting is activated for this geometry
129  * \param useMeshColor if true, the mesh color is used. If false, the defaultColor is used.
130  * \return an osg::Geometry containing the mesh
131  * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
132  */
133  osg::ref_ptr< osg::Geometry > convertToOsgGeometry( WTriangleMesh::SPtr mesh,
134  const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
135  bool includeNormals = false,
136  bool lighting = false,
137  bool useMeshColor = true );
138 
139 
140  /**
141  * Extract the vertices and triangles from a WTriangleMesh and save them
142  * into an osg::Geometry in order to produce a flat shaded rendering.
143  *
144  * \param mesh the WTriangleMesh used as input
145  * \param includeNormals When true, calculate the triangle normals and include
146  * them into the geometry.
147  * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
148  * \param lighting if true, a standard lighting is activated for this geometry
149  * \param useMeshColor if true, the mesh color is used. If false, the defaultColor is used.
150  * \return an osg::Geometry containing the mesh
151  * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
152  */
153  osg::ref_ptr< osg::Geometry > convertToOsgGeometryFlatShaded( WTriangleMesh::SPtr mesh,
154  const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
155  bool includeNormals = false,
156  bool lighting = false,
157  bool useMeshColor = true );
158  /**
159  * Extract the vertices and triangles from a WTriangleMesh and save them
160  * into an osg::Geometry. It can use the normals and per-vertex colors of the mesh.
161  * This method additionally uses the specified vertexID-color map to provide additional coloring.
162  *
163  * \param mesh the WTriangleMesh used as input
164  * \param colorMap the map from vertex to color.
165  * \param includeNormals When true, calculate the vertex normals and include
166  * them into the geometry.
167  * \param defaultColor This color is used in case the colorMap does not provide a color for a vertex
168  * \param lighting if true, a standard lighting is activated for this geometry*
169  * \return an osg::Geometry containing the mesh
170  * \note mesh cannot be const since osg::Geometry needs non-const pointers to the contained arrays
171  */
172  osg::ref_ptr< osg::Geometry > convertToOsgGeometry( WTriangleMesh::SPtr mesh, const WColoredVertices& colorMap,
173  const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
174  bool includeNormals = false,
175  bool lighting = false
176  );
177 
178 
179  /**
180  * Convert triangle mesh to lines representing it. Draws lines twice (ATM).
181  *
182  * \param mesh The WTriangleMesh used as input.
183  * \param defaultColor This color is used in case the useMeshColor parameter is false or no colors are defined in the mesh.
184  * \param useMeshColor If true, the mesh color is used. If false, the defaultColor is used.
185  *
186  * \return an osg::Geometry containing the mesh as lines
187  */
188  osg::ref_ptr< osg::Geometry > convertToOsgGeometryLines( WTriangleMesh::SPtr mesh,
189  const WColor& defaultColor = WColor( 1.0, 1.0, 1.0, 1.0 ),
190  bool useMeshColor = true );
191 
192  /**
193  * helper function to add a label somewhere
194  *
195  * \param position position of the label
196  * \param text text
197  * \return a positionattitudetransfom object containing the label
198  */
199  osg::ref_ptr< osg::PositionAttitudeTransform > addLabel( osg::Vec3 position, std::string text );
200 
201  /**
202  * helper function to add a label with it's position vector
203  *
204  * \param position position of the label
205  * \return a positionattitudetransfom object containing the label
206  */
207  osg::ref_ptr< osg::PositionAttitudeTransform > vector2label( osg::Vec3 position );
208 
209  /**
210  * Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane.
211  *
212  * \param xSize how far the plane from its center along the x-axis should be drawn (both directions)
213  * \param ySize how far the plane from its center along the y-axis should be drawn (both directions)
214  * \param p The plane instance
215  * \param color The color of the plane
216  * \param border If true than a border around each plane is drawn in inverse color of the plane
217  *
218  * \return The new assembled geode for this plane
219  */
220  osg::ref_ptr< osg::Geode > genFinitePlane( double xSize,
221  double ySize,
222  const WPlane& p,
223  const WColor& color = WColor( 0.0, 0.7, 0.7, 1.0 ),
224  bool border = false );
225 
226  /**
227  * Create a coordinate system. The coordinate system is build from cylinders and cones and includes a positive-to-negative
228  * color gradient.
229  *
230  * \param middle osg::Vec3( middleX, middleY, middleZ ) middle points of X, Y, Z
231  * \param sizeX whole lenght of X
232  * \param sizeY whole lenght of Y
233  * \param sizeZ whole lenght of Z
234  *
235  * \return Group Node
236  */
237  osg::ref_ptr< osg::Group > creatCoordinateSystem(
238  osg::Vec3 middle,
239  double sizeX,
240  double sizeY,
241  double sizeZ
242  );
243 
244  /**
245  * Generates a geode out of two vectors and an origin position.
246  *
247  * \param base the origin position. NOT the center.
248  * \param a the first vector spanning the plane
249  * \param b the second vector spanning the plane
250  * \param color the color to use.
251  *
252  * \return the geode
253  */
254  osg::ref_ptr< osg::Geode > genFinitePlane( osg::Vec3 const& base, osg::Vec3 const& a, osg::Vec3 const& b,
255  const WColor& color = defaultColor::WHITE );
256 } // end of namespace wge
257 
258 #endif // WGEGEODEUTILS_H
Represents a std::map where for each vertex ID a color is stored.
Represents a plane with a normal vector and a position in space.
Definition: WPlane.h:39
This only is a 3d double vector.
std::shared_ptr< WTriangleMesh > SPtr
Shared pointer.
Definition: WTriangleMesh.h:55
Some default colors.
Definition: WColor.cpp:36
Extend the wge utils namespace with additional methods relating WDataTexture3D.
osg::ref_ptr< osg::Geometry > convertToOsgGeometryLines(WTriangleMesh::SPtr mesh, const WColor &defaultColor=WColor(1.0, 1.0, 1.0, 1.0), bool useMeshColor=true)
Convert triangle mesh to lines representing it.
osg::ref_ptr< osg::Geode > generateBoundingBoxGeode(const WBoundingBox &bb, const WColor &color)
Generates an OSG geode for the bounding box.
osg::ref_ptr< osg::PositionAttitudeTransform > vector2label(osg::Vec3 position)
helper function to add a label with it's position vector
osg::ref_ptr< osg::Geometry > convertToOsgGeometry(WTriangleMesh::SPtr mesh, const WColor &defaultColor=WColor(1.0, 1.0, 1.0, 1.0), bool includeNormals=false, bool lighting=false, bool useMeshColor=true)
Extract the vertices and triangles from a WTriangleMesh and save them into an osg::Geometry.
osg::ref_ptr< osg::Geometry > createUnitCube(const WColor &color)
Creates a osg::Geometry containing an unit cube, having 3D texture coordinates.
osg::ref_ptr< osg::PositionAttitudeTransform > addLabel(osg::Vec3 position, std::string text)
helper function to add a label somewhere
osg::ref_ptr< osg::Group > creatCoordinateSystem(osg::Vec3 middle, double sizeX, double sizeY, double sizeZ)
Create a coordinate system.
osg::ref_ptr< osg::Geometry > convertToOsgGeometryFlatShaded(WTriangleMesh::SPtr mesh, const WColor &defaultColor=WColor(1.0, 1.0, 1.0, 1.0), bool includeNormals=false, bool lighting=false, bool useMeshColor=true)
Extract the vertices and triangles from a WTriangleMesh and save them into an osg::Geometry in order ...
osg::ref_ptr< osg::Node > generateSolidBoundingBoxNode(const WBoundingBox &bb, const WColor &color, bool threeDTexCoords=true)
Generates an OSG node for the specified bounding box.
osg::ref_ptr< osg::Geode > genFinitePlane(double xSize, double ySize, const WPlane &p, const WColor &color=WColor(0.0, 0.7, 0.7, 1.0), bool border=false)
Generates a geode out of a Plane with a fixed size in direction of the vectors which span that plane.
void createCube(osg::ref_ptr< osg::Geometry > geometry, const WPosition &position, const WPosition &size, const WColor &color)
Create an arbitrary cube and insert it into the given geometry.
osg::ref_ptr< osg::Geometry > createUnitCubeAsLines(const WColor &color, bool asLines=false)
Creates a osg::Geometry containing an unit cube as line-strips, having 3D texture coordinates.