Pure geometry core for the upcoming wall reflector detection action. The wall line is fitted from dark rays only; each strip edge is the half-step bearing between the intensity core and its neighbour, intersected with that line, so the range bias of bright returns never enters the pose. Exactly one valid pair is required; two or more report ambiguous. Synthetic tests cover geometry, quantisation, bloom, bright range bias, realistic range noise, wall ends, occluders, ambiguity and malformed input.
203 lines
6.2 KiB
C++
203 lines
6.2 KiB
C++
#ifndef DOCK_PATTERN_DETECTOR_TEST_WALL_SCAN_SIM_H_
|
|
#define DOCK_PATTERN_DETECTOR_TEST_WALL_SCAN_SIM_H_
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
#include <limits>
|
|
#include <random>
|
|
#include <vector>
|
|
|
|
#include <robot_line_extraction/types.h>
|
|
|
|
namespace wall_scan_sim
|
|
{
|
|
struct Interval
|
|
{
|
|
double center = 0.0;
|
|
double width = 0.05;
|
|
};
|
|
|
|
struct Box
|
|
{
|
|
double min_x;
|
|
double max_x;
|
|
double min_y;
|
|
double max_y;
|
|
float intensity = 1000.0f;
|
|
};
|
|
|
|
struct Scan
|
|
{
|
|
std::vector<double> ranges;
|
|
std::vector<float> intensities;
|
|
};
|
|
|
|
struct World
|
|
{
|
|
double x = 1.0;
|
|
double y = 0.0;
|
|
double normal_yaw = 3.14159265358979323846;
|
|
double half_length = 2.0;
|
|
double min_along = -std::numeric_limits<double>::infinity();
|
|
double max_along = std::numeric_limits<double>::infinity();
|
|
double step_start = std::numeric_limits<double>::infinity();
|
|
double step_depth = 0.0;
|
|
float wall_intensity = 1000.0f;
|
|
float tape_intensity = 20000.0f;
|
|
float bloom_intensity = 9000.0f;
|
|
std::vector<Interval> tapes{{-0.2, 0.05}, {0.2, 0.05}};
|
|
std::vector<Box> boxes;
|
|
int bloom_left = 0;
|
|
int bloom_right = 0;
|
|
double bright_range_bias = 0.0;
|
|
double noise_sigma = 0.0;
|
|
unsigned int seed = 7;
|
|
};
|
|
|
|
inline robot_line_extraction::ScanGeometry geometry(double step, double phase = 0.0,
|
|
bool reversed = false)
|
|
{
|
|
robot_line_extraction::ScanGeometry scan_geometry;
|
|
scan_geometry.size = static_cast<std::size_t>(std::ceil(2.0 * 3.14159265358979323846 / step));
|
|
scan_geometry.angle_min = -3.14159265358979323846 + phase;
|
|
scan_geometry.angle_increment = step;
|
|
if (reversed)
|
|
{
|
|
scan_geometry.angle_min += (scan_geometry.size - 1) * step;
|
|
scan_geometry.angle_increment = -step;
|
|
}
|
|
return scan_geometry;
|
|
}
|
|
|
|
inline Scan rayCast(const World& world, const robot_line_extraction::ScanGeometry& scan_geometry)
|
|
{
|
|
Scan scan;
|
|
const double infinity = std::numeric_limits<double>::infinity();
|
|
scan.ranges.assign(scan_geometry.size, infinity);
|
|
scan.intensities.assign(scan_geometry.size, 0.0f);
|
|
const double normal_x = std::cos(world.normal_yaw);
|
|
const double normal_y = std::sin(world.normal_yaw);
|
|
const double tangent_x = -normal_y;
|
|
const double tangent_y = normal_x;
|
|
const double rho = normal_x * world.x + normal_y * world.y;
|
|
std::mt19937 random(world.seed);
|
|
std::normal_distribution<double> noise(0.0, world.noise_sigma);
|
|
std::vector<bool> is_tape(scan_geometry.size, false);
|
|
std::vector<bool> wall_hit(scan_geometry.size, false);
|
|
for (std::size_t index = 0; index < scan_geometry.size; ++index)
|
|
{
|
|
const double angle = scan_geometry.angle_min + index * scan_geometry.angle_increment;
|
|
const double ray_x = std::cos(angle);
|
|
const double ray_y = std::sin(angle);
|
|
const double denominator = normal_x * ray_x + normal_y * ray_y;
|
|
if (std::abs(denominator) > 1e-12)
|
|
{
|
|
double range = rho / denominator;
|
|
double along = tangent_x * (range * ray_x - world.x) + tangent_y * (range * ray_y - world.y);
|
|
if (along >= world.step_start)
|
|
{
|
|
range = (rho - world.step_depth) / denominator;
|
|
along = tangent_x * (range * ray_x - world.x) + tangent_y * (range * ray_y - world.y);
|
|
}
|
|
if (range > 0.0 && std::abs(along) <= world.half_length &&
|
|
along >= world.min_along && along <= world.max_along)
|
|
{
|
|
scan.ranges[index] = range;
|
|
wall_hit[index] = true;
|
|
scan.intensities[index] = world.wall_intensity;
|
|
for (const auto& tape : world.tapes)
|
|
{
|
|
if (std::abs(along - tape.center) <= tape.width / 2.0)
|
|
{
|
|
is_tape[index] = true;
|
|
scan.intensities[index] = world.tape_intensity;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// Ray casting với hộp chắn, lấy giao điểm gần nhất theo phương pháp slabs.
|
|
for (const auto& box : world.boxes)
|
|
{
|
|
double near = 0.0;
|
|
double far = infinity;
|
|
const auto slab = [&near, &far](double component, double minimum, double maximum)
|
|
{
|
|
if (std::abs(component) < 1e-12)
|
|
{
|
|
return minimum <= 0.0 && maximum >= 0.0;
|
|
}
|
|
double first = minimum / component;
|
|
double last = maximum / component;
|
|
if (first > last)
|
|
{
|
|
std::swap(first, last);
|
|
}
|
|
near = std::max(near, first);
|
|
far = std::min(far, last);
|
|
return far >= near;
|
|
};
|
|
if (slab(ray_x, box.min_x, box.max_x) && slab(ray_y, box.min_y, box.max_y) &&
|
|
near > 0.0 && near < scan.ranges[index])
|
|
{
|
|
scan.ranges[index] = near;
|
|
scan.intensities[index] = box.intensity;
|
|
is_tape[index] = false;
|
|
wall_hit[index] = false;
|
|
}
|
|
}
|
|
}
|
|
for (std::size_t index = 0; index < scan_geometry.size;)
|
|
{
|
|
if (!is_tape[index])
|
|
{
|
|
++index;
|
|
continue;
|
|
}
|
|
const std::size_t first = index;
|
|
while (index < scan_geometry.size && is_tape[index])
|
|
{
|
|
++index;
|
|
}
|
|
const std::size_t last = index - 1;
|
|
for (int offset = 1; offset <= world.bloom_left && static_cast<std::size_t>(offset) <= first;
|
|
++offset)
|
|
{
|
|
if (wall_hit[first - offset] && !is_tape[first - offset])
|
|
{
|
|
scan.intensities[first - offset] = world.wall_intensity +
|
|
(world.bloom_intensity - world.wall_intensity) *
|
|
static_cast<float>(world.bloom_left - offset + 1) / world.bloom_left;
|
|
}
|
|
}
|
|
for (int offset = 1; offset <= world.bloom_right && last + offset < scan_geometry.size;
|
|
++offset)
|
|
{
|
|
if (wall_hit[last + offset] && !is_tape[last + offset])
|
|
{
|
|
scan.intensities[last + offset] = world.wall_intensity +
|
|
(world.bloom_intensity - world.wall_intensity) *
|
|
static_cast<float>(world.bloom_right - offset + 1) / world.bloom_right;
|
|
}
|
|
}
|
|
}
|
|
for (std::size_t index = 0; index < scan_geometry.size; ++index)
|
|
{
|
|
if (!std::isfinite(scan.ranges[index]))
|
|
{
|
|
continue;
|
|
}
|
|
if (is_tape[index])
|
|
{
|
|
scan.ranges[index] += world.bright_range_bias;
|
|
}
|
|
if (world.noise_sigma > 0.0)
|
|
{
|
|
scan.ranges[index] += noise(random);
|
|
}
|
|
}
|
|
return scan;
|
|
}
|
|
} // namespace wall_scan_sim
|
|
#endif // DOCK_PATTERN_DETECTOR_TEST_WALL_SCAN_SIM_H_
|