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