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, std::optional<size_t> totalCount)
25 mrpt::math::TPoint3Df mean{0, 0, 0};
26 mrpt::math::CMatrixFixed<double, 3, 3> mat_a;
34 THROW_EXCEPTION(
"totalCount: at least 3 points required.");
37 const float inv_n = 1.0f /
static_cast<float>(*totalCount);
38 for (
size_t i = 0; i < *totalCount; i++)
45 for (
size_t i = 0; i < *totalCount; i++)
47 const auto a = mrpt::math::TPoint3Df(xs[i], ys[i], zs[i]) - mean;
48 mat_a(0, 0) += a.x * a.x;
49 mat_a(1, 0) += a.x * a.y;
50 mat_a(2, 0) += a.x * a.z;
51 mat_a(1, 1) += a.y * a.y;
52 mat_a(2, 1) += a.y * a.z;
53 mat_a(2, 2) += a.z * a.z;
59 ASSERTMSG_(indices,
"Provide either optional<> indices or totalCount.");
60 const auto& idxs = indices->get();
64 THROW_EXCEPTION(
"indices: at least 3 points required.");
67 const float inv_n = 1.0f /
static_cast<float>(idxs.size());
68 for (
size_t i = 0; i < idxs.size(); i++)
70 const auto pt_idx = idxs[i];
76 for (
size_t i = 0; i < idxs.size(); i++)
78 const auto pt_idx = idxs[i];
79 const auto a = mrpt::math::TPoint3Df(xs[pt_idx], ys[pt_idx], zs[pt_idx]) - mean;
80 mat_a(0, 0) += a.x * a.x;
81 mat_a(1, 0) += a.x * a.y;
82 mat_a(2, 0) += a.x * a.z;
83 mat_a(1, 1) += a.y * a.y;
84 mat_a(2, 1) += a.y * a.z;
85 mat_a(2, 2) += a.z * a.z;
91 mat_a(0, 1) = mat_a(1, 0);
92 mat_a(0, 2) = mat_a(2, 0);
93 mat_a(1, 2) = mat_a(2, 1);
96 ret.
meanCov = {mrpt::poses::CPoint3D(mean), mat_a};
100 mrpt::math::CMatrixFixed<double, 3, 3> eigVectors;
101 std::vector<double> eigVals;
103 mat_a.eig_symmetric(eigVectors, eigVals);
105 for (
int i = 0; i < 3; i++)
108 const auto ev = eigVectors.extractColumn<mrpt::math::TVector3D>(i);
118 const std::vector<mrpt::math::TPoint3Df>& pts, std::vector<float>& xs, std::vector<float>& ys,
119 std::vector<float>& zs)
121 const auto N = pts.size();
125 for (
size_t i = 0; i < N; i++)