00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef __OCTAPLAYGROUND_H
00035 #define __OCTAPLAYGROUND_H
00036
00037 #include <math.h>
00038 #include <vector>
00039 #include <osg/Matrix>
00040
00041 #include "primitive.h"
00042 #include "abstractobstacle.h"
00043
00044 namespace lpzrobots {
00045
00046 class OctaPlayground : public AbstractObstacle {
00047
00048 double radius, width, height;
00049 Pos pos;
00050
00051 vector<Primitive*> obst;
00052
00053 bool obstacle_exists;
00054
00055 int number_elements;
00056 double angle;
00057 double box_length;
00058
00059 public:
00060
00061 OctaPlayground(const OdeHandle& odeHandle, const OsgHandle& osgHandle,
00062 const Pos& geometry = Pos(7,0.2,0.5), int numberCorners=8):
00063 AbstractObstacle::AbstractObstacle(odeHandle, osgHandle){
00064
00065 radius = geometry.x();
00066 width = geometry.y();
00067 height = geometry.z();
00068
00069 obstacle_exists=false;
00070
00071 number_elements=numberCorners;
00072 angle= 2*M_PI/number_elements;
00073 obst.resize(number_elements);
00074
00075 calcBoxLength();
00076 };
00077
00078 virtual ~OctaPlayground(){
00079 for (unsigned int i=0; i < obst.size(); i++){
00080 if(obst[i]) delete obst[i];
00081 }
00082 obst.clear();
00083 }
00084
00085 virtual void update(){
00086 for (unsigned int i=0; i<obst.size(); i++){
00087 if(obst[i]) obst[i]->update();
00088 }
00089 };
00090
00091
00092 virtual void setPose(const osg::Matrix& pose){
00093 this->pose = pose;
00094 if (obstacle_exists){
00095 destroy();
00096 }
00097 create();
00098 };
00099
00100 virtual osg::Matrix getPose(){
00101 return pose;
00102 }
00103
00104 protected:
00105
00106 virtual void create(){
00107
00108
00109 double r = sqrt(pow((1+cos(angle))/2, 2) + pow( sin(angle)/2 ,2)) * radius;
00110 for (int i=0; i<number_elements; i++){
00111 obst[i] = new Box(width , box_length , height);
00112 obst[i]->init(odeHandle, 0, osgHandle, Primitive::Geom | Primitive::Draw);
00113 osg::Matrix R = osg::Matrix::rotate(- i*angle, 0,0,1) *
00114 osg::Matrix::translate( cos(M_PI - i*angle) * r,
00115 sin(M_PI - i*angle) * r,
00116 height/2) * pose;
00117 obst[i]->setPose(R);
00118
00119 }
00120 };
00121
00122
00123 virtual void destroy(){
00124 for(int i=0; i < number_elements; i++){
00125 if(obst[i]) delete obst[i];
00126 }
00127 obstacle_exists=false;
00128 };
00129
00130 virtual void calcBoxLength(){
00131 double r = radius+width/2;
00132
00133 box_length = sqrt(pow( 1 - cos(angle), 2) + pow(sin(angle),2)) * r;
00134 }
00135
00136 };
00137
00138 }
00139
00140 #endif