NASA Astrobee Robot Software  0.19.1
Flight software for the Astrobee robots operating inside the International Space Station.
indexed_octree_key.h
Go to the documentation of this file.
1 /* Copyright (c) 2017, United States Government, as represented by the
2  * Administrator of the National Aeronautics and Space Administration.
3  *
4  * All rights reserved.
5  *
6  * The Astrobee platform is licensed under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations
16  * under the License.
17  */
18 
19 #ifndef MAPPER_INDEXED_OCTREE_KEY_H_
20 #define MAPPER_INDEXED_OCTREE_KEY_H_
21 
22 #include <octomap/octomap.h>
23 #include <octomap/OcTree.h>
24 #include <unordered_set>
25 
26 namespace octoclass {
27 
28 typedef uint16_t key_type;
29 
30 // This class is used in graphs for finding the index for a given key in the octomap
32  public:
34  IndexedOcTreeKey(key_type a, key_type b, key_type c, uint index_in) {
35  k_[0] = a;
36  k_[1] = b;
37  k_[2] = c;
38  index_ = index_in;
39  }
40 
41  IndexedOcTreeKey(const octomap::OcTreeKey& other, uint index_in) {
42  k_[0] = other.k[0];
43  k_[1] = other.k[1];
44  k_[2] = other.k[2];
45  index_ = index_in;
46  }
47 
48  bool operator==(const IndexedOcTreeKey &other) const {
49  return ((k_[0] == other[0]) && (k_[1] == other[1]) && (k_[2] == other[2]));
50  }
51 
52  bool operator!=(const IndexedOcTreeKey& other) const {
53  return( (k_[0] != other[0]) || (k_[1] != other[1]) || (k_[2] != other[2]) );
54  }
55 
57  k_[0] = other.k_[0]; k_[1] = other.k_[1]; k_[2] = other.k_[2]; index_ = other.index_;
58  return *this;
59  }
60 
61  const key_type& operator[] (unsigned int i) const {
62  return k_[i];
63  }
64 
65  key_type& operator[] (unsigned int i) {
66  return k_[i];
67  }
68 
70  uint index_;
71 
73  struct KeyHash{
74  size_t operator()(const IndexedOcTreeKey& key) const {
75  // a simple hashing function
76  // explicit casts to size_t to operate on the complete range
77  // constanst will be promoted according to C++ standard
78  return static_cast<size_t>(key.k_[0])
79  + 1447*static_cast<size_t>(key.k_[1])
80  + 345637*static_cast<size_t>(key.k_[2]);
81  }
82  };
83 };
84 
85 typedef std::tr1::unordered_set<IndexedOcTreeKey, IndexedOcTreeKey::KeyHash> KeySet;
86 
87 
88 // Class for saving pairs of keys/indexes
90  public:
92 
93  // Methods
94  void Insert(const octomap::OcTreeKey &key, uint &index) {
95  set_.insert(IndexedOcTreeKey(key, index));
96  }
97 
98  bool Key2Index(const octomap::OcTreeKey &key, uint *index_out) {
99  std::tr1::unordered_set<IndexedOcTreeKey, IndexedOcTreeKey::KeyHash>::const_iterator key2index;
100  key2index = set_.find(IndexedOcTreeKey(key, 0));
101  if (key2index == set_.end()) {
102  return false;
103  } else {
104  *index_out = key2index->index_;
105  return true;
106  }
107  }
108 
109  size_t Size() { return set_.size(); }
110 };
111 
112 } // namespace octoclass
113 
114 #endif // MAPPER_INDEXED_OCTREE_KEY_H_
octoclass::IndexedOcTreeKey
Definition: indexed_octree_key.h:31
octoclass::key_type
uint16_t key_type
Definition: indexed_octree_key.h:28
octoclass::IndexedOcTreeKey::operator=
IndexedOcTreeKey & operator=(const IndexedOcTreeKey &other)
Definition: indexed_octree_key.h:56
octoclass::IndexedOcTreeKey::k_
key_type k_[3]
Definition: indexed_octree_key.h:69
octoclass::IndexedOcTreeKey::operator[]
const key_type & operator[](unsigned int i) const
Definition: indexed_octree_key.h:61
octoclass::IndexedOcTreeKey::IndexedOcTreeKey
IndexedOcTreeKey()
Definition: indexed_octree_key.h:33
octoclass::IndexedKeySet::Size
size_t Size()
Definition: indexed_octree_key.h:109
octoclass::IndexedOcTreeKey::operator!=
bool operator!=(const IndexedOcTreeKey &other) const
Definition: indexed_octree_key.h:52
octoclass::IndexedOcTreeKey::index_
uint index_
Definition: indexed_octree_key.h:70
octoclass::IndexedOcTreeKey::KeyHash::operator()
size_t operator()(const IndexedOcTreeKey &key) const
Definition: indexed_octree_key.h:74
octoclass::KeySet
std::tr1::unordered_set< IndexedOcTreeKey, IndexedOcTreeKey::KeyHash > KeySet
Definition: indexed_octree_key.h:85
octoclass::IndexedOcTreeKey::operator==
bool operator==(const IndexedOcTreeKey &other) const
Definition: indexed_octree_key.h:48
octoclass
Definition: indexed_octree_key.h:26
octoclass::IndexedOcTreeKey::IndexedOcTreeKey
IndexedOcTreeKey(const octomap::OcTreeKey &other, uint index_in)
Definition: indexed_octree_key.h:41
octoclass::IndexedKeySet
Definition: indexed_octree_key.h:89
octoclass::IndexedKeySet::Insert
void Insert(const octomap::OcTreeKey &key, uint &index)
Definition: indexed_octree_key.h:94
octoclass::IndexedOcTreeKey::KeyHash
Provides a hash function on Keys.
Definition: indexed_octree_key.h:73
octoclass::IndexedOcTreeKey::IndexedOcTreeKey
IndexedOcTreeKey(key_type a, key_type b, key_type c, uint index_in)
Definition: indexed_octree_key.h:34
octoclass::IndexedKeySet::set_
KeySet set_
Definition: indexed_octree_key.h:91
octoclass::IndexedKeySet::Key2Index
bool Key2Index(const octomap::OcTreeKey &key, uint *index_out)
Definition: indexed_octree_key.h:98