PhysicsBasedAnimationToolkit 0.0.10
Cross-platform C++20 library of algorithms and data structures commonly used in computer graphics research on physically-based simulation.
Loading...
Searching...
No Matches
Aabb.cuh
1#ifndef PBAT_GPU_IMPL_GEOMETRY_AABB_H
2#define PBAT_GPU_IMPL_GEOMETRY_AABB_H
3
5#include "pbat/gpu/Aliases.h"
6#include "pbat/gpu/impl/common/Buffer.cuh"
8#include "pbat/math/linalg/mini/Matrix.h"
9#include "pbat/math/linalg/mini/UnaryOperations.h"
10
11#include <algorithm>
12#include <cstddef>
13#include <thrust/execution_policy.h>
14#include <thrust/for_each.h>
15#include <thrust/iterator/counting_iterator.h>
16
17namespace pbat {
18namespace gpu {
19namespace impl {
20namespace geometry {
21
22template <auto kDims>
23struct Aabb
24{
25 Aabb() = default;
26 Aabb(GpuIndex nBoxes) : b(nBoxes), e(nBoxes) {}
27
28 template <auto kSimplexVerts>
29 Aabb(
32
33 void Resize(GpuIndex nBoxes);
34
35 template <class FLowerUpper>
36 void Construct(FLowerUpper&& fLowerUpper, GpuIndex begin = 0, GpuIndex end = -1);
37
38 template <auto kSimplexVerts>
39 void Construct(
42 GpuIndex begin = 0);
43
44 GpuIndex Size() const { return static_cast<GpuIndex>(b.Size()); }
45
47};
48
49template <auto kDims>
50template <auto kSimplexVerts>
51inline Aabb<kDims>::Aabb(
54{
55 Construct(V, S);
56}
57
58template <auto kDims>
59template <class FLowerUpper>
60inline void Aabb<kDims>::Construct(FLowerUpper&& fLowerUpper, GpuIndex begin, GpuIndex end)
61{
62 PBAT_PROFILE_CUDA_NAMED_SCOPE("pbat.gpu.impl.geometry.Aabb.Construct");
63 using namespace pbat::math::linalg;
64 auto const nBoxes = static_cast<GpuIndex>(b.Size());
65 end = end < 0 ? nBoxes : end;
66 thrust::for_each(
67 thrust::device,
68 thrust::counting_iterator<GpuIndex>(begin),
69 thrust::counting_iterator<GpuIndex>(end),
70 [begin,
71 b = b.Raw(),
72 e = e.Raw(),
73 fLowerUpper = std::forward<FLowerUpper>(fLowerUpper)] PBAT_DEVICE(GpuIndex i) {
74 mini::SMatrix<GpuScalar, kDims, 2> LU = fLowerUpper(i - begin);
75 mini::ToBuffers(LU.Col(0), b, i);
76 mini::ToBuffers(LU.Col(1), e, i);
77 });
78}
79
80template <auto kDims>
81template <auto kSimplexVerts>
82inline void Aabb<kDims>::Construct(
83 common::Buffer<GpuScalar, kDims> const& V,
84 common::Buffer<GpuIndex, kSimplexVerts> const& S,
85 GpuIndex begin)
86{
87 PBAT_PROFILE_CUDA_NAMED_SCOPE("pbat.gpu.impl.geometry.Aabb.Construct");
88 using namespace pbat::math::linalg;
89 auto const nSimplices = static_cast<GpuIndex>(S.Size());
90 if (Size() < nSimplices)
91 Resize(nSimplices);
92 auto const end = std::min(begin + nSimplices, Size());
93 thrust::for_each(
94 thrust::device,
95 thrust::counting_iterator(begin),
96 thrust::counting_iterator(end),
97 [begin, b = b.Raw(), e = e.Raw(), V = V.Raw(), S = S.Raw()] PBAT_DEVICE(GpuIndex i) {
98 auto s = i - begin;
99 auto inds = mini::FromBuffers<kSimplexVerts, 1>(S, s);
100 auto P = mini::FromBuffers(V, inds.Transpose());
101 pbat::common::ForRange<0, kDims>([i, b, e, &P] PBAT_DEVICE<auto d>() {
102 b[d][i] = Min(P.Row(d));
103 e[d][i] = Max(P.Row(d));
104 });
105 });
106}
107
108template <auto kDims>
109inline void Aabb<kDims>::Resize(GpuIndex nBoxes)
110{
111 PBAT_PROFILE_CUDA_NAMED_SCOPE("pbat.gpu.impl.geometry.Aabb.Resize");
112 b.Resize(nBoxes);
113 e.Resize(nBoxes);
114}
115
116} // namespace geometry
117} // namespace impl
118} // namespace gpu
119} // namespace pbat
120
121#endif // PBAT_GPU_IMPL_GEOMETRY_AABB_H
Compile-time for loops.
Definition Buffer.cuh:21
Definition Matrix.h:121
Type aliases for GPU code.
Profiling utilities for host-side GPU code.
constexpr void ForRange(F &&f)
Compile-time for loop over a range of values.
Definition ConstexprFor.h:55
GPU algorithm implementations.
Definition VertexTriangleMixedCcdDcd.h:21
GPU related public functionality.
Definition Buffer.cu:16
Linear Algebra related functionality.
Definition FilterEigenvalues.h:7
The main namespace of the library.
Definition Aliases.h:15
std::int32_t GpuIndex
Index type for GPU code.
Definition Aliases.h:20