Class VoxelHashMap¶
Defined in File voxel_hash_map.h
Class Documentation¶
-
class VoxelHashMap¶
A spatial hash map organizing 3D points, and optionally per-point attributes, into cubic voxels.
Each VoxelDownsampleStrategy is implemented by its own internal map type: a different per-voxel bucket and insertion strategy, specialized again for positions-only points or points carrying attributes. create() picks the right one and returns this proxy, which forwards every call to it. Callers choose a strategy at runtime and never name the implementation types.
FIRST_N_POINT keeps the first max_points_per_voxel points to reach a voxel, skipping any that land too close to one already kept.
LAST_N_POINT is the same, but keeps the most recently admitted max_points_per_voxel points instead: a full voxel evicts its oldest.
AVERAGE_POINT keeps each voxel’s average position and attributes.
RANDOM reservoir-samples max_points_per_voxel points per voxel.
SMALLEST / LARGEST keep the points with the smallest / largest value of one scalar attribute (see attribute_index), needing a non-empty attribute_layout.
With an empty attribute layout the positions-only implementation is used, which stores fixed-size xyz points; otherwise the attributed one.
Points are exchanged as Eigen::VectorXd of point_cols() values: xyz followed by the attributes as doubles. Reference positions (a location to trim around, a nearest-neighbor query) only need their first three values. The packed-record methods (add_points(xyz, records), pointcloud(), …) keep attributes in their own types instead, so e.g. a uint32 RANGE is never widened to double.
Each call is one virtual dispatch into the underlying map, and the per-point work stays inside it, so bulk operations cost the same as on the implementation type. get_closest_neighbor_position() is the allocation-free query for hot loops such as ICP correspondence search.
Public Types
-
using point_type = Eigen::VectorXd¶
Point type every method speaks: xyz plus the attributes as doubles.
Public Functions
-
virtual ~VoxelHashMap() = default¶
-
virtual VoxelDownsampleStrategy strategy() const = 0¶
The strategy create() selected the underlying map for.
- Returns:
The per-voxel reduction strategy.
-
virtual bool empty() const = 0¶
Check if the hash map is empty.
- Returns:
True if no voxels are present, false otherwise.
-
virtual void clear() = 0¶
Clear all voxels from the map.
-
virtual void update(const std::vector<point_type> &points, const point_type &position) = 0¶
Insert points and trim voxels that are far from
position.- Parameters:
points[in] Points of point_cols() values each.
position[in] Reference position for distance trimming (xyz first).
- Throws:
std::invalid_argument – if any point’s size differs from point_cols(); no point is inserted in that case.
-
virtual void update(const Eigen::Ref<const ArrayXXdR> &points, const point_type &position) = 0¶
Insert points from an Eigen matrix and trim voxels far from
position.The same as the std::vector overload, but zero-copy:
pointsbinds directly to whatever row-major buffer backs it (e.g. a numpy array), with no intermediate per-point copy.- Parameters:
points[in] Row-major matrix of point_cols() columns, one row per point.
position[in] Reference position for distance trimming (xyz first).
- Throws:
std::invalid_argument – if points.cols() differs from point_cols().
-
virtual void add_point(const point_type &point) = 0¶
Insert a single point into the map.
- Parameters:
point[in] The point to insert, point_cols() values.
- Throws:
std::invalid_argument – if point.size() differs from point_cols().
-
virtual void add_point(const Eigen::Ref<const Eigen::Vector3d> &xyz, const Eigen::Ref<const AttributeRecord> &record) = 0¶
Insert a single point with an already-packed attribute record.
- Parameters:
xyz[in] The point’s position.
record[in]
attribute_layout().stride()bytes, or empty when the layout has no attributes.
- Throws:
std::invalid_argument – if record.size() does not equal attribute_layout().stride().
-
virtual void add_points(const std::vector<point_type> &points) = 0¶
Insert points into the map.
- Parameters:
points[in] Points of point_cols() values each.
- Throws:
std::invalid_argument – if any point’s size differs from point_cols(); every point is checked before any is inserted.
-
virtual void add_points(const Eigen::Ref<const ArrayXXdR> &points) = 0¶
Insert points from an Eigen matrix into the map.
- Parameters:
points[in] Row-major matrix of point_cols() columns, one row per point.
- Throws:
std::invalid_argument – if points.cols() differs from point_cols().
-
virtual void add_points(const Eigen::Ref<const ArrayX3dR> &xyz, const Eigen::Ref<const AttributeRecords> &records) = 0¶
Insert points whose attributes are already packed in the map’s layout.
Nothing is converted: the
attribute_layout().stride()bytes belonging to each row are copied into the voxel as-is, so integer attributes keep their exact values and their original width.- Parameters:
xyz[in] Row-major Nx3 array of point positions.
records[in]
xyz.rows()xattribute_layout().stride()packed attribute records, one row per row ofxyz. May be empty when the layout has no attributes.
- Throws:
std::invalid_argument – if
recordsis notxyz.rows()xattribute_layout().stride().
-
inline void add_points(const std::vector<Eigen::Vector3d> &points)¶
Convenience overload for callers already holding a positions-only std::vector<Eigen::Vector3d>, e.g.
from ICPRegistration. Zero-copy: forwards through an Eigen::Map to add_points(ArrayXXdR), no intermediate copy of
pointsis made.- Parameters:
points[in] The points to insert.
- Throws:
std::invalid_argument – if points.cols() (always 3 here) differs from point_cols(), i.e. if this map carries attributes.
-
virtual void add_points(const AttributedPointCloud &cloud) = 0¶
Insert a pointcloud read back from a map, e.g.
by pointcloud().
- Parameters:
cloud[in] Positions, packed records and the layout describing them.
- Throws:
std::invalid_argument – if
cloud.layoutdiffers from attribute_layout(), or if the records are notcloud.xyz.rows()xattribute_layout().stride().
-
virtual AttributedPointCloud pointcloud() const = 0¶
Get the complete pointcloud, with attributes in their original types.
Use
.xyzfor just the positions, and AttributedPointCloud’s typed accessors (e.g.attribute<std::uint32_t>(0)) for the attributes. A map without an attribute layout returns zero-width records.- Returns:
Positions plus packed attribute records; see AttributedPointCloud.
-
virtual AttributedPointCloud extract_voxels_far_from_location(const point_type &origin) = 0¶
Remove voxels far from a specified location and return their points.
The same as remove_voxels_far_from_location(), but returns what it removed in the form pointcloud() uses, attributes in their original types.
- Parameters:
origin[in] Location to measure distance from (xyz first).
- Returns:
Positions plus packed attribute records of the extracted voxels.
-
virtual void remove_voxels_far_from_location(const point_type &origin) = 0¶
Remove voxels far from a specified location.
- Parameters:
origin[in] Location to measure distance from (xyz first).
-
virtual std::vector<point_type> pointcloud_vector() const = 0¶
Get a complete pointcloud as a vector.
- Returns:
Every point of every voxel, point_cols() values each.
-
virtual std::tuple<point_type, double> get_closest_neighbor(const point_type &query, double max_distance_sq) const = 0¶
Find the closest point in the map to a query point.
Only the query’s voxel and its 26 neighbors are searched, and only within
8 * voxel_size^2(squared): the effective radius ismin(max_distance_sq, 8 * voxel_size^2). A point outside that neighborhood is never returned, even if it is the closest in the map.- Parameters:
query[in] The query point (xyz first).
max_distance_sq[in] Maximum squared distance to search.
- Returns:
Tuple of (closest_point, squared_distance). If no point is found within the effective radius, squared_distance will be >= max_distance_sq.
-
inline std::tuple<point_type, double> get_closest_neighbor(const point_type &query) const¶
Find the closest point in the map to a query point, with no limit beyond the neighborhood bound described above (the 27 voxels around
query, within8 * voxel_size^2squared).- Parameters:
query[in] The query point (xyz first).
- Returns:
Tuple of (closest_point, squared_distance). If nothing lies in that neighborhood, squared_distance is
std::numeric_limits<double>::max().
-
virtual std::pair<Eigen::Vector3d, double> get_closest_neighbor_position(const Eigen::Vector3d &query, double max_distance_sq) const = 0¶
Find the position of the closest point in the map to
query.Same search as get_closest_neighbor(), but returns only the position, decodes no attributes and allocates nothing, whatever the strategy or attribute layout, so it suits being called per point from a hot (e.g. parallel ICP) loop.
- Parameters:
query[in] The query position.
max_distance_sq[in] Maximum squared distance to search, bounded as for get_closest_neighbor().
- Returns:
(closest_position, squared_distance). If no point is found within the effective radius, squared_distance will be >= max_distance_sq.
-
virtual Eigen::Index point_cols() const = 0¶
Returns the row width of a serialized point.
- Returns:
3 plus the attribute layout’s element count.
-
virtual const AttributeLayout &attribute_layout() const = 0¶
Types and widths of the per-point attributes stored in this map.
- Returns:
The attribute layout; empty when the map stores positions only.
-
virtual VHMInternalStorageMode storage_mode() const = 0¶
Returns the internal storage representation of points stored in the voxels.
- Returns:
Internal storage mode (never AUTO: the mode AUTO resolved to)
-
virtual std::size_t max_points_per_voxel() const = 0¶
Return the maximum number of points allowed per voxel.
- Returns:
Maximum points per voxel as set at construction.
-
virtual std::size_t min_points_per_voxel() const = 0¶
Return the minimum number of raw insertions required for a voxel to appear in the output.
- Returns:
Minimum points threshold as set at construction.
-
virtual double voxel_size() const = 0¶
The voxel size.
- Returns:
The voxel size
-
virtual double max_distance() const = 0¶
Maximum distance of points to retain.
- Returns:
The maximum distance
Public Static Functions
-
static std::unique_ptr<VoxelHashMap> create(double voxel_size, std::size_t max_points_per_voxel = DEFAULT_MAX_POINTS_PER_VOXEL, std::size_t min_points_per_voxel = DEFAULT_MIN_POINTS_PER_VOXEL, VoxelDownsampleStrategy strategy = DEFAULT_STRATEGY, AttributeLayout attribute_layout = AttributeLayout(), double max_distance = DEFAULT_MAX_DISTANCE, VHMInternalStorageMode internal_storage_mode = DEFAULT_INTERNAL_STORAGE_MODE, std::size_t attribute_index = 0)¶
Build the map implementing
strategy.The parameters match voxel_downsample_xd(), in the same order and with the same defaults, except that create() takes a max_distance where voxel_downsample_xd() takes the attribute records.
- Parameters:
voxel_size[in] Edge length of each cubic voxel.
max_points_per_voxel[in] Maximum points kept per voxel (FIRST_N_POINT, LAST_N_POINT, RANDOM, SMALLEST, LARGEST).
min_points_per_voxel[in] Minimum insertions before a voxel contributes.
strategy[in] The VoxelDownsampleStrategy selecting the per-voxel reduction.
attribute_layout[in] Types and widths of the per-point attributes; empty for positions only. SMALLEST and LARGEST need a non-empty layout — there is nothing to rank positions-only points by.
max_distance[in] Maximum distance to retain points from a reference.
internal_storage_mode[in] How points are stored inside the voxels. AUTO picks fixed point for small voxels, which returns kept positions within a small fraction of the voxel size of the input; ABS returns them exactly.
attribute_index[in] Index into
attribute_layoutof the scalar attribute to rank by; ignored unlessstrategyis SMALLEST or LARGEST, where it must name a scalar attribute.
- Throws:
std::invalid_argument – for an unknown
strategy, a non-positivevoxel_sizeormax_distance, a zeromax_points_per_voxel, an emptyattribute_layoutwithstrategySMALLEST or LARGEST, or (same two strategies) anattribute_indexthat does not name a scalar attribute ofattribute_layout.- Returns:
The proxy, owning the selected map.
Public Static Attributes
-
static std::size_t DEFAULT_MAX_POINTS_PER_VOXEL = 1¶
Default max_points_per_voxel configuration.
-
static std::size_t DEFAULT_MIN_POINTS_PER_VOXEL = 1¶
Default min_points_per_voxel configuration.
-
static VoxelDownsampleStrategy DEFAULT_STRATEGY = VoxelDownsampleStrategy::RANDOM¶
Default strategy configuration.
-
static double DEFAULT_MAX_DISTANCE = 100.0¶
Default max_distance configuration.
-
static VHMInternalStorageMode DEFAULT_INTERNAL_STORAGE_MODE = VHMInternalStorageMode::AUTO¶
Default internal_storage_mode configuration.