SuperDex Physics C++ API
Loading...
Searching...
No Matches
obb.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.
28
29#include <type_traits>
30
31namespace superdex {
32
33/**************************************************************************************************
34 Obb - Oriented Bounding Box
35*/
36class Obb final {
37 public:
39
40 // All other constructors forward to this one
41 MOCHI_FORCE_INLINE Obb(MatrixTransformRT const& transform, Vec4r halfExtents)
42 : _transform(transform), _halfExtents(ToSimdDirection(halfExtents)) {
44 (Get<0>(_halfExtents) >= 0_r) && (Get<1>(_halfExtents) >= 0_r) &&
45 (Get<2>(_halfExtents) >= 0_r),
46 "Extents must >= 0");
47 }
48
49 // clang-format off
50
51 MOCHI_FORCE_INLINE Obb(TransformRT const& transform, Vec4r halfExtents) : Obb(ToMatrixTransformRT(transform), halfExtents) {}
52 MOCHI_FORCE_INLINE Obb(TransformRT const& transform, Real3 const& halfExtents) : Obb(ToMatrixTransformRT(transform), ToSimd(halfExtents)) {}
53 MOCHI_FORCE_INLINE Obb(MatrixTransformRT const& transform, Real3 const& halfExtents) : Obb(transform, ToSimd(halfExtents)) {}
54
55 // SIMD accessors (note that the 4th component is always 1 by convention)
56 [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetCenter() const { return _transform.VGetTranslation(); }
57 [[nodiscard]] MOCHI_FORCE_INLINE VMatrix3x3r VGetRotation() const { return _transform.VGetRotation(); }
58 [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetHalfExtents() const { return _halfExtents; }
59 [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetSize() const { return 2.0_r * _halfExtents; }
60
61 // Scalar accessors (for convenience)
62 [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetCenter() const { return ToReal3(VGetCenter()); }
63 [[nodiscard]] MOCHI_FORCE_INLINE Matrix3x3r GetRotation() const { return _transform.GetRotation(); }
64 [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetHalfExtents() const { return ToReal3(VGetHalfExtents()); }
65 [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetSize() const { return ToReal3(VGetSize()); }
66
67 // Retrieves orientation and center as translation.
68 [[nodiscard]] MOCHI_FORCE_INLINE MatrixTransformRT const& GetTransform() const { return _transform; }
69
70 // Retrieves the Obb's corners.
71 template <int i> [[nodiscard]] MOCHI_FORCE_INLINE Vec4r VGetCorner() const {
72 // 3 +--------+ 7
73 // /| /| y z
74 // / | / | | /
75 // 2 +--------+ 6| |/
76 // | | | | *-- x
77 // |1 +-----|--+ 5
78 // | / | /
79 // |/ |/
80 // 0 +--------+ 4
81 bool constexpr negX = (i / 4) % 2 == 0;
82 bool constexpr negY = (i / 2) % 2 == 0;
83 bool constexpr negZ = i % 2 == 0;
84 return _transform.TransformPoint(Neg<negX, negY, negZ, false>(_halfExtents));
85 }
86
91
92 template <int i> [[nodiscard]] MOCHI_FORCE_INLINE Real3 GetCorner() const { return ToReal3(VGetCorner<i>()); }
93
98
99 // clang-format on
100
101 private:
102 // TODO: Look into the slow performance of Obb geometry utilities.
103 // TODO: Remove SIMD padding from this class, or at least remove it from reflection serialization.
104 MatrixTransformRT _transform;
105 Vec4r _halfExtents = SimdZero();
106
107 public:
109 MOCHI_FIELD_NAME(_transform, "transform")
110 MOCHI_FIELD_NAME(_halfExtents, "halfExtents")
112};
113
114namespace details {
115template <>
116inline constexpr bool IsPrimitiveShapeDef<Obb> = true;
117} // namespace details
118
119} // namespace superdex
Real3 GetCenter() const
Definition obb.h:62
Real3 GetHalfExtents() const
Definition obb.h:64
Real3 GetCorner() const
Definition obb.h:92
VMatrix3x3r VGetRotation() const
Definition obb.h:57
Obb(MatrixTransformRT const &transform, Vec4r halfExtents)
Definition obb.h:41
Vec4r VGetCorner() const
Definition obb.h:71
Vec4r VGetCenter() const
Definition obb.h:56
Matrix3x3r GetRotation() const
Definition obb.h:63
Obb(MatrixTransformRT const &transform, Real3 const &halfExtents)
Definition obb.h:53
Vec4r VGetHalfExtents() const
Definition obb.h:58
Obb(TransformRT const &transform, Vec4r halfExtents)
Definition obb.h:51
NdArray< real, 8, 3 > GetCorners() const
Definition obb.h:94
MatrixTransformRT const & GetTransform() const
Definition obb.h:68
Real3 GetSize() const
Definition obb.h:65
Vec4r VGetSize() const
Definition obb.h:59
Obb(TransformRT const &transform, Real3 const &halfExtents)
Definition obb.h:52
NdArray< Vec4r, 8 > VGetCorners() const
Definition obb.h:87
Obb()=default
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
Definition debug.h:102
#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
NdArray< Simd< real, 4 >, 3 > VMatrix3x3r
Definition vmatrix.h:52
V ToSimdDirection(V a)
Definition simd_inl.h:179
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
T Get(Simd< T, N > v)
Definition simd_inl.h:300
NdArray< real, 3 > Real3
Definition nd_array.h:106
MatrixTransformRT ToMatrixTransformRT(TransformRT const &a)
NdArray< real, 3, 3 > Matrix3x3r
Definition nd_array.h:125
Simd< T, N > Neg(Simd< T, N > a)
Definition simd_inl.h:340
#define MOCHI_FIELD_NAME(realName, customName)
Definition reflection.h:294
#define MOCHI_STRUCT_END()
Definition reflection.h:279
#define MOCHI_STRUCT_BEGIN(name)
Definition reflection.h:278