Antkeeper  0.0.1
transform.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef ANTKEEPER_MATH_TRANSFORM_HPP
21 #define ANTKEEPER_MATH_TRANSFORM_HPP
22 
23 #include <engine/math/vector.hpp>
25 #include <engine/math/matrix.hpp>
26 
27 namespace math {
28 
34 template <class T>
35 struct transform
36 {
38  using scalar_type = T;
39 
42 
45 
48 
51 
54 
57 
64  [[nodiscard]] constexpr explicit operator matrix_type() const noexcept
65  {
67 
68  matrix[0][0] *= scale[0];
69  matrix[0][1] *= scale[0];
70  matrix[0][2] *= scale[0];
71 
72  matrix[1][0] *= scale[1];
73  matrix[1][1] *= scale[1];
74  matrix[1][2] *= scale[1];
75 
76  matrix[2][0] *= scale[2];
77  matrix[2][1] *= scale[2];
78  matrix[2][2] *= scale[2];
79 
80  matrix[3][0] = translation[0];
81  matrix[3][1] = translation[1];
82  matrix[3][2] = translation[2];
83 
84  return matrix;
85  }
86 
87  [[nodiscard]] inline constexpr matrix_type matrix() const noexcept
88  {
89  return matrix_type(*this);
90  }
92 
93  /*
94  * Type-casts the transform scalars using `static_cast`.
95  *
96  * @tparam U Target scalar type.
97  *
98  * @return Type-casted transform.
99  */
100  template <class U>
101  [[nodiscard]] inline constexpr explicit operator transform<U>() const noexcept
102  {
104  }
105 
107  [[nodiscard]] static inline constexpr transform identity() noexcept
108  {
110  }
111 };
112 
118 template <class T>
119 [[nodiscard]] transform<T> inverse(const transform<T>& t) noexcept;
120 
129 template <class T>
130 [[nodiscard]] transform<T> mul(const transform<T>& x, const transform<T>& y);
131 
140 template <class T>
141 [[nodiscard]] constexpr vector<T, 3> mul(const transform<T>& t, const vector<T, 3>& v) noexcept;
142 
151 template <class T>
152 [[nodiscard]] constexpr vector<T, 3> mul(const vector<T, 3>& v, const transform<T>& t) noexcept;
153 
154 template <class T>
156 {
157  return {-t.translation * t.rotation, conjugate(t.rotation), T{1} / t.scale};
158 }
159 
160 template <class T>
162 {
163  return
164  {
165  mul(x, y.translation),
166  normalize(x.rotation * y.rotation),
167  x.scale * y.scale
168  };
169 }
170 
171 template <class T>
172 constexpr vector<T, 3> mul(const transform<T>& t, const vector<T, 3>& v) noexcept
173 {
174  return t.translation + t.rotation * (t.scale * v);
175 }
176 
177 template <class T>
178 inline constexpr vector<T, 3> mul(const vector<T, 3>& v, const transform<T>& t) noexcept
179 {
180  return mul(inverse(t), v);
181 }
182 
183 namespace operators {
184 
186 template <class T>
187 [[nodiscard]] inline math::transform<T> operator*(const math::transform<T>& x, const math::transform<T>& y)
188 {
189  return mul(x, y);
190 }
191 
193 template <class T>
194 [[nodiscard]] inline constexpr math::vec3<T> operator*(const math::transform<T>& t, const math::vec3<T>& v) noexcept
195 {
196  return mul(t, v);
197 }
198 
200 template <class T>
201 [[nodiscard]] inline constexpr math::vec3<T> operator*(const math::vec3<T>& v, const math::transform<T>& t) noexcept
202 {
203  return mul(v, t);
204 }
205 
214 template <class T>
216 {
217  return (x = x * y);
218 }
219 
220 } // namespace operators
221 
222 } // namespace math
223 
224 // Bring transform operators into global namespace
225 using namespace math::operators;
226 
227 #endif // ANTKEEPER_MATH_TRANSFORM_HPP
Mathematical operators.
constexpr matrix< T, P, M > operator*(const matrix< T, N, M > &a, const matrix< T, P, N > &b) noexcept
Multiplies two matrices.
constexpr matrix< T, N, N > & operator*=(matrix< T, N, N > &a, const matrix< T, N, N > &b) noexcept
Multiplies two values and stores the result in the first value.
Mathematical functions and data types.
Definition: angles.hpp:26
constexpr matrix< T, P, M > mul(const matrix< T, N, M > &a, const matrix< T, P, N > &b) noexcept
Multiplies two matrices.
quaternion< T > normalize(const quaternion< T > &q)
Normalizes a quaternion.
Definition: quaternion.hpp:679
constexpr quaternion< T > conjugate(const quaternion< T > &q) noexcept
Calculates the conjugate of a quaternion.
Definition: quaternion.hpp:566
constexpr matrix< T, N, N > inverse(const matrix< T, N, N > &m) noexcept
Calculates the inverse of a square matrix.
n by m column-major matrix.
Definition: math/matrix.hpp:44
Quaternion composed of a real scalar part and imaginary vector part.
Definition: quaternion.hpp:39
static constexpr quaternion identity() noexcept
Returns a rotation identity quaternion.
Definition: quaternion.hpp:200
SRT transformation.
Definition: transform.hpp:36
vector_type scale
Scale vector.
Definition: transform.hpp:56
static constexpr transform identity() noexcept
Returns an identity transform.
Definition: transform.hpp:107
T scalar_type
Scalar type.
Definition: transform.hpp:38
mat4< T > matrix_type
Transformation matrix type.
Definition: transform.hpp:47
constexpr matrix_type matrix() const noexcept
Constructs a matrix representing the transformation.
Definition: transform.hpp:87
vector_type translation
Translation vector.
Definition: transform.hpp:50
quaternion_type rotation
Rotation quaternion.
Definition: transform.hpp:53
n-dimensional vector.
Definition: vector.hpp:44
static constexpr vector zero() noexcept
Returns a zero vector, where every element is equal to zero.
Definition: vector.hpp:306
static constexpr vector one() noexcept
Returns a vector of ones, where every element is equal to one.
Definition: vector.hpp:324