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
AxisAlignedBoundingBox.h
Go to the documentation of this file.
1
11#ifndef PBAT_GEOMETRY_AXISALIGNEDBOUNDINGBOX_H
12#define PBAT_GEOMETRY_AXISALIGNEDBOUNDINGBOX_H
13
14#include <Eigen/Geometry>
15#include <array>
16#include <pbat/Aliases.h>
17#include <vector>
18
19namespace pbat::geometry {
27template <int Dims>
28class AxisAlignedBoundingBox : public Eigen::AlignedBox<Scalar, Dims>
29{
30public:
31 using BaseType = Eigen::AlignedBox<Scalar, Dims>;
32 using SelfType = AxisAlignedBoundingBox;
33
34 static auto constexpr kDims = Dims;
35
36 AxisAlignedBoundingBox() = default;
37
42 AxisAlignedBoundingBox(BaseType const& box);
47 AxisAlignedBoundingBox(BaseType&& box);
53 AxisAlignedBoundingBox& operator=(BaseType const& box);
59 AxisAlignedBoundingBox& operator=(BaseType&& box);
68 template <class TDerivedMin, class TDerivedMax>
69 AxisAlignedBoundingBox(
70 Eigen::DenseBase<TDerivedMin> const& min,
71 Eigen::DenseBase<TDerivedMax> const& max);
78 template <class TDerived>
79 AxisAlignedBoundingBox(Eigen::DenseBase<TDerived> const& P);
86 template <class TDerived>
87 std::vector<Index> contained(Eigen::MatrixBase<TDerived> const& P) const;
88};
89
90template <int Dims>
91inline AxisAlignedBoundingBox<Dims>::AxisAlignedBoundingBox(BaseType const& box)
92 : BaseType(box)
93{
94}
95
96template <int Dims>
97inline AxisAlignedBoundingBox<Dims>::AxisAlignedBoundingBox(BaseType&& box)
98 : BaseType(box)
99{
100}
101
102template <int Dims>
103inline AxisAlignedBoundingBox<Dims>& AxisAlignedBoundingBox<Dims>::operator=(BaseType const& box)
104{
105 BaseType::template operator=(box);
106 return *this;
107}
108
109template <int Dims>
110inline AxisAlignedBoundingBox<Dims>& AxisAlignedBoundingBox<Dims>::operator=(BaseType&& box)
111{
112 BaseType::template operator=(box);
113 return *this;
114}
115
116template <int Dims>
117template <class TDerivedMin, class TDerivedMax>
118inline AxisAlignedBoundingBox<Dims>::AxisAlignedBoundingBox(
119 Eigen::DenseBase<TDerivedMin> const& min,
120 Eigen::DenseBase<TDerivedMax> const& max)
121 : BaseType(min, max)
122{
123}
124
125template <int Dims>
126template <class TDerived>
127inline AxisAlignedBoundingBox<Dims>::AxisAlignedBoundingBox(Eigen::DenseBase<TDerived> const& P)
128 : BaseType(P.rowwise().minCoeff(), P.rowwise().maxCoeff())
129{
130}
131
132template <int Dims>
133template <class TDerived>
134inline std::vector<Index>
135AxisAlignedBoundingBox<Dims>::contained(Eigen::MatrixBase<TDerived> const& P) const
136{
137 std::vector<Index> inds{};
138 inds.reserve(static_cast<std::size_t>(P.cols()));
139 for (auto i = 0; i < P.cols(); ++i)
140 {
141 if (BaseType::template contains(P.col(i)))
142 {
143 inds.push_back(static_cast<Index>(i));
144 }
145 }
146 return inds;
147}
148
163template <auto kDims, auto kClusterNodes, class FCluster, class TDerivedL, class TDerivedU>
164inline void ClustersToAabbs(
165 FCluster fCluster,
166 Index nClusters,
167 Eigen::DenseBase<TDerivedL>& L,
168 Eigen::DenseBase<TDerivedU>& U)
169{
170 using MatrixType = std::invoke_result_t<FCluster, Index>;
171 for (auto c = 0; c < nClusters; ++c)
172 {
173 MatrixType const& XC = fCluster(c);
174 L.col(c).template head<kDims>() = XC.rowwise().minCoeff();
175 U.col(c).template head<kDims>() = XC.rowwise().maxCoeff();
176 }
177}
178
191template <auto kDims, auto kClusterNodes, class FCluster, class TDerivedB>
192inline void ClustersToAabbs(FCluster fCluster, Index nClusters, Eigen::DenseBase<TDerivedB>& B)
193{
194 using MatrixType = std::invoke_result_t<FCluster, Index>;
195 for (auto c = 0; c < nClusters; ++c)
196 {
197 MatrixType const& XC = fCluster(c);
198 B.col(c).template head<kDims>() = XC.rowwise().minCoeff();
199 B.col(c).template tail<kDims>() = XC.rowwise().maxCoeff();
200 }
201}
202
217template <
218 auto kDims,
219 auto kElemNodes,
220 class TDerivedX,
221 class TDerivedE,
222 class TDerivedL,
223 class TDerivedU>
224inline void MeshToAabbs(
225 Eigen::DenseBase<TDerivedX> const& X,
226 Eigen::DenseBase<TDerivedE> const& E,
227 Eigen::DenseBase<TDerivedL>& L,
228 Eigen::DenseBase<TDerivedU>& U)
229{
231 [&](Index e) {
232 return X(Eigen::placeholders::all, E.col(e))
233 .template topLeftCorner<kDims, kElemNodes>();
234 },
235 E.cols(),
236 L,
237 U);
238}
239
252template <auto kDims, auto kElemNodes, class TDerivedX, class TDerivedE, class TDerivedB>
253inline void MeshToAabbs(
254 Eigen::DenseBase<TDerivedX> const& X,
255 Eigen::DenseBase<TDerivedE> const& E,
256 Eigen::DenseBase<TDerivedB>& B)
257{
259 [&](Index e) {
260 return X(Eigen::placeholders::all, E.col(e))
261 .template topLeftCorner<kDims, kElemNodes>();
262 },
263 E.cols(),
264 B);
265}
266} // namespace pbat::geometry
267
268#endif // PBAT_GEOMETRY_AXISALIGNEDBOUNDINGBOX_H
static auto constexpr kDims
Number of dimensions.
Definition AxisAlignedBoundingBox.h:34
std::vector< Index > contained(Eigen::MatrixBase< TDerived > const &P) const
Get indices of points in P contained in the bounding box.
Definition AxisAlignedBoundingBox.h:135
AxisAlignedBoundingBox SelfType
Self type.
Definition AxisAlignedBoundingBox.h:32
AxisAlignedBoundingBox & operator=(BaseType const &box)
Copy assign AxisAlignedBoundingBox from Eigen::AlignedBox.
Definition AxisAlignedBoundingBox.h:103
Eigen::AlignedBox< Scalar, Dims > BaseType
Base type.
Definition AxisAlignedBoundingBox.h:31
Geometric queries, quantities and data structures.
Definition AabbKdTreeHierarchy.h:23
void ClustersToAabbs(FCluster fCluster, Index nClusters, Eigen::DenseBase< TDerivedL > &L, Eigen::DenseBase< TDerivedU > &U)
Computes AABBs of nClusters kDims-dimensional point clusters.
Definition AxisAlignedBoundingBox.h:164
void MeshToAabbs(Eigen::DenseBase< TDerivedX > const &X, Eigen::DenseBase< TDerivedE > const &E, Eigen::DenseBase< TDerivedL > &L, Eigen::DenseBase< TDerivedU > &U)
Computes AABBs of nElemNodes simplex mesh elements in kDims dimensions.
Definition AxisAlignedBoundingBox.h:224
std::ptrdiff_t Index
Index type.
Definition Aliases.h:17