stellarlib 0.1.0
Loading...
Searching...
No Matches
transformations.hpp
1/* clang-format off */
2
3/*
4 stellarlib
5 Copyright (C) 2025-2026 Domán Zana
6
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 2. Altered source versions must be plainly marked as such, and must not be
20 misrepresented as being the original software.
21 3. This notice may not be removed or altered from any source distribution.
22*/
23
24#ifndef STELLARLIB_LIN_TRANSFORMATIONS_HPP
25#define STELLARLIB_LIN_TRANSFORMATIONS_HPP
26
27#include <stellarlib/lin/intrinsics.hpp>
28#include <stellarlib/lin/matrix.hpp>
29
30#include <cstddef>
31#include <type_traits>
32
33/**
34 * @brief Linear algebra utilities
35 */
36namespace stellarlib::lin
37{
38/**
39 * @brief Translates a 3x3 transformation matrix using a 2D vector
40 * @param m 3x3 transformation matrix to be translated
41 * @param v 2D translation vector
42 * @return Translated 3x3 transformation matrix
43 */
44template <typename T, typename U, std::size_t M, std::size_t N>
45[[nodiscard]]
46constexpr auto translate(const internal::matrix<T, 3, 3> &m, const internal::matrix<U, M, N> &v) noexcept
47 -> internal::matrix<std::common_type_t<T, U>, 3, 3>
48 requires (M * N == 2)
49{
50 internal::matrix<std::common_type_t<T, U>, 3, 3> res;
51 res[0] = m[0];
52 res[1] = m[1];
53 res[2] = m[0] * v.x() + m[1] * v.y() + m[2];
54 return res;
55}
56
57/**
58 * @brief Translates a 4x4 transformation matrix using a 3D vector
59 * @param m 4x4 transformation matrix to be translated
60 * @param v 3D translation vector
61 * @return Translated 4x4 transformation matrix
62 */
63template <typename T, typename U, std::size_t M, std::size_t N>
64[[nodiscard]]
65constexpr auto translate(const internal::matrix<T, 4, 4> &m, const internal::matrix<U, M, N> &v) noexcept
66 -> internal::matrix<std::common_type_t<T, U>, 4, 4>
67 requires (M * N == 3)
68{
69 internal::matrix<std::common_type_t<T, U>, 4, 4> res;
70 res[0] = m[0];
71 res[1] = m[1];
72 res[2] = m[2];
73 res[3] = m[0] * v.x() + m[1] * v.y() + m[2] * v.z() + m[3];
74 return res;
75}
76
77/**
78 * @brief Rotates a 3x3 transformation matrix using radians
79 * @param m 3x3 transformation matrix to be rotated
80 * @param angle Rotation angle in radians
81 * @return Rotated 3x3 transformation matrix
82 */
83template <typename T, typename U>
84[[nodiscard]]
85constexpr auto rotate(const internal::matrix<T, 3, 3> &m, const U angle) noexcept
86 -> internal::matrix<std::common_type_t<T, U>, 3, 3>
87 requires (std::is_arithmetic_v<U>)
88{
89 const auto c{internal::cos(angle)};
90 const auto s{internal::sin(angle)};
91 internal::matrix<std::common_type_t<T, U>, 3, 3> res;
92 res[0] = m[0] * c + m[1] * s;
93 res[1] = m[0] * -s + m[1] * c;
94 res[2] = m[2];
95 return res;
96}
97
98/**
99 * @brief Rotates a 4x4 transformation matrix using radians and a 3D axis
100 * @param m 4x4 transformation matrix to be rotated
101 * @param angle Rotation angle in radians
102 * @param axis 3D rotation axis
103 * @return Rotated 4x4 transformation matrix
104 */
105template <typename T, typename U, typename V, std::size_t M, std::size_t N>
106[[nodiscard]]
107constexpr auto rotate(const internal::matrix<T, 4, 4> &m, const U angle, const internal::matrix<V, M, N> &axis) noexcept
108 -> internal::matrix<std::common_type_t<T, U, V>, 4, 4>
109 requires (std::is_arithmetic_v<U> && M * N == 3)
110{
111 const auto c{internal::cos(angle)};
112 const auto n{internal::normalize(axis)};
113 const auto t{(1 - c) * n};
114 const auto s{internal::sin(angle)};
115 internal::matrix<std::common_type_t<T, U, V>, 4, 4> res;
116 res[0] = m[0] * internal::mad(t.x(), n.x(), c) + m[1] * (t.x() * n.y() + s * n.z()) + m[2] * (t.x() * n.z() - s * n.y());
117 res[1] = m[0] * (t.y() * n.x() - s * n.z()) + m[1] * internal::mad(t.y(), n.y(), c) + m[2] * (t.y() * n.z() + s * n.x());
118 res[2] = m[0] * (t.z() * n.x() + s * n.y()) + m[1] * (t.z() * n.y() - s * n.x()) + m[2] * internal::mad(t.z(), n.z(), c);
119 res[3] = m[3];
120 return res;
121}
122
123/**
124 * @brief Scales a 3x3 transformation matrix using a 2D vector
125 * @param m 3x3 transformation matrix to be scaled
126 * @param v 2D scaling vector
127 * @return Scaled 3x3 transformation matrix
128 */
129template <typename T, typename U, std::size_t M, std::size_t N>
130[[nodiscard]]
131constexpr auto scale(const internal::matrix<T, 3, 3> &m, const internal::matrix<U, M, N> &v) noexcept
132 -> internal::matrix<std::common_type_t<T, U>, 3, 3>
133 requires (M * N == 2)
134{
135 internal::matrix<std::common_type_t<T, U>, 3, 3> res;
136 res[0] = m[0] * v.x();
137 res[1] = m[1] * v.y();
138 res[2] = m[2];
139 return res;
140}
141
142/**
143 * @brief Scales a 4x4 transformation matrix using a 3D vector
144 * @param m 4x4 transformation matrix to be scaled
145 * @param v 3D scaling vector
146 * @return Scaled 4x4 transformation matrix
147 */
148template <typename T, typename U, std::size_t M, std::size_t N>
149[[nodiscard]]
150constexpr auto scale(const internal::matrix<T, 4, 4> &m, const internal::matrix<U, M, N> &v) noexcept
151 -> internal::matrix<std::common_type_t<T, U>, 4, 4>
152 requires (M * N == 3)
153{
154 internal::matrix<std::common_type_t<T, U>, 4, 4> res;
155 res[0] = m[0] * v.x();
156 res[1] = m[1] * v.y();
157 res[2] = m[2] * v.z();
158 res[3] = m[3];
159 return res;
160}
161
162/**
163 * @brief Constructs a 4x4 perspective matrix
164 * @param fovy Vertical field of view of the camera
165 * @param aspect Aspect ratio of the viewport
166 * @param near Distance of the near clipping plane
167 * @return Constructed 4x4 perspective matrix
168 */
169template <typename T, typename U, typename V>
170[[nodiscard]]
171constexpr auto perspective(const T fovy, const U aspect, const V near) noexcept
172 requires (std::is_arithmetic_v<T> && std::is_arithmetic_v<U> && std::is_arithmetic_v<V>)
173{
174 const auto f{internal::rcp(internal::tan(fovy / 2))};
175
176 return internal::matrix<std::common_type_t<T, U, V>, 4, 4>{
177 f / aspect, 0, 0, 0,
178 0, f, 0, 0,
179 0, 0, -1, -1,
180 0, 0, -near, 0
181 };
182}
183}
184
185#endif
Linear algebra utilities.
Definition intrinsics.hpp:42
constexpr auto scale(const internal::matrix< T, 3, 3 > &m, const internal::matrix< U, M, N > &v) noexcept -> internal::matrix< std::common_type_t< T, U >, 3, 3 >
Scales a 3x3 transformation matrix using a 2D vector.
Definition transformations.hpp:131
constexpr auto perspective(const T fovy, const U aspect, const V near) noexcept
Constructs a 4x4 perspective matrix.
Definition transformations.hpp:171
constexpr auto translate(const internal::matrix< T, 3, 3 > &m, const internal::matrix< U, M, N > &v) noexcept -> internal::matrix< std::common_type_t< T, U >, 3, 3 >
Translates a 3x3 transformation matrix using a 2D vector.
Definition transformations.hpp:46
constexpr auto rotate(const internal::matrix< T, 3, 3 > &m, const U angle) noexcept -> internal::matrix< std::common_type_t< T, U >, 3, 3 >
Rotates a 3x3 transformation matrix using radians.
Definition transformations.hpp:85