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