SuperDex Physics C++ API
Loading...
Searching...
No Matches
matrix_transform_rt_inl.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
20#include <mochi_core/utils/matrix_transform_rt.h> // for intellisense
23
24namespace superdex {
25
26/************************************************************************************
27 MatrixTransformRT class inlines
28*/
29
30inline MatrixTransformRT::MatrixTransformRT(VMatrix3x3r const& rotation, Vec4r translation)
31 : _rotation(
32 ToSimdDirection(rotation[0]),
33 ToSimdDirection(rotation[1]),
34 ToSimdDirection(rotation[2])),
35 _translation(ToSimdPoint(translation)) {
36#if MOCHI_ASSERT_VERBOSE_ENABLED
37 // Check for illegal scale or skew in the matrix
38 VMatrix3x3r basis = Transpose3x3(_rotation);
39 Real3 scale{Norm<3>(basis[0]), Norm<3>(basis[1]), Norm<3>(basis[2])};
40 Real3 dot{Dot<3>(basis[0], basis[1]), Dot<3>(basis[1], basis[2]), Dot<3>(basis[2], basis[0])};
41 real constexpr kCloseEnough = 1e-4_r;
43 NearEqual(Real3{1_r, 1_r, 1_r}, scale, kCloseEnough),
44 "Non-unit scale detected in 3x3 rotation matrix");
46 NearEqual(Real3{0_r, 0_r, 0_r}, dot, kCloseEnough), "Skew detected in 3x3 rotation matrix");
47#endif // MOCHI_ASSERT_VERBOSE_ENABLED
48}
49
51 Matrix3x3r const& rotation,
52 Real3 const& translation)
53 : MatrixTransformRT(ToSimdMatrix(rotation), ToSimd(translation)) {}
54
56 return ToSimdPoint(DotMatVec3x3(_rotation, pt) + _translation);
57}
58
62
64 return ToSimdPoint(DotVecMat3x3(pt - _translation, _rotation));
65}
66
70
74
78
82
86
92
93/************************************************************************************
94 MatrixTransformRT Utilities
95*/
96
100
104
105MOCHI_FORCE_INLINE MatrixTransformRT
106ToMatrixTransformRT(Quaternion const& rot, Real3 const& trans) {
107 return ToMatrixTransformRT(TransformRT{rot, trans});
108}
109
111 VMatrix3x3r const& rot = Transpose3x3(a.VGetRotation());
112 return VMatrix4x4r{rot[0], rot[1], rot[2], a.VGetTranslation()};
113}
114
116 // Pack the translation into the 4th column
117 VMatrix3x3r const& r = a.VGetRotation();
118 Vec4r t = a.VGetTranslation();
119 return VMatrix4x4r{
123 Vec4r(0_r, 0_r, 0_r, 1_r)};
124}
125
127 // This assumes that the 3x3 rotation is an orthonormal matrix (as required by MatrixTransformRT).
128 auto rot = a.VGetRotation();
129 auto trans = a.VGetTranslation();
130 return MatrixTransformRT{Transpose3x3(rot), DotVecMat3x3(-trans, rot)};
131}
132
135 return AllTrue<3>(
137 VNearEqual(a.VGetTranslation(), b.VGetTranslation(), epsilon));
138}
139
141NearEqual(MatrixTransformRT const& a, MatrixTransformRT const& b, real epsilon) {
142 return NearEqual(a, b, Vec4r{epsilon});
143}
144
145} // namespace superdex
Vec4r InverseTransformDirection(Vec4r pt) const
Vec4r TransformDirection(Vec4r pt) const
MatrixTransformRT operator*(MatrixTransformRT const &rhs) const
Vec4r InverseTransformPoint(Vec4r pt) const
VMatrix3x3r const & VGetRotation() const
Vec4r VGetTranslation() const
#define MOCHI_ASSERT_VERBOSE(condition_without_side_effects,...)
Definition debug.h:102
#define MOCHI_FORCE_INLINE
T Dot(Simd< T, N > a, Simd< T, N > b)
Definition simd.h:666
Simd< T, 4 > ToSimd(NdArray< T, 2 > const &v, T z=T(0), T w=T(0))
Definition vmatrix.h:413
T Norm(Simd< T, N > a)
Definition simd_inl.h:854
bool AllTrue(T const &a)
Definition basic_utils.h:60
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
NdArray< Simd< T, 4 >, 3 > Transpose3x3(NdArray< Simd< T, 4 >, D0 > const &m)
Simd< real, 4 > Vec4r
Definition simd.h:206
V VNearEqual(V a, V b, V epsilon)
Definition simd_inl.h:733
V Broadcast(typename V::Scalar a)
Definition simd_inl.h:115
NdArray< Simd< real, 4 >, 4 > VMatrix4x4r
Definition vmatrix.h:55
Simd< T, N > Blend(Simd< T, N > a, Simd< T, N > b)
Definition simd_inl.h:285
TransformRT Invert(TransformRT const &a)
VMatrix4x4r ToVMatrix4x4(TransformRT const &a)
Simd< T, 4 > DotVecMat3x3(Simd< T, 4 > v, NdArray< Simd< T, 4 >, D0 > const &m)
VMatrix3x3r VGetRotationMatrix(TransformRT const &transform)
NdArray< real, 3 > Real3
Definition nd_array.h:106
Simd< T, 4 > DotMatVec3x3(NdArray< Simd< T, 4 >, D0 > const &m, Simd< T, 4 > v)
MatrixTransformRT ToMatrixTransformRT(TransformRT const &a)
NdArray< real, 3, 3 > Matrix3x3r
Definition nd_array.h:125
VMatrix4x4r ToVMatrix4x4Transpose(TransformRT const &a)
bool NearEqual(TransformRT const &a, TransformRT const &b, real epsilon=kDefaultNearEqualEpsilon< real >)
Quaternion QuaternionFromMatrix(VMatrix3x3r const &matrix, real eps=1e-3_r)
V ToSimdPoint(V a)
Definition simd_inl.h:174
Simd< T, 4 > ToSimdMatrix(NdArray< T, 2, 2 > const &m)
Definition vmatrix.h:428
NdArray< Simd< T, 4 >, 3 > Dot3x3(NdArray< Simd< T, 4 >, D0A > const &a, NdArray< Simd< T, 4 >, D0B > const &b)
TransformRT ToTransformRT(MatrixTransformRT const &a)