Robotsystem of the Robot Group Leipzig: primitive.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Directories
- Related Pages
- Examples

- Search for |

- File List
- File Members

# 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.15  2008/09/11 15:24:01  martius
    00031  *   motioncallback resurrected
    00032  *   noContact substance
    00033  *   use slider center of the connecting objects for slider drawing
    00034  *
    00035  *   Revision 1.14  2008/05/07 16:45:51  martius
    00036  *   code cosmetics and documentation
    00037  *
    00038  *   Revision 1.13  2008/05/01 22:03:54  martius
    00039  *   build system expanded to allow system wide installation
    00040  *   that implies  <ode_robots/> for headers in simulations
    00041  *
    00042  *   Revision 1.12  2007/11/07 13:19:01  martius
    00043  *   toLocal: coordinate transformation
    00044  *
    00045  *   Revision 1.11  2007/08/23 14:51:28  martius
    00046  *   Ray
    00047  *
    00048  *   Revision 1.10  2007/07/31 08:21:34  martius
    00049  *   OSGMesh does not need GlobalData
    00050  *   drawBoundings moved to OsgHandle
    00051  *
    00052  *   Revision 1.9  2007/07/17 07:20:04  martius
    00053  *   setMass added
    00054  *
    00055  *   Revision 1.8  2007/07/03 13:12:52  martius
    00056  *   limitLinearVel
    00057  *
    00058  *   Revision 1.7  2007/03/16 10:51:36  martius
    00059  *   each primitive has a substance
    00060  *   geom userdata is set to primitive itself
    00061  *
    00062  *   Revision 1.6  2007/02/23 15:13:24  martius
    00063  *   setColor
    00064  *
    00065  *   Revision 1.5  2007/01/26 12:05:36  martius
    00066  *   joint support forces in uniform manner
    00067  *
    00068  *   Revision 1.4  2006/12/13 09:09:56  martius
    00069  *   transform objects delete child
    00070  *
    00071  *   Revision 1.3  2006/08/30 08:58:56  martius
    00072  *   categories and collision mask used for static geoms to reduce number of collision checks
    00073  *
    00074  *   Revision 1.2  2006/07/14 12:23:35  martius
    00075  *   selforg becomes HEAD
    00076  *
    00077  *   Revision 1.1.2.18  2006/07/14 11:23:38  martius
    00078  *   revert to older revision of robot3
    00079  *
    00080  *   Revision 1.1.2.16  2006/06/29 16:35:32  robot3
    00081  *   -Mesh code optimized
    00082  *   -includes cleared up, more using forward declarations
    00083  *    (sometimes additionally #include "osgprimitive.h" is needed)
    00084  *
    00085  *   Revision 1.1.2.15  2006/06/27 14:14:29  robot3
    00086  *   -optimized mesh and boundingshape code
    00087  *   -other changes
    00088  *
    00089  *   Revision 1.1.2.14  2006/06/23 08:53:56  robot3
    00090  *   made some changes on primitive Mesh
    00091  *
    00092  *   Revision 1.1.2.13  2006/05/29 22:03:26  martius
    00093  *   cylinder
    00094  *
    00095  *   Revision 1.1.2.12  2006/05/29 21:27:02  robot3
    00096  *   made some preparations for the boundingshape of the Mesh
    00097  *
    00098  *   Revision 1.1.2.11  2006/05/28 22:14:57  martius
    00099  *   heightfield included
    00100  *
    00101  *   Revision 1.1.2.10  2006/05/24 12:23:10  robot3
    00102  *   -passive_mesh works now (simple bound_version)
    00103  *   -Primitive Mesh now exists (simple bound_version)
    00104  *
    00105  *   Revision 1.1.2.9  2006/04/04 14:13:24  fhesse
    00106  *   documentation improved
    00107  *
    00108  *   Revision 1.1.2.8  2006/03/29 15:07:17  martius
    00109  *   Dummy Primitive
    00110  *
    00111  *   Revision 1.1.2.7  2006/01/31 15:45:35  martius
    00112  *   proper destruction
    00113  *
    00114  *   Revision 1.1.2.6  2006/01/12 14:21:00  martius
    00115  *   drawmode, material
    00116  *
    00117  *   Revision 1.1.2.5  2005/12/29 12:58:42  martius
    00118  *   *** empty log message ***
    00119  *
    00120  *   Revision 1.1.2.4  2005/12/15 17:03:43  martius
    00121  *   cameramanupulator setPose is working
    00122  *   joints have setter and getter parameters
    00123  *   Primitives are not longer inherited from OSGPrimitive, moreover
    00124  *   they aggregate them
    00125  *
    00126  *   Revision 1.1.2.3  2005/12/14 15:36:45  martius
    00127  *   joints are visible now
    00128  *
    00129  *   Revision 1.1.2.2  2005/12/13 18:11:14  martius
    00130  *   transform primitive added, some joints stuff done, forward declaration
    00131  *
    00132  *   Revision 1.1.2.1  2005/12/06 10:13:25  martius
    00133  *   openscenegraph integration started
    00134  *
    00135  *                                                                 *
    00136  *                                                                         *
    00137  ***************************************************************************/
    00138 #ifndef __PRIMITIVE_H
    00139 #define __PRIMITIVE_H
    00140
    00141 #include <osg/Matrix>
    00142 #include <ode/common.h>
    00143
    00144 #include "substance.h"
    00145 // another forward declaration "block"
    00146 #include "osgforwarddecl.h"
    00147
    00148 namespace lpzrobots {
    00149
    00150    /***** begin of forward declaration block *****/
    00151    class BoundingShape;
    00152    class OSGPrimitive;
    00153    class OSGPlane;
    00154    class OSGBox;
    00155    class OSGSphere;
    00156    class OSGCapsule;
    00157    class OSGCylinder;
    00158    class OSGDummy;
    00159    class OSGMesh;
    00160    typedef struct GlobalData;
    00161    class OdeHandle;
    00162    class OsgHandle;
    00163    class Color;
    00164    /*****  end of forward declaration block  *****/
    00165
    00166
    00167 /// returns the osg (4x4) pose matrix of the ode geom
    00168 osg::Matrix osgPose( dGeomID geom );
    00169 /// returns the osg (4x4) pose matrix of the ode body
    00170 osg::Matrix osgPose( dBodyID body );
    00171 /// converts a position vector and a rotation matrix from ode to osg 4x4 matrix
    00172 osg::Matrix osgPose( const double * position , const double * rotation );
    00173 /// converts the rotation component of pose into an ode rotation matrix
    00174 void odeRotation( const osg::Matrix& pose , dMatrix3& odematrix);
    00175
    00176 /**
    00177    Interface class for primitives represented in the physical and graphical world.
    00178    This is intended to bring OSG and ODE together and hide most implementation details.
    00179 */
    00180 class Primitive {
    00181 public:
    00182   /** Body means that it is a dynamic object with a body.
    00183       Geom means it has a geometrical represenation used for collision detection.
    00184       Draw means the primitive is drawn
    00185       Child is only used internally and is used for transformed geoms.
    00186   */
    00187   typedef enum Modes {Body=1, Geom=2, Draw=4, Child=8};
    00188   typedef enum Category { Dyn=1, Stat=2};
    00189
    00190   Primitive ();
    00191   virtual ~Primitive ();
    00192   /** registers primitive in ODE and OSG.
    00193       @param osgHandle scruct with ODE variables inside (to specify space, world...)
    00194       @param mass Mass of the object in ODE (if withBody = true)
    00195       @param osgHandle scruct with OSG variables inside (scene node, color ...)
    00196       @param mode is a conjuction of Modes.
    00197    */
    00198   virtual void init(const OdeHandle& odeHandle, double mass,
    00199                     const OsgHandle& osgHandle,
    00200                     char mode = Body | Geom | Draw)  = 0 ;
    00201
    00202   /** Updates the OSG nodes with ODE coordinates.
    00203       This function must be overloaded (usually calls setMatrix of OsgPrimitives)
    00204    */
    00205   virtual void update() =0 ;
    00206
    00207   /// returns the assoziated osg primitive if there or 0
    00208   virtual OSGPrimitive* getOSGPrimitive() = 0;
    00209
    00210   /// sets the color for the underlaying osgprimitive
    00211   virtual void setColor(const Color& color);
    00212
    00213   /// assigns a texture to the primitive
    00214   virtual void setTexture(const std::string& filename);
    00215   /// assigns a texture to the primitive, you can choose if the texture should be repeated
    00216   virtual void setTexture(const std::string& filename, bool repeatOnX, bool repeatOnY);
    00217
    00218   /// set the position of the primitive (orientation is preserved)
    00219   void setPosition(const osg::Vec3& pos);
    00220   /// set the pose of the primitive
    00221   void setPose(const osg::Matrix& pose);
    00222   /// returns the position
    00223   osg::Vec3 getPosition() const;
    00224   /// returns the pose
    00225   osg::Matrix getPose() const;
    00226
    00227   /// sets the mass of the body (uniform)
    00228   virtual void setMass(double mass) = 0;
    00229   /** sets full mass specification
    00230     \param cg center of gravity vector
    00231     \param I  3x3 interia tensor
    00232   */
    00233   void setMass(double mass, double cgx, double cgy, double cgz,
    00234                double I11, double I22, double I33,
    00235                double I12, double I13, double I23);
    00236
    00237   /// returns ODE geomID if there
    00238   dGeomID getGeom() const;
    00239   /// returns ODE bodyID if there
    00240   dBodyID getBody() const;
    00241
    00242   /// checks whether the object has higher velocity than maxVel and limits it in case
    00243   bool limitLinearVel(double maxVel);
    00244
    00245   /// return the given point transformed to local coordinates of the primitive
    00246   osg::Vec3 toLocal(const osg::Vec3& pos) const;
    00247   /** return the given vector or axis transformed to local coordinates
    00248       of the primitive (translation depends on the 4th coordinate)
    00249   */
    00250   osg::Vec4 toLocal(const osg::Vec4& axis) const;
    00251
    00252 protected:
    00253   /** attaches geom to body (if any) and sets the category bits and collision bitfields.
    00254       assumes: mode & Geom != 0
    00255    */
    00256   virtual void attachGeomAndSetColliderFlags();
    00257
    00258 public:
    00259   Substance substance; // substance description
    00260 protected:
    00261   dGeomID geom;
    00262   dBodyID body;
    00263   char mode;
    00264 };
    00265
    00266
    00267 /** Plane primitive */
    00268 class Plane : public Primitive {
    00269 public:
    00270   Plane();
    00271   virtual ~Plane();
    00272   virtual void init(const OdeHandle& odeHandle, double mass,
    00273                     const OsgHandle& osgHandle,
    00274                     char mode = Body | Geom | Draw) ;
    00275
    00276   virtual void update();
    00277   virtual OSGPrimitive* getOSGPrimitive();
    00278
    00279   virtual void setMass(double mass);
    00280
    00281 protected:
    00282   OSGPlane* osgplane;
    00283 };
    00284
    00285
    00286 /** Box primitive */
    00287 class Box : public Primitive {
    00288 public:
    00289
    00290   Box(float lengthX, float lengthY, float lengthZ);
    00291   virtual ~Box();
    00292
    00293   virtual void init(const OdeHandle& odeHandle, double mass,
    00294                     const OsgHandle& osgHandle,
    00295                     char mode = Body | Geom | Draw) ;
    00296
    00297   virtual void update();
    00298   virtual OSGPrimitive* getOSGPrimitive();
    00299
    00300   virtual void setMass(double mass);
    00301 protected:
    00302   OSGBox* osgbox;
    00303 };
    00304
    00305
    00306 /** Sphere primitive */
    00307 class Sphere : public Primitive {
    00308 public:
    00309   Sphere(float radius);
    00310   virtual ~Sphere();
    00311
    00312   virtual void init(const OdeHandle& odeHandle, double mass,
    00313                     const OsgHandle& osgHandle,
    00314                     char mode = Body | Geom | Draw) ;
    00315
    00316   virtual void update();
    00317   virtual OSGPrimitive* getOSGPrimitive();
    00318
    00319   virtual void setMass(double mass);
    00320
    00321 protected:
    00322   OSGSphere* osgsphere;
    00323 };
    00324
    00325 /** Capsule primitive */
    00326 class Capsule : public Primitive {
    00327 public:
    00328   Capsule(float radius, float height);
    00329   virtual ~Capsule();
    00330   virtual void init(const OdeHandle& odeHandle, double mass,
    00331                     const OsgHandle& osgHandle,
    00332                     char mode = Body | Geom | Draw) ;
    00333
    00334   virtual void update();
    00335   virtual OSGPrimitive* getOSGPrimitive();
    00336
    00337   virtual void setMass(double mass);
    00338
    00339 protected:
    00340   OSGCapsule* osgcapsule;
    00341 };
    00342
    00343 /** Cylinder primitive */
    00344 class Cylinder : public Primitive {
    00345 public:
    00346   Cylinder(float radius, float height);
    00347   virtual ~Cylinder();
    00348   virtual void init(const OdeHandle& odeHandle, double mass,
    00349                     const OsgHandle& osgHandle,
    00350                     char mode = Body | Geom | Draw) ;
    00351
    00352   virtual void update();
    00353   virtual OSGPrimitive* getOSGPrimitive();
    00354
    00355   virtual void setMass(double mass);
    00356 protected:
    00357   OSGCylinder* osgcylinder;
    00358 };
    00359
    00360 /** Ray primitive
    00361     The actual visual representation can have different length than the
    00362     ray object. This is specified by length.
    00363     SetLength is an efficient way to change the length at runtime.
    00364 */
    00365 class Ray : public Primitive {
    00366 public:
    00367   Ray(double range, float thickness, float length);
    00368   virtual ~Ray();
    00369   virtual void init(const OdeHandle& odeHandle, double mass,
    00370       const OsgHandle& osgHandle,
    00371       char mode = Geom | Draw) ;
    00372
    00373   void setLength(float len);
    00374   virtual void update();
    00375   virtual OSGPrimitive* getOSGPrimitive();
    00376
    00377   virtual void setMass(double mass);
    00378 protected:
    00379   double range;
    00380   float thickness;
    00381   float length;
    00382   OSGBox* osgbox;
    00383 };
    00384
    00385
    00386
    00387
    00388 /** Mesh primitive */
    00389 class Mesh : public Primitive {
    00390 public:
    00391   Mesh(const std::string& filename,float scale);
    00392   virtual ~Mesh();
    00393   virtual void init(const OdeHandle& odeHandle, double mass,
    00394                     const OsgHandle& osgHandle,
    00395                     char mode = Body | Geom | Draw) ;
    00396   virtual void update();
    00397   virtual OSGPrimitive* getOSGPrimitive();
    00398   virtual float getRadius();
    00399
    00400   virtual void setMass(double mass);
    00401 protected:
    00402   OSGMesh* osgmesh;
    00403   const std::string filename;
    00404   float scale;
    00405   BoundingShape* boundshape;
    00406 };
    00407
    00408
    00409 /**
    00410    Primitive for transforming a geom (primitive without body)
    00411     in respect to a body (primitive with body).
    00412    Hides complexity of ODE TransformGeoms.
    00413 */
    00414 class Transform : public Primitive {
    00415 public:
    00416   /**
    00417       @param parent primitive should have a body and should be initialised
    00418       @param child  is transformed by pose in respect to parent.
    00419       This Primitive must NOT have a body and should not be initialised
    00420   */
    00421   Transform(Primitive* parent, Primitive* child, const osg::Matrix& pose);
    00422
    00423   /// destructor deletes child object
    00424   ~Transform();
    00425
    00426   /** initialised the transform object. This automatically
    00427       initialises the child geom.
    00428       @param mass mass of the child
    00429       @param mode is the mode for the child, except that Body bit is ignored (child can't have a body)
    00430    */
    00431   virtual void init(const OdeHandle& odeHandle, double mass,
    00432                     const OsgHandle& osgHandle,
    00433                     char mode = Body | Geom | Draw);
    00434
    00435   virtual void update();
    00436   virtual OSGPrimitive* getOSGPrimitive();
    00437
    00438   virtual void setMass(double mass);
    00439 protected:
    00440   Primitive* parent;
    00441   Primitive* child;
    00442   osg::Matrix pose;
    00443 };
    00444
    00445 /**
    00446    Dummy Primitive which returns 0 for geom and body.
    00447    Only useful for representing the static world in terms of primitives.
    00448 */
    00449 class DummyPrimitive : public Primitive {
    00450 public:
    00451
    00452   DummyPrimitive() {
    00453     body=0;
    00454     geom=0;
    00455   }
    00456   virtual void init(const OdeHandle& odeHandle, double mass,
    00457                     const OsgHandle& osgHandle, char mode = Body | Geom | Draw) {
    00458   }
    00459   virtual void update() {}
    00460   virtual OSGPrimitive* getOSGPrimitive() { return 0; }
    00461
    00462   virtual void setMass(double mass) {}
    00463
    00464 };
    00465
    00466
    00467 }
    00468 #endif
    00469