OcTreeKey.h
Go to the documentation of this file.
1 /*
2  * OctoMap - An Efficient Probabilistic 3D Mapping Framework Based on Octrees
3  * http://octomap.github.com/
4  *
5  * Copyright (c) 2009-2013, K.M. Wurm and A. Hornung, University of Freiburg
6  * All rights reserved.
7  * License: New BSD
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  * * Neither the name of the University of Freiburg nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef OCTOMAP_OCTREE_KEY_H
35 #define OCTOMAP_OCTREE_KEY_H
36 
37 /* According to c++ standard including this header has no practical effect
38  * but it can be used to determine the c++ standard library implementation.
39  */
40 #include <ciso646>
41 
42 #include <assert.h>
43 #include <inttypes.h>
44 
45 /* Libc++ does not implement the TR1 namespace, all c++11 related functionality
46  * is instead implemented in the std namespace.
47  */
48 #if defined(__GNUC__) && ! defined(_LIBCPP_VERSION)
49  #include <tr1/unordered_set>
50  #include <tr1/unordered_map>
51  namespace octomap {
52  namespace unordered_ns = std::tr1;
53  };
54 #else
55  #include <unordered_set>
56  #include <unordered_map>
57  namespace octomap {
58  namespace unordered_ns = std;
59  }
60 #endif
61 
62 namespace octomap {
63 
64  typedef uint16_t key_type;
65 
71  class OcTreeKey {
72 
73  public:
74  OcTreeKey () {}
75  OcTreeKey (key_type a, key_type b, key_type c){
76  k[0] = a;
77  k[1] = b;
78  k[2] = c;
79  }
80 
81  OcTreeKey(const OcTreeKey& other){
82  k[0] = other.k[0];
83  k[1] = other.k[1];
84  k[2] = other.k[2];
85  }
86 
87  bool operator== (const OcTreeKey &other) const {
88  return ((k[0] == other[0]) && (k[1] == other[1]) && (k[2] == other[2]));
89  }
90 
91  bool operator!= (const OcTreeKey& other) const {
92  return( (k[0] != other[0]) || (k[1] != other[1]) || (k[2] != other[2]) );
93  }
94 
95  OcTreeKey& operator=(const OcTreeKey& other){
96  k[0] = other.k[0]; k[1] = other.k[1]; k[2] = other.k[2];
97  return *this;
98  }
99 
100  const key_type& operator[] (unsigned int i) const {
101  return k[i];
102  }
103 
104  key_type& operator[] (unsigned int i) {
105  return k[i];
106  }
107 
108  key_type k[3];
109 
111  struct KeyHash{
112  size_t operator()(const OcTreeKey& key) const{
113  // a simple hashing function
114  // explicit casts to size_t to operate on the complete range
115  // constanst will be promoted according to C++ standard
116  return size_t(key.k[0]) + 1447*size_t(key.k[1]) + 345637*size_t(key.k[2]);
117  }
118  };
119 
120  };
121 
128  typedef unordered_ns::unordered_set<OcTreeKey, OcTreeKey::KeyHash> KeySet;
129 
135  typedef unordered_ns::unordered_map<OcTreeKey, bool, OcTreeKey::KeyHash> KeyBoolMap;
136 
137 
138  class KeyRay {
139  public:
140 
141  KeyRay () {
142  ray.resize(maxSize);
143  reset();
144  }
145 
146  KeyRay(const KeyRay& other){
147  ray = other.ray;
148  size_t dSize = other.end() - other.begin();
149  end_of_ray = ray.begin() + dSize;
150  }
151 
152  void reset() {
153  end_of_ray = begin();
154  }
155 
156  void addKey(const OcTreeKey& k) {
157  assert(end_of_ray != ray.end());
158  *end_of_ray = k;
159  ++end_of_ray;
160  }
161 
162  size_t size() const { return end_of_ray - ray.begin(); }
163  size_t sizeMax() const { return maxSize; }
164 
165  typedef std::vector<OcTreeKey>::iterator iterator;
166  typedef std::vector<OcTreeKey>::const_iterator const_iterator;
167  typedef std::vector<OcTreeKey>::reverse_iterator reverse_iterator;
168 
169  iterator begin() { return ray.begin(); }
170  iterator end() { return end_of_ray; }
171  const_iterator begin() const { return ray.begin(); }
172  const_iterator end() const { return end_of_ray; }
173 
174  reverse_iterator rbegin() { return (reverse_iterator) end_of_ray; }
175  reverse_iterator rend() { return ray.rend(); }
176 
177  private:
178  std::vector<OcTreeKey> ray;
179  std::vector<OcTreeKey>::iterator end_of_ray;
180  const static size_t maxSize = 100000;
181  };
182 
192  inline void computeChildKey (unsigned int pos, key_type center_offset_key,
193  const OcTreeKey& parent_key, OcTreeKey& child_key) {
194  // x-axis
195  if (pos & 1) child_key[0] = parent_key[0] + center_offset_key;
196  else child_key[0] = parent_key[0] - center_offset_key - (center_offset_key ? 0 : 1);
197  // y-axis
198  if (pos & 2) child_key[1] = parent_key[1] + center_offset_key;
199  else child_key[1] = parent_key[1] - center_offset_key - (center_offset_key ? 0 : 1);
200  // z-axis
201  if (pos & 4) child_key[2] = parent_key[2] + center_offset_key;
202  else child_key[2] = parent_key[2] - center_offset_key - (center_offset_key ? 0 : 1);
203  }
204 
206  inline uint8_t computeChildIdx(const OcTreeKey& key, int depth){
207  uint8_t pos = 0;
208  if (key.k[0] & (1 << depth))
209  pos += 1;
210 
211  if (key.k[1] & (1 << depth))
212  pos += 2;
213 
214  if (key.k[2] & (1 << depth))
215  pos += 4;
216 
217  return pos;
218  }
219 
227  inline OcTreeKey computeIndexKey(key_type level, const OcTreeKey& key) {
228  if (level == 0)
229  return key;
230  else {
231  key_type mask = 65535 << level;
232  OcTreeKey result = key;
233  result[0] &= mask;
234  result[1] &= mask;
235  result[2] &= mask;
236  return result;
237  }
238  }
239 
240 } // namespace
241 
242 #endif
OcTreeKey(key_type a, key_type b, key_type c)
Definition: OcTreeKey.h:75
key_type k[3]
Definition: OcTreeKey.h:108
iterator begin()
Definition: OcTreeKey.h:169
Provides a hash function on Keys.
Definition: OcTreeKey.h:111
std::vector< OcTreeKey > ray
Definition: OcTreeKey.h:178
uint16_t key_type
Definition: OcTreeKey.h:64
bool operator!=(const OcTreeKey &other) const
Definition: OcTreeKey.h:91
void addKey(const OcTreeKey &k)
Definition: OcTreeKey.h:156
OcTreeKey & operator=(const OcTreeKey &other)
Definition: OcTreeKey.h:95
std::vector< OcTreeKey >::iterator end_of_ray
Definition: OcTreeKey.h:179
unordered_ns::unordered_map< OcTreeKey, bool, OcTreeKey::KeyHash > KeyBoolMap
Definition: OcTreeKey.h:135
const key_type & operator[](unsigned int i) const
Definition: OcTreeKey.h:100
KeyRay(const KeyRay &other)
Definition: OcTreeKey.h:146
const_iterator begin() const
Definition: OcTreeKey.h:171
OcTreeKey(const OcTreeKey &other)
Definition: OcTreeKey.h:81
std::vector< OcTreeKey >::iterator iterator
Definition: OcTreeKey.h:165
uint8_t computeChildIdx(const OcTreeKey &key, int depth)
generate child index (between 0 and 7) from key at given tree depth
Definition: OcTreeKey.h:206
size_t operator()(const OcTreeKey &key) const
Definition: OcTreeKey.h:112
const_iterator end() const
Definition: OcTreeKey.h:172
reverse_iterator rend()
Definition: OcTreeKey.h:175
std::vector< OcTreeKey >::const_iterator const_iterator
Definition: OcTreeKey.h:166
unordered_ns::unordered_set< OcTreeKey, OcTreeKey::KeyHash > KeySet
Definition: OcTreeKey.h:128
reverse_iterator rbegin()
Definition: OcTreeKey.h:174
bool operator==(const OcTreeKey &other) const
Definition: OcTreeKey.h:87
size_t size() const
Definition: OcTreeKey.h:162
iterator end()
Definition: OcTreeKey.h:170
void computeChildKey(unsigned int pos, key_type center_offset_key, const OcTreeKey &parent_key, OcTreeKey &child_key)
Definition: OcTreeKey.h:192
OcTreeKey computeIndexKey(key_type level, const OcTreeKey &key)
Definition: OcTreeKey.h:227
std::vector< OcTreeKey >::reverse_iterator reverse_iterator
Definition: OcTreeKey.h:167
size_t sizeMax() const
Definition: OcTreeKey.h:163


octomap
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Wed Jun 5 2019 19:26:27