15 #include <mrpt/core/exceptions.h>
20 const float* xs,
const float* ys,
const float* zs,
21 mrpt::optional_ref<
const std::vector<size_t>> indices,
22 std::optional<size_t> totalCount)
26 mrpt::math::TPoint3Df mean{0, 0, 0};
27 mrpt::math::CMatrixFixed<double, 3, 3> mat_a;
34 THROW_EXCEPTION(
"totalCount: at least 3 points required.");
36 const float inv_n = (1.0f / *totalCount);
37 for (
size_t i = 0; i < *totalCount; i++)
44 for (
size_t i = 0; i < *totalCount; i++)
46 const auto a = mrpt::math::TPoint3Df(xs[i], ys[i], zs[i]) - mean;
47 mat_a(0, 0) += a.x * a.x;
48 mat_a(1, 0) += a.x * a.y;
49 mat_a(2, 0) += a.x * a.z;
50 mat_a(1, 1) += a.y * a.y;
51 mat_a(2, 1) += a.y * a.z;
52 mat_a(2, 2) += a.z * a.z;
58 ASSERTMSG_(indices,
"Provide either optional<> indices or totalCount.");
59 const auto& idxs = indices->get();
62 THROW_EXCEPTION(
"indices: at least 3 points required.");
64 const float inv_n = (1.0f / idxs.size());
65 for (
size_t i = 0; i < idxs.size(); i++)
67 const auto pt_idx = idxs[i];
73 for (
size_t i = 0; i < idxs.size(); i++)
75 const auto pt_idx = idxs[i];
77 mrpt::math::TPoint3Df(xs[pt_idx], ys[pt_idx], zs[pt_idx]) -
79 mat_a(0, 0) += a.x * a.x;
80 mat_a(1, 0) += a.x * a.y;
81 mat_a(2, 0) += a.x * a.z;
82 mat_a(1, 1) += a.y * a.y;
83 mat_a(2, 1) += a.y * a.z;
84 mat_a(2, 2) += a.z * a.z;
90 mat_a(0, 1) = mat_a(1, 0);
91 mat_a(0, 2) = mat_a(2, 0);
92 mat_a(1, 2) = mat_a(2, 1);
95 ret.
meanCov = {mrpt::poses::CPoint3D(mean), mat_a};
99 mrpt::math::CMatrixFixed<double, 3, 3> eigVectors;
100 std::vector<double> eigVals;
102 mat_a.eig_symmetric(eigVectors, eigVals);
104 for (
int i = 0; i < 3; i++)
107 const auto ev = eigVectors.extractColumn<mrpt::math::TVector3D>(i);
117 const std::vector<mrpt::math::TPoint3Df>& pts, std::vector<float>& xs,
118 std::vector<float>& ys, std::vector<float>& zs)
120 const auto N = pts.size();
124 for (
size_t i = 0; i < N; i++)