meshobstacle.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 LpzRobots development team                    *
00003  *    Georg Martius  <georg dot martius at web dot de>                     *
00004  *    Frank Guettler <guettler at informatik dot uni-leipzig dot de        *
00005  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
00006  *    Ralf Der       <ralfder at mis dot mpg dot de>                       *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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    * updates the position of the geoms  ( not nessary for static objects)
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()));       // set sphere higher
00096       mesh->setMatrix(osg::Matrix::translate(0.0f,0.0f,bsphere.radius())*pose); // set obstacle higher
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
Generated on Thu Jun 28 14:45:36 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3