24#ifndef STELLARLIB_LIN_TRANSFORMATIONS_HPP
25#define STELLARLIB_LIN_TRANSFORMATIONS_HPP
27#include <stellarlib/lin/intrinsics.hpp>
28#include <stellarlib/lin/matrix.hpp>
44template <
typename T,
typename U, std::
size_t M, std::
size_t N>
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>
50 internal::matrix<std::common_type_t<T, U>, 3, 3> res;
53 res[2] = m[0] * v.x() + m[1] * v.y() + m[2];
63template <
typename T,
typename U, std::
size_t M, std::
size_t N>
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>
69 internal::matrix<std::common_type_t<T, U>, 4, 4> res;
73 res[3] = m[0] * v.x() + m[1] * v.y() + m[2] * v.z() + m[3];
83template <
typename T,
typename U>
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>)
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;
105template <
typename T,
typename U,
typename V, std::
size_t M, std::
size_t N>
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)
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);
129template <
typename T,
typename U, std::
size_t M, std::
size_t N>
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)
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();
148template <
typename T,
typename U, std::
size_t M, std::
size_t N>
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)
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();
169template <
typename T,
typename U,
typename V>
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>)
174 const auto f{internal::rcp(internal::tan(fovy / 2))};
176 return internal::matrix<std::common_type_t<T, U, V>, 4, 4>{
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