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