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