feat(depth): certify evidence points instead of whole voxels (phase 1)

The whole-voxel rule never clears a voxel whose box crosses the image edge.
A person bending over to lift an object leaves high voxels at the top edge
of the image: in the 2026-10-15 lift bag 0 of 54 phantom cells cleared.

- DepthEvidenceStore keeps, per marked voxel, the 6 per-axis extreme points
  plus the most recent ones, keyed by a window-independent cell index.
- DepthVisibilityChecker::classifyPoints: Free only when every evidence
  point projects with a full patch inside the usable image and sees valid
  depth behind it; any surface in front keeps it, invalid depth keeps it.
- Paired marking drops points within depth_marking_border_px of the image
  edge, so every kept point can be re-checked from the pose that saw it.
- Voxels without evidence keep the whole-voxel rule; evidence is erased on
  clear, pruned on rolling shift and every 50 frames.
- New keys depth_evidence_points_enabled (default false),
  depth_evidence_points_per_voxel (8, 6..16), depth_marking_border_px
  (must be > depth_projection_margin_px when enabled); startup log and
  counters (by_evidence, border_dropped_points, evidence_voxels).
- Tests: depth_evidence_store_test; checker and layer tests for the image
  top cut, border drop, rolling shift and a still-visible object.
This commit is contained in:
2026-10-15 13:22:28 +07:00
parent 2ca0bcc1c1
commit 5e38857e48
11 changed files with 608 additions and 15 deletions

View File

@@ -0,0 +1,86 @@
#ifndef ROBOT_COSTMAP_2D_DEPTH_EVIDENCE_STORE_H_
#define ROBOT_COSTMAP_2D_DEPTH_EVIDENCE_STORE_H_
#include <Eigen/Core>
#include <array>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <unordered_map>
#include <vector>
namespace robot_costmap_2d
{
/**
* @brief Measured points that marked each voxel of a depth-only VoxelLayer.
*
* The observed_volume clearing certifies these points instead of the whole voxel box:
* the occupancy of a depth-only layer is exactly what the camera saw, so the part of a
* voxel the camera never saw carries no occupancy of its own. Keys use a
* window-independent cell index, so a rolling origin shift needs no copy; callers drop
* keys that left the window with eraseIf().
*
* Per voxel it keeps the 6 per-axis extreme points (min/max x, y, z) and fills the
* remaining slots with the most recent points.
*/
class DepthEvidenceStore
{
public:
static constexpr unsigned int kExtremeSlots = 6;
static constexpr unsigned int kMaxPoints = 16;
struct VoxelKey
{
int x;
int y;
unsigned int z;
bool operator==(const VoxelKey& other) const
{
return x == other.x && y == other.y && z == other.z;
}
};
explicit DepthEvidenceStore(unsigned int points_per_voxel = 8);
/// Clamped to [kExtremeSlots, kMaxPoints]; drops all stored evidence.
void setPointsPerVoxel(unsigned int points_per_voxel);
unsigned int pointsPerVoxel() const { return points_per_voxel_; }
/// Records a marking point [global frame, m]. fresh = true drops points left over from
/// an earlier occupancy of the voxel (it was not MARKED before this point).
void add(const VoxelKey& key, const Eigen::Vector3f& point, bool fresh);
/// Distinct evidence points of one voxel. Returns false when the voxel has none.
bool points(const VoxelKey& key, std::vector<Eigen::Vector3d>& out) const;
void erase(const VoxelKey& key);
void eraseIf(const std::function<bool(const VoxelKey&)>& predicate);
void clear();
std::size_t size() const { return entries_.size(); }
private:
struct KeyHash
{
std::size_t operator()(const VoxelKey& key) const noexcept
{
return (static_cast<std::size_t>(static_cast<uint32_t>(key.x)) * 73856093u) ^
(static_cast<std::size_t>(static_cast<uint32_t>(key.y)) * 19349663u) ^
(static_cast<std::size_t>(key.z) * 83492791u);
}
};
struct Entry
{
std::array<Eigen::Vector3f, kMaxPoints> points;
unsigned char count = 0; ///< valid slots, extremes included
unsigned char next_recent = 0; ///< ring position among the recent slots
};
unsigned int points_per_voxel_;
std::unordered_map<VoxelKey, Entry, KeyHash> entries_;
};
} // namespace robot_costmap_2d
#endif // ROBOT_COSTMAP_2D_DEPTH_EVIDENCE_STORE_H_

View File

@@ -25,7 +25,24 @@ public:
DepthVisibilityResult classify(const Eigen::Vector3d& lower,
const Eigen::Vector3d& upper) const;
/**
* @brief Certifies the measured evidence points of one voxel [global frame, m].
*
* Free only when every point projects with its projection_margin_px patch fully inside
* the usable image, every patch pixel holds valid depth, and that depth lies behind the
* point by more than free_margin_m. Any pixel in front keeps the voxel (Surface); an
* invalid pixel or a point out of view keeps it as Unknown. An empty list is Unknown.
*/
DepthVisibilityResult classifyPoints(const std::vector<Eigen::Vector3d>& points) const;
/// True when a global-frame point projects at least border_px inside the usable image
/// (the clear_left/right border strips are not usable). Used to gate evidence marking.
bool projectsInside(const Eigen::Vector3d& point, unsigned int border_px) const;
private:
/// Raw depth at a pixel [m]; NaN for an unsupported encoding. Caller checks bounds.
double depthAt(int u, int v) const;
robot_sensor_msgs::DepthCameraData::ConstPtr data_;
Eigen::Isometry3d camera_from_global_{Eigen::Isometry3d::Identity()};
double min_height_{0}, max_height_{0};

View File

@@ -92,6 +92,15 @@ struct DepthFrustumConfig
double valid_max_z_m = 3.0;
double free_margin_m = 0.02;
unsigned int projection_margin_px = 1;
/// Certify the measured evidence points of each marked voxel instead of the whole
/// voxel box. A box cut by the image edge can never be certified, while the points
/// that marked it were inside the image when they were seen.
bool evidence_points_enabled = false;
/// [points] evidence kept per voxel: 6 per-axis extremes plus the most recent ones.
unsigned int evidence_points_per_voxel = 8;
/// [px] with evidence points, marking drops points that project closer than this to
/// the usable image edge, so each kept point has a full projection patch in view.
unsigned int marking_border_px = 0;
};
class DepthCameraObservation

View File

@@ -49,6 +49,7 @@
#include <robot_sensor_msgs/PointCloud2.h>
#include <robot_sensor_msgs/point_cloud_conversion.h>
#include <robot_costmap_2d/obstacle_layer.h>
#include <robot_costmap_2d/depth_evidence_store.h>
#include <robot_voxel_grid/voxel_grid.h>
#include <limits>
@@ -57,6 +58,8 @@
namespace robot_costmap_2d
{
class DepthVisibilityChecker;
class VoxelLayer : public ObstacleLayer
{
public:
@@ -111,14 +114,35 @@ private:
double min_range, double max_range, double skip_dist,
double* min_x, double* min_y, double* max_x, double* max_y);
bool clipColumnSegment(double& sx, double& sy, double& ex, double& ey) const;
/// Paired depth marking: the checker of the same frame gates marking at the image
/// border and the store records evidence points. Null for every other source.
struct PairedMarking
{
const DepthVisibilityChecker* checker;
const DepthFrustumConfig* config;
};
void markObservation(const Observation& observation, double* min_x, double* min_y,
double* max_x, double* max_y);
double* max_x, double* max_y, const PairedMarking* paired = nullptr);
void processPairedDepth(double* min_x, double* min_y, double* max_x, double* max_y);
struct VisibilityCounts
{
uint64_t free{0}, unknown{0}, surface{0}, frames{0};
uint64_t by_evidence{0}; ///< decisions made from evidence points
uint64_t border_dropped{0}; ///< marking points dropped at the image border
} visibility_counts_;
/// Window-independent key: local cell index plus the cells the rolling origin moved.
DepthEvidenceStore::VoxelKey evidenceKey(unsigned int x, unsigned int y, unsigned int z) const
{
return {static_cast<int>(x) + evidence_offset_x_, static_cast<int>(y) + evidence_offset_y_, z};
}
/// Drops evidence of voxels that left the window or are no longer MARKED.
void pruneDepthEvidence();
DepthEvidenceStore depth_evidence_;
int evidence_offset_x_ = 0;
int evidence_offset_y_ = 0;
uint64_t frames_since_evidence_prune_ = 0;
bool publish_voxel_;
robot_voxel_grid::VoxelGrid robot_voxel_grid_;