8 #ifndef SEARCH_KDTREE_NANOFLANN_H 9 #define SEARCH_KDTREE_NANOFLANN_H 17 #include "nanoflann.hpp" 26 namespace Portage {
namespace Meshfree {
28 template <
size_t D,
class SwarmType>
29 class SwarmNanoflann {
31 explicit SwarmNanoflann(SwarmType
const& swarm) : swarm_(swarm) {}
34 SwarmNanoflann & operator=(SwarmNanoflann
const& inswarm) {
36 swarm_ = inswarm.swarm_;
45 inline size_t kdtree_get_point_count()
const {
46 return swarm_.num_particles(Entity_type::ALL);
50 inline double kdtree_distance(
double const *p1,
size_t const idx,
size_t )
const {
52 Point<D>
const& p2 = swarm_.get_particle_coordinates(idx);
53 for (
int i = 0; i < D; i++)
54 dist2 += (p1[i]-p2[i])*(p1[i]-p2[i]);
59 inline double kdtree_get_pt(
size_t const idx,
int d)
const {
60 assert(d >= 0 && d < D);
61 Point<D>
const& p = swarm_.get_particle_coordinates(idx);
68 bool kdtree_get_bbox(BBOX& )
const {
return false;}
75 Point<D> get_point(
size_t const idx)
const {
76 return swarm_.get_particle_coordinates(idx);
80 SwarmType
const& swarm_;
92 template <
int D,
class SourceSwarmType,
class TargetSwarmType>
93 class Search_KDTree_Nanoflann {
98 nanoflann::KDTreeSingleIndexAdaptor<
99 nanoflann::L2_Simple_Adaptor<double, SwarmNanoflann<D, SourceSwarmType>>,
100 SwarmNanoflann<D, SourceSwarmType>,
105 Search_KDTree_Nanoflann() =
delete;
118 Search_KDTree_Nanoflann(SourceSwarmType
const& source_swarm,
119 TargetSwarmType
const& target_swarm,
120 std::shared_ptr<
std::vector<Point<D>>> source_extents,
121 std::shared_ptr<
std::vector<Point<D>>> target_extents,
122 WeightCenter center = Gather) :
123 sourceSwarm_(source_swarm), targetSwarm_(target_swarm) {
125 assert(center == Gather);
127 int ntgt = target_swarm.num_owned_particles();
128 search_radii_.resize(ntgt);
129 for (
int i = 0; i < ntgt; i++) {
131 for (
int d = 0; d < D; d++)
132 len += (*target_extents)[i][d]*(*target_extents)[i][d];
133 search_radii_[i] = sqrt(len);
136 kdtree_ = std::make_shared<kdtree_t>(D, sourceSwarm_,
137 nanoflann::KDTreeSingleIndexAdaptorParams(10 ));
138 kdtree_->buildIndex();
150 std::vector<unsigned int> operator() (
size_t pointId)
const;
153 SwarmNanoflann<D, SourceSwarmType>
const sourceSwarm_;
154 SwarmNanoflann<D, TargetSwarmType>
const targetSwarm_;
155 std::vector<double> search_radii_;
156 std::shared_ptr<kdtree_t> kdtree_ =
nullptr;
160 template<
int D,
class SourceSwarmType,
class TargetSwarmType>
162 Search_KDTree_Nanoflann<D, SourceSwarmType, TargetSwarmType>::operator() (
size_t pointId)
const {
163 std::vector<int> candidates;
166 Point<D> tpcoord = targetSwarm_.get_point(pointId);
169 for (
int i = 0; i < D; i++)
172 std::vector<std::pair<size_t, double>> matches;
174 nanoflann::SearchParams params {};
177 double radius = search_radii_[pointId];
178 const size_t nMatches = kdtree_->radiusSearch(p, radius, matches, params);
180 for (
auto const &idx_dist_pair : matches)
181 candidates.push_back(static_cast<int>(idx_dist_pair.first));
188 #endif // HAVE_NANOFLANN 191 #endif // SEARCH_KDTREE_NANOFLANN_H std::vector< T > vector
Definition: portage.h:238
Definition: coredriver.h:42