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

@@ -153,10 +153,7 @@ DepthVisibilityResult DepthVisibilityChecker::classify(const Eigen::Vector3d& bo
{
if (!intersectsPixel(hull, u, v, margin + .5)) continue;
any_pixel = true;
double measured_z;
const auto* pixel = image.data.data() + static_cast<size_t>(v) * image.step + u * bytes_per_pixel_;
if (bytes_per_pixel_ == 2) { uint16_t z; std::memcpy(&z, pixel, 2); measured_z = z * .001; }
else { float z; std::memcpy(&z, pixel, 4); measured_z = z; }
const double measured_z = depthAt(u, v);
if (!std::isfinite(measured_z) || measured_z < config_.valid_min_z_m || measured_z > config_.valid_max_z_m)
return {DepthVisibility::Unknown, DepthVisibilityReason::InvalidDepth};
if (measured_z <= far_z + config_.free_margin_m)
@@ -164,4 +161,92 @@ DepthVisibilityResult DepthVisibilityChecker::classify(const Eigen::Vector3d& bo
}
return any_pixel ? DepthVisibilityResult{DepthVisibility::Free, DepthVisibilityReason::Free} : DepthVisibilityResult{};
}
double DepthVisibilityChecker::depthAt(int u, int v) const
{
const auto& image = data_->depth;
const auto* pixel = image.data.data() + static_cast<size_t>(v) * image.step +
static_cast<size_t>(u) * bytes_per_pixel_;
if (bytes_per_pixel_ == 2)
{
uint16_t millimeters;
std::memcpy(&millimeters, pixel, 2);
return millimeters * .001;
}
if (bytes_per_pixel_ == 4)
{
float meters;
std::memcpy(&meters, pixel, 4);
return meters;
}
return std::numeric_limits<double>::quiet_NaN();
}
namespace
{
// Pixel index of a projection; the bounds test keeps lround() away from huge values.
bool projectToPixel(const Eigen::Vector3d& camera_point, const robot_sensor_msgs::CameraInfo& ci,
int left, int right, int top, int bottom, int& u, int& v)
{
const double pu = ci.K[0] * camera_point.x() / camera_point.z() + ci.K[2];
const double pv = ci.K[4] * camera_point.y() / camera_point.z() + ci.K[5];
// round(p) in [lo, hi] <=> p in [lo - 0.5, hi + 0.5)
if (!std::isfinite(pu) || !std::isfinite(pv) || pu < left - .5 || pu >= right + .5 ||
pv < top - .5 || pv >= bottom + .5)
return false;
u = static_cast<int>(std::lround(pu));
v = static_cast<int>(std::lround(pv));
return true;
}
} // namespace
bool DepthVisibilityChecker::projectsInside(const Eigen::Vector3d& point, unsigned int border_px) const
{
if (!data_ || !point.allFinite())
return false;
const auto& image = data_->depth;
const Eigen::Vector3d p = camera_from_global_ * point;
if (!p.allFinite() || p.z() < config_.valid_min_z_m)
return false;
const int border = static_cast<int>(border_px);
int u, v;
return projectToPixel(p, data_->camera_info,
static_cast<int>(config_.clear_left_border_px) + border,
static_cast<int>(image.width) - 1 - static_cast<int>(config_.clear_right_border_px) - border,
border, static_cast<int>(image.height) - 1 - border, u, v);
}
DepthVisibilityResult DepthVisibilityChecker::classifyPoints(const std::vector<Eigen::Vector3d>& points) const
{
if (!data_ || points.empty())
return {};
const auto& image = data_->depth;
const int r = static_cast<int>(config_.projection_margin_px);
for (const auto& point : points)
{
if (!point.allFinite())
return {};
const Eigen::Vector3d p = camera_from_global_ * point;
// Same view limits as classify(): out of the trusted optical range is not seen.
if (!p.allFinite() || p.z() < config_.valid_min_z_m || p.z() > config_.valid_max_z_m)
return {};
int u, v;
if (!projectToPixel(p, data_->camera_info,
static_cast<int>(config_.clear_left_border_px) + r,
static_cast<int>(image.width) - 1 - static_cast<int>(config_.clear_right_border_px) - r,
r, static_cast<int>(image.height) - 1 - r, u, v))
return {};
for (int dv = -r; dv <= r; ++dv)
for (int du = -r; du <= r; ++du)
{
const double measured_z = depthAt(u + du, v + dv);
if (!std::isfinite(measured_z) || measured_z < config_.valid_min_z_m ||
measured_z > config_.valid_max_z_m)
return {DepthVisibility::Unknown, DepthVisibilityReason::InvalidDepth};
if (measured_z <= p.z() + config_.free_margin_m)
return {DepthVisibility::Surface, DepthVisibilityReason::Surface};
}
}
return {DepthVisibility::Free, DepthVisibilityReason::Free};
}
} // namespace robot_costmap_2d