1 /*
2  * Copyright (c) 2021 - 2024 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 #include "../../lv_conf_internal.h"
24 #if LV_USE_THORVG_INTERNAL
25 
26 #include "tvgMath.h"
27 
28 #define BEZIER_EPSILON 1e-2f
29 
30 
31 /************************************************************************/
32 /* Internal Class Implementation                                        */
33 /************************************************************************/
34 
_lineLengthApprox(const Point & pt1,const Point & pt2)35 static float _lineLengthApprox(const Point& pt1, const Point& pt2)
36 {
37     /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
38        With alpha = 1, beta = 3/8, giving results with the largest error less
39        than 7% compared to the exact value. */
40     Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
41     if (diff.x < 0) diff.x = -diff.x;
42     if (diff.y < 0) diff.y = -diff.y;
43     return (diff.x > diff.y) ? (diff.x + diff.y * 0.375f) : (diff.y + diff.x * 0.375f);
44 }
45 
46 
_lineLength(const Point & pt1,const Point & pt2)47 static float _lineLength(const Point& pt1, const Point& pt2)
48 {
49     Point diff = {pt2.x - pt1.x, pt2.y - pt1.y};
50     return sqrtf(diff.x * diff.x + diff.y * diff.y);
51 }
52 
53 
54 template<typename LengthFunc>
_bezLength(const Bezier & cur,LengthFunc lineLengthFunc)55 float _bezLength(const Bezier& cur, LengthFunc lineLengthFunc)
56 {
57     Bezier left, right;
58     auto len = lineLengthFunc(cur.start, cur.ctrl1) + lineLengthFunc(cur.ctrl1, cur.ctrl2) + lineLengthFunc(cur.ctrl2, cur.end);
59     auto chord = lineLengthFunc(cur.start, cur.end);
60 
61     if (fabsf(len - chord) > BEZIER_EPSILON) {
62         cur.split(left, right);
63         return _bezLength(left, lineLengthFunc) + _bezLength(right, lineLengthFunc);
64     }
65     return len;
66 }
67 
68 
69 template<typename LengthFunc>
_bezAt(const Bezier & bz,float at,float length,LengthFunc lineLengthFunc)70 float _bezAt(const Bezier& bz, float at, float length, LengthFunc lineLengthFunc)
71 {
72     auto biggest = 1.0f;
73     auto smallest = 0.0f;
74     auto t = 0.5f;
75 
76     //just in case to prevent an infinite loop
77     if (at <= 0) return 0.0f;
78     if (at >= length) return 1.0f;
79 
80     while (true) {
81         auto right = bz;
82         Bezier left;
83         right.split(t, left);
84         length = _bezLength(left, lineLengthFunc);
85         if (fabsf(length - at) < BEZIER_EPSILON || fabsf(smallest - biggest) < 1e-3f) {
86             break;
87         }
88         if (length < at) {
89             smallest = t;
90             t = (t + biggest) * 0.5f;
91         } else {
92             biggest = t;
93             t = (smallest + t) * 0.5f;
94         }
95     }
96     return t;
97 }
98 
99 
100 /************************************************************************/
101 /* External Class Implementation                                        */
102 /************************************************************************/
103 
104 namespace tvg {
105 
106 //https://en.wikipedia.org/wiki/Remez_algorithm
atan2(float y,float x)107 float atan2(float y, float x)
108 {
109     if (y == 0.0f && x == 0.0f) return 0.0f;
110     auto a = std::min(fabsf(x), fabsf(y)) / std::max(fabsf(x), fabsf(y));
111     auto s = a * a;
112     auto r = ((-0.0464964749f * s + 0.15931422f) * s - 0.327622764f) * s * a + a;
113     if (fabsf(y) > fabsf(x)) r = 1.57079637f - r;
114     if (x < 0) r = 3.14159274f - r;
115     if (y < 0) return -r;
116     return r;
117 }
118 
119 
inverse(const Matrix * m,Matrix * out)120 bool inverse(const Matrix* m, Matrix* out)
121 {
122     auto det = m->e11 * (m->e22 * m->e33 - m->e32 * m->e23) -
123                m->e12 * (m->e21 * m->e33 - m->e23 * m->e31) +
124                m->e13 * (m->e21 * m->e32 - m->e22 * m->e31);
125 
126     auto invDet = 1.0f / det;
127     if (std::isinf(invDet)) return false;
128 
129     out->e11 = (m->e22 * m->e33 - m->e32 * m->e23) * invDet;
130     out->e12 = (m->e13 * m->e32 - m->e12 * m->e33) * invDet;
131     out->e13 = (m->e12 * m->e23 - m->e13 * m->e22) * invDet;
132     out->e21 = (m->e23 * m->e31 - m->e21 * m->e33) * invDet;
133     out->e22 = (m->e11 * m->e33 - m->e13 * m->e31) * invDet;
134     out->e23 = (m->e21 * m->e13 - m->e11 * m->e23) * invDet;
135     out->e31 = (m->e21 * m->e32 - m->e31 * m->e22) * invDet;
136     out->e32 = (m->e31 * m->e12 - m->e11 * m->e32) * invDet;
137     out->e33 = (m->e11 * m->e22 - m->e21 * m->e12) * invDet;
138 
139     return true;
140 }
141 
142 
identity(const Matrix * m)143 bool identity(const Matrix* m)
144 {
145     if (m->e11 != 1.0f || m->e12 != 0.0f || m->e13 != 0.0f ||
146         m->e21 != 0.0f || m->e22 != 1.0f || m->e23 != 0.0f ||
147         m->e31 != 0.0f || m->e32 != 0.0f || m->e33 != 1.0f) {
148         return false;
149     }
150     return true;
151 }
152 
153 
rotate(Matrix * m,float degree)154 void rotate(Matrix* m, float degree)
155 {
156     if (degree == 0.0f) return;
157 
158     auto radian = degree / 180.0f * MATH_PI;
159     auto cosVal = cosf(radian);
160     auto sinVal = sinf(radian);
161 
162     m->e12 = m->e11 * -sinVal;
163     m->e11 *= cosVal;
164     m->e21 = m->e22 * sinVal;
165     m->e22 *= cosVal;
166 }
167 
168 
operator *(const Matrix & lhs,const Matrix & rhs)169 Matrix operator*(const Matrix& lhs, const Matrix& rhs)
170 {
171     Matrix m;
172 
173     m.e11 = lhs.e11 * rhs.e11 + lhs.e12 * rhs.e21 + lhs.e13 * rhs.e31;
174     m.e12 = lhs.e11 * rhs.e12 + lhs.e12 * rhs.e22 + lhs.e13 * rhs.e32;
175     m.e13 = lhs.e11 * rhs.e13 + lhs.e12 * rhs.e23 + lhs.e13 * rhs.e33;
176 
177     m.e21 = lhs.e21 * rhs.e11 + lhs.e22 * rhs.e21 + lhs.e23 * rhs.e31;
178     m.e22 = lhs.e21 * rhs.e12 + lhs.e22 * rhs.e22 + lhs.e23 * rhs.e32;
179     m.e23 = lhs.e21 * rhs.e13 + lhs.e22 * rhs.e23 + lhs.e23 * rhs.e33;
180 
181     m.e31 = lhs.e31 * rhs.e11 + lhs.e32 * rhs.e21 + lhs.e33 * rhs.e31;
182     m.e32 = lhs.e31 * rhs.e12 + lhs.e32 * rhs.e22 + lhs.e33 * rhs.e32;
183     m.e33 = lhs.e31 * rhs.e13 + lhs.e32 * rhs.e23 + lhs.e33 * rhs.e33;
184 
185     return m;
186 }
187 
188 
operator ==(const Matrix & lhs,const Matrix & rhs)189 bool operator==(const Matrix& lhs, const Matrix& rhs)
190 {
191     if (!tvg::equal(lhs.e11, rhs.e11) || !tvg::equal(lhs.e12, rhs.e12) || !tvg::equal(lhs.e13, rhs.e13) ||
192         !tvg::equal(lhs.e21, rhs.e21) || !tvg::equal(lhs.e22, rhs.e22) || !tvg::equal(lhs.e23, rhs.e23) ||
193         !tvg::equal(lhs.e31, rhs.e31) || !tvg::equal(lhs.e32, rhs.e32) || !tvg::equal(lhs.e33, rhs.e33)) {
194        return false;
195     }
196     return true;
197 }
198 
199 
operator *=(Point & pt,const Matrix & m)200 void operator*=(Point& pt, const Matrix& m)
201 {
202     auto tx = pt.x * m.e11 + pt.y * m.e12 + m.e13;
203     auto ty = pt.x * m.e21 + pt.y * m.e22 + m.e23;
204     pt.x = tx;
205     pt.y = ty;
206 }
207 
208 
operator *(const Point & pt,const Matrix & m)209 Point operator*(const Point& pt, const Matrix& m)
210 {
211     auto tx = pt.x * m.e11 + pt.y * m.e12 + m.e13;
212     auto ty = pt.x * m.e21 + pt.y * m.e22 + m.e23;
213     return {tx, ty};
214 }
215 
216 
normal(const Point & p1,const Point & p2)217 Point normal(const Point& p1, const Point& p2)
218 {
219     auto dir = p2 - p1;
220     auto len = length(dir);
221     if (tvg::zero(len)) return {};
222 
223     auto unitDir = dir / len;
224     return {-unitDir.y, unitDir.x};
225 }
226 
227 
length() const228 float Line::length() const
229 {
230     return _lineLength(pt1, pt2);
231 }
232 
233 
split(float at,Line & left,Line & right) const234 void Line::split(float at, Line& left, Line& right) const
235 {
236     auto len = length();
237     auto dx = ((pt2.x - pt1.x) / len) * at;
238     auto dy = ((pt2.y - pt1.y) / len) * at;
239     left.pt1 = pt1;
240     left.pt2.x = left.pt1.x + dx;
241     left.pt2.y = left.pt1.y + dy;
242     right.pt1 = left.pt2;
243     right.pt2 = pt2;
244 }
245 
246 
split(Bezier & left,Bezier & right) const247 void Bezier::split(Bezier& left, Bezier& right) const
248 {
249     auto c = (ctrl1.x + ctrl2.x) * 0.5f;
250     left.ctrl1.x = (start.x + ctrl1.x) * 0.5f;
251     right.ctrl2.x = (ctrl2.x + end.x) * 0.5f;
252     left.start.x = start.x;
253     right.end.x = end.x;
254     left.ctrl2.x = (left.ctrl1.x + c) * 0.5f;
255     right.ctrl1.x = (right.ctrl2.x + c) * 0.5f;
256     left.end.x = right.start.x = (left.ctrl2.x + right.ctrl1.x) * 0.5f;
257 
258     c = (ctrl1.y + ctrl2.y) * 0.5f;
259     left.ctrl1.y = (start.y + ctrl1.y) * 0.5f;
260     right.ctrl2.y = (ctrl2.y + end.y) * 0.5f;
261     left.start.y = start.y;
262     right.end.y = end.y;
263     left.ctrl2.y = (left.ctrl1.y + c) * 0.5f;
264     right.ctrl1.y = (right.ctrl2.y + c) * 0.5f;
265     left.end.y = right.start.y = (left.ctrl2.y + right.ctrl1.y) * 0.5f;
266 }
267 
268 
split(float at,Bezier & left,Bezier & right) const269 void Bezier::split(float at, Bezier& left, Bezier& right) const
270 {
271     right = *this;
272     auto t = right.at(at, right.length());
273     right.split(t, left);
274 }
275 
276 
length() const277 float Bezier::length() const
278 {
279     return _bezLength(*this, _lineLength);
280 }
281 
282 
lengthApprox() const283 float Bezier::lengthApprox() const
284 {
285     return _bezLength(*this, _lineLengthApprox);
286 }
287 
288 
split(float t,Bezier & left)289 void Bezier::split(float t, Bezier& left)
290 {
291     left.start = start;
292 
293     left.ctrl1.x = start.x + t * (ctrl1.x - start.x);
294     left.ctrl1.y = start.y + t * (ctrl1.y - start.y);
295 
296     left.ctrl2.x = ctrl1.x + t * (ctrl2.x - ctrl1.x); //temporary holding spot
297     left.ctrl2.y = ctrl1.y + t * (ctrl2.y - ctrl1.y); //temporary holding spot
298 
299     ctrl2.x = ctrl2.x + t * (end.x - ctrl2.x);
300     ctrl2.y = ctrl2.y + t * (end.y - ctrl2.y);
301 
302     ctrl1.x = left.ctrl2.x + t * (ctrl2.x - left.ctrl2.x);
303     ctrl1.y = left.ctrl2.y + t * (ctrl2.y - left.ctrl2.y);
304 
305     left.ctrl2.x = left.ctrl1.x + t * (left.ctrl2.x - left.ctrl1.x);
306     left.ctrl2.y = left.ctrl1.y + t * (left.ctrl2.y - left.ctrl1.y);
307 
308     left.end.x = start.x = left.ctrl2.x + t * (ctrl1.x - left.ctrl2.x);
309     left.end.y = start.y = left.ctrl2.y + t * (ctrl1.y - left.ctrl2.y);
310 }
311 
312 
at(float at,float length) const313 float Bezier::at(float at, float length) const
314 {
315     return _bezAt(*this, at, length, _lineLength);
316 }
317 
318 
atApprox(float at,float length) const319 float Bezier::atApprox(float at, float length) const
320 {
321     return _bezAt(*this, at, length, _lineLengthApprox);
322 }
323 
324 
at(float t) const325 Point Bezier::at(float t) const
326 {
327     Point cur;
328     auto it = 1.0f - t;
329 
330     auto ax = start.x * it + ctrl1.x * t;
331     auto bx = ctrl1.x * it + ctrl2.x * t;
332     auto cx = ctrl2.x * it + end.x * t;
333     ax = ax * it + bx * t;
334     bx = bx * it + cx * t;
335     cur.x = ax * it + bx * t;
336 
337     float ay = start.y * it + ctrl1.y * t;
338     float by = ctrl1.y * it + ctrl2.y * t;
339     float cy = ctrl2.y * it + end.y * t;
340     ay = ay * it + by * t;
341     by = by * it + cy * t;
342     cur.y = ay * it + by * t;
343 
344     return cur;
345 }
346 
347 
angle(float t) const348 float Bezier::angle(float t) const
349 {
350     if (t < 0 || t > 1) return 0;
351 
352     //derivate
353     // p'(t) = 3 * (-(1-2t+t^2) * p0 + (1 - 4 * t + 3 * t^2) * p1 + (2 * t - 3 *
354     // t^2) * p2 + t^2 * p3)
355     float mt = 1.0f - t;
356     float d = t * t;
357     float a = -mt * mt;
358     float b = 1 - 4 * t + 3 * d;
359     float c = 2 * t - 3 * d;
360 
361     Point pt ={a * start.x + b * ctrl1.x + c * ctrl2.x + d * end.x, a * start.y + b * ctrl1.y + c * ctrl2.y + d * end.y};
362     pt.x *= 3;
363     pt.y *= 3;
364 
365     return rad2deg(tvg::atan2(pt.y, pt.x));
366 }
367 
368 
lerp(const uint8_t & start,const uint8_t & end,float t)369 uint8_t lerp(const uint8_t &start, const uint8_t &end, float t)
370 {
371     auto result = static_cast<int>(start + (end - start) * t);
372     tvg::clamp(result, 0, 255);
373     return static_cast<uint8_t>(result);
374 }
375 
376 }
377 
378 
379 #endif /* LV_USE_THORVG_INTERNAL */
380 
381