Robot Simulator of the Robotics Group for Self-Organization of Control: osgprimitive.h Source File

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

- Search for |

- File List
- File Members

# osgprimitive.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 openscenegraph usage.         *
    00026  *                                                                         *
    00027  *                                                                         *
    00028  *                                                                         *
    00029  *   $Log: osgprimitive.h,v $
    00030  *   Revision 1.7  2009/07/30 11:34:15  guettler
    00031  *   added check if noGraphics in OsgHandle is set
    00032  *
    00033  *   Revision 1.6  2009/04/26 10:28:49  martius
    00034  *   added setColor for OsgBoxTex
    00035  *
    00036  *   Revision 1.5  2009/01/20 17:29:10  martius
    00037  *   changed texture handling. In principle it is possible to set multiple textures
    00038  *   per osgPrimitive.
    00039  *   New osgboxtex started that supports custom textures.
    00040  *
    00041  *   Revision 1.4  2008/05/07 16:45:51  martius
    00042  *   code cosmetics and documentation
    00043  *
    00044  *   Revision 1.3  2007/08/23 14:52:26  martius
    00045  *   box is resizeable
    00046  *
    00047  *   Revision 1.2  2006/07/14 12:23:35  martius
    00048  *   selforg becomes HEAD
    00049  *
    00050  *   Revision 1.1.2.13  2006/07/14 11:23:38  martius
    00051  *   revert to older revision of robot3
    00052  *
    00053  *   Revision 1.1.2.11  2006/05/24 12:23:10  robot3
    00054  *   -passive_mesh works now (simple bound_version)
    00055  *   -Primitive Mesh now exists (simple bound_version)
    00056  *
    00057  *   Revision 1.1.2.10  2006/05/18 07:39:41  robot3
    00058  *   -setTexture(string& filename,bool repeatOnX, bool repeatOnY) added
    00059  *    note that this does not work yet (the bool parameter have no effect)
    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:06:40  martius
    00065  *   OSGMesh
    00066  *
    00067  *   Revision 1.1.2.7  2005/12/22 14:14:12  martius
    00068  *   quality level
    00069  *
    00070  *   Revision 1.1.2.6  2005/12/15 17:03:43  martius
    00071  *   cameramanupulator setPose is working
    00072  *   joints have setter and getter parameters
    00073  *   Primitives are not longer inherited from OSGPrimitive, moreover
    00074  *   they aggregate them
    00075  *
    00076  *   Revision 1.1.2.5  2005/12/14 15:36:45  martius
    00077  *   joints are visible now
    00078  *
    00079  *   Revision 1.1.2.4  2005/12/13 18:11:13  martius
    00080  *   transform primitive added, some joints stuff done, forward declaration
    00081  *
    00082  *   Revision 1.1.2.3  2005/12/11 23:35:08  martius
    00083  *   *** empty log message ***
    00084  *
    00085  *   Revision 1.1.2.2  2005/12/09 16:54:16  martius
    00086  *   camera is woring now
    00087  *
    00088  *   Revision 1.1.2.1  2005/12/06 10:13:24  martius
    00089  *   openscenegraph integration started
    00090  *
    00091  *                                                                 *
    00092  *                                                                         *
    00093  ***************************************************************************/
    00094 #ifndef __OSGPRIMITIVE_H
    00095 #define __OSGPRIMITIVE_H
    00096
    00097 #include <string>
    00098 #include <vector>
    00099 #include <osg/ref_ptr>
    00100 #include "osgforwarddecl.h"
    00101 #include "osghandle.h"
    00102 #include <osgDB/ReadFile>
    00103
    00104 namespace lpzrobots {
    00105
    00106   /**
    00107      holds texture file and repeat information.
    00108
    00109    */
    00110   class TextureDescr {
    00111   public:
    00112     TextureDescr(){}
    00113     /**
    00114        If repeatOnX is negativ then it is used as a unit length for the texture.
    00115     */
    00116     TextureDescr(const std::string& filename, double repeatOnR, double repeatOnS)
    00117       : filename(filename), repeatOnR(repeatOnR), repeatOnS(repeatOnS)
    00118     {
    00119     }
    00120     std::string filename;
    00121     double repeatOnR;
    00122     double repeatOnS;
    00123   };
    00124
    00125   /**
    00126      Interface class for graphic primitives like spheres, boxes, and meshes,
    00127      which can be drawn by OSG. The idea is to hide all the details of the OSG
    00128      implementation.
    00129   */
    00130   class OSGPrimitive {
    00131   public:
    00132     /* typedef */ enum Quality {Low, Middle, High};
    00133
    00134     OSGPrimitive ();
    00135     virtual ~OSGPrimitive ();
    00136     /** Initialisation of the primitive. Must in order to place the object into the scene.
    00137         This function should be overloaded */
    00138     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle) = 0;
    00139     /// Sets the transformation matrix of this object (position and orientation)
    00140     virtual void setMatrix( const osg::Matrix& m4x4 );
    00141     /// returns the group object which is the root of all subcomponents of this primitive
    00142     virtual osg::Group* getGroup();
    00143     /// assigns a texture to the primitive
    00144     virtual void setTexture(const std::string& filename);
    00145     /// assigns a texture to the primitive, you can choose how often to repeat
    00146     virtual void setTexture(const std::string& filename, double repeatOnR, double repeatOnS);
    00147     /// assigns a texture to the x-th surface of the primitive, you can choose how often to repeat
    00148     virtual void setTexture(int surface, const std::string& filename, double repeatOnR, double repeatOnS);
    00149     /// returns the list of textures
    00150     virtual std::vector<TextureDescr> getTextures();
    00151     /// sets the color for painting this primitive
    00152     virtual void setColor(const Color& color);
    00153     /// returns a osg transformation object;
    00154     virtual osg::Transform* getTransform();
    00155
    00156   protected:
    00157     /// this actually sets the textures
    00158     virtual void applyTextures();
    00159
    00160     osg::ref_ptr<osg::Geode> geode;
    00161     osg::ref_ptr<osg::MatrixTransform> transform;
    00162     osg::ref_ptr<osg::ShapeDrawable> shape;
    00163
    00164     std::vector<TextureDescr > textures;
    00165
    00166     const OsgHandle* osgHandle;
    00167   };
    00168
    00169   /**
    00170      A dummy graphical object, which has no representation in the graphical world.
    00171   */
    00172   class OSGDummy : public OSGPrimitive {
    00173   public:
    00174     OSGDummy();
    00175
    00176     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
    00177     virtual void setMatrix( const osg::Matrix& m4x4 );
    00178     virtual osg::Group* getGroup();
    00179     virtual void setTexture(const std::string& filename);
    00180     virtual void setColor(const Color& color);
    00181     /// returns a osg transformation object;
    00182     virtual osg::Transform* getTransform();
    00183   };
    00184
    00185
    00186   /**
    00187      Graphical plane (represented as a large thin box, because OSG does not draw planes)
    00188   */
    00189   class OSGPlane : public OSGPrimitive {
    00190   public:
    00191     OSGPlane();
    00192
    00193     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
    00194   };
    00195
    00196
    00197   /**
    00198      Graphical box
    00199   */
    00200   class OSGBox : public OSGPrimitive {
    00201   public:
    00202     OSGBox(float lengthX, float lengthY, float lengthZ);
    00203     OSGBox(osg::Vec3 dim);
    00204
    00205     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
    00206
    00207     osg::Vec3 getDim();
    00208     void setDim(osg::Vec3);
    00209
    00210   protected:
    00211     osg::Vec3 dim;
    00212     osg::Box* box;
    00213   };
    00214
    00215  /**
    00216      Graphical box with Textures
    00217   */
    00218   class OSGBoxTex : public OSGPrimitive {
    00219   public:
    00220     OSGBoxTex(float lengthX, float lengthY, float lengthZ);
    00221     OSGBoxTex(osg::Vec3 dim);
    00222
    00223     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
    00224
    00225     osg::Vec3 getDim() const { return dim;}
    00226     void setDim(const osg::Vec3& _dim) { dim = _dim;}
    00227
    00228     virtual void setColor(const Color& color);
    00229
    00230   protected:
    00231     /// this actually sets the textures, overwritten
    00232     virtual void applyTextures();
    00233
    00234     osg::Vec3 dim;
    00235     // we use one geode for each face of the box for the texture handling
    00236     osg::ref_ptr<osg::Geode> faces[6];
    00237   };
    00238
    00239
    00240   /**
    00241      Graphical sphere
    00242   */
    00243   class OSGSphere : public OSGPrimitive {
    00244   public:
    00245     OSGSphere(float radius);
    00246
    00247     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
    00248
    00249     float getRadius() { return radius; }
    00250   protected:
    00251     float radius;
    00252   };
    00253
    00254   /**
    00255      Graphical capsule (a cylinder with round ends)
    00256   */
    00257   class OSGCapsule : public OSGPrimitive {
    00258   public:
    00259     OSGCapsule(float radius, float height);
    00260
    00261     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
    00262
    00263     float getRadius() { return radius; }
    00264     float getHeight() { return height; }
    00265   protected:
    00266     float radius;
    00267     float height;
    00268   };
    00269
    00270
    00271   /**
    00272      Graphical cylinder
    00273   */
    00274   class OSGCylinder : public OSGPrimitive {
    00275   public:
    00276     OSGCylinder(float radius, float height);
    00277
    00278     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
    00279
    00280     float getRadius() { return radius; }
    00281     float getHeight() { return height; }
    00282   protected:
    00283     float radius;
    00284     float height;
    00285   };
    00286
    00287
    00288   /**
    00289      Graphical Mesh or arbitrary OSG model.
    00290   */
    00291   class OSGMesh : public OSGPrimitive {
    00292   public:
    00293     /**
    00294        Constuctor
    00295        @param filename filename of the model file (search path is osg data path)
    00296        @param scale scale factor used for scaling the model
    00297        @param options for model reader
    00298      */
    00299     OSGMesh(const std::string& filename, float scale = 1, const osgDB::ReaderWriter::Options* options = 0);
    00300     ~OSGMesh();
    00301     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
    00302     virtual float getRadius();
    00303     float getScale() { return scale; }
    00304
    00305   protected:
    00306     std::string filename;
    00307     float scale;
    00308     const osgDB::ReaderWriter::Options* options;
    00309     osg::ref_ptr<osg::Node> mesh;
    00310     osg::ref_ptr<osg::MatrixTransform> scaletrans;
    00311
    00312   };
    00313
    00314 }
    00315
    00316 #endif
    00317