14 #include <mrpt/core/exceptions.h>
15 #include <mrpt/core/round.h>
16 #include <mrpt/version.h>
18 #if defined(MP2P_HAS_TBB)
19 #include <tbb/blocked_range.h>
20 #include <tbb/parallel_reduce.h>
29 mrpt::system::COutputLogger::setLoggerName(
30 "Matcher_Points_DistanceThreshold");
34 const mrpt::containers::yaml& params)
44 const mrpt::maps::CMetricMap& pcGlobalMap,
45 const mrpt::maps::CPointsMap& pcLocal,
46 const mrpt::poses::CPose3D& localPose,
MatchState& ms,
58 const mrpt::maps::NearestNeighborsCapable& nnGlobal =
64 if (pcGlobalMap.isEmpty() || pcLocal.empty())
return;
70 if (!pcGlobalMap.boundingBox().intersection(
71 {tl.localMin, tl.localMax},
76 out.paired_pt2pt.reserve(
out.paired_pt2pt.size() + pcLocal.size());
80 const float maxDistForCorrespondenceSquared = mrpt::square(
threshold);
81 const float angularThresholdFactorSquared =
84 const auto& lxs = pcLocal.getPointsBufferRef_x();
85 const auto& lys = pcLocal.getPointsBufferRef_y();
86 const auto& lzs = pcLocal.getPointsBufferRef_z();
87 const size_t nLocalPts = lxs.size();
91 nnGlobal.nn_prepare_for_3d_queries();
93 const auto lambdaAddPair =
94 [
this, &ms, &globalName, &localName, &lxs, &lys, &lzs](
95 mrpt::tfest::TMatchingPairList& outPairs,
const size_t localIdx,
96 const mrpt::math::TPoint3Df& globalPt,
const uint64_t globalIdxOrID,
106 auto& p = outPairs.emplace_back();
108 p.globalIdx = globalIdxOrID;
109 p.localIdx = localIdx;
111 p.local = {lxs[localIdx], lys[localIdx], lzs[localIdx]};
113 p.errorSquareAfterTransformation = errSqr;
125 #if defined(MP2P_HAS_TBB)
128 using Result = mrpt::tfest::TMatchingPairList;
130 auto newPairs = tbb::parallel_reduce(
132 tbb::blocked_range<size_t>{0, nLocalPts},
136 [&](
const tbb::blocked_range<size_t>& r, Result
res) -> Result
138 res.reserve(r.size());
139 std::vector<uint64_t> neighborIndices;
140 std::vector<float> neighborSqrDists;
141 std::vector<mrpt::math::TPoint3Df> neighborPts;
142 for (
size_t i = r.begin(); i < r.end(); i++)
144 const size_t localIdx = tl.
idxs.has_value() ? (*tl.
idxs)[i] : i;
154 const float localNormSqr =
155 mrpt::square(lx) + mrpt::square(ly) + mrpt::square(lz);
161 neighborIndices.resize(1);
162 neighborSqrDists.resize(1);
163 neighborPts.resize(1);
165 if (!nnGlobal.nn_single_search(
167 neighborPts[0], neighborSqrDists[0],
170 neighborIndices.clear();
171 neighborSqrDists.clear();
179 nnGlobal.nn_radius_search(
181 maxDistForCorrespondenceSquared, neighborPts,
186 for (
size_t k = 0; k < neighborIndices.size(); k++)
188 const auto tentativeErrSqr = neighborSqrDists.at(k);
190 const float finalThresSqr =
191 maxDistForCorrespondenceSquared +
192 angularThresholdFactorSquared * localNormSqr;
194 if (tentativeErrSqr >= finalThresSqr)
198 res, localIdx, neighborPts.at(k), neighborIndices.at(k),
205 [](Result a,
const Result& b) -> Result
208 a.end(), std::make_move_iterator(b.begin()),
209 std::make_move_iterator(b.end()));
213 out.paired_pt2pt.insert(
214 out.paired_pt2pt.end(), std::make_move_iterator(newPairs.begin()),
215 std::make_move_iterator(newPairs.end()));
218 out.paired_pt2pt.reserve(nLocalPts);
220 std::vector<uint64_t> neighborIndices;
221 std::vector<float> neighborSqrDists;
222 std::vector<mrpt::math::TPoint3Df> neighborPts;
224 for (
size_t i = 0; i < nLocalPts; i++)
226 const size_t localIdx = tl.
idxs.has_value() ? (*tl.
idxs)[i] : i;
236 const float localNormSqr =
237 mrpt::square(lx) + mrpt::square(ly) + mrpt::square(lz);
243 neighborIndices.resize(1);
244 neighborSqrDists.resize(1);
245 neighborPts.resize(1);
247 if (!nnGlobal.nn_single_search(
249 neighborPts[0], neighborSqrDists[0], neighborIndices[0]))
251 neighborIndices.clear();
252 neighborSqrDists.clear();
258 nnGlobal.nn_multiple_search(
265 for (
size_t k = 0; k < neighborIndices.size(); k++)
267 const auto tentativeErrSqr = neighborSqrDists.at(k);
269 const float finalThresSqr =
270 maxDistForCorrespondenceSquared +
271 angularThresholdFactorSquared * localNormSqr;
273 if (tentativeErrSqr >= finalThresSqr)
277 out.paired_pt2pt, localIdx, neighborPts.at(k),
278 neighborIndices.at(k), tentativeErrSqr);