Image Component Library (ICL)
OctreeObject.h
Go to the documentation of this file.
1 /********************************************************************
2 ** Image Component Library (ICL) **
3 ** **
4 ** Copyright (C) 2006-2013 CITEC, University of Bielefeld **
5 ** Neuroinformatics Group **
6 ** Website: www.iclcv.org and **
7 ** http://opensource.cit-ec.de/projects/icl **
8 ** **
9 ** File : ICLGeom/src/ICLGeom/OctreeObject.h **
10 ** Module : ICLGeom **
11 ** Authors: Christof Elbrechter **
12 ** **
13 ** **
14 ** GNU LESSER GENERAL PUBLIC LICENSE **
15 ** This file may be used under the terms of the GNU Lesser General **
16 ** Public License version 3.0 as published by the **
17 ** **
18 ** Free Software Foundation and appearing in the file LICENSE.LGPL **
19 ** included in the packaging of this file. Please review the **
20 ** following information to ensure the license requirements will **
21 ** be met: http://www.gnu.org/licenses/lgpl-3.0.txt **
22 ** **
23 ** The development of this software was supported by the **
24 ** Excellence Cluster EXC 277 Cognitive Interaction Technology. **
25 ** The Excellence Cluster EXC 277 is a grant of the Deutsche **
26 ** Forschungsgemeinschaft (DFG) in the context of the German **
27 ** Excellence Initiative. **
28 ** **
29 ********************************************************************/
30 
31 #pragma once
32 
33 #include <ICLUtils/CompatMacros.h>
34 #include <ICLMath/Octree.h>
35 #include <ICLGeom/SceneObject.h>
37 #include <ICLGeom/Camera.h>
38 
39 
40 #ifdef ICL_SYSTEM_APPLE
41 #include <OpenGL/gl.h>
42 #include <OpenGL/glu.h>
43 #elif WIN32
44 #define NOMINMAX
45 #include <Windows.h>
46 #include <GL/gl.h>
47 #include <GL/glu.h>
48 #else
49 #include <GL/gl.h>
50 #include <GL/glu.h>
51 #endif
52 
53 namespace icl{
54  namespace geom{
55 
57  ICLGeom_API void octree_object_render_box(float x0, float y0, float z0,
58  float x1, float y1, float z1);
59 
60  template<class Scalar, int CAPACITY, int SF, class Pt, int ALLOC_CHUNK_SIZE>
61  struct OctreePointRenderer{
62  typedef typename math::Octree<Scalar,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>::Node Node;
63  static void render(const Node *node){
64  glBegin(GL_POINTS);
65  for(const Pt *p = node->points; p < node->next;++p){
66  glVertex3f( (*p)[0],(*p)[1],(*p)[2]);
67  }
68  glEnd();
69  }
70  };
71 
72 #if 0
73  template<int CAPACITY, int SF, int ALLOC_CHUNK_SIZE>
75  struct OctreePointRenderer<float,CAPACITY,SF,math::FixedColVector<float,4>,ALLOC_CHUNK_SIZE>{
76  typedef FixedColVector<float,4> Pt;
77  typedef typename math::Octree<float,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>::Node Node;
78  static void render(const Node *node){
79  glEnableClientState(GL_VERTEX_ARRAY);
80  glVertexPointer(4,GL_FLOAT,0,node->points);
81  glDrawArrays(GL_POINTS, 0, (int)(node->next - node->points));
82  glDisableClientState(GL_VERTEX_ARRAY);
83  }
84  };
85 
86  template<int CAPACITY, int SF, int ALLOC_CHUNK_SIZE>
87  struct OctreePointRenderer<icl32s,CAPACITY,SF,math::FixedColVector<icl32s,4>,ALLOC_CHUNK_SIZE>{
88  typedef FixedColVector<icl32s,4> Pt;
89  typedef typename math::Octree<icl32s,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>::Node Node;
90  static void render(const Node *node){
91  glEnableClientState(GL_VERTEX_ARRAY);
92  glVertexPointer(4,GL_INT,0,node->points);
93  glDrawArrays(GL_POINTS, 0, (int)(node->next - node->points));
94  glDisableClientState(GL_VERTEX_ARRAY);
95  }
96  };
97 #endif
98 
103 
128  template<class Scalar, int CAPACITY=4, int SF=32, class Pt=math::FixedColVector<Scalar,4>, int ALLOC_CHUNK_SIZE=1024>
129  class OctreeObject : public math::Octree<Scalar,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>, public SceneObject{
130 
137 
138  protected:
139  void init(){
140  m_renderPoints = false;
141  m_renderBoxes = true;
142  m_pointColor = GeomColor(0,0.5,1,1);
143  m_boxColor = GeomColor(0,1,0,0.3);
144  setLockingEnabled(true);
145  }
146 
147  public:
148 
150  OctreeObject(const Scalar &minX, const Scalar &minY, const Scalar &minZ,
151  const Scalar &width, const Scalar &height, const Scalar &depth):
152  Parent(minX,minY,minZ,width,height,depth){
153  init();
155  }
156 
158  OctreeObject(const Scalar &min, const Scalar &len):Parent(min,len){
159  init();
160  }
161 
163 
165  void fill(const PointCloudObjectBase &obj, bool clearBefore=true){
167  if(clearBefore) Parent::clear();
169  const core::DataSegment<float,4> xyzh = obj.selectXYZH();
170  for(int i=0;i<xyzh.getDim();i+=1){
171  Parent::insert(xyzh[i]);
172  }
173  }else if(obj.supports(PointCloudObjectBase::XYZ)){
174  const core::DataSegment<float,3> xyz = obj.selectXYZ();
175  for(int i=0;i<xyz.getDim();i+=1){
176  Parent::insert(xyz[i].resize<1,4>(1));
177  }
178  }
180  }
181 
183 
191  template<class Filter>
192  void fill(const PointCloudObjectBase &obj, Filter f, bool clearBefore){
194  if(clearBefore) Parent::clear();
196  const core::DataSegment<float,4> xyzh = obj.selectXYZH();
197  for(int i=0;i<xyzh.getDim();i+=1){
198  if(f(xyzh[i])){
199  Parent::insert(xyzh[i]);
200  }
201  }
202  }else if(obj.supports(PointCloudObjectBase::XYZ)){
203  const core::DataSegment<float,3> xyz = obj.selectXYZ();
204  for(int i=0;i<xyz.getDim();i+=1){
205  if(f(xyz[i])){
206  Parent::insert(xyz[i].resize<1,4>(1));
207  }
208  }
209  }
211  }
212 
213 
215 
223  struct PointFilter {
225  float sqrRadius;
226 
228  PointFilter(const Vec &p, float radius=10){
229  pos = p.resize<1,3>();
230  sqrRadius = radius*radius;
231  }
232 
234 
237  pos = cam.getPosition().resize<1,3>();
238  this->sqrRadius = utils::sqr(cam.getFocalLength());
239  }
240 
242 
243  template<int N>
244  inline bool operator()(const math::FixedColVector<float,N> &v) const{
245  return ( ( sqr(v[0]-pos[0]) +
246  sqr(v[1]-pos[1]) +
247  sqr(v[2]-pos[2]) ) > sqrRadius);
248  }
249  };
250 
251 
253 
256  void setRenderPoints(bool enabled) {
257  m_renderPoints = enabled;
258  }
259 
261  bool getRenderPoints() const {
262  return m_renderPoints;
263  }
264 
266  void setRenderBoxes(bool enabled) {
267  m_renderBoxes= enabled;
268  }
269 
271  bool getRenderBoxes() const {
272  return m_renderBoxes;
273  }
274 
277  m_boxColor = color/255;
278  }
279 
282  return m_boxColor*255;
283  }
284 
286 
288  m_pointColor = color/255;
289  }
290 
293  return m_pointColor*255;
294  }
295 
297  virtual void customRender(){
298  if(!m_renderPoints && !m_renderBoxes) return;
299  glMatrixMode(GL_MODELVIEW);
300  glPushMatrix();
301 
302  glScalef(1./SF,1./SF,1./SF);
303 
304  GLboolean lightWasOn = true;
305  glGetBooleanv(GL_LIGHTING,&lightWasOn);
306  glDisable(GL_LIGHTING);
307 
308  glPointSize(m_pointSize);
309  if(m_renderPoints){
311  }else{
312  glColor4fv(m_boxColor.data());
314  }
315  if(lightWasOn){
316  glEnable(GL_LIGHTING);
317  }
318 
319  glPopMatrix();
320  }
321 
322  protected:
323 
325  void box(const typename Parent::AABB &bb) const {
326  const Pt &c = bb.center, s = bb.halfSize;
327  octree_object_render_box(c[0] - s[0],c[1] - s[1],c[2] - s[2],
328  c[0] + s[0],c[1] + s[1],c[2] + s[2]);
329 
330  }
331 
333  void renderNodeWithPoints(const typename Parent::Node *node) const {
334  if(m_renderBoxes){
335  glColor4fv(m_boxColor.data());
336  box(node->boundary);
337  }
338 
339  glColor4fv(m_pointColor.data());
340  OctreePointRenderer<Scalar,CAPACITY,SF,Pt,ALLOC_CHUNK_SIZE>::render(node);
341 
342  if(node->children){
343  for(int i=0;i<8;++i){
344  renderNodeWithPoints(node->children+i);
345  }
346  }
347  }
348 
350  void renderNode(const typename Parent::Node *node) const{
351  box(node->boundary);
352  if(node->children){
353  for(int i=0;i<8;++i){
354  renderNode(node->children+i);
355  }
356  }
357  }
358  };
359 
360  }
361 
362 }
void setBoxColor(const GeomColor &color)
sets the color used for boxes (default is semi-transparent green)
Definition: OctreeObject.h:276
void fill(const PointCloudObjectBase &obj, bool clearBefore=true)
Adds all points from the given point cloud object to the octree.
Definition: OctreeObject.h:165
GeomColor m_pointColor
color used for the points (if rendered)
Definition: OctreeObject.h:135
undocument this line if you encounter any issues!
Definition: Any.h:37
const Vec & getPosition() const
Definition: Camera.h:439
void clear()
removes all contained points and nodes
Definition: Octree.h:471
Base class for point cloud data types.
Definition: PointCloudObjectBase.h:98
void insert(const OtherVectorType &pIn)
inserts a node into the QuadTree
Definition: Octree.h:410
#define ICLGeom_API
Definition: CompatMacros.h:179
OctreeObject(const Scalar &min, const Scalar &len)
create OctreeObject from given cubic axis-aligned bounding box
Definition: OctreeObject.h:158
The OctreeObjects provides a visualizable SceneObject interface for the Octree class.
Definition: OctreeObject.h:129
bool m_renderPoints
flag whether points are rendered as well
Definition: OctreeObject.h:133
void fill(const PointCloudObjectBase &obj, Filter f, bool clearBefore)
Adds all points from the given point cloud object to the octree.
Definition: OctreeObject.h:192
math::Octree< Scalar, CAPACITY, SF, Pt, ALLOC_CHUNK_SIZE > Parent
typedef to the parent class type
Definition: OctreeObject.h:132
void setRenderPoints(bool enabled)
sets whether points are rendered as well
Definition: OctreeObject.h:256
float m_pointSize
Definition: SceneObject.h:829
void setPointColor(const GeomColor &color)
sets the color used for rendering ppoints (if point rendering is activated)
Definition: OctreeObject.h:287
PointFilter(const Vec &p, float radius=10)
Create point filter with given center and radius.
Definition: OctreeObject.h:228
FixedMatrix< T, NEW_WIDTH, NEW_HEIGHT > resize(const T &init=T(0)) const
extends/shrinks matrix dimensions while preserving content on remaining elements (without scaling)
Definition: FixedMatrix.h:811
GeomColor getBoxColor() const
returns the box color
Definition: OctreeObject.h:281
[float x,y,z, padding]
Definition: PointCloudObjectBase.h:161
Generic Octree Implementation.
Definition: Octree.h:60
T * data()
return internal data pointer
Definition: FixedMatrix.h:453
Camera class.
Definition: Camera.h:132
Internal most common fill-Filter to filter out single points.
Definition: OctreeObject.h:223
virtual core::DataSegment< float, 3 > selectXYZ()
well know features XYZ (three floats, this feature must usually be available)
Definition: PointCloudObjectBase.h:230
void setRenderBoxes(bool enabled)
sets whether aabbs are to be rendered (default: true)
Definition: OctreeObject.h:266
Ipp32s icl32s
32bit signed integer type for the ICL
Definition: BasicTypes.h:58
OctreeObject(const Scalar &minX, const Scalar &minY, const Scalar &minZ, const Scalar &width, const Scalar &height, const Scalar &depth)
create OctreeObject from given axis-aligned bounding box
Definition: OctreeObject.h:150
bool operator()(const math::FixedColVector< float, N > &v) const
actual filter function
Definition: OctreeObject.h:244
math::Vec3 pos
position to filter out
Definition: OctreeObject.h:224
void renderNode(const typename Parent::Node *node) const
recursive render function rendering a node's AABB only
Definition: OctreeObject.h:350
virtual core::DataSegment< float, 4 > selectXYZH()
common way to store XYZ-data (4th float define homogeneous part)
Definition: PointCloudObjectBase.h:233
float getFocalLength() const
Definition: Camera.h:443
virtual void lock() const
locks the internal mutex if locking enabled is set to true
Definition: SceneObject.h:698
static T sqr(const T &x)
square template (faster than pow(x,2)
Definition: Macros.h:212
bool getRenderBoxes() const
return whether aabbs are rendered
Definition: OctreeObject.h:271
float sqrRadius
squared distance threshold to the filter point
Definition: OctreeObject.h:225
depth
determines the pixel type of an image (8Bit-int or 32Bit-float)
Definition: Types.h:60
virtual void customRender()
adapted customRenderMethod
Definition: OctreeObject.h:297
bool getRenderPoints() const
return whether points are rendered as well
Definition: OctreeObject.h:261
void renderNodeWithPoints(const typename Parent::Node *node) const
recursive render function rendering a node's AABB and its points
Definition: OctreeObject.h:333
core::Color4D32f GeomColor
color for geometry primitives
Definition: GeomDefs.h:45
ICLQt_API void color(float r, float g=-1, float b=-1, float alpha=255)
sets the current color to given r,g,b,alpha value
bool m_renderBoxes
flag whether aabb boxes are rendered
Definition: OctreeObject.h:134
void box(const typename Parent::AABB &bb) const
utility function to render AABB-boxes
Definition: OctreeObject.h:325
virtual void unlock() const
unlocks the internal mutex if locking enabled is set to true
Definition: SceneObject.h:708
void setLockingEnabled(bool enabled)
sets locking enabled or disabled
Definition: SceneObject.h:682
GeomColor m_boxColor
color used for the aabb-boxes
Definition: OctreeObject.h:136
GeomColor getPointColor() const
returns the point rendering color
Definition: OctreeObject.h:292
PointFilter(const geom::Camera &cam)
Create point filter with given camera.
Definition: OctreeObject.h:236
The SceneObject class defines visible objects in scenes or scene graph nodes.
Definition: SceneObject.h:140
Node * root
root node pointer
Definition: Octree.h:238
[float x,y,z, homogenous part]
Definition: PointCloudObjectBase.h:162
void init()
Definition: OctreeObject.h:139
int getDim() const
returns the number of elements
Definition: DataSegmentBase.h:153
virtual bool supports(FeatureType t) const =0
interface for supported features