Loading...
Searching...
No Matches
Matrix.h
Go to the documentation of this file.
1/**************************************************************************************************
2 * This file is a part of Ultralight. *
3 * *
4 * See <https://ultralig.ht> for licensing and more. *
5 * *
6 * (C) 2024 Ultralight, Inc. *
7 **************************************************************************************************/
8#pragma once
10#include <Ultralight/RefPtr.h>
11#include <Ultralight/Geometry.h>
12
13namespace ultralight {
14
15///
16/// 4x4 Matrix Helper
17///
19 ///
20 /// Raw 4x4 matrix as an array in column-major order.
21 ///
22 float data[16];
23
24 ///
25 /// Set to identity matrix.
26 ///
28};
29
30///
31/// Transformation Matrix helper
32///
33struct UExport alignas(16) Matrix {
34 typedef double Aligned4x4[4][4];
35
36 ///
37 /// Raw matrix data in column-major order (eg, `data[column][row]`)
38 ///
39 Aligned4x4 data;
40
41 ///
42 /// Set to identity matrix.
43 ///
45
46 ///
47 /// Set to an orthographic projection matrix suitable for use with our
48 /// vertex shaders. Optionally flip the y-coordinate space (eg, for OpenGL).
49 ///
50 void SetOrthographicProjection(double screen_width, double screen_height,
51 bool flip_y);
52
53 ///
54 /// Set to another matrix.
55 ///
56 void Set(const Matrix& other);
57
58 ///
59 /// Set to another matrix.
60 ///
61 void Set(const Matrix4x4& other);
62
63 ///
64 /// Set from raw affine members.
65 ///
66 void Set(double a, double b, double c, double d, double e, double f);
67
68 ///
69 /// Set from raw 4x4 components.
70 ///
71 void Set(double m11, double m12, double m13, double m14,
72 double m21, double m22, double m23, double m24,
73 double m31, double m32, double m33, double m34,
74 double m41, double m42, double m43, double m44);
75
76 inline double m11() const { return data[0][0]; }
77 inline double m12() const { return data[0][1]; }
78 inline double m13() const { return data[0][2]; }
79 inline double m14() const { return data[0][3]; }
80 inline double m21() const { return data[1][0]; }
81 inline double m22() const { return data[1][1]; }
82 inline double m23() const { return data[1][2]; }
83 inline double m24() const { return data[1][3]; }
84 inline double m31() const { return data[2][0]; }
85 inline double m32() const { return data[2][1]; }
86 inline double m33() const { return data[2][2]; }
87 inline double m34() const { return data[2][3]; }
88 inline double m41() const { return data[3][0]; }
89 inline double m42() const { return data[3][1]; }
90 inline double m43() const { return data[3][2]; }
91 inline double m44() const { return data[3][3]; }
92
93 inline double a() const { return data[0][0]; }
94 inline double b() const { return data[0][1]; }
95 inline double c() const { return data[1][0]; }
96 inline double d() const { return data[1][1]; }
97 inline double e() const { return data[3][0]; }
98 inline double f() const { return data[3][1]; }
99
100 ///
101 /// Whether or not this is an identity matrix.
102 ///
103 bool IsIdentity() const;
104
105 ///
106 /// Whether or not this is an identity matrix or translation.
107 ///
109
110 ///
111 /// Whether or not this matrix uses only affine transformations.
112 ///
113 bool IsAffine() const;
114
115 ///
116 /// Whether or not this is an identity, translation, or non-negative
117 /// uniform scale.
118 ///
119 bool IsSimple() const;
120
121 ///
122 /// Translate by x and y.
123 ///
124 void Translate(double x, double y);
125
126 ///
127 /// Scale by x and y.
128 ///
129 void Scale(double x, double y);
130
131 ///
132 /// Rotate matrix by theta (in degrees)
133 ///
134 void Rotate(double theta);
135
136 ///
137 /// Rotate matrix by x and y
138 ///
139 void Rotate(double x, double y);
140
141 ///
142 /// Transform (multiply) by another Matrix
143 ///
144 void Transform(const Matrix& other);
145
146 ///
147 /// Get the inverse of this matrix. May return false if not invertible.
148 ///
149 bool GetInverse(Matrix& result) const;
150
151 ///
152 /// Transform point by this matrix and get the result.
153 ///
154 Point Apply(const Point& p) const;
155
156 ///
157 /// Transform rect by this matrix and get the result as an axis-aligned rect.
158 ///
159 Rect Apply(const Rect& r) const;
160
161 ///
162 /// Get an integer hash of this matrix's members.
163 ///
164 uint32_t Hash() const;
165
166 ///
167 /// Get this matrix as unaligned 4x4 float components (for use passing to
168 /// GPU driver APIs).
169 ///
171};
172
173bool UExport operator==(const Matrix& a, const Matrix& b);
174bool UExport operator!=(const Matrix& a, const Matrix& b);
175
176bool UExport operator==(const Matrix4x4& a, const Matrix4x4& b);
177bool UExport operator!=(const Matrix4x4& a, const Matrix4x4& b);
178
179} // namespace ultralight
#define UExport
Definition Exports.h:25
Definition App.h:14
bool operator==(const Matrix &a, const Matrix &b)
bool operator!=(const Matrix &a, const Matrix &b)
4x4 Matrix Helper
Definition Matrix.h:18
void SetIdentity()
Set to identity matrix.
Transformation Matrix helper.
Definition Matrix.h:33
void Translate(double x, double y)
Translate by x and y.
double e() const
Definition Matrix.h:97
bool IsAffine() const
Whether or not this matrix uses only affine transformations.
bool IsIdentity() const
Whether or not this is an identity matrix.
Point Apply(const Point &p) const
Transform point by this matrix and get the result.
void SetIdentity()
Set to identity matrix.
double m42() const
Definition Matrix.h:89
uint32_t Hash() const
Get an integer hash of this matrix's members.
Aligned4x4 data
Raw matrix data in column-major order (eg, data[column][row])
Definition Matrix.h:39
double b() const
Definition Matrix.h:94
double m32() const
Definition Matrix.h:85
bool IsIdentityOrTranslation() const
Whether or not this is an identity matrix or translation.
double m23() const
Definition Matrix.h:82
bool IsSimple() const
Whether or not this is an identity, translation, or non-negative uniform scale.
void Set(double a, double b, double c, double d, double e, double f)
Set from raw affine members.
void Rotate(double theta)
Rotate matrix by theta (in degrees)
double m21() const
Definition Matrix.h:80
void Set(double m11, double m12, double m13, double m14, double m21, double m22, double m23, double m24, double m31, double m32, double m33, double m34, double m41, double m42, double m43, double m44)
Set from raw 4x4 components.
double m12() const
Definition Matrix.h:77
void Set(const Matrix &other)
Set to another matrix.
Rect Apply(const Rect &r) const
Transform rect by this matrix and get the result as an axis-aligned rect.
double a() const
Definition Matrix.h:93
double m14() const
Definition Matrix.h:79
double m34() const
Definition Matrix.h:87
bool GetInverse(Matrix &result) const
Get the inverse of this matrix.
double m43() const
Definition Matrix.h:90
void Rotate(double x, double y)
Rotate matrix by x and y.
double m41() const
Definition Matrix.h:88
double m44() const
Definition Matrix.h:91
void SetOrthographicProjection(double screen_width, double screen_height, bool flip_y)
Set to an orthographic projection matrix suitable for use with our vertex shaders.
void Set(const Matrix4x4 &other)
Set to another matrix.
double m11() const
Definition Matrix.h:76
double m24() const
Definition Matrix.h:83
Matrix4x4 GetMatrix4x4() const
Get this matrix as unaligned 4x4 float components (for use passing to GPU driver APIs).
double m13() const
Definition Matrix.h:78
double m22() const
Definition Matrix.h:81
void Scale(double x, double y)
Scale by x and y.
double c() const
Definition Matrix.h:95
double d() const
Definition Matrix.h:96
void Transform(const Matrix &other)
Transform (multiply) by another Matrix.
double f() const
Definition Matrix.h:98
double m33() const
Definition Matrix.h:86
double m31() const
Definition Matrix.h:84
Float Rectangle Helper.
Definition Geometry.h:419
2D Vector Helper
Definition Geometry.h:20