Files
costmap_2d/include/robot_costmap_2d/depth_evidence_store.h
duongtd 9a67eee148 wip: state before depth cleanup
Uncommitted work as it ran on the KIT robot until 2026-10-20: the ROC observed_volume tuning patch on top of 63707c9 and the DepthCameraObstacleLayer plugin (clone of tsengapola/costmap_depth_camera noetic, B6 window fix), its 17 unit tests and the CMake wiring. Kept as one commit so the cleanup that follows has a recoverable starting point; not meant to be pushed.
2026-10-20 09:36:53 +07:00

93 lines
3.1 KiB
C++

#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). A marking
/// point also restarts the voxel's re-observe miss count.
void add(const VoxelKey& key, const Eigen::Vector3f& point, bool fresh);
/// Re-observe-or-clear: adds weight to the voxel's miss count and returns the new count;
/// 0 when the voxel has no evidence.
float addMiss(const VoxelKey& key, float weight);
void resetMiss(const VoxelKey& key);
/// 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
float miss = 0.f; ///< re-observe misses since last marked or confirmed
};
unsigned int points_per_voxel_;
std::unordered_map<VoxelKey, Entry, KeyHash> entries_;
};
} // namespace robot_costmap_2d
#endif // ROBOT_COSTMAP_2D_DEPTH_EVIDENCE_STORE_H_