SuperDex Physics C++ API
Loading...
Searching...
No Matches
aabb.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19// PLEASE DO NOT ADD OTHER INCLUDES HERE. This header is included in the mochi_physics public API.
26
27#include <type_traits>
28
29namespace superdex {
30
31/**************************************************************************************************
32 Aabb - Axis-Aligned Bounding Box
33*/
34class Aabb final {
35 public:
36 // clang-format off
37
39 MOCHI_FORCE_INLINE Aabb(Real3 const& min, Real3 const& max) : _min(ToSimd(min, 1_r)), _max(ToSimd(max, 1_r)) {}
40 MOCHI_FORCE_INLINE Aabb(Vec4r min, Vec4r max) : _min(ToSimdPoint(min)), _max(ToSimdPoint(max)) {}
41
42 // SIMD accessors (note that the 4th component is always 1 by convention)
43 [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetMin() const { return _min; }
44 [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetMax() const { return _max; }
45 [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetHalfExtents() const { return 0.5_r * VGetSize(); }
46 [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetCenter() const { return 0.5_r * (_max + _min); }
47 [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetSize() const { return _max - _min; }
48
49 // Scalar accessors
50 [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetMin() const { return ToReal3(_min); }
51 MOCHI_FORCE_INLINE void SetMin(Real3 const& min) { _min = ToSimd(min, 1_r); }
52 [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetMax() const { return ToReal3(_max); }
53 MOCHI_FORCE_INLINE void SetMax(Real3 const& max) { _max = ToSimd(max, 1_r); }
54 [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetHalfExtents() const { return ToReal3(VGetHalfExtents()); }
55 [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetCenter() const { return ToReal3(VGetCenter()); }
56 [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetSize() const { return ToReal3(VGetSize()); }
57
58 // Exact equality
59 [[nodiscard]] MOCHI_FORCE_INLINE bool operator==(Aabb const& rhs) const { return AllTrue<3>(VEqual(_min, rhs._min) & VEqual(_max, rhs._max)); }
60 [[nodiscard]] MOCHI_FORCE_INLINE bool operator!=(Aabb const& rhs) const { return !(*this == rhs); }
61
62 // clang-format on
63 private:
64 Vec4r _min = ToSimdPoint(SimdZero());
65 Vec4r _max = ToSimdPoint(SimdZero());
66
67#if MOCHI_USE_REFLECTION
68 template <typename T>
69 friend struct ::SReflectTypeTraits;
70#endif // MOCHI_USE_REFLECTION
71};
72
73namespace details {
74template <>
75inline constexpr bool IsPrimitiveShapeDef<Aabb> = true;
76} // namespace details
77
78} // namespace superdex
79
80/************************************************************************************
81 Reflection support for Aabb.
82 Serializes like struct with two fields of type Real3.
83*/
84#if MOCHI_USE_REFLECTION
85template <>
86struct SReflectTypeTraits<superdex::Aabb> {
87 static constexpr SReflect::CoreType coreType = SReflect::CoreType::CT_struct;
88 static SReflect::StructTypeInfo const& GetTypeInfo() {
89 static auto const* s_typeInfo = []() {
90 auto* ti = SReflect::MakeStructTypeInfo<superdex::Aabb>("superdex::Aabb");
91 SReflect::detail::AddField(
92 *ti, "min", offsetof(superdex::Aabb, _min), SReflect::GetTypeInfo<superdex::Real3>());
93 SReflect::detail::AddField(
94 *ti, "max", offsetof(superdex::Aabb, _max), SReflect::GetTypeInfo<superdex::Real3>());
95 return ti;
96 }();
97 return *s_typeInfo;
98 }
99};
100#endif // MOCHI_USE_REFLECTION
Aabb()=default
Vec4r VGetSize() const
Definition aabb.h:47
Real3 GetHalfExtents() const
Definition aabb.h:54
Vec4r VGetCenter() const
Definition aabb.h:46
void SetMax(Real3 const &max)
Definition aabb.h:53
bool operator==(Aabb const &rhs) const
Definition aabb.h:59
Real3 GetMin() const
Definition aabb.h:50
Real3 GetCenter() const
Definition aabb.h:55
Aabb(Real3 const &min, Real3 const &max)
Definition aabb.h:39
void SetMin(Real3 const &min)
Definition aabb.h:51
Vec4r VGetHalfExtents() const
Definition aabb.h:45
Real3 GetMax() const
Definition aabb.h:52
Vec4r VGetMax() const
Definition aabb.h:44
bool operator!=(Aabb const &rhs) const
Definition aabb.h:60
Real3 GetSize() const
Definition aabb.h:56
Aabb(Vec4r min, Vec4r max)
Definition aabb.h:40
Vec4r VGetMin() const
Definition aabb.h:43
#define MOCHI_FORCE_INLINE
Simd< T, 4 > ToSimd(NdArray< T, 2 > const &v, T z=T(0), T w=T(0))
Definition vmatrix.h:413
bool AllTrue(T const &a)
Definition basic_utils.h:60
V VEqual(V a, V b)
Definition simd_inl.h:700
Real3 ToReal3(Simd< T, 4 > v)
Definition vmatrix.h:354
Simd< real, 4 > Vec4r
Definition simd.h:206
V SimdZero()
Definition simd_inl.h:146
NdArray< real, 3 > Real3
Definition nd_array.h:106
V ToSimdPoint(V a)
Definition simd_inl.h:174