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  *                                                                         *
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  *                                                                         *
00025  *   This file provides basic primitives for ODE and openscenegraph        *
00026  *                                                                         *
00027  *                                                                         *
00028  *   $Log: primitive.h,v $
00029  *   Revision 1.1.2.9  2006/04/04 14:13:24  fhesse
00030  *   documentation improved
00031  *
00032  *   Revision 1.1.2.8  2006/03/29 15:07:17  martius
00033  *   Dummy Primitive
00034  *
00035  *   Revision 1.1.2.7  2006/01/31 15:45:35  martius
00036  *   proper destruction
00037  *
00038  *   Revision 1.1.2.6  2006/01/12 14:21:00  martius
00039  *   drawmode, material
00040  *
00041  *   Revision 1.1.2.5  2005/12/29 12:58:42  martius
00042  *   *** empty log message ***
00043  *
00044  *   Revision 1.1.2.4  2005/12/15 17:03:43  martius
00045  *   cameramanupulator setPose is working
00046  *   joints have setter and getter parameters
00047  *   Primitives are not longer inherited from OSGPrimitive, moreover
00048  *   they aggregate them
00049  *
00050  *   Revision 1.1.2.3  2005/12/14 15:36:45  martius
00051  *   joints are visible now
00052  *
00053  *   Revision 1.1.2.2  2005/12/13 18:11:14  martius
00054  *   transform primitive added, some joints stuff done, forward declaration
00055  *
00056  *   Revision 1.1.2.1  2005/12/06 10:13:25  martius
00057  *   openscenegraph integration started
00058  *
00059  *                                                                 *
00060  *                                                                         *
00061  ***************************************************************************/
00062 #ifndef __PRIMITIVE_H
00063 #define __PRIMITIVE_H
00064 
00065 #include "osgprimitive.h"
00066 #include "odehandle.h"
00067 #include <osg/Matrix>
00068 
00069 #include <ode/common.h>
00070 
00071 namespace lpzrobots {
00072 
00073 
00074 /// returns the osg (4x4) pose matrix of the ode geom
00075 osg::Matrix osgPose( dGeomID geom );
00076 /// returns the osg (4x4) pose matrix of the ode body
00077 osg::Matrix osgPose( dBodyID body );
00078 /// converts a position vector and a rotation matrix from ode to osg 4x4 matrix
00079 osg::Matrix osgPose( const double * position , const double * rotation );
00080 /// converts the rotation component of pose into an ode rotation matrix
00081 void odeRotation( const osg::Matrix& pose , dMatrix3& odematrix);
00082 
00083 /**
00084    Interface class for primitives represented in the physical and graphical world.
00085    This is intended to bring OSG and ODE together and hide most implementation details.
00086 */
00087 class Primitive {
00088 public:
00089   /** Body means that it is a dynamic object with a body.
00090       Geom means it has a geometrical represenation used for collision detection.
00091       Draw means the primitive is drawn
00092   */
00093   typedef enum Modes {Body=1, Geom=2, Draw=4};
00094 
00095   Primitive ();
00096   virtual ~Primitive ();
00097   /** registers primitive in ODE and OSG. 
00098       @param osgHandle scruct with ODE variables inside (to specify space, world...)
00099       @param mass Mass of the object in ODE (if withBody = true)
00100       @param osgHandle scruct with OSG variables inside (scene node, color ...)
00101       @param mode is a conjuction of Modes.
00102    */
00103   virtual void init(const OdeHandle& odeHandle, double mass,
00104                     const OsgHandle& osgHandle,
00105                     char mode = Body | Geom | Draw)  = 0 ;
00106 
00107   /** Updates the OSG nodes with ODE coordinates.
00108       This function must be overloaded (usually calls setMatrix of OsgPrimitives)
00109    */
00110   virtual void update() =0 ;
00111 
00112   /// returns the assoziated osg primitive if there or 0
00113   virtual OSGPrimitive* getOSGPrimitive() = 0;
00114 
00115   /// set the position of the primitive (orientation is preserved)
00116   void setPosition(const osg::Vec3& pos);
00117   /// set the pose of the primitive
00118   void setPose(const osg::Matrix& pose);
00119   /// returns the position
00120   osg::Vec3 getPosition() const;
00121   /// returns the pose
00122   osg::Matrix getPose() const;
00123 
00124   /// returns ODE geomID if there
00125   dGeomID getGeom() const;    
00126   /// returns ODE bodyID if there
00127   dBodyID getBody() const;
00128 
00129 protected:
00130   dGeomID geom;
00131   dBodyID body;
00132   char mode;
00133 };
00134 
00135 
00136 /** Plane primitive */
00137 class Plane : public Primitive {
00138 public:
00139   Plane();
00140   virtual ~Plane();
00141   virtual void init(const OdeHandle& odeHandle, double mass, 
00142                     const OsgHandle& osgHandle,
00143                     char mode = Body | Geom | Draw) ;
00144 
00145   virtual void update();  
00146   virtual OSGPrimitive* getOSGPrimitive() { return osgplane; }
00147 
00148 protected:
00149   OSGPlane* osgplane;
00150 };
00151 
00152 
00153 /** Box primitive */
00154 class Box : public Primitive {
00155 public:
00156 
00157   Box(float lengthX, float lengthY, float lengthZ);
00158   virtual ~Box();
00159 
00160   virtual void init(const OdeHandle& odeHandle, double mass,
00161                     const OsgHandle& osgHandle,
00162                     char mode = Body | Geom | Draw) ;
00163 
00164   virtual void update();
00165   virtual OSGPrimitive* getOSGPrimitive() { return osgbox; }
00166 
00167 protected:
00168   OSGBox* osgbox;
00169 };
00170 
00171 
00172 /** Sphere primitive */
00173 class Sphere : public Primitive {
00174 public:
00175   Sphere(float radius);
00176   virtual ~Sphere();
00177 
00178   virtual void init(const OdeHandle& odeHandle, double mass, 
00179                     const OsgHandle& osgHandle,
00180                     char mode = Body | Geom | Draw) ;
00181 
00182   virtual void update();
00183   virtual OSGPrimitive* getOSGPrimitive() { return osgsphere; }
00184 
00185 protected:
00186   OSGSphere* osgsphere;
00187 };
00188 
00189 /** Capsule primitive */
00190 class Capsule : public Primitive {
00191 public:
00192   Capsule(float radius, float height);
00193   virtual ~Capsule();
00194   virtual void init(const OdeHandle& odeHandle, double mass,
00195                     const OsgHandle& osgHandle,
00196                     char mode = Body | Geom | Draw) ;
00197 
00198   virtual void update();
00199   virtual OSGPrimitive* getOSGPrimitive() { return osgcapsule; }
00200 
00201 protected:
00202   OSGCapsule* osgcapsule;
00203 };
00204 
00205 
00206 /**
00207    Primitive for transforming a geom (primitive without body) 
00208     in respect to a body (primitive with body). 
00209    Hides complexity of ODE TransformGeoms. 
00210 */
00211 class Transform : public Primitive {
00212 public:
00213   /** 
00214       @param parent primitive should have a body and should be initialised
00215       @param child  is transformed by pose in respect to parent. 
00216       This Primitive must NOT have a body
00217   */
00218   Transform(Primitive* parent, Primitive* child, const osg::Matrix& pose);
00219   /// mode is ignored
00220   virtual void init(const OdeHandle& odeHandle, double mass, 
00221                     const OsgHandle& osgHandle,
00222                     char mode = Body | Geom | Draw);
00223 
00224   virtual void update();
00225   virtual OSGPrimitive* getOSGPrimitive() { return 0; }
00226 
00227 protected:
00228   Primitive* parent;
00229   Primitive* child;
00230   osg::Matrix pose;
00231 };
00232 
00233 /**
00234    Dummy Primitive which returns 0 for geom and body. 
00235    Only useful for representing the static world in terms of primitives.
00236 */
00237 class DummyPrimitive : public Primitive {
00238 public:
00239   /** 
00240       @param parent primitive should have a body and should be initialised
00241       @param child  is transformed by pose in respect to parent. 
00242       This Primitive must NOT have a body
00243   */
00244   DummyPrimitive() {     
00245     body=0;
00246     geom=0;
00247   }
00248   virtual void init(const OdeHandle& odeHandle, double mass, 
00249                     const OsgHandle& osgHandle, char mode = Body | Geom | Draw) {
00250   }
00251   virtual void update() {}
00252   virtual OSGPrimitive* getOSGPrimitive() { return 0; }
00253 };
00254 
00255 
00256 }
00257 #endif
00258 

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