extract() dropped any run whose weighted fit ran out of iterations. That was too harsh: a failed refinement loses only the covariance, while the angle, radius, endpoints and support all remain those of the endpoint fit and stay usable. Measured on a V dock plate at 2 m with 34 rays on the plate: both wings split correctly with 17-18 points each and lengths correct to a few millimetres, yet one of the two vanished on roughly half of the noise realizations because its fit hit the 50-iteration cap. Everything downstream then saw one line where there were two, and the corner disappeared. End-to-end V detection rate was 48%; keeping the segment takes it to 100% across 0-5 mm tape protrusion at both 2 m and 3 m. Line now reports hasCovariance(), false when the fit did not converge, and mergeLines() refuses to merge such a segment - merging decides by comparing line parameters against their own covariance, so a meaningless covariance must not be allowed to make that decision. TheLeastSquaresLoopIsBounded keeps guarding the iteration cap itself; the new ALineThatFailedToRefineIsStillReturned guards against dropping the segment, and fails if extract() goes back to discarding it.
159 lines
5.2 KiB
C++
159 lines
5.2 KiB
C++
/*********************************************************************
|
|
*
|
|
* Software License Agreement (BSD License)
|
|
*
|
|
* robot_line_extraction — one fitted line segment.
|
|
*
|
|
* Author: DuongTD
|
|
* Derived from laser_line_extraction (Marc Gallant, BSD), stripped of ROS.
|
|
*********************************************************************/
|
|
#ifndef ROBOT_LINE_EXTRACTION_LINE_H_
|
|
#define ROBOT_LINE_EXTRACTION_LINE_H_
|
|
|
|
#include <array>
|
|
#include <vector>
|
|
|
|
#include <robot_line_extraction/types.h>
|
|
|
|
namespace robot_line_extraction
|
|
{
|
|
|
|
/**
|
|
* @class Line
|
|
* @brief A line in polar form (`angle`, `radius`) plus the endpoints and support that produced it.
|
|
*
|
|
* Polar rather than slope-intercept because a vertical line has no finite slope, and a laser sees
|
|
* vertical walls constantly. `radius` is the perpendicular distance from the sensor origin to the
|
|
* line and is kept non-negative; `angle` is the bearing of that perpendicular, in (-pi, pi].
|
|
*
|
|
* Two fits, used in sequence. @ref endpointFit is cheap and runs during splitting, where only a
|
|
* rough line is needed to measure how far points stray from it. @ref leastSqFit is the real fit,
|
|
* weighting each point by its own covariance, and it also produces the covariance that decides
|
|
* whether two lines are the same line.
|
|
*
|
|
* @note Holds copies of the scan data it was built from. That is deliberate: a segment outlives the
|
|
* split recursion that created it, and pointing back at a buffer being rebuilt each frame is
|
|
* how a use-after-free gets written.
|
|
*/
|
|
class Line
|
|
{
|
|
public:
|
|
/// @brief Build from a run of consecutive rays. Nothing is fitted yet.
|
|
Line(const CachedData& c_data, const RangeData& r_data, const LineExtractionParams& params,
|
|
std::vector<unsigned int> indices);
|
|
|
|
/// @brief Build from already-known parameters. Used when two lines merge into one.
|
|
Line(double angle, double radius, const std::array<double, 4>& covariance,
|
|
const std::array<double, 2>& start, const std::array<double, 2>& end,
|
|
const std::vector<unsigned int>& indices);
|
|
|
|
double getAngle() const
|
|
{
|
|
return angle_;
|
|
}
|
|
|
|
double getRadius() const
|
|
{
|
|
return radius_;
|
|
}
|
|
|
|
/// @brief Row-major 2x2 covariance of (radius, angle). Only meaningful when @ref hasCovariance.
|
|
const std::array<double, 4>& getCovariance() const
|
|
{
|
|
return covariance_;
|
|
}
|
|
|
|
/**
|
|
* @brief Whether the weighted fit converged, so the covariance means something.
|
|
*
|
|
* false = the segment carries only its endpoint fit. Angle, radius, endpoints and support are
|
|
* all still usable — the dump of a failed run shows them correct to a millimetre — but the
|
|
* covariance is whatever the iteration left behind, so nothing may decide by it. @ref
|
|
* LineExtractor::mergeLines refuses to merge such a segment for exactly that reason.
|
|
*/
|
|
bool hasCovariance() const
|
|
{
|
|
return has_covariance_;
|
|
}
|
|
|
|
const std::array<double, 2>& getStart() const
|
|
{
|
|
return start_;
|
|
}
|
|
|
|
const std::array<double, 2>& getEnd() const
|
|
{
|
|
return end_;
|
|
}
|
|
|
|
/// @brief Ray numbers supporting this line, ascending.
|
|
const std::vector<unsigned int>& getIndices() const
|
|
{
|
|
return indices_;
|
|
}
|
|
|
|
/// @brief [m] Perpendicular distance from ray @p index to this line.
|
|
double distToPoint(unsigned int index) const;
|
|
|
|
/// @brief [m] Distance between the endpoints.
|
|
double length() const;
|
|
|
|
std::size_t numPoints() const
|
|
{
|
|
return indices_.size();
|
|
}
|
|
|
|
/// @brief Cheap fit through the first and last supporting point. Used while splitting.
|
|
void endpointFit();
|
|
|
|
/**
|
|
* @brief Weighted least-squares fit, iterated to convergence, then the covariance.
|
|
*
|
|
* @return false if it did not converge within the iteration cap, leaving the ENDPOINT fit in
|
|
* place and @ref hasCovariance false. Upstream looped unbounded here; a scan that makes
|
|
* the increment oscillate then hangs the control thread outright, which is worse than a
|
|
* line that failed to refine.
|
|
*
|
|
* @note false is not a reason to discard the segment. It stops being refined, not being a
|
|
* segment — see @ref hasCovariance.
|
|
*/
|
|
bool leastSqFit();
|
|
|
|
/// @brief Slide the endpoints onto the fitted line, perpendicular to it.
|
|
void projectEndpoints();
|
|
|
|
private:
|
|
void angleFromEndpoints();
|
|
void radiusFromEndpoints();
|
|
void angleFromLeastSq();
|
|
double angleIncrement() const;
|
|
void calcCovariance();
|
|
void calcPointCovariances();
|
|
void calcPointParameters();
|
|
void calcPointScalarCovariances();
|
|
void radiusFromLeastSq();
|
|
|
|
// Declaration order is destruction and initialisation order; keep it matching the member list of
|
|
// the constructors so -Wreorder stays quiet and the two never drift apart.
|
|
CachedData c_data_;
|
|
RangeData r_data_;
|
|
LineExtractionParams params_;
|
|
std::vector<unsigned int> indices_;
|
|
|
|
PointParams p_params_;
|
|
std::vector<double> point_scalar_vars_;
|
|
std::vector<std::array<double, 4>> point_covs_;
|
|
double p_rr_ = 0.0;
|
|
|
|
double angle_ = 0.0;
|
|
double radius_ = 0.0;
|
|
std::array<double, 2> start_{ { 0.0, 0.0 } };
|
|
std::array<double, 2> end_{ { 0.0, 0.0 } };
|
|
std::array<double, 4> covariance_{ { 0.0, 0.0, 0.0, 0.0 } };
|
|
bool has_covariance_ = false;
|
|
};
|
|
|
|
} // namespace robot_line_extraction
|
|
|
|
#endif // ROBOT_LINE_EXTRACTION_LINE_H_
|