Image Component Library (ICL)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
OctreeObject.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/OctreeObject.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.GPL  **
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 #include <ICLMath/Octree.h>
00034 #include <ICLGeom/SceneObject.h>
00035 
00036 
00037 #ifdef ICL_SYSTEM_APPLE
00038 #include <OpenGL/gl.h>
00039 #include <OpenGL/glu.h>
00040 #else
00041 #include <GL/gl.h>
00042 #include <GL/glu.h>
00043 #endif 
00044 
00045 namespace icl{
00046   namespace geom{
00047     
00049     void octree_object_render_box(float x0, float y0, float z0, 
00050                                   float x1, float y1, float z1);
00051     
00052     template<class Scalar, int CAPACITY, int SF, class Pt, int ALLOC_CHUNK_SIZE>
00053     struct OctreePointRenderer{
00054       typedef typename math::Octree<Scalar,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>::Node Node;
00055       static void render(const Node *node){
00056         glBegin(GL_POINTS);
00057         for(const Pt *p = node->points; p < node->next;++p){
00058           glVertex3f( (*p)[0],(*p)[1],(*p)[2]);
00059         }
00060         glEnd();
00061       }
00062     };
00063     
00064 #if 0
00065 
00066     template<int CAPACITY, int SF, int ALLOC_CHUNK_SIZE>
00067     struct OctreePointRenderer<float,CAPACITY,SF,math::FixedColVector<float,4>,ALLOC_CHUNK_SIZE>{
00068       typedef FixedColVector<float,4> Pt;
00069       typedef typename math::Octree<float,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>::Node Node;
00070       static void render(const Node *node){
00071         glEnableClientState(GL_VERTEX_ARRAY);
00072         glVertexPointer(4,GL_FLOAT,0,node->points);
00073         glDrawArrays(GL_POINTS, 0, (int)(node->next - node->points));
00074         glDisableClientState(GL_VERTEX_ARRAY);
00075       }
00076     };  
00077 
00078     template<int CAPACITY, int SF, int ALLOC_CHUNK_SIZE>
00079     struct OctreePointRenderer<icl32s,CAPACITY,SF,math::FixedColVector<icl32s,4>,ALLOC_CHUNK_SIZE>{
00080       typedef FixedColVector<icl32s,4> Pt;
00081       typedef typename math::Octree<icl32s,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>::Node Node;
00082       static void render(const Node *node){
00083         glEnableClientState(GL_VERTEX_ARRAY);
00084         glVertexPointer(4,GL_INT,0,node->points);
00085         glDrawArrays(GL_POINTS, 0, (int)(node->next - node->points));
00086         glDisableClientState(GL_VERTEX_ARRAY);
00087       }
00088     };
00089 #endif
00090     
00095 
00096     template<class Scalar, int CAPACITY=4, int SF=32, class Pt=math::FixedColVector<Scalar,4>, int ALLOC_CHUNK_SIZE=1024>
00097     class OctreeObject : public math::Octree<Scalar,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>, public SceneObject{
00098 
00100       typedef math::Octree<Scalar,CAPACITY,SF,Pt, ALLOC_CHUNK_SIZE> Parent;
00101       bool m_renderPoints;     
00102       bool m_renderBoxes;      
00103       GeomColor m_pointColor;  
00104       GeomColor m_boxColor;    
00105 
00106       protected:
00107       void init(){
00108         m_renderPoints = false;
00109         m_renderBoxes = true;
00110         m_pointColor = GeomColor(0,0.5,1,1);
00111         m_boxColor = GeomColor(0,1,0,0.3);
00112         setLockingEnabled(true);
00113       }
00114       
00115       public:
00116       
00118       OctreeObject(const Scalar &minX, const Scalar &minY, const Scalar &minZ,
00119                    const Scalar &width, const Scalar &height, const Scalar &depth):
00120       Parent(minX,minY,minZ,width,height,depth){ 
00121         init();
00122       }
00123       
00125       OctreeObject(const Scalar &min, const Scalar &len):Parent(min,len){
00126         init();
00127       }
00128 
00130 
00133       void setRenderPoints(bool enabled) {
00134         m_renderPoints = enabled; 
00135       }
00136       
00138       bool getRenderPoints() const { 
00139         return m_renderPoints; 
00140       }
00141 
00143       void setRenderBoxes(bool enabled) {
00144         m_renderBoxes= enabled; 
00145       }
00146       
00148       bool getRenderBoxes() const { 
00149         return m_renderBoxes; 
00150       }
00151       
00153       void setBoxColor(const GeomColor &color){
00154         m_boxColor = color/255;
00155       }
00156       
00158       GeomColor getBoxColor() const{
00159         return m_boxColor*255;
00160       }
00161 
00163 
00164       void setPointColor(const GeomColor &color){
00165         m_pointColor = color/255;
00166       }
00167       
00169       GeomColor getPointColor() const{
00170         return m_pointColor*255;
00171       }
00172 
00174       virtual void customRender(){
00175         if(!m_renderPoints && !m_renderBoxes) return;
00176         glMatrixMode(GL_MODELVIEW);
00177         glPushMatrix();
00178         
00179         glScalef(1./SF,1./SF,1./SF);
00180         
00181         GLboolean lightWasOn = true;
00182         glGetBooleanv(GL_LIGHTING,&lightWasOn);
00183         glDisable(GL_LIGHTING);
00184         
00185         glPointSize(m_pointSize);
00186         if(m_renderPoints){
00187           renderNodeWithPoints(Parent::root);
00188         }else{
00189           glColor4fv(m_boxColor.data());
00190           renderNode(Parent::root);
00191         }
00192         if(lightWasOn){
00193           glEnable(GL_LIGHTING);
00194         }
00195 
00196         glPopMatrix();
00197       }
00198 
00199       protected:
00200       
00202       void box(const typename Parent::AABB &bb) const {
00203         const Pt &c = bb.center, s = bb.halfSize;
00204         octree_object_render_box(c[0] - s[0],c[1] - s[1],c[2] - s[2],
00205                                  c[0] + s[0],c[1] + s[1],c[2] + s[2]);
00206 
00207       }
00208 
00210       void renderNodeWithPoints(const typename Parent::Node *node) const  {
00211         if(m_renderBoxes){
00212           glColor4fv(m_boxColor.data());
00213           box(node->boundary);
00214         }
00215 
00216         glColor4fv(m_pointColor.data());
00217         OctreePointRenderer<Scalar,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>::render(node);
00218         
00219         if(node->children){
00220           for(int i=0;i<8;++i){
00221             renderNodeWithPoints(node->children+i);
00222           }
00223         }
00224       }
00225       
00227       void renderNode(const typename Parent::Node *node) const{
00228         box(node->boundary);
00229         if(node->children){
00230           for(int i=0;i<8;++i){
00231             renderNode(node->children+i);
00232           }
00233         }
00234       }
00235     };
00236 
00237   }
00238 
00239 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines