1/*
2 * Copyright (c) 2021 - 2023 the ThorVG project. 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
23#ifndef _TVG_MATH_H_
24#define _TVG_MATH_H_
25
26 #define _USE_MATH_DEFINES
27
28#include <float.h>
29#include <math.h>
30#include "tvgCommon.h"
31
32
33#define mathMin(x, y) (((x) < (y)) ? (x) : (y))
34#define mathMax(x, y) (((x) > (y)) ? (x) : (y))
35
36
37static inline bool mathZero(float a)
38{
39 return (fabsf(a) < FLT_EPSILON) ? true : false;
40}
41
42
43static inline bool mathEqual(float a, float b)
44{
45 return (fabsf(a - b) < FLT_EPSILON);
46}
47
48static inline bool mathEqual(const Matrix& a, const Matrix& b)
49{
50 if (!mathEqual(a.e11, b.e11) || !mathEqual(a.e12, b.e12) || !mathEqual(a.e13, b.e13) ||
51 !mathEqual(a.e21, b.e21) || !mathEqual(a.e22, b.e22) || !mathEqual(a.e23, b.e23) ||
52 !mathEqual(a.e31, b.e31) || !mathEqual(a.e32, b.e32) || !mathEqual(a.e33, b.e33)) {
53 return false;
54 }
55 return true;
56}
57
58static inline bool mathRightAngle(const Matrix* m)
59{
60 auto radian = fabsf(atan2f(m->e21, m->e11));
61 if (radian < FLT_EPSILON || mathEqual(radian, float(M_PI_2)) || mathEqual(radian, float(M_PI))) return true;
62 return false;
63}
64
65
66static inline bool mathSkewed(const Matrix* m)
67{
68 return (fabsf(m->e21 + m->e12) > FLT_EPSILON);
69}
70
71
72static inline bool mathIdentity(const Matrix* m)
73{
74 if (!mathEqual(m->e11, 1.0f) || !mathZero(m->e12) || !mathZero(m->e13) ||
75 !mathZero(m->e21) || !mathEqual(m->e22, 1.0f) || !mathZero(m->e23) ||
76 !mathZero(m->e31) || !mathZero(m->e32) || !mathEqual(m->e33, 1.0f)) {
77 return false;
78 }
79 return true;
80}
81
82
83static inline bool mathInverse(const Matrix* m, Matrix* out)
84{
85 auto det = m->e11 * (m->e22 * m->e33 - m->e32 * m->e23) -
86 m->e12 * (m->e21 * m->e33 - m->e23 * m->e31) +
87 m->e13 * (m->e21 * m->e32 - m->e22 * m->e31);
88
89 if (mathZero(det)) return false;
90
91 auto invDet = 1 / det;
92
93 out->e11 = (m->e22 * m->e33 - m->e32 * m->e23) * invDet;
94 out->e12 = (m->e13 * m->e32 - m->e12 * m->e33) * invDet;
95 out->e13 = (m->e12 * m->e23 - m->e13 * m->e22) * invDet;
96 out->e21 = (m->e23 * m->e31 - m->e21 * m->e33) * invDet;
97 out->e22 = (m->e11 * m->e33 - m->e13 * m->e31) * invDet;
98 out->e23 = (m->e21 * m->e13 - m->e11 * m->e23) * invDet;
99 out->e31 = (m->e21 * m->e32 - m->e31 * m->e22) * invDet;
100 out->e32 = (m->e31 * m->e12 - m->e11 * m->e32) * invDet;
101 out->e33 = (m->e11 * m->e22 - m->e21 * m->e12) * invDet;
102
103 return true;
104}
105
106
107static inline void mathIdentity(Matrix* m)
108{
109 m->e11 = 1.0f;
110 m->e12 = 0.0f;
111 m->e13 = 0.0f;
112 m->e21 = 0.0f;
113 m->e22 = 1.0f;
114 m->e23 = 0.0f;
115 m->e31 = 0.0f;
116 m->e32 = 0.0f;
117 m->e33 = 1.0f;
118}
119
120
121static inline void mathScale(Matrix* m, float sx, float sy)
122{
123 m->e11 *= sx;
124 m->e22 *= sy;
125}
126
127
128static inline void mathTranslate(Matrix* m, float x, float y)
129{
130 m->e13 += x;
131 m->e23 += y;
132}
133
134
135static inline void mathRotate(Matrix* m, float degree)
136{
137 auto radian = degree / 180.0f * M_PI;
138 auto cosVal = cosf(radian);
139 auto sinVal = sinf(radian);
140
141 m->e12 = m->e11 * -sinVal;
142 m->e11 *= cosVal;
143 m->e21 = m->e22 * sinVal;
144 m->e22 *= cosVal;
145}
146
147
148static inline void mathMultiply(Point* pt, const Matrix* transform)
149{
150 auto tx = pt->x * transform->e11 + pt->y * transform->e12 + transform->e13;
151 auto ty = pt->x * transform->e21 + pt->y * transform->e22 + transform->e23;
152 pt->x = tx;
153 pt->y = ty;
154}
155
156
157static inline Matrix mathMultiply(const Matrix* lhs, const Matrix* rhs)
158{
159 Matrix m;
160
161 m.e11 = lhs->e11 * rhs->e11 + lhs->e12 * rhs->e21 + lhs->e13 * rhs->e31;
162 m.e12 = lhs->e11 * rhs->e12 + lhs->e12 * rhs->e22 + lhs->e13 * rhs->e32;
163 m.e13 = lhs->e11 * rhs->e13 + lhs->e12 * rhs->e23 + lhs->e13 * rhs->e33;
164
165 m.e21 = lhs->e21 * rhs->e11 + lhs->e22 * rhs->e21 + lhs->e23 * rhs->e31;
166 m.e22 = lhs->e21 * rhs->e12 + lhs->e22 * rhs->e22 + lhs->e23 * rhs->e32;
167 m.e23 = lhs->e21 * rhs->e13 + lhs->e22 * rhs->e23 + lhs->e23 * rhs->e33;
168
169 m.e31 = lhs->e31 * rhs->e11 + lhs->e32 * rhs->e21 + lhs->e33 * rhs->e31;
170 m.e32 = lhs->e31 * rhs->e12 + lhs->e32 * rhs->e22 + lhs->e33 * rhs->e32;
171 m.e33 = lhs->e31 * rhs->e13 + lhs->e32 * rhs->e23 + lhs->e33 * rhs->e33;
172
173 return m;
174}
175
176
177static inline Point operator-(const Point& lhs, const Point& rhs)
178{
179 return {lhs.x - rhs.x, lhs.y - rhs.y};
180}
181
182
183static inline Point operator+(const Point& lhs, const Point& rhs)
184{
185 return {lhs.x + rhs.x, lhs.y + rhs.y};
186}
187
188
189static inline Point operator*(const Point& lhs, float rhs)
190{
191 return {lhs.x * rhs, lhs.y * rhs};
192}
193
194
195template <typename T>
196static inline T mathLerp(const T &start, const T &end, float t)
197{
198 return static_cast<T>(start + (end - start) * t);
199}
200
201
202#endif //_TVG_MATH_H_
203