Permalink
Browse files

Merge pull request #12748 from snschune/radius_search_KDTree_12747

Add radiusSearch to KDTree
  • Loading branch information...
permcody committed Jan 22, 2019
2 parents fa9a510 + 3514e16 commit 9e9a9c1b9fd3451e07c199c8d4a691ff067e9758
Showing with 65 additions and 0 deletions.
  1. +4 −0 framework/include/utils/KDTree.h
  2. +9 −0 framework/src/utils/KDTree.C
  3. +52 −0 unit/src/KDTreeTest.C
@@ -33,6 +33,10 @@ class KDTree
std::vector<std::size_t> & return_index,
std::vector<Real> & return_dist_sqr);

void radiusSearch(Point & query_point,
Real radius,
std::vector<std::pair<std::size_t, Real>> & indices_dist);

using KdTreeT = nanoflann::KDTreeSingleIndexAdaptor<
nanoflann::L2_Simple_Adaptor<Real, PointListAdaptor<Point>>,
PointListAdaptor<Point>,
@@ -48,3 +48,12 @@ KDTree::neighborSearch(Point & query_point,
return_index.resize(n_result);
return_dist_sqr.resize(n_result);
}

void
KDTree::radiusSearch(Point & query_point,
Real radius,
std::vector<std::pair<std::size_t, Real>> & indices_dist)
{
nanoflann::SearchParams sp;
_kd_tree->radiusSearch(&query_point(0), radius * radius, indices_dist, sp);
}
@@ -0,0 +1,52 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#include "gtest_include.h"
#include "KDTree.h"

#define TOL 1e-10

/**
* Test KDTree proxy object that uses underlying nanoflann implementation
*/
TEST(KDTree, radiusSearch)
{
std::vector<Point> master_points = {Point(0.0, -0.2, 0.1),
Point(0.0, 0.0, 0.1),
Point(0.2, 0.3, 0.1),
Point(0.2, 0.3, 0.2),
Point(1.0, 0.3, 0.2)};
KDTree _kd_tree(master_points, 50);
std::vector<std::pair<std::size_t, Real>> indices_dist;

// get all points
Point origin(-0.1, -0.2, 0.1);
_kd_tree.radiusSearch(origin, 10.0, indices_dist);
EXPECT_EQ(5, indices_dist.size());
for (unsigned int j = 0; j < 5; ++j)
{
EXPECT_EQ(j, indices_dist[j].first);
EXPECT_NEAR((master_points[j] - origin).norm_sq(), indices_dist[j].second, TOL);
}

// all except the last one
_kd_tree.radiusSearch(origin, 1.2, indices_dist);
EXPECT_EQ(4, indices_dist.size());
for (unsigned int j = 0; j < 4; ++j)
{
EXPECT_EQ(j, indices_dist[j].first);
EXPECT_NEAR((master_points[j] - origin).norm_sq(), indices_dist[j].second, TOL);
}

// just one
_kd_tree.radiusSearch(origin, 0.1001, indices_dist);
EXPECT_EQ(1, indices_dist.size());
EXPECT_EQ(0, indices_dist[0].first);
EXPECT_NEAR((master_points[0] - origin).norm_sq(), indices_dist[0].second, TOL);
}

0 comments on commit 9e9a9c1

Please sign in to comment.