Go to the documentation of this file.
20 #ifndef GEOMETRY__SPATIAL_HASH_HPP_
21 #define GEOMETRY__SPATIAL_HASH_HPP_
27 #include <unordered_map>
40 namespace spatial_hash
50 template<
typename Po
intT,
typename ConfigT>
56 std::is_same<ConfigT, Config2d>::value || std::is_same<ConfigT, Config3d>::value,
57 "SpatialHash only works with Config2d or Config3d");
60 using Hash = std::unordered_multimap<Index, PointT>;
61 using IT =
typename Hash::const_iterator;
70 : m_iterator(iterator),
78 return m_iterator->second;
88 operator const PointT &()
const
96 return get_iterator();
128 if (size() >= capacity()) {
129 throw std::length_error{
"SpatialHash: Cannot insert past capacity"};
131 return insert_impl(pt);
140 template<
typename IteratorT>
144 if ((size() + std::distance(begin,
end)) > capacity()) {
145 throw std::length_error{
"SpatialHash: Cannot multi-insert past capacity"};
147 for (IteratorT it = begin; it !=
end; ++it) {
148 (void)insert_impl(*it);
161 return m_hash.size();
167 return m_config.get_capacity();
173 return m_hash.empty();
179 return m_hash.begin();
213 return m_neighbors_found;
232 const Index3 ref_idx = m_config.index3(
x,
y, z);
234 Index3 idx = idx_range.first;
240 if (m_config.is_candidate_bin(ref_idx, idx)) {
242 const Index jdx = m_config.index(idx);
243 const auto range = m_hash.equal_range(jdx);
244 for (
auto it = range.first; it != range.second; ++it) {
245 const auto & pt = it->second;
246 const float32_t dist2 = m_config.distance_squared(
x,
y, z, pt);
247 if (dist2 <= m_config.radius2()) {
249 m_neighbors.emplace_back(it, sqrtf(dist2));
253 }
while (m_config.next_bin(idx_range, idx));
255 m_neighbors_found += m_neighbors.size();
262 GEOMETRY_LOCAL IT insert_impl(
const PointT & pt)
268 return m_hash.insert(std::make_pair(idx, pt));
271 const ConfigT m_config;
273 OutputVector m_neighbors;
275 Index m_neighbors_found;
282 template<
typename Po
intT,
typename ConfigT>
287 template<
typename Po
intT>
304 return this->near_impl(
x,
y, 0.0F);
320 template<
typename Po
intT>
339 return this->near_impl(
x,
y, z);
362 #endif // GEOMETRY__SPATIAL_HASH_HPP_
Wrapper around an iterator and a distance (from some query point)
Definition: spatial_hash.hpp:63
IT cend() const
Get iterator to end of data structure.
Definition: spatial_hash.hpp:195
IT begin() const
Get iterator to beginning of data structure.
Definition: spatial_hash.hpp:177
std::unordered_multimap< Index, PointT > Hash
Definition: spatial_hash.hpp:60
auto x_(const PointT &pt)
Gets the x value for a point.
Definition: common_2d.hpp:47
const OutputVector & near(const PointT &pt)
Finds all points within a fixed radius of a reference point.
Definition: spatial_hash.hpp:347
This file includes common type definition.
DifferentialState y
Definition: kinematic_bicycle.snippet.hpp:28
Index size() const
Get current number of element stored in this data structure.
Definition: spatial_hash.hpp:159
typename Hash::const_iterator IT
Definition: spatial_hash.hpp:61
typename std::vector< Output > OutputVector
Definition: spatial_hash.hpp:109
const OutputVector & near(const PointT &pt)
Finds all points within a fixed radius of a reference point.
Definition: spatial_hash.hpp:312
IT cbegin() const
Get iterator to beginning of data structure.
Definition: spatial_hash.hpp:189
Configuration class for a 2d spatial hash.
Definition: spatial_hash_config.hpp:342
SpatialHash(const Config2d &cfg)
Definition: spatial_hash.hpp:293
float32_t get_distance() const
Get distance to reference point.
Definition: spatial_hash.hpp:100
Index capacity() const
Get the maximum capacity of the data structure.
Definition: spatial_hash.hpp:165
IT get_iterator() const
Get underlying iterator.
Definition: spatial_hash.hpp:82
Output(const IT iterator, const float32_t distance)
Constructor.
Definition: spatial_hash.hpp:69
IT insert(const PointT &pt)
Inserts point.
Definition: spatial_hash.hpp:126
const OutputVector & near_impl(const float32_t x, const float32_t y, const float32_t z)
Finds all points within a fixed radius of a reference point.
Definition: spatial_hash.hpp:224
const OutputVector & near(const float32_t x, const float32_t y)
Finds all points within a fixed radius of a reference point.
Definition: spatial_hash.hpp:302
void clear()
Reset the state of the data structure.
Definition: spatial_hash.hpp:153
bool bool8_t
Definition: types.hpp:39
typename SpatialHashBase< PointT, Config2d >::OutputVector OutputVector
Definition: spatial_hash.hpp:291
Internal struct for packing three indices together.
Definition: spatial_hash_config.hpp:59
This file defines the lanelet2_map_provider_node class.
Definition: quick_sort.hpp:24
typename SpatialHashBase< PointT, Config3d >::OutputVector OutputVector
Definition: spatial_hash.hpp:324
Index bins_hit() const
Get the number of bins touched during the lifetime of this object, for debugging and size tuning.
Definition: spatial_hash.hpp:203
class GEOMETRY_PUBLIC SpatialHash
The class to be used for specializing on apex_app::common::geometry::spatial_hash::SpatialHashBase to...
Definition: spatial_hash.hpp:283
autoware::common::geometry::spatial_hash::SpatialHash2d< PointXYZII > Hash
Definition: euclidean_cluster.hpp:87
DifferentialState x
Definition: kinematic_bicycle.snippet.hpp:28
Index neighbors_found() const
Get number of near neighbors found during the lifetime of this object, for debugging and size tuning.
Definition: spatial_hash.hpp:211
void insert(IteratorT begin, IteratorT end)
Inserts a range of points.
Definition: spatial_hash.hpp:141
An implementation of the spatial hash or integer lattice data structure for efficient (O(1)) near nei...
Definition: spatial_hash.hpp:51
bool8_t empty() const
Whether the hash is empty.
Definition: spatial_hash.hpp:171
SpatialHashBase(const ConfigT &cfg)
Constructor.
Definition: spatial_hash.hpp:113
float float32_t
Definition: types.hpp:45
SpatialHash< T, Config3d > SpatialHash3d
Definition: spatial_hash.hpp:356
SpatialHash(const Config3d &cfg)
Definition: spatial_hash.hpp:326
SpatialHash< T, Config2d > SpatialHash2d
Definition: spatial_hash.hpp:354
Configuration class for a 3d spatial hash.
Definition: spatial_hash_config.hpp:404
auto y_(const PointT &pt)
Gets the y value for a point.
Definition: common_2d.hpp:56
This file implements a spatial hash for efficient fixed-radius near neighbor queries in 2D.
auto z_(const PointT &pt)
Gets the z value for a point.
Definition: common_2d.hpp:65
std::pair< Index3, Index3 > BinRange
Definition: spatial_hash_config.hpp:66
const OutputVector & near(const float32_t x, const float32_t y, const float32_t z)
Finds all points within a fixed radius of a reference point.
Definition: spatial_hash.hpp:337
end
Definition: scripts/get_open_port.py:23
const PointT & get_point() const
Get stored point.
Definition: spatial_hash.hpp:76
IT end() const
Get iterator to end of data structure.
Definition: spatial_hash.hpp:183
std::size_t Index
Index type for identifying bins of the hash/lattice.
Definition: spatial_hash_config.hpp:49