refactor(depth): keep DepthCameraObstacleLayer, drop the legacy depth clearing

Two depth-image clearing designs lived in this package: the frustum ray
clearing inherited from dev_cam_intel (DepthCameraData sources of
ObstacleLayer/VoxelLayer, frustum_* and column_* keys) and the observed_volume
evidence clearing (ROC) on top of it. Both are replaced on the robot by
DepthCameraObstacleLayer, a port of costmap_depth_camera that takes the
filtered PointCloud2 directly, so both are removed and the tree is rebuilt
from main (a63c084) plus the plugin.

- observation.h / observation_buffer: DepthFrustumConfig, DepthCameraObservation
  and the depth buffering API are gone; buffers hold point clouds only.
- ObstacleLayer/VoxelLayer: no frustum ray casting, column clearing or depth
  ray cache. A DepthCameraData source or frustum_clearing_enabled: true now
  fails at startup with a message pointing to DepthCameraObstacleLayer
  instead of silently dropping the camera; DepthCameraData reaching the layer
  through the gateway is ignored.
- copyParentParameters no longer forwards the frustum_* / column_* keys.
- ROC files (depth_evidence_store, depth_visibility, depth_frame_pairer) and
  their tests are not carried over; CloudProcessingConfig goes with them.
- PCL is needed by the plugin library only. The plugin test moves under
  BUILD_COSTMAP_TESTS; BUILD_DEPTH_MARKING_TESTS is gone.
- README documents DepthCameraObstacleLayer and its known limits.

The plugin code is unchanged from the version running on the KIT robot.
This commit is contained in:
2026-10-20 09:47:25 +07:00
parent 9a67eee148
commit b23ff27a3c
20 changed files with 222 additions and 3768 deletions

View File

@@ -180,28 +180,6 @@ TEST(CostmapPerformanceRegression, observation_copy_shares_cloud_payload)
EXPECT_EQ(copied.cloud_->data, cloud.data);
}
TEST(CostmapPerformanceRegression, latest_depth_frame_is_consumed_once)
{
// tf3::BufferCore tf_buffer(tf3::Duration(10.0));
// ObservationBuffer buffer(
// "/camera/depth/data", 0.0, 0.5, 0.0, 2.0, 2.5, 3.0,
// 8, 0.2, 3.0, tf_buffer, "odom", "", 0.2);
// robot_sensor_msgs::DepthCameraData::ConstPtr depth =
// boost::make_shared<robot_sensor_msgs::DepthCameraData>();
// buffer.bufferDepthCamera(depth);
// std::vector<DepthCameraObservation> first_snapshot;
// buffer.getDepthObservations(first_snapshot);
// ASSERT_EQ(first_snapshot.size(), 1u);
// EXPECT_EQ(first_snapshot.front().data_, depth.get());
// EXPECT_EQ(first_snapshot.front().topic_, "/camera/depth/data");
// std::vector<DepthCameraObservation> second_snapshot;
// buffer.getDepthObservations(second_snapshot);
// EXPECT_TRUE(second_snapshot.empty());
}
TEST(CostmapPerformanceRegression, inflation_buckets_preserve_radial_costs)
{
ASSERT_EQ(setenv("PNKX_NAV_CORE_CONFIG_DIR", ROBOT_COSTMAP_2D_DIR, 1), 0);

View File

@@ -1,110 +0,0 @@
#include <robot_costmap_2d/depth_evidence_store.h>
#include <gtest/gtest.h>
#include <algorithm>
#include <cmath>
using robot_costmap_2d::DepthEvidenceStore;
namespace
{
bool contains(const std::vector<Eigen::Vector3d>& points, const Eigen::Vector3d& point)
{
return std::any_of(points.begin(), points.end(),
[&](const Eigen::Vector3d& p) { return (p - point).norm() < 1e-6; });
}
} // namespace
TEST(DepthEvidenceStore, FirstPointIsTheOnlyEvidence)
{
DepthEvidenceStore store(8);
std::vector<Eigen::Vector3d> points;
EXPECT_FALSE(store.points({1, 2, 3}, points));
store.add({1, 2, 3}, Eigen::Vector3f(1.f, 2.f, .3f), false);
ASSERT_TRUE(store.points({1, 2, 3}, points));
ASSERT_EQ(points.size(), 1u);
EXPECT_TRUE(contains(points, Eigen::Vector3d(1.0, 2.0, 0.3)));
EXPECT_FALSE(store.points({1, 2, 4}, points));
}
TEST(DepthEvidenceStore, KeepsAxisExtremesWhenFull)
{
DepthEvidenceStore store(6); // extremes only, no recent slots
const DepthEvidenceStore::VoxelKey key{0, 0, 0};
store.add(key, Eigen::Vector3f(.02f, .02f, .02f), false);
store.add(key, Eigen::Vector3f(.00f, .03f, .03f), false); // new min x
store.add(key, Eigen::Vector3f(.04f, .01f, .01f), false); // new max x, min y, min z
store.add(key, Eigen::Vector3f(.02f, .02f, .02f), false); // inside: changes nothing
std::vector<Eigen::Vector3d> points;
ASSERT_TRUE(store.points(key, points));
EXPECT_TRUE(contains(points, Eigen::Vector3d(.00, .03, .03)));
EXPECT_TRUE(contains(points, Eigen::Vector3d(.04, .01, .01)));
EXPECT_LE(points.size(), 6u);
}
TEST(DepthEvidenceStore, RecentSlotsRotate)
{
DepthEvidenceStore store(7); // one recent slot
const DepthEvidenceStore::VoxelKey key{0, 0, 0};
store.add(key, Eigen::Vector3f(.0f, .0f, .0f), false);
store.add(key, Eigen::Vector3f(.04f, .04f, .04f), false);
store.add(key, Eigen::Vector3f(.01f, .02f, .03f), false);
store.add(key, Eigen::Vector3f(.03f, .02f, .01f), false);
std::vector<Eigen::Vector3d> points;
ASSERT_TRUE(store.points(key, points));
EXPECT_TRUE(contains(points, Eigen::Vector3d(.03, .02, .01)));
EXPECT_FALSE(contains(points, Eigen::Vector3d(.01, .02, .03)));
}
TEST(DepthEvidenceStore, FreshVoxelDropsOldEvidence)
{
DepthEvidenceStore store(8);
const DepthEvidenceStore::VoxelKey key{5, -3, 2};
store.add(key, Eigen::Vector3f(1.f, 1.f, .2f), false);
store.add(key, Eigen::Vector3f(1.04f, 1.04f, .24f), true);
std::vector<Eigen::Vector3d> points;
ASSERT_TRUE(store.points(key, points));
ASSERT_EQ(points.size(), 1u);
EXPECT_TRUE(contains(points, Eigen::Vector3d(1.04, 1.04, .24)));
}
TEST(DepthEvidenceStore, EraseEraseIfClearAndNonFinite)
{
DepthEvidenceStore store(8);
store.add({0, 0, 0}, Eigen::Vector3f(0.f, 0.f, 0.f), false);
store.add({1, 0, 0}, Eigen::Vector3f(.05f, 0.f, 0.f), false);
store.add({2, 0, 0}, Eigen::Vector3f(.1f, 0.f, 0.f), false);
store.add({3, 0, 0}, Eigen::Vector3f(std::nanf(""), 0.f, 0.f), false);
EXPECT_EQ(store.size(), 3u);
store.erase({0, 0, 0});
EXPECT_EQ(store.size(), 2u);
store.eraseIf([](const DepthEvidenceStore::VoxelKey& key) { return key.x == 2; });
std::vector<Eigen::Vector3d> points;
EXPECT_TRUE(store.points({1, 0, 0}, points));
EXPECT_FALSE(store.points({2, 0, 0}, points));
store.clear();
EXPECT_EQ(store.size(), 0u);
}
TEST(DepthEvidenceStore, MissCountRestartsOnMarkingAndLeavesWithTheVoxel)
{
DepthEvidenceStore store(8);
const DepthEvidenceStore::VoxelKey key{1, 2, 3};
EXPECT_FLOAT_EQ(store.addMiss(key, 1.f), 0.f); // no evidence, nothing to count
store.add(key, Eigen::Vector3f(1.f, 2.f, .3f), false);
EXPECT_FLOAT_EQ(store.addMiss(key, 1.f), 1.f);
EXPECT_FLOAT_EQ(store.addMiss(key, .5f), 1.5f);
store.add(key, Eigen::Vector3f(1.01f, 2.f, .3f), false); // marked again
EXPECT_FLOAT_EQ(store.addMiss(key, 1.f), 1.f);
store.resetMiss(key);
EXPECT_FLOAT_EQ(store.addMiss(key, 1.f), 1.f);
store.erase(key);
EXPECT_FLOAT_EQ(store.addMiss(key, 1.f), 0.f);
}
TEST(DepthEvidenceStore, CapacityIsClamped)
{
EXPECT_EQ(DepthEvidenceStore(2).pointsPerVoxel(), DepthEvidenceStore::kExtremeSlots);
EXPECT_EQ(DepthEvidenceStore(100).pointsPerVoxel(), DepthEvidenceStore::kMaxPoints);
}

View File

@@ -1,277 +0,0 @@
#include <robot_costmap_2d/observation_buffer.h>
#include <robot_costmap_2d/voxel_layer.h>
#include <robot_costmap_2d/costmap_2d_robot.h>
#include <robot_sensor_msgs/point_cloud2_iterator.h>
#include <gtest/gtest.h>
#include <array>
#include <limits>
#include <filesystem>
#include <cstdlib>
namespace
{
using robot_costmap_2d::CloudProcessingConfig;
using robot_costmap_2d::Observation;
using robot_costmap_2d::ObservationBuffer;
using Points = std::vector<std::array<float, 3>>;
robot_sensor_msgs::PointCloud2 cloud(const Points &points)
{
robot_sensor_msgs::PointCloud2 out;
out.height = 1;
out.header.frame_id = "camera";
out.header.stamp = robot::Time(10, 123456789);
out.header.seq = 9;
robot_sensor_msgs::PointCloud2Modifier m(out);
m.setPointCloud2FieldsByString(1, "xyz");
m.resize(points.size());
robot_sensor_msgs::PointCloud2Iterator<float> x(out, "x"), y(out, "y"), z(out, "z");
for (const auto &p : points)
{
*x = p[0]; *y = p[1]; *z = p[2];
++x; ++y; ++z;
}
return out;
}
void transform(tf3::BufferCore &tf, const std::string &child, double x, double z,
uint32_t sec = 10, bool fixed = true)
{
tf3::TransformStampedMsg t;
t.header.frame_id = "odom";
t.header.stamp = tf3::Time(sec, 123456789);
t.child_frame_id = child;
t.transform.translation.x = x;
t.transform.translation.z = z;
ASSERT_TRUE(tf.setTransform(t, "test", fixed));
}
CloudProcessingConfig config(bool voxel = true)
{
CloudProcessingConfig c;
c.transform_at_stamp = true;
c.voxel_filter_enabled = voxel;
return c;
}
Points output(ObservationBuffer &buffer, Observation *observation = nullptr)
{
std::vector<Observation> observations;
buffer.getObservations(observations);
if (observations.empty()) return {};
if (observation) *observation = observations.front();
const auto &c = *observations.front().cloud_;
Points points;
robot_sensor_msgs::PointCloud2ConstIterator<float> x(c, "x"), y(c, "y"), z(c, "z");
for (size_t i = 0; i < c.width * c.height; ++i, ++x, ++y, ++z)
points.push_back({*x, *y, *z});
return points;
}
TEST(DepthBuffer, HeightGatePrecedesCentroidAndKeepsExactHeader)
{
tf3::BufferCore tf;
transform(tf, "camera", 0, 0.04);
ObservationBuffer b("depth", 0, 0, 0.05, 1, 2.5, 3, tf, "odom", "", 0.2);
b.setCloudProcessingConfig(config());
// Two floor points + a 6 cm obstacle share a camera-frame leaf before TF.
// Their premature centroid would be below the 5 cm height threshold.
ASSERT_TRUE(b.tryBufferCloud(cloud({{0.011f, 0.011f, 0.001f},
{0.013f, 0.011f, 0.001f},
{0.019f, 0.011f, 0.020f}})));
Observation obs;
const auto pts = output(b, &obs);
ASSERT_EQ(pts.size(), 1u);
EXPECT_NEAR(pts[0][2], 0.06, 1e-6);
EXPECT_EQ(obs.cloud_->header.frame_id, "odom");
EXPECT_EQ(obs.cloud_->header.stamp.nsec, 123456789u);
EXPECT_EQ(obs.cloud_->header.seq, 9u);
EXPECT_DOUBLE_EQ(obs.origin_.z, 0.04);
}
TEST(DepthBuffer, VoxelCentroidsArePerSourceOptIn)
{
tf3::BufferCore tf;
transform(tf, "camera", 0, 0);
const auto c = cloud({{.011f, .011f, .11f}, {.019f, .011f, .11f}});
ObservationBuffer filtered("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
filtered.setCloudProcessingConfig(config());
ASSERT_TRUE(filtered.tryBufferCloud(c));
ASSERT_EQ(output(filtered).size(), 1u);
EXPECT_NEAR(output(filtered)[0][0], .015, 1e-6);
ObservationBuffer legacy("other", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
ASSERT_TRUE(legacy.tryBufferCloud(c));
EXPECT_EQ(output(legacy).size(), 2u);
}
TEST(DepthBuffer, UsesMeasurementTransformForCloudAndDifferentOriginFrame)
{
tf3::BufferCore tf;
transform(tf, "camera", 1, 0, 10, false);
transform(tf, "camera", 4, 0, 11, false);
transform(tf, "sensor_origin", .5, 0, 10, false);
transform(tf, "sensor_origin", 3.5, 0, 11, false);
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "sensor_origin", .2);
b.setCloudProcessingConfig(config(false));
ASSERT_TRUE(b.tryBufferCloud(cloud({{0, 0, .1f}})));
Observation obs;
ASSERT_EQ(output(b, &obs).size(), 1u);
EXPECT_NEAR(output(b)[0][0], 1, 1e-6);
EXPECT_DOUBLE_EQ(obs.origin_.x, .5);
}
TEST(DepthBuffer, EmptyCloudIsValidAndMissingTfDoesNotReplaceIt)
{
tf3::BufferCore tf;
transform(tf, "camera", 0, 0, 10, false);
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
b.setCloudProcessingConfig(config());
ASSERT_TRUE(b.tryBufferCloud(cloud({})));
auto late = cloud({{0, 0, .1f}});
late.header.stamp.sec = 20;
EXPECT_FALSE(b.tryBufferCloud(late));
std::vector<Observation> obs;
b.getObservations(obs);
ASSERT_EQ(obs.size(), 1u);
EXPECT_EQ(obs[0].cloud_->header.stamp.sec, 10u);
EXPECT_EQ(obs[0].cloud_->width, 0u);
EXPECT_FALSE(obs[0].cloud_->fields.empty());
}
TEST(DepthBuffer, FiniteHeightAndRangeGatesPrecedePcl)
{
tf3::BufferCore tf;
transform(tf, "camera", 0, 0);
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
b.setCloudProcessingConfig(config(false));
const float nan = std::numeric_limits<float>::quiet_NaN();
ASSERT_TRUE(b.tryBufferCloud(cloud({{nan, 0, .1f}, {0, 0, nan},
{3, 0, .1f}, {0, 0, .049f},
{.1f, 0, .05f}, {.2f, 0, 1.f}, {.3f, 0, 1.01f}})));
EXPECT_EQ(output(b).size(), 2u);
}
TEST(DepthBuffer, RejectsZeroStampAndInvalidFilterConfig)
{
tf3::BufferCore tf;
transform(tf, "camera", 0, 0);
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
b.setCloudProcessingConfig(config());
auto c = cloud({}); c.header.stamp = robot::Time();
EXPECT_FALSE(b.tryBufferCloud(c));
auto bad = config();
for (double leaf : {0., -1., std::numeric_limits<double>::infinity()})
{
bad.voxel_leaf_size = leaf;
EXPECT_THROW(b.setCloudProcessingConfig(bad), std::invalid_argument);
}
bad = config(); bad.transform_at_stamp = false;
EXPECT_THROW(b.setCloudProcessingConfig(bad), std::invalid_argument);
}
TEST(DepthBuffer, RejectsMalformedXyzFieldsBeforeReadingOrTransforming)
{
tf3::BufferCore tf;
transform(tf, "camera", 0, 0);
ObservationBuffer b("depth", 0, 0, .05, 1, 2.5, 3, tf, "odom", "", .2);
b.setCloudProcessingConfig(config());
auto c = cloud({{0, 0, .1f}});
c.fields[0].offset = c.point_step;
EXPECT_FALSE(b.tryBufferCloud(c));
c = cloud({}); c.fields.pop_back();
EXPECT_FALSE(b.tryBufferCloud(c));
c = cloud({}); c.is_bigendian = true;
EXPECT_FALSE(b.tryBufferCloud(c));
}
class InspectVoxelLayer : public robot_costmap_2d::VoxelLayer
{
public:
using robot_costmap_2d::ObstacleLayer::getMarkingObservations;
size_t pairedSources() const { return paired_depth_sources_.size(); }
};
TEST(DepthBuffer, RuntimeYamlActuallyEnablesTimestampAndPclOnDepthSource)
{
const auto runtime = std::filesystem::path(ROBOT_COSTMAP_2D_DIR)
.parent_path().parent_path().parent_path() / "config/runtime";
ASSERT_EQ(setenv("PNKX_NAV_CORE_CONFIG_DIR", runtime.c_str(), 1), 0);
tf3::BufferCore tf;
transform(tf, "camera", 1, 0, 10, false);
transform(tf, "camera", 4, 0, 11, false);
transform(tf, "base_link", 0, 0);
tf3::TransformStampedMsg map_odom;
map_odom.header.frame_id = "map";
map_odom.child_frame_id = "odom";
ASSERT_TRUE(tf.setTransform(map_odom, "test", true));
// Run the real costmap factory so copyParentParameters(), not test-only
// manual parameter injection, forwards common source options to this layer.
// Select the camera observation profile in memory; the default runtime may
// intentionally use f_scan. No config file or ROS parameter server is written.
robot::NodeHandle root("~");
root.setParam("obstacles/observation_sources", std::string("pc_marking"));
root.setParam("local_costmap/library_path", std::string(DEPTH_MARKING_TEST_PLUGIN_PATH));
robot_costmap_2d::Costmap2DROBOT configured("local_costmap", tf);
configured.pause();
robot::NodeHandle settings("local_costmap/obstacles/pc_marking");
bool stamp_tf = false, pcl_enabled = false;
ASSERT_TRUE(settings.getParam("cloud_transform_at_stamp", stamp_tf));
ASSERT_TRUE(settings.getParam("marking_voxel_filter/enabled", pcl_enabled));
EXPECT_TRUE(stamp_tf);
EXPECT_TRUE(pcl_enabled);
robot_costmap_2d::LayeredCostmap parent("odom", true, false);
parent.getCostmap()->resizeMap(40, 40, .05, -1, -1);
InspectVoxelLayer layer;
ASSERT_NO_THROW(layer.initialize(&parent, "local_costmap/obstacles", &tf));
ASSERT_TRUE(layer.isEnabled());
layer.dataCallBack(cloud({{.011f, .011f, .11f}, {.019f, .011f, .11f}}),
"/camera/depth/points_proc");
std::vector<Observation> observations;
EXPECT_TRUE(layer.getMarkingObservations(observations));
ASSERT_EQ(observations.size(), 1u);
const auto &c = *observations.front().cloud_;
ASSERT_EQ(c.width, 1u);
robot_sensor_msgs::PointCloud2ConstIterator<float> x(c, "x");
EXPECT_NEAR(*x, 1.015, 1e-6);
EXPECT_EQ(c.header.stamp.nsec, 123456789u);
}
TEST(DepthBuffer, RuntimeFactoryForwardsObservedVolumeModeAndPairsSources)
{
const auto runtime = std::filesystem::path(ROBOT_COSTMAP_2D_DIR)
.parent_path().parent_path().parent_path() / "config/runtime";
ASSERT_EQ(setenv("PNKX_NAV_CORE_CONFIG_DIR", runtime.c_str(), 1), 0);
tf3::BufferCore tf;
transform(tf, "base_link", 0, 0);
tf3::TransformStampedMsg map_odom;
map_odom.header.frame_id = "map"; map_odom.child_frame_id = "odom";
ASSERT_TRUE(tf.setTransform(map_odom, "test", true));
robot::NodeHandle root("~");
root.setParam("obstacles/observation_sources", std::string("pc_marking pc_clearing"));
root.setParam("local_costmap/library_path", std::string(DEPTH_MARKING_TEST_PLUGIN_PATH));
robot_costmap_2d::Costmap2DROBOT configured("local_costmap", tf);
configured.pause();
robot::NodeHandle settings("local_costmap/obstacles/pc_clearing");
std::string mode;
ASSERT_TRUE(settings.getParam("depth_clearing_mode", mode));
EXPECT_EQ(mode,"observed_volume");
// Every observed_volume key of the runtime YAML must reach the layer namespace; a key
// missing from the factory forward list silently falls back to its C++ default.
const YAML::Node runtime_clearing =
YAML::LoadFile((runtime / "costmap_common_params.yaml").string())["obstacles"]["pc_clearing"];
for (const char* key : {"depth_evidence_points_enabled", "depth_evidence_points_per_voxel",
"depth_marking_border_px", "depth_projection_margin_px", "depth_valid_max_z_m",
"depth_far_return_is_behind", "depth_reobserve_clearing", "depth_confirm_tolerance_m",
"depth_miss_frames", "depth_no_return_weight", "depth_stereo_baseline_m",
"depth_stereo_band_margin_px"})
{
ASSERT_TRUE(runtime_clearing[key].IsDefined()) << key;
ASSERT_TRUE(settings.hasParam(key)) << key;
EXPECT_EQ(YAML::Dump(settings.getParamValue(key)), YAML::Dump(runtime_clearing[key])) << key;
}
// Instantiate with the factory's scoped parameters, then verify the paired
// owner exists and a marking-only half does not enter legacy observations.
robot_costmap_2d::LayeredCostmap parent("odom", true, false);
parent.getCostmap()->resizeMap(40,40,.05,-1,-1);
InspectVoxelLayer layer;
ASSERT_NO_THROW(layer.initialize(&parent,"local_costmap/obstacles",&tf));
ASSERT_EQ(layer.pairedSources(),1u);
layer.dataCallBack(cloud({{.1f,0,.1f}}),"/camera/depth/points_proc");
std::vector<Observation> observations;
EXPECT_TRUE(layer.getMarkingObservations(observations));
EXPECT_TRUE(observations.empty());
}
} // namespace
int main(int argc, char **argv)
{
robot::Time::init();
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@@ -1,542 +0,0 @@
#include <robot_costmap_2d/depth_visibility.h>
#include <robot_costmap_2d/depth_frame_pairer.h>
#include <robot_costmap_2d/voxel_layer.h>
#include <robot_costmap_2d/inflation_layer.h>
#include <robot_sensor_msgs/point_cloud2_iterator.h>
#include <boost/make_shared.hpp>
#include <gtest/gtest.h>
#include <filesystem>
#include <cstring>
#include <thread>
using namespace robot_costmap_2d;
namespace {
robot_sensor_msgs::DepthCameraData::Ptr depth(double meters, robot::Time stamp = robot::Time(100,0)) {
auto d = boost::make_shared<robot_sensor_msgs::DepthCameraData>();
d->header.stamp=stamp; d->header.frame_id="camera";
d->depth.header=d->header; d->camera_info.header=d->header;
d->depth.width=d->camera_info.width=200; d->depth.height=d->camera_info.height=160;
d->depth.step=400; d->depth.encoding="16UC1"; d->depth.data.resize(64000);
uint16_t value=static_cast<uint16_t>(meters*1000);
for(size_t i=0;i<d->depth.data.size();i+=2) std::memcpy(&d->depth.data[i], &value, 2);
d->camera_info.K={100,0,100,0,100,80,0,0,1};
d->camera_info.P={100,0,100,0,0,100,80,0,0,0,1,0};
return d;
}
robot_sensor_msgs::PointCloud2 cloud(robot::Time stamp, bool point=true) {
robot_sensor_msgs::PointCloud2 c; c.header.stamp=stamp; c.header.frame_id="camera"; c.height=1;
robot_sensor_msgs::PointCloud2Modifier m(c); m.setPointCloud2FieldsByString(1,"xyz"); m.resize(point?1:0);
if(point) { robot_sensor_msgs::PointCloud2Iterator<float> x(c,"x"), y(c,"y"), z(c,"z");
*x=-.01;*y=.11;*z=.62; }
return c;
}
// One marking point given in the optical frame of the camera.
robot_sensor_msgs::PointCloud2 cloudAt(robot::Time stamp, float ox, float oy, float oz) {
robot_sensor_msgs::PointCloud2 c=cloud(stamp,true);
robot_sensor_msgs::PointCloud2Iterator<float> x(c,"x"), y(c,"y"), z(c,"z");
*x=ox;*y=oy;*z=oz;
return c;
}
tf3::TransformStampedMsg pose(robot::Time stamp, double yaw=0) {
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
p.header.stamp=tf3::Time(stamp.sec,stamp.nsec);
Eigen::Matrix3d optical; optical << 0,0,1, -1,0,0, 0,-1,0;
Eigen::Quaterniond q(Eigen::AngleAxisd(yaw,Eigen::Vector3d::UnitZ())*optical);
p.transform.rotation.x=q.x();p.transform.rotation.y=q.y();
p.transform.rotation.z=q.z();p.transform.rotation.w=q.w();p.transform.translation.z=.19;
return p;
}
TEST(DepthVisibility, FullRegionFreeInvalidOccludedAndOutsideView) {
DepthVisibilityChecker c; DepthFrustumConfig cfg;
auto d=depth(2);
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
p.header.stamp=tf3::Time(100,0);p.transform.rotation.w=1;
Eigen::Vector3d lo(-.02,-.02,1),hi(.02,.02,1.05);
ASSERT_TRUE(c.prepare(d,p,0,3,cfg));
EXPECT_EQ(c.classify(lo,hi).state,DepthVisibility::Free);
uint16_t zero=0;std::memcpy(&d->depth.data[80*400+100*2],&zero,2);
ASSERT_TRUE(c.prepare(d,p,0,3,cfg));EXPECT_EQ(c.classify(lo,hi).state,DepthVisibility::Unknown);
d=depth(.8);ASSERT_TRUE(c.prepare(d,p,0,3,cfg));
EXPECT_EQ(c.classify(lo,hi).state,DepthVisibility::Surface);
d=depth(2);ASSERT_TRUE(c.prepare(d,p,0,3,cfg));
EXPECT_EQ(c.classify(Eigen::Vector3d(1,-.02,1),Eigen::Vector3d(1.05,.02,1.05)).state,DepthVisibility::Unknown);
EXPECT_EQ(c.classify(Eigen::Vector3d(-.02,-.02,.05),hi).state,DepthVisibility::Unknown);
EXPECT_EQ(c.classify(Eigen::Vector3d(1e30,-.02,1),Eigen::Vector3d(2e30,.02,1.05)).state,DepthVisibility::Unknown);
}
TEST(DepthVisibility, InvalidPixelAtProjectedEdgeAlsoBlocksClear) {
auto d=depth(2); DepthVisibilityChecker c;DepthFrustumConfig cfg;
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
p.header.stamp=tf3::Time(100,0);p.transform.rotation.w=1;
uint16_t zero=0;std::memcpy(&d->depth.data[80*400+97*2],&zero,2);
ASSERT_TRUE(c.prepare(d,p,0,3,cfg));
EXPECT_EQ(c.classify(Eigen::Vector3d(-.02,-.02,1),Eigen::Vector3d(.02,.02,1.05)).state,DepthVisibility::Unknown);
}
TEST(DepthVisibility, RejectsWrongStampModelAndMalformedImage) {
auto d=depth(2);DepthVisibilityChecker c;DepthFrustumConfig cfg;
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
p.header.stamp=tf3::Time(101,0);p.transform.rotation.w=1;
EXPECT_FALSE(c.prepare(d,p,0,3,cfg));p.header.stamp=tf3::Time(100,0);
d->camera_info.P[0]=90;EXPECT_FALSE(c.prepare(d,p,0,3,cfg));
d=depth(2);d->depth.data.pop_back();EXPECT_FALSE(c.prepare(d,p,0,3,cfg));
d=depth(2);d->depth.is_bigendian=true;EXPECT_FALSE(c.prepare(d,p,0,3,cfg));
}
// Camera frame == global frame here. A box whose top crosses the image top can never be
// certified, while a measured point inside the image can.
TEST(DepthVisibility, EvidencePointsCertifyAVoxelCutByTheImageTop) {
DepthVisibilityChecker c; DepthFrustumConfig cfg;
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
p.header.stamp=tf3::Time(100,0);p.transform.rotation.w=1;
const Eigen::Vector3d lo(-.02,-.80,1),hi(.02,-.75,1.05); // v from about 0 to 5 px
const std::vector<Eigen::Vector3d> evidence{Eigen::Vector3d(0,-.77,1)}; // v = 3
auto d=depth(2);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classify(lo,hi).state,DepthVisibility::Unknown);
EXPECT_EQ(c.classifyPoints(evidence).state,DepthVisibility::Free);
d=depth(.9);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classifyPoints(evidence).state,DepthVisibility::Surface);
d=depth(2);uint16_t zero=0;std::memcpy(&d->depth.data[4*400+101*2],&zero,2); // inside the 3x3 patch
ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classifyPoints(evidence).state,DepthVisibility::Unknown);
d=depth(2);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classifyPoints({Eigen::Vector3d(0,-.80,1)}).state,DepthVisibility::Unknown); // v = 0
EXPECT_EQ(c.classifyPoints({Eigen::Vector3d(0,0,-1)}).state,DepthVisibility::Unknown); // behind
EXPECT_EQ(c.classifyPoints({}).state,DepthVisibility::Unknown);
// Every point must be free: one point in front of a surface keeps the voxel.
d=depth(1.5);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classifyPoints({Eigen::Vector3d(0,0,1),Eigen::Vector3d(0,0,1.6)}).state,DepthVisibility::Surface);
}
// A return beyond valid_max_z_m (3 m) proves the ray passed a point well inside the range.
TEST(DepthVisibility, FarReturnCountsAsBehindOnlyWhenEnabled) {
DepthVisibilityChecker c; DepthFrustumConfig cfg;
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
p.header.stamp=tf3::Time(100,0);p.transform.rotation.w=1;
const std::vector<Eigen::Vector3d> point{Eigen::Vector3d(0,0,1)}; // u = 100, v = 80
auto d=depth(4);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classifyPoints(point).state,DepthVisibility::Unknown); // default: not proof
cfg.far_return_is_behind=true;ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classifyPoints(point).state,DepthVisibility::Free);
// point + margin reaches the range limit: a far return proves nothing about it
EXPECT_EQ(c.classifyPoints({Eigen::Vector3d(0,0,2.99)}).state,DepthVisibility::Unknown);
d=depth(4);uint16_t zero=0;std::memcpy(&d->depth.data[80*400+101*2],&zero,2); // no-data in the patch
ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classifyPoints(point).state,DepthVisibility::Unknown);
d=depth(.5);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.classifyPoints(point).state,DepthVisibility::Surface); // a nearer surface still keeps it
}
TEST(DepthVisibility, ProjectsInsideHonoursBorderAndUsableStrips) {
DepthVisibilityChecker c; DepthFrustumConfig cfg;cfg.clear_left_border_px=10;
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
p.header.stamp=tf3::Time(100,0);p.transform.rotation.w=1;
auto d=depth(2);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_TRUE(c.projectsInside(Eigen::Vector3d(0,-.77,1),2)); // v = 3
EXPECT_FALSE(c.projectsInside(Eigen::Vector3d(0,-.79,1),2)); // v = 1
EXPECT_TRUE(c.projectsInside(Eigen::Vector3d(-.88,0,1),2)); // u = 12
EXPECT_FALSE(c.projectsInside(Eigen::Vector3d(-.89,0,1),2)); // u = 11, inside the left strip + 2
EXPECT_FALSE(c.projectsInside(Eigen::Vector3d(0,0,.05),2)); // nearer than valid_min_z
}
// Re-observe-or-clear checker. Camera frame == global frame; (0,0,1) projects to u = 100, v = 80.
void setPixel(robot_sensor_msgs::DepthCameraData::Ptr& d,unsigned u,unsigned v,double meters) {
uint16_t value=static_cast<uint16_t>(meters*1000);std::memcpy(&d->depth.data[v*400+u*2],&value,2);
}
tf3::TransformStampedMsg identity() {
tf3::TransformStampedMsg p; p.header.frame_id="odom";p.child_frame_id="camera";
p.header.stamp=tf3::Time(100,0);p.transform.rotation.w=1;return p;
}
TEST(DepthReobserve, FourOutcomesOfOnePoint) {
DepthVisibilityChecker c; DepthFrustumConfig cfg; const auto p=identity();
const std::vector<Eigen::Vector3d> point{Eigen::Vector3d(0,0,1)};
auto d=depth(1.03);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints(point),DepthObservation::Confirmed); // within 5 cm
d=depth(.9);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints(point),DepthObservation::Occluded);
d=depth(1.2);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints(point),DepthObservation::SeenThrough);
d=depth(0);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints(point),DepthObservation::NoReturn);
}
TEST(DepthReobserve, OutOfViewWhenNotProjectableOrOutsideTheTrustedRange) {
DepthVisibilityChecker c; DepthFrustumConfig cfg; const auto p=identity();
auto d=depth(2);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints({}),DepthObservation::OutOfView);
EXPECT_EQ(c.observePoints({Eigen::Vector3d(0,0,-1)}),DepthObservation::OutOfView); // behind the camera
EXPECT_EQ(c.observePoints({Eigen::Vector3d(0,0,3.5)}),DepthObservation::OutOfView); // beyond valid_max_z_m
EXPECT_EQ(c.observePoints({Eigen::Vector3d(0,-.80,1)}),DepthObservation::OutOfView); // v = 0: no full patch
EXPECT_EQ(c.observePoints({Eigen::Vector3d(5,0,1)}),DepthObservation::OutOfView); // right of the image
}
TEST(DepthReobserve, InvalidPixelsDoNotBlockSeeingThrough) {
DepthVisibilityChecker c; DepthFrustumConfig cfg; const auto p=identity();
const std::vector<Eigen::Vector3d> point{Eigen::Vector3d(0,0,1)};
auto d=depth(0);setPixel(d,101,81,2); // one valid pixel behind, eight with no return
ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints(point),DepthObservation::SeenThrough);
}
TEST(DepthReobserve, OccluderInThePatchBeatsSeenThroughAndConfirmBeatsBoth) {
DepthVisibilityChecker c; DepthFrustumConfig cfg; const auto p=identity();
const std::vector<Eigen::Vector3d> point{Eigen::Vector3d(0,0,1)};
auto d=depth(2);setPixel(d,99,79,.5);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints(point),DepthObservation::Occluded);
setPixel(d,101,81,1.02);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints(point),DepthObservation::Confirmed);
// The voxel takes its strongest point: one point still on a surface keeps it.
d=depth(1.5);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints({Eigen::Vector3d(0,0,1),Eigen::Vector3d(0,0,1.5)}),DepthObservation::Confirmed);
d=depth(1.2);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints({Eigen::Vector3d(0,0,1),Eigen::Vector3d(0,0,1.5)}),DepthObservation::Occluded);
}
TEST(DepthReobserve, FarReturnIsBehindOnlyWhenEnabledAndInsideTheRange) {
DepthVisibilityChecker c; DepthFrustumConfig cfg; const auto p=identity();
auto d=depth(4);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints({Eigen::Vector3d(0,0,1)}),DepthObservation::NoReturn);
cfg.far_return_is_behind=true;ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints({Eigen::Vector3d(0,0,1)}),DepthObservation::SeenThrough);
EXPECT_EQ(c.observePoints({Eigen::Vector3d(0,0,2.99)}),DepthObservation::NoReturn); // 2.99 + 0.05 >= 3
}
// fx = 100: the band at depth z is ceil(100 * 0.1 / z) + 10 px. Column u = 30 is in it at 0.5 m
// (30 + 1 px patch > 30) and clear of it at 2.5 m (14 + 1 px).
TEST(DepthReobserve, StereoBandIsOutOfViewNearAndUsableFar) {
DepthVisibilityChecker c; DepthFrustumConfig cfg; const auto p=identity();
const Eigen::Vector3d near(-.35,0,.5), far(-1.75,0,2.5); // both u = 30
auto d=depth(.5);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints({near}),DepthObservation::Confirmed); // band disabled
cfg.stereo_baseline_m=.1;cfg.stereo_band_margin_px=10;ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints({near}),DepthObservation::OutOfView);
EXPECT_FALSE(c.projectsInside(near,2));
d=depth(2.5);ASSERT_TRUE(c.prepare(d,p,-10,10,cfg));
EXPECT_EQ(c.observePoints({far}),DepthObservation::Confirmed);
EXPECT_TRUE(c.projectsInside(far,2));
}
TEST(DepthPairer, MissingHalfExactPairAndDeduplication) {
robot::Time::setNow(robot::Time(100,0));DepthFrustumConfig cfg;DepthFramePairer p(cfg,.5);
auto c=cloud(robot::Time(100,0));p.addCloud(c);DepthFramePairer::Candidate next;
EXPECT_FALSE(p.next(next));p.addDepth(depth(2));ASSERT_TRUE(p.next(next));
EXPECT_EQ(next.stamp,c.header.stamp.toNSec());p.applied(next.stamp);
p.addCloud(c);p.addDepth(depth(2));EXPECT_FALSE(p.next(next));
EXPECT_EQ(p.counts().applied,1u);EXPECT_EQ(p.counts().duplicate,2u);
}
TEST(DepthPairer, NewerAppliedFrameCannotBeResurrectedByDelayedOlderCloud) {
robot::Time::setNow(robot::Time(100,0));DepthFrustumConfig cfg;DepthFramePairer p(cfg,.5);
p.addCloud(cloud(robot::Time(100,0)));robot::Time::setNow(robot::Time(100,100000000));
p.addCloud(cloud(robot::Time(100,100000000)));p.addDepth(depth(2,robot::Time(100,100000000)));
DepthFramePairer::Candidate n;ASSERT_TRUE(p.next(n));p.applied(n.stamp);
p.addDepth(depth(2));EXPECT_FALSE(p.next(n));p.reset();
p.addCloud(cloud(robot::Time(100,0)));p.addDepth(depth(2));EXPECT_FALSE(p.next(n));
}
TEST(DepthPairer, BoundTimeoutAgeAndClockReset) {
robot::Time::setNow(robot::Time(100,0));DepthFrustumConfig cfg;cfg.pair_queue_size=2;cfg.pair_max_wait_s=.005;
DepthFramePairer p(cfg,.5);
for(int i=0;i<3;++i) {robot::Time::setNow(robot::Time(100,i*10000000));p.addCloud(cloud(robot::Time(100,i*10000000)));}
EXPECT_EQ(p.counts().overflow,1u);std::this_thread::sleep_for(std::chrono::milliseconds(10));
DepthFramePairer::Candidate n;EXPECT_FALSE(p.next(n));EXPECT_EQ(p.counts().expired,2u);
p.addCloud(cloud(robot::Time(99,0)));EXPECT_EQ(p.counts().stale,1u);
p.applied(robot::Time(100,20000000).toNSec());
robot::Time::setNow(robot::Time(10,0));p.addCloud(cloud(robot::Time(10,0)));p.addDepth(depth(2,robot::Time(10,0)));
EXPECT_TRUE(p.next(n));
}
class DepthLayerTest:public ::testing::Test {
protected:
tf3::BufferCore tf;
LayeredCostmap parent{"odom",false,false};
VoxelLayer layer;
uint32_t tick=0;
void SetUp() override {
robot::Time::setNow(robot::Time(100,0));
auto runtime=std::filesystem::path(ROBOT_COSTMAP_2D_DIR).parent_path().parent_path().parent_path()/"config/runtime";
setenv("PNKX_NAV_CORE_CONFIG_DIR",runtime.c_str(),1);
YAML::Node cfg=YAML::Clone(YAML::LoadFile((runtime/"costmap_common_params.yaml").string())["obstacles"]);
cfg["observation_sources"]="pc_marking pc_clearing";cfg["footprint_clearing_enabled"]=false;cfg["enabled"]=true;
// Explicit, so the result does not depend on what the runtime YAML currently enables.
cfg["pc_clearing"]["depth_evidence_points_enabled"]=false;
cfg["pc_clearing"]["depth_far_return_is_behind"]=false;
cfg["pc_clearing"]["depth_reobserve_clearing"]=false;
cfg["pc_clearing"]["depth_stereo_baseline_m"]=0.0;
configure(cfg);
robot::NodeHandle nh("~");nh.setParam("depth_test",cfg);
parent.getCostmap()->resizeMap(80,80,.05,-2,-2);layer.initialize(&parent,"depth_test",&tf);
ASSERT_TRUE(layer.isEnabled());
}
virtual void configure(YAML::Node&) {}
robot::Time now() {return robot::Time(100,++tick*10000000);}
// Marking point given in the camera optical frame; the image shows `measurement` everywhere.
void frameCloud(double measurement,float ox,float oy,float oz) {
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
layer.dataCallBack(cloudAt(stamp,ox,oy,oz),"/camera/depth/points_proc");
robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(measurement,stamp);
layer.dataCallBack(d,"/camera/depth/data");update();
}
void update() {double a=1e9,b=1e9,c=-1e9,d=-1e9;layer.updateBounds(0,0,0,&a,&b,&c,&d);}
void frame(double measurement,bool point=false,double yaw=0,bool hasTf=true,bool hasCloud=true,bool hasDepth=true) {
auto stamp=now();robot::Time::setNow(stamp);
if(hasTf) ASSERT_TRUE(tf.setTransform(pose(stamp,yaw),"test"));
if(hasCloud) layer.dataCallBack(cloud(stamp,point),"/camera/depth/points_proc");
if(hasDepth) {robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(measurement,stamp);
layer.dataCallBack(d,"/camera/depth/data");}
update();
}
unsigned char at(double x=.62,double y=.01) {unsigned a,b;EXPECT_TRUE(layer.worldToMap(x,y,a,b));return layer.getCost(a,b);}
};
TEST_F(DepthLayerTest, RetainsOutsideInvalidOccludedAndClearsOnFirstValidFrame) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2,false,1.57);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(0);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(.4);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),FREE_SPACE); // one valid frame; no free-frame delay
}
TEST_F(DepthLayerTest, MissingCloudDepthOrTfCannotClearAndNoTtl) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2,false,0,true,false,true);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2,false,0,true,true,false);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2,false,0,false,true,true);EXPECT_EQ(at(),LETHAL_OBSTACLE);
robot::Time::setNow(robot::Time(110,0));update();EXPECT_EQ(at(),LETHAL_OBSTACLE);
EXPECT_FALSE(layer.isCurrent());
}
TEST_F(DepthLayerTest, ClearingFrameDoesNotRepeatOldMarkingCloud) {
frame(.62,true);frame(2);ASSERT_EQ(at(),FREE_SPACE);
layer.dataCallBack(cloud(robot::Time(100,10000000)),"/camera/depth/points_proc");
update();EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthLayerTest, RollingOriginPreservesOverlapAndDropsExitedCells) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
layer.updateOrigin(-1.5,-2);EXPECT_EQ(at(),LETHAL_OBSTACLE);
layer.updateOrigin(1,-2);layer.updateOrigin(-2,-2);EXPECT_NE(at(),LETHAL_OBSTACLE);
}
TEST_F(DepthLayerTest, WaitingPairAppliesWhenItsExactTfArrives) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2,false,0,false);EXPECT_EQ(at(),LETHAL_OBSTACLE);
auto stamp=robot::Time(100,tick*10000000);
ASSERT_TRUE(tf.setTransform(pose(stamp),"late_tf"));
update();EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthLayerTest, PendingFrameSurvivesRollingStorageShift) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
layer.dataCallBack(cloud(stamp,false),"/camera/depth/points_proc");
robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(2,stamp);layer.dataCallBack(d,"/camera/depth/data");
layer.updateOrigin(-1.5,-2);update();EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthLayerTest, MasterAndInflationRemoveTheClearedObstacle) {
parent.addPlugin(boost::shared_ptr<Layer>(&layer,[](Layer*){}));
auto inflation=boost::make_shared<InflationLayer>();
robot::NodeHandle nh("~");nh.setParam("depth_inflation/inflation_radius",.3);
nh.setParam("depth_inflation/cost_scaling_factor",10.);
parent.addPlugin(inflation);inflation->initialize(&parent,"depth_inflation",&tf);
std::vector<robot_geometry_msgs::Point> footprint(4);
footprint[0].x=footprint[1].x=-.1;footprint[2].x=footprint[3].x=.1;
footprint[0].y=footprint[3].y=-.1;footprint[1].y=footprint[2].y=.1;
parent.setFootprint(footprint);
for(bool occupied : {true,false}) {
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
layer.dataCallBack(cloud(stamp,occupied),"/camera/depth/points_proc");
robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(occupied?.62:2,stamp);
layer.dataCallBack(d,"/camera/depth/data");parent.updateMap(0,0,0);
unsigned x,y;ASSERT_TRUE(parent.getCostmap()->worldToMap(.62,.01,x,y));
EXPECT_EQ(parent.getCostmap()->getCost(x,y),occupied?LETHAL_OBSTACLE:FREE_SPACE);
ASSERT_TRUE(parent.getCostmap()->worldToMap(.72,.01,x,y));
if(occupied) EXPECT_GT(parent.getCostmap()->getCost(x,y),FREE_SPACE);
else EXPECT_EQ(parent.getCostmap()->getCost(x,y),FREE_SPACE);
}
}
TEST_F(DepthLayerTest, AStillOccludedVoxelKeepsItsColumnLethal) {
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
auto c=cloud(stamp);robot_sensor_msgs::PointCloud2Modifier m(c);m.resize(2);
robot_sensor_msgs::PointCloud2Iterator<float> x(c,"x"),y(c,"y"),z(c,"z");
*x=-.01;*y=.11;*z=.62;++x;++y;++z;*x=-.01;*y=-.29;*z=.62;
layer.dataCallBack(c,"/camera/depth/points_proc");
robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(.62,stamp);layer.dataCallBack(d,"/camera/depth/data");update();
ASSERT_EQ(at(),LETHAL_OBSTACLE);
stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
auto partial=depth(2,stamp);uint16_t blocked=400;
for(unsigned v=0;v<65;++v) for(unsigned u=0;u<200;++u)
std::memcpy(&partial->depth.data[v*400+u*2],&blocked,2);
layer.dataCallBack(cloud(stamp,false),"/camera/depth/points_proc");
d=partial;layer.dataCallBack(d,"/camera/depth/data");update();EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),FREE_SPACE);
}
// A point seen 7 px below the image top marks voxel z [0.625, 0.6875] m, whose top
// corner projects above the image. Whole-voxel rule: never certified (the person/hand
// phantom of the 2026-10-15 lift bag).
TEST_F(DepthLayerTest, WholeVoxelRuleKeepsAVoxelCutByTheImageTop) {
frameCloud(.62,-.01f,-.45f,.62f);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),LETHAL_OBSTACLE);
}
class DepthEvidenceLayerTest:public DepthLayerTest {
protected:
void configure(YAML::Node& cfg) override {
cfg["pc_clearing"]["depth_evidence_points_enabled"]=true;
cfg["pc_clearing"]["depth_marking_border_px"]=2;
}
};
TEST_F(DepthEvidenceLayerTest, EvidencePointsClearAVoxelCutByTheImageTop) {
frameCloud(.62,-.01f,-.45f,.62f);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthEvidenceLayerTest, RetainsOutsideInvalidOccludedAndClearsOnFirstValidFrame) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2,false,1.57);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(0);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(.4);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthEvidenceLayerTest, MarkingDropsPointsInTheImageBorder) {
frameCloud(.62,-.01f,-.49f,.62f);EXPECT_NE(at(),LETHAL_OBSTACLE); // v = 0.97 px
frameCloud(.62,-.01f,-.45f,.62f);EXPECT_EQ(at(),LETHAL_OBSTACLE); // v = 7.4 px
}
TEST_F(DepthEvidenceLayerTest, EvidenceSurvivesRollingShift) {
frameCloud(.62,-.01f,-.45f,.62f);ASSERT_EQ(at(),LETHAL_OBSTACLE);
layer.updateOrigin(-1.5,-2);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthEvidenceLayerTest, AStillVisibleObjectIsNeverCertifiedFree) {
frameCloud(.62,-.01f,-.45f,.62f);ASSERT_EQ(at(),LETHAL_OBSTACLE);
for(int i=0;i<3;++i) {frameCloud(.62,-.01f,-.45f,.62f);EXPECT_EQ(at(),LETHAL_OBSTACLE);}
frame(.62);EXPECT_EQ(at(),LETHAL_OBSTACLE); // no marking this frame, surface still seen
}
// Two voxels in the column of (0.62, 0.01): z 0.08 m (seen by image rows below 65) and
// z 0.48 m (rows above 65). The partial frame frees the low one and keeps the high one
// occluded, so a column update would re-derive the cell from any voxel left behind.
class DepthColumnClearTest:public DepthEvidenceLayerTest {
protected:
void configure(YAML::Node& cfg) override {
DepthEvidenceLayerTest::configure(cfg);
cfg["footprint_clearing_enabled"]=true;
}
void markTwoVoxels() {
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
auto c=cloud(stamp);robot_sensor_msgs::PointCloud2Modifier m(c);m.resize(2);
robot_sensor_msgs::PointCloud2Iterator<float> x(c,"x"),y(c,"y"),z(c,"z");
*x=-.01;*y=.11;*z=.62;++x;++y;++z;*x=-.01;*y=-.29;*z=.62;
layer.dataCallBack(c,"/camera/depth/points_proc");
robot_sensor_msgs::DepthCameraData::ConstPtr d=depth(.62,stamp);layer.dataCallBack(d,"/camera/depth/data");update();
}
void partialFrame() {
auto stamp=now();robot::Time::setNow(stamp);ASSERT_TRUE(tf.setTransform(pose(stamp),"test"));
auto partial=depth(2,stamp);uint16_t blocked=400;
for(unsigned v=0;v<65;++v) for(unsigned u=0;u<200;++u)
std::memcpy(&partial->depth.data[v*400+u*2],&blocked,2);
layer.dataCallBack(cloud(stamp,false),"/camera/depth/points_proc");
robot_sensor_msgs::DepthCameraData::ConstPtr d=partial;layer.dataCallBack(d,"/camera/depth/data");update();
}
void setFootprint(double x0,double x1,double y0,double y1) {
std::vector<robot_geometry_msgs::Point> footprint(4);
footprint[0].x=footprint[1].x=x0;footprint[2].x=footprint[3].x=x1;
footprint[0].y=footprint[3].y=y0;footprint[1].y=footprint[2].y=y1;
parent.setFootprint(footprint);
}
uint32_t markedBits(double x=.62,double y=.01) {
unsigned a,b;EXPECT_TRUE(layer.worldToMap(x,y,a,b));
return layer.voxelSnapshot().data[b*layer.getSizeInCellsX()+a]>>16;
}
};
TEST_F(DepthColumnClearTest, FootprintClearingAlsoFreesTheVoxelColumn) {
setFootprint(-1.9,-1.8,-1.9,-1.8); // far corner of the window
markTwoVoxels();ASSERT_EQ(at(),LETHAL_OBSTACLE);ASSERT_NE(markedBits(),0u);
setFootprint(.50,.75,-.10,.10);update(); // robot body now covers the cell
EXPECT_EQ(at(),FREE_SPACE);EXPECT_EQ(markedBits(),0u);
setFootprint(-1.9,-1.8,-1.9,-1.8);partialFrame();
EXPECT_EQ(at(),FREE_SPACE); // nothing left in the column to re-derive the cell
}
TEST_F(DepthColumnClearTest, ClearAreaAlsoFreesTheVoxelColumn) {
setFootprint(-1.9,-1.8,-1.9,-1.8);
markTwoVoxels();ASSERT_EQ(at(),LETHAL_OBSTACLE);
layer.clearArea(0,0,0,0,false); // empty kept window: clear the whole layer
EXPECT_EQ(at(),NO_INFORMATION);EXPECT_EQ(markedBits(),0u);
partialFrame();EXPECT_NE(at(),LETHAL_OBSTACLE);
}
TEST_F(DepthColumnClearTest, ClearAreaFollowsTheBaseWindowSelection) {
setFootprint(-1.9,-1.8,-1.9,-1.8);
markTwoVoxels();ASSERT_EQ(at(),LETHAL_OBSTACLE);
unsigned ux,uy;ASSERT_TRUE(layer.worldToMap(.62,.01,ux,uy));
const int x=static_cast<int>(ux),y=static_cast<int>(uy);
layer.clearArea(x-3,y-3,x+3,y+3,false); // default recovery: keep the window, clear outside it
EXPECT_EQ(at(),LETHAL_OBSTACLE);EXPECT_NE(markedBits(),0u);
layer.clearArea(x-3,y-3,x+3,y+3,true); // inverted: clear inside the window
EXPECT_EQ(at(),NO_INFORMATION);EXPECT_EQ(markedBits(),0u);
}
class DepthFarReturnLayerTest:public DepthEvidenceLayerTest {
protected:
void configure(YAML::Node& cfg) override {
DepthEvidenceLayerTest::configure(cfg);
cfg["pc_clearing"]["depth_far_return_is_behind"]=true;
}
};
TEST_F(DepthEvidenceLayerTest, FarReturnKeepsTheVoxelByDefault) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(4);EXPECT_EQ(at(),LETHAL_OBSTACLE);
}
TEST_F(DepthFarReturnLayerTest, FarReturnClearsWhenEnabledButNoDataStillKeeps) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(4);EXPECT_EQ(at(),FREE_SPACE);
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(0);EXPECT_EQ(at(),LETHAL_OBSTACLE);
}
// Re-observe-or-clear on the layer. The marked point (0.62, 0.01, 0.08) projects to (98, 98);
// frame(m) shows depth m everywhere: .62 re-observes it, 2 sees through, .4 occludes, 0 is no return.
class DepthReobserveLayerTest:public DepthEvidenceLayerTest {
protected:
void configure(YAML::Node& cfg) override {
DepthEvidenceLayerTest::configure(cfg);
cfg["pc_clearing"]["depth_reobserve_clearing"]=true;
cfg["pc_clearing"]["depth_confirm_tolerance_m"]=.05;
cfg["pc_clearing"]["depth_miss_frames"]=3;
cfg["pc_clearing"]["depth_no_return_weight"]=noReturnWeight();
cfg["pc_clearing"]["depth_stereo_baseline_m"]=stereoBaseline();
cfg["pc_clearing"]["depth_stereo_band_margin_px"]=10;
}
virtual double noReturnWeight() const {return 0.;}
virtual double stereoBaseline() const {return 0.;}
};
TEST_F(DepthReobserveLayerTest, SeenThroughFreesOnlyAfterMissFrames) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthReobserveLayerTest, ConfirmRestartsTheMissCount) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2);frame(2);frame(.62); // surface measured again, no marking point
frame(2);frame(2);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthReobserveLayerTest, RemarkedVoxelStartsANewMissCount) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
frame(2);
frameCloud(2,-.01f,.11f,.62f); // seen through (2nd miss), then marked again in the same frame
frame(2);frame(2);EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthReobserveLayerTest, OccludedOutOfViewAndNoReturnNeverExpire) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
for(int i=0;i<20;++i) frame(.4);
EXPECT_EQ(at(),LETHAL_OBSTACLE);
for(int i=0;i<20;++i) frame(2,false,1.57); // camera turned away
EXPECT_EQ(at(),LETHAL_OBSTACLE);
for(int i=0;i<20;++i) frame(0);
EXPECT_EQ(at(),LETHAL_OBSTACLE);
frame(2);frame(2);EXPECT_EQ(at(),LETHAL_OBSTACLE); // none of the above counted as a miss
frame(2);EXPECT_EQ(at(),FREE_SPACE);
}
TEST_F(DepthReobserveLayerTest, StillVisibleObjectIsKept) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
for(int i=0;i<20;++i) frame(i%2?.64:.60); // depth noise inside the 5 cm tolerance
EXPECT_EQ(at(),LETHAL_OBSTACLE);
}
class DepthReobserveNoReturnTest:public DepthReobserveLayerTest {
protected:
double noReturnWeight() const override {return .5;}
};
TEST_F(DepthReobserveNoReturnTest, NoReturnFreesWhenWeighted) {
frame(.62,true);ASSERT_EQ(at(),LETHAL_OBSTACLE);
for(int i=0;i<5;++i) frame(0);
EXPECT_EQ(at(),LETHAL_OBSTACLE); // 2.5 misses
frame(0);EXPECT_EQ(at(),FREE_SPACE);
}
// Band at 0.62 m: ceil(100 * 0.1 / 0.62) + 10 = 27 px, plus the 2 px marking border.
class DepthReobserveStereoBandTest:public DepthReobserveLayerTest {
protected:
double stereoBaseline() const override {return .1;}
};
TEST_F(DepthReobserveStereoBandTest, MarkingSkipsTheStereoBand) {
frameCloud(.62,-.496f,.11f,.62f);EXPECT_NE(at(.62,.496),LETHAL_OBSTACLE); // u = 20
frameCloud(.62,-.248f,.11f,.62f);EXPECT_EQ(at(.62,.248),LETHAL_OBSTACLE); // u = 60
}
}
int main(int argc,char**argv) {robot::Time::init();testing::InitGoogleTest(&argc,argv);return RUN_ALL_TESTS();}