meshobstacle.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  *                                                                         *
00022  *   $Log: meshobstacle.h,v $
00023  *   Revision 1.1.2.1  2006/03/29 15:05:32  martius
00024  *   obstacle that uses a Mesh / OSGModel for graphics and a bounding shape for ODE
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    * updates the position of the geoms  ( not nessary for static objects)
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

Generated on Tue Apr 4 19:05:03 2006 for Robotsystem from Robot Group Leipzig by  doxygen 1.4.5