15 #include <mrpt/containers/yaml.h>
16 #include <mrpt/maps/CSimplePointsMap.h>
17 #include <mrpt/math/ops_containers.h>
18 #include <mrpt/random/RandomGenerators.h>
45 MRPT_LOG_DEBUG_STREAM(
"Loading these params:\n" << c);
62 mrpt::maps::CPointsMap::Ptr outPc =
66 mrpt::maps::CPointsMap* pcPtr =
nullptr;
90 const auto& rawPc = *pcPtr;
92 mrpt::maps::CSimplePointsMap pc;
93 pc.reserve(rawPc.size());
95 const auto& xs = rawPc.getPointsBufferRef_x();
96 const auto& ys = rawPc.getPointsBufferRef_y();
97 const auto& zs = rawPc.getPointsBufferRef_z();
98 for (
size_t i = 0; i < xs.size(); i++)
102 pc.mark_as_modified();
105 outPc->reserve(outPc->size() + pc.size() / 10);
114 auto rng = mrpt::random::CRandomGenerator();
118 auto lambdaInsertPt = [&outPc](
float x,
float y,
float z) { outPc->insertPointFast(
x, y, z); };
120 size_t nonEmptyVoxels = 0;
125 if (vxl.
indices.empty())
return;
132 auto mean = mrpt::math::TPoint3Df(0, 0, 0);
133 const float inv_n = (1.0f / vxl.
indices.size());
134 for (
size_t i = 0; i < vxl.
indices.size(); i++)
136 const auto pt_idx = vxl.
indices[i];
137 mean.x += xs[pt_idx];
138 mean.y += ys[pt_idx];
139 mean.z += zs[pt_idx];
145 std::optional<float> minSqrErr;
146 std::optional<size_t> bestIdx;
148 for (
size_t i = 0; i < vxl.
indices.size(); i++)
150 const auto pt_idx = vxl.
indices[i];
151 const float sqrErr = mrpt::square(xs[pt_idx] - mean.x) +
152 mrpt::square(ys[pt_idx] - mean.y) +
153 mrpt::square(zs[pt_idx] - mean.z);
155 if (!minSqrErr.has_value() || sqrErr < *minSqrErr)
162 lambdaInsertPt(xs[*bestIdx], ys[*bestIdx], zs[*bestIdx]);
167 lambdaInsertPt(mean.x, mean.y, mean.z);
174 ? (rng.drawUniform64bit() % vxl.
indices.size())
177 const auto pt_idx = vxl.
indices.at(idxInVoxel);
178 lambdaInsertPt(xs[pt_idx], ys[pt_idx], zs[pt_idx]);
182 outPc->mark_as_modified();
184 MRPT_LOG_DEBUG_STREAM(
"Voxel counts: total=" << nonEmptyVoxels);