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// clang-format off
17#include <lagrange/utils/warnoff.h>
18#include <nanoflann.hpp>
19#include <lagrange/utils/warnon.h>
20// clang-format on
21
22#include <lagrange/Logger.h>
23#include <lagrange/bvh/BVH.h>
24#include <lagrange/utils/assert.h>
25#include <lagrange/utils/safe_cast.h>
26
27namespace lagrange {
28namespace bvh {
29
30template <typename _VertexArray, typename _ElementArray = lagrange::Triangles>
31class BVHNanoflann : public BVH<_VertexArray, _ElementArray>
32{
33public:
34 using Parent = BVH<_VertexArray, _ElementArray>;
35 using VertexArray = typename Parent::VertexArray;
36 using ElementArray = typename Parent::ElementArray;
37 using Scalar = typename Parent::Scalar;
38 using Index = typename Parent::Index;
39 using PointType = typename Parent::PointType;
40 using ClosestPoint = typename Parent::ClosestPoint;
41 using KDTree = nanoflann::KDTreeEigenMatrixAdaptor<VertexArray>;
42
43public:
44 BVHType get_bvh_type() const override { return BVHType::NANOFLANN; }
45
46 bool does_support_pointcloud() const override { return true; }
47 bool does_support_triangles() const override { return false; }
48 bool does_support_lines() const override { return false; }
49
50 void build(const VertexArray&, const ElementArray&) override
51 {
52 la_runtime_assert(0, "BVHNannoflann does not support elements.");
53 }
54
55 void build(const VertexArray& vertices) override
56 {
57 // Nanoflann stores a const ref to vertices, we need to make sure
58 // vertices outlives the BVH. Storing a local copy for now.
59 m_vertices = vertices;
60
61 constexpr int max_leaf = 10; // TODO: Experiment with different values.
62 m_tree = std::make_unique<KDTree>(m_vertices.cols(), m_vertices, max_leaf);
63 m_tree->index_->buildIndex();
64 }
65
66 bool does_support_query_closest_point() const override { return true; }
67 ClosestPoint query_closest_point(const PointType& p) const override
68 {
69 la_runtime_assert(m_tree);
70 ClosestPoint r;
71
72 typename KDTree::IndexType out_idx[1];
73 Scalar out_sq_dist[1];
74 auto num_valid_pts = m_tree->index_->knnSearch(p.data(), 1, out_idx, out_sq_dist);
75
76 if (num_valid_pts == 0) {
77 throw std::runtime_error("Nanoflann did not find any valid closest points.");
78 }
79
80 r.closest_vertex_idx = lagrange::safe_cast<Index>(out_idx[0]);
81 r.closest_point = m_vertices.row(out_idx[0]);
82 r.squared_distance = out_sq_dist[0];
83
84 return r;
85 }
86
87 bool does_support_query_k_nearest_neighbours() const override { return true; }
88 std::vector<ClosestPoint> query_k_nearest_neighbours(const PointType& p, int k) const override
89 {
90 la_runtime_assert(m_tree);
91 std::vector<ClosestPoint> rs;
92
93 std::vector<typename KDTree::IndexType> out_idxs(k);
94 std::vector<Scalar> out_sq_dists(k);
95 auto num_valid_pts =
96 m_tree->index_->knnSearch(p.data(), k, out_idxs.data(), out_sq_dists.data());
97
98 rs.resize(num_valid_pts);
99 for (size_t i = 0; i < num_valid_pts; i++) {
100 rs[i].closest_vertex_idx = lagrange::safe_cast<Index>(out_idxs[i]);
101 rs[i].closest_point = m_vertices.row(out_idxs[i]);
102 rs[i].squared_distance = out_sq_dists[i];
103 }
104
105 return rs;
106 }
107
108 bool does_support_query_in_sphere_neighbours() const override { return true; }
109 std::vector<ClosestPoint> query_in_sphere_neighbours(const PointType& p, const Scalar radius)
110 const override
111 {
112 la_runtime_assert(m_tree);
113 std::vector<ClosestPoint> r;
114
115 std::vector<nanoflann::ResultItem<typename KDTree::IndexType, Scalar>> output;
116 nanoflann::SearchParameters params(
117 0, // Epsilon, should be 0.
118 true); // Sort result by distance.
119 m_tree->index_->radiusSearch(p.data(), radius * radius, output, params);
120
121 r.reserve(output.size());
122 for (const auto& entry : output) {
123 ClosestPoint cp;
124 cp.closest_vertex_idx = (Index)entry.first;
125 cp.closest_point = m_vertices.row(cp.closest_vertex_idx);
126 cp.squared_distance = entry.second;
127 r.push_back(std::move(cp));
128 }
129 return r;
130 }
131
132 std::vector<ClosestPoint> batch_query_closest_point(const VertexArray& query_pts) const override
133 {
134 return Parent::default_batch_query_closest_point(query_pts);
135 }
136
137
138private:
139 VertexArray m_vertices;
140 std::unique_ptr<KDTree> m_tree;
141};
142
143} // namespace bvh
144} // namespace lagrange
Definition BVHNanoflann.h:32
bool does_support_pointcloud() const override
Does it support supplying elements or just points?
Definition BVHNanoflann.h:46
std::vector< ClosestPoint > batch_query_closest_point(const VertexArray &query_pts) const override
Batch query closest points.
Definition BVHNanoflann.h:132
bool does_support_query_k_nearest_neighbours() const override
Query for the k nearest neighbours.
Definition BVHNanoflann.h:87
BVHType get_bvh_type() const override
Get the enum type.
Definition BVHNanoflann.h:44
bool does_support_query_closest_point() const override
Query for the closest point.
Definition BVHNanoflann.h:66
bool does_support_query_in_sphere_neighbours() const override
Query for the closest point with in radius.
Definition BVHNanoflann.h:108
@ 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.