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