Debian Codegraph @

thirdparty/thorvg/src/renderer/sw_engine/tvgSwMath.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2020 - 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 "tvgMath.h"
24 #include "tvgSwCommon.h"
25
26
27 /************************************************************************/
28 /* Internal Class Implementation */
29 /************************************************************************/
30
31 static float TO_RADIAN(SwFixed angle)
32 {
33 return (float(angle) / 65536.0f) * (MATH_PI / 180.0f);
34 }
35
36
37 /************************************************************************/
38 /* External Class Implementation */
39 /************************************************************************/
40
41 SwFixed mathMean(SwFixed angle1, SwFixed angle2)
42 {
43 return angle1 + mathDiff(angle1, angle2) / 2;
44 }
45
46
47 int mathCubicAngle(const SwPoint* base, SwFixed& angleIn, SwFixed& angleMid, SwFixed& angleOut)
48 {
49 auto d1 = base[2] - base[3];
50 auto d2 = base[1] - base[2];
51 auto d3 = base[0] - base[1];
52
53 if (d1.small()) {
54 if (d2.small()) {
55 if (d3.small()) {
56 angleIn = angleMid = angleOut = 0;
57 return -1; //ignoreable
58 } else {
59 angleIn = angleMid = angleOut = mathAtan(d3);
60 }
61 } else {
62 if (d3.small()) {
63 angleIn = angleMid = angleOut = mathAtan(d2);
64 } else {
65 angleIn = angleMid = mathAtan(d2);
66 angleOut = mathAtan(d3);
67 }
68 }
69 } else {
70 if (d2.small()) {
71 if (d3.small()) {
72 angleIn = angleMid = angleOut = mathAtan(d1);
73 } else {
74 angleIn = mathAtan(d1);
75 angleOut = mathAtan(d3);
76 angleMid = mathMean(angleIn, angleOut);
77 }
78 } else {
79 if (d3.small()) {
80 angleIn = mathAtan(d1);
81 angleMid = angleOut = mathAtan(d2);
82 } else {
83 angleIn = mathAtan(d1);
84 angleMid = mathAtan(d2);
85 angleOut = mathAtan(d3);
86 }
87 }
88 }
89
90 auto theta1 = abs(mathDiff(angleIn, angleMid));
91 auto theta2 = abs(mathDiff(angleMid, angleOut));
92
93 if ((theta1 < (SW_ANGLE_PI / 8)) && (theta2 < (SW_ANGLE_PI / 8))) return 0; //small size
94 return 1;
95 }
96
97
98 int64_t mathMultiply(int64_t a, int64_t b)
99 {
100 int32_t s = 1;
101
102 //move sign
103 if (a < 0) {
104 a = -a;
105 s = -s;
106 }
107 if (b < 0) {
108 b = -b;
109 s = -s;
110 }
111 int64_t c = (a * b + 0x8000L) >> 16;
112 return (s > 0) ? c : -c;
113 }
114
115
116 int64_t mathDivide(int64_t a, int64_t b)
117 {
118 int32_t s = 1;
119
120 //move sign
121 if (a < 0) {
122 a = -a;
123 s = -s;
124 }
125 if (b < 0) {
126 b = -b;
127 s = -s;
128 }
129 int64_t q = b > 0 ? ((a << 16) + (b >> 1)) / b : 0x7FFFFFFFL;
130 return (s < 0 ? -q : q);
131 }
132
133
134 int64_t mathMulDiv(int64_t a, int64_t b, int64_t c)
135 {
136 int32_t s = 1;
137
138 //move sign
139 if (a < 0) {
140 a = -a;
141 s = -s;
142 }
143 if (b < 0) {
144 b = -b;
145 s = -s;
146 }
147 if (c < 0) {
148 c = -c;
149 s = -s;
150 }
151 int64_t d = c > 0 ? (a * b + (c >> 1)) / c : 0x7FFFFFFFL;
152
153 return (s > 0 ? d : -d);
154 }
155
156
157 void mathRotate(SwPoint& pt, SwFixed angle)
158 {
159 if (angle == 0 || pt.zero()) return;
160
161 Point v = pt.toPoint();
162
163 auto radian = TO_RADIAN(angle);
164 auto cosv = cosf(radian);
165 auto sinv = sinf(radian);
166
167 pt.x = SwCoord(nearbyint((v.x * cosv - v.y * sinv) * 64.0f));
168 pt.y = SwCoord(nearbyint((v.x * sinv + v.y * cosv) * 64.0f));
169 }
170
171
172 SwFixed mathTan(SwFixed angle)
173 {
174 if (angle == 0) return 0;
175 return SwFixed(tanf(TO_RADIAN(angle)) * 65536.0f);
176 }
177
178
179 SwFixed mathAtan(const SwPoint& pt)
180 {
181 if (pt.zero()) return 0;
182 return SwFixed(tvg::atan2(TO_FLOAT(pt.y), TO_FLOAT(pt.x)) * (180.0f / MATH_PI) * 65536.0f);
183 }
184
185
186 SwFixed mathSin(SwFixed angle)
187 {
188 if (angle == 0) return 0;
189 return mathCos(SW_ANGLE_PI2 - angle);
190 }
191
192
193 SwFixed mathCos(SwFixed angle)
194 {
195 return SwFixed(cosf(TO_RADIAN(angle)) * 65536.0f);
196 }
197
198
199 SwFixed mathLength(const SwPoint& pt)
200 {
201 if (pt.zero()) return 0;
202
203 //trivial case
204 if (pt.x == 0) return abs(pt.y);
205 if (pt.y == 0) return abs(pt.x);
206
207 auto v = pt.toPoint();
208 //return static_cast<SwFixed>(sqrtf(v.x * v.x + v.y * v.y) * 65536.0f);
209
210 /* approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
211 With alpha = 1, beta = 3/8, giving results with the largest error less
212 than 7% compared to the exact value. */
213 if (v.x < 0) v.x = -v.x;
214 if (v.y < 0) v.y = -v.y;
215 return static_cast<SwFixed>((v.x > v.y) ? (v.x + v.y * 0.375f) : (v.y + v.x * 0.375f));
216 }
217
218
219 void mathSplitCubic(SwPoint* base)
220 {
221 SwCoord a, b, c, d;
222
223 base[6].x = base[3].x;
224 c = base[1].x;
225 d = base[2].x;
226 base[1].x = a = (base[0].x + c) >> 1;
227 base[5].x = b = (base[3].x + d) >> 1;
228 c = (c + d) >> 1;
229 base[2].x = a = (a + c) >> 1;
230 base[4].x = b = (b + c) >> 1;
231 base[3].x = (a + b) >> 1;
232
233 base[6].y = base[3].y;
234 c = base[1].y;
235 d = base[2].y;
236 base[1].y = a = (base[0].y + c) >> 1;
237 base[5].y = b = (base[3].y + d) >> 1;
238 c = (c + d) >> 1;
239 base[2].y = a = (a + c) >> 1;
240 base[4].y = b = (b + c) >> 1;
241 base[3].y = (a + b) >> 1;
242 }
243
244
245 void mathSplitLine(SwPoint* base)
246 {
247 base[2] = base[1];
248 base[1] = {(base[0].x >> 1) + (base[1].x >> 1), (base[0].y >> 1) + (base[1].y >> 1)};
249 }
250
251
252 SwFixed mathDiff(SwFixed angle1, SwFixed angle2)
253 {
254 auto delta = angle2 - angle1;
255
256 delta %= SW_ANGLE_2PI;
257 if (delta < 0) delta += SW_ANGLE_2PI;
258 if (delta > SW_ANGLE_PI) delta -= SW_ANGLE_2PI;
259
260 return delta;
261 }
262
263
264 SwPoint mathTransform(const Point* to, const Matrix& transform)
265 {
266 auto tx = to->x * transform.e11 + to->y * transform.e12 + transform.e13;
267 auto ty = to->x * transform.e21 + to->y * transform.e22 + transform.e23;
268
269 return {TO_SWCOORD(tx), TO_SWCOORD(ty)};
270 }
271
272
273 bool mathClipBBox(const SwBBox& clipper, SwBBox& clippee)
274 {
275 clippee.max.x = (clippee.max.x < clipper.max.x) ? clippee.max.x : clipper.max.x;
276 clippee.max.y = (clippee.max.y < clipper.max.y) ? clippee.max.y : clipper.max.y;
277 clippee.min.x = (clippee.min.x > clipper.min.x) ? clippee.min.x : clipper.min.x;
278 clippee.min.y = (clippee.min.y > clipper.min.y) ? clippee.min.y : clipper.min.y;
279
280 //Check valid region
281 if (clippee.max.x - clippee.min.x < 1 && clippee.max.y - clippee.min.y < 1) return false;
282
283 //Check boundary
284 if (clippee.min.x >= clipper.max.x || clippee.min.y >= clipper.max.y ||
285 clippee.max.x <= clipper.min.x || clippee.max.y <= clipper.min.y) return false;
286
287 return true;
288 }
289
290
291 bool mathUpdateOutlineBBox(const SwOutline* outline, const SwBBox& clipRegion, SwBBox& renderRegion, bool fastTrack)
292 {
293 if (!outline) return false;
294
295 if (outline->pts.empty() || outline->cntrs.empty()) {
296 renderRegion.reset();
297 return false;
298 }
299
300 auto pt = outline->pts.begin();
301
302 auto xMin = pt->x;
303 auto xMax = pt->x;
304 auto yMin = pt->y;
305 auto yMax = pt->y;
306
307 for (++pt; pt < outline->pts.end(); ++pt) {
308 if (xMin > pt->x) xMin = pt->x;
309 if (xMax < pt->x) xMax = pt->x;
310 if (yMin > pt->y) yMin = pt->y;
311 if (yMax < pt->y) yMax = pt->y;
312 }
313
314 if (fastTrack) {
315 renderRegion.min.x = static_cast<SwCoord>(round(xMin / 64.0f));
316 renderRegion.max.x = static_cast<SwCoord>(round(xMax / 64.0f));
317 renderRegion.min.y = static_cast<SwCoord>(round(yMin / 64.0f));
318 renderRegion.max.y = static_cast<SwCoord>(round(yMax / 64.0f));
319 } else {
320 renderRegion.min.x = xMin >> 6;
321 renderRegion.max.x = (xMax + 63) >> 6;
322 renderRegion.min.y = yMin >> 6;
323 renderRegion.max.y = (yMax + 63) >> 6;
324 }
325 return mathClipBBox(clipRegion, renderRegion);
326 }