40 #include <pcl/common/point_tests.h>
41 #include <pcl/search/brute_force.h>
45 template <
typename Po
intT>
float
49 return (point1.getVector3fMap () - point2.getVector3fMap ()).squaredNorm ();
53 template <
typename Po
intT>
int
55 const PointT& point,
int k,
Indices& k_indices, std::vector<float>& k_distances)
const
57 assert (
isFinite (point) &&
"Invalid (NaN, Inf) point coordinates given to nearestKSearch!");
65 return denseKSearch (point, k, k_indices, k_distances);
66 return sparseKSearch (point, k, k_indices, k_distances);
70 template <
typename Po
intT>
int
72 const PointT &point,
int k,
Indices &k_indices, std::vector<float> &k_distances)
const
75 std::vector<Entry> result;
77 std::priority_queue<Entry> queue;
80 auto iIt = indices_->cbegin ();
81 auto iEnd = indices_->cbegin () + std::min (
static_cast<unsigned> (k),
static_cast<unsigned> (indices_->size ()));
82 for (; iIt != iEnd; ++iIt)
83 result.push_back (Entry (*iIt, getDistSqr ((*input_)[*iIt], point)));
85 queue = std::priority_queue<Entry> (result.begin (), result.end ());
89 for (; iIt != indices_->cend (); ++iIt)
91 entry.distance = getDistSqr ((*input_)[*iIt], point);
92 if (queue.top ().distance > entry.distance)
103 for (entry.index = 0; entry.index < std::min<pcl::index_t> (k, input_->size ()); ++entry.index)
105 entry.distance = getDistSqr ((*input_)[entry.index], point);
106 result.push_back (entry);
109 queue = std::priority_queue<Entry> (result.begin (), result.end ());
112 for (; entry.index <
static_cast<pcl::index_t>(input_->size ()); ++entry.index)
114 entry.distance = getDistSqr ((*input_)[entry.index], point);
115 if (queue.top ().distance > entry.distance)
123 k_indices.resize (queue.size ());
124 k_distances.resize (queue.size ());
125 std::size_t idx = queue.size () - 1;
126 while (!queue.empty ())
128 k_indices [idx] = queue.top ().index;
129 k_distances [idx] = queue.top ().distance;
134 return (
static_cast<int> (k_indices.size ()));
138 template <
typename Po
intT>
int
140 const PointT &point,
int k,
Indices &k_indices, std::vector<float> &k_distances)
const
143 std::vector<Entry> result;
146 std::priority_queue<Entry> queue;
149 auto iIt =indices_->cbegin ();
150 for (; iIt != indices_->cend () && result.size () <
static_cast<unsigned> (k); ++iIt)
152 if (std::isfinite ((*input_)[*iIt].x))
153 result.push_back (Entry (*iIt, getDistSqr ((*input_)[*iIt], point)));
156 queue = std::priority_queue<Entry> (result.begin (), result.end ());
161 for (; iIt != indices_->cend (); ++iIt)
163 if (!std::isfinite ((*input_)[*iIt].x))
166 entry.distance = getDistSqr ((*input_)[*iIt], point);
167 if (queue.top ().distance > entry.distance)
178 for (entry.index = 0; (entry.index <
static_cast<pcl::index_t>(input_->size ())) && (result.size () <
static_cast<std::size_t
> (k)); ++entry.index)
180 if (std::isfinite ((*input_)[entry.index].x))
182 entry.distance = getDistSqr ((*input_)[entry.index], point);
183 result.push_back (entry);
186 queue = std::priority_queue<Entry> (result.begin (), result.end ());
189 for (; entry.index <
static_cast<pcl::index_t>(input_->size ()); ++entry.index)
191 if (!std::isfinite ((*input_)[entry.index].x))
194 entry.distance = getDistSqr ((*input_)[entry.index], point);
195 if (queue.top ().distance > entry.distance)
203 k_indices.resize (queue.size ());
204 k_distances.resize (queue.size ());
205 std::size_t idx = queue.size () - 1;
206 while (!queue.empty ())
208 k_indices [idx] = queue.top ().index;
209 k_distances [idx] = queue.top ().distance;
213 return (
static_cast<int> (k_indices.size ()));
217 template <
typename Po
intT>
int
219 const PointT& point,
double radius,
220 Indices &k_indices, std::vector<float> &k_sqr_distances,
221 unsigned int max_nn)
const
225 std::size_t reserve = max_nn;
229 reserve = std::min (indices_->size (), input_->size ());
231 reserve = input_->size ();
233 k_indices.reserve (reserve);
234 k_sqr_distances.reserve (reserve);
238 for (
const auto& idx : *indices_)
240 distance = getDistSqr ((*input_)[idx], point);
243 k_indices.push_back (idx);
244 k_sqr_distances.push_back (
distance);
245 if (k_indices.size () == max_nn)
252 for (std::size_t index = 0; index < input_->size (); ++index)
254 distance = getDistSqr ((*input_)[index], point);
257 k_indices.push_back (index);
258 k_sqr_distances.push_back (
distance);
259 if (k_indices.size () == max_nn)
266 this->sortResults (k_indices, k_sqr_distances);
268 return (
static_cast<int> (k_indices.size ()));
272 template <
typename Po
intT>
int
274 const PointT& point,
double radius,
275 Indices &k_indices, std::vector<float> &k_sqr_distances,
276 unsigned int max_nn)
const
280 std::size_t reserve = max_nn;
284 reserve = std::min (indices_->size (), input_->size ());
286 reserve = input_->size ();
288 k_indices.reserve (reserve);
289 k_sqr_distances.reserve (reserve);
294 for (
const auto& idx : *indices_)
296 if (!std::isfinite ((*input_)[idx].x))
299 distance = getDistSqr ((*input_)[idx], point);
302 k_indices.push_back (idx);
303 k_sqr_distances.push_back (
distance);
304 if (k_indices.size () == max_nn)
311 for (std::size_t index = 0; index < input_->size (); ++index)
313 if (!std::isfinite ((*input_)[index].x))
315 distance = getDistSqr ((*input_)[index], point);
318 k_indices.push_back (index);
319 k_sqr_distances.push_back (
distance);
320 if (k_indices.size () == max_nn)
327 this->sortResults (k_indices, k_sqr_distances);
329 return (
static_cast<int> (k_indices.size ()));
333 template <
typename Po
intT>
int
336 std::vector<float> &k_sqr_distances,
unsigned int max_nn)
const
338 assert (
isFinite (point) &&
"Invalid (NaN, Inf) point coordinates given to nearestKSearch!");
341 k_sqr_distances.clear ();
345 if (input_->is_dense)
346 return denseRadiusSearch (point, radius, k_indices, k_sqr_distances, max_nn);
347 return sparseRadiusSearch (point, radius, k_indices, k_sqr_distances, max_nn);
350 #define PCL_INSTANTIATE_BruteForce(T) template class PCL_EXPORTS pcl::search::BruteForce<T>;
Implementation of a simple brute force search algorithm.
int nearestKSearch(const PointT &point, int k, Indices &k_indices, std::vector< float > &k_distances) const override
Search for the k-nearest neighbors for the given query point.
int radiusSearch(const PointT &point, double radius, Indices &k_indices, std::vector< float > &k_sqr_distances, unsigned int max_nn=0) const override
Search for all the nearest neighbors of the query point in a given radius.
float distance(const PointT &p1, const PointT &p2)
bool isFinite(const PointT &pt)
Tests if the 3D components of a point are all finite param[in] pt point to be tested return true if f...
detail::int_type_t< detail::index_type_size, detail::index_type_signed > index_t
Type used for an index in PCL.
IndicesAllocator<> Indices
Type used for indices in PCL.
A point structure representing Euclidean xyz coordinates, and the RGB color.