Autoware.Auto
ndt_map.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_MAP_HPP_
18 #define NDT__NDT_MAP_HPP_
19 
20 #include <ndt/ndt_common.hpp>
21 #include <ndt/ndt_voxel.hpp>
22 #include <ndt/ndt_voxel_view.hpp>
23 #include <ndt/ndt_grid.hpp>
24 #include <sensor_msgs/point_cloud2_iterator.hpp>
26 #include <vector>
27 #include <limits>
28 #include <unordered_map>
29 #include <utility>
30 #include <string>
31 
33 
34 namespace autoware
35 {
36 namespace localization
37 {
38 namespace ndt
39 {
47 uint32_t NDT_PUBLIC validate_pcl_map(const sensor_msgs::msg::PointCloud2 & msg);
48 
51 class NDT_PUBLIC DynamicNDTMap
52 {
53 public:
56  using Point = Eigen::Vector3d;
57  using TimePoint = std::chrono::system_clock::time_point;
58  using VoxelViewVector = std::vector<VoxelView<Voxel>>;
61 
64  static constexpr uint32_t kNumConfigPoints = 3U;
65 
66  explicit DynamicNDTMap(const Config & voxel_grid_config);
67 
70  void set(const sensor_msgs::msg::PointCloud2 & msg);
71 
75  void insert(const sensor_msgs::msg::PointCloud2 & msg);
76 
82  template<typename DeserializingMapT>
83  void serialize_as(sensor_msgs::msg::PointCloud2 & msg_out) const;
84 
89  const VoxelViewVector & cell(const Point & pt) const;
90 
97  const VoxelViewVector & cell(float32_t x, float32_t y, float32_t z) const;
98 
101  const std::string & frame_id() const noexcept;
102 
105  TimePoint stamp() const noexcept;
106 
109  bool valid() const noexcept;
110 
113  const ConfigPoint & cell_size() const noexcept;
114 
118  std::size_t size() const noexcept;
119 
122  typename VoxelGrid::const_iterator begin() const noexcept;
123 
126  typename VoxelGrid::const_iterator end() const noexcept;
127 
129  void clear() noexcept;
130 
131 private:
132  NDTGrid<DynamicNDTVoxel> m_grid;
133  TimePoint m_stamp{};
134  std::string m_frame_id{};
135 };
136 
140 class NDT_PUBLIC StaticNDTMap
141 {
142 public:
145  using TimePoint = std::chrono::system_clock::time_point;
146  using Point = Eigen::Vector3d;
147  using VoxelViewVector = std::vector<VoxelView<Voxel>>;
150 
159  void set(const sensor_msgs::msg::PointCloud2 & msg);
160 
165  const VoxelViewVector & cell(const Point & pt) const;
166 
173  const VoxelViewVector & cell(float32_t x, float32_t y, float32_t z) const;
174 
177  const std::string & frame_id() const noexcept;
178 
181  TimePoint stamp() const noexcept;
182 
185  bool valid() const noexcept;
186 
189  const ConfigPoint & cell_size() const;
190 
194  std::size_t size() const;
195 
198  typename VoxelGrid::const_iterator begin() const;
199 
202  typename VoxelGrid::const_iterator end() const;
203 
205  void clear();
206 
207 private:
210  void deserialize_from(const sensor_msgs::msg::PointCloud2 & msg);
211  std::experimental::optional<NDTGrid<StaticNDTVoxel>> m_grid{};
212  TimePoint m_stamp{};
213  std::string m_frame_id{};
214 };
215 
216 template<typename PCMessageT>
217 using PC2DoubleIterator = typename std::conditional<std::is_const<PCMessageT>::value,
218  sensor_msgs::PointCloud2ConstIterator<ndt::Real>,
219  sensor_msgs::PointCloud2Iterator<ndt::Real>>::type;
220 
222 template<typename PointCloud2T>
223 std::array<PC2DoubleIterator<PointCloud2T>, 9U> get_iterators(PointCloud2T & msg)
224 {
225  return std::array<PC2DoubleIterator<PointCloud2T>, 9U>{
229  PC2DoubleIterator<PointCloud2T>{msg, "icov_xx"},
230  PC2DoubleIterator<PointCloud2T>{msg, "icov_xy"},
231  PC2DoubleIterator<PointCloud2T>{msg, "icov_xz"},
232  PC2DoubleIterator<PointCloud2T>{msg, "icov_yy"},
233  PC2DoubleIterator<PointCloud2T>{msg, "icov_yz"},
234  PC2DoubleIterator<PointCloud2T>{msg, "icov_zz"}
235  };
236 }
237 
240 {
241  Real x{0.0};
242  Real y{0.0};
243  Real z{0.0};
244  Real icov_xx{0.0};
245  Real icov_xy{0.0};
246  Real icov_xz{0.0};
247  Real icov_yy{0.0};
248  Real icov_yz{0.0};
249  Real icov_zz{0.0};
250 };
251 
255 void push_back(
256  std::array<sensor_msgs::PointCloud2Iterator<ndt::Real>, 9U> & pc_its,
257  const SerializedNDTMapPoint & point);
258 
262 template<typename T>
263 SerializedNDTMapPoint next(std::array<T, 9U> & pc_its)
264 {
265  const auto ret = SerializedNDTMapPoint{
266  *pc_its[0U], *pc_its[1U], *pc_its[2U], // x, y, z
267  *pc_its[3U], *pc_its[4U], *pc_its[5U], // xx, yy, zz
268  *pc_its[6U], *pc_its[7U], // yy, yz
269  *pc_its[8U] // zz
270  };
271  std::for_each(pc_its.begin(), pc_its.end(), [](auto & it) {++it;});
272  return ret;
273 }
274 
275 } // namespace ndt
276 } // namespace localization
277 } // namespace autoware
278 #endif // NDT__NDT_MAP_HPP_
autoware::localization::ndt::DynamicNDTMap::VoxelViewVector
std::vector< VoxelView< Voxel > > VoxelViewVector
Definition: ndt_map.hpp:58
autoware::localization::ndt::SerializedNDTMapPoint::y
Real y
Definition: ndt_map.hpp:242
y
DifferentialState y
Definition: kinematic_bicycle.snippet.hpp:28
autoware::localization::ndt::SerializedNDTMapPoint::icov_yy
Real icov_yy
Definition: ndt_map.hpp:247
autoware::localization::ndt::get_iterators
std::array< PC2DoubleIterator< PointCloud2T >, 9U > get_iterators(PointCloud2T &msg)
Get the pointcloud2 iterators for the serialized ndt map.
Definition: ndt_map.hpp:223
autoware::localization::ndt::DynamicNDTMap::ConfigPoint
NDTGrid< Voxel >::ConfigPoint ConfigPoint
Definition: ndt_map.hpp:60
ndt_voxel_view.hpp
autoware::localization::ndt::StaticNDTMap::Point
Eigen::Vector3d Point
Definition: ndt_map.hpp:146
autoware::localization::ndt::Real
float64_t Real
Definition: ndt_common.hpp:39
autoware::localization::ndt::NDTGrid
A voxel grid implementation for normal distribution transform.
Definition: ndt_grid.hpp:40
autoware::localization::ndt::SerializedNDTMapPoint
Struct to represent a point in the serialized ndt map. This is a minimal voxel representation.
Definition: ndt_map.hpp:239
autoware::localization::ndt::SerializedNDTMapPoint::z
Real z
Definition: ndt_map.hpp:243
autoware::localization::ndt::SerializedNDTMapPoint::x
Real x
Definition: ndt_map.hpp:241
autoware::localization::ndt::SerializedNDTMapPoint::icov_xx
Real icov_xx
Definition: ndt_map.hpp:244
autoware::localization::ndt::StaticNDTVoxel
Definition: ndt_voxel.hpp:98
autoware::localization::ndt::SerializedNDTMapPoint::icov_xy
Real icov_xy
Definition: ndt_map.hpp:245
autoware::localization::ndt::StaticNDTMap::VoxelGrid
NDTGrid< Voxel >::Grid VoxelGrid
Definition: ndt_map.hpp:148
autoware::localization::ndt::StaticNDTMap::ConfigPoint
NDTGrid< Voxel >::ConfigPoint ConfigPoint
Definition: ndt_map.hpp:149
autoware
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
autoware::localization::ndt::next
SerializedNDTMapPoint next(std::array< T, 9U > &pc_its)
Get the next voxel representation from the iterators of a serialized ndt map.
Definition: ndt_map.hpp:263
autoware::localization::ndt::StaticNDTMap::TimePoint
std::chrono::system_clock::time_point TimePoint
Definition: ndt_map.hpp:145
autoware::localization::ndt::DynamicNDTMap::VoxelGrid
NDTGrid< Voxel >::Grid VoxelGrid
Definition: ndt_map.hpp:59
autoware::localization::ndt::SerializedNDTMapPoint::icov_xz
Real icov_xz
Definition: ndt_map.hpp:246
ndt_common.hpp
autoware::localization::ndt::DynamicNDTMap
Definition: ndt_map.hpp:51
autoware::localization::ndt::validate_pcl_map
uint32_t NDT_PUBLIC validate_pcl_map(const sensor_msgs::msg::PointCloud2 &msg)
Definition: ndt_map.cpp:28
x
DifferentialState x
Definition: kinematic_bicycle.snippet.hpp:28
ndt_grid.hpp
autoware::localization::ndt::StaticNDTMap::VoxelViewVector
std::vector< VoxelView< Voxel > > VoxelViewVector
Definition: ndt_map.hpp:147
autoware::common::types::float32_t
float float32_t
Definition: types.hpp:45
autoware::perception::filters::voxel_grid::Config
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
autoware::localization::ndt::DynamicNDTMap::TimePoint
std::chrono::system_clock::time_point TimePoint
Definition: ndt_map.hpp:57
autoware::localization::ndt::StaticNDTMap
Definition: ndt_map.hpp:140
autoware::localization::ndt::NDTGrid::Grid
std::unordered_map< uint64_t, VoxelT > Grid
Definition: ndt_grid.hpp:43
autoware::localization::ndt::NDTGrid::ConfigPoint
std::decay_t< decltype(std::declval< Config >().get_min_point())> ConfigPoint
Definition: ndt_grid.hpp:47
time_utils.hpp
ndt_voxel.hpp
autoware::localization::ndt::SerializedNDTMapPoint::icov_yz
Real icov_yz
Definition: ndt_map.hpp:248
autoware::localization::ndt::push_back
void push_back(std::array< sensor_msgs::PointCloud2Iterator< ndt::Real >, 9U > &pc_its, const SerializedNDTMapPoint &point)
Push a SerializedNDTMapPoint into a pointcloud2 object containing the serialized map.
Definition: ndt_map.cpp:145
autoware::localization::ndt::DynamicNDTVoxel
Definition: ndt_voxel.hpp:44
autoware::localization::ndt::PC2DoubleIterator
typename std::conditional< std::is_const< PCMessageT >::value, sensor_msgs::PointCloud2ConstIterator< ndt::Real >, sensor_msgs::PointCloud2Iterator< ndt::Real > >::type PC2DoubleIterator
Definition: ndt_map.hpp:219
autoware::localization::ndt::SerializedNDTMapPoint::icov_zz
Real icov_zz
Definition: ndt_map.hpp:249
get_open_port.end
end
Definition: scripts/get_open_port.py:23
autoware::localization::ndt::DynamicNDTMap::Point
Eigen::Vector3d Point
Definition: ndt_map.hpp:56