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 #ifndef __MESHOBSTACLE_H
00029 #define __MESHOBSTACLE_H
00030
00031 #include <stdio.h>
00032 #include <math.h>
00033 #include <osg/BoundingSphere>
00034
00035 #include "primitive.h"
00036 #include "abstractobstacle.h"
00037 #include "boundingshape.h"
00038
00039 namespace lpzrobots {
00040
00041 class MeshObstacle : public AbstractObstacle {
00042 protected:
00043
00044
00045 std::string filename;
00046 float scale;
00047 OSGMesh* mesh;
00048 Sphere* bound;
00049 BoundingShape* boundshape;
00050 bool obstacle_exists;
00051
00052 public:
00053
00054 MeshObstacle(const OdeHandle& odeHandle, const OsgHandle& osgHandle ,
00055 std::string filename, double scale = 1):
00056 AbstractObstacle::AbstractObstacle(odeHandle, osgHandle),
00057 filename(filename), scale(scale)
00058 {
00059 mesh = 0;
00060 bound = 0;
00061 boundshape = 0;
00062 obstacle_exists=false;
00063 };
00064
00065
00066
00067
00068 virtual void update(){
00069 };
00070
00071
00072 virtual void setPose(const osg::Matrix& pose){
00073 this->pose = pose;
00074 if (obstacle_exists){
00075 destroy();
00076 }
00077 create();
00078 };
00079
00080 virtual osg::Matrix getPose(){
00081 return pose;
00082 }
00083
00084 protected:
00085 virtual void create(){
00086
00087 mesh = new OSGMesh(filename, scale);
00088 mesh->init(osgHandle);
00089 mesh->setMatrix(pose);
00090 const osg::BoundingSphere& bsphere = mesh->getGroup()->getBound();
00091
00092 boundshape = new BoundingShape(filename + ".bbox" );
00093 if(!boundshape->init(odeHandle, osgHandle.changeColor(Color(0,1,0,0.2)),
00094 pose, scale, Primitive::Geom | Primitive::Draw)){
00095 printf("use default bounding box, because bbox file not found\n");
00096 bound = new Sphere(bsphere.radius());
00097 bound->init(odeHandle, 0, osgHandle.changeColor(Color(1,0,0,0.2)), Primitive::Geom | Primitive::Draw);
00098 bound->setPose(osg::Matrix::translate(bsphere.center()));
00099 }
00100
00101 obstacle_exists=true;
00102 };
00103
00104
00105 virtual void destroy(){
00106 if(mesh) delete(mesh);
00107 if(bound) delete(bound);
00108 if(boundshape) delete(boundshape);
00109 mesh=0;
00110 bound=0;
00111 boundshape=0;
00112 obstacle_exists=false;
00113 };
00114
00115 };
00116
00117 }
00118
00119 #endif