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