SuperDex Physics C++ API
Loading...
Searching...
No Matches
transform_rt.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 <utility>
28
29namespace superdex {
30
31/**************************************************************************************************
32 Affine Transform (rotation & translation, but no scale)
33*/
35 // Tolerance for unitary quaternion checks.
36 [[maybe_unused]] static constexpr real kQuaternionTol = 50_r * kDefaultNearEqualEpsilon<real>;
37
38 public:
39 // Constructors
40 TransformRT() = default;
41 explicit TransformRT(Quaternion const& rotation, Vec4r translation);
42 explicit TransformRT(Quaternion const& rotation, Real3 const& translation);
43 explicit TransformRT(Quaternion const& rotation);
44 explicit TransformRT(Vec4r translation);
45 explicit TransformRT(Real3 const& translation);
46
47 [[nodiscard]] static TransformRT Identity();
48 [[nodiscard]] static TransformRT FromOrthoNormal(VMatrix4x4r const& m);
49 [[nodiscard]] static TransformRT FromOrthoNormalTranspose(VMatrix4x4r const& mT);
50
51 // Rotation
52 [[nodiscard]] Quaternion const& GetRotation() const;
53 void SetRotation(Quaternion const& rotation); // should be unit length
54
55 // Translation
56 [[nodiscard]] Real3 GetTranslation() const;
57 [[nodiscard]] Vec4r VGetTranslation() const; // (x,y,z,1)
58 void SetTranslation(Real3 translation);
59 void SetTranslation(Vec4r translation);
60
61 // Exact Equality
62 [[nodiscard]] bool operator==(TransformRT const& rhs) const;
63 [[nodiscard]] bool operator!=(TransformRT const& rhs) const;
64
65 // Multiplication with another TransformRT concatenates transforms
66 [[nodiscard]] TransformRT operator*(TransformRT const& rhs) const;
68
69 // Utilities to apply the transform to a point or a direction.
70 [[nodiscard]] Real3 TransformPoint(Real3 const& pt) const; // rotates & translates
71 [[nodiscard]] Real3 TransformDirection(Real3 const& dir) const; // only rotates
72
73 [[nodiscard]] Vec4r TransformPoint(Vec4r const& pt) const; // rotates & translates
74 [[nodiscard]] Vec4r TransformDirection(Vec4r const& dir) const; // only rotates
75
76 // Utilities to apply the inverse transform to a point or a direction.
77 // WARNING: These utilities are slow. To invert multiple points or directions, consider
78 // precomputing the inverse transform as VMatrix and applying it to all points or directions.
79 [[nodiscard]] Real3 TransformPointInverse(Real3 const& pt) const; // rotates & translates
80 [[nodiscard]] Real3 TransformDirectionInverse(Real3 const& dir) const; // only rotates
81
82 [[nodiscard]] Vec4r TransformPointInverse(Vec4r const& pt) const; // rotates & translates
83 [[nodiscard]] Vec4r TransformDirectionInverse(Vec4r const& dir) const; // only rotates
84
85 private:
86 void WarnIfRotationNotNormalized() const;
87
88 Quaternion _rotation = {};
89 Real3 _translation{0_r, 0_r, 0_r};
90
91 public:
93 MOCHI_FIELD_NAME(_rotation, "rotation")
94 MOCHI_FIELD_NAME(_translation, "translation")
96};
97
98/************************************************************************************
99 Utilities
100*/
101
102// Compute a Matrix4x4r from TransformRT. The resulting matrix can be used to transform vectors by
103// calling DotMatVec4x4(VMatrix4x4r, Vec4r), which is equivalent to (TransformRT * Vec4r). However,
104// if you have to transform multiple points, consider using ToVMatrix4x4Transpose to get the
105// transpose, so that you can then use DotVecMat4x4 instead. DotVecMat4x4 is >40% faster with SIMD
106// than DotMatVec4x4.
108
109// Equivalent to Transpose4x4(ToVMatrix4x4(a)) only faster.
111
112// Take a 4x4 matrix consisting of scale (possibly non-uniform, possibly negative), rotation, and
113// translation. Return the scale and TransformRT that could be re-combined to form the same matrix.
114std::pair<Real3, TransformRT> DecomposeMatrixTransform(VMatrix4x4r const& matrixTransform);
115
116// NearEqual: (abs(a-b) <= epsilon)
117[[nodiscard]] MOCHI_FORCE_INLINE bool NearEqual(
118 TransformRT const& a,
119 TransformRT const& b,
121
122// IsFinite returns true if all values are finite
123[[nodiscard]] MOCHI_FORCE_INLINE bool IsFinite(TransformRT const& a);
124
125// Invert TransformRT (opposite rotation & translation)
126[[nodiscard]] MOCHI_FORCE_INLINE TransformRT Invert(TransformRT const& a);
127
128// Return a TransformRT with a normalized (unit length) rotation Quaternion.
130
131// Repivot a transform such that calling the returned transform with the given pivot produces the
132// same results.
134Repivot(TransformRT const& transform, Real3 const& pivot);
135
136// Interpolate
137[[nodiscard]] TransformRT Interpolate(TransformRT const& a, TransformRT const& b, real t);
138
139} // namespace superdex
140
141#include "transform_rt_inl.h"
static TransformRT Identity()
Real3 TransformPointInverse(Real3 const &pt) const
Quaternion const & GetRotation() const
Vec4r VGetTranslation() const
Real3 TransformDirectionInverse(Real3 const &dir) const
bool operator!=(TransformRT const &rhs) const
Real3 TransformDirection(Real3 const &dir) const
TransformRT operator*(TransformRT const &rhs) const
void SetTranslation(Real3 translation)
TransformRT & operator*=(TransformRT const &rhs)
bool operator==(TransformRT const &rhs) const
static TransformRT FromOrthoNormalTranspose(VMatrix4x4r const &mT)
Real3 TransformPoint(Real3 const &pt) const
static TransformRT FromOrthoNormal(VMatrix4x4r const &m)
void SetRotation(Quaternion const &rotation)
#define MOCHI_FORCE_INLINE
constexpr T kDefaultNearEqualEpsilon
Simd< real, 4 > Vec4r
Definition simd.h:206
std::pair< Real3, TransformRT > DecomposeMatrixTransform(VMatrix4x4r const &matrixTransform)
NdArray< Simd< real, 4 >, 4 > VMatrix4x4r
Definition vmatrix.h:55
TransformRT Invert(TransformRT const &a)
VMatrix4x4r ToVMatrix4x4(TransformRT const &a)
NdArray< real, 3 > Real3
Definition nd_array.h:106
TransformRT Repivot(TransformRT const &transform, Real3 const &pivot)
VMatrix4x4r ToVMatrix4x4Transpose(TransformRT const &a)
TransformRT NormalizeRotation(TransformRT const &a)
TransformRT Interpolate(TransformRT const &a, TransformRT const &b, real t)
bool NearEqual(TransformRT const &a, TransformRT const &b, real epsilon=kDefaultNearEqualEpsilon< real >)
bool IsFinite(TransformRT const &a)
#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