feat(depth): observed_volume depth clearing as deployed 2026-10-14

Baseline commit of the working-tree refactor built on 2026-10-14 17:24 and
running on the KIT robot, recorded in one commit before further changes.

- VoxelLayer clearing looks up TF at the depth image stamp.
- ObservationBuffer: per-source cloud preparation at the measurement stamp,
  height/range gate before the PCL VoxelGrid.
- DepthFramePairer: pairs the marking cloud with the clearing image by
  source and stamp, bounded queue and age.
- DepthVisibilityChecker and the observed_volume clearing mode in VoxelLayer;
  the legacy frustum mode stays the default.
- Tests: depth_observation_buffer_test, depth_visibility_test.
This commit is contained in:
2026-10-15 11:37:56 +07:00
parent a63c084002
commit 2ca0bcc1c1
15 changed files with 1336 additions and 19 deletions

View File

@@ -122,6 +122,25 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
// now we need to split the topics based on whitespace which we can use a stringstream for
std::stringstream ss(topics_string);
struct CloudSourceDefinition
{
boost::shared_ptr<ObservationBuffer> buffer;
CloudProcessingConfig processing;
std::string topic, data_type;
double min_height, max_height, keep_time, expected_period;
bool marking, clearing;
};
struct DepthSourceDefinition
{
boost::shared_ptr<ObservationBuffer> buffer;
DepthFrustumConfig config;
std::string topic, data_type;
double expected_period;
};
std::map<std::string, CloudSourceDefinition> cloud_sources;
std::vector<DepthSourceDefinition> depth_sources;
paired_depth_sources_.clear();
std::string source;
callback_infos_.clear();
@@ -225,6 +244,41 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
if (priv_nh.hasParam("frustum_depth_camera_topic"))
priv_nh.getParam("frustum_depth_camera_topic", depth_camera_data_topic_);
const auto read_depth_option = [&](const char* key, auto& value) {
value = loadParam(layer[source], key, value);
if (priv_nh.hasParam(key)) priv_nh.getParam(key, value);
};
std::string depth_mode = "legacy";
read_depth_option("depth_clearing_mode", depth_mode);
if (depth_mode != "legacy" && depth_mode != "observed_volume")
throw std::invalid_argument("unknown depth_clearing_mode: " + depth_mode);
frustum_config.observed_volume_clearing = depth_mode == "observed_volume";
read_depth_option("paired_marking_source", frustum_config.paired_marking_source);
int pair_queue = 4, projection_margin = 1;
read_depth_option("depth_pair_queue_size", pair_queue);
read_depth_option("depth_pair_max_wait_s", frustum_config.pair_max_wait_s);
read_depth_option("depth_observation_max_age_s", frustum_config.observation_max_age_s);
read_depth_option("depth_valid_min_z_m", frustum_config.valid_min_z_m);
read_depth_option("depth_valid_max_z_m", frustum_config.valid_max_z_m);
read_depth_option("depth_free_margin_m", frustum_config.free_margin_m);
read_depth_option("depth_projection_margin_px", projection_margin);
read_depth_option("depth_voxel_z_resolution_m", frustum_config.voxel_z_resolution_m);
if (pair_queue < 1 || pair_queue > 64 || projection_margin < 0 || projection_margin > 16)
throw std::invalid_argument("depth pair queue/projection margin out of bounds");
frustum_config.pair_queue_size = static_cast<unsigned int>(pair_queue);
frustum_config.projection_margin_px = static_cast<unsigned int>(projection_margin);
if (frustum_config.observed_volume_clearing &&
(!frustum_clearing_enabled || data_type != "DepthCameraData"))
throw std::invalid_argument("observed_volume requires a DepthCameraData frustum source");
if (frustum_config.observed_volume_clearing &&
(!std::isfinite(frustum_config.valid_min_z_m) || frustum_config.valid_min_z_m <= 0 ||
!std::isfinite(frustum_config.valid_max_z_m) || frustum_config.valid_max_z_m <= frustum_config.valid_min_z_m ||
!std::isfinite(frustum_config.free_margin_m) || frustum_config.free_margin_m < 0))
throw std::invalid_argument("observed_volume requires finite optical depth limits and free margin");
if (frustum_config.observed_volume_clearing &&
(!std::isfinite(frustum_config.voxel_z_resolution_m) || frustum_config.voxel_z_resolution_m <= 0))
throw std::invalid_argument("observed_volume requires finite positive voxel z resolution");
frustum_config.pixel_step = static_cast<unsigned int>(std::max(1, frustum_pixel_step));
frustum_config.clear_left_border_px =
static_cast<unsigned int>(std::max(0, frustum_clear_left_border));
@@ -252,6 +306,25 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
if (priv_nh.hasParam("raytrace_range"))
priv_nh.getParam("raytrace_range", raytrace_range);
CloudProcessingConfig cloud_processing;
cloud_processing.transform_at_stamp =
loadParam(layer[source], "cloud_transform_at_stamp", false);
const YAML::Node voxel_filter = layer[source]["marking_voxel_filter"];
if (voxel_filter && voxel_filter.IsMap())
{
cloud_processing.voxel_filter_enabled = loadParam(voxel_filter, "enabled", false);
cloud_processing.voxel_leaf_size = loadParam(voxel_filter, "leaf_size_m", 0.05);
}
if (priv_nh.hasParam("cloud_transform_at_stamp"))
priv_nh.getParam("cloud_transform_at_stamp", cloud_processing.transform_at_stamp);
if (priv_nh.hasParam("marking_voxel_filter/enabled"))
priv_nh.getParam("marking_voxel_filter/enabled", cloud_processing.voxel_filter_enabled);
if (priv_nh.hasParam("marking_voxel_filter/leaf_size_m"))
priv_nh.getParam("marking_voxel_filter/leaf_size_m", cloud_processing.voxel_leaf_size);
if (cloud_processing.voxel_filter_enabled &&
(data_type != "PointCloud2" || !marking || clearing || frustum_clearing_enabled))
throw std::invalid_argument("marking_voxel_filter requires a marking-only PointCloud2 source");
if (!(data_type == "PointCloud2" || data_type == "PointCloud" || data_type == "LaserScan" || data_type == "DepthCameraData"))
{
robot::log_error("Only topics that use point clouds or laser scans are currently supported\n");
@@ -279,6 +352,16 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
> (new ObservationBuffer(topic, observation_keep_time, expected_update_rate, min_obstacle_height,
max_obstacle_height, obstacle_range, raytrace_range, *tf_, global_frame_,
sensor_frame, transform_tolerance)));
observation_buffers_.back()->setCloudProcessingConfig(cloud_processing);
cloud_sources.emplace(source, CloudSourceDefinition{observation_buffers_.back(), cloud_processing,
topic, data_type, min_obstacle_height, max_obstacle_height, observation_keep_time,
expected_update_rate, marking, clearing});
robot::log_info("source %s: cloud_tf=%s height=[%.3f, %.3f] m range=%.3f m; "
"PCL_after_height_range=%s leaf=%.3f m\n", source.c_str(),
cloud_processing.transform_at_stamp ? "measurement_stamp" : "latest",
min_obstacle_height, max_obstacle_height, obstacle_range,
cloud_processing.voxel_filter_enabled ? "true" : "false",
cloud_processing.voxel_leaf_size);
if (marking)
marking_buffers_.push_back(observation_buffers_.back());
@@ -301,6 +384,8 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
max_obstacle_height, obstacle_range, raytrace_range, frustum_config,
*tf_, global_frame_,
sensor_frame, transform_tolerance)));
depth_sources.push_back({depth_observation_buffers_.back(), frustum_config,
topic, data_type, expected_update_rate});
}
robot::log_info(
@@ -308,7 +393,35 @@ bool ObstacleLayer::getParams(const std::string& config_file_name, robot::NodeHa
"expected update rate: %.2f, observation persistence: %.2f\n",
topic.c_str(), global_frame_.c_str(), expected_update_rate, observation_keep_time);
}
}
for (const auto& depth_source : depth_sources)
{
if (!depth_source.config.observed_volume_clearing) continue;
const auto marker = cloud_sources.find(depth_source.config.paired_marking_source);
if (marker == cloud_sources.end() || marker->second.data_type != "PointCloud2" ||
!marker->second.marking || marker->second.clearing || marker->second.keep_time != 0 ||
!marker->second.processing.transform_at_stamp || getType() != LayerType::VOXEL_LAYER)
throw std::invalid_argument("observed_volume needs a timestamped marking-only PointCloud2 pair with persistence=0");
for (const auto& other : cloud_sources)
if (other.first != marker->first && (other.second.marking || other.second.clearing))
throw std::invalid_argument("observed_volume depth must use its own VoxelLayer; mixed sensor ownership is unsupported");
if (!paired_depth_sources_.empty() || depth_sources.size() != 1)
throw std::invalid_argument("observed_volume supports one depth camera per VoxelLayer");
const auto& m = marker->second;
double period = m.expected_period;
if (period <= 0 || (depth_source.expected_period > 0 && depth_source.expected_period < period))
period = depth_source.expected_period;
auto paired = boost::make_shared<PairedDepthSource>(depth_source.config, period);
paired->cloud_topic = m.topic; paired->depth_topic = depth_source.topic;
paired->marking_buffer = m.buffer; paired->depth_buffer = depth_source.buffer;
paired->min_height = m.min_height; paired->max_height = m.max_height;
paired_depth_sources_.push_back(paired);
marking_buffers_.erase(std::remove(marking_buffers_.begin(), marking_buffers_.end(), m.buffer), marking_buffers_.end());
robot::log_info("depth clearing observed_volume: paired %s + %s, height=[%.3f, %.3f] m; "
"unknown/occluded/outside FOV retained; queue=%u age=%.3f s; one valid free frame\n",
paired->cloud_topic.c_str(), paired->depth_topic.c_str(), m.min_height, m.max_height,
paired->config.pair_queue_size, paired->config.observation_max_age_s);
}
}
catch (const YAML::BadFile& e) {
robot::log_error("Cannot open YAML file: %s\n", e.what());
@@ -399,7 +512,11 @@ void ObstacleLayer::handleImpl(const void* data,
topic == callback_depth_infos_[i].topic)
{
// robot::log_error_throttle(1.0,"TEST");
depthImageCallback(depth_camera_data_ptr, buffer);
bool paired = false;
for (const auto& source : paired_depth_sources_)
if (source->depth_topic == topic)
{ source->pairer.addDepth(depth_camera_data_ptr); paired = true; break; }
if (!paired) depthImageCallback(depth_camera_data_ptr, buffer);
}
}
}
@@ -448,7 +565,11 @@ void ObstacleLayer::handleImpl(const void* data,
{
robot::log_error("obstacle_layer: inf_is_valid option is not applicable to PointCloud observations.\n");
}
pointCloud2Callback(*static_cast<const robot_sensor_msgs::PointCloud2*>(data), buffer);
bool paired = false;
for (const auto& source : paired_depth_sources_)
if (source->cloud_topic == topic)
{ source->pairer.addCloud(*static_cast<const robot_sensor_msgs::PointCloud2*>(data)); paired = true; break; }
if (!paired) pointCloud2Callback(*static_cast<const robot_sensor_msgs::PointCloud2*>(data), buffer);
}
// else
// {
@@ -744,6 +865,10 @@ bool ObstacleLayer::getFrustumClearingObservations(std::vector<DepthCameraObserv
for (const boost::shared_ptr<ObservationBuffer>& buffer : depth_observation_buffers_)
{
bool paired = false;
for (const auto& source : paired_depth_sources_)
if (source->depth_buffer == buffer) { paired = true; break; }
if (paired) continue;
buffer->lock();
buffer->getDepthObservations(frustum_clearing_observations);
current = buffer->isCurrent() && current;
@@ -754,6 +879,18 @@ bool ObstacleLayer::getFrustumClearingObservations(std::vector<DepthCameraObserv
return current;
}
bool ObstacleLayer::pairedDepthCurrent() const
{
bool current = true;
for (const auto& source : paired_depth_sources_) current = source->pairer.isCurrent() && current;
return current;
}
void ObstacleLayer::resetPairedDepth()
{
for (const auto& source : paired_depth_sources_) source->pairer.reset();
}
void ObstacleLayer::raytraceFreespace(const Observation& clearing_observation, double* min_x, double* min_y,
double* max_x, double* max_y)
{