clearArea and footprint clearing only wrote the 2D cells, so the voxels and evidence points stayed MARKED and the next column update brought the phantom back. VoxelLayer now frees the voxel column and its evidence in both paths (footprint only for observed_volume; legacy unchanged). A valid return deeper than depth_valid_max_z_m kept every evidence point unknown, so phantoms with nothing within 3 m behind never cleared. New key depth_far_return_is_behind (default false) counts such a return as behind a point that lies at least free_margin_m inside the trusted range. Replay of the 2026-10-15 bags: real phantoms 62 -> 53, static-cell dropouts unchanged, object kept on 10/11 FOV exits as before.
256 lines
12 KiB
C++
256 lines
12 KiB
C++
#include <robot_costmap_2d/depth_visibility.h>
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <limits>
|
|
|
|
namespace robot_costmap_2d
|
|
{
|
|
namespace
|
|
{
|
|
double cross(const Eigen::Vector2d& a, const Eigen::Vector2d& b, const Eigen::Vector2d& c)
|
|
{
|
|
const auto x = b - a, y = c - a;
|
|
return x.x() * y.y() - x.y() * y.x();
|
|
}
|
|
std::vector<Eigen::Vector2d> convexHull(std::vector<Eigen::Vector2d> points)
|
|
{
|
|
std::sort(points.begin(), points.end(), [](const auto& a, const auto& b) {
|
|
return a.x() < b.x() || (a.x() == b.x() && a.y() < b.y());
|
|
});
|
|
points.erase(std::unique(points.begin(), points.end(), [](const auto& a, const auto& b) {
|
|
return a.x() == b.x() && a.y() == b.y();
|
|
}), points.end());
|
|
std::vector<Eigen::Vector2d> hull;
|
|
for (const auto& p : points)
|
|
{
|
|
while (hull.size() >= 2 && cross(hull[hull.size()-2], hull.back(), p) <= 0) hull.pop_back();
|
|
hull.push_back(p);
|
|
}
|
|
const size_t lower_size = hull.size();
|
|
for (auto it = points.rbegin() + (points.empty() ? 0 : 1); it != points.rend(); ++it)
|
|
{
|
|
while (hull.size() > lower_size && cross(hull[hull.size()-2], hull.back(), *it) <= 0) hull.pop_back();
|
|
hull.push_back(*it);
|
|
}
|
|
if (!hull.empty()) hull.pop_back();
|
|
return hull;
|
|
}
|
|
// SAT tests the full pixel cell expanded by calibration margin, not only its center.
|
|
bool intersectsPixel(const std::vector<Eigen::Vector2d>& polygon, int u, int v, double half)
|
|
{
|
|
const Eigen::Vector2d center(u, v);
|
|
for (int dimension = 0; dimension < 2; ++dimension)
|
|
{
|
|
double lo = std::numeric_limits<double>::infinity(), hi = -lo;
|
|
for (const auto& p : polygon) { lo = std::min(lo, p[dimension]); hi = std::max(hi, p[dimension]); }
|
|
if (center[dimension] + half < lo - 1e-9 || center[dimension] - half > hi + 1e-9) return false;
|
|
}
|
|
for (size_t i = 0; i < polygon.size(); ++i)
|
|
{
|
|
const auto edge = polygon[(i+1) % polygon.size()] - polygon[i];
|
|
const Eigen::Vector2d axis(-edge.y(), edge.x());
|
|
double lo = std::numeric_limits<double>::infinity(), hi = -lo;
|
|
for (const auto& p : polygon) { const double d = axis.dot(p); lo = std::min(lo, d); hi = std::max(hi, d); }
|
|
const double c = axis.dot(center), r = half * (std::abs(axis.x()) + std::abs(axis.y()));
|
|
if (c + r < lo - 1e-9 || c - r > hi + 1e-9) return false;
|
|
}
|
|
return true;
|
|
}
|
|
} // namespace
|
|
|
|
bool DepthVisibilityChecker::prepare(robot_sensor_msgs::DepthCameraData::ConstPtr data,
|
|
const tf3::TransformStampedMsg& pose, double min_height, double max_height,
|
|
const DepthFrustumConfig& config)
|
|
{
|
|
data_.reset();
|
|
if (!data || !std::isfinite(min_height) || !std::isfinite(max_height) || min_height > max_height ||
|
|
!std::isfinite(config.valid_min_z_m) || !std::isfinite(config.valid_max_z_m) ||
|
|
config.valid_min_z_m <= 0 || config.valid_max_z_m <= config.valid_min_z_m ||
|
|
!std::isfinite(config.free_margin_m) || config.free_margin_m < 0)
|
|
return false;
|
|
const auto& image = data->depth;
|
|
const auto& ci = data->camera_info;
|
|
bytes_per_pixel_ = image.encoding == "16UC1" || image.encoding == "mono16" ? 2u : image.encoding == "32FC1" ? 4u : 0u;
|
|
if (!bytes_per_pixel_ || image.is_bigendian || !image.width || !image.height ||
|
|
image.width > static_cast<unsigned>(std::numeric_limits<int>::max()) ||
|
|
image.height > static_cast<unsigned>(std::numeric_limits<int>::max()) ||
|
|
image.step < static_cast<size_t>(image.width) * bytes_per_pixel_ ||
|
|
image.data.size() < static_cast<size_t>(image.step) * image.height ||
|
|
ci.width != image.width || ci.height != image.height || image.header.stamp.isZero() ||
|
|
image.header.stamp != ci.header.stamp || image.header.stamp != data->header.stamp ||
|
|
pose.header.stamp != tf3::Time(image.header.stamp.sec, image.header.stamp.nsec) ||
|
|
image.header.frame_id.empty() || image.header.frame_id != ci.header.frame_id ||
|
|
image.header.frame_id != data->header.frame_id || image.header.frame_id != pose.child_frame_id ||
|
|
pose.header.frame_id.empty() || ci.binning_x > 1 || ci.binning_y > 1 ||
|
|
ci.roi.x_offset || ci.roi.y_offset || ci.roi.width || ci.roi.height)
|
|
return false;
|
|
for (double value : ci.K) if (!std::isfinite(value)) return false;
|
|
for (double value : ci.P) if (!std::isfinite(value)) return false;
|
|
if (ci.K[0] <= 0 || ci.K[4] <= 0 || std::abs(ci.K[1]) > 1e-6 ||
|
|
std::abs(ci.K[3]) > 1e-6 || std::abs(ci.K[6]) > 1e-6 ||
|
|
std::abs(ci.K[7]) > 1e-6 || std::abs(ci.K[8] - 1) > 1e-6 ||
|
|
std::abs(ci.P[0] - ci.K[0]) > 1e-6 || std::abs(ci.P[5] - ci.K[4]) > 1e-6 ||
|
|
std::abs(ci.P[2] - ci.K[2]) > 1e-6 || std::abs(ci.P[6] - ci.K[5]) > 1e-6 ||
|
|
std::abs(ci.P[3]) > 1e-6 || std::abs(ci.P[7]) > 1e-6) return false;
|
|
const auto& q = pose.transform.rotation;
|
|
const auto& t = pose.transform.translation;
|
|
Eigen::Quaterniond rotation(q.w, q.x, q.y, q.z);
|
|
if (!rotation.coeffs().allFinite() || std::abs(rotation.norm() - 1.0) > .01 ||
|
|
!std::isfinite(t.x) || !std::isfinite(t.y) || !std::isfinite(t.z)) return false;
|
|
Eigen::Isometry3d global_from_camera = Eigen::Isometry3d::Identity();
|
|
global_from_camera.linear() = rotation.normalized().toRotationMatrix();
|
|
global_from_camera.translation() = Eigen::Vector3d(t.x, t.y, t.z);
|
|
camera_from_global_ = global_from_camera.inverse();
|
|
min_height_ = min_height; max_height_ = max_height; config_ = config;
|
|
data_ = std::move(data);
|
|
return true;
|
|
}
|
|
|
|
DepthVisibilityResult DepthVisibilityChecker::classify(const Eigen::Vector3d& box_lower,
|
|
const Eigen::Vector3d& box_upper) const
|
|
{
|
|
if (!data_ || !box_lower.allFinite() || !box_upper.allFinite()) return {};
|
|
Eigen::Vector3d lower = box_lower, upper = box_upper;
|
|
lower.z() = std::max(lower.z(), min_height_); upper.z() = std::min(upper.z(), max_height_);
|
|
if (upper.z() < lower.z() || upper.x() <= lower.x() || upper.y() <= lower.y())
|
|
return {DepthVisibility::Unknown, DepthVisibilityReason::EmptyRegion};
|
|
const auto& image = data_->depth;
|
|
const auto& ci = data_->camera_info;
|
|
std::vector<Eigen::Vector2d> corners;
|
|
double far_z = 0;
|
|
for (int i = 0; i < 8; ++i)
|
|
{
|
|
const Eigen::Vector3d world(i & 1 ? upper.x() : lower.x(), i & 2 ? upper.y() : lower.y(), i & 4 ? upper.z() : lower.z());
|
|
const auto p = camera_from_global_ * world;
|
|
if (!p.allFinite() || p.z() < config_.valid_min_z_m || p.z() > config_.valid_max_z_m) return {};
|
|
far_z = std::max(far_z, p.z());
|
|
corners.emplace_back(ci.K[0] * p.x() / p.z() + ci.K[2], ci.K[4] * p.y() / p.z() + ci.K[5]);
|
|
}
|
|
const auto hull = convexHull(corners);
|
|
if (hull.size() < 3) return {};
|
|
double min_u = image.width, max_u = -1, min_v = image.height, max_v = -1;
|
|
for (const auto& p : hull)
|
|
{
|
|
min_u = std::min(min_u, p.x()); max_u = std::max(max_u, p.x());
|
|
min_v = std::min(min_v, p.y()); max_v = std::max(max_v, p.y());
|
|
}
|
|
const double margin = config_.projection_margin_px;
|
|
// Reject unbounded projections before converting to signed pixel indices.
|
|
if (!std::isfinite(min_u) || !std::isfinite(max_u) || !std::isfinite(min_v) || !std::isfinite(max_v) ||
|
|
min_u - margin - .5 < 0 || max_u + margin + .5 >= image.width ||
|
|
min_v - margin - .5 < 0 || max_v + margin + .5 >= image.height) return {};
|
|
const int left = static_cast<int>(std::floor(min_u - margin - .5));
|
|
const int right = static_cast<int>(std::ceil(max_u + margin + .5));
|
|
const int top = static_cast<int>(std::floor(min_v - margin - .5));
|
|
const int bottom = static_cast<int>(std::ceil(max_v + margin + .5));
|
|
if (left < static_cast<int>(config_.clear_left_border_px) ||
|
|
right >= static_cast<int>(image.width) - static_cast<int>(config_.clear_right_border_px) ||
|
|
top < 0 || bottom >= static_cast<int>(image.height)) return {};
|
|
bool any_pixel = false;
|
|
for (int v = top; v <= bottom; ++v)
|
|
for (int u = left; u <= right; ++u)
|
|
{
|
|
if (!intersectsPixel(hull, u, v, margin + .5)) continue;
|
|
any_pixel = true;
|
|
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)
|
|
return {DepthVisibility::Surface, DepthVisibilityReason::Surface};
|
|
}
|
|
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 (config_.far_return_is_behind && std::isfinite(measured_z) &&
|
|
measured_z > config_.valid_max_z_m && p.z() + config_.free_margin_m < config_.valid_max_z_m)
|
|
continue;
|
|
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
|