PeriDEM 0.2.0
PeriDEM -- Peridynamics-based high-fidelity model for granular media
Loading...
Searching...
No Matches
nsearch.h
Go to the documentation of this file.
1/*
2 * -------------------------------------------
3 * Copyright (c) 2021 - 2024 Prashant K. Jha
4 * -------------------------------------------
5 * PeriDEM https://github.com/prashjha/PeriDEM
6 *
7 * Distributed under the Boost Software License, Version 1.0. (See accompanying
8 * file LICENSE)
9 */
10
11#ifndef NSEARCH_NSEARCH_H
12#define NSEARCH_NSEARCH_H
13
14#include "util/point.h" // definition of Point
15#include "util/methods.h"
16#include <cstdint> // uint8_t type
17#include <string> // size_t type
18#include <vector>
19
20#include "nflannSetup.h"
21
23namespace nsearch {
24
29
30public:
36 BaseNSearch(std::string name, size_t debug = 0)
37 : d_debug(debug), d_treeType(name) {}
38
45 virtual double updatePointCloud(const std::vector<util::Point> &x,
46 bool parallel = true) = 0;
47
52 virtual double setInputCloud() = 0;
53
62 virtual size_t radiusSearch(
63 const util::Point &searchPoint, const double &search_r,
64 std::vector<int> &neighs,
65 std::vector<float> &sqr_dist) = 0;
66
73 virtual size_t radiusSearch(
74 const util::Point &searchPoint, const double &search_r,
75 std::vector<size_t> &neighs,
76 std::vector<double> &sqr_dist) = 0;
77
91 virtual size_t radiusSearchExcludeTag(
92 const util::Point &searchPoint,
93 const double &search_r,
94 std::vector<size_t> &neighs,
95 std::vector<double> &sqr_dist,
96 const size_t &searchPointTag,
97 const std::vector<size_t> &dataTags) = 0;
98
109 const util::Point &searchPoint,
110 const double &search_r,
111 std::vector<int> &neighs,
112 std::vector<float> &sqr_dist,
113 const size_t &searchPointTag,
114 const std::vector<size_t> &dataTags) = 0;
115
130 const util::Point &searchPoint,
131 const double &search_r,
132 std::vector<size_t> &neighs,
133 std::vector<double> &sqr_dist,
134 const size_t &searchPointTag,
135 const std::vector<size_t> &dataTags) = 0;
136
147 const util::Point &searchPoint,
148 const double &search_r,
149 std::vector<int> &neighs,
150 std::vector<float> &sqr_dist,
151 const size_t &searchPointTag,
152 const std::vector<size_t> &dataTags) = 0;
153
154
161 virtual void closestPoint(
162 const util::Point &searchPoint,
163 size_t &neigh,
164 double &sqrDistNeigh) = 0;
165
166public:
168 size_t d_debug;
169
171 std::string d_treeType;
172};
173
177template <int dim = 3>
179
180public:
188 explicit NFlannSearchKd(const PointCloud &x, size_t debug = 0, size_t max_leafs = 10)
189 : BaseNSearch("nflann_kdtree", debug),
190 d_cloud(x),
191 d_tree(dim, d_cloud,
192 nanoflann::KDTreeSingleIndexAdaptorParams(max_leafs /* max leaf */)) {
193 d_params.sorted = false;
194 };
195
200 double setInputCloud() override {
201 auto t1 = steady_clock::now();
202 d_tree.buildIndex();
203 auto t2 = steady_clock::now();
204 return util::methods::timeDiff(t1, t2);
205 };
206
216 double updatePointCloud(const std::vector<util::Point> &x,
217 bool parallel = true) override {
218 return 0;
219 };
220
228 const util::Point &searchPoint, const double &search_r,
229 std::vector<size_t> &neighs,
230 std::vector<double> &sqr_dist) override {
231
232 double query_pt[3] = {searchPoint[0], searchPoint[1], searchPoint[2]};
233
234 TreeSearchRes resultSet(search_r * search_r, neighs, sqr_dist);
235 return d_tree.radiusSearchCustomCallback(&query_pt[0], resultSet, d_params);
236 };
237
245 const util::Point &searchPoint, const double &search_r,
246 std::vector<int> &neighs,
247 std::vector<float> &sqr_dist) override {
248
249 // ugly but quick fix
250 // first, get results using int and float and then convert
251 std::vector<size_t> neighs_temp;
252 std::vector<double> sqr_dist_temp;
253 auto N =
254 this->radiusSearch(searchPoint, search_r, neighs_temp, sqr_dist_temp);
255
256 if (N > 0) {
257 neighs.resize(N);
258 sqr_dist.resize(N);
259 for (size_t i=0; i<N; i++) {
260 neighs.push_back(int(neighs_temp[i]));
261 sqr_dist.push_back(float(sqr_dist_temp[i]));
262 }
263 }
264
265 return N;
266 };
267
268
279 const util::Point &searchPoint,
280 const double &search_r,
281 std::vector<size_t> &neighs,
282 std::vector<double> &sqr_dist,
283 const size_t &searchPointTag,
284 const std::vector<size_t> &dataTags) override {
285
286 double query_pt[3] = {searchPoint[0], searchPoint[1], searchPoint[2]};
287
288 TreeSearchCheckIDExcludeRes resultSet(search_r * search_r,
289 neighs, sqr_dist, searchPointTag, dataTags);
290
291 return d_tree.radiusSearchCustomCallback(&query_pt[0], resultSet, d_params);
292 };
293
304 const util::Point &searchPoint,
305 const double &search_r,
306 std::vector<int> &neighs,
307 std::vector<float> &sqr_dist,
308 const size_t &searchPointTag,
309 const std::vector<size_t> &dataTags) override {
310
311 // first, get results using int and float and then convert
312 std::vector<size_t> neighs_temp;
313 std::vector<double> sqr_dist_temp;
314 auto N =
315 this->radiusSearchExcludeTag(searchPoint, search_r,
316 neighs_temp, sqr_dist_temp,
317 searchPointTag, dataTags);
318
319 if (N > 0) {
320 neighs.resize(N);
321 sqr_dist.resize(N);
322 for (size_t i=0; i<N; i++) {
323 neighs.push_back(int(neighs_temp[i]));
324 sqr_dist.push_back(float(sqr_dist_temp[i]));
325 }
326 }
327
328 return N;
329 };
330
341 const util::Point &searchPoint,
342 const double &search_r,
343 std::vector<size_t> &neighs,
344 std::vector<double> &sqr_dist,
345 const size_t &searchPointTag,
346 const std::vector<size_t> &dataTags) override {
347
348 double query_pt[3] = {searchPoint[0], searchPoint[1], searchPoint[2]};
349
350 TreeSearchCheckIDIncludeRes resultSet(search_r * search_r,
351 neighs, sqr_dist,
352 searchPointTag, dataTags);
353
354 return d_tree.radiusSearchCustomCallback(&query_pt[0], resultSet, d_params);
355 };
356
367 const util::Point &searchPoint,
368 const double &search_r,
369 std::vector<int> &neighs,
370 std::vector<float> &sqr_dist,
371 const size_t &searchPointTag,
372 const std::vector<size_t> &dataTags) override {
373
374 // first, get results using int and float and then convert
375 std::vector<size_t> neighs_temp;
376 std::vector<double> sqr_dist_temp;
377 auto N =
378 this->radiusSearchIncludeTag(searchPoint, search_r,
379 neighs_temp, sqr_dist_temp,
380 searchPointTag, dataTags);
381
382 if (N > 0) {
383 neighs.resize(N);
384 sqr_dist.resize(N);
385 for (size_t i=0; i<N; i++) {
386 neighs.push_back(int(neighs_temp[i]));
387 sqr_dist.push_back(float(sqr_dist_temp[i]));
388 }
389 }
390
391 return N;
392 };
393
401 const util::Point &searchPoint,
402 size_t &neigh,
403 double &sqrDistNeigh) override {
404
405 double query_pt[3] = {searchPoint[0], searchPoint[1], searchPoint[2]};
406 unsigned int neigh_temp = 0;
407 d_tree.knnSearch(&query_pt[0], 1, &neigh_temp, &sqrDistNeigh);
408 neigh = neigh_temp;
409 };
410
411public:
414
416 nanoflann::KDTreeSingleIndexAdaptor<
417 nanoflann::L2_Simple_Adaptor<double, PointCloudAdaptor>, PointCloudAdaptor,
418 dim
420
422 nanoflann::SearchParameters d_params;
423};
424
425} // namespace nsearch
426
427#endif // NSEARCH_NSEARCH_H
A class for nearest neighbor search.
Definition nsearch.h:28
virtual size_t radiusSearchExcludeTag(const util::Point &searchPoint, const double &search_r, std::vector< size_t > &neighs, std::vector< double > &sqr_dist, const size_t &searchPointTag, const std::vector< size_t > &dataTags)=0
Perform radius search to find points in a point cloud within specified distance from a given point....
virtual size_t radiusSearch(const util::Point &searchPoint, const double &search_r, std::vector< int > &neighs, std::vector< float > &sqr_dist)=0
Perform radius search to find points in a point cloud within specified distance from a given point.
virtual double updatePointCloud(const std::vector< util::Point > &x, bool parallel=true)=0
Function to implement point cloud update.
std::string d_treeType
name of tree: nflann_kdtree
Definition nsearch.h:171
virtual size_t radiusSearchExcludeTag(const util::Point &searchPoint, const double &search_r, std::vector< int > &neighs, std::vector< float > &sqr_dist, const size_t &searchPointTag, const std::vector< size_t > &dataTags)=0
= 0
virtual size_t radiusSearch(const util::Point &searchPoint, const double &search_r, std::vector< size_t > &neighs, std::vector< double > &sqr_dist)=0
Perform radius search to find points in a point cloud within specified distance from a given point....
size_t d_debug
control the verbosity
Definition nsearch.h:168
virtual size_t radiusSearchIncludeTag(const util::Point &searchPoint, const double &search_r, std::vector< size_t > &neighs, std::vector< double > &sqr_dist, const size_t &searchPointTag, const std::vector< size_t > &dataTags)=0
Perform radius search to find points in a point cloud within specified distance from a given point....
virtual double setInputCloud()=0
Set input cloud.
BaseNSearch(std::string name, size_t debug=0)
Constructor.
Definition nsearch.h:36
virtual size_t radiusSearchIncludeTag(const util::Point &searchPoint, const double &search_r, std::vector< int > &neighs, std::vector< float > &sqr_dist, const size_t &searchPointTag, const std::vector< size_t > &dataTags)=0
= 0
virtual void closestPoint(const util::Point &searchPoint, size_t &neigh, double &sqrDistNeigh)=0
Find the closest points to the specified point.
A class for nearest neighbor search using nanoflann library.
Definition nsearch.h:178
nanoflann::SearchParameters d_params
Tree search parameters.
Definition nsearch.h:422
double setInputCloud() override
Set input cloud.
Definition nsearch.h:200
size_t radiusSearch(const util::Point &searchPoint, const double &search_r, std::vector< int > &neighs, std::vector< float > &sqr_dist) override
Perform radius search to find points in a point cloud within specified distance from a given point....
Definition nsearch.h:244
size_t radiusSearchIncludeTag(const util::Point &searchPoint, const double &search_r, std::vector< int > &neighs, std::vector< float > &sqr_dist, const size_t &searchPointTag, const std::vector< size_t > &dataTags) override
= 0
Definition nsearch.h:366
size_t radiusSearchExcludeTag(const util::Point &searchPoint, const double &search_r, std::vector< int > &neighs, std::vector< float > &sqr_dist, const size_t &searchPointTag, const std::vector< size_t > &dataTags) override
= 0
Definition nsearch.h:303
PointCloudAdaptor d_cloud
coordinates of the points
Definition nsearch.h:413
NFlannSearchKd(const PointCloud &x, size_t debug=0, size_t max_leafs=10)
Constructor.
Definition nsearch.h:188
double updatePointCloud(const std::vector< util::Point > &x, bool parallel=true) override
Function to implement point cloud update.
Definition nsearch.h:216
nanoflann::KDTreeSingleIndexAdaptor< nanoflann::L2_Simple_Adaptor< double, PointCloudAdaptor >, PointCloudAdaptor, dim > d_tree
Tree.
Definition nsearch.h:419
size_t radiusSearch(const util::Point &searchPoint, const double &search_r, std::vector< size_t > &neighs, std::vector< double > &sqr_dist) override
Perform radius search to find points in a point cloud within specified distance from a given point....
Definition nsearch.h:227
size_t radiusSearchIncludeTag(const util::Point &searchPoint, const double &search_r, std::vector< size_t > &neighs, std::vector< double > &sqr_dist, const size_t &searchPointTag, const std::vector< size_t > &dataTags) override
= 0
Definition nsearch.h:340
size_t radiusSearchExcludeTag(const util::Point &searchPoint, const double &search_r, std::vector< size_t > &neighs, std::vector< double > &sqr_dist, const size_t &searchPointTag, const std::vector< size_t > &dataTags) override
= 0
Definition nsearch.h:278
void closestPoint(const util::Point &searchPoint, size_t &neigh, double &sqrDistNeigh) override
= 0
Definition nsearch.h:400
To collect results of nanoflann tree search. In this class, we check the tag of a potential point and...
To collect results of nanoflann tree search. In this class, we check the tag of a potential point and...
To collect results of nanoflann tree search. Default result output of nanoflann search uses std::vect...
Definition nflannSetup.h:86
Methods for performing efficient search of neighboring points.
Definition nflannSetup.h:17
std::vector< util::Point > PointCloud
Define list of points for tree search using nanoflann lib.
Definition nflannSetup.h:20
float timeDiff(std::chrono::steady_clock::time_point begin, std::chrono::steady_clock::time_point end, std::string unit="microseconds")
Returns difference between two times.
Definition methods.h:304
Allows custom point cloud data structure to interface with nanoflann. See https://github....
Definition nflannSetup.h:26
A structure to represent 3d vectors.
Definition point.h:30