SA_Maths
Sapphire Suite's C++ Maths Library
Loading...
Searching...
No Matches
TransformTRSMatrixFunctor.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Sapphire Development Team. All Rights Reserved.
2
3#pragma once
4
5#ifndef SAPPHIRE_MATHS_TRANSFORM_TRS_MATRIX_FUNCTOR_GUARD
6#define SAPPHIRE_MATHS_TRANSFORM_TRS_MATRIX_FUNCTOR_GUARD
7
9
11
16
27namespace SA
28{
35 {
36 template <typename T, template <typename> typename... TrArgs>
37 Vec3<T> ComputeScale(const Tr<T, TrArgs...>& _tr)
38 {
39 // Get global scale from components.
40
41 if constexpr (TrHasComponent<TrScale>() && TrHasComponent<TrUScale>())
42 return _tr.scale * _tr.uScale;
43 else if constexpr (TrHasComponent<TrScale>())
44 return _tr.scale;
45 else if constexpr (TrHasComponent<TrUScale>())
46 return _tr.uScale;
47 else
48 return Vec3<T>::One;
49 }
50
51 template <typename T>
52 void ApplyScaleToRotation(const Vec3<T>& _scale, Mat4<T>& _rot)
53 {
54 /*
55 * Optimization of R * S.
56 *
57 * R { S {
58 * { a, b, c, 0 }, { Sx, 0, 0, 0 },
59 * { d, e, f, 0 }, { 0, Sy, 0, 0 },
60 * { g, h, i, 0 }, { 0, 0, Sz, 0 },
61 * { 0, 0, 0, 1 } { 0, 0, 0, 1}
62 * } }
63 *
64 *
65 * Result {
66 * { a Sx, b Sy, c Sz, 0 },
67 * { d Sx, e Sy, f Sz, 0 },
68 * { g Sx, h Sy, i Sz, 0 },
69 * { 0, 0, 0, 1 }
70 * }
71 *
72 */
73
74 _rot.e00 *= _scale.x;
75 _rot.e01 *= _scale.y;
76 _rot.e02 *= _scale.z;
77
78 _rot.e10 *= _scale.x;
79 _rot.e11 *= _scale.y;
80 _rot.e12 *= _scale.z;
81
82 _rot.e20 *= _scale.x;
83 _rot.e21 *= _scale.y;
84 _rot.e22 *= _scale.z;
85 }
86
87 template <typename T>
88 void ApplyTranslation(const Vec3<T>& _trsl, Mat4<T>& _rot)
89 {
90 /*
91 * Optimization of T * M with:
92 *
93 * T { M {
94 * { 1, 0, 0, Tx }, { a, b, c, 0 },
95 * { 0, 1, 0, Ty }, { d, e, f, 0 },
96 * { 0, 0, 1, Tz }, { g, h, i, 0 },
97 * { 0, 0, 0, 1 } { 0, 0, 0, 1}
98 * } }
99 *
100 *
101 * Result {
102 * { a, b, c, Tx },
103 * { d, e, f, Ty },
104 * { g, h, i, Tz },
105 * { 0, 0, 0, 1 }
106 * }
107 *
108 */
109
110 _rot.e03 = _trsl.x;
111 _rot.e13 = _trsl.y;
112 _rot.e23 = _trsl.z;
113 }
114
115 public:
125 template <typename T, template <typename> typename... TrArgs>
127 {
129
130 const Vec3<T> scale = ComputeScale(_tr);
131 constexpr bool bHasScale = TrHasComponent<TrScale>() || TrHasComponent<TrUScale>();
132
133 if constexpr (TrHasComponent<TrRotation>())
134 {
135 out = Mat4<T>::MakeRotation(_tr.rotation);
136
137 if constexpr (bHasScale)
138 ApplyScaleToRotation(scale, out);
139 }
140 else if constexpr (bHasScale)
141 {
142 // Scale but no rotation: simple scale matrix.
143
144 out = Mat4<T>::MakeScale(scale);
145 }
146
147 if constexpr (TrHasComponent<TrPosition>())
148 ApplyTranslation(_tr.position, out);
149
150 return out;
151 }
152 };
153}
154
155
158#endif // GUARD
Matrix 4x4 type implementation.
Transform Functor base helper definition.
Transform position component definition.
Transform rotation component definition.
Transform scale component definition.
Transform uniform scale component definition.
Compute TRS Matrix from Transform. Order is applied from right to left: (T * (R * S))....
Definition TransformTRSMatrixFunctor.hpp:35
Mat4< T > operator()(const Tr< T, TrArgs... > &_tr)
Compute matrix from input transform. Functor implementation.
Definition TransformTRSMatrixFunctor.hpp:126
Matrix 4x4 Sapphire-Maths class.
Definition Matrix4.hpp:68
static Mat4 MakeRotation(const Quat< T > &_rot) noexcept
Make rotation matrix from quaternion.
static Mat4 MakeScale(const Vec3< T > &_scale) noexcept
Make scale matrix from vector3.
Transform Sapphire's class.
Definition Transform.hpp:33
Vector 3 Sapphire-Maths class.
Definition Vector3.hpp:43
T z
Vector's Z component (axis value).
Definition Vector3.hpp:54
T y
Vector's Y component (axis value).
Definition Vector3.hpp:51
T x
Vector's X component (axis value).
Definition Vector3.hpp:48