1 #ifndef _RIVE_MAT2D_HPP_
2 #define _RIVE_MAT2D_HPP_
4 #include "rive/math/vec2d.hpp"
8 class TransformComponents;
14 Mat2D() : m_Buffer{1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f} {}
15 Mat2D(const Mat2D& copy) = default;
16 Mat2D(float x1, float y1, float x2, float y2, float tx, float ty) :
17 m_Buffer{x1, y1, x2, y2, tx, ty} {}
19 inline const float* values() const { return m_Buffer; }
21 float& operator[](std::size_t idx) { return m_Buffer[idx]; }
22 const float& operator[](std::size_t idx) const { return m_Buffer[idx]; }
24 static Mat2D fromRotation(float rad);
25 static Mat2D fromScale(float sx, float sy) { return {sx, 0, 0, sy, 0, 0}; }
26 static Mat2D fromTranslate(float tx, float ty) { return {1, 0, 0, 1, tx, ty}; }
28 void scaleByValues(float sx, float sy);
30 Mat2D& operator*=(const Mat2D& rhs) {
31 *this = Mat2D::multiply(*this, rhs);
35 // If returns true, result holds the inverse.
36 // If returns false, result is unchnaged.
37 bool invert(Mat2D* result) const;
39 Mat2D invertOrIdentity() const {
40 Mat2D inverse; // initialized to identity
41 (void)invert(&inverse); // inverse is unchanged if invert() fails
45 TransformComponents decompose() const;
46 static Mat2D compose(const TransformComponents&);
47 Mat2D scale(Vec2D) const;
49 static Mat2D multiply(const Mat2D& a, const Mat2D& b);
51 float xx() const { return m_Buffer[0]; }
52 float xy() const { return m_Buffer[1]; }
53 float yx() const { return m_Buffer[2]; }
54 float yy() const { return m_Buffer[3]; }
55 float tx() const { return m_Buffer[4]; }
56 float ty() const { return m_Buffer[5]; }
58 Vec2D translation() const { return {m_Buffer[4], m_Buffer[5]}; }
60 void xx(float value) { m_Buffer[0] = value; }
61 void xy(float value) { m_Buffer[1] = value; }
62 void yx(float value) { m_Buffer[2] = value; }
63 void yy(float value) { m_Buffer[3] = value; }
64 void tx(float value) { m_Buffer[4] = value; }
65 void ty(float value) { m_Buffer[5] = value; }
68 inline Vec2D operator*(const Mat2D& m, Vec2D v) {
70 m[0] * v.x + m[2] * v.y + m[4],
71 m[1] * v.x + m[3] * v.y + m[5],
75 inline Mat2D operator*(const Mat2D& a, const Mat2D& b) { return Mat2D::multiply(a, b); }
77 inline bool operator==(const Mat2D& a, const Mat2D& b) {
78 return a[0] == b[0] && a[1] == b[1] && a[2] == b[2] && a[3] == b[3] && a[4] == b[4] &&