2 * Copyright (c) 2020 - 2023 the ThorVG project. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
24 #include "tvgSwCommon.h"
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
31 //clz: count leading zero’s
32 #if defined(_MSC_VER) && !defined(__clang__)
34 static uint32_t __inline _clz(uint32_t value)
36 unsigned long leadingZero = 0;
37 if (_BitScanReverse(&leadingZero, value)) return 31 - leadingZero;
41 #define _clz(x) __builtin_clz((x))
45 constexpr SwFixed CORDIC_FACTOR = 0xDBD95B16UL; //the Cordic shrink factor 0.858785336480436 * 2^32
47 //this table was generated for SW_FT_PI = 180L << 16, i.e. degrees
48 constexpr static auto ATAN_MAX = 23;
49 constexpr static SwFixed ATAN_TBL[] = {
50 1740967L, 919879L, 466945L, 234379L, 117304L, 58666L, 29335L,
51 14668L, 7334L, 3667L, 1833L, 917L, 458L, 229L, 115L,
52 57L, 29L, 14L, 7L, 4L, 2L, 1L};
54 static inline SwCoord SATURATE(const SwCoord x)
56 return (x >> (sizeof(SwCoord) * 8 - 1));
60 static inline SwFixed PAD_ROUND(const SwFixed x, int32_t n)
62 return (((x) + ((n)/2)) & ~((n)-1));
66 static SwCoord _downscale(SwFixed x)
68 //multiply a give value by the CORDIC shrink factor
70 int64_t t = (s * static_cast<int64_t>(CORDIC_FACTOR)) + 0x100000000UL;
71 s = static_cast<SwFixed>(t >> 32);
77 static int32_t _normalize(SwPoint& pt)
79 /* the highest bit in overflow-safe vector components
80 MSB of 0.858785336480436 * sqrt(0.5) * 2^30 */
81 constexpr auto SAFE_MSB = 29;
86 int32_t shift = 31 - _clz(abs(v.x) | abs(v.y));
88 if (shift <= SAFE_MSB) {
89 shift = SAFE_MSB - shift;
90 pt.x = static_cast<SwCoord>((unsigned long)v.x << shift);
91 pt.y = static_cast<SwCoord>((unsigned long)v.y << shift);
102 static void _polarize(SwPoint& pt)
107 //Get the vector into [-PI/4, PI/4] sector
113 theta = SW_ANGLE_PI2;
115 theta = v.y > 0 ? SW_ANGLE_PI : -SW_ANGLE_PI;
121 theta = -SW_ANGLE_PI2;
130 auto atan = ATAN_TBL;
134 //Pseudorotations. with right shifts
135 for (i = 1, j = 1; i < ATAN_MAX; j <<= 1, ++i) {
137 auto tmp = v.x + ((v.y + j) >> i);
138 v.y = v.y - ((v.x + j) >> i);
142 auto tmp = v.x - ((v.y + j) >> i);
143 v.y = v.y + ((v.x + j) >> i);
150 if (theta >= 0) theta = PAD_ROUND(theta, 32);
151 else theta = -PAD_ROUND(-theta, 32);
158 static void _rotate(SwPoint& pt, SwFixed theta)
163 //Rotate inside [-PI/4, PI/4] sector
164 while (theta < -SW_ANGLE_PI4) {
168 theta += SW_ANGLE_PI2;
171 while (theta > SW_ANGLE_PI4) {
175 theta -= SW_ANGLE_PI2;
178 auto atan = ATAN_TBL;
182 for (i = 1, j = 1; i < ATAN_MAX; j <<= 1, ++i) {
184 auto tmp = x + ((y + j) >> i);
185 y = y - ((x + j) >> i);
189 auto tmp = x - ((y + j) >> i);
190 y = y + ((x + j) >> i);
196 pt.x = static_cast<SwCoord>(x);
197 pt.y = static_cast<SwCoord>(y);
201 /************************************************************************/
202 /* External Class Implementation */
203 /************************************************************************/
205 SwFixed mathMean(SwFixed angle1, SwFixed angle2)
207 return angle1 + mathDiff(angle1, angle2) / 2;
211 bool mathSmallCubic(const SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut)
213 auto d1 = base[2] - base[3];
214 auto d2 = base[1] - base[2];
215 auto d3 = base[0] - base[1];
221 //do nothing to retain original direction
223 angleIn = angleMid = angleOut = mathAtan(d3);
227 angleIn = angleMid = angleOut = mathAtan(d2);
229 angleIn = angleMid = mathAtan(d2);
230 angleOut = mathAtan(d3);
236 angleIn = angleMid = angleOut = mathAtan(d1);
238 angleIn = mathAtan(d1);
239 angleOut = mathAtan(d3);
240 angleMid = mathMean(angleIn, angleOut);
244 angleIn = mathAtan(d1);
245 angleMid = angleOut = mathAtan(d2);
247 angleIn = mathAtan(d1);
248 angleMid = mathAtan(d2);
249 angleOut = mathAtan(d3);
254 auto theta1 = abs(mathDiff(angleIn, angleMid));
255 auto theta2 = abs(mathDiff(angleMid, angleOut));
257 if ((theta1 < (SW_ANGLE_PI / 8)) && (theta2 < (SW_ANGLE_PI / 8))) return true;
262 int64_t mathMultiply(int64_t a, int64_t b)
275 int64_t c = (a * b + 0x8000L) >> 16;
276 return (s > 0) ? c : -c;
280 int64_t mathDivide(int64_t a, int64_t b)
293 int64_t q = b > 0 ? ((a << 16) + (b >> 1)) / b : 0x7FFFFFFFL;
294 return (s < 0 ? -q : q);
298 int64_t mathMulDiv(int64_t a, int64_t b, int64_t c)
315 int64_t d = c > 0 ? (a * b + (c >> 1)) / c : 0x7FFFFFFFL;
317 return (s > 0 ? d : -d);
321 void mathRotate(SwPoint& pt, SwFixed angle)
323 if (angle == 0 || (pt.x == 0 && pt.y == 0)) return;
326 auto shift = _normalize(v);
331 v.x = _downscale(v.x);
332 v.y = _downscale(v.y);
335 auto half = static_cast<int32_t>(1L << (shift - 1));
336 pt.x = (v.x + half + SATURATE(v.x)) >> shift;
337 pt.y = (v.y + half + SATURATE(v.y)) >> shift;
340 pt.x = static_cast<SwCoord>((unsigned long)v.x << shift);
341 pt.y = static_cast<SwCoord>((unsigned long)v.y << shift);
345 SwFixed mathTan(SwFixed angle)
347 SwPoint v = {CORDIC_FACTOR >> 8, 0};
349 return mathDivide(v.y, v.x);
353 SwFixed mathAtan(const SwPoint& pt)
355 if (pt.x == 0 && pt.y == 0) return 0;
365 SwFixed mathSin(SwFixed angle)
367 return mathCos(SW_ANGLE_PI2 - angle);
371 SwFixed mathCos(SwFixed angle)
373 SwPoint v = {CORDIC_FACTOR >> 8, 0};
375 return (v.x + 0x80L) >> 8;
379 SwFixed mathLength(const SwPoint& pt)
384 if (v.x == 0) return abs(v.y);
385 if (v.y == 0) return abs(v.x);
388 auto shift = _normalize(v);
390 v.x = _downscale(v.x);
392 if (shift > 0) return (v.x + (static_cast<SwFixed>(1) << (shift -1))) >> shift;
393 return static_cast<SwFixed>((uint32_t)v.x << -shift);
397 void mathSplitCubic(SwPoint* base)
401 base[6].x = base[3].x;
404 base[1].x = a = (base[0].x + c) / 2;
405 base[5].x = b = (base[3].x + d) / 2;
407 base[2].x = a = (a + c) / 2;
408 base[4].x = b = (b + c) / 2;
409 base[3].x = (a + b) / 2;
411 base[6].y = base[3].y;
414 base[1].y = a = (base[0].y + c) / 2;
415 base[5].y = b = (base[3].y + d) / 2;
417 base[2].y = a = (a + c) / 2;
418 base[4].y = b = (b + c) / 2;
419 base[3].y = (a + b) / 2;
423 SwFixed mathDiff(SwFixed angle1, SwFixed angle2)
425 auto delta = angle2 - angle1;
427 delta %= SW_ANGLE_2PI;
428 if (delta < 0) delta += SW_ANGLE_2PI;
429 if (delta > SW_ANGLE_PI) delta -= SW_ANGLE_2PI;
435 SwPoint mathTransform(const Point* to, const Matrix* transform)
437 if (!transform) return {TO_SWCOORD(to->x), TO_SWCOORD(to->y)};
439 auto tx = to->x * transform->e11 + to->y * transform->e12 + transform->e13;
440 auto ty = to->x * transform->e21 + to->y * transform->e22 + transform->e23;
442 return {TO_SWCOORD(tx), TO_SWCOORD(ty)};
446 bool mathClipBBox(const SwBBox& clipper, SwBBox& clipee)
448 clipee.max.x = (clipee.max.x < clipper.max.x) ? clipee.max.x : clipper.max.x;
449 clipee.max.y = (clipee.max.y < clipper.max.y) ? clipee.max.y : clipper.max.y;
450 clipee.min.x = (clipee.min.x > clipper.min.x) ? clipee.min.x : clipper.min.x;
451 clipee.min.y = (clipee.min.y > clipper.min.y) ? clipee.min.y : clipper.min.y;
454 if (clipee.max.x - clipee.min.x < 1 && clipee.max.y - clipee.min.y < 1) return false;
457 if (clipee.min.x >= clipper.max.x || clipee.min.y >= clipper.max.y ||
458 clipee.max.x <= clipper.min.x || clipee.max.y <= clipper.min.y) return false;
464 bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, SwBBox& renderRegion, bool fastTrack)
466 if (!outline) return false;
468 auto pt = outline->pts;
470 if (outline->ptsCnt == 0 || outline->cntrsCnt <= 0) {
471 renderRegion.reset();
482 for (uint32_t i = 1; i < outline->ptsCnt; ++i, ++pt) {
483 if (xMin > pt->x) xMin = pt->x;
484 if (xMax < pt->x) xMax = pt->x;
485 if (yMin > pt->y) yMin = pt->y;
486 if (yMax < pt->y) yMax = pt->y;
488 //Since no antialiasing is applied in the Fast Track case,
489 //the rasterization region has to be rearranged.
490 //https://github.com/Samsung/thorvg/issues/916
492 renderRegion.min.x = static_cast<SwCoord>(round(xMin / 64.0f));
493 renderRegion.max.x = static_cast<SwCoord>(round(xMax / 64.0f));
494 renderRegion.min.y = static_cast<SwCoord>(round(yMin / 64.0f));
495 renderRegion.max.y = static_cast<SwCoord>(round(yMax / 64.0f));
497 renderRegion.min.x = xMin >> 6;
498 renderRegion.max.x = (xMax + 63) >> 6;
499 renderRegion.min.y = yMin >> 6;
500 renderRegion.max.y = (yMax + 63) >> 6;
502 return mathClipBBox(clipRegion, renderRegion);