Files
dock_pattern_detector/test/leg_scan_sim.h
duongtd c568e90c53 feat(leg_pattern): add LegPatternMatcher for 4-leg trolleys
Finds a 4-leg trolley in a raw LaserScan without line extraction:
shadow filter, leg clusters in ray order with free/occluded/attached edges,
pair hypotheses from cart_opening/cart_depth/diagonal, then a Gauss-Newton
pose fit over every leg point with an along-ray residual, Huber weights and
backtracking. Gates: pair spacing after the fit, see-through leg positions,
optional empty interior, entry side (entry tape / prior yaw / facing the
sensor), optional prior, confidence, and ambiguity between two carts.

LegProfile validates every key at load. Two gtests (7 + 23) run on a
synthetic ray-cast scan (test/leg_scan_sim.h). Used by trolley_detection_action.
2026-10-28 13:43:56 +07:00

217 lines
6.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*********************************************************************
*
* Software License Agreement (BSD License)
*
* Dựng scan TỔNG HỢP cho test dò xe hàng: đặt chân hộp/tròn, tường, vật cản vào một thế giới 2D,
* chiếu tia từ gốc, biết trước đáp án. Chỉ dùng trong test.
*
* Author: DuongTD
*********************************************************************/
#ifndef DOCK_PATTERN_DETECTOR_TEST_LEG_SCAN_SIM_H_
#define DOCK_PATTERN_DETECTOR_TEST_LEG_SCAN_SIM_H_
#include <cmath>
#include <cstddef>
#include <limits>
#include <random>
#include <vector>
#include <robot_line_extraction/types.h>
#include <dock_pattern_detector/leg_profile.h>
namespace leg_scan_sim
{
struct Segment
{
double ax, ay, bx, by; // [m]
int shape; // vật mà đoạn thuộc về
};
struct Circle
{
double cx, cy, r; // [m]
int shape;
};
/// @brief Thế giới 2D. Mỗi vật có một id; vật có id trong `bright` trả cường độ cao.
struct World
{
std::vector<Segment> segments;
std::vector<Circle> circles;
std::vector<bool> bright; // theo id vật
int next_id = 0;
int newShape(bool is_bright = false)
{
bright.push_back(is_bright);
return next_id++;
}
/// @brief Hộp tâm (cx, cy), cạnh `size_x` dọc trục x của nó, `size_y` dọc trục y, xoay `yaw`.
int addBox(double cx, double cy, double size_x, double size_y, double yaw, bool is_bright = false)
{
const int id = newShape(is_bright);
const double c = std::cos(yaw);
const double s = std::sin(yaw);
const double hx = size_x / 2.0;
const double hy = size_y / 2.0;
const double lx[4] = { hx, -hx, -hx, hx };
const double ly[4] = { hy, hy, -hy, -hy };
double px[4], py[4];
for (int i = 0; i < 4; ++i)
{
px[i] = cx + c * lx[i] - s * ly[i];
py[i] = cy + s * lx[i] + c * ly[i];
}
for (int i = 0; i < 4; ++i)
{
const int j = (i + 1) % 4;
segments.push_back({ px[i], py[i], px[j], py[j], id });
}
return id;
}
int addCircle(double cx, double cy, double r, bool is_bright = false)
{
const int id = newShape(is_bright);
circles.push_back({ cx, cy, r, id });
return id;
}
int addWall(double ax, double ay, double bx, double by)
{
const int id = newShape(false);
segments.push_back({ ax, ay, bx, by, id });
return id;
}
};
/**
* @brief Đặt 4 chân của xe: tâm (cx, cy), trục x của xe (pháp tuyến cạnh vào hướng ra ngoài) theo
* @p axis. Bỏ chân nào thì đặt `skip[k] = true` (thứ tự góc như `LegCorner`).
* @return id của 4 chân (−1 nếu bỏ).
*/
inline std::vector<int> addCart(World& w, const dock_pattern_detector::LegProfile& p, double cx,
double cy, double axis, const bool skip[4] = nullptr,
const bool taped[4] = nullptr)
{
const double hx = p.cart_depth / 2.0;
const double hy = p.cart_opening / 2.0;
const double lx[4] = { hx, hx, -hx, -hx };
const double ly[4] = { hy, -hy, hy, -hy };
const double c = std::cos(axis);
const double s = std::sin(axis);
std::vector<int> ids(4, -1);
for (int k = 0; k < 4; ++k)
{
if (skip != nullptr && skip[k])
{
continue;
}
const double x = cx + c * lx[k] - s * ly[k];
const double y = cy + s * lx[k] + c * ly[k];
const bool is_bright = taped != nullptr && taped[k];
if (p.leg_shape == dock_pattern_detector::LegShape::kRound)
{
ids[k] = w.addCircle(x, y, p.leg_width / 2.0, is_bright);
}
else
{
ids[k] = w.addBox(x, y, p.effectiveLegDepth(), p.leg_width, axis + p.leg_yaw_offset,
is_bright);
}
}
return ids;
}
/// @brief Hình học SICK S300 sim của hook_150: 541 tia trên ±1.588 rad.
inline robot_line_extraction::ScanGeometry s300Geometry()
{
robot_line_extraction::ScanGeometry g;
g.angle_min = -1.588249619;
g.size = 541;
g.angle_increment = (2.0 * 1.588249619) / static_cast<double>(g.size - 1);
return g;
}
struct Scan
{
std::vector<double> ranges;
std::vector<float> intensities;
std::vector<int> hit; // id vật mà tia trúng, −1 nếu không trúng
};
/**
* @brief Chiếu tia từ gốc. @p sigma > 0 thì cộng nhiễu Gauss vào range, hạt giống @p seed cố định
* — test vẫn tất định.
*/
inline Scan rayCast(const World& w, const robot_line_extraction::ScanGeometry& g, double sigma = 0.0,
unsigned seed = 1)
{
constexpr double kInf = std::numeric_limits<double>::infinity();
Scan scan;
scan.ranges.assign(g.size, kInf);
scan.intensities.assign(g.size, 0.0f);
scan.hit.assign(g.size, -1);
std::mt19937 rng(seed);
std::normal_distribution<double> noise(0.0, sigma > 0.0 ? sigma : 1.0);
for (std::size_t i = 0; i < g.size; ++i)
{
const double bearing = g.angle_min + static_cast<double>(i) * g.angle_increment;
const double dx = std::cos(bearing);
const double dy = std::sin(bearing);
double best = kInf;
int best_id = -1;
for (const Segment& s : w.segments)
{
const double ex = s.bx - s.ax;
const double ey = s.by - s.ay;
const double det = dx * (-ey) - dy * (-ex);
if (std::fabs(det) < 1e-12)
{
continue;
}
const double t = (s.ax * (-ey) - s.ay * (-ex)) / det;
const double u = (dx * s.ay - dy * s.ax) / det;
if (t > 0.0 && u >= 0.0 && u <= 1.0 && t < best)
{
best = t;
best_id = s.shape;
}
}
for (const Circle& c : w.circles)
{
// |t·d − c|² = r² → t² − 2t(d·c) + |c|² − r² = 0
const double b = dx * c.cx + dy * c.cy;
const double disc = b * b - (c.cx * c.cx + c.cy * c.cy - c.r * c.r);
if (disc < 0.0)
{
continue;
}
const double t = b - std::sqrt(disc);
if (t > 0.0 && t < best)
{
best = t;
best_id = c.shape;
}
}
if (best_id >= 0)
{
const double n = sigma > 0.0 ? noise(rng) : 0.0;
scan.ranges[i] = best + n;
scan.hit[i] = best_id;
scan.intensities[i] = w.bright[best_id] ? 2000.0f : 100.0f;
}
}
return scan;
}
} // namespace leg_scan_sim
#endif // DOCK_PATTERN_DETECTOR_TEST_LEG_SCAN_SIM_H_