Skip to content

Commit 1424c46

Browse files
Hendiadyoin1awesomekling
authored andcommitted
LibGfx: Constexpr Matrices and Vectors
1 parent 09f8507 commit 1424c46

File tree

4 files changed

+61
-61
lines changed

4 files changed

+61
-61
lines changed

Userland/Libraries/LibGfx/Matrix.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class Matrix {
1616
public:
1717
static constexpr size_t Size = N;
1818

19-
Matrix() = default;
20-
Matrix(std::initializer_list<T> elements)
19+
constexpr Matrix() = default;
20+
constexpr Matrix(std::initializer_list<T> elements)
2121
{
2222
VERIFY(elements.size() == N * N);
2323
size_t i = 0;
@@ -28,7 +28,7 @@ class Matrix {
2828
}
2929

3030
template<typename... Args>
31-
Matrix(Args... args)
31+
constexpr Matrix(Args... args)
3232
: Matrix({ (T)args... })
3333
{
3434
}
@@ -38,10 +38,10 @@ class Matrix {
3838
__builtin_memcpy(m_elements, other.elements(), sizeof(T) * N * N);
3939
}
4040

41-
auto elements() const { return m_elements; }
42-
auto elements() { return m_elements; }
41+
constexpr auto elements() const { return m_elements; }
42+
constexpr auto elements() { return m_elements; }
4343

44-
Matrix operator*(const Matrix& other) const
44+
constexpr Matrix operator*(const Matrix& other) const
4545
{
4646
Matrix product;
4747
for (int i = 0; i < N; ++i) {

Userland/Libraries/LibGfx/Matrix4x4.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ namespace Gfx {
1616
template<typename T>
1717
class Matrix4x4 final : public Matrix<4, T> {
1818
public:
19-
Matrix4x4() = default;
20-
Matrix4x4(T _11, T _12, T _13, T _14,
19+
constexpr Matrix4x4() = default;
20+
constexpr Matrix4x4(T _11, T _12, T _13, T _14,
2121
T _21, T _22, T _23, T _24,
2222
T _31, T _32, T _33, T _34,
2323
T _41, T _42, T _43, T _44)
@@ -30,10 +30,10 @@ class Matrix4x4 final : public Matrix<4, T> {
3030
{
3131
}
3232

33-
auto elements() const { return m_elements; }
34-
auto elements() { return m_elements; }
33+
constexpr auto elements() const { return m_elements; }
34+
constexpr auto elements() { return m_elements; }
3535

36-
Matrix4x4 operator*(const Matrix4x4& other) const
36+
constexpr Matrix4x4 operator*(const Matrix4x4& other) const
3737
{
3838
Matrix4x4 product;
3939
for (int i = 0; i < 4; ++i) {
@@ -47,24 +47,24 @@ class Matrix4x4 final : public Matrix<4, T> {
4747
return product;
4848
}
4949

50-
Vector4<T> operator*(const Vector4<T>& v) const
50+
constexpr Vector4<T> operator*(const Vector4<T>& v) const
5151
{
5252
return Vector4<T>(
5353
v.x() * m_elements[0][0] + v.y() * m_elements[1][0] + v.z() * m_elements[2][0] + v.w() * m_elements[3][0],
5454
v.x() * m_elements[0][1] + v.y() * m_elements[1][1] + v.z() * m_elements[2][1] + v.w() * m_elements[3][1],
5555
v.x() * m_elements[0][2] + v.y() * m_elements[1][2] + v.z() * m_elements[2][2] + v.w() * m_elements[3][2],
5656
v.x() * m_elements[0][3] + v.y() * m_elements[1][3] + v.z() * m_elements[2][3] + v.w() * m_elements[3][3]);
5757
}
58-
59-
Vector3<T> transform_point(const Vector3<T>& p) const
58+
59+
constexpr Vector3<T> transform_point(const Vector3<T>& p) const
6060
{
6161
return Vector3<T>(
6262
p.x() * m_elements[0][0] + p.y() * m_elements[1][0] + p.z() * m_elements[2][0] + m_elements[3][0],
6363
p.x() * m_elements[0][1] + p.y() * m_elements[1][1] + p.z() * m_elements[2][1] + m_elements[3][1],
6464
p.x() * m_elements[0][2] + p.y() * m_elements[1][2] + p.z() * m_elements[2][2] + m_elements[3][2]);
6565
}
6666

67-
static Matrix4x4 identity()
67+
constexpr static Matrix4x4 identity()
6868
{
6969
return Matrix4x4(
7070
1, 0, 0, 0,
@@ -73,7 +73,7 @@ class Matrix4x4 final : public Matrix<4, T> {
7373
0, 0, 0, 1);
7474
}
7575

76-
static Matrix4x4 translate(const Vector3<T>& p)
76+
constexpr static Matrix4x4 translate(const Vector3<T>& p)
7777
{
7878
return Matrix4x4(
7979
1, 0, 0, 0,
@@ -82,7 +82,7 @@ class Matrix4x4 final : public Matrix<4, T> {
8282
p.x(), p.y(), p.z(), 1);
8383
}
8484

85-
static Matrix4x4 scale(const Vector3<T>& s)
85+
constexpr static Matrix4x4 scale(const Vector3<T>& s)
8686
{
8787
return Matrix4x4(
8888
s.x(), 0, 0, 0,
@@ -91,7 +91,7 @@ class Matrix4x4 final : public Matrix<4, T> {
9191
0, 0, 0, 1);
9292
}
9393

94-
static Matrix4x4 rotate(const Vector3<T>& axis, T angle)
94+
constexpr static Matrix4x4 rotate(const Vector3<T>& axis, T angle)
9595
{
9696
T c = cos(angle);
9797
T s = sin(angle);

Userland/Libraries/LibGfx/Vector3.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,85 +12,85 @@ namespace Gfx {
1212
template<typename T>
1313
class Vector3 final {
1414
public:
15-
Vector3() = default;
16-
Vector3(T x, T y, T z)
15+
constexpr Vector3() = default;
16+
constexpr Vector3(T x, T y, T z)
1717
: m_x(x)
1818
, m_y(y)
1919
, m_z(z)
2020
{
2121
}
2222

23-
T x() const { return m_x; }
24-
T y() const { return m_y; }
25-
T z() const { return m_z; }
23+
constexpr T x() const { return m_x; }
24+
constexpr T y() const { return m_y; }
25+
constexpr T z() const { return m_z; }
2626

27-
void set_x(T value) { m_x = value; }
28-
void set_y(T value) { m_y = value; }
29-
void set_z(T value) { m_z = value; }
27+
constexpr void set_x(T value) { m_x = value; }
28+
constexpr void set_y(T value) { m_y = value; }
29+
constexpr void set_z(T value) { m_z = value; }
3030

31-
Vector3& operator+=(const Vector3& other)
31+
constexpr Vector3& operator+=(const Vector3& other)
3232
{
3333
m_x += other.m_x;
3434
m_y += other.m_y;
3535
m_z += other.m_z;
3636
return *this;
3737
}
3838

39-
Vector3& operator-=(const Vector3& other)
39+
constexpr Vector3& operator-=(const Vector3& other)
4040
{
4141
m_x -= other.m_x;
4242
m_y -= other.m_y;
4343
m_z -= other.m_z;
4444
return *this;
4545
}
4646

47-
Vector3 operator+(const Vector3& other) const
47+
constexpr Vector3 operator+(const Vector3& other) const
4848
{
4949
return Vector3(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z);
5050
}
5151

52-
Vector3 operator-(const Vector3& other) const
52+
constexpr Vector3 operator-(const Vector3& other) const
5353
{
5454
return Vector3(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z);
5555
}
5656

57-
Vector3 operator*(T f) const
57+
constexpr Vector3 operator*(T f) const
5858
{
5959
return Vector3(m_x * f, m_y * f, m_z * f);
6060
}
6161

62-
Vector3 operator/(T f) const
62+
constexpr Vector3 operator/(T f) const
6363
{
6464
return Vector3(m_x / f, m_y / f, m_z / f);
6565
}
6666

67-
T dot(const Vector3& other) const
67+
constexpr T dot(const Vector3& other) const
6868
{
6969
return m_x * other.m_x + m_y * other.m_y + m_z * other.m_z;
7070
}
7171

72-
Vector3 cross(const Vector3& other) const
72+
constexpr Vector3 cross(const Vector3& other) const
7373
{
7474
return Vector3(
7575
m_y * other.m_z - m_z * other.m_y,
7676
m_z * other.m_x - m_x * other.m_z,
7777
m_x * other.m_y - m_y * other.m_x);
7878
}
7979

80-
Vector3 normalized() const
80+
constexpr Vector3 normalized() const
8181
{
8282
T inv_length = 1 / length();
8383
return *this * inv_length;
8484
}
8585

86-
Vector3 clamped(T m, T x) const
86+
constexpr Vector3 clamped(T m, T x) const
8787
{
8888
Vector3 copy { *this };
8989
copy.clamp(m, x);
9090
return copy;
9191
}
9292

93-
void clamp(T min_value, T max_value)
93+
constexpr void clamp(T min_value, T max_value)
9494
{
9595
m_x = max(min_value, m_x);
9696
m_y = max(min_value, m_y);
@@ -100,15 +100,15 @@ class Vector3 final {
100100
m_z = min(max_value, m_z);
101101
}
102102

103-
void normalize()
103+
constexpr void normalize()
104104
{
105105
T inv_length = 1 / length();
106106
m_x *= inv_length;
107107
m_y *= inv_length;
108108
m_z *= inv_length;
109109
}
110110

111-
T length() const
111+
constexpr T length() const
112112
{
113113
return sqrt(m_x * m_x + m_y * m_y + m_z * m_z);
114114
}

Userland/Libraries/LibGfx/Vector4.h

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ namespace Gfx {
1212
template<typename T>
1313
class Vector4 final {
1414
public:
15-
Vector4() = default;
16-
Vector4(T x, T y, T z, T w)
15+
constexpr Vector4() = default;
16+
constexpr Vector4(T x, T y, T z, T w)
1717
: m_x(x)
1818
, m_y(y)
1919
, m_z(z)
2020
, m_w(w)
2121
{
2222
}
2323

24-
T x() const { return m_x; }
25-
T y() const { return m_y; }
26-
T z() const { return m_z; }
27-
T w() const { return m_w; }
24+
constexpr T x() const { return m_x; }
25+
constexpr T y() const { return m_y; }
26+
constexpr T z() const { return m_z; }
27+
constexpr T w() const { return m_w; }
2828

29-
void set_x(T value) { m_x = value; }
30-
void set_y(T value) { m_y = value; }
31-
void set_z(T value) { m_z = value; }
32-
void set_w(T value) { m_w = value; }
29+
constexpr void set_x(T value) { m_x = value; }
30+
constexpr void set_y(T value) { m_y = value; }
31+
constexpr void set_z(T value) { m_z = value; }
32+
constexpr void set_w(T value) { m_w = value; }
3333

34-
Vector4& operator+=(const Vector4& other)
34+
constexpr Vector4& operator+=(const Vector4& other)
3535
{
3636
m_x += other.m_x;
3737
m_y += other.m_y;
@@ -40,7 +40,7 @@ class Vector4 final {
4040
return *this;
4141
}
4242

43-
Vector4& operator-=(const Vector4& other)
43+
constexpr Vector4& operator-=(const Vector4& other)
4444
{
4545
m_x -= other.m_x;
4646
m_y -= other.m_y;
@@ -49,45 +49,45 @@ class Vector4 final {
4949
return *this;
5050
}
5151

52-
Vector4 operator+(const Vector4& other) const
52+
constexpr Vector4 operator+(const Vector4& other) const
5353
{
5454
return Vector4(m_x + other.m_x, m_y + other.m_y, m_z + other.m_z, m_w + other.m_w);
5555
}
5656

57-
Vector4 operator-(const Vector4& other) const
57+
constexpr Vector4 operator-(const Vector4& other) const
5858
{
5959
return Vector4(m_x - other.m_x, m_y - other.m_y, m_z - other.m_z, m_w - other.m_w);
6060
}
6161

62-
Vector4 operator*(T f) const
62+
constexpr Vector4 operator*(T f) const
6363
{
6464
return Vector4(m_x * f, m_y * f, m_z * f, m_w * f);
6565
}
6666

67-
Vector4 operator/(T f) const
67+
constexpr Vector4 operator/(T f) const
6868
{
6969
return Vector4(m_x / f, m_y / f, m_z / f, m_w / f);
7070
}
7171

72-
T dot(const Vector4& other) const
72+
constexpr T dot(const Vector4& other) const
7373
{
7474
return m_x * other.m_x + m_y * other.m_y + m_z * other.m_z + m_w * other.m_w;
7575
}
7676

77-
Vector4 normalized() const
77+
constexpr Vector4 normalized() const
7878
{
7979
T inv_length = 1 / length();
8080
return *this * inv_length;
8181
}
8282

83-
Vector4 clamped(T m, T x) const
83+
constexpr Vector4 clamped(T m, T x) const
8484
{
8585
Vector4 copy { *this };
8686
copy.clamp(m, x);
8787
return copy;
8888
}
8989

90-
void clamp(T min_value, T max_value)
90+
constexpr void clamp(T min_value, T max_value)
9191
{
9292
m_x = max(min_value, m_x);
9393
m_y = max(min_value, m_y);
@@ -99,7 +99,7 @@ class Vector4 final {
9999
m_w = min(max_value, m_w);
100100
}
101101

102-
void normalize()
102+
constexpr void normalize()
103103
{
104104
T inv_length = 1 / length();
105105
m_x *= inv_length;
@@ -108,7 +108,7 @@ class Vector4 final {
108108
m_w *= inv_length;
109109
}
110110

111-
T length() const
111+
constexpr T length() const
112112
{
113113
return sqrt(m_x * m_x + m_y * m_y + m_z * m_z + m_w * m_w);
114114
}

0 commit comments

Comments
 (0)