Lagrange
Loading...
Searching...
No Matches
AABB.h
1/*
2 * Copyright 2025 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 <lagrange/utils/function_ref.h>
15#include <lagrange/utils/invalid.h>
16#include <lagrange/utils/span.h>
17
18#include <Eigen/Geometry>
19#include <functional>
20#include <vector>
21
22namespace lagrange::bvh {
23
26
37template <typename Scalar, int Dim>
38class AABB
39{
40public:
41 using Index = uint32_t;
42 using Box = Eigen::AlignedBox<Scalar, Dim>;
43 using Point = typename Box::VectorType;
44
45public:
51 void build(span<Box> boxes);
52
59 void intersect(const Box& query_box, std::vector<Index>& results) const;
60
70 void intersect(const Box& query_box, function_ref<bool(Index)> callback) const;
71
79 Index intersect_first(const Box& query_box) const;
80
89 Index get_closest_element(const Point& q, function_ref<Scalar(Index)> sq_dist_fn) const;
90
101 const Point& q,
102 Scalar sq_radius,
103 function_ref<void(Index)> func) const;
104
110 bool empty() const { return m_root == invalid<Index>(); }
111
112private:
113 struct Node
114 {
115 Box bbox;
116 Index left = invalid<Index>();
117 Index right = invalid<Index>();
118 Index element_idx = invalid<Index>();
119
120 bool is_leaf() const { return left == invalid<Index>() && right == invalid<Index>(); }
121 };
122
123private:
124 std::vector<Node> m_nodes;
125 Index m_root = invalid<Index>();
126};
127
129
130} // namespace lagrange::bvh
Axis-Aligned Bounding Box (AABB) tree for efficient spatial queries.
Definition AABB.h:39
Index get_closest_element(const Point &q, function_ref< Scalar(Index)> sq_dist_fn) const
Find the index of the closest element to a query point.
Definition AABB.cpp:188
void foreach_element_within_radius(const Point &q, Scalar sq_radius, function_ref< void(Index)> func) const
Call a function for each element within a given radius from a query point.
Definition AABB.cpp:253
Index intersect_first(const Box &query_box) const
Find the first box that intersects with a query box.
Definition AABB.cpp:177
bool empty() const
Check if the tree is empty.
Definition AABB.h:110
void build(span< Box > boxes)
Build the AABB tree from a collection of bounding boxes.
Definition AABB.cpp:32
void intersect(const Box &query_box, std::vector< Index > &results) const
Find all boxes that intersect with a query box.
Definition AABB.cpp:127
A lightweight non-owning reference to a callable.
Definition function_ref.h:47
@ Scalar
Mesh attribute must have exactly 1 channel.
Definition AttributeFwd.h:56
::nonstd::span< T, Extent > span
A bounds-safe view for sequences of objects.
Definition span.h:27
constexpr T invalid()
You can use invalid<T>() to get a value that can represent "invalid" values, such as invalid indices ...
Definition invalid.h:40