Point Cloud Library (PCL)  1.7.1
octree_key.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 #ifndef PCL_OCTREE_KEY_H
39 #define PCL_OCTREE_KEY_H
40 
41 #include <string>
42 
43 namespace pcl
44 {
45  namespace octree
46  {
47  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
48  /** \brief @b Octree key class
49  * \note Octree keys contain integer indices for each coordinate axis in order to address an octree leaf node.
50  * \author Julius Kammerl (julius@kammerl.de)
51  */
52  //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
53  class OctreeKey
54  {
55  public:
56 
57  /** \brief Empty constructor. */
58  OctreeKey () :
59  x (0), y (0), z (0)
60  {
61  }
62 
63  /** \brief Constructor for key initialization. */
64  OctreeKey (unsigned int keyX, unsigned int keyY, unsigned int keyZ) :
65  x (keyX), y (keyY), z (keyZ)
66  {
67  }
68 
69  /** \brief Copy constructor. */
70  OctreeKey (const OctreeKey& source)
71  {
72  memcpy(key_, source.key_, sizeof(key_));
73  }
74 
75  /** \brief Operator== for comparing octree keys with each other.
76  * \return "true" if leaf node indices are identical; "false" otherwise.
77  * */
78  bool
79  operator == (const OctreeKey& b) const
80  {
81  return ((b.x == this->x) && (b.y == this->y) && (b.z == this->z));
82  }
83 
84  /** \brief Operator<= for comparing octree keys with each other.
85  * \return "true" if key indices are not greater than the key indices of b ; "false" otherwise.
86  * */
87  bool
88  operator <= (const OctreeKey& b) const
89  {
90  return ((b.x >= this->x) && (b.y >= this->y) && (b.z >= this->z));
91  }
92 
93  /** \brief Operator>= for comparing octree keys with each other.
94  * \return "true" if key indices are not smaller than the key indices of b ; "false" otherwise.
95  * */
96  bool
97  operator >= (const OctreeKey& b) const
98  {
99  return ((b.x <= this->x) && (b.y <= this->y) && (b.z <= this->z));
100  }
101 
102  /** \brief push a child node to the octree key
103  * \param[in] childIndex index of child node to be added (0-7)
104  * */
105  inline void
106  pushBranch (unsigned char childIndex)
107  {
108  this->x = (this->x << 1) | (!!(childIndex & (1 << 2)));
109  this->y = (this->y << 1) | (!!(childIndex & (1 << 1)));
110  this->z = (this->z << 1) | (!!(childIndex & (1 << 0)));
111  }
112 
113  /** \brief pop child node from octree key
114  * */
115  inline void
117  {
118  this->x >>= 1;
119  this->y >>= 1;
120  this->z >>= 1;
121  }
122 
123  /** \brief get child node index using depthMask
124  * \param[in] depthMask bit mask with single bit set at query depth
125  * \return child node index
126  * */
127  inline unsigned char
128  getChildIdxWithDepthMask (unsigned int depthMask) const
129  {
130  return static_cast<unsigned char> (((!!(this->x & depthMask)) << 2)
131  | ((!!(this->y & depthMask)) << 1)
132  | (!!(this->z & depthMask)));
133  }
134 
135  /* \brief maximum depth that can be addressed */
136  static const unsigned char maxDepth = static_cast<const unsigned char>(sizeof(uint32_t)*8);
137 
138  // Indices addressing a voxel at (X, Y, Z)
139 
140  union
141  {
142  struct
143  {
144  uint32_t x;
145  uint32_t y;
146  uint32_t z;
147  };
148  uint32_t key_[3];
149  };
150 
151 
152  };
153  }
154 }
155 
156 #endif