osgprimitive.h

Go to the documentation of this file.
00001  
00002 /***************************************************************************
00003  *   Copyright (C) 2005-2011 LpzRobots development team                    *
00004  *    Georg Martius  <georg dot martius at web dot de>                     *
00005  *    Frank Guettler <guettler at informatik dot uni-leipzig dot de        *
00006  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
00007  *    Ralf Der       <ralfder at mis dot mpg dot 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 #ifndef __OSGPRIMITIVE_H
00026 #define __OSGPRIMITIVE_H
00027 
00028 #include <string>
00029 #include <vector>
00030 #include <osg/ref_ptr>
00031 #include "osgforwarddecl.h"
00032 #include "osghandle.h"
00033 #include <osgDB/ReadFile>
00034 #include <osgText/Text>
00035 
00036 namespace lpzrobots {
00037 
00038   /**
00039      holds texture file and repeat information. 
00040      
00041    */
00042   class TextureDescr {
00043   public:
00044     TextureDescr(){}
00045     /**
00046        If repeatOnX is negativ then it is used as a unit length for the texture.
00047     */
00048     TextureDescr(const std::string& filename, double repeatOnR, double repeatOnS)
00049       : filename(filename), repeatOnR(repeatOnR), repeatOnS(repeatOnS) 
00050     {      
00051     }
00052     std::string filename;
00053     double repeatOnR;
00054     double repeatOnS;    
00055   };
00056 
00057   /**
00058      Interface class for graphic primitives like spheres, boxes, and meshes,
00059      which can be drawn by OSG. The idea is to hide all the details of the OSG
00060      implementation.
00061   */
00062   class OSGPrimitive {
00063   public:
00064     /* typedef */ enum Quality {Low, Middle, High};
00065 
00066     OSGPrimitive ();
00067     virtual ~OSGPrimitive ();
00068     /** Initialisation of the primitive. Must in order to place the object into the scene.
00069         This function should be overloaded */
00070     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle) = 0;
00071     /// Sets the transformation matrix of this object (position and orientation)
00072     virtual void setMatrix( const osg::Matrix& m4x4 );
00073     /// returns the group object which is the root of all subcomponents of this primitive
00074     virtual osg::Group* getGroup();
00075     /// assigns a texture to the primitive
00076     virtual void setTexture(const std::string& filename);
00077     /// assigns a texture to the primitive, you can choose how often to repeat
00078     virtual void setTexture(const TextureDescr& texture);
00079     /// assigns a texture to the x-th surface of the primitive, you can choose how often to repeat
00080     virtual void setTexture(int surface, const TextureDescr& texture);
00081     /// assign a set of texture to the surfaces of the primitive
00082     virtual void setTextures(const std::vector<TextureDescr>& textures);
00083     /// returns the list of textures
00084     virtual std::vector<TextureDescr> getTextures() const;
00085     /// sets the color for painting this primitive
00086     virtual void setColor(const Color& color);
00087     /// sets the color using the colorschema of osgHandle
00088     virtual void setColor(const std::string& color);
00089     /// returns the current color
00090     virtual Color getColor();
00091 
00092     /// returns a osg transformation object;
00093     virtual osg::Transform* getTransform();
00094     /// returns the osgHandle object
00095     virtual const OsgHandle& getOsgHandle();
00096 
00097   protected:
00098     /// this actually sets the textures
00099     virtual void applyTextures();
00100 
00101     osg::ref_ptr<osg::Geode> geode;
00102     osg::ref_ptr<osg::MatrixTransform> transform;  
00103     osg::ref_ptr<osg::ShapeDrawable> shape;
00104 
00105     std::vector<TextureDescr > textures; 
00106 
00107     OsgHandle osgHandle;
00108   };
00109 
00110   /**
00111      A dummy graphical object, which has no representation in the graphical world.
00112   */
00113   class OSGDummy : public OSGPrimitive {
00114   public:
00115     OSGDummy();
00116 
00117     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00118     virtual void setMatrix( const osg::Matrix& m4x4 );
00119     virtual osg::Group* getGroup();
00120     virtual void setTexture(const std::string& filename);
00121     virtual void setColor(const Color& color);
00122     /// returns a osg transformation object;
00123     virtual osg::Transform* getTransform();  
00124   };
00125 
00126 
00127   /**
00128      Graphical plane (represented as a large thin box, because OSG does not draw planes)
00129   */
00130   class OSGPlane : public OSGPrimitive {
00131   public:
00132     OSGPlane();
00133 
00134     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00135   };
00136 
00137 
00138   /**
00139      Graphical box
00140   */
00141   class OSGBox : public OSGPrimitive {
00142   public:
00143     OSGBox(float lengthX, float lengthY, float lengthZ);
00144     OSGBox(osg::Vec3 dim);
00145 
00146     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00147 
00148     virtual osg::Vec3 getDim();
00149     virtual void setDim(osg::Vec3);
00150   
00151   protected:
00152     osg::Vec3 dim;
00153     osg::Box* box;
00154   };
00155 
00156  /**
00157      Graphical box with Textures
00158   */
00159   class OSGBoxTex : public OSGPrimitive {
00160   public:
00161     OSGBoxTex(float lengthX, float lengthY, float lengthZ);
00162     OSGBoxTex(osg::Vec3 dim);
00163 
00164     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00165 
00166     virtual osg::Vec3 getDim() const { return dim;}
00167     virtual void setDim(const osg::Vec3& _dim) { dim = _dim;}
00168       
00169     virtual void setColor(const Color& color);
00170 
00171   protected:
00172     /// this actually sets the textures, overwritten
00173     virtual void applyTextures();
00174 
00175     osg::Vec3 dim;
00176     // we use one geode for each face of the box for the texture handling
00177     osg::ref_ptr<osg::Geode> faces[6];
00178   };
00179 
00180 
00181   /**
00182      Graphical sphere
00183   */
00184   class OSGSphere : public OSGPrimitive {
00185   public:
00186     OSGSphere(float radius);
00187 
00188     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00189 
00190     float getRadius() { return radius; }
00191   protected:
00192     float radius;  
00193   };
00194 
00195   /**
00196      Graphical capsule (a cylinder with round ends)
00197   */
00198   class OSGCapsule : public OSGPrimitive {
00199   public:
00200     OSGCapsule(float radius, float height);
00201 
00202     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00203 
00204     float getRadius() { return radius; }
00205     float getHeight() { return height; }
00206   protected:
00207     float radius;
00208     float height;
00209   };
00210 
00211 
00212   /**
00213      Graphical cylinder
00214   */
00215   class OSGCylinder : public OSGPrimitive {
00216   public:
00217     OSGCylinder(float radius, float height);
00218 
00219     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00220 
00221     float getRadius() { return radius; }
00222     float getHeight() { return height; }
00223   protected:
00224     float radius;  
00225     float height;
00226   };
00227 
00228 
00229   /**
00230      Graphical Mesh or arbitrary OSG model.
00231   */
00232   class OSGMesh : public OSGPrimitive {
00233   public:
00234     /**
00235        Constuctor
00236        @param filename filename of the model file (search path is osg data path)
00237        @param scale scale factor used for scaling the model 
00238        @param options for model reader
00239      */
00240     OSGMesh(const std::string& filename, float scale = 1, const osgDB::ReaderWriter::Options* options = 0);
00241     ~OSGMesh();
00242     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00243     /**
00244      * Same as init, but the mesh file is not loaded and therefore not displayed.
00245      * This method ensures that the transform is correctly initialised.
00246      * @param osgHandle
00247      */
00248     virtual void virtualInit(const OsgHandle& osgHandle);
00249     virtual float getRadius();
00250     float getScale() { return scale; }
00251 
00252   protected:
00253     std::string filename;
00254     float scale;  
00255     const osgDB::ReaderWriter::Options* options;        
00256     osg::ref_ptr<osg::Node> mesh;
00257     osg::ref_ptr<osg::MatrixTransform> scaletrans;
00258 
00259     virtual void internInit(const OsgHandle& osgHandle, bool loadAndDisplayMesh, Quality quality = Middle);
00260         
00261   };
00262 
00263   /**
00264      Text to be displayed on the hud
00265   */
00266   class OSGText : public OSGPrimitive {
00267   public:
00268     OSGText(const std::string& text, int fontsize = 12, 
00269             osgText::Text::AlignmentType align = osgText::Text::LEFT_BASE_LINE);
00270 
00271     virtual ~OSGText(); 
00272 
00273     virtual void init(const OsgHandle& osgHandle, Quality quality = Middle);
00274     virtual void setMatrix( const osg::Matrix& m4x4 );
00275     virtual osg::Group* getGroup();
00276     virtual void setColor(const Color& color);
00277     /// returns a osg transformation object;
00278     virtual osg::Transform* getTransform();  
00279   private:
00280     osgText::Text* osgText;
00281   };
00282 
00283 
00284 }
00285 
00286 #endif
00287 
Generated on Thu Jun 28 14:45:37 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3