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