Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
osgprimitive.h
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * Copyright (C) 2005-2011 LpzRobots development team *
4  * Georg Martius <georg dot martius at web dot de> *
5  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
6  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
7  * Ralf Der <ralfder at mis dot mpg dot de> *
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  * This program is distributed in the hope that it will be useful, *
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17  * GNU General Public License for more details. *
18  * *
19  * You should have received a copy of the GNU General Public License *
20  * along with this program; if not, write to the *
21  * Free Software Foundation, Inc., *
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
23  * *
24  ***************************************************************************/
25 #ifndef __OSGPRIMITIVE_H
26 #define __OSGPRIMITIVE_H
27 
28 #include <string>
29 #include <vector>
30 #include <osg/ref_ptr>
31 #include "osgforwarddecl.h"
32 #include "osghandle.h"
33 #include <osgDB/ReadFile>
34 #include <osgText/Text>
35 
36 namespace lpzrobots {
37 
38  /**
39  holds texture file and repeat information.
40 
41  */
42  class TextureDescr {
43  public:
45  /**
46  If repeatOnX is negativ then it is used as a unit length for the texture.
47  */
48  TextureDescr(const std::string& filename, double repeatOnR, double repeatOnS)
49  : filename(filename), repeatOnR(repeatOnR), repeatOnS(repeatOnS)
50  {
51  }
52  std::string filename;
53  double repeatOnR;
54  double repeatOnS;
55  };
56 
57  /**
58  Interface class for graphic primitives like spheres, boxes, and meshes,
59  which can be drawn by OSG. The idea is to hide all the details of the OSG
60  implementation.
61  */
62  class OSGPrimitive {
63  public:
64  /* typedef */ enum Quality {Low, Middle, High};
65 
66  OSGPrimitive ();
67  virtual ~OSGPrimitive ();
68  /** Initialisation of the primitive. Must in order to place the object into the scene.
69  This function should be overloaded */
70  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle) = 0;
71  /// Sets the transformation matrix of this object (position and orientation)
72  virtual void setMatrix( const osg::Matrix& m4x4 );
73  /// returns the group object which is the root of all subcomponents of this primitive
74  virtual osg::Group* getGroup();
75  /// assigns a texture to the primitive
76  virtual void setTexture(const std::string& filename);
77  /// assigns a texture to the primitive, you can choose how often to repeat
78  virtual void setTexture(const TextureDescr& texture);
79  /// assigns a texture to the x-th surface of the primitive, you can choose how often to repeat
80  virtual void setTexture(int surface, const TextureDescr& texture);
81  /// assign a set of texture to the surfaces of the primitive
82  virtual void setTextures(const std::vector<TextureDescr>& textures);
83  /// returns the list of textures
84  virtual std::vector<TextureDescr> getTextures() const;
85  /// sets the color for painting this primitive
86  virtual void setColor(const Color& color);
87  /// sets the color using the colorschema of osgHandle
88  virtual void setColor(const std::string& color);
89  /// returns the current color
90  virtual Color getColor();
91 
92  /// returns a osg transformation object;
93  virtual osg::Transform* getTransform();
94  /// returns the osgHandle object
95  virtual const OsgHandle& getOsgHandle();
96 
97  protected:
98  /// this actually sets the textures
99  virtual void applyTextures();
100 
101  osg::ref_ptr<osg::Geode> geode;
102  osg::ref_ptr<osg::MatrixTransform> transform;
103  osg::ref_ptr<osg::ShapeDrawable> shape;
104 
105  std::vector<TextureDescr > textures;
106 
108  };
109 
110  /**
111  A dummy graphical object, which has no representation in the graphical world.
112  */
113  class OSGDummy : public OSGPrimitive {
114  public:
115  OSGDummy();
116 
117  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
118  virtual void setMatrix( const osg::Matrix& m4x4 );
119  virtual osg::Group* getGroup();
120  virtual void setTexture(const std::string& filename);
121  virtual void setColor(const Color& color);
122  /// returns a osg transformation object;
123  virtual osg::Transform* getTransform();
124  };
125 
126 
127  /**
128  Graphical plane (represented as a large thin box, because OSG does not draw planes)
129  */
130  class OSGPlane : public OSGPrimitive {
131  public:
132  OSGPlane();
133 
134  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
135  };
136 
137 
138  /**
139  Graphical box
140  */
141  class OSGBox : public OSGPrimitive {
142  public:
143  OSGBox(float lengthX, float lengthY, float lengthZ);
145 
146  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
147 
148  virtual osg::Vec3 getDim();
149  virtual void setDim(osg::Vec3);
150 
151  protected:
153  osg::Box* box;
154  };
155 
156  /**
157  Graphical box with Textures
158  */
159  class OSGBoxTex : public OSGPrimitive {
160  public:
161  OSGBoxTex(float lengthX, float lengthY, float lengthZ);
163 
164  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
165 
166  virtual osg::Vec3 getDim() const { return dim;}
167  virtual void setDim(const osg::Vec3& _dim) { dim = _dim;}
168 
169  virtual void setColor(const Color& color);
170 
171  protected:
172  /// this actually sets the textures, overwritten
173  virtual void applyTextures();
174 
176  // we use one geode for each face of the box for the texture handling
177  osg::ref_ptr<osg::Geode> faces[6];
178  };
179 
180 
181  /**
182  Graphical sphere
183  */
184  class OSGSphere : public OSGPrimitive {
185  public:
186  OSGSphere(float radius);
187 
188  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
189 
190  float getRadius() { return radius; }
191  protected:
192  float radius;
193  };
194 
195  /**
196  Graphical capsule (a cylinder with round ends)
197  */
198  class OSGCapsule : public OSGPrimitive {
199  public:
200  OSGCapsule(float radius, float height);
201 
202  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
203 
204  float getRadius() { return radius; }
205  float getHeight() { return height; }
206  protected:
207  float radius;
208  float height;
209  };
210 
211 
212  /**
213  Graphical cylinder
214  */
215  class OSGCylinder : public OSGPrimitive {
216  public:
217  OSGCylinder(float radius, float height);
218 
219  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
220 
221  float getRadius() { return radius; }
222  float getHeight() { return height; }
223  protected:
224  float radius;
225  float height;
226  };
227 
228  class OSGLine : public OSGPrimitive {
229  public:
230  // the list of points is considered pairwise, start-end points of each line segment
231  OSGLine(const std::list<osg::Vec3>& points);
232 
233  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
234 
235  virtual void applyTextures(){}
236 
237  virtual void setColor(const Color& color);
238 
239  // use the new points
240  virtual void setPoints(const std::list<osg::Vec3>& points);
241 
242  protected:
243  std::list<osg::Vec3> points;
244  osg::Geometry *geometry;
245 
246  virtual void updatePoints();
247 
248  };
249 
250  /**
251  Graphical Mesh or arbitrary OSG model.
252  */
253  class OSGMesh : public OSGPrimitive {
254  public:
255  /**
256  Constuctor
257  @param filename filename of the model file (search path is osg data path)
258  @param scale scale factor used for scaling the model
259  @param options for model reader
260  */
261  OSGMesh(const std::string& filename, float scale = 1, const osgDB::ReaderWriter::Options* options = 0);
262  ~OSGMesh();
263  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
264  /**
265  * Same as init, but the mesh file is not loaded and therefore not displayed.
266  * This method ensures that the transform is correctly initialised.
267  * @param osgHandle
268  */
269  virtual void virtualInit(const OsgHandle& osgHandle);
270  virtual float getRadius();
271  float getScale() { return scale; }
272 
273  protected:
274  std::string filename;
275  float scale;
276  const osgDB::ReaderWriter::Options* options;
277  osg::ref_ptr<osg::Node> mesh;
278  osg::ref_ptr<osg::MatrixTransform> scaletrans;
279 
280  virtual void internInit(const OsgHandle& osgHandle, bool loadAndDisplayMesh, Quality quality = Middle);
281 
282  };
283 
284  /**
285  Text to be displayed on the hud
286  */
287  class OSGText : public OSGPrimitive {
288  public:
289  OSGText(const std::string& text, int fontsize = 12,
290  osgText::Text::AlignmentType align = osgText::Text::LEFT_BASE_LINE);
291 
292  virtual ~OSGText();
293 
294  virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
295  virtual void setMatrix( const osg::Matrix& m4x4 );
296  virtual osg::Group* getGroup();
297  virtual void setColor(const Color& color);
298  /// returns a osg transformation object;
299  virtual osg::Transform* getTransform();
300  private:
301  osgText::Text* osgText;
302  };
303 
304 
305 }
306 
307 #endif
308 
virtual void virtualInit(const OsgHandle &osgHandle)
Same as init, but the mesh file is not loaded and therefore not displayed.
Definition: osgprimitive.cpp:575
osg::ref_ptr< osg::ShapeDrawable > shape
Definition: osgprimitive.h:103
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:288
virtual void setColor(const Color &color)
sets the color for painting this primitive
Definition: osgprimitive.cpp:158
virtual osg::Vec3 getDim()
Definition: osgprimitive.cpp:269
float getRadius()
Definition: osgprimitive.h:221
virtual osg::Transform * getTransform()
returns a osg transformation object;
Definition: osgprimitive.cpp:92
virtual void setColor(const Color &color)
sets the color for painting this primitive
Definition: osgprimitive.cpp:749
virtual osg::Transform * getTransform()
returns a osg transformation object;
Definition: osgprimitive.cpp:754
float radius
Definition: osgprimitive.h:192
virtual osg::Transform * getTransform()
returns a osg transformation object;
Definition: osgprimitive.cpp:201
osg::Geometry * geometry
Definition: osgprimitive.h:244
Graphical capsule (a cylinder with round ends)
Definition: osgprimitive.h:198
Graphical Mesh or arbitrary OSG model.
Definition: osgprimitive.h:253
Graphical box with Textures.
Definition: osgprimitive.h:159
Matrixd Matrix
Definition: osgforwarddecl.h:47
OSGPrimitive()
Definition: osgprimitive.cpp:67
virtual void setDim(osg::Vec3)
Definition: osgprimitive.cpp:272
Interface class for graphic primitives like spheres, boxes, and meshes, which can be drawn by OSG...
Definition: osgprimitive.h:62
float scale
Definition: osgprimitive.h:275
osg::ref_ptr< osg::MatrixTransform > scaletrans
Definition: osgprimitive.h:278
virtual void setTextures(const std::vector< TextureDescr > &textures)
assign a set of texture to the surfaces of the primitive
Definition: osgprimitive.cpp:122
OSGBox(float lengthX, float lengthY, float lengthZ)
Definition: osgprimitive.cpp:236
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:579
virtual void setColor(const Color &color)
sets the color for painting this primitive
Definition: osgprimitive.cpp:197
OSGCapsule(float radius, float height)
Definition: osgprimitive.cpp:396
osg::Vec3 dim
Definition: osgprimitive.h:152
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:182
float getScale()
Definition: osgprimitive.h:271
std::string filename
Definition: osgprimitive.h:52
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:243
virtual void applyTextures()
this actually sets the textures, overwritten
Definition: osgprimitive.cpp:360
osg::ref_ptr< osg::Geode > faces[6]
Definition: osgprimitive.h:177
OSGSphere(float radius)
Definition: osgprimitive.cpp:366
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)=0
Initialisation of the primitive.
std::string filename
Definition: osgprimitive.h:274
osg::Box * box
Definition: osgprimitive.h:153
Data structure for accessing the OpenSceneGraph.
Definition: osghandle.h:79
Definition: osgprimitive.h:228
virtual void setColor(const Color &color)
sets the color for painting this primitive
Definition: osgprimitive.cpp:503
OSGMesh(const std::string &filename, float scale=1, const osgDB::ReaderWriter::Options *options=0)
Constuctor.
Definition: osgprimitive.cpp:517
virtual Color getColor()
returns the current color
Definition: osgprimitive.cpp:173
TextureDescr(const std::string &filename, double repeatOnR, double repeatOnS)
If repeatOnX is negativ then it is used as a unit length for the texture.
Definition: osgprimitive.h:48
Graphical box.
Definition: osgprimitive.h:141
A dummy graphical object, which has no representation in the graphical world.
Definition: osgprimitive.h:113
float radius
Definition: osgprimitive.h:207
OSGLine(const std::list< osg::Vec3 > &points)
Definition: osgprimitive.cpp:456
osg::Vec3 dim
Definition: osgprimitive.h:175
virtual ~OSGText()
Definition: osgprimitive.cpp:725
Vec3f Vec3
Definition: osgforwarddecl.h:42
Text to be displayed on the hud.
Definition: osgprimitive.h:287
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:209
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:731
virtual osg::Vec3 getDim() const
Definition: osgprimitive.h:166
virtual const OsgHandle & getOsgHandle()
returns the osgHandle object
Definition: osgprimitive.cpp:96
holds texture file and repeat information.
Definition: osgprimitive.h:42
virtual void setMatrix(const osg::Matrix &m4x4)
Sets the transformation matrix of this object (position and orientation)
Definition: osgprimitive.cpp:186
virtual void setMatrix(const osg::Matrix &m4x4)
Sets the transformation matrix of this object (position and orientation)
Definition: osgprimitive.cpp:83
virtual void internInit(const OsgHandle &osgHandle, bool loadAndDisplayMesh, Quality quality=Middle)
Definition: osgprimitive.cpp:532
Graphical sphere.
Definition: osgprimitive.h:184
TextureDescr()
Definition: osgprimitive.h:44
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:460
virtual void applyTextures()
this actually sets the textures
Definition: osgprimitive.h:235
Definition: osgprimitive.h:64
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:400
std::list< osg::Vec3 > points
Definition: osgprimitive.h:243
virtual ~OSGPrimitive()
Definition: osgprimitive.cpp:71
float height
Definition: osgprimitive.h:225
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:430
OSGCylinder(float radius, float height)
Definition: osgprimitive.cpp:426
Definition: color.h:32
OsgHandle osgHandle
Definition: osgprimitive.h:107
osg::ref_ptr< osg::MatrixTransform > transform
Definition: osgprimitive.h:102
virtual void setMatrix(const osg::Matrix &m4x4)
Sets the transformation matrix of this object (position and orientation)
Definition: osgprimitive.cpp:738
float getRadius()
Definition: osgprimitive.h:204
virtual std::vector< TextureDescr > getTextures() const
returns the list of textures
Definition: osgprimitive.cpp:135
Definition: osgprimitive.h:64
virtual osg::Group * getGroup()
returns the group object which is the root of all subcomponents of this primitive ...
Definition: osgprimitive.cpp:744
virtual void setDim(const osg::Vec3 &_dim)
Definition: osgprimitive.h:167
virtual void setTexture(const std::string &filename)
assigns a texture to the primitive
Definition: osgprimitive.cpp:101
const osgDB::ReaderWriter::Options * options
Definition: osgprimitive.h:276
Quality
Definition: osgprimitive.h:64
virtual void applyTextures()
this actually sets the textures
Definition: osgprimitive.cpp:139
OSGText(const std::string &text, int fontsize=12, osgText::Text::AlignmentType align=osgText::Text::LEFT_BASE_LINE)
Definition: osgprimitive.cpp:715
OSGBoxTex(float lengthX, float lengthY, float lengthZ)
Definition: osgprimitive.cpp:281
osg::ref_ptr< osg::Geode > geode
Definition: osgprimitive.h:101
float getHeight()
Definition: osgprimitive.h:205
OSGPlane()
Definition: osgprimitive.cpp:206
double repeatOnR
Definition: osgprimitive.h:53
virtual void updatePoints()
Definition: osgprimitive.cpp:483
Graphical cylinder.
Definition: osgprimitive.h:215
~OSGMesh()
Definition: osgprimitive.cpp:523
osg::ref_ptr< osg::Node > mesh
Definition: osgprimitive.h:277
OSGDummy()
Definition: osgprimitive.cpp:180
Definition: osgprimitive.h:64
virtual float getRadius()
Definition: osgprimitive.cpp:526
virtual osg::Group * getGroup()
returns the group object which is the root of all subcomponents of this primitive ...
Definition: osgprimitive.cpp:189
Graphical plane (represented as a large thin box, because OSG does not draw planes) ...
Definition: osgprimitive.h:130
virtual osg::Group * getGroup()
returns the group object which is the root of all subcomponents of this primitive ...
Definition: osgprimitive.cpp:88
float getRadius()
Definition: osgprimitive.h:190
float height
Definition: osgprimitive.h:208
virtual void setPoints(const std::list< osg::Vec3 > &points)
Definition: osgprimitive.cpp:478
virtual void init(const OsgHandle &osgHandle, Quality quality=Middle)
Initialisation of the primitive.
Definition: osgprimitive.cpp:370
double repeatOnS
Definition: osgprimitive.h:54
float getHeight()
Definition: osgprimitive.h:222
virtual void setColor(const Color &color)
sets the color for painting this primitive
Definition: osgprimitive.cpp:353
float radius
Definition: osgprimitive.h:224
std::vector< TextureDescr > textures
Definition: osgprimitive.h:105
virtual void setTexture(const std::string &filename)
assigns a texture to the primitive
Definition: osgprimitive.cpp:193