Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ void observationPickingRender(const ObservationPicking& observation_picking);
void renderLoopClosure(
PointClouds& point_clouds_container, int index_loop_closure_source, int index_loop_closure_target, int before, int after);
void renderLoopClosureLabels(PointClouds& point_clouds_container);
void drawEllipsoid(const Eigen::Matrix3d& transform, const Eigen::Vector3d& center, Color color);
void renderGroundControlPoints(const GroundControlPoints& ground_control_points, const PointClouds& point_clouds_container);
void renderGroundControlPointsLabels(const GroundControlPoints& ground_control_points, const PointClouds& point_clouds_container);
void renderGNSS(const GNSS& gnss, const PointClouds& point_clouds_container);
Expand Down Expand Up @@ -4312,6 +4313,34 @@ void renderLoopClosure(
Vector3 pMidUp = Vector3{ static_cast<float>(midUp.x()), static_cast<float>(midUp.y()), static_cast<float>(midUp.z()) };
DrawCylinderEx(pMid, pMidUp, tubeRadius, tubeRadius, 8, c);
}

// px, py, pz ellipsoid of the active edge's ICP uncertainty (Monte Carlo: magenta, at the mean result; else yellow,
// at the current pose) around the index_to scan origin. The covariance is in the index_from scan's frame and is
// rotated here to world.
const auto& loop_closure = session.pose_graph_loop_closure;
if (loop_closure.icp_uncertainty_is_current())
{
const auto& uncertainty = loop_closure.icp_uncertainty;
const bool monte_carlo = uncertainty.method == ManualPoseGraphLoopClosure::IcpUncertaintyMethod::MonteCarlo;
const Eigen::Matrix<double, 6, 6>& covariance_6 = monte_carlo ? uncertainty.spread.covariance : uncertainty.residuals.covariance;
if (covariance_6.allFinite())
{
const auto& edge = loop_closure.edges[uncertainty.edge];
const Eigen::Affine3d& m_from = pointClouds[edge.index_from].m_pose;
const TaitBryanPose& pose = edge.relative_pose_tb;
Eigen::Vector3d center_local(pose.px, pose.py, pose.pz);
if (monte_carlo)
{
center_local += uncertainty.spread.mean_offset.head<3>();
}
const Eigen::Matrix3d covariance = m_from.linear() * covariance_6.topLeftCorner<3, 3>() * m_from.linear().transpose();
// Eigen decomposition rather than Cholesky: a direction can have zero variance.
const Eigen::SelfAdjointEigenSolver<Eigen::Matrix3d> eigen_solver(covariance);
const Eigen::Matrix3d transform = uncertainty.ellipsoid_sigma * eigen_solver.eigenvectors() *
eigen_solver.eigenvalues().cwiseMax(0.0).cwiseSqrt().asDiagonal();
drawEllipsoid(transform, m_from * center_local, monte_carlo ? MAGENTA : YELLOW);
}
}
}

// Was GroundControlPoints::draw_ellipse() (core/src/ground_control_points.cpp)
Expand All @@ -4323,8 +4352,13 @@ void renderLoopClosure(
void drawUncertaintyEllipse(const Eigen::Matrix3d& covar, const Eigen::Vector3d& mean, Color color)
{
Eigen::LLT<Eigen::Matrix<double, 3, 3>> cholSolver(covar);
Eigen::Matrix3d transform = cholSolver.matrixL();
drawEllipsoid(cholSolver.matrixL(), mean, color);
}

//! Wireframe of the unit sphere mapped by x -> transform * x + center.
void drawEllipsoid(const Eigen::Matrix3d& transform, const Eigen::Vector3d& center, Color color)
{
const Eigen::Vector3d& mean = center;
const double pi = 3.141592;
const double di = 0.02;
const double dj = 0.04;
Expand Down
287 changes: 266 additions & 21 deletions apps/session_uncertainty_calculation/main.cpp

Large diffs are not rendered by default.

42 changes: 36 additions & 6 deletions core/include/Core/manual_pose_graph_loop_closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,48 @@ class ManualPoseGraphLoopClosure : public PoseGraphLoopClosure
bool progress_drawn = false; //!< the progress bar has been shown at least once
};
IcpAllEdgesRun icp_all_edges_run;
//! Last "Calculate ICP uncertainty" result: the edge and the relative pose it was computed for, so it is shown only
//! while both are unchanged.
//! Methods of the "Calculate ICP uncertainty" dropdown.
enum class IcpUncertaintyMethod : int
{
PointToPoint = 0, //!< sigma0^2 * (J^T J)^-1 from point-to-point residuals
PointToPlane = 1, //!< sigma0^2 * (J^T J)^-1 from point-to-plane residuals
MonteCarlo = 2, //!< spread of ICP runs from randomly perturbed start poses
};
//! "Calculate ICP uncertainty": its settings and last result. The result is shown only while the method, the edge
//! and the relative pose it was computed for are unchanged.
struct IcpUncertainty
{
IcpUncertaintyMethod method = IcpUncertaintyMethod::PointToPlane;
int number_of_runs = 20; //!< Monte Carlo
double start_offset_m = 0.05; //!< Monte Carlo
double start_offset_deg = 0.5; //!< Monte Carlo
//! Size of the drawn translation ellipsoid, in standard deviations.
double ellipsoid_sigma = 10.0;

IcpUncertaintyMethod computed_method = IcpUncertaintyMethod::PointToPlane;
int edge = -1;
TaitBryanPose relative_pose;
TaitBryanPose point_to_point;
TaitBryanPose point_to_plane;
IcpResidualStatistics point_to_point_residuals;
IcpResidualStatistics point_to_plane_residuals;
IcpResidualStatistics residuals; //!< point-to-point, point-to-plane
IcpSpread spread; //!< Monte Carlo
};
IcpUncertainty icp_uncertainty;
//! True when edge is the active edge and its relative pose still equals relative_pose.
bool is_active_edge_at(int edge, const TaitBryanPose& relative_pose) const
{
if (edge != index_active_edge || edge < 0 || edge >= static_cast<int>(edges.size()))
{
return false;
}
const TaitBryanPose& a = edges[edge].relative_pose_tb;
const TaitBryanPose& b = relative_pose;
return a.px == b.px && a.py == b.py && a.pz == b.pz && a.om == b.om && a.fi == b.fi && a.ka == b.ka;
}
//! True when icp_uncertainty was computed with the selected method for the active edge at its current relative pose.
bool icp_uncertainty_is_current() const
{
return icp_uncertainty.computed_method == icp_uncertainty.method &&
is_active_edge_at(icp_uncertainty.edge, icp_uncertainty.relative_pose);
}
//! "Keep initial trajectory curvature": compute_pose_graph_slam() uses the initial poses as the motion model, else the current ones.
bool keep_initial_trajectory_curvature = true;

Expand Down
53 changes: 45 additions & 8 deletions core/include/Core/pair_wise_iterative_closest_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include <Core/structures.h>

#include <Eigen/Eigen>
#include <functional>
#include <vector>

#include <limits>
#include <utility>

//! Residuals behind an ICP uncertainty estimate.
//! Residuals and pose covariance behind an ICP uncertainty estimate.
struct IcpResidualStatistics
{
double sum_squared_residuals = 0.0; //!< [m^2]
Expand All @@ -16,6 +18,23 @@ struct IcpResidualStatistics
//! Standard deviation of unit weight, sqrt(sum_squared_residuals / (number_of_observations - 6)) [m]; NaN when
//! there are not more observations than the 6 pose parameters.
double sigma0 = std::numeric_limits<double>::quiet_NaN();
//! Pose covariance sigma0^2 * (J^T J)^-1, rows and columns px, py, pz, om, fi, ka ([m^2], [m*rad], [rad^2]); NaN
//! when it is not computed (too few correspondences, degenerate geometry).
Eigen::Matrix<double, 6, 6> covariance = Eigen::Matrix<double, 6, 6>::Constant(std::numeric_limits<double>::quiet_NaN());
};

//! Spread of an ICP result over runs started from randomly perturbed poses (Monte Carlo). Parameters are in the order
//! px, py, pz [m], om, fi, ka [rad].
struct IcpSpread
{
int number_of_runs = 0;
int number_of_converged_runs = 0; //!< runs where PairWiseICP::compute() succeeded
//! Sample standard deviations of the starting offsets from the reference pose.
Eigen::Matrix<double, 6, 1> start_sigma = Eigen::Matrix<double, 6, 1>::Constant(std::numeric_limits<double>::quiet_NaN());
//! Mean converged pose minus the reference one.
Eigen::Matrix<double, 6, 1> mean_offset = Eigen::Matrix<double, 6, 1>::Constant(std::numeric_limits<double>::quiet_NaN());
//! Sample covariance of the converged poses; NaN with fewer than 2 converged runs.
Eigen::Matrix<double, 6, 6> covariance = Eigen::Matrix<double, 6, 6>::Constant(std::numeric_limits<double>::quiet_NaN());
};

class PairWiseICP
Expand All @@ -24,6 +43,9 @@ class PairWiseICP
PairWiseICP() = default;
~PairWiseICP() = default;

//! false: compute() and compute_icp_montecarlo() print only failures, not every iteration.
bool verbose = true;

bool compute(
const std::vector<Eigen::Vector3d>& source,
const std::vector<Eigen::Vector3d>& target,
Expand All @@ -32,23 +54,38 @@ class PairWiseICP
Eigen::Affine3d& m_pose_result);
//! Pose standard deviations (px, py, pz in metres; om, fi, ka in radians) of an aligned source from point-to-point
//! residuals to the nearest target point within search_radious. Covariance = sigma0^2 * (J^T J)^-1,
//! sigma0^2 = sum r^2 / (3n - 6). If statistics is given, the residual statistics are written there.
TaitBryanPose compute_icp_uncertainty(
//! sigma0^2 = sum r^2 / (3n - 6).
//! @return the standard deviations and the residual statistics with the covariance
std::pair<TaitBryanPose, IcpResidualStatistics> compute_icp_uncertainty(
const std::vector<Eigen::Vector3d>& source,
const std::vector<Eigen::Vector3d>& target,
const Eigen::Affine3d& m_pose_result,
double search_radious,
IcpResidualStatistics* statistics = nullptr);
double search_radious);

//! Pose standard deviations (px, py, pz in metres; om, fi, ka in radians) of an aligned source from point-to-plane
//! residuals: each source point is matched to the plane fitted to the target points within search_radious
//! (non-planar neighbourhoods are skipped). Covariance = sigma0^2 * (J^T J)^-1, sigma0^2 = sum r^2 / (n - 6).
//! Returns infinity for every parameter when there are too few correspondences or the geometry is degenerate.
//! If statistics is given, the residual statistics are written there.
TaitBryanPose compute_icp_uncertainty_point_to_plane(
//! @return the standard deviations and the residual statistics with the covariance
std::pair<TaitBryanPose, IcpResidualStatistics> compute_icp_uncertainty_point_to_plane(
const std::vector<Eigen::Vector3d>& source,
const std::vector<Eigen::Vector3d>& target,
const Eigen::Affine3d& m_pose_result,
double search_radious);

//! Monte Carlo ICP: runs compute() number_of_runs times from m_pose plus a random Tait-Bryan offset, uniform in
//! [-start_offset_m, start_offset_m] on px, py, pz and [-start_offset_rad, start_offset_rad] on om, fi, ka, and
//! returns the spread of the results around m_pose. The offsets come from a fixed seed, so the result is
//! repeatable. Takes time.
//! @param on_run_done if given, called after each run with the number of runs done
IcpSpread compute_icp_montecarlo(
const std::vector<Eigen::Vector3d>& source,
const std::vector<Eigen::Vector3d>& target,
const Eigen::Affine3d& m_pose,
double search_radious,
IcpResidualStatistics* statistics = nullptr);
int number_of_iterations,
int number_of_runs,
double start_offset_m,
double start_offset_rad,
const std::function<void(int run)>& on_run_done = {});
};
21 changes: 16 additions & 5 deletions core/include/Core/pose_graph_loop_closure.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,27 @@ class PoseGraphLoopClosure
//! Standard deviations of one edge's relative pose (px, py, pz in metres; om, fi, ka in radians) from the ICP
//! normal matrix at the current relative pose, on the same clouds as run_icp().
//! @param point_to_plane false: point-to-point residuals (as run_icp() minimises); true: point-to-plane residuals
//! @param statistics if given, receives the residual statistics (sum of squared residuals, sigma0, ...)
//! @return infinity for every parameter when the geometry does not constrain the pose
TaitBryanPose compute_icp_uncertainty(
//! @return the standard deviations (infinity for every parameter when the geometry does not constrain the pose) and
//! the residual statistics with the covariance
std::pair<TaitBryanPose, IcpResidualStatistics> compute_icp_uncertainty(
const PointClouds& point_clouds_container,
int index_active_edge,
double search_radius,
int num_edge_extended_before,
int num_edge_extended_after,
bool point_to_plane,
IcpResidualStatistics* statistics = nullptr);
bool point_to_plane);
//! Monte Carlo ICP ("ICP spread") of one edge's relative pose: PairWiseICP::compute_icp_montecarlo() on the same
//! clouds as run_icp(). The edge is left unchanged.
IcpSpread compute_icp_montecarlo(
const PointClouds& point_clouds_container,
int index_active_edge,
double search_radius,
int number_of_iterations,
int num_edge_extended_before,
int num_edge_extended_after,
int number_of_runs,
double start_offset_m,
double start_offset_rad) const;
//! Search radii for coarse-to-fine ICP: `start_radius`, then those of the per-edge ICP buttons' steps (2.0, 1.0,
//! 0.5, 0.25, 0.1 m) below it.
static std::vector<float> icp_search_radii(float start_radius);
Expand Down
Loading
Loading