Move Vectormath main
C++20 game and graphics mathematics
Loading...
Searching...
No Matches
AffineTransform3.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <optional>
4#include <type_traits>
5
6#include <mv/math/Mat3.hpp>
7#include <mv/math/Vec3.hpp>
8
9namespace mv::math
10{
11 // Standard affine point/vector transform; storage and semantic separation
12 // are Move-specific.
13 template <typename T>
14 requires std::is_floating_point_v<T>
16 {
17 public:
18 using Component = T;
19
21 AxisX_(T(1), T(0), T(0)),
22 AxisY_(T(0), T(1), T(0)),
23 AxisZ_(T(0), T(0), T(1)),
24 Translation_(T(0), T(0), T(0))
25 {
26 }
27
29 const Vec3<T>& axisY,
30 const Vec3<T>& axisZ,
32 AxisX_(axisX),
33 AxisY_(axisY),
34 AxisZ_(axisZ),
35 Translation_(translation)
36 {
37 }
38
40 {
41 return AffineTransform3();
42 }
43
45 const Mat3<T>& linear, const Vec3<T>& translation) noexcept
46 {
47 return AffineTransform3(linear.Row(0U), linear.Row(1U),
48 linear.Row(2U), translation);
49 }
50
52 {
53 return AxisX_;
54 }
56 {
57 return AxisY_;
58 }
60 {
61 return AxisZ_;
62 }
64 {
65 return Translation_;
66 }
67
69 {
70 return Mat3<T>(AxisX_, AxisY_, AxisZ_);
71 }
72
73 [[nodiscard]] std::optional<AffineTransform3> TryInverse()
75 {
76 const auto inverseLinear = Linear().TryInverse();
77 if (!inverseLinear)
78 {
79 return std::nullopt;
80 }
82 *inverseLinear, (Translation_ * *inverseLinear) * T(-1));
83 }
84
85 [[nodiscard]] friend bool operator==(
87 const AffineTransform3& right) noexcept
88 {
89 return left.AxisX_ == right.AxisX_ && left.AxisY_ == right.AxisY_ &&
90 left.AxisZ_ == right.AxisZ_ &&
91 left.Translation_ == right.Translation_;
92 }
93
94 private:
95 Vec3<T> AxisX_;
96 Vec3<T> AxisY_;
97 Vec3<T> AxisZ_;
98 Vec3<T> Translation_;
99 };
100
103
104 template <typename T>
105 requires std::is_floating_point_v<T>
107 const AffineTransform3<T>& transform, const Vec3<T>& vector) noexcept
108 {
109 return transform.AxisX() * vector.X() + transform.AxisY() * vector.Y() +
110 transform.AxisZ() * vector.Z();
111 }
112
113 template <typename T>
114 requires std::is_floating_point_v<T>
116 const AffineTransform3<T>& transform, const Vec3<T>& point) noexcept
117 {
118 return TransformVector(transform, point) + transform.Translation();
119 }
120
121 template <typename T>
122 requires std::is_floating_point_v<T>
125 const AffineTransform3<T>& second) noexcept
126 {
127 // Row-vector affine composition: apply first, then second. The linear
128 // and translation equations are cross-checked against RTM 2.3.1
129 // qvv_mul/matrix_mul conventions (MIT).
131 first.Linear() * second.Linear(),
132 TransformVector(second, first.Translation()) +
133 second.Translation());
134 }
135} // namespace mv::math
136
137static_assert(sizeof(mv::math::AffineTransform3f) == 64);
138static_assert(alignof(mv::math::AffineTransform3f) == 16);
139static_assert(std::is_trivially_copyable_v<mv::math::AffineTransform3f>);
140static_assert(sizeof(mv::math::AffineTransform3d) == 128);
141static_assert(alignof(mv::math::AffineTransform3d) == 32);
142static_assert(std::is_trivially_copyable_v<mv::math::AffineTransform3d>);
static AffineTransform3 FromLinearTranslation(const Mat3< T > &linear, const Vec3< T > &translation) noexcept
std::optional< AffineTransform3 > TryInverse() const noexcept
const Vec3< T > & AxisY() const noexcept
Mat3< T > Linear() const noexcept
const Vec3< T > & AxisZ() const noexcept
const Vec3< T > & AxisX() const noexcept
AffineTransform3(const Vec3< T > &axisX, const Vec3< T > &axisY, const Vec3< T > &axisZ, const Vec3< T > &translation) noexcept
friend bool operator==(const AffineTransform3 &left, const AffineTransform3 &right) noexcept
static AffineTransform3 Identity() noexcept
const Vec3< T > & Translation() const noexcept
Vec3< T > TransformVector(const AffineTransform3< T > &transform, const Vec3< T > &vector) noexcept
AffineTransform3< float > AffineTransform3f
AffineTransform3< T > Compose(const AffineTransform3< T > &first, const AffineTransform3< T > &second) noexcept
Vec3< T > TransformPoint(const AffineTransform3< T > &transform, const Vec3< T > &point) noexcept
AffineTransform3< double > AffineTransform3d