Autoware.Auto
ndt_grid.hpp
Go to the documentation of this file.
1 // Copyright 2019 the Autoware Foundation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // Co-developed by Tier IV, Inc. and Apex.AI, Inc.
16 
17 #ifndef NDT__NDT_GRID_HPP_
18 #define NDT__NDT_GRID_HPP_
19 
20 #include <common/types.hpp>
21 #include <ndt/ndt_common.hpp>
22 #include <ndt/ndt_voxel_view.hpp>
23 #include <vector>
24 #include <limits>
25 #include <unordered_map>
26 #include <utility>
27 #include <string>
28 
30 
31 namespace autoware
32 {
33 namespace localization
34 {
35 namespace ndt
36 {
39 template<typename VoxelT>
40 class NDTGrid
41 {
42 public:
43  using Grid = std::unordered_map<uint64_t, VoxelT>;
44  using Point = Eigen::Vector3d;
46  using VoxelViewVector = std::vector<VoxelView<VoxelT>>;
47  using ConfigPoint = std::decay_t<decltype(std::declval<Config>().get_min_point())>;
48 
51  explicit NDTGrid(const Config & voxel_grid_config)
52  : m_config(voxel_grid_config), m_map(m_config.get_capacity())
53  {
54  m_output_vector.reserve(1U);
55  }
56 
57  // Maps should be moved rather than being copied.
58  NDTGrid(const NDTGrid &) = delete;
59 
60  NDTGrid & operator=(const NDTGrid &) = delete;
61 
62  // Explicitly declaring to default is needed since we explicitly deleted the copy methods.
63  NDTGrid(NDTGrid &&) = default;
64 
65  NDTGrid & operator=(NDTGrid &&) = default;
66 
74  {
75  return cell(Point({x, y, z}));
76  }
77 
82  const VoxelViewVector & cell(const Point & pt) const
83  {
84  // TODO(yunus.caliskan): revisit after multi-cell lookup support. #985
85  m_output_vector.clear();
86  const auto vx_it = m_map.find(m_config.index(pt));
87  // Only return a voxel if it's occupied (i.e. has enough points to compute covariance.)
88  if (vx_it != m_map.end() && vx_it->second.usable()) {
89  m_output_vector.emplace_back(vx_it->second);
90  }
91  return m_output_vector;
92  }
93 
97  std::size_t size() const noexcept
98  {
99  return m_map.size();
100  }
101 
104  const ConfigPoint & cell_size() const noexcept
105  {
106  return m_config.get_voxel_size();
107  }
108 
111  typename Grid::const_iterator begin() const noexcept
112  {
113  return cbegin();
114  }
115 
118  typename Grid::iterator begin() noexcept
119  {
120  return m_map.begin();
121  }
122 
125  typename Grid::const_iterator cbegin() const noexcept
126  {
127  return m_map.cbegin();
128  }
129 
132  typename Grid::const_iterator end() const noexcept
133  {
134  return cend();
135  }
136 
139  typename Grid::iterator end() noexcept
140  {
141  return m_map.end();
142  }
143 
146  typename Grid::const_iterator cend() const noexcept
147  {
148  return m_map.cend();
149  }
150 
152  void clear() noexcept
153  {
154  m_map.clear();
155  }
156 
160  auto index(const Point & pt) const
161  {
162  return m_config.index(pt);
163  }
164 
168  template<typename ... Args>
169  auto emplace_voxel(Args && ... args)
170  {
171  return m_map.emplace(std::forward<Args>(args)...);
172  }
173 
174  void add_observation(const Point & pt)
175  {
176  m_map[index(pt)].add_observation(pt);
177  }
178 
179 private:
180  mutable VoxelViewVector m_output_vector;
181  const Config m_config;
182  Grid m_map;
183 };
184 
185 } // namespace ndt
186 } // namespace localization
187 
188 namespace common
189 {
190 namespace geometry
191 {
192 namespace point_adapter
193 {
197 template<>
198 inline NDT_PUBLIC auto x_(const Eigen::Vector3d & pt)
199 {
200  return static_cast<float32_t>(pt(0));
201 }
202 
203 template<>
204 inline NDT_PUBLIC auto y_(const Eigen::Vector3d & pt)
205 {
206  return static_cast<float32_t>(pt(1));
207 }
208 
209 template<>
210 inline NDT_PUBLIC auto z_(const Eigen::Vector3d & pt)
211 {
212  return static_cast<float32_t>(pt(2));
213 }
214 
215 template<>
216 inline NDT_PUBLIC auto & xr_(Eigen::Vector3d & pt)
217 {
218  return pt(0);
219 }
220 
221 template<>
222 inline NDT_PUBLIC auto & yr_(Eigen::Vector3d & pt)
223 {
224  return pt(1);
225 }
226 
227 template<>
228 inline NDT_PUBLIC auto & zr_(Eigen::Vector3d & pt)
229 {
230  return pt(2);
231 }
232 
233 
234 } // namespace point_adapter
235 } // namespace geometry
236 } // namespace common
237 } // namespace autoware
238 #endif // NDT__NDT_GRID_HPP_
const PointXYZ & get_voxel_size() const
Gets the voxel size of the voxel grid.
Definition: perception/filters/voxel_grid/src/config.cpp:115
NDTGrid & operator=(const NDTGrid &)=delete
NDT_PUBLIC auto & xr_(Eigen::Vector3d &pt)
Definition: ndt_grid.hpp:216
A voxel grid implementation for normal distribution transform.
Definition: ndt_grid.hpp:40
Grid::const_iterator begin() const noexcept
Returns an const iterator to the first element of the map.
Definition: ndt_grid.hpp:111
std::size_t size() const noexcept
Definition: ndt_grid.hpp:97
NDT_PUBLIC auto x_(const Eigen::Vector3d &pt)
Definition: ndt_grid.hpp:198
float float32_t
Definition: types.hpp:36
NDT_PUBLIC auto y_(const Eigen::Vector3d &pt)
Definition: ndt_grid.hpp:204
std::decay_t< decltype(std::declval< Config >().get_min_point())> ConfigPoint
Definition: ndt_grid.hpp:47
std::vector< VoxelView< autoware::localization::ndt::DynamicNDTVoxel > > VoxelViewVector
Definition: ndt_grid.hpp:46
const VoxelViewVector & cell(const Point &pt) const
Definition: ndt_grid.hpp:82
std::unordered_map< uint64_t, autoware::localization::ndt::DynamicNDTVoxel > Grid
Definition: ndt_grid.hpp:43
void add_observation(const Point &pt)
Definition: ndt_grid.hpp:174
This file includes common type definition.
Grid::iterator begin() noexcept
Returns an iterator to the first element of the map.
Definition: ndt_grid.hpp:118
A configuration class for the VoxelGrid data structure, also includes some helper functionality for c...
Definition: perception/filters/voxel_grid/include/voxel_grid/config.hpp:52
const ConfigPoint & cell_size() const noexcept
Definition: ndt_grid.hpp:104
Grid::const_iterator cend() const noexcept
Returns a const iterator to one past the last element of the map.
Definition: ndt_grid.hpp:146
Grid::const_iterator end() const noexcept
Returns a const iterator to one past the last element of the map.
Definition: ndt_grid.hpp:132
Grid::iterator end() noexcept
Returns an iterator to one past the last element of the map.
Definition: ndt_grid.hpp:139
auto emplace_voxel(Args &&... args)
Emplace a new voxel into the grid.
Definition: ndt_grid.hpp:169
NDT_PUBLIC auto & zr_(Eigen::Vector3d &pt)
Definition: ndt_grid.hpp:228
DifferentialState x
Definition: kinematic_bicycle.snippet.hpp:28
const VoxelViewVector & cell(float32_t x, float32_t y, float32_t z) const
Definition: ndt_grid.hpp:73
Grid::const_iterator cbegin() const noexcept
Returns a const iterator to the first element of the map.
Definition: ndt_grid.hpp:125
NDTGrid(const Config &voxel_grid_config)
Definition: ndt_grid.hpp:51
auto index(const Point &pt) const
Definition: ndt_grid.hpp:160
DifferentialState y
Definition: kinematic_bicycle.snippet.hpp:28
uint64_t index(const PointT &pt) const
Computes index for a given point given the voxelgrid configuration parameters.
Definition: perception/filters/voxel_grid/include/voxel_grid/config.hpp:84
NDT_PUBLIC auto & yr_(Eigen::Vector3d &pt)
Definition: ndt_grid.hpp:222
NDT_PUBLIC auto z_(const Eigen::Vector3d &pt)
Definition: ndt_grid.hpp:210
void clear() noexcept
Clear all voxels in the map.
Definition: ndt_grid.hpp:152
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24