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 
44 /* Libc++ does not implement the TR1 namespace, all c++11 related functionality
45  * is instead implemented in the std namespace.
46  */
47 #if defined(__GNUC__) && ! defined(_LIBCPP_VERSION)
48  #include <tr1/unordered_set>
49  #include <tr1/unordered_map>
50  namespace octomap {
51  namespace unordered_ns = std::tr1;
52  };
53 #else
54  #include <unordered_set>
55  #include <unordered_map>
56  namespace octomap {
57  namespace unordered_ns = std;
58  }
59 #endif
60 
61 namespace octomap {
62 
68  class OcTreeKey {
69 
70  public:
71  OcTreeKey () {}
72  OcTreeKey (unsigned short int a, unsigned short int b, unsigned short int c)
73  { k[0] = a; k[1] = b; k[2] = c; }
74  OcTreeKey(const OcTreeKey& other){
75  k[0] = other.k[0]; k[1] = other.k[1]; k[2] = other.k[2];
76  }
77  bool operator== (const OcTreeKey &other) const {
78  return ((k[0] == other[0]) && (k[1] == other[1]) && (k[2] == other[2]));
79  }
80  bool operator!= (const OcTreeKey &other) const {
81  return( (k[0] != other[0]) || (k[1] != other[1]) || (k[2] != other[2]) );
82  }
83  OcTreeKey& operator=(const OcTreeKey& other){
84  k[0] = other.k[0]; k[1] = other.k[1]; k[2] = other.k[2];
85  return *this;
86  }
87  const unsigned short int& operator[] (unsigned int i) const {
88  return k[i];
89  }
90  unsigned short int& operator[] (unsigned int i) {
91  return k[i];
92  }
93 
94  unsigned short int k[3];
95 
97  struct KeyHash{
98  size_t operator()(const OcTreeKey& key) const{
99  // a simple hashing function
100  // explicit casts to size_t to operate on the complete range
101  // constanst will be promoted according to C++ standard
102  return size_t(key.k[0]) + 1447*size_t(key.k[1]) + 345637*size_t(key.k[2]);
103  }
104  };
105 
106  };
107 
114  typedef unordered_ns::unordered_set<OcTreeKey, OcTreeKey::KeyHash> KeySet;
115 
121  typedef unordered_ns::unordered_map<OcTreeKey, bool, OcTreeKey::KeyHash> KeyBoolMap;
122 
123 
124  class KeyRay {
125  public:
126 
127  KeyRay () {
128  ray.resize(100000);
129  reset();
130  }
131  void reset() {
132  end_of_ray = begin();
133  }
134  void addKey(OcTreeKey& k) {
135  assert(end_of_ray != ray.end());
136  *end_of_ray = k;
137  end_of_ray++;
138  }
139 
140  size_t size() const { return end_of_ray - ray.begin(); }
141  size_t sizeMax() const { return 100000; }
142 
143  typedef std::vector<OcTreeKey>::iterator iterator;
144  typedef std::vector<OcTreeKey>::const_iterator const_iterator;
145  typedef std::vector<OcTreeKey>::reverse_iterator reverse_iterator;
146 
147  iterator begin() { return ray.begin(); }
148  iterator end() { return end_of_ray; }
149  const_iterator begin() const { return ray.begin(); }
150  const_iterator end() const { return end_of_ray; }
151 
152  reverse_iterator rbegin() { return (reverse_iterator) end_of_ray; }
153  reverse_iterator rend() { return ray.rend(); }
154 
155  public:
156 
157  std::vector<OcTreeKey> ray;
158  std::vector<OcTreeKey>::iterator end_of_ray;
159  };
160 
170  inline void computeChildKey (const unsigned int& pos, const unsigned short int& center_offset_key,
171  const OcTreeKey& parent_key, OcTreeKey& child_key) {
172  // x-axis
173  if (pos & 1) child_key[0] = parent_key[0] + center_offset_key;
174  else child_key[0] = parent_key[0] - center_offset_key - (center_offset_key ? 0 : 1);
175  // y-axis
176  if (pos & 2) child_key[1] = parent_key[1] + center_offset_key;
177  else child_key[1] = parent_key[1] - center_offset_key - (center_offset_key ? 0 : 1);
178  // z-axis
179  if (pos & 4) child_key[2] = parent_key[2] + center_offset_key;
180  else child_key[2] = parent_key[2] - center_offset_key - (center_offset_key ? 0 : 1);
181  }
182 
184  inline unsigned char computeChildIdx(const OcTreeKey& key, int depth){
185  unsigned char pos = 0;
186  if (key.k[0] & (1 << depth)) pos += 1;
187  if (key.k[1] & (1 << depth)) pos += 2;
188  if (key.k[2] & (1 << depth)) pos += 4;
189  return pos;
190  }
191 
199  inline OcTreeKey computeIndexKey(unsigned short int level, const OcTreeKey& key) {
200  if (level == 0)
201  return key;
202  else {
203  unsigned short int mask = 65535 << level;
204  OcTreeKey result = key;
205  result[0] &= mask;
206  result[1] &= mask;
207  result[2] &= mask;
208  return result;
209  }
210  }
211 
212 } // namespace
213 
214 #endif
iterator begin()
Definition: OcTreeKey.h:147
Provides a hash function on Keys.
Definition: OcTreeKey.h:97
std::vector< OcTreeKey > ray
Definition: OcTreeKey.h:157
unsigned char computeChildIdx(const OcTreeKey &key, int depth)
generate child index (between 0 and 7) from key at given tree depth
Definition: OcTreeKey.h:184
bool operator!=(const OcTreeKey &other) const
Definition: OcTreeKey.h:80
OcTreeKey & operator=(const OcTreeKey &other)
Definition: OcTreeKey.h:83
std::vector< OcTreeKey >::iterator end_of_ray
Definition: OcTreeKey.h:158
unordered_ns::unordered_map< OcTreeKey, bool, OcTreeKey::KeyHash > KeyBoolMap
Definition: OcTreeKey.h:121
void computeChildKey(const unsigned int &pos, const unsigned short int &center_offset_key, const OcTreeKey &parent_key, OcTreeKey &child_key)
Definition: OcTreeKey.h:170
const_iterator begin() const
Definition: OcTreeKey.h:149
OcTreeKey(const OcTreeKey &other)
Definition: OcTreeKey.h:74
unsigned short int k[3]
Definition: OcTreeKey.h:94
std::vector< OcTreeKey >::iterator iterator
Definition: OcTreeKey.h:143
size_t operator()(const OcTreeKey &key) const
Definition: OcTreeKey.h:98
const_iterator end() const
Definition: OcTreeKey.h:150
const unsigned short int & operator[](unsigned int i) const
Definition: OcTreeKey.h:87
reverse_iterator rend()
Definition: OcTreeKey.h:153
std::vector< OcTreeKey >::const_iterator const_iterator
Definition: OcTreeKey.h:144
unordered_ns::unordered_set< OcTreeKey, OcTreeKey::KeyHash > KeySet
Definition: OcTreeKey.h:114
void addKey(OcTreeKey &k)
Definition: OcTreeKey.h:134
reverse_iterator rbegin()
Definition: OcTreeKey.h:152
bool operator==(const OcTreeKey &other) const
Definition: OcTreeKey.h:77
size_t size() const
Definition: OcTreeKey.h:140
OcTreeKey(unsigned short int a, unsigned short int b, unsigned short int c)
Definition: OcTreeKey.h:72
iterator end()
Definition: OcTreeKey.h:148
std::vector< OcTreeKey >::reverse_iterator reverse_iterator
Definition: OcTreeKey.h:145
size_t sizeMax() const
Definition: OcTreeKey.h:141
OcTreeKey computeIndexKey(unsigned short int level, const OcTreeKey &key)
Definition: OcTreeKey.h:199


octomap
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Mon Jun 10 2019 14:00:13