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