Update rive-cpp to 2.0 version
[platform/core/uifw/rive-tizen.git] / submodule / rive-cpp / include / rive / math / mat2d.hpp
1 #ifndef _RIVE_MAT2D_HPP_
2 #define _RIVE_MAT2D_HPP_
3
4 #include "rive/math/vec2d.hpp"
5 #include <cstddef>
6
7 namespace rive {
8     class TransformComponents;
9     class Mat2D {
10     private:
11         float m_Buffer[6];
12
13     public:
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} {}
18
19         inline const float* values() const { return m_Buffer; }
20
21         float& operator[](std::size_t idx) { return m_Buffer[idx]; }
22         const float& operator[](std::size_t idx) const { return m_Buffer[idx]; }
23
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}; }
27
28         void scaleByValues(float sx, float sy);
29
30         Mat2D& operator*=(const Mat2D& rhs) {
31             *this = Mat2D::multiply(*this, rhs);
32             return *this;
33         }
34
35         // If returns true, result holds the inverse.
36         // If returns false, result is unchnaged.
37         bool invert(Mat2D* result) const;
38
39         Mat2D invertOrIdentity() const {
40             Mat2D inverse;          // initialized to identity
41             (void)invert(&inverse); // inverse is unchanged if invert() fails
42             return inverse;
43         }
44     
45         TransformComponents decompose() const;
46         static Mat2D compose(const TransformComponents&);
47         Mat2D scale(Vec2D) const;
48
49         static Mat2D multiply(const Mat2D& a, const Mat2D& b);
50
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]; }
57
58         Vec2D translation() const { return {m_Buffer[4], m_Buffer[5]}; }
59
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; }
66     };
67
68     inline Vec2D operator*(const Mat2D& m, Vec2D v) {
69         return {
70             m[0] * v.x + m[2] * v.y + m[4],
71             m[1] * v.x + m[3] * v.y + m[5],
72         };
73     }
74
75     inline Mat2D operator*(const Mat2D& a, const Mat2D& b) { return Mat2D::multiply(a, b); }
76
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] &&
79                a[5] == b[5];
80     }
81 } // namespace rive
82 #endif