2020-04-18 10:22:42 +00:00
|
|
|
/*
|
2021-05-29 10:38:28 +00:00
|
|
|
* Copyright (c) 2020, Stephan Unverwerth <s.unverwerth@serenityos.org>
|
2020-04-18 10:22:42 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-18 10:22:42 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-07-17 16:29:28 +00:00
|
|
|
#include <AK/Math.h>
|
2023-11-17 22:57:21 +00:00
|
|
|
#include <LibGfx/AffineTransform.h>
|
2020-07-24 14:54:36 +00:00
|
|
|
#include <LibGfx/Matrix.h>
|
2020-04-18 10:22:42 +00:00
|
|
|
#include <LibGfx/Vector3.h>
|
2021-04-18 14:25:25 +00:00
|
|
|
#include <LibGfx/Vector4.h>
|
2020-04-18 10:22:42 +00:00
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
|
|
template<typename T>
|
2021-05-13 19:33:17 +00:00
|
|
|
using Matrix4x4 = Matrix<4, T>;
|
2020-04-18 10:22:42 +00:00
|
|
|
|
2021-05-13 19:33:17 +00:00
|
|
|
template<typename T>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr static Vector4<T> operator*(Matrix4x4<T> const& m, Vector4<T> const& v)
|
2021-05-13 19:33:17 +00:00
|
|
|
{
|
|
|
|
auto const& elements = m.elements();
|
|
|
|
return Vector4<T>(
|
|
|
|
v.x() * elements[0][0] + v.y() * elements[0][1] + v.z() * elements[0][2] + v.w() * elements[0][3],
|
|
|
|
v.x() * elements[1][0] + v.y() * elements[1][1] + v.z() * elements[1][2] + v.w() * elements[1][3],
|
|
|
|
v.x() * elements[2][0] + v.y() * elements[2][1] + v.z() * elements[2][2] + v.w() * elements[2][3],
|
|
|
|
v.x() * elements[3][0] + v.y() * elements[3][1] + v.z() * elements[3][2] + v.w() * elements[3][3]);
|
|
|
|
}
|
2020-04-18 10:22:42 +00:00
|
|
|
|
2022-03-23 11:00:33 +00:00
|
|
|
// FIXME: this is a specific Matrix4x4 * Vector3 interaction that implies W=1; maybe move this out of LibGfx
|
|
|
|
// or replace a Matrix4x4 * Vector4 operation?
|
2021-05-13 19:33:17 +00:00
|
|
|
template<typename T>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr static Vector3<T> transform_point(Matrix4x4<T> const& m, Vector3<T> const& p)
|
2021-05-13 19:33:17 +00:00
|
|
|
{
|
|
|
|
auto const& elements = m.elements();
|
|
|
|
return Vector3<T>(
|
|
|
|
p.x() * elements[0][0] + p.y() * elements[0][1] + p.z() * elements[0][2] + elements[0][3],
|
|
|
|
p.x() * elements[1][0] + p.y() * elements[1][1] + p.z() * elements[1][2] + elements[1][3],
|
|
|
|
p.x() * elements[2][0] + p.y() * elements[2][1] + p.z() * elements[2][2] + elements[2][3]);
|
|
|
|
}
|
2020-04-18 10:22:42 +00:00
|
|
|
|
2021-05-13 19:33:17 +00:00
|
|
|
template<typename T>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr static Matrix4x4<T> translation_matrix(Vector3<T> const& p)
|
2021-05-13 19:33:17 +00:00
|
|
|
{
|
|
|
|
return Matrix4x4<T>(
|
|
|
|
1, 0, 0, p.x(),
|
|
|
|
0, 1, 0, p.y(),
|
|
|
|
0, 0, 1, p.z(),
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
2020-04-18 10:22:42 +00:00
|
|
|
|
2021-05-13 19:33:17 +00:00
|
|
|
template<typename T>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr static Matrix4x4<T> scale_matrix(Vector3<T> const& s)
|
2021-05-13 19:33:17 +00:00
|
|
|
{
|
|
|
|
return Matrix4x4<T>(
|
|
|
|
s.x(), 0, 0, 0,
|
|
|
|
0, s.y(), 0, 0,
|
|
|
|
0, 0, s.z(), 0,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
2021-05-13 17:59:57 +00:00
|
|
|
|
2021-05-13 19:33:17 +00:00
|
|
|
template<typename T>
|
2022-04-01 17:58:27 +00:00
|
|
|
constexpr static Matrix4x4<T> rotation_matrix(Vector3<T> const& axis, T angle)
|
2021-05-13 19:33:17 +00:00
|
|
|
{
|
2022-02-03 11:48:17 +00:00
|
|
|
T c, s;
|
|
|
|
AK::sincos(angle, s, c);
|
2021-05-13 19:33:17 +00:00
|
|
|
T t = 1 - c;
|
|
|
|
T x = axis.x();
|
|
|
|
T y = axis.y();
|
|
|
|
T z = axis.z();
|
|
|
|
|
|
|
|
return Matrix4x4<T>(
|
|
|
|
t * x * x + c, t * x * y - z * s, t * x * z + y * s, 0,
|
|
|
|
t * x * y + z * s, t * y * y + c, t * y * z - x * s, 0,
|
|
|
|
t * x * z - y * s, t * y * z + x * s, t * z * z + c, 0,
|
|
|
|
0, 0, 0, 1);
|
|
|
|
}
|
2020-04-18 10:22:42 +00:00
|
|
|
|
2023-11-17 22:57:21 +00:00
|
|
|
template<typename T>
|
|
|
|
Gfx::AffineTransform extract_2d_affine_transform(Matrix4x4<T> const& matrix)
|
|
|
|
{
|
|
|
|
auto* m = matrix.elements();
|
|
|
|
return Gfx::AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][3], m[1][3]);
|
|
|
|
}
|
|
|
|
|
2020-04-18 10:22:42 +00:00
|
|
|
typedef Matrix4x4<float> FloatMatrix4x4;
|
|
|
|
typedef Matrix4x4<double> DoubleMatrix4x4;
|
|
|
|
}
|
|
|
|
|
|
|
|
using Gfx::DoubleMatrix4x4;
|
|
|
|
using Gfx::FloatMatrix4x4;
|
|
|
|
using Gfx::Matrix4x4;
|