16#include <nanoflann.hpp>
18#include <lagrange/Logger.h>
19#include <lagrange/bvh/BVH.h>
20#include <lagrange/utils/assert.h>
21#include <lagrange/utils/safe_cast.h>
26template <
typename _VertexArray,
typename _ElementArray = lagrange::Triangles>
31 using VertexArray =
typename Parent::VertexArray;
32 using ElementArray =
typename Parent::ElementArray;
33 using Scalar =
typename Parent::Scalar;
34 using Index =
typename Parent::Index;
35 using PointType =
typename Parent::PointType;
37 using KDTree = nanoflann::KDTreeEigenMatrixAdaptor<VertexArray>;
42 return BVHType::NANOFLANN;
49 bool does_support_triangles()
const override
53 bool does_support_lines()
const override
58 void build(
const VertexArray&,
const ElementArray&)
override
63 void build(
const VertexArray& vertices)
override
67 m_vertices = vertices;
69 constexpr int max_leaf = 10;
70 m_tree = std::make_unique<KDTree>(m_vertices.cols(), m_vertices, max_leaf);
71 m_tree->index_->buildIndex();
78 ClosestPoint query_closest_point(
const PointType& p)
const override
83 typename KDTree::IndexType out_idx[1];
84 Scalar out_sq_dist[1];
85 auto num_valid_pts = m_tree->index_->knnSearch(p.data(), 1, out_idx, out_sq_dist);
87 if (num_valid_pts == 0) {
88 throw std::runtime_error(
"Nanoflann did not find any valid closest points.");
91 r.closest_vertex_idx = lagrange::safe_cast<Index>(out_idx[0]);
92 r.closest_point = m_vertices.row(out_idx[0]);
93 r.squared_distance = out_sq_dist[0];
102 std::vector<ClosestPoint> query_k_nearest_neighbours(
const PointType& p,
int k)
const override
105 std::vector<ClosestPoint> rs;
107 std::vector<typename KDTree::IndexType> out_idxs(k);
108 std::vector<Scalar> out_sq_dists(k);
110 m_tree->index_->knnSearch(p.data(), k, out_idxs.data(), out_sq_dists.data());
112 rs.resize(num_valid_pts);
113 for (
size_t i = 0; i < num_valid_pts; i++) {
114 rs[i].closest_vertex_idx = lagrange::safe_cast<Index>(out_idxs[i]);
115 rs[i].closest_point = m_vertices.row(out_idxs[i]);
116 rs[i].squared_distance = out_sq_dists[i];
126 std::vector<ClosestPoint> query_in_sphere_neighbours(
const PointType& p,
const Scalar radius)
130 std::vector<ClosestPoint> r;
132 std::vector<nanoflann::ResultItem<typename KDTree::IndexType, Scalar>> output;
133 nanoflann::SearchParameters params(
136 m_tree->index_->radiusSearch(p.data(), radius * radius, output, params);
138 r.reserve(output.size());
139 for (
const auto& entry : output) {
141 cp.closest_vertex_idx = (Index)entry.first;
142 cp.closest_point = m_vertices.row(cp.closest_vertex_idx);
143 cp.squared_distance = entry.second;
144 r.push_back(std::move(cp));
151 return Parent::default_batch_query_closest_point(query_pts);
156 VertexArray m_vertices;
157 std::unique_ptr<KDTree> m_tree;
Definition: BVHNanoflann.h:28
bool does_support_pointcloud() const override
Does it support supplying elements or just points?
Definition: BVHNanoflann.h:45
std::vector< ClosestPoint > batch_query_closest_point(const VertexArray &query_pts) const override
Batch query closest points.
Definition: BVHNanoflann.h:149
bool does_support_query_k_nearest_neighbours() const override
Query for the k nearest neighbours.
Definition: BVHNanoflann.h:98
BVHType get_bvh_type() const override
Get the enum type.
Definition: BVHNanoflann.h:40
bool does_support_query_closest_point() const override
Query for the closest point.
Definition: BVHNanoflann.h:74
bool does_support_query_in_sphere_neighbours() const override
Query for the closest point with in radius.
Definition: BVHNanoflann.h:122
#define la_runtime_assert(...)
Runtime assertion check.
Definition: assert.h:169
Main namespace for Lagrange.
Definition: AABBIGL.h:30