primitive.h

Go to the documentation of this file.
00001  
00002 /***************************************************************************
00003  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00004  *    martius@informatik.uni-leipzig.de                                    *
00005  *    fhesse@informatik.uni-leipzig.de                                     *
00006  *    der@informatik.uni-leipzig.de                                        *
00007  *    frankguettler@gmx.de                                                 *
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  *   This program is distributed in the hope that it will be useful,       *
00015  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017  *   GNU General Public License for more details.                          *
00018  *                                                                         *
00019  *   You should have received a copy of the GNU General Public License     *
00020  *   along with this program; if not, write to the                         *
00021  *   Free Software Foundation, Inc.,                                       *
00022  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00023  *                                                                         *
00024  ***************************************************************************
00025  *                                                                         *
00026  *   This file provides basic primitives for ODE and openscenegraph        *
00027  *                                                                         *
00028  *                                                                         *
00029  *   $Log: primitive.h,v $
00030  *   Revision 1.2  2006/07/14 12:23:35  martius
00031  *   selforg becomes HEAD
00032  *
00033  *   Revision 1.1.2.18  2006/07/14 11:23:38  martius
00034  *   revert to older revision of robot3
00035  *
00036  *   Revision 1.1.2.16  2006/06/29 16:35:32  robot3
00037  *   -Mesh code optimized
00038  *   -includes cleared up, more using forward declarations
00039  *    (sometimes additionally #include "osgprimitive.h" is needed)
00040  *
00041  *   Revision 1.1.2.15  2006/06/27 14:14:29  robot3
00042  *   -optimized mesh and boundingshape code
00043  *   -other changes
00044  *
00045  *   Revision 1.1.2.14  2006/06/23 08:53:56  robot3
00046  *   made some changes on primitive Mesh
00047  *
00048  *   Revision 1.1.2.13  2006/05/29 22:03:26  martius
00049  *   cylinder
00050  *
00051  *   Revision 1.1.2.12  2006/05/29 21:27:02  robot3
00052  *   made some preparations for the boundingshape of the Mesh
00053  *
00054  *   Revision 1.1.2.11  2006/05/28 22:14:57  martius
00055  *   heightfield included
00056  *
00057  *   Revision 1.1.2.10  2006/05/24 12:23:10  robot3
00058  *   -passive_mesh works now (simple bound_version)
00059  *   -Primitive Mesh now exists (simple bound_version)
00060  *
00061  *   Revision 1.1.2.9  2006/04/04 14:13:24  fhesse
00062  *   documentation improved
00063  *
00064  *   Revision 1.1.2.8  2006/03/29 15:07:17  martius
00065  *   Dummy Primitive
00066  *
00067  *   Revision 1.1.2.7  2006/01/31 15:45:35  martius
00068  *   proper destruction
00069  *
00070  *   Revision 1.1.2.6  2006/01/12 14:21:00  martius
00071  *   drawmode, material
00072  *
00073  *   Revision 1.1.2.5  2005/12/29 12:58:42  martius
00074  *   *** empty log message ***
00075  *
00076  *   Revision 1.1.2.4  2005/12/15 17:03:43  martius
00077  *   cameramanupulator setPose is working
00078  *   joints have setter and getter parameters
00079  *   Primitives are not longer inherited from OSGPrimitive, moreover
00080  *   they aggregate them
00081  *
00082  *   Revision 1.1.2.3  2005/12/14 15:36:45  martius
00083  *   joints are visible now
00084  *
00085  *   Revision 1.1.2.2  2005/12/13 18:11:14  martius
00086  *   transform primitive added, some joints stuff done, forward declaration
00087  *
00088  *   Revision 1.1.2.1  2005/12/06 10:13:25  martius
00089  *   openscenegraph integration started
00090  *
00091  *                                                                 *
00092  *                                                                         *
00093  ***************************************************************************/
00094 #ifndef __PRIMITIVE_H
00095 #define __PRIMITIVE_H
00096 
00097 #include <osg/Matrix>
00098 #include <ode/common.h>
00099 
00100 // another forward declaration "block"
00101 #include "osgforwarddecl.h"
00102 
00103 namespace lpzrobots {
00104 
00105    /***** begin of forward declaration block *****/
00106    class BoundingShape;
00107    class OSGPrimitive;
00108    class OSGPlane;
00109    class OSGBox;
00110    class OSGSphere;
00111    class OSGCapsule;
00112    class OSGCylinder;
00113    class OSGDummy;
00114    class OSGMesh;
00115    typedef struct GlobalData;
00116    class OdeHandle;
00117    class OsgHandle;
00118    /*****  end of forward declaration block  *****/
00119 
00120 
00121 /// returns the osg (4x4) pose matrix of the ode geom
00122 osg::Matrix osgPose( dGeomID geom );
00123 /// returns the osg (4x4) pose matrix of the ode body
00124 osg::Matrix osgPose( dBodyID body );
00125 /// converts a position vector and a rotation matrix from ode to osg 4x4 matrix
00126 osg::Matrix osgPose( const double * position , const double * rotation );
00127 /// converts the rotation component of pose into an ode rotation matrix
00128 void odeRotation( const osg::Matrix& pose , dMatrix3& odematrix);
00129 
00130 /**
00131    Interface class for primitives represented in the physical and graphical world.
00132    This is intended to bring OSG and ODE together and hide most implementation details.
00133 */
00134 class Primitive {
00135 public:
00136   /** Body means that it is a dynamic object with a body.
00137       Geom means it has a geometrical represenation used for collision detection.
00138       Draw means the primitive is drawn
00139   */
00140   typedef enum Modes {Body=1, Geom=2, Draw=4};
00141 
00142   Primitive ();
00143   virtual ~Primitive ();
00144   /** registers primitive in ODE and OSG. 
00145       @param osgHandle scruct with ODE variables inside (to specify space, world...)
00146       @param mass Mass of the object in ODE (if withBody = true)
00147       @param osgHandle scruct with OSG variables inside (scene node, color ...)
00148       @param mode is a conjuction of Modes.
00149    */
00150   virtual void init(const OdeHandle& odeHandle, double mass,
00151                     const OsgHandle& osgHandle,
00152                     char mode = Body | Geom | Draw)  = 0 ;
00153 
00154   /** Updates the OSG nodes with ODE coordinates.
00155       This function must be overloaded (usually calls setMatrix of OsgPrimitives)
00156    */
00157   virtual void update() =0 ;
00158 
00159   /// returns the assoziated osg primitive if there or 0
00160   virtual OSGPrimitive* getOSGPrimitive() = 0;
00161 
00162   /// assigns a texture to the primitive
00163   virtual void setTexture(const std::string& filename);
00164   /// assigns a texture to the primitive, you can choose if the texture should be repeated
00165   virtual void setTexture(const std::string& filename, bool repeatOnX, bool repeatOnY);
00166 
00167   /// set the position of the primitive (orientation is preserved)
00168   void setPosition(const osg::Vec3& pos);
00169   /// set the pose of the primitive
00170   void setPose(const osg::Matrix& pose);
00171   /// returns the position
00172   osg::Vec3 getPosition() const;
00173   /// returns the pose
00174   osg::Matrix getPose() const;
00175 
00176   /// returns ODE geomID if there
00177   dGeomID getGeom() const;    
00178   /// returns ODE bodyID if there
00179   dBodyID getBody() const;
00180 
00181 protected:
00182   dGeomID geom;
00183   dBodyID body;
00184   char mode;
00185 };
00186 
00187 
00188 /** Plane primitive */
00189 class Plane : public Primitive {
00190 public:
00191   Plane();
00192   virtual ~Plane();
00193   virtual void init(const OdeHandle& odeHandle, double mass, 
00194                     const OsgHandle& osgHandle,
00195                     char mode = Body | Geom | Draw) ;
00196 
00197   virtual void update();  
00198   virtual OSGPrimitive* getOSGPrimitive();
00199 
00200 protected:
00201   OSGPlane* osgplane;
00202 };
00203 
00204 
00205 /** Box primitive */
00206 class Box : public Primitive {
00207 public:
00208 
00209   Box(float lengthX, float lengthY, float lengthZ);
00210   virtual ~Box();
00211 
00212   virtual void init(const OdeHandle& odeHandle, double mass,
00213                     const OsgHandle& osgHandle,
00214                     char mode = Body | Geom | Draw) ;
00215 
00216   virtual void update();
00217   virtual OSGPrimitive* getOSGPrimitive();
00218 
00219 protected:
00220   OSGBox* osgbox;
00221 };
00222 
00223 
00224 /** Sphere primitive */
00225 class Sphere : public Primitive {
00226 public:
00227   Sphere(float radius);
00228   virtual ~Sphere();
00229 
00230   virtual void init(const OdeHandle& odeHandle, double mass, 
00231                     const OsgHandle& osgHandle,
00232                     char mode = Body | Geom | Draw) ;
00233 
00234   virtual void update();
00235   virtual OSGPrimitive* getOSGPrimitive();
00236 
00237 protected:
00238   OSGSphere* osgsphere;
00239 };
00240 
00241 /** Capsule primitive */
00242 class Capsule : public Primitive {
00243 public:
00244   Capsule(float radius, float height);
00245   virtual ~Capsule();
00246   virtual void init(const OdeHandle& odeHandle, double mass,
00247                     const OsgHandle& osgHandle,
00248                     char mode = Body | Geom | Draw) ;
00249 
00250   virtual void update();
00251   virtual OSGPrimitive* getOSGPrimitive();
00252 
00253 protected:
00254   OSGCapsule* osgcapsule;
00255 };
00256 
00257 /** Cylinder primitive */
00258 class Cylinder : public Primitive {
00259 public:
00260   Cylinder(float radius, float height);
00261   virtual ~Cylinder();
00262   virtual void init(const OdeHandle& odeHandle, double mass,
00263                     const OsgHandle& osgHandle,
00264                     char mode = Body | Geom | Draw) ;
00265 
00266   virtual void update();
00267   virtual OSGPrimitive* getOSGPrimitive();
00268 
00269 protected:
00270   OSGCylinder* osgcylinder;
00271 };
00272 
00273 
00274 /** Mesh primitive */
00275 class Mesh : public Primitive {
00276 public:
00277   Mesh(const std::string& filename,float scale,GlobalData& global);
00278   virtual ~Mesh();
00279   virtual void init(const OdeHandle& odeHandle, double mass,
00280                     const OsgHandle& osgHandle,
00281                     char mode = Body | Geom | Draw) ;
00282   virtual void update();
00283   virtual OSGPrimitive* getOSGPrimitive();
00284   virtual float getRadius();
00285 
00286 protected:
00287   OSGMesh* osgmesh;
00288   char drawBoundingMode;
00289   const std::string filename;
00290   float scale;
00291   BoundingShape* boundshape;
00292 };
00293 
00294 
00295 /**
00296    Primitive for transforming a geom (primitive without body) 
00297     in respect to a body (primitive with body). 
00298    Hides complexity of ODE TransformGeoms. 
00299 */
00300 class Transform : public Primitive {
00301 public:
00302   /** 
00303       @param parent primitive should have a body and should be initialised
00304       @param child  is transformed by pose in respect to parent. 
00305       This Primitive must NOT have a body
00306   */
00307   Transform(Primitive* parent, Primitive* child, const osg::Matrix& pose);
00308   /// mode is not ignored
00309   virtual void init(const OdeHandle& odeHandle, double mass, 
00310                     const OsgHandle& osgHandle,
00311                     char mode = Body | Geom | Draw);
00312 
00313   virtual void update();
00314   virtual OSGPrimitive* getOSGPrimitive();
00315 
00316 protected:
00317   Primitive* parent;
00318   Primitive* child;
00319   osg::Matrix pose;
00320 };
00321 
00322 /**
00323    Dummy Primitive which returns 0 for geom and body. 
00324    Only useful for representing the static world in terms of primitives.
00325 */
00326 class DummyPrimitive : public Primitive {
00327 public:
00328   /** 
00329       @param parent primitive should have a body and should be initialised
00330       @param child  is transformed by pose in respect to parent. 
00331       This Primitive must NOT have a body
00332   */
00333   DummyPrimitive() {     
00334     body=0;
00335     geom=0;
00336   }
00337   virtual void init(const OdeHandle& odeHandle, double mass, 
00338                     const OsgHandle& osgHandle, char mode = Body | Geom | Draw) {
00339   }
00340   virtual void update() {}
00341   virtual OSGPrimitive* getOSGPrimitive() { return 0; }
00342 };
00343 
00344 
00345 }
00346 #endif
00347 

Generated on Mon Aug 7 16:40:14 2006 for Robotsystem of the Robot Group Leipzig by  doxygen 1.4.7