Antkeeper  0.0.1
transform-functions.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_FUNCTIONS_HPP
21 #define ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP
22 
25 
26 namespace math {
27 
33 template <class T>
34 [[nodiscard]] transform<T> inverse(const transform<T>& t);
35 
42 template <class T>
43 [[nodiscard]] matrix<T, 4, 4> matrix_cast(const transform<T>& t);
44 
52 template <class T>
53 [[nodiscard]] transform<T> mul(const transform<T>& x, const transform<T>& y);
54 
62 template <class T>
63 [[nodiscard]] vector<T, 3> mul(const transform<T>& t, const vector<T, 3>& v);
64 
65 template <class T>
67 {
68  transform<T> inverse_t;
69  inverse_t.scale = {T(1) / t.scale.x(), T(1) / t.scale.y(), T(1) / t.scale.z()};
70  inverse_t.rotation = conjugate(t.rotation);
71  inverse_t.translation = negate(mul(inverse_t.rotation, t.translation));
72  return inverse_t;
73 }
74 
75 template <class T>
77 {
78  matrix<T, 4, 4> transformation = matrix<T, 4, 4>(matrix<T, 3, 3>(t.rotation));
79  transformation[3] = {t.translation[0], t.translation[1], t.translation[2], T(1)};
80  return scale(transformation, t.scale);
81 }
82 
83 template <class T>
85 {
86  return
87  {
88  mul(x, y.translation),
89  normalize(mul(x.rotation, y.rotation)),
90  mul(x.scale, y.scale)
91  };
92 }
93 
94 template <class T>
96 {
97  return add(t.translation, (mul(t.rotation, mul(v, t.scale))));
98 }
99 
100 } // namespace math
101 
102 #endif // ANTKEEPER_MATH_TRANSFORM_FUNCTIONS_HPP
103 
Mathematical functions and data types.
Definition: angles.hpp:26
constexpr matrix< T, 4, 4 > scale(const matrix< T, 4, 4 > &m, const vector< T, 3 > &v)
Scales a matrix.
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:603
constexpr matrix< T, N, M > add(const matrix< T, N, M > &a, const matrix< T, N, M > &b) noexcept
Adds two matrices.
matrix< T, 4, 4 > matrix_cast(const transform< T > &t)
Converts a transform to a transformation matrix.
constexpr quaternion< T > conjugate(const quaternion< T > &q) noexcept
Calculates the conjugate of a quaternion.
Definition: quaternion.hpp:492
constexpr matrix< T, N, N > inverse(const matrix< T, N, N > &m) noexcept
Calculates the inverse of a square matrix.
constexpr quaternion< T > negate(const quaternion< T > &q) noexcept
Negates a quaternion.
Definition: quaternion.hpp:591
n by m column-major matrix.
Definition: math/matrix.hpp:44
Represents 3D TRS transformation.
quaternion< T > rotation
Rotation quaternion.
vector< T, 3 > translation
Translation vector.
vector< T, 3 > scale
Scale vector.