meshobstacle.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef __MESHOBSTACLE_H
00025 #define __MESHOBSTACLE_H
00026
00027 #include <stdio.h>
00028 #include <cmath>
00029 #include <osg/BoundingSphere>
00030
00031 #include "primitive.h"
00032 #include "abstractobstacle.h"
00033 #include "boundingshape.h"
00034
00035 namespace lpzrobots {
00036
00037 class MeshObstacle : public AbstractObstacle {
00038 protected:
00039
00040
00041 std::string filename;
00042 float scale;
00043 OSGMesh* mesh;
00044 Sphere* bound;
00045 BoundingShape* boundshape;
00046
00047 public:
00048
00049 MeshObstacle(const OdeHandle& odeHandle, const OsgHandle& osgHandle ,
00050 std::string filename, double scale = 1):
00051 AbstractObstacle::AbstractObstacle(odeHandle, osgHandle),
00052 filename(filename), scale(scale)
00053 {
00054 mesh = 0;
00055 bound = 0;
00056 boundshape = 0;
00057 obstacle_exists=false;
00058 };
00059
00060
00061
00062
00063 virtual void update(){
00064
00065 };
00066
00067
00068
00069
00070 virtual void setPose(const osg::Matrix& pose){
00071 this->pose = pose;
00072 if (obstacle_exists){
00073 destroy();
00074 }
00075 create();
00076 };
00077
00078
00079
00080 protected:
00081 virtual void create(){
00082
00083 mesh = new OSGMesh(filename, scale);
00084 mesh->init(osgHandle);
00085 mesh->setMatrix(pose);
00086 const osg::BoundingSphere& bsphere = mesh->getGroup()->getBound();
00087
00088 boundshape = new BoundingShape(filename + ".bbox" );
00089 if(!boundshape->init(odeHandle, osgHandle.changeColor(Color(0,1,0,0.2)),
00090 pose, scale, Primitive::Geom | Primitive::Draw)){
00091 printf("use default bounding box, because bbox file not found\n");
00092 bound = new Sphere(bsphere.radius());
00093 bound->init(odeHandle, 0, osgHandle.changeColor(Color(1,0,0,0.2)), Primitive::Geom | Primitive::Draw);
00094 bound->setPose(osg::Matrix::translate(bsphere.center())*
00095 osg::Matrix::translate(0.0f,0.0f,bsphere.radius()));
00096 mesh->setMatrix(osg::Matrix::translate(0.0f,0.0f,bsphere.radius())*pose);
00097 }
00098 obstacle_exists=true;
00099 };
00100
00101
00102 virtual void destroy(){
00103 if(mesh) delete(mesh);
00104 if(bound) delete(bound);
00105 if(boundshape) delete(boundshape);
00106 mesh=0;
00107 bound=0;
00108 boundshape=0;
00109 obstacle_exists=false;
00110 };
00111
00112 };
00113
00114 }
00115
00116 #endif