Point Cloud Library (PCL)  1.9.1-dev
orr_octree.h
1 /*
2  * Software License Agreement (BSD License)
3  *
4  * Point Cloud Library (PCL) - www.pointclouds.org
5  * Copyright (c) 2010-2012, Willow Garage, Inc.
6  *
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * * Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  * * Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  * * Neither the name of Willow Garage, Inc. nor the names of its
20  * contributors may be used to endorse or promote products derived
21  * from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34  * POSSIBILITY OF SUCH DAMAGE.
35  *
36  *
37  */
38 
39 /*
40  * orr_octree.h
41  *
42  * Created on: Oct 23, 2012
43  * Author: papazov
44  */
45 
46 #pragma once
47 
48 #include "auxiliary.h"
49 #include <pcl/point_types.h>
50 #include <pcl/point_cloud.h>
51 #include <pcl/pcl_exports.h>
52 #include <cstdlib>
53 #include <ctime>
54 #include <vector>
55 #include <list>
56 #include <set>
57 
58 //#define PCL_REC_ORR_OCTREE_VERBOSE
59 
60 namespace pcl
61 {
62  namespace recognition
63  {
64  /** \brief That's a very specialized and simple octree class. That's the way it is intended to
65  * be, that's why no templates and stuff like this.
66  *
67  * \author Chavdar Papazov
68  * \ingroup recognition
69  */
70  class PCL_EXPORTS ORROctree
71  {
72  public:
76 
77  class Node
78  {
79  public:
80  class Data
81  {
82  public:
83  Data (int id_x, int id_y, int id_z, int lin_id, void* user_data = nullptr)
84  : id_x_ (id_x),
85  id_y_ (id_y),
86  id_z_ (id_z),
87  lin_id_ (lin_id),
88  num_points_ (0),
89  user_data_ (user_data)
90  {
91  n_[0] = n_[1] = n_[2] = p_[0] = p_[1] = p_[2] = 0.0f;
92  }
93 
94  virtual~ Data (){}
95 
96  inline void
97  addToPoint (float x, float y, float z)
98  {
99  p_[0] += x; p_[1] += y; p_[2] += z;
100  ++num_points_;
101  }
102 
103  inline void
105  {
106  if ( num_points_ < 2 )
107  return;
108 
109  aux::mult3 (p_, 1.0f/static_cast<float> (num_points_));
110  num_points_ = 1;
111  }
112 
113  inline void
114  addToNormal (float x, float y, float z) { n_[0] += x; n_[1] += y; n_[2] += z;}
115 
116  inline const float*
117  getPoint () const { return p_;}
118 
119  inline float*
120  getPoint (){ return p_;}
121 
122  inline const float*
123  getNormal () const { return n_;}
124 
125  inline float*
126  getNormal (){ return n_;}
127 
128  inline void
129  get3dId (int id[3]) const
130  {
131  id[0] = id_x_;
132  id[1] = id_y_;
133  id[2] = id_z_;
134  }
135 
136  inline int
137  get3dIdX () const {return id_x_;}
138 
139  inline int
140  get3dIdY () const {return id_y_;}
141 
142  inline int
143  get3dIdZ () const {return id_z_;}
144 
145  inline int
146  getLinearId () const { return lin_id_;}
147 
148  inline void
149  setUserData (void* user_data){ user_data_ = user_data;}
150 
151  inline void*
152  getUserData () const { return user_data_;}
153 
154  inline void
155  insertNeighbor (Node* node){ neighbors_.insert (node);}
156 
157  inline const std::set<Node*>&
158  getNeighbors () const { return (neighbors_);}
159 
160  protected:
161  float n_[3], p_[3];
162  int id_x_, id_y_, id_z_, lin_id_, num_points_;
163  std::set<Node*> neighbors_;
164  void *user_data_;
165  };
166 
167  Node ()
168  : data_ (nullptr),
169  parent_ (nullptr),
170  children_(nullptr)
171  {}
172 
173  virtual~ Node ()
174  {
175  this->deleteChildren ();
176  this->deleteData ();
177  }
178 
179  inline void
180  setCenter(const float *c) { center_[0] = c[0]; center_[1] = c[1]; center_[2] = c[2];}
181 
182  inline void
183  setBounds(const float *b) { bounds_[0] = b[0]; bounds_[1] = b[1]; bounds_[2] = b[2]; bounds_[3] = b[3]; bounds_[4] = b[4]; bounds_[5] = b[5];}
184 
185  inline void
186  setParent(Node* parent) { parent_ = parent;}
187 
188  inline void
189  setData(Node::Data* data) { data_ = data;}
190 
191  /** \brief Computes the "radius" of the node which is half the diagonal length. */
192  inline void
194  {
195  float v[3] = {0.5f*(bounds_[1]-bounds_[0]), 0.5f*(bounds_[3]-bounds_[2]), 0.5f*(bounds_[5]-bounds_[4])};
196  radius_ = static_cast<float> (aux::length3 (v));
197  }
198 
199  inline const float*
200  getCenter() const { return center_;}
201 
202  inline const float*
203  getBounds() const { return bounds_;}
204 
205  inline void
206  getBounds(float b[6]) const
207  {
208  memcpy (b, bounds_, 6*sizeof (float));
209  }
210 
211  inline Node*
212  getChild (int id) { return &children_[id];}
213 
214  inline Node*
215  getChildren () { return children_;}
216 
217  inline Node::Data*
218  getData (){ return data_;}
219 
220  inline const Node::Data*
221  getData () const { return data_;}
222 
223  inline void
224  setUserData (void* user_data){ data_->setUserData (user_data);}
225 
226  inline Node*
227  getParent (){ return parent_;}
228 
229  inline bool
230  hasData (){ return static_cast<bool> (data_);}
231 
232  inline bool
233  hasChildren (){ return static_cast<bool> (children_);}
234 
235  /** \brief Computes the "radius" of the node which is half the diagonal length. */
236  inline float
237  getRadius (){ return radius_;}
238 
239  bool
240  createChildren ();
241 
242  inline void
244  {
245  if ( children_ )
246  {
247  delete[] children_;
248  children_ = nullptr;
249  }
250  }
251 
252  inline void
254  {
255  if ( data_ )
256  {
257  delete data_;
258  data_ = nullptr;
259  }
260  }
261 
262  /** \brief Make this and 'node' neighbors by inserting each node in the others node neighbor set. Nothing happens
263  * of either of the nodes has no data. */
264  inline void
266  {
267  if ( !this->getData () || !node->getData () )
268  return;
269 
270  this->getData ()->insertNeighbor (node);
271  node->getData ()->insertNeighbor (this);
272  }
273 
274  protected:
276  float center_[3], bounds_[6], radius_;
277  Node *parent_, *children_;
278  };
279 
280  ORROctree ();
281  virtual ~ORROctree (){ this->clear ();}
282 
283  void
284  clear ();
285 
286  /** \brief Creates an octree which encloses 'points' and with leaf size equal to 'voxel_size'.
287  * 'enlarge_bounds' makes sure that no points from the input will lie on the octree boundary
288  * by enlarging the bounds by that factor. For example, enlarge_bounds = 1 means that the
289  * bounds will be enlarged by 100%. The default value is fine. */
290  void
291  build (const PointCloudIn& points, float voxel_size, const PointCloudN* normals = nullptr, float enlarge_bounds = 0.00001f);
292 
293  /** \brief Creates an empty octree with bounds at least as large as the ones provided as input and with leaf
294  * size equal to 'voxel_size'. */
295  void
296  build (const float* bounds, float voxel_size);
297 
298  /** \brief Creates the leaf containing p = (x, y, z) and returns a pointer to it, however, only if p lies within
299  * the octree bounds! A more general version which allows p to be out of bounds is not implemented yet. The method
300  * returns NULL if p is not within the root bounds. If the leaf containing p already exists nothing happens and
301  * method just returns a pointer to the leaf. */
302  inline ORROctree::Node*
303  createLeaf (float x, float y, float z)
304  {
305  // Make sure that the input point is within the octree bounds
306  if ( x < bounds_[0] || x > bounds_[1] ||
307  y < bounds_[2] || y > bounds_[3] ||
308  z < bounds_[4] || z > bounds_[5] )
309  {
310  return (nullptr);
311  }
312 
313  ORROctree::Node* node = root_;
314  const float *c;
315  int id;
316 
317  // Go down to the right leaf
318  for ( int l = 0 ; l < tree_levels_ ; ++l )
319  {
320  node->createChildren ();
321  c = node->getCenter ();
322  id = 0;
323 
324  if ( x >= c[0] ) id |= 4;
325  if ( y >= c[1] ) id |= 2;
326  if ( z >= c[2] ) id |= 1;
327 
328  node = node->getChild (id);
329  }
330 
331  if ( !node->getData () )
332  {
333  Node::Data* data = new Node::Data (
334  static_cast<int> ((node->getCenter ()[0] - bounds_[0])/voxel_size_),
335  static_cast<int> ((node->getCenter ()[1] - bounds_[2])/voxel_size_),
336  static_cast<int> ((node->getCenter ()[2] - bounds_[4])/voxel_size_),
337  static_cast<int> (full_leaves_.size ()));
338 
339  node->setData (data);
340  this->insertNeighbors (node);
341  full_leaves_.push_back (node);
342  }
343 
344  return (node);
345  }
346 
347  /** \brief This method returns a super set of the full leavess which are intersected by the sphere
348  * with radius 'radius' and centered at 'p'. Pointers to the intersected full leaves are saved in
349  * 'out'. The method computes a super set in the sense that in general not all leaves saved in 'out'
350  * are really intersected by the sphere. The intersection test is based on the leaf radius (since
351  * its faster than checking all leaf corners and sides), so we report more leaves than we should,
352  * but still, this is a fair approximation. */
353  void
354  getFullLeavesIntersectedBySphere (const float* p, float radius, std::list<ORROctree::Node*>& out) const;
355 
356  /** \brief Randomly chooses and returns a full leaf that is intersected by the sphere with center 'p'
357  * and 'radius'. Returns NULL if no leaf is intersected by that sphere. */
359  getRandomFullLeafOnSphere (const float* p, float radius) const;
360 
361  /** \brief Since the leaves are aligned in a rectilinear grid, each leaf has a unique id. The method returns the leaf
362  * with id [i, j, k] or NULL is no such leaf exists. */
364  getLeaf (int i, int j, int k)
365  {
366  float offset = 0.5f*voxel_size_;
367  float p[3] = {bounds_[0] + offset + static_cast<float> (i)*voxel_size_,
368  bounds_[2] + offset + static_cast<float> (j)*voxel_size_,
369  bounds_[4] + offset + static_cast<float> (k)*voxel_size_};
370 
371  return (this->getLeaf (p[0], p[1], p[2]));
372  }
373 
374  /** \brief Returns a pointer to the leaf containing p = (x, y, z) or NULL if no such leaf exists. */
375  inline ORROctree::Node*
376  getLeaf (float x, float y, float z)
377  {
378  // Make sure that the input point is within the octree bounds
379  if ( x < bounds_[0] || x > bounds_[1] ||
380  y < bounds_[2] || y > bounds_[3] ||
381  z < bounds_[4] || z > bounds_[5] )
382  {
383  return (nullptr);
384  }
385 
386  ORROctree::Node* node = root_;
387  const float *c;
388  int id;
389 
390  // Go down to the right leaf
391  for ( int l = 0 ; l < tree_levels_ ; ++l )
392  {
393  if ( !node->hasChildren () )
394  return (nullptr);
395 
396  c = node->getCenter ();
397  id = 0;
398 
399  if ( x >= c[0] ) id |= 4;
400  if ( y >= c[1] ) id |= 2;
401  if ( z >= c[2] ) id |= 1;
402 
403  node = node->getChild (id);
404  }
405 
406  return (node);
407  }
408 
409  /** \brief Deletes the branch 'node' is part of. */
410  void
411  deleteBranch (Node* node);
412 
413  /** \brief Returns a vector with all octree leaves which contain at least one point. */
414  inline std::vector<ORROctree::Node*>&
415  getFullLeaves () { return full_leaves_;}
416 
417  inline const std::vector<ORROctree::Node*>&
418  getFullLeaves () const { return full_leaves_;}
419 
420  void
421  getFullLeavesPoints (PointCloudOut& out) const;
422 
423  void
424  getNormalsOfFullLeaves (PointCloudN& out) const;
425 
426  inline ORROctree::Node*
427  getRoot (){ return root_;}
428 
429  inline const float*
430  getBounds () const
431  {
432  return (bounds_);
433  }
434 
435  inline void
436  getBounds (float b[6]) const
437  {
438  memcpy (b, bounds_, 6*sizeof (float));
439  }
440 
441  inline float
442  getVoxelSize () const { return voxel_size_;}
443 
444  inline void
446  {
447  const float* c = node->getCenter ();
448  float s = 0.5f*voxel_size_;
449  Node *neigh;
450 
451  neigh = this->getLeaf (c[0]+s, c[1]+s, c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
452  neigh = this->getLeaf (c[0]+s, c[1]+s, c[2] ); if ( neigh ) node->makeNeighbors (neigh);
453  neigh = this->getLeaf (c[0]+s, c[1]+s, c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
454  neigh = this->getLeaf (c[0]+s, c[1] , c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
455  neigh = this->getLeaf (c[0]+s, c[1] , c[2] ); if ( neigh ) node->makeNeighbors (neigh);
456  neigh = this->getLeaf (c[0]+s, c[1] , c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
457  neigh = this->getLeaf (c[0]+s, c[1]-s, c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
458  neigh = this->getLeaf (c[0]+s, c[1]-s, c[2] ); if ( neigh ) node->makeNeighbors (neigh);
459  neigh = this->getLeaf (c[0]+s, c[1]-s, c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
460 
461  neigh = this->getLeaf (c[0] , c[1]+s, c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
462  neigh = this->getLeaf (c[0] , c[1]+s, c[2] ); if ( neigh ) node->makeNeighbors (neigh);
463  neigh = this->getLeaf (c[0] , c[1]+s, c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
464  neigh = this->getLeaf (c[0] , c[1] , c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
465  //neigh = this->getLeaf (c[0] , c[1] , c[2] ); if ( neigh ) node->makeNeighbors (neigh);
466  neigh = this->getLeaf (c[0] , c[1] , c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
467  neigh = this->getLeaf (c[0] , c[1]-s, c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
468  neigh = this->getLeaf (c[0] , c[1]-s, c[2] ); if ( neigh ) node->makeNeighbors (neigh);
469  neigh = this->getLeaf (c[0] , c[1]-s, c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
470 
471  neigh = this->getLeaf (c[0]-s, c[1]+s, c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
472  neigh = this->getLeaf (c[0]-s, c[1]+s, c[2] ); if ( neigh ) node->makeNeighbors (neigh);
473  neigh = this->getLeaf (c[0]-s, c[1]+s, c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
474  neigh = this->getLeaf (c[0]-s, c[1] , c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
475  neigh = this->getLeaf (c[0]-s, c[1] , c[2] ); if ( neigh ) node->makeNeighbors (neigh);
476  neigh = this->getLeaf (c[0]-s, c[1] , c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
477  neigh = this->getLeaf (c[0]-s, c[1]-s, c[2]+s); if ( neigh ) node->makeNeighbors (neigh);
478  neigh = this->getLeaf (c[0]-s, c[1]-s, c[2] ); if ( neigh ) node->makeNeighbors (neigh);
479  neigh = this->getLeaf (c[0]-s, c[1]-s, c[2]-s); if ( neigh ) node->makeNeighbors (neigh);
480  }
481 
482  protected:
483  float voxel_size_, bounds_[6];
486  std::vector<Node*> full_leaves_;
487  };
488  } // namespace recognition
489 } // namespace pcl
const float * getNormal() const
Definition: orr_octree.h:123
float getRadius()
Computes the "radius" of the node which is half the diagonal length.
Definition: orr_octree.h:237
const Node::Data * getData() const
Definition: orr_octree.h:221
void setUserData(void *user_data)
Definition: orr_octree.h:224
void mult3(T *v, T scalar)
v = scalar*v.
Definition: auxiliary.h:222
This file defines compatibility wrappers for low level I/O functions.
Definition: convolution.h:45
Data(int id_x, int id_y, int id_z, int lin_id, void *user_data=nullptr)
Definition: orr_octree.h:83
void computeRadius()
Computes the "radius" of the node which is half the diagonal length.
Definition: orr_octree.h:193
ORROctree::Node * createLeaf(float x, float y, float z)
Creates the leaf containing p = (x, y, z) and returns a pointer to it, however, only if p lies within...
Definition: orr_octree.h:303
void getBounds(float b[6]) const
Definition: orr_octree.h:206
const std::set< Node * > & getNeighbors() const
Definition: orr_octree.h:158
const float * getCenter() const
Definition: orr_octree.h:200
float getVoxelSize() const
Definition: orr_octree.h:442
std::vector< ORROctree::Node * > & getFullLeaves()
Returns a vector with all octree leaves which contain at least one point.
Definition: orr_octree.h:415
void setCenter(const float *c)
Definition: orr_octree.h:180
const float * getBounds() const
Definition: orr_octree.h:430
const float * getBounds() const
Definition: orr_octree.h:203
ORROctree::Node * getRoot()
Definition: orr_octree.h:427
void setData(Node::Data *data)
Definition: orr_octree.h:189
void insertNeighbors(Node *node)
Definition: orr_octree.h:445
std::vector< Node * > full_leaves_
Definition: orr_octree.h:486
void setBounds(const float *b)
Definition: orr_octree.h:183
ORROctree::Node * getLeaf(int i, int j, int k)
Since the leaves are aligned in a rectilinear grid, each leaf has a unique id.
Definition: orr_octree.h:364
T length3(const T v[3])
Returns the length of v.
Definition: auxiliary.h:186
ORROctree::Node * getLeaf(float x, float y, float z)
Returns a pointer to the leaf containing p = (x, y, z) or NULL if no such leaf exists.
Definition: orr_octree.h:376
void getBounds(float b[6]) const
Definition: orr_octree.h:436
void makeNeighbors(Node *node)
Make this and &#39;node&#39; neighbors by inserting each node in the others node neighbor set...
Definition: orr_octree.h:265
const std::vector< ORROctree::Node * > & getFullLeaves() const
Definition: orr_octree.h:418
void addToNormal(float x, float y, float z)
Definition: orr_octree.h:114
void setParent(Node *parent)
Definition: orr_octree.h:186
That&#39;s a very specialized and simple octree class.
Definition: orr_octree.h:70
void addToPoint(float x, float y, float z)
Definition: orr_octree.h:97