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
boxpile.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __BOXPILE_H
25 #define __BOXPILE_H
26 
27 #include <stdio.h>
28 #include <cmath>
29 
30 #include "primitive.h"
31 #include "joint.h"
32 #include "abstractobstacle.h"
33 
34 namespace lpzrobots {
35 
36 /**
37  * Boxpile
38  */
39 class Boxpile : public AbstractObstacle {
40  Pos dimension;
41  int num;
42  int seed;
43  Pos boxsizemean;
44  Pos boxsizevar;
45  RandGen randGen;
46 
47 public:
48  /**
49  * Constructor
50  * a random boxpile is fixed to the world
51  * @param dimension size of pile (height is always 1, use boxsize and boxvar)
52  * @param num number of boxes
53  * @param seed seed for random generator (it is fixed to generate deterministic piles)
54  * @param boxsizemean mean size of boxes
55  * @param boxsizevar variance of boxes sizes
56  */
58  const osg::Vec3& dimension = osg::Vec3(5.0, 5.0, 1.0),
59  int num = 30, int seed = 1,
60  const osg::Vec3& boxsizemean = osg::Vec3(1.0, 1.0, 0.2),
61  const osg::Vec3& boxsizevar = osg::Vec3(0.5, 0.5, 0.1) )
62  : AbstractObstacle::AbstractObstacle(odeHandle, osgHandle), dimension(dimension),
63  num(num), boxsizemean(boxsizemean), boxsizevar(boxsizevar)
64  {
65  setTexture("Images/wood_sw.jpg");
66  randGen.init(seed);
67  this->dimension.z()=1;
68  obstacle_exists=false;
69  };
70 
71 
72  virtual void setPose(const osg::Matrix& pose){
73  this->pose = pose;
74  if (!obstacle_exists) {
75  create();
76  }
77  };
78 
79  virtual Primitive* getMainPrimitive() const {
80  if(!obst.empty()) return obst[0];
81  else return 0;
82  }
83 
84 protected:
85  virtual void create(){
86  OdeHandle oh(odeHandle);
87  oh.createNewSimpleSpace(odeHandle.space,true);
88  double size=dimension.length();
89  for(int i=0; i< num; i++){
90  Box* b;
91  Pos rand(randGen.rand()-0.5,randGen.rand()-0.5,randGen.rand()-0.5);
92  Pos s = boxsizemean + ((rand*2) & boxsizevar); // & component wise mult
93  Pos pos = (dimension & Pos(randGen.rand()-0.5,randGen.rand()-0.5,0));
94  double angle = randGen.rand()*M_PI;
95 
96  // make sure box has positive dimensions
97  s.x()=fabs(s.x());
98  s.y()=fabs(s.y());
99  s.z()=fabs(s.z());
100  // make pile round
101  s.z()*=fabs((size-pos.length())/size); // linear ramping of heights
102 
103  pos.z() = s.z()/2.0;
104  b = new Box(s);
105  b->setTextures(getTextures(i));
106  b->init(oh, 0, osgHandle, Primitive::Geom | Primitive::Draw);
107 
108  b->setPose(ROTM(angle, 0,0,1)*TRANSM(pos) * pose);
109  obst.push_back(b);
110  }
111  obstacle_exists=true;
112  };
113 
114 };
115 
116 }
117 
118 #endif
119 
#define TRANSM
Definition: amos4legs.cpp:53
double rand()
returns a value in [0,1)
Definition: randomgenerator.h:42
Data structure for accessing the ODE.
Definition: odehandle.h:44
OsgHandle osgHandle
Definition: abstractobstacle.h:174
virtual std::vector< TextureDescr > getTextures(int primitive) const
returns the textures of the given primitive
Definition: abstractobstacle.cpp:147
virtual void setTexture(const std::string &texturefilename)
assigns a texture to the all primitives of this obstactle with repeat -1,-1
Definition: abstractobstacle.cpp:107
Abstract class (interface) for obstacles.
Definition: abstractobstacle.h:46
Matrixd Matrix
Definition: osgforwarddecl.h:47
bool obstacle_exists
Definition: abstractobstacle.h:171
OdeHandle odeHandle
Definition: abstractobstacle.h:173
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
Definition: pos.h:36
Data structure for accessing the OpenSceneGraph.
Definition: osghandle.h:79
Definition: primitive.h:89
Boxpile(const OdeHandle &odeHandle, const OsgHandle &osgHandle, const osg::Vec3 &dimension=osg::Vec3(5.0, 5.0, 1.0), int num=30, int seed=1, const osg::Vec3 &boxsizemean=osg::Vec3(1.0, 1.0, 0.2), const osg::Vec3 &boxsizevar=osg::Vec3(0.5, 0.5, 0.1))
Constructor a random boxpile is fixed to the world.
Definition: boxpile.h:57
void init(long int seedval)
Definition: randomgenerator.h:38
Vec3f Vec3
Definition: osgforwarddecl.h:42
Definition: primitive.h:89
void createNewSimpleSpace(dSpaceID parentspace, bool ignore_inside_collisions)
use this function to create a new space with optional ignored collisions, use deleteSpace to destroy ...
Definition: odehandle.cpp:85
Interface class for primitives represented in the physical and graphical world.
Definition: primitive.h:80
std::vector< Primitive * > obst
primitives which belong to this obstacle
Definition: abstractobstacle.h:166
Box primitive.
Definition: primitive.h:267
virtual void setTextures(const std::vector< TextureDescr > &textures)
assign a set of texture to the surfaces of the primitive
Definition: primitive.cpp:142
osg::Matrix pose
Definition: abstractobstacle.h:170
virtual void create()
overload this function to create the obstactle. All primitives should go into the list "obst" ...
Definition: boxpile.h:85
virtual void setPose(const osg::Matrix &pose)
sets position of the obstacle and creates/recreates obstacle if necessary
Definition: boxpile.h:72
virtual void setPose(const Pose &pose)
set the pose of the primitive
Definition: primitive.cpp:156
dSpaceID space
Definition: odehandle.h:51
Boxpile.
Definition: boxpile.h:39
#define ROTM
Definition: amos4legs.cpp:52
virtual Primitive * getMainPrimitive() const
return the "main" primitive of the obtactle. The meaning of "main" is arbitrary
Definition: boxpile.h:79
virtual void init(const OdeHandle &odeHandle, double mass, const OsgHandle &osgHandle, char mode=Body|Geom|Draw)
registers primitive in ODE and OSG.
Definition: primitive.cpp:417