Lagrange
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
BVH.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 <algorithm>
15#include <exception>
16#include <vector>
17
18#include <lagrange/bvh/BVHType.h>
19#include <lagrange/common.h>
20#include <lagrange/utils/range.h>
21
22namespace lagrange {
23namespace bvh {
24
25template <typename _VertexArray, typename _ElementArray>
26class BVH
27{
28public:
29 using VertexArray = _VertexArray;
30 using ElementArray = _ElementArray;
31 using Scalar = typename VertexArray::Scalar;
32 using Index = typename ElementArray::Scalar;
33 using PointType = Eigen::Matrix<Scalar, 1, VertexArray::ColsAtCompileTime>;
34
35public:
36 BVH() = default;
37 virtual ~BVH() = default;
38
39public:
43 virtual BVHType get_bvh_type() const = 0;
44
51 virtual bool does_support_pointcloud() const = 0;
52 virtual bool does_support_triangles() const = 0;
53 virtual bool does_support_lines() const = 0;
54
58 virtual void build(const VertexArray& vertices, const ElementArray& elements) = 0;
59
60 virtual void build(const VertexArray& vertices) = 0;
61
63 {
64 Index embedding_element_idx = invalid<Index>();
65 Index closest_vertex_idx = invalid<Index>();
66 PointType closest_point;
67 Scalar squared_distance = invalid<Scalar>();
68 };
69
73 virtual bool does_support_query_closest_point() const = 0;
74 virtual ClosestPoint query_closest_point(const PointType& p) const = 0;
75
80 virtual std::vector<ClosestPoint> query_k_nearest_neighbours(const PointType& p, int k)
81 const = 0;
82
87 virtual std::vector<ClosestPoint> query_in_sphere_neighbours(
88 const PointType& p,
89 const Scalar radius) const = 0;
90
94 virtual std::vector<ClosestPoint> batch_query_closest_point(
95 const VertexArray& query_pts) const = 0;
96
97protected:
98 std::vector<ClosestPoint> default_batch_query_closest_point(const VertexArray& query_pts) const
99 {
100 const auto num_queries = query_pts.rows();
101 std::vector<ClosestPoint> results(num_queries);
102
103 // TODO: investigate use of tbb here if need be.
104 std::transform(
105 query_pts.rowwise().begin(),
106 query_pts.rowwise().end(),
107 results.begin(),
108 [this](const PointType& p) { return this->query_closest_point(p); });
109
110 return results;
111 }
112};
113
114} // namespace bvh
115} // namespace lagrange
Definition: BVH.h:27
virtual bool does_support_query_in_sphere_neighbours() const =0
Query for the closest point with in radius.
virtual bool does_support_pointcloud() const =0
Does it support supplying elements or just points?
virtual bool does_support_query_closest_point() const =0
Query for the closest point.
virtual std::vector< ClosestPoint > batch_query_closest_point(const VertexArray &query_pts) const =0
Batch query closest points.
virtual void build(const VertexArray &vertices, const ElementArray &elements)=0
Construct bvh based on vertices and elements.
virtual BVHType get_bvh_type() const =0
Get the enum type.
virtual bool does_support_query_k_nearest_neighbours() const =0
Query for the k nearest neighbours.
Main namespace for Lagrange.
Definition: AABBIGL.h:30