Lagrange
Loading...
Searching...
No Matches
BVHNanoflann.h
1/*
2 * Copyright 2019 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <memory>
15
16#include <nanoflann.hpp>
17
18#include <lagrange/Logger.h>
19#include <lagrange/bvh/BVH.h>
20#include <lagrange/utils/assert.h>
21#include <lagrange/utils/safe_cast.h>
22
23namespace lagrange {
24namespace bvh {
25
26template <typename _VertexArray, typename _ElementArray = lagrange::Triangles>
27class BVHNanoflann : public BVH<_VertexArray, _ElementArray>
28{
29public:
30 using Parent = BVH<_VertexArray, _ElementArray>;
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;
36 using ClosestPoint = typename Parent::ClosestPoint;
37 using KDTree = nanoflann::KDTreeEigenMatrixAdaptor<VertexArray>;
38
39public:
40 BVHType get_bvh_type() const override { return BVHType::NANOFLANN; }
41
42 bool does_support_pointcloud() const override { return true; }
43 bool does_support_triangles() const override { return false; }
44 bool does_support_lines() const override { return false; }
45
46 void build(const VertexArray&, const ElementArray&) override
47 {
48 la_runtime_assert(0, "BVHNannoflann does not support elements.");
49 }
50
51 void build(const VertexArray& vertices) override
52 {
53 // Nanoflann stores a const ref to vertices, we need to make sure
54 // vertices outlives the BVH. Storing a local copy for now.
55 m_vertices = vertices;
56
57 constexpr int max_leaf = 10; // TODO: Experiment with different values.
58 m_tree = std::make_unique<KDTree>(m_vertices.cols(), m_vertices, max_leaf);
59 m_tree->index_->buildIndex();
60 }
61
62 bool does_support_query_closest_point() const override { return true; }
63 ClosestPoint query_closest_point(const PointType& p) const override
64 {
65 la_runtime_assert(m_tree);
66 ClosestPoint r;
67
68 typename KDTree::IndexType out_idx[1];
69 Scalar out_sq_dist[1];
70 auto num_valid_pts = m_tree->index_->knnSearch(p.data(), 1, out_idx, out_sq_dist);
71
72 if (num_valid_pts == 0) {
73 throw std::runtime_error("Nanoflann did not find any valid closest points.");
74 }
75
76 r.closest_vertex_idx = lagrange::safe_cast<Index>(out_idx[0]);
77 r.closest_point = m_vertices.row(out_idx[0]);
78 r.squared_distance = out_sq_dist[0];
79
80 return r;
81 }
82
83 bool does_support_query_k_nearest_neighbours() const override { return true; }
84 std::vector<ClosestPoint> query_k_nearest_neighbours(const PointType& p, int k) const override
85 {
86 la_runtime_assert(m_tree);
87 std::vector<ClosestPoint> rs;
88
89 std::vector<typename KDTree::IndexType> out_idxs(k);
90 std::vector<Scalar> out_sq_dists(k);
91 auto num_valid_pts =
92 m_tree->index_->knnSearch(p.data(), k, out_idxs.data(), out_sq_dists.data());
93
94 rs.resize(num_valid_pts);
95 for (size_t i = 0; i < num_valid_pts; i++) {
96 rs[i].closest_vertex_idx = lagrange::safe_cast<Index>(out_idxs[i]);
97 rs[i].closest_point = m_vertices.row(out_idxs[i]);
98 rs[i].squared_distance = out_sq_dists[i];
99 }
100
101 return rs;
102 }
103
104 bool does_support_query_in_sphere_neighbours() const override { return true; }
105 std::vector<ClosestPoint> query_in_sphere_neighbours(const PointType& p, const Scalar radius)
106 const override
107 {
108 la_runtime_assert(m_tree);
109 std::vector<ClosestPoint> r;
110
111 std::vector<nanoflann::ResultItem<typename KDTree::IndexType, Scalar>> output;
112 nanoflann::SearchParameters params(
113 0, // Epsilon, should be 0.
114 true); // Sort result by distance.
115 m_tree->index_->radiusSearch(p.data(), radius * radius, output, params);
116
117 r.reserve(output.size());
118 for (const auto& entry : output) {
119 ClosestPoint cp;
120 cp.closest_vertex_idx = (Index)entry.first;
121 cp.closest_point = m_vertices.row(cp.closest_vertex_idx);
122 cp.squared_distance = entry.second;
123 r.push_back(std::move(cp));
124 }
125 return r;
126 }
127
128 std::vector<ClosestPoint> batch_query_closest_point(const VertexArray& query_pts) const override
129 {
130 return Parent::default_batch_query_closest_point(query_pts);
131 }
132
133
134private:
135 VertexArray m_vertices;
136 std::unique_ptr<KDTree> m_tree;
137};
138
139} // namespace bvh
140} // namespace lagrange
Definition BVHNanoflann.h:28
bool does_support_pointcloud() const override
Does it support supplying elements or just points?
Definition BVHNanoflann.h:42
std::vector< ClosestPoint > batch_query_closest_point(const VertexArray &query_pts) const override
Batch query closest points.
Definition BVHNanoflann.h:128
bool does_support_query_k_nearest_neighbours() const override
Query for the k nearest neighbours.
Definition BVHNanoflann.h:83
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:62
bool does_support_query_in_sphere_neighbours() const override
Query for the closest point with in radius.
Definition BVHNanoflann.h:104
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
constexpr auto safe_cast(SourceType value) -> std::enable_if_t<!std::is_same< SourceType, TargetType >::value, TargetType >
Perform safe cast from SourceType to TargetType, where "safe" means:
Definition safe_cast.h:50
Main namespace for Lagrange.