blob: 14ad33e798899d419ceab6e89e91388175ca1f1a [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkMatrix.h"
9#include "include/core/SkPoint3.h"
10#include "include/private/SkNx.h"
11#include "src/core/SkGeometry.h"
12#include "src/core/SkPointPriv.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
Ben Wagnerf08d1d02018-06-18 15:11:00 -040014#include <utility>
15
reedb6402032015-03-20 13:23:43 -070016static SkVector to_vector(const Sk2s& x) {
17 SkVector vector;
mtklein507ef6d2016-01-31 08:02:47 -080018 x.store(&vector);
reedb6402032015-03-20 13:23:43 -070019 return vector;
20}
21
reed@android.com8a1c16f2008-12-17 15:59:43 +000022////////////////////////////////////////////////////////////////////////
23
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000024static int is_not_monotonic(SkScalar a, SkScalar b, SkScalar c) {
25 SkScalar ab = a - b;
26 SkScalar bc = b - c;
reed@google.com8f4d2302013-12-17 16:44:46 +000027 if (ab < 0) {
28 bc = -bc;
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 }
reed@google.com8f4d2302013-12-17 16:44:46 +000030 return ab == 0 || bc < 0;
31}
reed@android.com8a1c16f2008-12-17 15:59:43 +000032
33////////////////////////////////////////////////////////////////////////
34
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000035static int valid_unit_divide(SkScalar numer, SkScalar denom, SkScalar* ratio) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000036 SkASSERT(ratio);
37
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000038 if (numer < 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 numer = -numer;
40 denom = -denom;
41 }
42
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000043 if (denom == 0 || numer == 0 || numer >= denom) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000044 return 0;
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000045 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000046
reed80ea19c2015-05-12 10:37:34 -070047 SkScalar r = numer / denom;
reed@android.com15161622010-03-08 17:44:42 +000048 if (SkScalarIsNaN(r)) {
49 return 0;
50 }
Mike Klein11fca3f2014-09-12 12:17:25 -040051 SkASSERTF(r >= 0 && r < SK_Scalar1, "numer %f, denom %f, r %f", numer, denom, r);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000052 if (r == 0) { // catch underflow if numer <<<< denom
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 return 0;
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000054 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000055 *ratio = r;
56 return 1;
57}
58
Mike Reed1e259cd2018-06-21 10:51:35 -040059// Just returns its argument, but makes it easy to set a break-point to know when
60// SkFindUnitQuadRoots is going to return 0 (an error).
61static int return_check_zero(int value) {
62 if (value == 0) {
63 return 0;
64 }
65 return value;
66}
67
reed@android.com8a1c16f2008-12-17 15:59:43 +000068/** From Numerical Recipes in C.
69
70 Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C])
71 x1 = Q / A
72 x2 = C / Q
73*/
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000074int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 SkASSERT(roots);
76
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000077 if (A == 0) {
Mike Reed1e259cd2018-06-21 10:51:35 -040078 return return_check_zero(valid_unit_divide(-C, B, roots));
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000079 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
81 SkScalar* r = roots;
82
Mike Reed1e259cd2018-06-21 10:51:35 -040083 // use doubles so we don't overflow temporarily trying to compute R
84 double dr = (double)B * B - 4 * (double)A * C;
85 if (dr < 0) {
86 return return_check_zero(0);
reed@android.com15161622010-03-08 17:44:42 +000087 }
Mike Reed1e259cd2018-06-21 10:51:35 -040088 dr = sqrt(dr);
89 SkScalar R = SkDoubleToScalar(dr);
90 if (!SkScalarIsFinite(R)) {
91 return return_check_zero(0);
92 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
94 SkScalar Q = (B < 0) ? -(B-R)/2 : -(B+R)/2;
95 r += valid_unit_divide(Q, A, r);
96 r += valid_unit_divide(C, Q, r);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +000097 if (r - roots == 2) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -040098 if (roots[0] > roots[1]) {
99 using std::swap;
100 swap(roots[0], roots[1]);
101 } else if (roots[0] == roots[1]) { // nearly-equal?
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 r -= 1; // skip the double root
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400103 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 }
Mike Reed1e259cd2018-06-21 10:51:35 -0400105 return return_check_zero((int)(r - roots));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106}
107
reed@google.com8f4d2302013-12-17 16:44:46 +0000108///////////////////////////////////////////////////////////////////////////////
109///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110
reed65cb2cd2015-03-19 10:18:47 -0700111void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, SkVector* tangent) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 SkASSERT(src);
113 SkASSERT(t >= 0 && t <= SK_Scalar1);
mtklein950e9862015-03-19 12:03:29 -0700114
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000115 if (pt) {
caryclark5ba2b962016-01-26 17:02:30 -0800116 *pt = SkEvalQuadAt(src, t);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000117 }
118 if (tangent) {
caryclark45398df2015-08-25 13:19:06 -0700119 *tangent = SkEvalQuadTangentAt(src, t);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000120 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000121}
122
reed65cb2cd2015-03-19 10:18:47 -0700123SkPoint SkEvalQuadAt(const SkPoint src[3], SkScalar t) {
caryclark5ba2b962016-01-26 17:02:30 -0800124 return to_point(SkQuadCoeff(src).eval(t));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000125}
126
reed40b7dd52015-03-20 06:01:08 -0700127SkVector SkEvalQuadTangentAt(const SkPoint src[3], SkScalar t) {
caryclark45398df2015-08-25 13:19:06 -0700128 // The derivative equation is 2(b - a +(a - 2b +c)t). This returns a
129 // zero tangent vector when t is 0 or 1, and the control point is equal
130 // to the end point. In this case, use the quad end points to compute the tangent.
131 if ((t == 0 && src[0] == src[1]) || (t == 1 && src[1] == src[2])) {
132 return src[2] - src[0];
133 }
reed40b7dd52015-03-20 06:01:08 -0700134 SkASSERT(src);
135 SkASSERT(t >= 0 && t <= SK_Scalar1);
136
reedb6402032015-03-20 13:23:43 -0700137 Sk2s P0 = from_point(src[0]);
138 Sk2s P1 = from_point(src[1]);
139 Sk2s P2 = from_point(src[2]);
reed40b7dd52015-03-20 06:01:08 -0700140
reedb6402032015-03-20 13:23:43 -0700141 Sk2s B = P1 - P0;
142 Sk2s A = P2 - P1 - B;
143 Sk2s T = A * Sk2s(t) + B;
reed40b7dd52015-03-20 06:01:08 -0700144
reedb6402032015-03-20 13:23:43 -0700145 return to_vector(T + T);
reed40b7dd52015-03-20 06:01:08 -0700146}
147
reed40b7dd52015-03-20 06:01:08 -0700148static inline Sk2s interp(const Sk2s& v0, const Sk2s& v1, const Sk2s& t) {
149 return v0 + (v1 - v0) * t;
150}
151
reedc08330f2015-03-26 07:26:08 -0700152void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t) {
reed40b7dd52015-03-20 06:01:08 -0700153 SkASSERT(t > 0 && t < SK_Scalar1);
154
reedb6402032015-03-20 13:23:43 -0700155 Sk2s p0 = from_point(src[0]);
156 Sk2s p1 = from_point(src[1]);
157 Sk2s p2 = from_point(src[2]);
reedce6acc92015-03-20 13:46:08 -0700158 Sk2s tt(t);
mtkleinc9adb052015-03-30 10:50:27 -0700159
reed40b7dd52015-03-20 06:01:08 -0700160 Sk2s p01 = interp(p0, p1, tt);
161 Sk2s p12 = interp(p1, p2, tt);
162
reedb6402032015-03-20 13:23:43 -0700163 dst[0] = to_point(p0);
164 dst[1] = to_point(p01);
165 dst[2] = to_point(interp(p01, p12, tt));
166 dst[3] = to_point(p12);
167 dst[4] = to_point(p2);
reed40b7dd52015-03-20 06:01:08 -0700168}
169
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000170void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]) {
caryclarkb6474dd2016-01-19 08:07:49 -0800171 SkChopQuadAt(src, dst, 0.5f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000172}
173
174/** Quad'(t) = At + B, where
175 A = 2(a - 2b + c)
176 B = 2(b - a)
177 Solve for t, only if it fits between 0 < t < 1
178*/
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000179int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValue[1]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000180 /* At + B == 0
181 t = -B / A
182 */
reed@android.com8a1c16f2008-12-17 15:59:43 +0000183 return valid_unit_divide(a - b, a - b - b + c, tValue);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000184}
185
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000186static inline void flatten_double_quad_extrema(SkScalar coords[14]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000187 coords[2] = coords[6] = coords[4];
188}
189
reed@android.com8a1c16f2008-12-17 15:59:43 +0000190/* Returns 0 for 1 quad, and 1 for two quads, either way the answer is
reed@android.com77f0ef72009-11-17 18:47:52 +0000191 stored in dst[]. Guarantees that the 1/2 quads will be monotonic.
192 */
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000193int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000194 SkASSERT(src);
195 SkASSERT(dst);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000196
reed@android.com8a1c16f2008-12-17 15:59:43 +0000197 SkScalar a = src[0].fY;
198 SkScalar b = src[1].fY;
199 SkScalar c = src[2].fY;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000200
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000201 if (is_not_monotonic(a, b, c)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000202 SkScalar tValue;
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000203 if (valid_unit_divide(a - b, a - b - b + c, &tValue)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000204 SkChopQuadAt(src, dst, tValue);
205 flatten_double_quad_extrema(&dst[0].fY);
206 return 1;
207 }
208 // if we get here, we need to force dst to be monotonic, even though
209 // we couldn't compute a unit_divide value (probably underflow).
210 b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c;
211 }
212 dst[0].set(src[0].fX, a);
213 dst[1].set(src[1].fX, b);
214 dst[2].set(src[2].fX, c);
215 return 0;
216}
217
reed@android.com77f0ef72009-11-17 18:47:52 +0000218/* Returns 0 for 1 quad, and 1 for two quads, either way the answer is
219 stored in dst[]. Guarantees that the 1/2 quads will be monotonic.
220 */
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000221int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]) {
reed@android.com77f0ef72009-11-17 18:47:52 +0000222 SkASSERT(src);
223 SkASSERT(dst);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000224
reed@android.com77f0ef72009-11-17 18:47:52 +0000225 SkScalar a = src[0].fX;
226 SkScalar b = src[1].fX;
227 SkScalar c = src[2].fX;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000228
reed@android.com77f0ef72009-11-17 18:47:52 +0000229 if (is_not_monotonic(a, b, c)) {
230 SkScalar tValue;
231 if (valid_unit_divide(a - b, a - b - b + c, &tValue)) {
232 SkChopQuadAt(src, dst, tValue);
233 flatten_double_quad_extrema(&dst[0].fX);
234 return 1;
235 }
236 // if we get here, we need to force dst to be monotonic, even though
237 // we couldn't compute a unit_divide value (probably underflow).
238 b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c;
239 }
240 dst[0].set(a, src[0].fY);
241 dst[1].set(b, src[1].fY);
242 dst[2].set(c, src[2].fY);
243 return 0;
244}
245
reed@android.com8a1c16f2008-12-17 15:59:43 +0000246// F(t) = a (1 - t) ^ 2 + 2 b t (1 - t) + c t ^ 2
247// F'(t) = 2 (b - a) + 2 (a - 2b + c) t
248// F''(t) = 2 (a - 2b + c)
249//
250// A = 2 (b - a)
251// B = 2 (a - 2b + c)
252//
253// Maximum curvature for a quadratic means solving
254// Fx' Fx'' + Fy' Fy'' = 0
255//
256// t = - (Ax Bx + Ay By) / (Bx ^ 2 + By ^ 2)
257//
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000258SkScalar SkFindQuadMaxCurvature(const SkPoint src[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000259 SkScalar Ax = src[1].fX - src[0].fX;
260 SkScalar Ay = src[1].fY - src[0].fY;
261 SkScalar Bx = src[0].fX - src[1].fX - src[1].fX + src[2].fX;
262 SkScalar By = src[0].fY - src[1].fY - src[1].fY + src[2].fY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000263
Chris Dalton1d474dd2018-07-24 01:08:31 -0600264 SkScalar numer = -(Ax * Bx + Ay * By);
265 SkScalar denom = Bx * Bx + By * By;
266 if (denom < 0) {
267 numer = -numer;
268 denom = -denom;
269 }
270 if (numer <= 0) {
271 return 0;
272 }
273 if (numer >= denom) { // Also catches denom=0.
274 return 1;
275 }
276 SkScalar t = numer / denom;
Chris Daltonce038dc2018-09-14 14:14:49 -0600277 SkASSERT((0 <= t && t < 1) || SkScalarIsNaN(t));
egdaniel@google.com5383a752013-07-12 20:15:34 +0000278 return t;
279}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000280
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000281int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000282 SkScalar t = SkFindQuadMaxCurvature(src);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600283 if (t == 0 || t == 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000284 memcpy(dst, src, 3 * sizeof(SkPoint));
285 return 1;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000286 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000287 SkChopQuadAt(src, dst, t);
288 return 2;
289 }
290}
291
reed@google.com6fc321a2011-07-27 13:54:36 +0000292void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]) {
reeddaee7ea2015-03-26 20:22:33 -0700293 Sk2s scale(SkDoubleToScalar(2.0 / 3.0));
294 Sk2s s0 = from_point(src[0]);
295 Sk2s s1 = from_point(src[1]);
296 Sk2s s2 = from_point(src[2]);
297
Chris Daltona44436f2018-08-08 11:28:12 -0600298 dst[0] = to_point(s0);
reeddaee7ea2015-03-26 20:22:33 -0700299 dst[1] = to_point(s0 + (s1 - s0) * scale);
300 dst[2] = to_point(s2 + (s1 - s2) * scale);
Chris Daltona44436f2018-08-08 11:28:12 -0600301 dst[3] = to_point(s2);
reed@android.com945a1392010-02-05 20:41:02 +0000302}
303
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000304//////////////////////////////////////////////////////////////////////////////
305///// CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS /////
306//////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +0000307
caryclark5ba2b962016-01-26 17:02:30 -0800308static SkVector eval_cubic_derivative(const SkPoint src[4], SkScalar t) {
309 SkQuadCoeff coeff;
310 Sk2s P0 = from_point(src[0]);
311 Sk2s P1 = from_point(src[1]);
312 Sk2s P2 = from_point(src[2]);
313 Sk2s P3 = from_point(src[3]);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000314
caryclark5ba2b962016-01-26 17:02:30 -0800315 coeff.fA = P3 + Sk2s(3) * (P1 - P2) - P0;
316 coeff.fB = times_2(P2 - times_2(P1) + P0);
317 coeff.fC = P1 - P0;
318 return to_vector(coeff.eval(t));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000319}
320
caryclark5ba2b962016-01-26 17:02:30 -0800321static SkVector eval_cubic_2ndDerivative(const SkPoint src[4], SkScalar t) {
322 Sk2s P0 = from_point(src[0]);
323 Sk2s P1 = from_point(src[1]);
324 Sk2s P2 = from_point(src[2]);
325 Sk2s P3 = from_point(src[3]);
326 Sk2s A = P3 + Sk2s(3) * (P1 - P2) - P0;
327 Sk2s B = P2 - times_2(P1) + P0;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000328
caryclark5ba2b962016-01-26 17:02:30 -0800329 return to_vector(A * Sk2s(t) + B);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000330}
331
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000332void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* loc,
333 SkVector* tangent, SkVector* curvature) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000334 SkASSERT(src);
335 SkASSERT(t >= 0 && t <= SK_Scalar1);
336
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000337 if (loc) {
caryclark5ba2b962016-01-26 17:02:30 -0800338 *loc = to_point(SkCubicCoeff(src).eval(t));
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000339 }
340 if (tangent) {
caryclark45398df2015-08-25 13:19:06 -0700341 // The derivative equation returns a zero tangent vector when t is 0 or 1, and the
342 // adjacent control point is equal to the end point. In this case, use the
343 // next control point or the end points to compute the tangent.
344 if ((t == 0 && src[0] == src[1]) || (t == 1 && src[2] == src[3])) {
345 if (t == 0) {
346 *tangent = src[2] - src[0];
347 } else {
348 *tangent = src[3] - src[1];
349 }
350 if (!tangent->fX && !tangent->fY) {
351 *tangent = src[3] - src[0];
352 }
353 } else {
caryclark5ba2b962016-01-26 17:02:30 -0800354 *tangent = eval_cubic_derivative(src, t);
caryclark45398df2015-08-25 13:19:06 -0700355 }
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000356 }
357 if (curvature) {
caryclark5ba2b962016-01-26 17:02:30 -0800358 *curvature = eval_cubic_2ndDerivative(src, t);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000359 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000360}
361
362/** Cubic'(t) = At^2 + Bt + C, where
363 A = 3(-a + 3(b - c) + d)
364 B = 6(a - 2b + c)
365 C = 3(b - a)
366 Solve for t, keeping only those that fit betwee 0 < t < 1
367*/
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000368int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d,
369 SkScalar tValues[2]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000370 // we divide A,B,C by 3 to simplify
371 SkScalar A = d - a + 3*(b - c);
372 SkScalar B = 2*(a - b - b + c);
373 SkScalar C = b - a;
374
375 return SkFindUnitQuadRoots(A, B, C, tValues);
376}
377
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000378void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000379 SkASSERT(t > 0 && t < SK_Scalar1);
380
reed6b9ef902015-03-24 19:24:34 -0700381 Sk2s p0 = from_point(src[0]);
382 Sk2s p1 = from_point(src[1]);
383 Sk2s p2 = from_point(src[2]);
384 Sk2s p3 = from_point(src[3]);
385 Sk2s tt(t);
386
387 Sk2s ab = interp(p0, p1, tt);
388 Sk2s bc = interp(p1, p2, tt);
389 Sk2s cd = interp(p2, p3, tt);
390 Sk2s abc = interp(ab, bc, tt);
391 Sk2s bcd = interp(bc, cd, tt);
392 Sk2s abcd = interp(abc, bcd, tt);
mtkleinc9adb052015-03-30 10:50:27 -0700393
Chris Daltona44436f2018-08-08 11:28:12 -0600394 dst[0] = to_point(p0);
reed6b9ef902015-03-24 19:24:34 -0700395 dst[1] = to_point(ab);
396 dst[2] = to_point(abc);
397 dst[3] = to_point(abcd);
398 dst[4] = to_point(bcd);
399 dst[5] = to_point(cd);
Chris Daltona44436f2018-08-08 11:28:12 -0600400 dst[6] = to_point(p3);
reed6b9ef902015-03-24 19:24:34 -0700401}
402
reed@android.coma9640282009-08-28 20:06:54 +0000403/* http://code.google.com/p/skia/issues/detail?id=32
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000404
reed@android.coma9640282009-08-28 20:06:54 +0000405 This test code would fail when we didn't check the return result of
406 valid_unit_divide in SkChopCubicAt(... tValues[], int roots). The reason is
407 that after the first chop, the parameters to valid_unit_divide are equal
408 (thanks to finite float precision and rounding in the subtracts). Thus
409 even though the 2nd tValue looks < 1.0, after we renormalize it, we end
410 up with 1.0, hence the need to check and just return the last cubic as
411 a degenerate clump of 4 points in the sampe place.
412
413 static void test_cubic() {
414 SkPoint src[4] = {
415 { 556.25000, 523.03003 },
416 { 556.23999, 522.96002 },
417 { 556.21997, 522.89001 },
418 { 556.21997, 522.82001 }
419 };
420 SkPoint dst[10];
421 SkScalar tval[] = { 0.33333334f, 0.99999994f };
422 SkChopCubicAt(src, dst, tval, 2);
423 }
424 */
425
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000426void SkChopCubicAt(const SkPoint src[4], SkPoint dst[],
427 const SkScalar tValues[], int roots) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000428#ifdef SK_DEBUG
429 {
430 for (int i = 0; i < roots - 1; i++)
431 {
Chris Dalton1d474dd2018-07-24 01:08:31 -0600432 SkASSERT(0 < tValues[i] && tValues[i] < 1);
433 SkASSERT(0 < tValues[i+1] && tValues[i+1] < 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000434 SkASSERT(tValues[i] < tValues[i+1]);
435 }
436 }
437#endif
438
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000439 if (dst) {
440 if (roots == 0) { // nothing to chop
reed@android.com8a1c16f2008-12-17 15:59:43 +0000441 memcpy(dst, src, 4*sizeof(SkPoint));
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000442 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000443 SkScalar t = tValues[0];
444 SkPoint tmp[4];
445
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000446 for (int i = 0; i < roots; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000447 SkChopCubicAt(src, dst, t);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000448 if (i == roots - 1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000449 break;
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000450 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000451
reed@android.com8a1c16f2008-12-17 15:59:43 +0000452 dst += 3;
reed@android.coma9640282009-08-28 20:06:54 +0000453 // have src point to the remaining cubic (after the chop)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000454 memcpy(tmp, dst, 4 * sizeof(SkPoint));
455 src = tmp;
reed@android.coma9640282009-08-28 20:06:54 +0000456
457 // watch out in case the renormalized t isn't in range
458 if (!valid_unit_divide(tValues[i+1] - tValues[i],
459 SK_Scalar1 - tValues[i], &t)) {
460 // if we can't, just create a degenerate cubic
461 dst[4] = dst[5] = dst[6] = src[3];
462 break;
463 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000464 }
465 }
466 }
467}
468
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000469void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]) {
reedc08330f2015-03-26 07:26:08 -0700470 SkChopCubicAt(src, dst, 0.5f);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000471}
472
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000473static void flatten_double_cubic_extrema(SkScalar coords[14]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000474 coords[4] = coords[8] = coords[6];
475}
476
477/** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that
commit-bot@chromium.orgdef64682014-02-21 19:49:10 +0000478 the resulting beziers are monotonic in Y. This is called by the scan
479 converter. Depending on what is returned, dst[] is treated as follows:
reed@android.com8a1c16f2008-12-17 15:59:43 +0000480 0 dst[0..3] is the original cubic
481 1 dst[0..3] and dst[3..6] are the two new cubics
482 2 dst[0..3], dst[3..6], dst[6..9] are the three new cubics
483 If dst == null, it is ignored and only the count is returned.
484*/
reed@android.combb135862009-11-18 13:47:40 +0000485int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000486 SkScalar tValues[2];
reed@android.combb135862009-11-18 13:47:40 +0000487 int roots = SkFindCubicExtrema(src[0].fY, src[1].fY, src[2].fY,
488 src[3].fY, tValues);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000489
reed@android.com8a1c16f2008-12-17 15:59:43 +0000490 SkChopCubicAt(src, dst, tValues, roots);
reed@android.combb135862009-11-18 13:47:40 +0000491 if (dst && roots > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000492 // we do some cleanup to ensure our Y extrema are flat
493 flatten_double_cubic_extrema(&dst[0].fY);
reed@android.combb135862009-11-18 13:47:40 +0000494 if (roots == 2) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000495 flatten_double_cubic_extrema(&dst[3].fY);
reed@android.combb135862009-11-18 13:47:40 +0000496 }
497 }
498 return roots;
499}
500
501int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]) {
502 SkScalar tValues[2];
503 int roots = SkFindCubicExtrema(src[0].fX, src[1].fX, src[2].fX,
504 src[3].fX, tValues);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000505
reed@android.combb135862009-11-18 13:47:40 +0000506 SkChopCubicAt(src, dst, tValues, roots);
507 if (dst && roots > 0) {
508 // we do some cleanup to ensure our Y extrema are flat
509 flatten_double_cubic_extrema(&dst[0].fX);
510 if (roots == 2) {
511 flatten_double_cubic_extrema(&dst[3].fX);
512 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000513 }
514 return roots;
515}
516
517/** http://www.faculty.idc.ac.il/arik/quality/appendixA.html
518
519 Inflection means that curvature is zero.
520 Curvature is [F' x F''] / [F'^3]
521 So we solve F'x X F''y - F'y X F''y == 0
522 After some canceling of the cubic term, we get
523 A = b - a
524 B = c - 2b + a
525 C = d - 3c + 3b - a
526 (BxCy - ByCx)t^2 + (AxCy - AyCx)t + AxBy - AyBx == 0
527*/
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000528int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000529 SkScalar Ax = src[1].fX - src[0].fX;
530 SkScalar Ay = src[1].fY - src[0].fY;
531 SkScalar Bx = src[2].fX - 2 * src[1].fX + src[0].fX;
532 SkScalar By = src[2].fY - 2 * src[1].fY + src[0].fY;
533 SkScalar Cx = src[3].fX + 3 * (src[1].fX - src[2].fX) - src[0].fX;
534 SkScalar Cy = src[3].fY + 3 * (src[1].fY - src[2].fY) - src[0].fY;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000535
commit-bot@chromium.orgdef64682014-02-21 19:49:10 +0000536 return SkFindUnitQuadRoots(Bx*Cy - By*Cx,
537 Ax*Cy - Ay*Cx,
538 Ax*By - Ay*Bx,
539 tValues);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000540}
541
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000542int SkChopCubicAtInflections(const SkPoint src[], SkPoint dst[10]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000543 SkScalar tValues[2];
544 int count = SkFindCubicInflections(src, tValues);
545
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000546 if (dst) {
547 if (count == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000548 memcpy(dst, src, 4 * sizeof(SkPoint));
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000549 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000550 SkChopCubicAt(src, dst, tValues, count);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000551 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000552 }
553 return count + 1;
554}
555
caryclark8dd31cf2014-12-12 09:11:23 -0800556// Assumes the third component of points is 1.
557// Calcs p0 . (p1 x p2)
Chris Dalton390f6cd2017-06-12 11:22:54 -0600558static double calc_dot_cross_cubic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
Chris Daltonfc31be42017-11-08 17:04:47 -0700559 const double xComp = (double) p0.fX * ((double) p1.fY - (double) p2.fY);
560 const double yComp = (double) p0.fY * ((double) p2.fX - (double) p1.fX);
Chris Dalton390f6cd2017-06-12 11:22:54 -0600561 const double wComp = (double) p1.fX * (double) p2.fY - (double) p1.fY * (double) p2.fX;
caryclark8dd31cf2014-12-12 09:11:23 -0800562 return (xComp + yComp + wComp);
563}
564
Chris Dalton6fdbf612018-04-15 21:58:19 -0600565// Returns a positive power of 2 that, when multiplied by n, and excepting the two edge cases listed
566// below, shifts the exponent of n to yield a magnitude somewhere inside [1..2).
567// Returns 2^1023 if abs(n) < 2^-1022 (including 0).
568// Returns NaN if n is Inf or NaN.
569inline static double previous_inverse_pow2(double n) {
570 uint64_t bits;
571 memcpy(&bits, &n, sizeof(double));
572 bits = ((1023llu*2 << 52) + ((1llu << 52) - 1)) - bits; // exp=-exp
573 bits &= (0x7ffllu) << 52; // mantissa=1.0, sign=0
574 memcpy(&n, &bits, sizeof(double));
575 return n;
caryclark8dd31cf2014-12-12 09:11:23 -0800576}
577
Chris Dalton6fdbf612018-04-15 21:58:19 -0600578inline static void write_cubic_inflection_roots(double t0, double s0, double t1, double s1,
579 double* t, double* s) {
580 t[0] = t0;
581 s[0] = s0;
Chris Dalton4dbdc672017-10-31 12:35:54 -0600582
Chris Daltonfebbffa2017-06-08 13:12:02 -0600583 // This copysign/abs business orients the implicit function so positive values are always on the
584 // "left" side of the curve.
Chris Dalton6fdbf612018-04-15 21:58:19 -0600585 t[1] = -copysign(t1, t1 * s1);
586 s[1] = -fabs(s1);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600587
588 // Ensure t[0]/s[0] <= t[1]/s[1] (s[1] is negative from above).
Chris Dalton390f6cd2017-06-12 11:22:54 -0600589 if (copysign(s[1], s[0]) * t[0] > -fabs(s[0]) * t[1]) {
Ben Wagnerf08d1d02018-06-18 15:11:00 -0400590 using std::swap;
591 swap(t[0], t[1]);
592 swap(s[0], s[1]);
Chris Daltonfebbffa2017-06-08 13:12:02 -0600593 }
594}
595
Chris Dalton6fdbf612018-04-15 21:58:19 -0600596SkCubicType SkClassifyCubic(const SkPoint P[4], double t[2], double s[2], double d[4]) {
597 // Find the cubic's inflection function, I = [T^3 -3T^2 3T -1] dot D. (D0 will always be 0
598 // for integral cubics.)
599 //
600 // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware",
601 // 4.2 Curve Categorization:
602 //
603 // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
604 double A1 = calc_dot_cross_cubic(P[0], P[3], P[2]);
605 double A2 = calc_dot_cross_cubic(P[1], P[0], P[3]);
606 double A3 = calc_dot_cross_cubic(P[2], P[1], P[0]);
607
608 double D3 = 3 * A3;
609 double D2 = D3 - A2;
610 double D1 = D2 - A2 + A1;
611
612 // Shift the exponents in D so the largest magnitude falls somewhere in 1..2. This protects us
613 // from overflow down the road while solving for roots and KLM functionals.
614 double Dmax = std::max(std::max(fabs(D1), fabs(D2)), fabs(D3));
615 double norm = previous_inverse_pow2(Dmax);
616 D1 *= norm;
617 D2 *= norm;
618 D3 *= norm;
619
620 if (d) {
621 d[3] = D3;
622 d[2] = D2;
623 d[1] = D1;
624 d[0] = 0;
625 }
626
627 // Now use the inflection function to classify the cubic.
628 //
629 // See "Resolution Independent Curve Rendering using Programmable Graphics Hardware",
630 // 4.4 Integral Cubics:
631 //
632 // https://www.microsoft.com/en-us/research/wp-content/uploads/2005/01/p1000-loop.pdf
633 if (0 != D1) {
634 double discr = 3*D2*D2 - 4*D1*D3;
635 if (discr > 0) { // Serpentine.
Chris Dalton29011a22017-09-28 12:08:33 -0600636 if (t && s) {
Chris Dalton6fdbf612018-04-15 21:58:19 -0600637 double q = 3*D2 + copysign(sqrt(3*discr), D2);
638 write_cubic_inflection_roots(q, 6*D1, 2*D3, q, t, s);
Chris Dalton29011a22017-09-28 12:08:33 -0600639 }
Chris Dalton6fdbf612018-04-15 21:58:19 -0600640 return SkCubicType::kSerpentine;
641 } else if (discr < 0) { // Loop.
642 if (t && s) {
643 double q = D2 + copysign(sqrt(-discr), D2);
644 write_cubic_inflection_roots(q, 2*D1, 2*(D2*D2 - D3*D1), D1*q, t, s);
645 }
646 return SkCubicType::kLoop;
647 } else { // Cusp.
648 if (t && s) {
649 write_cubic_inflection_roots(D2, 2*D1, D2, 2*D1, t, s);
650 }
651 return SkCubicType::kLocalCusp;
Chris Dalton29011a22017-09-28 12:08:33 -0600652 }
Chris Daltonfebbffa2017-06-08 13:12:02 -0600653 } else {
Chris Dalton6fdbf612018-04-15 21:58:19 -0600654 if (0 != D2) { // Cusp at T=infinity.
655 if (t && s) {
656 write_cubic_inflection_roots(D3, 3*D2, 1, 0, t, s); // T1=infinity.
657 }
658 return SkCubicType::kCuspAtInfinity;
659 } else { // Degenerate.
660 if (t && s) {
661 write_cubic_inflection_roots(1, 0, 1, 0, t, s); // T0=T1=infinity.
662 }
663 return 0 != D3 ? SkCubicType::kQuadratic : SkCubicType::kLineOrPoint;
Chris Daltonfebbffa2017-06-08 13:12:02 -0600664 }
665 }
666}
667
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000668template <typename T> void bubble_sort(T array[], int count) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000669 for (int i = count - 1; i > 0; --i)
670 for (int j = i; j > 0; --j)
671 if (array[j] < array[j-1])
672 {
673 T tmp(array[j]);
674 array[j] = array[j-1];
675 array[j-1] = tmp;
676 }
677}
678
reed@google.com087d5aa2012-02-29 20:59:24 +0000679/**
680 * Given an array and count, remove all pair-wise duplicates from the array,
681 * keeping the existing sorting, and return the new count
682 */
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000683static int collaps_duplicates(SkScalar array[], int count) {
reed@google.com087d5aa2012-02-29 20:59:24 +0000684 for (int n = count; n > 1; --n) {
685 if (array[0] == array[1]) {
686 for (int i = 1; i < n; ++i) {
687 array[i - 1] = array[i];
688 }
689 count -= 1;
690 } else {
691 array += 1;
692 }
693 }
694 return count;
695}
696
697#ifdef SK_DEBUG
698
699#define TEST_COLLAPS_ENTRY(array) array, SK_ARRAY_COUNT(array)
700
701static void test_collaps_duplicates() {
702 static bool gOnce;
703 if (gOnce) { return; }
704 gOnce = true;
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000705 const SkScalar src0[] = { 0 };
706 const SkScalar src1[] = { 0, 0 };
707 const SkScalar src2[] = { 0, 1 };
708 const SkScalar src3[] = { 0, 0, 0 };
709 const SkScalar src4[] = { 0, 0, 1 };
710 const SkScalar src5[] = { 0, 1, 1 };
711 const SkScalar src6[] = { 0, 1, 2 };
reed@google.com087d5aa2012-02-29 20:59:24 +0000712 const struct {
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000713 const SkScalar* fData;
reed@google.com087d5aa2012-02-29 20:59:24 +0000714 int fCount;
715 int fCollapsedCount;
716 } data[] = {
717 { TEST_COLLAPS_ENTRY(src0), 1 },
718 { TEST_COLLAPS_ENTRY(src1), 1 },
719 { TEST_COLLAPS_ENTRY(src2), 2 },
720 { TEST_COLLAPS_ENTRY(src3), 1 },
721 { TEST_COLLAPS_ENTRY(src4), 2 },
722 { TEST_COLLAPS_ENTRY(src5), 2 },
723 { TEST_COLLAPS_ENTRY(src6), 3 },
724 };
725 for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000726 SkScalar dst[3];
reed@google.com087d5aa2012-02-29 20:59:24 +0000727 memcpy(dst, data[i].fData, data[i].fCount * sizeof(dst[0]));
728 int count = collaps_duplicates(dst, data[i].fCount);
729 SkASSERT(data[i].fCollapsedCount == count);
730 for (int j = 1; j < count; ++j) {
731 SkASSERT(dst[j-1] < dst[j]);
732 }
733 }
734}
735#endif
736
reed@google.com3c128402013-12-16 14:17:40 +0000737static SkScalar SkScalarCubeRoot(SkScalar x) {
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000738 return SkScalarPow(x, 0.3333333f);
reed@google.com3c128402013-12-16 14:17:40 +0000739}
740
reed@android.com8a1c16f2008-12-17 15:59:43 +0000741/* Solve coeff(t) == 0, returning the number of roots that
742 lie withing 0 < t < 1.
743 coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3]
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000744
reed@google.com087d5aa2012-02-29 20:59:24 +0000745 Eliminates repeated roots (so that all tValues are distinct, and are always
746 in increasing order.
reed@android.com8a1c16f2008-12-17 15:59:43 +0000747*/
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000748static int solve_cubic_poly(const SkScalar coeff[4], SkScalar tValues[3]) {
749 if (SkScalarNearlyZero(coeff[0])) { // we're just a quadratic
reed@android.com8a1c16f2008-12-17 15:59:43 +0000750 return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues);
751 }
752
reed@google.com3c128402013-12-16 14:17:40 +0000753 SkScalar a, b, c, Q, R;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000754
755 {
756 SkASSERT(coeff[0] != 0);
757
reed@google.com3c128402013-12-16 14:17:40 +0000758 SkScalar inva = SkScalarInvert(coeff[0]);
759 a = coeff[1] * inva;
760 b = coeff[2] * inva;
761 c = coeff[3] * inva;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000762 }
reed@google.com3c128402013-12-16 14:17:40 +0000763 Q = (a*a - b*3) / 9;
764 R = (2*a*a*a - 9*a*b + 27*c) / 54;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000765
reed@google.com3c128402013-12-16 14:17:40 +0000766 SkScalar Q3 = Q * Q * Q;
767 SkScalar R2MinusQ3 = R * R - Q3;
768 SkScalar adiv3 = a / 3;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000769
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000770 if (R2MinusQ3 < 0) { // we have 3 real roots
caryclark93ca8842016-05-27 05:24:37 -0700771 // the divide/root can, due to finite precisions, be slightly outside of -1...1
Brian Osmanaba642c2020-02-06 12:52:25 -0500772 SkScalar theta = SkScalarACos(SkTPin(R / SkScalarSqrt(Q3), -1.0f, 1.0f));
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000773 SkScalar neg2RootQ = -2 * SkScalarSqrt(Q);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000774
Brian Osmanaba642c2020-02-06 12:52:25 -0500775 tValues[0] = SkTPin(neg2RootQ * SkScalarCos(theta/3) - adiv3, 0.0f, 1.0f);
776 tValues[1] = SkTPin(neg2RootQ * SkScalarCos((theta + 2*SK_ScalarPI)/3) - adiv3, 0.0f, 1.0f);
777 tValues[2] = SkTPin(neg2RootQ * SkScalarCos((theta - 2*SK_ScalarPI)/3) - adiv3, 0.0f, 1.0f);
reed@google.com087d5aa2012-02-29 20:59:24 +0000778 SkDEBUGCODE(test_collaps_duplicates();)
779
reed@android.com8a1c16f2008-12-17 15:59:43 +0000780 // now sort the roots
Chris Dalton1d474dd2018-07-24 01:08:31 -0600781 bubble_sort(tValues, 3);
782 return collaps_duplicates(tValues, 3);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000783 } else { // we have 1 real root
reed@google.com3c128402013-12-16 14:17:40 +0000784 SkScalar A = SkScalarAbs(R) + SkScalarSqrt(R2MinusQ3);
785 A = SkScalarCubeRoot(A);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000786 if (R > 0) {
reed@google.com3c128402013-12-16 14:17:40 +0000787 A = -A;
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000788 }
789 if (A != 0) {
reed@google.com3c128402013-12-16 14:17:40 +0000790 A += Q / A;
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000791 }
Brian Osmanaba642c2020-02-06 12:52:25 -0500792 tValues[0] = SkTPin(A - adiv3, 0.0f, 1.0f);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600793 return 1;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000794 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000795}
796
797/* Looking for F' dot F'' == 0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000798
reed@android.com8a1c16f2008-12-17 15:59:43 +0000799 A = b - a
800 B = c - 2b + a
801 C = d - 3c + 3b - a
802
803 F' = 3Ct^2 + 6Bt + 3A
804 F'' = 6Ct + 6B
805
806 F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
807*/
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000808static void formulate_F1DotF2(const SkScalar src[], SkScalar coeff[4]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000809 SkScalar a = src[2] - src[0];
810 SkScalar b = src[4] - 2 * src[2] + src[0];
811 SkScalar c = src[6] + 3 * (src[2] - src[4]) - src[0];
812
reed@google.com3c128402013-12-16 14:17:40 +0000813 coeff[0] = c * c;
814 coeff[1] = 3 * b * c;
815 coeff[2] = 2 * b * b + c * a;
816 coeff[3] = a * b;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000817}
818
reed@android.com8a1c16f2008-12-17 15:59:43 +0000819/* Looking for F' dot F'' == 0
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000820
reed@android.com8a1c16f2008-12-17 15:59:43 +0000821 A = b - a
822 B = c - 2b + a
823 C = d - 3c + 3b - a
824
825 F' = 3Ct^2 + 6Bt + 3A
826 F'' = 6Ct + 6B
827
828 F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
829*/
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000830int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]) {
reed@google.com3c128402013-12-16 14:17:40 +0000831 SkScalar coeffX[4], coeffY[4];
832 int i;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000833
834 formulate_F1DotF2(&src[0].fX, coeffX);
835 formulate_F1DotF2(&src[0].fY, coeffY);
836
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000837 for (i = 0; i < 4; i++) {
reed@google.com3c128402013-12-16 14:17:40 +0000838 coeffX[i] += coeffY[i];
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000839 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000840
Chris Dalton1d474dd2018-07-24 01:08:31 -0600841 int numRoots = solve_cubic_poly(coeffX, tValues);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000842 // now remove extrema where the curvature is zero (mins)
843 // !!!! need a test for this !!!!
Chris Dalton1d474dd2018-07-24 01:08:31 -0600844 return numRoots;
reed@android.com8a1c16f2008-12-17 15:59:43 +0000845}
846
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000847int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13],
848 SkScalar tValues[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000849 SkScalar t_storage[3];
850
halcanary96fcdcc2015-08-27 07:41:13 -0700851 if (tValues == nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000852 tValues = t_storage;
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000853 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000854
Chris Dalton1d474dd2018-07-24 01:08:31 -0600855 SkScalar roots[3];
856 int rootCount = SkFindCubicMaxCurvature(src, roots);
857
858 // Throw out values not inside 0..1.
859 int count = 0;
860 for (int i = 0; i < rootCount; ++i) {
861 if (0 < roots[i] && roots[i] < 1) {
862 tValues[count++] = roots[i];
863 }
864 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000865
egdaniel@google.com5383a752013-07-12 20:15:34 +0000866 if (dst) {
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000867 if (count == 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000868 memcpy(dst, src, 4 * sizeof(SkPoint));
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000869 } else {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000870 SkChopCubicAt(src, dst, tValues, count);
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000871 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000872 }
873 return count + 1;
874}
875
Cary Clarkdb160012018-08-31 15:07:51 -0400876// Returns a constant proportional to the dimensions of the cubic.
877// Constant found through experimentation -- maybe there's a better way....
878static SkScalar calc_cubic_precision(const SkPoint src[4]) {
879 return (SkPointPriv::DistanceToSqd(src[1], src[0]) + SkPointPriv::DistanceToSqd(src[2], src[1])
880 + SkPointPriv::DistanceToSqd(src[3], src[2])) * 1e-8f;
881}
882
883// Returns true if both points src[testIndex], src[testIndex+1] are in the same half plane defined
884// by the line segment src[lineIndex], src[lineIndex+1].
885static bool on_same_side(const SkPoint src[4], int testIndex, int lineIndex) {
886 SkPoint origin = src[lineIndex];
887 SkVector line = src[lineIndex + 1] - origin;
888 SkScalar crosses[2];
889 for (int index = 0; index < 2; ++index) {
890 SkVector testLine = src[testIndex + index] - origin;
891 crosses[index] = line.cross(testLine);
892 }
893 return crosses[0] * crosses[1] >= 0;
894}
895
896// Return location (in t) of cubic cusp, if there is one.
897// Note that classify cubic code does not reliably return all cusp'd cubics, so
898// it is not called here.
899SkScalar SkFindCubicCusp(const SkPoint src[4]) {
900 // When the adjacent control point matches the end point, it behaves as if
901 // the cubic has a cusp: there's a point of max curvature where the derivative
902 // goes to zero. Ideally, this would be where t is zero or one, but math
903 // error makes not so. It is not uncommon to create cubics this way; skip them.
904 if (src[0] == src[1]) {
905 return -1;
906 }
907 if (src[2] == src[3]) {
908 return -1;
909 }
910 // Cubics only have a cusp if the line segments formed by the control and end points cross.
911 // Detect crossing if line ends are on opposite sides of plane formed by the other line.
912 if (on_same_side(src, 0, 2) || on_same_side(src, 2, 0)) {
913 return -1;
914 }
915 // Cubics may have multiple points of maximum curvature, although at most only
916 // one is a cusp.
917 SkScalar maxCurvature[3];
918 int roots = SkFindCubicMaxCurvature(src, maxCurvature);
919 for (int index = 0; index < roots; ++index) {
920 SkScalar testT = maxCurvature[index];
921 if (0 >= testT || testT >= 1) { // no need to consider max curvature on the end
922 continue;
923 }
924 // A cusp is at the max curvature, and also has a derivative close to zero.
925 // Choose the 'close to zero' meaning by comparing the derivative length
926 // with the overall cubic size.
927 SkVector dPt = eval_cubic_derivative(src, testT);
928 SkScalar dPtMagnitude = SkPointPriv::LengthSqd(dPt);
929 SkScalar precision = calc_cubic_precision(src);
930 if (dPtMagnitude < precision) {
931 // All three max curvature t values may be close to the cusp;
932 // return the first one.
933 return testT;
934 }
935 }
936 return -1;
937}
938
Mike Kleinc0bd9f92019-04-23 12:05:21 -0500939#include "src/pathops/SkPathOpsCubic.h"
reeddc308852015-04-30 07:47:13 -0700940
941typedef int (SkDCubic::*InterceptProc)(double intercept, double roots[3]) const;
942
943static bool cubic_dchop_at_intercept(const SkPoint src[4], SkScalar intercept, SkPoint dst[7],
944 InterceptProc method) {
945 SkDCubic cubic;
946 double roots[3];
947 int count = (cubic.set(src).*method)(intercept, roots);
948 if (count > 0) {
949 SkDCubicPair pair = cubic.chopAt(roots[0]);
950 for (int i = 0; i < 7; ++i) {
951 dst[i] = pair.pts[i].asSkPoint();
952 }
953 return true;
954 }
955 return false;
956}
957
958bool SkChopMonoCubicAtY(SkPoint src[4], SkScalar y, SkPoint dst[7]) {
959 return cubic_dchop_at_intercept(src, y, dst, &SkDCubic::horizontalIntersect);
960}
961
962bool SkChopMonoCubicAtX(SkPoint src[4], SkScalar x, SkPoint dst[7]) {
963 return cubic_dchop_at_intercept(src, x, dst, &SkDCubic::verticalIntersect);
964}
965
commit-bot@chromium.orgb39d5612014-02-21 12:17:34 +0000966///////////////////////////////////////////////////////////////////////////////
commit-bot@chromium.orgdef64682014-02-21 19:49:10 +0000967//
968// NURB representation for conics. Helpful explanations at:
969//
970// http://citeseerx.ist.psu.edu/viewdoc/
971// download?doi=10.1.1.44.5740&rep=rep1&type=ps
972// and
973// http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html
974//
reed@google.com17a2c912013-04-16 21:07:27 +0000975// F = (A (1 - t)^2 + C t^2 + 2 B (1 - t) t w)
976// ------------------------------------------
977// ((1 - t)^2 + t^2 + 2 (1 - t) t w)
978//
979// = {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0}
980// ------------------------------------------------
981// {t^2 (2 - 2 w), t (-2 + 2 w), 1}
982//
reed@google.com17a2c912013-04-16 21:07:27 +0000983
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +0000984// F' = 2 (C t (1 + t (-1 + w)) - A (-1 + t) (t (-1 + w) - w) + B (1 - 2 t) w)
985//
mike@reedtribe.org6862cba2013-05-08 01:55:49 +0000986// t^2 : (2 P0 - 2 P2 - 2 P0 w + 2 P2 w)
987// t^1 : (-2 P0 + 2 P2 + 4 P0 w - 4 P1 w)
988// t^0 : -2 P0 w + 2 P1 w
989//
990// We disregard magnitude, so we can freely ignore the denominator of F', and
991// divide the numerator by 2
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +0000992//
reed@google.com17a2c912013-04-16 21:07:27 +0000993// coeff[0] for t^2
mike@reedtribe.org6862cba2013-05-08 01:55:49 +0000994// coeff[1] for t^1
995// coeff[2] for t^0
reed@google.com17a2c912013-04-16 21:07:27 +0000996//
commit-bot@chromium.orgdef64682014-02-21 19:49:10 +0000997static void conic_deriv_coeff(const SkScalar src[],
998 SkScalar w,
999 SkScalar coeff[3]) {
mike@reedtribe.org6862cba2013-05-08 01:55:49 +00001000 const SkScalar P20 = src[4] - src[0];
1001 const SkScalar P10 = src[2] - src[0];
1002 const SkScalar wP10 = w * P10;
1003 coeff[0] = w * P20 - P20;
1004 coeff[1] = P20 - 2 * wP10;
1005 coeff[2] = wP10;
reed@google.com17a2c912013-04-16 21:07:27 +00001006}
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001007
mike@reedtribe.org6862cba2013-05-08 01:55:49 +00001008static bool conic_find_extrema(const SkScalar src[], SkScalar w, SkScalar* t) {
1009 SkScalar coeff[3];
1010 conic_deriv_coeff(src, w, coeff);
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001011
1012 SkScalar tValues[2];
1013 int roots = SkFindUnitQuadRoots(coeff[0], coeff[1], coeff[2], tValues);
1014 SkASSERT(0 == roots || 1 == roots);
skia.committer@gmail.com45fb8b62013-04-17 07:00:56 +00001015
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001016 if (1 == roots) {
1017 *t = tValues[0];
1018 return true;
1019 }
1020 return false;
1021}
reed@google.com17a2c912013-04-16 21:07:27 +00001022
commit-bot@chromium.orgd50d87a2014-01-28 14:36:52 +00001023// We only interpolate one dimension at a time (the first, at +0, +3, +6).
1024static void p3d_interp(const SkScalar src[7], SkScalar dst[7], SkScalar t) {
reed@google.com0d099552013-04-12 21:55:26 +00001025 SkScalar ab = SkScalarInterp(src[0], src[3], t);
1026 SkScalar bc = SkScalarInterp(src[3], src[6], t);
1027 dst[0] = ab;
1028 dst[3] = SkScalarInterp(ab, bc, t);
1029 dst[6] = bc;
1030}
1031
Cary Clarke4442cb2017-10-18 11:46:18 -04001032static void ratquad_mapTo3D(const SkPoint src[3], SkScalar w, SkPoint3 dst[3]) {
reed@google.com0d099552013-04-12 21:55:26 +00001033 dst[0].set(src[0].fX * 1, src[0].fY * 1, 1);
1034 dst[1].set(src[1].fX * w, src[1].fY * w, w);
1035 dst[2].set(src[2].fX * 1, src[2].fY * 1, 1);
1036}
1037
Cary Clarke4442cb2017-10-18 11:46:18 -04001038static SkPoint project_down(const SkPoint3& src) {
1039 return {src.fX / src.fZ, src.fY / src.fZ};
1040}
1041
caryclark414c4292016-09-26 11:03:54 -07001042// return false if infinity or NaN is generated; caller must check
1043bool SkConic::chopAt(SkScalar t, SkConic dst[2]) const {
Cary Clarke4442cb2017-10-18 11:46:18 -04001044 SkPoint3 tmp[3], tmp2[3];
reed@google.com0d099552013-04-12 21:55:26 +00001045
1046 ratquad_mapTo3D(fPts, fW, tmp);
skia.committer@gmail.com4bb50b22013-04-13 07:01:15 +00001047
reed@google.com0d099552013-04-12 21:55:26 +00001048 p3d_interp(&tmp[0].fX, &tmp2[0].fX, t);
1049 p3d_interp(&tmp[0].fY, &tmp2[0].fY, t);
1050 p3d_interp(&tmp[0].fZ, &tmp2[0].fZ, t);
skia.committer@gmail.com4bb50b22013-04-13 07:01:15 +00001051
reed@google.com0d099552013-04-12 21:55:26 +00001052 dst[0].fPts[0] = fPts[0];
Cary Clarke4442cb2017-10-18 11:46:18 -04001053 dst[0].fPts[1] = project_down(tmp2[0]);
1054 dst[0].fPts[2] = project_down(tmp2[1]); dst[1].fPts[0] = dst[0].fPts[2];
1055 dst[1].fPts[1] = project_down(tmp2[2]);
reed@google.com0d099552013-04-12 21:55:26 +00001056 dst[1].fPts[2] = fPts[2];
1057
mike@reedtribe.org4af62802013-04-13 10:51:51 +00001058 // to put in "standard form", where w0 and w2 are both 1, we compute the
1059 // new w1 as sqrt(w1*w1/w0*w2)
1060 // or
1061 // w1 /= sqrt(w0*w2)
1062 //
commit-bot@chromium.orgdef64682014-02-21 19:49:10 +00001063 // However, in our case, we know that for dst[0]:
1064 // w0 == 1, and for dst[1], w2 == 1
mike@reedtribe.org4af62802013-04-13 10:51:51 +00001065 //
1066 SkScalar root = SkScalarSqrt(tmp2[1].fZ);
1067 dst[0].fW = tmp2[0].fZ / root;
1068 dst[1].fW = tmp2[2].fZ / root;
caryclark414c4292016-09-26 11:03:54 -07001069 SkASSERT(sizeof(dst[0]) == sizeof(SkScalar) * 7);
1070 SkASSERT(0 == offsetof(SkConic, fPts[0].fX));
1071 return SkScalarsAreFinite(&dst[0].fPts[0].fX, 7 * 2);
reed@google.comc5187102013-04-12 19:11:10 +00001072}
mike@reedtribe.org8d551012013-04-14 02:40:50 +00001073
caryclarkb6474dd2016-01-19 08:07:49 -08001074void SkConic::chopAt(SkScalar t1, SkScalar t2, SkConic* dst) const {
1075 if (0 == t1 || 1 == t2) {
1076 if (0 == t1 && 1 == t2) {
1077 *dst = *this;
caryclark414c4292016-09-26 11:03:54 -07001078 return;
caryclarkb6474dd2016-01-19 08:07:49 -08001079 } else {
1080 SkConic pair[2];
caryclark414c4292016-09-26 11:03:54 -07001081 if (this->chopAt(t1 ? t1 : t2, pair)) {
1082 *dst = pair[SkToBool(t1)];
1083 return;
1084 }
caryclarkb6474dd2016-01-19 08:07:49 -08001085 }
caryclarkb6474dd2016-01-19 08:07:49 -08001086 }
1087 SkConicCoeff coeff(*this);
1088 Sk2s tt1(t1);
1089 Sk2s aXY = coeff.fNumer.eval(tt1);
1090 Sk2s aZZ = coeff.fDenom.eval(tt1);
1091 Sk2s midTT((t1 + t2) / 2);
1092 Sk2s dXY = coeff.fNumer.eval(midTT);
1093 Sk2s dZZ = coeff.fDenom.eval(midTT);
1094 Sk2s tt2(t2);
1095 Sk2s cXY = coeff.fNumer.eval(tt2);
1096 Sk2s cZZ = coeff.fDenom.eval(tt2);
1097 Sk2s bXY = times_2(dXY) - (aXY + cXY) * Sk2s(0.5f);
1098 Sk2s bZZ = times_2(dZZ) - (aZZ + cZZ) * Sk2s(0.5f);
1099 dst->fPts[0] = to_point(aXY / aZZ);
1100 dst->fPts[1] = to_point(bXY / bZZ);
1101 dst->fPts[2] = to_point(cXY / cZZ);
1102 Sk2s ww = bZZ / (aZZ * cZZ).sqrt();
mtklein7c249e52016-02-21 10:54:19 -08001103 dst->fW = ww[0];
reedb6402032015-03-20 13:23:43 -07001104}
1105
1106SkPoint SkConic::evalAt(SkScalar t) const {
caryclark5ba2b962016-01-26 17:02:30 -08001107 return to_point(SkConicCoeff(*this).eval(t));
reedb6402032015-03-20 13:23:43 -07001108}
1109
1110SkVector SkConic::evalTangentAt(SkScalar t) const {
caryclark45398df2015-08-25 13:19:06 -07001111 // The derivative equation returns a zero tangent vector when t is 0 or 1,
1112 // and the control point is equal to the end point.
1113 // In this case, use the conic endpoints to compute the tangent.
1114 if ((t == 0 && fPts[0] == fPts[1]) || (t == 1 && fPts[1] == fPts[2])) {
1115 return fPts[2] - fPts[0];
1116 }
reedb6402032015-03-20 13:23:43 -07001117 Sk2s p0 = from_point(fPts[0]);
1118 Sk2s p1 = from_point(fPts[1]);
1119 Sk2s p2 = from_point(fPts[2]);
reedce6acc92015-03-20 13:46:08 -07001120 Sk2s ww(fW);
reedb6402032015-03-20 13:23:43 -07001121
1122 Sk2s p20 = p2 - p0;
1123 Sk2s p10 = p1 - p0;
1124
1125 Sk2s C = ww * p10;
1126 Sk2s A = ww * p20 - p20;
1127 Sk2s B = p20 - C - C;
1128
caryclark5ba2b962016-01-26 17:02:30 -08001129 return to_vector(SkQuadCoeff(A, B, C).eval(t));
reedb6402032015-03-20 13:23:43 -07001130}
1131
reedf0b6c552016-01-01 13:05:10 -08001132void SkConic::evalAt(SkScalar t, SkPoint* pt, SkVector* tangent) const {
1133 SkASSERT(t >= 0 && t <= SK_Scalar1);
mtklein507ef6d2016-01-31 08:02:47 -08001134
reedf0b6c552016-01-01 13:05:10 -08001135 if (pt) {
1136 *pt = this->evalAt(t);
1137 }
1138 if (tangent) {
1139 *tangent = this->evalTangentAt(t);
1140 }
1141}
1142
mike@reedtribe.org3df87cb2013-04-15 15:20:52 +00001143static SkScalar subdivide_w_value(SkScalar w) {
mike@reedtribe.org6862cba2013-05-08 01:55:49 +00001144 return SkScalarSqrt(SK_ScalarHalf + w * SK_ScalarHalf);
mike@reedtribe.org3df87cb2013-04-15 15:20:52 +00001145}
1146
reed55011032015-03-26 09:10:22 -07001147void SkConic::chop(SkConic * SK_RESTRICT dst) const {
1148 Sk2s scale = Sk2s(SkScalarInvert(SK_Scalar1 + fW));
reedb6402032015-03-20 13:23:43 -07001149 SkScalar newW = subdivide_w_value(fW);
1150
1151 Sk2s p0 = from_point(fPts[0]);
1152 Sk2s p1 = from_point(fPts[1]);
1153 Sk2s p2 = from_point(fPts[2]);
reedce6acc92015-03-20 13:46:08 -07001154 Sk2s ww(fW);
reedb6402032015-03-20 13:23:43 -07001155
1156 Sk2s wp1 = ww * p1;
caryclark5ba2b962016-01-26 17:02:30 -08001157 Sk2s m = (p0 + times_2(wp1) + p2) * scale * Sk2s(0.5f);
Cary Clarka0e30b12018-02-02 11:02:46 -05001158 SkPoint mPt = to_point(m);
1159 if (!mPt.isFinite()) {
1160 double w_d = fW;
1161 double w_2 = w_d * 2;
1162 double scale_half = 1 / (1 + w_d) * 0.5;
1163 mPt.fX = SkDoubleToScalar((fPts[0].fX + w_2 * fPts[1].fX + fPts[2].fX) * scale_half);
1164 mPt.fY = SkDoubleToScalar((fPts[0].fY + w_2 * fPts[1].fY + fPts[2].fY) * scale_half);
1165 }
reedb6402032015-03-20 13:23:43 -07001166 dst[0].fPts[0] = fPts[0];
1167 dst[0].fPts[1] = to_point((p0 + wp1) * scale);
Cary Clarka0e30b12018-02-02 11:02:46 -05001168 dst[0].fPts[2] = dst[1].fPts[0] = mPt;
reedb6402032015-03-20 13:23:43 -07001169 dst[1].fPts[1] = to_point((wp1 + p2) * scale);
1170 dst[1].fPts[2] = fPts[2];
1171
1172 dst[0].fW = dst[1].fW = newW;
1173}
1174
mike@reedtribe.org97514f22013-04-27 18:23:16 +00001175/*
1176 * "High order approximation of conic sections by quadratic splines"
1177 * by Michael Floater, 1993
1178 */
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +00001179#define AS_QUAD_ERROR_SETUP \
1180 SkScalar a = fW - 1; \
1181 SkScalar k = a / (4 * (2 + a)); \
1182 SkScalar x = k * (fPts[0].fX - 2 * fPts[1].fX + fPts[2].fX); \
1183 SkScalar y = k * (fPts[0].fY - 2 * fPts[1].fY + fPts[2].fY);
1184
1185void SkConic::computeAsQuadError(SkVector* err) const {
1186 AS_QUAD_ERROR_SETUP
1187 err->set(x, y);
1188}
1189
1190bool SkConic::asQuadTol(SkScalar tol) const {
1191 AS_QUAD_ERROR_SETUP
1192 return (x * x + y * y) <= tol * tol;
mike@reedtribe.org97514f22013-04-27 18:23:16 +00001193}
1194
reedf16c00e2015-02-12 17:56:21 -08001195// Limit the number of suggested quads to approximate a conic
1196#define kMaxConicToQuadPOW2 5
1197
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001198int SkConic::computeQuadPOW2(SkScalar tol) const {
Cary Clarkdf429f32017-11-08 11:44:31 -05001199 if (tol < 0 || !SkScalarIsFinite(tol) || !SkPointPriv::AreFinite(fPts, 3)) {
reedf16c00e2015-02-12 17:56:21 -08001200 return 0;
1201 }
1202
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +00001203 AS_QUAD_ERROR_SETUP
reedf16c00e2015-02-12 17:56:21 -08001204
reedf16c00e2015-02-12 17:56:21 -08001205 SkScalar error = SkScalarSqrt(x * x + y * y);
1206 int pow2;
1207 for (pow2 = 0; pow2 < kMaxConicToQuadPOW2; ++pow2) {
1208 if (error <= tol) {
1209 break;
1210 }
1211 error *= 0.25f;
1212 }
1213 // float version -- using ceil gives the same results as the above.
1214 if (false) {
1215 SkScalar err = SkScalarSqrt(x * x + y * y);
1216 if (err <= tol) {
1217 return 0;
1218 }
1219 SkScalar tol2 = tol * tol;
1220 if (tol2 == 0) {
1221 return kMaxConicToQuadPOW2;
1222 }
1223 SkScalar fpow2 = SkScalarLog2((x * x + y * y) / tol2) * 0.25f;
1224 int altPow2 = SkScalarCeilToInt(fpow2);
1225 if (altPow2 != pow2) {
1226 SkDebugf("pow2 %d altPow2 %d fbits %g err %g tol %g\n", pow2, altPow2, fpow2, err, tol);
1227 }
1228 pow2 = altPow2;
1229 }
1230 return pow2;
mike@reedtribe.org3df87cb2013-04-15 15:20:52 +00001231}
1232
Ben Wagner63fd7602017-10-09 15:45:33 -04001233// This was originally developed and tested for pathops: see SkOpTypes.h
caryclarkbac10462016-09-22 10:24:59 -07001234// returns true if (a <= b <= c) || (a >= b >= c)
1235static bool between(SkScalar a, SkScalar b, SkScalar c) {
1236 return (a - b) * (c - b) <= 0;
1237}
1238
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001239static SkPoint* subdivide(const SkConic& src, SkPoint pts[], int level) {
mike@reedtribe.org3df87cb2013-04-15 15:20:52 +00001240 SkASSERT(level >= 0);
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +00001241
mike@reedtribe.org3df87cb2013-04-15 15:20:52 +00001242 if (0 == level) {
1243 memcpy(pts, &src.fPts[1], 2 * sizeof(SkPoint));
1244 return pts + 2;
1245 } else {
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001246 SkConic dst[2];
mike@reedtribe.org3df87cb2013-04-15 15:20:52 +00001247 src.chop(dst);
caryclarkbac10462016-09-22 10:24:59 -07001248 const SkScalar startY = src.fPts[0].fY;
Cary Clarka0e30b12018-02-02 11:02:46 -05001249 SkScalar endY = src.fPts[2].fY;
caryclarkbac10462016-09-22 10:24:59 -07001250 if (between(startY, src.fPts[1].fY, endY)) {
1251 // If the input is monotonic and the output is not, the scan converter hangs.
1252 // Ensure that the chopped conics maintain their y-order.
1253 SkScalar midY = dst[0].fPts[2].fY;
1254 if (!between(startY, midY, endY)) {
1255 // If the computed midpoint is outside the ends, move it to the closer one.
1256 SkScalar closerY = SkTAbs(midY - startY) < SkTAbs(midY - endY) ? startY : endY;
1257 dst[0].fPts[2].fY = dst[1].fPts[0].fY = closerY;
1258 }
1259 if (!between(startY, dst[0].fPts[1].fY, dst[0].fPts[2].fY)) {
1260 // If the 1st control is not between the start and end, put it at the start.
1261 // This also reduces the quad to a line.
1262 dst[0].fPts[1].fY = startY;
1263 }
1264 if (!between(dst[1].fPts[0].fY, dst[1].fPts[1].fY, endY)) {
1265 // If the 2nd control is not between the start and end, put it at the end.
1266 // This also reduces the quad to a line.
1267 dst[1].fPts[1].fY = endY;
1268 }
1269 // Verify that all five points are in order.
1270 SkASSERT(between(startY, dst[0].fPts[1].fY, dst[0].fPts[2].fY));
1271 SkASSERT(between(dst[0].fPts[1].fY, dst[0].fPts[2].fY, dst[1].fPts[1].fY));
1272 SkASSERT(between(dst[0].fPts[2].fY, dst[1].fPts[1].fY, endY));
1273 }
mike@reedtribe.org3df87cb2013-04-15 15:20:52 +00001274 --level;
1275 pts = subdivide(dst[0], pts, level);
1276 return subdivide(dst[1], pts, level);
1277 }
1278}
1279
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001280int SkConic::chopIntoQuadsPOW2(SkPoint pts[], int pow2) const {
mike@reedtribe.orgaf5c5062013-04-30 02:14:58 +00001281 SkASSERT(pow2 >= 0);
caryclark3cebe242016-08-23 09:41:00 -07001282 *pts = fPts[0];
1283 SkDEBUGCODE(SkPoint* endPts);
1284 if (pow2 == kMaxConicToQuadPOW2) { // If an extreme weight generates many quads ...
1285 SkConic dst[2];
1286 this->chop(dst);
1287 // check to see if the first chop generates a pair of lines
Cary Clarkdf429f32017-11-08 11:44:31 -05001288 if (SkPointPriv::EqualsWithinTolerance(dst[0].fPts[1], dst[0].fPts[2]) &&
1289 SkPointPriv::EqualsWithinTolerance(dst[1].fPts[0], dst[1].fPts[1])) {
caryclark3cebe242016-08-23 09:41:00 -07001290 pts[1] = pts[2] = pts[3] = dst[0].fPts[1]; // set ctrl == end to make lines
1291 pts[4] = dst[1].fPts[2];
1292 pow2 = 1;
1293 SkDEBUGCODE(endPts = &pts[5]);
1294 goto commonFinitePtCheck;
1295 }
1296 }
1297 SkDEBUGCODE(endPts = ) subdivide(*this, pts + 1, pow2);
1298commonFinitePtCheck:
reedb1b12f82016-07-13 10:56:53 -07001299 const int quadCount = 1 << pow2;
1300 const int ptCount = 2 * quadCount + 1;
reedb1b12f82016-07-13 10:56:53 -07001301 SkASSERT(endPts - pts == ptCount);
Cary Clarkdf429f32017-11-08 11:44:31 -05001302 if (!SkPointPriv::AreFinite(pts, ptCount)) {
reedb1b12f82016-07-13 10:56:53 -07001303 // if we generated a non-finite, pin ourselves to the middle of the hull,
1304 // as our first and last are already on the first/last pts of the hull.
1305 for (int i = 1; i < ptCount - 1; ++i) {
1306 pts[i] = fPts[1];
1307 }
1308 }
mike@reedtribe.org3df87cb2013-04-15 15:20:52 +00001309 return 1 << pow2;
1310}
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001311
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001312bool SkConic::findXExtrema(SkScalar* t) const {
mike@reedtribe.org6862cba2013-05-08 01:55:49 +00001313 return conic_find_extrema(&fPts[0].fX, fW, t);
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001314}
1315
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001316bool SkConic::findYExtrema(SkScalar* t) const {
mike@reedtribe.org6862cba2013-05-08 01:55:49 +00001317 return conic_find_extrema(&fPts[0].fY, fW, t);
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001318}
1319
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001320bool SkConic::chopAtXExtrema(SkConic dst[2]) const {
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001321 SkScalar t;
1322 if (this->findXExtrema(&t)) {
caryclark414c4292016-09-26 11:03:54 -07001323 if (!this->chopAt(t, dst)) {
1324 // if chop can't return finite values, don't chop
1325 return false;
1326 }
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001327 // now clean-up the middle, since we know t was meant to be at
1328 // an X-extrema
1329 SkScalar value = dst[0].fPts[2].fX;
1330 dst[0].fPts[1].fX = value;
1331 dst[1].fPts[0].fX = value;
1332 dst[1].fPts[1].fX = value;
1333 return true;
1334 }
1335 return false;
1336}
1337
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001338bool SkConic::chopAtYExtrema(SkConic dst[2]) const {
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001339 SkScalar t;
1340 if (this->findYExtrema(&t)) {
caryclark414c4292016-09-26 11:03:54 -07001341 if (!this->chopAt(t, dst)) {
1342 // if chop can't return finite values, don't chop
1343 return false;
1344 }
mike@reedtribe.org0c5c3862013-04-17 01:21:01 +00001345 // now clean-up the middle, since we know t was meant to be at
1346 // an Y-extrema
1347 SkScalar value = dst[0].fPts[2].fY;
1348 dst[0].fPts[1].fY = value;
1349 dst[1].fPts[0].fY = value;
1350 dst[1].fPts[1].fY = value;
1351 return true;
1352 }
1353 return false;
1354}
1355
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001356void SkConic::computeTightBounds(SkRect* bounds) const {
mike@reedtribe.org5c082a12013-04-17 02:25:33 +00001357 SkPoint pts[4];
1358 pts[0] = fPts[0];
1359 pts[1] = fPts[2];
1360 int count = 2;
1361
1362 SkScalar t;
1363 if (this->findXExtrema(&t)) {
1364 this->evalAt(t, &pts[count++]);
1365 }
1366 if (this->findYExtrema(&t)) {
1367 this->evalAt(t, &pts[count++]);
1368 }
Mike Reed92b33352019-08-24 19:39:13 -04001369 bounds->setBounds(pts, count);
mike@reedtribe.org5c082a12013-04-17 02:25:33 +00001370}
1371
mike@reedtribe.org28552e12013-04-26 00:58:29 +00001372void SkConic::computeFastBounds(SkRect* bounds) const {
Mike Reed92b33352019-08-24 19:39:13 -04001373 bounds->setBounds(fPts, 3);
mike@reedtribe.org5c082a12013-04-17 02:25:33 +00001374}
commit-bot@chromium.orgdef64682014-02-21 19:49:10 +00001375
caryclark612f70d2015-05-19 11:05:37 -07001376#if 0 // unimplemented
commit-bot@chromium.orgdef64682014-02-21 19:49:10 +00001377bool SkConic::findMaxCurvature(SkScalar* t) const {
1378 // TODO: Implement me
1379 return false;
1380}
caryclark612f70d2015-05-19 11:05:37 -07001381#endif
reed220f9262014-12-17 08:21:04 -08001382
Mike Reed0a78e1e2018-02-22 14:13:21 -05001383SkScalar SkConic::TransformW(const SkPoint pts[], SkScalar w, const SkMatrix& matrix) {
reed220f9262014-12-17 08:21:04 -08001384 if (!matrix.hasPerspective()) {
1385 return w;
1386 }
1387
Cary Clarke4442cb2017-10-18 11:46:18 -04001388 SkPoint3 src[3], dst[3];
mtklein950e9862015-03-19 12:03:29 -07001389
reed220f9262014-12-17 08:21:04 -08001390 ratquad_mapTo3D(pts, w, src);
1391
Cary Clarke4442cb2017-10-18 11:46:18 -04001392 matrix.mapHomogeneousPoints(dst, src, 3);
reed220f9262014-12-17 08:21:04 -08001393
1394 // w' = sqrt(w1*w1/w0*w2)
Mike Reed0a78e1e2018-02-22 14:13:21 -05001395 // use doubles temporarily, to handle small numer/denom
1396 double w0 = dst[0].fZ;
1397 double w1 = dst[1].fZ;
1398 double w2 = dst[2].fZ;
Mike Reed5a5e0842018-05-24 14:12:47 -04001399 return sk_double_to_float(sqrt(sk_ieee_double_divide(w1 * w1, w0 * w2)));
reed220f9262014-12-17 08:21:04 -08001400}
reedd5d27d92015-02-09 13:54:43 -08001401
1402int SkConic::BuildUnitArc(const SkVector& uStart, const SkVector& uStop, SkRotationDirection dir,
1403 const SkMatrix* userMatrix, SkConic dst[kMaxConicsForArc]) {
1404 // rotate by x,y so that uStart is (1.0)
1405 SkScalar x = SkPoint::DotProduct(uStart, uStop);
1406 SkScalar y = SkPoint::CrossProduct(uStart, uStop);
1407
1408 SkScalar absY = SkScalarAbs(y);
1409
1410 // check for (effectively) coincident vectors
1411 // this can happen if our angle is nearly 0 or nearly 180 (y == 0)
1412 // ... we use the dot-prod to distinguish between 0 and 180 (x > 0)
1413 if (absY <= SK_ScalarNearlyZero && x > 0 && ((y >= 0 && kCW_SkRotationDirection == dir) ||
1414 (y <= 0 && kCCW_SkRotationDirection == dir))) {
1415 return 0;
1416 }
1417
1418 if (dir == kCCW_SkRotationDirection) {
1419 y = -y;
1420 }
1421
1422 // We decide to use 1-conic per quadrant of a circle. What quadrant does [xy] lie in?
1423 // 0 == [0 .. 90)
1424 // 1 == [90 ..180)
1425 // 2 == [180..270)
1426 // 3 == [270..360)
1427 //
1428 int quadrant = 0;
1429 if (0 == y) {
1430 quadrant = 2; // 180
1431 SkASSERT(SkScalarAbs(x + SK_Scalar1) <= SK_ScalarNearlyZero);
1432 } else if (0 == x) {
1433 SkASSERT(absY - SK_Scalar1 <= SK_ScalarNearlyZero);
1434 quadrant = y > 0 ? 1 : 3; // 90 : 270
1435 } else {
1436 if (y < 0) {
1437 quadrant += 2;
1438 }
1439 if ((x < 0) != (y < 0)) {
1440 quadrant += 1;
1441 }
1442 }
1443
1444 const SkPoint quadrantPts[] = {
1445 { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 }
1446 };
1447 const SkScalar quadrantWeight = SK_ScalarRoot2Over2;
1448
1449 int conicCount = quadrant;
1450 for (int i = 0; i < conicCount; ++i) {
1451 dst[i].set(&quadrantPts[i * 2], quadrantWeight);
1452 }
1453
1454 // Now compute any remaing (sub-90-degree) arc for the last conic
1455 const SkPoint finalP = { x, y };
1456 const SkPoint& lastQ = quadrantPts[quadrant * 2]; // will already be a unit-vector
1457 const SkScalar dot = SkVector::DotProduct(lastQ, finalP);
reed88f0a992015-02-10 08:45:06 -08001458 SkASSERT(0 <= dot && dot <= SK_Scalar1 + SK_ScalarNearlyZero);
reedd5d27d92015-02-09 13:54:43 -08001459
caryclarkc6325cd2015-05-11 14:36:33 -07001460 if (dot < 1) {
reedd5d27d92015-02-09 13:54:43 -08001461 SkVector offCurve = { lastQ.x() + x, lastQ.y() + y };
1462 // compute the bisector vector, and then rescale to be the off-curve point.
1463 // we compute its length from cos(theta/2) = length / 1, using half-angle identity we get
1464 // length = sqrt(2 / (1 + cos(theta)). We already have cos() when to computed the dot.
1465 // This is nice, since our computed weight is cos(theta/2) as well!
1466 //
1467 const SkScalar cosThetaOver2 = SkScalarSqrt((1 + dot) / 2);
1468 offCurve.setLength(SkScalarInvert(cosThetaOver2));
Cary Clarkdf429f32017-11-08 11:44:31 -05001469 if (!SkPointPriv::EqualsWithinTolerance(lastQ, offCurve)) {
caryclarkf71ab8f2016-08-26 09:54:25 -07001470 dst[conicCount].set(lastQ, offCurve, finalP, cosThetaOver2);
1471 conicCount += 1;
Ben Wagner63fd7602017-10-09 15:45:33 -04001472 }
reedd5d27d92015-02-09 13:54:43 -08001473 }
1474
1475 // now handle counter-clockwise and the initial unitStart rotation
1476 SkMatrix matrix;
1477 matrix.setSinCos(uStart.fY, uStart.fX);
1478 if (dir == kCCW_SkRotationDirection) {
1479 matrix.preScale(SK_Scalar1, -SK_Scalar1);
1480 }
1481 if (userMatrix) {
1482 matrix.postConcat(*userMatrix);
1483 }
1484 for (int i = 0; i < conicCount; ++i) {
1485 matrix.mapPoints(dst[i].fPts, 3);
1486 }
1487 return conicCount;
1488}