example: just renamed the svg file.
[platform/core/graphics/tizenvg.git] / src / lib / tvgMath.h
1 /*
2  * Copyright (c) 2021 - 2022 Samsung Electronics Co., Ltd. All rights reserved.
3
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef _TVG_MATH_H_
23 #define _TVG_MATH_H_
24
25  #define _USE_MATH_DEFINES
26
27 #include <float.h>
28 #include <math.h>
29 #include "tvgCommon.h"
30
31
32 #define mathMin(x, y) (((x) < (y)) ? (x) : (y))
33 #define mathMax(x, y) (((x) > (y)) ? (x) : (y))
34
35
36 static inline bool mathZero(float a)
37 {
38     return (fabsf(a) < FLT_EPSILON) ? true : false;
39 }
40
41
42 static inline bool mathEqual(float a, float b)
43 {
44     return (fabsf(a - b) < FLT_EPSILON);
45 }
46
47
48 static inline bool mathRightAngle(const Matrix* m)
49 {
50    auto radian = fabsf(atan2f(m->e21, m->e11));
51    if (radian < FLT_EPSILON || mathEqual(radian, float(M_PI_2)) || mathEqual(radian, float(M_PI))) return true;
52    return false;
53 }
54
55
56 static inline bool mathIdentity(const Matrix* m)
57 {
58     if (!mathEqual(m->e11, 1.0f) || !mathZero(m->e12) || !mathZero(m->e13) ||
59         !mathZero(m->e21) || !mathEqual(m->e22, 1.0f) || !mathZero(m->e23) ||
60         !mathZero(m->e31) || !mathZero(m->e32) || !mathEqual(m->e33, 1.0f)) {
61         return false;
62     }
63     return true;
64 }
65
66
67 static inline bool mathInverse(const Matrix* m, Matrix* out)
68 {
69     auto det = m->e11 * (m->e22 * m->e33 - m->e32 * m->e23) -
70                m->e12 * (m->e21 * m->e33 - m->e23 * m->e31) +
71                m->e13 * (m->e21 * m->e32 - m->e22 * m->e31);
72
73     if (mathZero(det)) return false;
74
75     auto invDet = 1 / det;
76
77     out->e11 = (m->e22 * m->e33 - m->e32 * m->e23) * invDet;
78     out->e12 = (m->e13 * m->e32 - m->e12 * m->e33) * invDet;
79     out->e13 = (m->e12 * m->e23 - m->e13 * m->e22) * invDet;
80     out->e21 = (m->e23 * m->e31 - m->e21 * m->e33) * invDet;
81     out->e22 = (m->e11 * m->e33 - m->e13 * m->e31) * invDet;
82     out->e23 = (m->e21 * m->e13 - m->e11 * m->e23) * invDet;
83     out->e31 = (m->e21 * m->e32 - m->e31 * m->e22) * invDet;
84     out->e32 = (m->e31 * m->e12 - m->e11 * m->e32) * invDet;
85     out->e33 = (m->e11 * m->e22 - m->e21 * m->e12) * invDet;
86
87     return true;
88 }
89
90
91 static inline void mathIdentity(Matrix* m)
92 {
93     m->e11 = 1.0f;
94     m->e12 = 0.0f;
95     m->e13 = 0.0f;
96     m->e21 = 0.0f;
97     m->e22 = 1.0f;
98     m->e23 = 0.0f;
99     m->e31 = 0.0f;
100     m->e32 = 0.0f;
101     m->e33 = 1.0f;
102 }
103
104
105 static inline void mathScale(Matrix* m, float scale)
106 {
107     m->e11 = scale;
108     m->e22 = scale;
109 }
110
111
112 static inline void mathTranslate(Matrix* m, float x, float y)
113 {
114     m->e13 = x;
115     m->e23 = y;   
116 }
117
118
119 static inline void mathRotate(Matrix* m, float degree)
120 {
121     auto radian = degree / 180.0f * M_PI;
122     auto cosVal = cosf(radian);
123     auto sinVal = sinf(radian);
124
125     m->e12 = m->e11 * -sinVal;
126     m->e11 *= cosVal;
127     m->e21 = m->e22 * sinVal;
128     m->e22 *= cosVal;
129 }
130
131
132 static inline void mathMultiply(Point* pt, const Matrix* transform)
133 {
134     auto tx = pt->x * transform->e11 + pt->y * transform->e12 + transform->e13;
135     auto ty = pt->x * transform->e21 + pt->y * transform->e22 + transform->e23;
136     pt->x = tx;
137     pt->y = ty;
138 }
139
140
141 static inline Matrix mathMultiply(const Matrix* lhs, const Matrix* rhs)
142 {
143     Matrix m;
144
145     m.e11 = lhs->e11 * rhs->e11 + lhs->e12 * rhs->e21 + lhs->e13 * rhs->e31;
146     m.e12 = lhs->e11 * rhs->e12 + lhs->e12 * rhs->e22 + lhs->e13 * rhs->e32;
147     m.e13 = lhs->e11 * rhs->e13 + lhs->e12 * rhs->e23 + lhs->e13 * rhs->e33;
148
149     m.e21 = lhs->e21 * rhs->e11 + lhs->e22 * rhs->e21 + lhs->e23 * rhs->e31;
150     m.e22 = lhs->e21 * rhs->e12 + lhs->e22 * rhs->e22 + lhs->e23 * rhs->e32;
151     m.e23 = lhs->e21 * rhs->e13 + lhs->e22 * rhs->e23 + lhs->e23 * rhs->e33;
152
153     m.e31 = lhs->e31 * rhs->e11 + lhs->e32 * rhs->e21 + lhs->e33 * rhs->e31;
154     m.e32 = lhs->e31 * rhs->e12 + lhs->e32 * rhs->e22 + lhs->e33 * rhs->e32;
155     m.e33 = lhs->e31 * rhs->e13 + lhs->e32 * rhs->e23 + lhs->e33 * rhs->e33;
156
157     return m;
158 }
159
160
161 #endif //_TVG_MATH_H_