mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-23 17:52:26 -05:00
LibGfx: Constexpr Matrices and Vectors
This commit is contained in:
parent
09f850728a
commit
1424c4651f
Notes:
sideshowbarker
2024-07-18 18:32:57 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/1424c4651f9 Pull-request: https://github.com/SerenityOS/serenity/pull/6375 Reviewed-by: https://github.com/IdanHo Reviewed-by: https://github.com/Quaker762 Reviewed-by: https://github.com/alimpfard Reviewed-by: https://github.com/ccapitalK Reviewed-by: https://github.com/gmta Reviewed-by: https://github.com/sunverwerth ✅
4 changed files with 61 additions and 61 deletions
|
@ -16,8 +16,8 @@ class Matrix {
|
|||
public:
|
||||
static constexpr size_t Size = N;
|
||||
|
||||
Matrix() = default;
|
||||
Matrix(std::initializer_list<T> elements)
|
||||
constexpr Matrix() = default;
|
||||
constexpr Matrix(std::initializer_list<T> elements)
|
||||
{
|
||||
VERIFY(elements.size() == N * N);
|
||||
size_t i = 0;
|
||||
|
@ -28,7 +28,7 @@ public:
|
|||
}
|
||||
|
||||
template<typename... Args>
|
||||
Matrix(Args... args)
|
||||
constexpr Matrix(Args... args)
|
||||
: Matrix({ (T)args... })
|
||||
{
|
||||
}
|
||||
|
@ -38,10 +38,10 @@ public:
|
|||
__builtin_memcpy(m_elements, other.elements(), sizeof(T) * N * N);
|
||||
}
|
||||
|
||||
auto elements() const { return m_elements; }
|
||||
auto elements() { return m_elements; }
|
||||
constexpr auto elements() const { return m_elements; }
|
||||
constexpr auto elements() { return m_elements; }
|
||||
|
||||
Matrix operator*(const Matrix& other) const
|
||||
constexpr Matrix operator*(const Matrix& other) const
|
||||
{
|
||||
Matrix product;
|
||||
for (int i = 0; i < N; ++i) {
|
||||
|
|
|
@ -16,8 +16,8 @@ namespace Gfx {
|
|||
template<typename T>
|
||||
class Matrix4x4 final : public Matrix<4, T> {
|
||||
public:
|
||||
Matrix4x4() = default;
|
||||
Matrix4x4(T _11, T _12, T _13, T _14,
|
||||
constexpr Matrix4x4() = default;
|
||||
constexpr Matrix4x4(T _11, T _12, T _13, T _14,
|
||||
T _21, T _22, T _23, T _24,
|
||||
T _31, T _32, T _33, T _34,
|
||||
T _41, T _42, T _43, T _44)
|
||||
|
@ -30,10 +30,10 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
auto elements() const { return m_elements; }
|
||||
auto elements() { return m_elements; }
|
||||
constexpr auto elements() const { return m_elements; }
|
||||
constexpr auto elements() { return m_elements; }
|
||||
|
||||
Matrix4x4 operator*(const Matrix4x4& other) const
|
||||
constexpr Matrix4x4 operator*(const Matrix4x4& other) const
|
||||
{
|
||||
Matrix4x4 product;
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
|
@ -47,7 +47,7 @@ public:
|
|||
return product;
|
||||
}
|
||||
|
||||
Vector4<T> operator*(const Vector4<T>& v) const
|
||||
constexpr Vector4<T> operator*(const Vector4<T>& v) const
|
||||
{
|
||||
return Vector4<T>(
|
||||
v.x() * m_elements[0][0] + v.y() * m_elements[1][0] + v.z() * m_elements[2][0] + v.w() * m_elements[3][0],
|
||||
|
@ -55,8 +55,8 @@ public:
|
|||
v.x() * m_elements[0][2] + v.y() * m_elements[1][2] + v.z() * m_elements[2][2] + v.w() * m_elements[3][2],
|
||||
v.x() * m_elements[0][3] + v.y() * m_elements[1][3] + v.z() * m_elements[2][3] + v.w() * m_elements[3][3]);
|
||||
}
|
||||
|
||||
Vector3<T> transform_point(const Vector3<T>& p) const
|
||||
|
||||
constexpr Vector3<T> transform_point(const Vector3<T>& p) const
|
||||
{
|
||||
return Vector3<T>(
|
||||
p.x() * m_elements[0][0] + p.y() * m_elements[1][0] + p.z() * m_elements[2][0] + m_elements[3][0],
|
||||
|
@ -64,7 +64,7 @@ public:
|
|||
p.x() * m_elements[0][2] + p.y() * m_elements[1][2] + p.z() * m_elements[2][2] + m_elements[3][2]);
|
||||
}
|
||||
|
||||
static Matrix4x4 identity()
|
||||
constexpr static Matrix4x4 identity()
|
||||
{
|
||||
return Matrix4x4(
|
||||
1, 0, 0, 0,
|
||||
|
@ -73,7 +73,7 @@ public:
|
|||
0, 0, 0, 1);
|
||||
}
|
||||
|
||||
static Matrix4x4 translate(const Vector3<T>& p)
|
||||
constexpr static Matrix4x4 translate(const Vector3<T>& p)
|
||||
{
|
||||
return Matrix4x4(
|
||||
1, 0, 0, 0,
|
||||
|
@ -82,7 +82,7 @@ public:
|
|||
p.x(), p.y(), p.z(), 1);
|
||||
}
|
||||
|
||||
static Matrix4x4 scale(const Vector3<T>& s)
|
||||
constexpr static Matrix4x4 scale(const Vector3<T>& s)
|
||||
{
|
||||
return Matrix4x4(
|
||||
s.x(), 0, 0, 0,
|
||||
|
@ -91,7 +91,7 @@ public:
|
|||
0, 0, 0, 1);
|
||||
}
|
||||
|
||||
static Matrix4x4 rotate(const Vector3<T>& axis, T angle)
|
||||
constexpr static Matrix4x4 rotate(const Vector3<T>& axis, T angle)
|
||||
{
|
||||
T c = cos(angle);
|
||||
T s = sin(angle);
|
||||
|
|
|
@ -12,23 +12,23 @@ namespace Gfx {
|
|||
template<typename T>
|
||||
class Vector3 final {
|
||||
public:
|
||||
Vector3() = default;
|
||||
Vector3(T x, T y, T z)
|
||||
constexpr Vector3() = default;
|
||||
constexpr Vector3(T x, T y, T z)
|
||||
: m_x(x)
|
||||
, m_y(y)
|
||||
, m_z(z)
|
||||
{
|
||||
}
|
||||
|
||||
T x() const { return m_x; }
|
||||
T y() const { return m_y; }
|
||||
T z() const { return m_z; }
|
||||
constexpr T x() const { return m_x; }
|
||||
constexpr T y() const { return m_y; }
|
||||
constexpr T z() const { return m_z; }
|
||||
|
||||
void set_x(T value) { m_x = value; }
|
||||
void set_y(T value) { m_y = value; }
|
||||
void set_z(T value) { m_z = value; }
|
||||
constexpr void set_x(T value) { m_x = value; }
|
||||
constexpr void set_y(T value) { m_y = value; }
|
||||
constexpr void set_z(T value) { m_z = value; }
|
||||
|
||||
Vector3& operator+=(const Vector3& other)
|
||||
constexpr Vector3& operator+=(const Vector3& other)
|
||||
{
|
||||
m_x += other.m_x;
|
||||
m_y += other.m_y;
|
||||
|
@ -36,7 +36,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
Vector3& operator-=(const Vector3& other)
|
||||
constexpr Vector3& operator-=(const Vector3& other)
|
||||
{
|
||||
m_x -= other.m_x;
|
||||
m_y -= other.m_y;
|
||||
|
@ -44,32 +44,32 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
Vector3 operator+(const Vector3& other) const
|
||||
constexpr Vector3 operator+(const Vector3& other) const
|
||||
{
|
||||
return Vector3(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z);
|
||||
}
|
||||
|
||||
Vector3 operator-(const Vector3& other) const
|
||||
constexpr Vector3 operator-(const Vector3& other) const
|
||||
{
|
||||
return Vector3(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z);
|
||||
}
|
||||
|
||||
Vector3 operator*(T f) const
|
||||
constexpr Vector3 operator*(T f) const
|
||||
{
|
||||
return Vector3(m_x * f, m_y * f, m_z * f);
|
||||
}
|
||||
|
||||
Vector3 operator/(T f) const
|
||||
constexpr Vector3 operator/(T f) const
|
||||
{
|
||||
return Vector3(m_x / f, m_y / f, m_z / f);
|
||||
}
|
||||
|
||||
T dot(const Vector3& other) const
|
||||
constexpr T dot(const Vector3& other) const
|
||||
{
|
||||
return m_x * other.m_x + m_y * other.m_y + m_z * other.m_z;
|
||||
}
|
||||
|
||||
Vector3 cross(const Vector3& other) const
|
||||
constexpr Vector3 cross(const Vector3& other) const
|
||||
{
|
||||
return Vector3(
|
||||
m_y * other.m_z - m_z * other.m_y,
|
||||
|
@ -77,20 +77,20 @@ public:
|
|||
m_x * other.m_y - m_y * other.m_x);
|
||||
}
|
||||
|
||||
Vector3 normalized() const
|
||||
constexpr Vector3 normalized() const
|
||||
{
|
||||
T inv_length = 1 / length();
|
||||
return *this * inv_length;
|
||||
}
|
||||
|
||||
Vector3 clamped(T m, T x) const
|
||||
constexpr Vector3 clamped(T m, T x) const
|
||||
{
|
||||
Vector3 copy { *this };
|
||||
copy.clamp(m, x);
|
||||
return copy;
|
||||
}
|
||||
|
||||
void clamp(T min_value, T max_value)
|
||||
constexpr void clamp(T min_value, T max_value)
|
||||
{
|
||||
m_x = max(min_value, m_x);
|
||||
m_y = max(min_value, m_y);
|
||||
|
@ -100,7 +100,7 @@ public:
|
|||
m_z = min(max_value, m_z);
|
||||
}
|
||||
|
||||
void normalize()
|
||||
constexpr void normalize()
|
||||
{
|
||||
T inv_length = 1 / length();
|
||||
m_x *= inv_length;
|
||||
|
@ -108,7 +108,7 @@ public:
|
|||
m_z *= inv_length;
|
||||
}
|
||||
|
||||
T length() const
|
||||
constexpr T length() const
|
||||
{
|
||||
return sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ namespace Gfx {
|
|||
template<typename T>
|
||||
class Vector4 final {
|
||||
public:
|
||||
Vector4() = default;
|
||||
Vector4(T x, T y, T z, T w)
|
||||
constexpr Vector4() = default;
|
||||
constexpr Vector4(T x, T y, T z, T w)
|
||||
: m_x(x)
|
||||
, m_y(y)
|
||||
, m_z(z)
|
||||
|
@ -21,17 +21,17 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
T x() const { return m_x; }
|
||||
T y() const { return m_y; }
|
||||
T z() const { return m_z; }
|
||||
T w() const { return m_w; }
|
||||
constexpr T x() const { return m_x; }
|
||||
constexpr T y() const { return m_y; }
|
||||
constexpr T z() const { return m_z; }
|
||||
constexpr T w() const { return m_w; }
|
||||
|
||||
void set_x(T value) { m_x = value; }
|
||||
void set_y(T value) { m_y = value; }
|
||||
void set_z(T value) { m_z = value; }
|
||||
void set_w(T value) { m_w = value; }
|
||||
constexpr void set_x(T value) { m_x = value; }
|
||||
constexpr void set_y(T value) { m_y = value; }
|
||||
constexpr void set_z(T value) { m_z = value; }
|
||||
constexpr void set_w(T value) { m_w = value; }
|
||||
|
||||
Vector4& operator+=(const Vector4& other)
|
||||
constexpr Vector4& operator+=(const Vector4& other)
|
||||
{
|
||||
m_x += other.m_x;
|
||||
m_y += other.m_y;
|
||||
|
@ -40,7 +40,7 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
Vector4& operator-=(const Vector4& other)
|
||||
constexpr Vector4& operator-=(const Vector4& other)
|
||||
{
|
||||
m_x -= other.m_x;
|
||||
m_y -= other.m_y;
|
||||
|
@ -49,45 +49,45 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
Vector4 operator+(const Vector4& other) const
|
||||
constexpr Vector4 operator+(const Vector4& other) const
|
||||
{
|
||||
return Vector4(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z, m_w + other.m_w);
|
||||
}
|
||||
|
||||
Vector4 operator-(const Vector4& other) const
|
||||
constexpr Vector4 operator-(const Vector4& other) const
|
||||
{
|
||||
return Vector4(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z, m_w - other.m_w);
|
||||
}
|
||||
|
||||
Vector4 operator*(T f) const
|
||||
constexpr Vector4 operator*(T f) const
|
||||
{
|
||||
return Vector4(m_x * f, m_y * f, m_z * f, m_w * f);
|
||||
}
|
||||
|
||||
Vector4 operator/(T f) const
|
||||
constexpr Vector4 operator/(T f) const
|
||||
{
|
||||
return Vector4(m_x / f, m_y / f, m_z / f, m_w / f);
|
||||
}
|
||||
|
||||
T dot(const Vector4& other) const
|
||||
constexpr T dot(const Vector4& other) const
|
||||
{
|
||||
return m_x * other.m_x + m_y * other.m_y + m_z * other.m_z + m_w * other.m_w;
|
||||
}
|
||||
|
||||
Vector4 normalized() const
|
||||
constexpr Vector4 normalized() const
|
||||
{
|
||||
T inv_length = 1 / length();
|
||||
return *this * inv_length;
|
||||
}
|
||||
|
||||
Vector4 clamped(T m, T x) const
|
||||
constexpr Vector4 clamped(T m, T x) const
|
||||
{
|
||||
Vector4 copy { *this };
|
||||
copy.clamp(m, x);
|
||||
return copy;
|
||||
}
|
||||
|
||||
void clamp(T min_value, T max_value)
|
||||
constexpr void clamp(T min_value, T max_value)
|
||||
{
|
||||
m_x = max(min_value, m_x);
|
||||
m_y = max(min_value, m_y);
|
||||
|
@ -99,7 +99,7 @@ public:
|
|||
m_w = min(max_value, m_w);
|
||||
}
|
||||
|
||||
void normalize()
|
||||
constexpr void normalize()
|
||||
{
|
||||
T inv_length = 1 / length();
|
||||
m_x *= inv_length;
|
||||
|
@ -108,7 +108,7 @@ public:
|
|||
m_w *= inv_length;
|
||||
}
|
||||
|
||||
T length() const
|
||||
constexpr T length() const
|
||||
{
|
||||
return sqrt(m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w);
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue