CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Transform.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
10#include "Angle.hpp"
11#include "Math.hpp"
12#include "Matrix.hpp"
13#include "Quaternion.hpp"
14#include "Vector.hpp"
15
17
19
20namespace CeresEngine::inline Math {
21
42
44 template<typename T>
45 static inline const constexpr TFacing<T> facingConvention{
46 .front = TVector3<T>(0.0, 0.0, -1.0),
47 .back = TVector3<T>(0.0, 0.0, 1.0),
48 .up = TVector3<T>(0.0, 1.0, 0.0),
49 .down = TVector3<T>(0.0, -1.0, 0.0),
50 .right = TVector3<T>(1.0, 0.0, 0.0),
51 .left = TVector3<T>(-1.0, 0.0, 0.0),
52 };
53
56
59
62 template<typename T> struct TTransform {
64 TVector3<T> position = TVector3<T>(0.0);
65
68
71
73 explicit constexpr TTransform() noexcept = default;
74
76 explicit constexpr TTransform(const TMatrix4<T>& matrix) noexcept {
77 TVector3<T> skew;
79 Math::decompose(matrix, scale, rotation, position, skew, perspective);
80// CE_ASSERT(Math::nearZero(skew.x, 1.0e-4));
81// CE_ASSERT(Math::nearZero(skew.y, 1.0e-4));
82// CE_ASSERT(Math::nearZero(skew.z, 1.0e-4));
83// CE_ASSERT(Math::nearZero(perspective.x, 1.0e-4));
84// CE_ASSERT(Math::nearZero(perspective.y, 1.0e-4));
85// CE_ASSERT(Math::nearZero(perspective.z, 1.0e-4));
86// CE_ASSERT(Math::approxEquals(perspective.w, 1.0, 1.0e-4));
87 }
88
90 constexpr TTransform(const TVector3<T>& position, const TQuaternion<T>& rotation, const TVector3<T>& scale)
91 : position(position), rotation(rotation), scale(scale) {}
92
93 constexpr TTransform(const TTransform&) noexcept = default;
94 constexpr TTransform& operator=(const TTransform&) noexcept = default;
95
97 [[nodiscard]] constexpr TMatrix4<T> getMatrix() const noexcept {
98// TMatrix4<T> matrix = identity<TMatrix4<T>>();
99
100 const Matrix4 translationMatrix = Math::translate(identity<TMatrix4<T>>(), position);
101 const Matrix4 rotationMatrix = TMatrix4<T>(rotation);
102 const Matrix4 scaleMatrix = Math::scale(identity<TMatrix4<T>>(), scale);
103
104 return translationMatrix * rotationMatrix * scaleMatrix;
105//
106// matrix = Math::translate(matrix, position);
107// matrix = matrix * TMatrix4<T>(rotation);
108// matrix = Math::scale(matrix, scale);
109//
110// return matrix;
111 }
112
114 [[nodiscard]] constexpr explicit operator TMatrix4<T>() const noexcept { return getMatrix(); }
115
117 [[nodiscard]] constexpr TVector4<T> transform(const TVector4<T>& input) const noexcept { return getMatrix() * input; }
118
120 [[nodiscard]] constexpr TVector4<T> operator()(const TVector4<T>& input) const noexcept { return transform(input); }
121
123 [[nodiscard]] constexpr TVector3<T> transform(const TVector3<T>& input) const noexcept { return TVector3<T>(transform(TVector4<T>(input, T(1)))); }
124
126 [[nodiscard]] constexpr TVector3<T> operator()(const TVector3<T>& input) const noexcept { return transform(input); }
127
128 public:
133 [[nodiscard]] constexpr TVector3<T> getDirection() const noexcept {
134 const TMatrix3<T> rotationMatrix = TMatrix3<T>(transpose(inverse(getMatrix())));
135 return normalize(rotationMatrix[2]);
136 }
137
139 [[nodiscard]] constexpr TFacing<T> getFacing() const noexcept {
140 TFacing<T> facing;
141 {
142 const TMatrix3<T> rotationMatrix = transpose(inverse(TMatrix3<T>(rotation)));
143
144 facing.front = rotationMatrix[2];
145 facing.back = -facing.front;
146
147 facing.up = rotationMatrix[1];
148 facing.down = -facing.up;
149
150 facing.right = rotationMatrix[0];
151 facing.left = -facing.right;
152 }
153 return facing;
154 }
155
157 [[nodiscard]] constexpr TVector3<T> getFront() const noexcept {
158 const TMatrix3<T> rotationMatrix = transpose(inverse(TMatrix3<T>(rotation)));
159 return normalize(rotationMatrix[2]);
160 }
161
163 [[nodiscard]] constexpr TVector3<T> getBack() const noexcept { return -getFront(); }
164
166 [[nodiscard]] constexpr TVector3<T> getUp() const noexcept {
167 const TMatrix3<T> rotationMatrix = transpose(inverse(TMatrix3<T>(rotation)));
168 return normalize(rotationMatrix[1]);
169 }
170
172 [[nodiscard]] constexpr TVector3<T> getDown() const noexcept { return -getUp(); }
173
175 [[nodiscard]] constexpr TVector3<T> getRight() const noexcept {
176 const TMatrix3<T> rotationMatrix = transpose(inverse(TMatrix3<T>(rotation)));
177 return normalize(rotationMatrix[0]);
178 }
179
181 [[nodiscard]] constexpr TVector3<T> getLeft() const noexcept { return -getRight(); }
182
183 public:
185 [[nodiscard]] constexpr TTransform moving(const TVector3<T>& distance) const noexcept { return TTransform(position + distance, rotation, scale); }
186
188 [[nodiscard]] constexpr TTransform moving(const TVector3<T>& direction, T distance) const noexcept { return moving(normalize(direction) * distance); }
189
191 [[nodiscard]] constexpr TTransform rotating(const TQuaternion<T>& quaternion) const noexcept {
192 return TTransform(position, quaternion * rotation, scale);
193 }
194
196 [[nodiscard]] constexpr TTransform rotating(const TVector3<T>& axis, const TRadian<T>& angle) const noexcept {
197 return rotating(angleAxis(angle.raw, axis));
198 }
199
201 [[nodiscard]] constexpr TTransform rolling(const TRadian<T>& angle) const noexcept { return rotating(Vector3(0.0, 0.0, 1.0), angle); }
202
204 [[nodiscard]] constexpr TTransform yawing(const TRadian<T>& angle) const noexcept { return rotating(Vector3(0.0, 1.0, 0.0), angle); }
205
207 [[nodiscard]] constexpr TTransform pitching(const TRadian<T>& angle) const noexcept { return rotating(Vector3(1.0, 0.0, 0.0), angle); }
208
210 [[nodiscard]] constexpr TTransform scaling(const TVector3<T>& factor) const noexcept { return TTransform(position, rotation, scale * factor); }
211
213 [[nodiscard]] constexpr TTransform scaling(T factor) const noexcept { return scaling(TVector3<T>(factor)); }
214
215 public:
217 [[nodiscard]] friend TVector3<T> operator*(const TTransform& transform, const TVector3<T>& input) noexcept { return transform(input); }
218
220 [[nodiscard]] friend TVector4<T> operator*(const TTransform& transform, const TVector4<T>& input) noexcept { return transform(input); }
221
222 [[nodiscard]] friend TTransform operator*(const TTransform& lhs, const TTransform& rhs) noexcept {
223 return TTransform(lhs.getMatrix() * rhs.getMatrix());
224 }
225
226 [[nodiscard]] friend TTransform inverse(const TTransform& transform) noexcept { return TTransform(inverse(transform.getMatrix())); }
227
228 [[nodiscard]] friend TTransform transpose(const TTransform& transform) noexcept { return TTransform(transpose(transform.getMatrix())); }
229 };
230
233
236
237} // namespace CeresEngine::inline Math
238
239template<typename T> struct std::hash<CeresEngine::Math::TTransform<T>> {
240 using O = CeresEngine::Math::TTransform<T>;
241 size_t operator()(const O& transform) const {
242 using CeresEngine::hash;
243 return hash(transform.position.x, transform.position.y, transform.position.z, transform.rotation.x, transform.rotation.y, transform.rotation.z,
244 transform.rotation.w, transform.scale.x, transform.scale.y, transform.scale.z);
245 }
246};
Definition Application.hpp:19
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Angle.hpp:20
TMatrix4< double > Matrix4
A 4x4 matrix matrix. Data is internally represented as double.
Definition Matrix.hpp:58
glm::qua< T, glm::highp > TQuaternion
A quaternion type that uses a internal representation of type T
Definition Quaternion.hpp:23
TVector3< double > Vector3
A three dimensional vector with (x, y, z) coordinates.
Definition Vector.hpp:84
TMatrix< 3, 3, T > TMatrix3
A 3x3 type that uses a internal representation of type T
Definition Matrix.hpp:41
TMatrix< 4, 4, T > TMatrix4
A 4x4 type that uses a internal representation of type T
Definition Matrix.hpp:54
T angle(const TVector2< T > &v1, const TVector2< T > &v2)
Definition Math.hpp:670
TVector< 4, T > TVector4
A four dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:115
constexpr TMatrix4< T > perspective(T fovy, T aspect, T zNear, T zFar) noexcept
Creates a matrix for a symetric perspective-view frustum based on the default handedness and default ...
Definition Math.hpp:170
TVector< 3, T > TVector3
A three dimensional vector type that uses a internal representation of type T
Definition Vector.hpp:80
A structure that describes orthogonal directions.
Definition Transform.hpp:23
TVector3< T > right
The right facing direction vector.
Definition Transform.hpp:37
TVector3< T > up
The up facing direction vector.
Definition Transform.hpp:31
TVector3< T > down
The down facing direction vector.
Definition Transform.hpp:34
TVector3< T > left
The left facing direction vector.
Definition Transform.hpp:40
TVector3< T > front
The front facing direction vector.
Definition Transform.hpp:25
TVector3< T > back
The back facing direction vector.
Definition Transform.hpp:28
Wrapper class which indicates a given angle value is in radians.
Definition Angle.hpp:125
A class that simplifies working with transformation matrices.
Definition Transform.hpp:62
constexpr TVector3< T > getLeft() const noexcept
The transform left facing direction vector.
Definition Transform.hpp:181
constexpr TVector4< T > transform(const TVector4< T > &input) const noexcept
Transforms the given point by the transform.
Definition Transform.hpp:117
constexpr TVector3< T > getBack() const noexcept
The transform back facing direction vector.
Definition Transform.hpp:163
constexpr TTransform yawing(const TRadian< T > &angle) const noexcept
Yaws the transform by angle radians.
Definition Transform.hpp:204
constexpr TTransform(const TTransform &) noexcept=default
friend TVector4< T > operator*(const TTransform &transform, const TVector4< T > &input) noexcept
Definition Transform.hpp:220
friend TTransform operator*(const TTransform &lhs, const TTransform &rhs) noexcept
Definition Transform.hpp:222
constexpr TTransform(const TVector3< T > &position, const TQuaternion< T > &rotation, const TVector3< T > &scale)
Creates a new transform from explicit values.
Definition Transform.hpp:90
friend TTransform inverse(const TTransform &transform) noexcept
Definition Transform.hpp:226
constexpr TVector3< T > transform(const TVector3< T > &input) const noexcept
Definition Transform.hpp:123
constexpr TVector4< T > operator()(const TVector4< T > &input) const noexcept
Definition Transform.hpp:120
constexpr TTransform rolling(const TRadian< T > &angle) const noexcept
Rolls the transform by angle radians.
Definition Transform.hpp:201
constexpr TVector3< T > getRight() const noexcept
The transform right facing direction vector.
Definition Transform.hpp:175
constexpr TVector3< T > getDirection() const noexcept
Computes the direction vector by applying the rotation to the base forward-facing vector.
Definition Transform.hpp:133
constexpr TTransform scaling(const TVector3< T > &factor) const noexcept
Scales the transform by the given factor.
Definition Transform.hpp:210
constexpr TTransform rotating(const TVector3< T > &axis, const TRadian< T > &angle) const noexcept
Rotates the transform angle radians on the given axis.
Definition Transform.hpp:196
friend TTransform transpose(const TTransform &transform) noexcept
Definition Transform.hpp:228
constexpr TTransform & operator=(const TTransform &) noexcept=default
constexpr TVector3< T > operator()(const TVector3< T > &input) const noexcept
Definition Transform.hpp:126
constexpr TMatrix4< T > getMatrix() const noexcept
Computes the transformation matrix.
Definition Transform.hpp:97
constexpr TTransform scaling(T factor) const noexcept
Scales the transform on all axes by the given factor.
Definition Transform.hpp:213
constexpr TVector3< T > getDown() const noexcept
The transform down facing direction vector.
Definition Transform.hpp:172
constexpr TTransform() noexcept=default
Creates a new default transform object.
constexpr TFacing< T > getFacing() const noexcept
Gets the facing vectors of the transform.
Definition Transform.hpp:139
constexpr TTransform moving(const TVector3< T > &distance) const noexcept
Moves the transform by the given distance.
Definition Transform.hpp:185
constexpr TVector3< T > getFront() const noexcept
The transform front facing direction vector.
Definition Transform.hpp:157
constexpr TTransform rotating(const TQuaternion< T > &quaternion) const noexcept
Rotates the transform by the given quaternion.
Definition Transform.hpp:191
constexpr TVector3< T > getUp() const noexcept
The transform up facing direction vector.
Definition Transform.hpp:166
friend TVector3< T > operator*(const TTransform &transform, const TVector3< T > &input) noexcept
Definition Transform.hpp:217
constexpr TTransform moving(const TVector3< T > &direction, T distance) const noexcept
Moves the transform by distance in the given direction.
Definition Transform.hpp:188
constexpr TTransform pitching(const TRadian< T > &angle) const noexcept
Pitches the transform by angle radians.
Definition Transform.hpp:207