Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Primitive.h
Go to the documentation of this file.
00001 /********************************************************************
00002 **                Image Component Library (ICL)                    **
00003 **                                                                 **
00004 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld          **
00005 **                         Neuroinformatics Group                  **
00006 ** Website: www.iclcv.org and                                      **
00007 **          http://opensource.cit-ec.de/projects/icl               **
00008 **                                                                 **
00009 ** File   : ICLGeom/src/ICLGeom/Primitive.h                        **
00010 ** Module : ICLGeom                                                **
00011 ** Authors: Christof Elbrechter                                    **
00012 **                                                                 **
00013 **                                                                 **
00014 ** GNU LESSER GENERAL PUBLIC LICENSE                               **
00015 ** This file may be used under the terms of the GNU Lesser General **
00016 ** Public License version 3.0 as published by the                  **
00017 **                                                                 **
00018 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
00019 ** included in the packaging of this file.  Please review the      **
00020 ** following information to ensure the license requirements will   **
00021 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt                **
00022 **                                                                 **
00023 ** The development of this software was supported by the           **
00024 ** Excellence Cluster EXC 277 Cognitive Interaction Technology.    **
00025 ** The Excellence Cluster EXC 277 is a grant of the Deutsche       **
00026 ** Forschungsgemeinschaft (DFG) in the context of the German       **
00027 ** Excellence Initiative.                                          **
00028 **                                                                 **
00029 ********************************************************************/
00030 
00031 #pragma once
00032 
00033 #ifndef ICL_HAVE_OPENGL
00034 #if WIN32
00035 #pragma WARNING("this header must not be included if ICL_HAVE_OPENGL is not defined")
00036 #else
00037 #warning "this header must not be included if ICL_HAVE_OPENGL is not defined"
00038 #endif
00039 #else
00040 
00041 #include <ICLUtils/CompatMacros.h>
00042 #include <ICLGeom/GeomDefs.h>
00043 #include <ICLCore/Img.h>
00044 #include <ICLMath/FixedVector.h>
00045 #include <ICLUtils/Array2D.h>
00046 #include <ICLQt/GLImg.h>
00047 
00048 namespace icl{
00049   namespace geom{
00051     class SceneObject;
00054 
00055 
00067     struct Primitive{
00068   
00070       enum Type{
00071         vertex   = 1<<0, //<! vertex
00072         line     = 1<<1, //<! line primitive (adressing two vertices -> start and end position of the line)
00073         triangle = 1<<2, //<! triange primitive (adressing three vertices)
00074         quad     = 1<<3, //<! quad primitve (adressing four vertices)
00075         polygon  = 1<<4, //<! polygon primitive (adressing at least 3 vertices)
00076         texture  = 1<<5, //<! texture primitive (using 4 vertices like a quad as textured rectangle)
00077         text     = 1<<6, //<! text primitive (internally implmented as texture or as billboard)
00078         nothing  = 1<<7, //<! internally used type
00079         custom   = 1<<20, //<! for custom primitives
00080         PRIMITIVE_TYPE_COUNT = 8,                //<! also for internal use only
00081         all      = (1<<PRIMITIVE_TYPE_COUNT)-1,    //<! all types
00082         faces    = triangle | quad | polygon | texture | text
00083       };
00084     
00085       Type type;        
00086       GeomColor color;  
00087       
00089 
00093       struct RenderContext{
00094         const std::vector<Vec> &vertices;            
00095         const std::vector<Vec> &normals;             
00096         const std::vector<GeomColor> &vertexColors;  
00097         const std::vector<utils::SmartPtr<qt::GLImg> > &sharedTextures; 
00098         bool lineColorsFromVertices;                 
00099         bool triangleColorsFromVertices;             
00100         bool quadColorsFromVertices;                 
00101         bool polygonColorsFromVertices;              
00102         SceneObject *object;                         
00103       };
00104   
00106       Primitive(Type type=nothing, const GeomColor &color=GeomColor(255,255,255,255)):type(type),color(color){}
00107   
00109       virtual ~Primitive() {}
00110   
00112       virtual void render(const Primitive::RenderContext &ctx) = 0;
00113       
00115       virtual Primitive *copy() const = 0;
00116     };
00117     
00119     struct LinePrimitive : public math::FixedColVector<int,2>, public Primitive{
00121       typedef math::FixedColVector<int,2> super; 
00122       
00124       LinePrimitive(int a, int b, const GeomColor &color):
00125       math::FixedColVector<int,2>(a,b),Primitive(Primitive::line,color){}
00126         
00128       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00129   
00131       inline int i(int idx) const { return super::operator[](idx); }
00132       
00134       virtual Primitive *copy() const { return new LinePrimitive(*this); }
00135     };
00136   
00138     struct TrianglePrimitive : public math::FixedColVector<int,6>, public Primitive{
00140       typedef  math::FixedColVector<int,6> super; 
00141       
00143       TrianglePrimitive(int a, int b, int c, const GeomColor &color, int na=-1, int nb=-1, int nc=-1):
00144       super(a,b,c,na,nb,nc),Primitive(Primitive::triangle,color){}
00145       
00147       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00148       
00150       inline int i(int idx) const { return super::operator[](idx); }
00151       
00153       virtual Primitive *copy() const { return new TrianglePrimitive(*this); }
00154 
00156 
00157       ICLGeom_API Vec computeNormal(const std::vector<Vec> &vertices) const;
00158 
00159     };
00160   
00162     struct QuadPrimitive : public math::FixedColVector<int,8>, public Primitive{
00164       typedef math::FixedColVector<int,8> super; 
00165       
00167       bool trySurfaceOptimization;
00168       
00170 
00172       int tesselationResolution;
00173   
00175       QuadPrimitive(int a, int b, int c, int d, const GeomColor &color, int na=-1, int nb=-1, int nc=-1, int nd=-1, 
00176                     bool trySurfaceOptimization=false,int tesselationResolution=1):
00177       super(a,b,c,d,na,nb,nc,nd),Primitive(Primitive::quad,color),trySurfaceOptimization(trySurfaceOptimization),
00178       tesselationResolution(tesselationResolution){}
00179       
00181       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00182   
00184       inline int i(int idx) const { return super::operator[](idx); }
00185   
00187       virtual Primitive *copy() const { return new QuadPrimitive(*this); }
00188       
00190 
00191       ICLGeom_API Vec computeNormal(const std::vector<Vec> &vertices) const;
00192     };
00193   
00195 
00196     struct PolygonPrimitive : public Primitive{
00197       
00199 
00203       utils::Array2D<int> idx;
00204       
00206       PolygonPrimitive(int n,const int *vidx, const GeomColor &color,const int *nidx=0):
00207       Primitive(Primitive::polygon,color),idx(n,nidx?2:1){
00208         std::copy(vidx,vidx+n,idx.begin());
00209         if(nidx) std::copy(nidx,nidx+n,idx.begin()+n);
00210       }
00211       
00213       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00214       
00216       virtual Primitive *copy() const { 
00217         PolygonPrimitive *p = new PolygonPrimitive(*this);
00218         p->idx.detach();
00219         return p;
00220       }
00221       
00223       inline int getNumPoints() const { return idx.getWidth(); }
00224       
00226       inline int getVertexIndex(int i) const { return idx(i,0); }
00227   
00229 
00230       inline int getNormalIndex(int i) const { return idx(i,1); }
00231       
00233       inline bool hasNormals() const { return idx.getHeight() == 2; }
00234     };
00235   
00237     struct AlphaFuncProperty{
00239       ICLGeom_API AlphaFuncProperty();
00240       AlphaFuncProperty(int alphaFunc, float alphaValue):alphaFunc(alphaFunc),alphaValue(alphaValue){}
00241     
00242       int alphaFunc;         
00243       float alphaValue;      
00244       
00246       void setAlphaFunc(int func, float value){
00247         alphaFunc = func;
00248         alphaValue = value;
00249       }
00250       
00251       ICLGeom_API void restoreAlphaDefaults();
00252     };
00253   
00254     
00256 
00265     struct TexturePrimitive : public QuadPrimitive, public AlphaFuncProperty{
00266       qt::GLImg texture;         
00267       const core::ImgBase *image;  
00268   
00270       TexturePrimitive(int a, int b, int c, int d, 
00271                        const core::ImgBase *image=0, bool createTextureOnce=true, 
00272                        int na=-1, int nb=-1, int nc=-1, int nd=-1, core::scalemode sm=core::interpolateLIN):
00273       QuadPrimitive(a,b,c,d,na,nb,nc,nd), texture(image,sm),
00274         image(createTextureOnce ? 0 : image){
00275         type = Primitive::texture;
00276       }
00277   
00279       TexturePrimitive(int a, int b, int c, int d, 
00280                        const core::Img8u &image,
00281                        int na=-1, int nb=-1, int nc=-1, int nd=-1, core::scalemode sm=core::interpolateLIN):
00282       QuadPrimitive(a,b,c,d,na,nb,nc,nd), texture(&image,sm), 
00283         image(0){
00284         type = Primitive::texture;
00285       }
00286   
00288       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00289   
00291       virtual Primitive *copy() const { 
00292         return new TexturePrimitive(i(0),i(1),i(2),i(3),
00293                                     image ? image : texture.extractImage(),
00294                                     !image,
00295                                     i(4),i(5),i(6),i(7),
00296                                     texture.getScaleMode());
00297       }
00298       
00299   
00300     };
00301   
00303 
00304     class TextureGridPrimitive : public Primitive, public AlphaFuncProperty{
00305       protected:
00306       friend class SceneObject;
00307       int w,h;
00308       qt::GLImg texture;
00309       const icl32f *px, *py, *pz, *pnx,  *pny, *pnz;
00310       int stride;
00311       const core::ImgBase *image;
00312       
00313       public:
00314       TextureGridPrimitive(int w, int h, const core::ImgBase *image,
00315                            const icl32f *px, const icl32f *py, const icl32f *pz,
00316                            const icl32f *pnx=0, const icl32f *pny=0, const icl32f *pnz=0,
00317                            int stride = 1,bool createTextureOnce=true,core::scalemode sm=core::interpolateLIN):
00318       Primitive(Primitive::texture),w(w),h(h),texture(image,sm),px(px),py(py),pz(pz),
00319       pnx(pnx),pny(pny),pnz(pnz),stride(stride),image(createTextureOnce ? 0 : image){}
00320   
00321       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00322       
00323       virtual Primitive *copy() const { 
00324         return new TextureGridPrimitive(w,h,image ? image : texture.extractImage(),
00325                                         px,py,pz,pnx,pny,pnz,stride,!image,
00326                                         texture.getScaleMode()); 
00327       }
00328       ICLGeom_API void getAABB(utils::Range32f aabb[3]);
00329       
00330       inline Vec getPos(int x, int y) const {
00331         const int idx = stride*(x + w*y);
00332         return Vec(px[idx],py[idx],pz[idx],1);
00333       }
00334     };
00335   
00336     class TwoSidedTextureGridPrimitive : public TextureGridPrimitive{
00337       qt::GLImg back;
00338       const core::ImgBase *iback;
00339       public:
00340       TwoSidedTextureGridPrimitive(int w, int h, const core::ImgBase *front, const core::ImgBase *back,
00341                                    const icl32f *px, const icl32f *py, const icl32f *pz,
00342                                    const icl32f *pnx=0, const icl32f *pny=0, const icl32f *pnz=0,
00343                                    int stride = 1,bool createFrontOnce=true,
00344                                    bool createBackOnce=true, core::scalemode sm=core::interpolateLIN):
00345       TextureGridPrimitive(w,h,front,px,py,pz,pnx,pny,pnz,stride,createFrontOnce,sm),back(back,sm),
00346       iback(createBackOnce ? 0 : back){}
00347       
00348       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00349       
00351       ICLGeom_API void setTextures(const core::ImgBase *front, const core::ImgBase *back);
00352     };
00353     
00354 
00356     class TwoSidedGridPrimitive: public Primitive{
00357       public:
00358       int w,h;
00359       const Vec *vertices, *normals;
00360       GeomColor front,back,lines;
00361       bool drawLines,drawQuads;
00362       
00363       inline int getIdx(int x, int y) const { return x+w*y; }
00364       TwoSidedGridPrimitive(int w, int h, const Vec *vertices, const Vec *normals=0, 
00365                             const GeomColor &frontColor=GeomColor(0,100,255,255), 
00366                             const GeomColor &backColor=GeomColor(255,0,100,255),
00367                             const GeomColor &lineColor=GeomColor(0,255,100,255),
00368                             bool drawLines=false, bool drawQuads=true):
00369       Primitive(Primitive::quad), w(w), h(h), vertices(vertices), normals(normals),
00370       front(frontColor*(1./255)),back(backColor*(1./255)),lines(lineColor*(1./255)),
00371       drawLines(drawLines),drawQuads(drawQuads){}
00372       
00373       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00374 
00375       virtual Primitive *copy() const{
00376         return new TwoSidedGridPrimitive(w,h,vertices, normals, front*255, back*255, 
00377                                          lines*255,drawLines,drawQuads);
00378       }
00379       inline const Vec &getPos(int x, int y) const {
00380         return vertices[x+w*y];
00381       }
00382     };
00383       
00385 
00387     struct SharedTexturePrimitive : public QuadPrimitive, public AlphaFuncProperty{
00388       int sharedTextureIndex;
00389       
00391       SharedTexturePrimitive(int a, int b, int c, int d, 
00392                        int sharedTextureIndex,
00393                        int na=-1, int nb=-1, int nc=-1, int nd=-1):
00394       QuadPrimitive(a,b,c,d,na,nb,nc,nd), sharedTextureIndex(sharedTextureIndex){
00395         type = Primitive::texture;
00396       }
00397   
00399       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00400   
00402       virtual Primitive *copy() const { 
00403         return new SharedTexturePrimitive(*this);
00404       }
00405     };
00406   
00408     struct GenericTexturePrimitive : public Primitive, public AlphaFuncProperty{
00409       utils::SmartPtr<qt::GLImg> texture;
00410       const core::ImgBase *image;
00411   
00412       std::vector<Vec> ps;
00413       std::vector<utils::Point32f> texCoords;
00414       std::vector<Vec> normals;
00415       
00417       std::vector<int> vertexIndices;
00418       std::vector<int> normalIndices;
00419   
00421       ICLGeom_API GenericTexturePrimitive(const core::ImgBase *image, int numPoints,
00422                               const float *xs, const float *ys, const float *zs, int xyzStride,
00423                               const utils::Point32f *texCoords, const float *nxs=0, const float *nys=0,
00424                               const float *nzs=0, int nxyzStride=1, bool createTextureOnce=true);
00425       
00427       ICLGeom_API GenericTexturePrimitive(const core::ImgBase *image, int numPoints, const int *vertexIndices,
00428                               const utils::Point32f *texCoords, const int *normalIndices = 0,
00429                               bool createTextureOnce=true);
00430       
00432       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);    
00433   
00435       virtual Primitive *copy() const {
00436         GenericTexturePrimitive *cpy = new GenericTexturePrimitive(*this);
00437         cpy->texture = new qt::GLImg(image ? image : texture->extractImage());
00438         return cpy;
00439       }
00440     };
00441     
00443 
00444     struct TextPrimitive : public TexturePrimitive{
00446       int textSize;
00447   
00449       GeomColor textColor;
00450       
00452       ICLGeom_API static core::Img8u create_texture(const std::string &text, const GeomColor &color, int textSize);
00453       
00455 
00457       float billboardHeight; 
00458       
00460       ICLGeom_API TextPrimitive(int a, int b, int c, int d, 
00461                     const std::string &text,
00462                     int textSize=20,
00463                     const GeomColor &textColor=GeomColor(255,255,255,255),
00464                     int na=-1, int nb=-1, int nc=-1, int nd=-1,
00465                     float billboardHeight=0,
00466                     core::scalemode sm=core::interpolateLIN);
00467       ICLGeom_API ~TextPrimitive();
00468   
00470       ICLGeom_API virtual void render(const Primitive::RenderContext &ctx);
00471   
00473       virtual Primitive *copy() const {
00474         Primitive *p = TexturePrimitive::copy();
00475         p->type = text;
00476         return p;
00477       }
00478   
00480       inline void updateText(const std::string &newText){
00481         core::Img8u t = create_texture(newText,textColor,textSize);
00482         texture.update(&t);
00483       }
00484   
00485     };
00486   
00487     
00488   } // namespace geom
00489 }
00490 
00491 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines