1/*
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
8#include "SkGeometry.h"
9#include "SkMatrix.h"
10#include "SkNx.h"
11
12static SkVector to_vector(const Sk2s& x) {
13    SkVector vector;
14    x.store(&vector);
15    return vector;
16}
17
18////////////////////////////////////////////////////////////////////////
19
20static int is_not_monotonic(SkScalar a, SkScalar b, SkScalar c) {
21    SkScalar ab = a - b;
22    SkScalar bc = b - c;
23    if (ab < 0) {
24        bc = -bc;
25    }
26    return ab == 0 || bc < 0;
27}
28
29////////////////////////////////////////////////////////////////////////
30
31static bool is_unit_interval(SkScalar x) {
32    return x > 0 && x < SK_Scalar1;
33}
34
35static int valid_unit_divide(SkScalar numer, SkScalar denom, SkScalar* ratio) {
36    SkASSERT(ratio);
37
38    if (numer < 0) {
39        numer = -numer;
40        denom = -denom;
41    }
42
43    if (denom == 0 || numer == 0 || numer >= denom) {
44        return 0;
45    }
46
47    SkScalar r = numer / denom;
48    if (SkScalarIsNaN(r)) {
49        return 0;
50    }
51    SkASSERTF(r >= 0 && r < SK_Scalar1, "numer %f, denom %f, r %f", numer, denom, r);
52    if (r == 0) { // catch underflow if numer <<<< denom
53        return 0;
54    }
55    *ratio = r;
56    return 1;
57}
58
59/** From Numerical Recipes in C.
60
61    Q = -1/2 (B + sign(B) sqrt[B*B - 4*A*C])
62    x1 = Q / A
63    x2 = C / Q
64*/
65int SkFindUnitQuadRoots(SkScalar A, SkScalar B, SkScalar C, SkScalar roots[2]) {
66    SkASSERT(roots);
67
68    if (A == 0) {
69        return valid_unit_divide(-C, B, roots);
70    }
71
72    SkScalar* r = roots;
73
74    SkScalar R = B*B - 4*A*C;
75    if (R < 0 || !SkScalarIsFinite(R)) {  // complex roots
76        // if R is infinite, it's possible that it may still produce
77        // useful results if the operation was repeated in doubles
78        // the flipside is determining if the more precise answer
79        // isn't useful because surrounding machinery (e.g., subtracting
80        // the axis offset from C) already discards the extra precision
81        // more investigation and unit tests required...
82        return 0;
83    }
84    R = SkScalarSqrt(R);
85
86    SkScalar Q = (B < 0) ? -(B-R)/2 : -(B+R)/2;
87    r += valid_unit_divide(Q, A, r);
88    r += valid_unit_divide(C, Q, r);
89    if (r - roots == 2) {
90        if (roots[0] > roots[1])
91            SkTSwap<SkScalar>(roots[0], roots[1]);
92        else if (roots[0] == roots[1])  // nearly-equal?
93            r -= 1; // skip the double root
94    }
95    return (int)(r - roots);
96}
97
98///////////////////////////////////////////////////////////////////////////////
99///////////////////////////////////////////////////////////////////////////////
100
101void SkEvalQuadAt(const SkPoint src[3], SkScalar t, SkPoint* pt, SkVector* tangent) {
102    SkASSERT(src);
103    SkASSERT(t >= 0 && t <= SK_Scalar1);
104
105    if (pt) {
106        *pt = SkEvalQuadAt(src, t);
107    }
108    if (tangent) {
109        *tangent = SkEvalQuadTangentAt(src, t);
110    }
111}
112
113SkPoint SkEvalQuadAt(const SkPoint src[3], SkScalar t) {
114    return to_point(SkQuadCoeff(src).eval(t));
115}
116
117SkVector SkEvalQuadTangentAt(const SkPoint src[3], SkScalar t) {
118    // The derivative equation is 2(b - a +(a - 2b +c)t). This returns a
119    // zero tangent vector when t is 0 or 1, and the control point is equal
120    // to the end point. In this case, use the quad end points to compute the tangent.
121    if ((t == 0 && src[0] == src[1]) || (t == 1 && src[1] == src[2])) {
122        return src[2] - src[0];
123    }
124    SkASSERT(src);
125    SkASSERT(t >= 0 && t <= SK_Scalar1);
126
127    Sk2s P0 = from_point(src[0]);
128    Sk2s P1 = from_point(src[1]);
129    Sk2s P2 = from_point(src[2]);
130
131    Sk2s B = P1 - P0;
132    Sk2s A = P2 - P1 - B;
133    Sk2s T = A * Sk2s(t) + B;
134
135    return to_vector(T + T);
136}
137
138static inline Sk2s interp(const Sk2s& v0, const Sk2s& v1, const Sk2s& t) {
139    return v0 + (v1 - v0) * t;
140}
141
142void SkChopQuadAt(const SkPoint src[3], SkPoint dst[5], SkScalar t) {
143    SkASSERT(t > 0 && t < SK_Scalar1);
144
145    Sk2s p0 = from_point(src[0]);
146    Sk2s p1 = from_point(src[1]);
147    Sk2s p2 = from_point(src[2]);
148    Sk2s tt(t);
149
150    Sk2s p01 = interp(p0, p1, tt);
151    Sk2s p12 = interp(p1, p2, tt);
152
153    dst[0] = to_point(p0);
154    dst[1] = to_point(p01);
155    dst[2] = to_point(interp(p01, p12, tt));
156    dst[3] = to_point(p12);
157    dst[4] = to_point(p2);
158}
159
160void SkChopQuadAtHalf(const SkPoint src[3], SkPoint dst[5]) {
161    SkChopQuadAt(src, dst, 0.5f);
162}
163
164/** Quad'(t) = At + B, where
165    A = 2(a - 2b + c)
166    B = 2(b - a)
167    Solve for t, only if it fits between 0 < t < 1
168*/
169int SkFindQuadExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar tValue[1]) {
170    /*  At + B == 0
171        t = -B / A
172    */
173    return valid_unit_divide(a - b, a - b - b + c, tValue);
174}
175
176static inline void flatten_double_quad_extrema(SkScalar coords[14]) {
177    coords[2] = coords[6] = coords[4];
178}
179
180/*  Returns 0 for 1 quad, and 1 for two quads, either way the answer is
181 stored in dst[]. Guarantees that the 1/2 quads will be monotonic.
182 */
183int SkChopQuadAtYExtrema(const SkPoint src[3], SkPoint dst[5]) {
184    SkASSERT(src);
185    SkASSERT(dst);
186
187    SkScalar a = src[0].fY;
188    SkScalar b = src[1].fY;
189    SkScalar c = src[2].fY;
190
191    if (is_not_monotonic(a, b, c)) {
192        SkScalar    tValue;
193        if (valid_unit_divide(a - b, a - b - b + c, &tValue)) {
194            SkChopQuadAt(src, dst, tValue);
195            flatten_double_quad_extrema(&dst[0].fY);
196            return 1;
197        }
198        // if we get here, we need to force dst to be monotonic, even though
199        // we couldn't compute a unit_divide value (probably underflow).
200        b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c;
201    }
202    dst[0].set(src[0].fX, a);
203    dst[1].set(src[1].fX, b);
204    dst[2].set(src[2].fX, c);
205    return 0;
206}
207
208/*  Returns 0 for 1 quad, and 1 for two quads, either way the answer is
209    stored in dst[]. Guarantees that the 1/2 quads will be monotonic.
210 */
211int SkChopQuadAtXExtrema(const SkPoint src[3], SkPoint dst[5]) {
212    SkASSERT(src);
213    SkASSERT(dst);
214
215    SkScalar a = src[0].fX;
216    SkScalar b = src[1].fX;
217    SkScalar c = src[2].fX;
218
219    if (is_not_monotonic(a, b, c)) {
220        SkScalar tValue;
221        if (valid_unit_divide(a - b, a - b - b + c, &tValue)) {
222            SkChopQuadAt(src, dst, tValue);
223            flatten_double_quad_extrema(&dst[0].fX);
224            return 1;
225        }
226        // if we get here, we need to force dst to be monotonic, even though
227        // we couldn't compute a unit_divide value (probably underflow).
228        b = SkScalarAbs(a - b) < SkScalarAbs(b - c) ? a : c;
229    }
230    dst[0].set(a, src[0].fY);
231    dst[1].set(b, src[1].fY);
232    dst[2].set(c, src[2].fY);
233    return 0;
234}
235
236//  F(t)    = a (1 - t) ^ 2 + 2 b t (1 - t) + c t ^ 2
237//  F'(t)   = 2 (b - a) + 2 (a - 2b + c) t
238//  F''(t)  = 2 (a - 2b + c)
239//
240//  A = 2 (b - a)
241//  B = 2 (a - 2b + c)
242//
243//  Maximum curvature for a quadratic means solving
244//  Fx' Fx'' + Fy' Fy'' = 0
245//
246//  t = - (Ax Bx + Ay By) / (Bx ^ 2 + By ^ 2)
247//
248SkScalar SkFindQuadMaxCurvature(const SkPoint src[3]) {
249    SkScalar    Ax = src[1].fX - src[0].fX;
250    SkScalar    Ay = src[1].fY - src[0].fY;
251    SkScalar    Bx = src[0].fX - src[1].fX - src[1].fX + src[2].fX;
252    SkScalar    By = src[0].fY - src[1].fY - src[1].fY + src[2].fY;
253    SkScalar    t = 0;  // 0 means don't chop
254
255    (void)valid_unit_divide(-(Ax * Bx + Ay * By), Bx * Bx + By * By, &t);
256    return t;
257}
258
259int SkChopQuadAtMaxCurvature(const SkPoint src[3], SkPoint dst[5]) {
260    SkScalar t = SkFindQuadMaxCurvature(src);
261    if (t == 0) {
262        memcpy(dst, src, 3 * sizeof(SkPoint));
263        return 1;
264    } else {
265        SkChopQuadAt(src, dst, t);
266        return 2;
267    }
268}
269
270void SkConvertQuadToCubic(const SkPoint src[3], SkPoint dst[4]) {
271    Sk2s scale(SkDoubleToScalar(2.0 / 3.0));
272    Sk2s s0 = from_point(src[0]);
273    Sk2s s1 = from_point(src[1]);
274    Sk2s s2 = from_point(src[2]);
275
276    dst[0] = src[0];
277    dst[1] = to_point(s0 + (s1 - s0) * scale);
278    dst[2] = to_point(s2 + (s1 - s2) * scale);
279    dst[3] = src[2];
280}
281
282//////////////////////////////////////////////////////////////////////////////
283///// CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS // CUBICS /////
284//////////////////////////////////////////////////////////////////////////////
285
286static SkVector eval_cubic_derivative(const SkPoint src[4], SkScalar t) {
287    SkQuadCoeff coeff;
288    Sk2s P0 = from_point(src[0]);
289    Sk2s P1 = from_point(src[1]);
290    Sk2s P2 = from_point(src[2]);
291    Sk2s P3 = from_point(src[3]);
292
293    coeff.fA = P3 + Sk2s(3) * (P1 - P2) - P0;
294    coeff.fB = times_2(P2 - times_2(P1) + P0);
295    coeff.fC = P1 - P0;
296    return to_vector(coeff.eval(t));
297}
298
299static SkVector eval_cubic_2ndDerivative(const SkPoint src[4], SkScalar t) {
300    Sk2s P0 = from_point(src[0]);
301    Sk2s P1 = from_point(src[1]);
302    Sk2s P2 = from_point(src[2]);
303    Sk2s P3 = from_point(src[3]);
304    Sk2s A = P3 + Sk2s(3) * (P1 - P2) - P0;
305    Sk2s B = P2 - times_2(P1) + P0;
306
307    return to_vector(A * Sk2s(t) + B);
308}
309
310void SkEvalCubicAt(const SkPoint src[4], SkScalar t, SkPoint* loc,
311                   SkVector* tangent, SkVector* curvature) {
312    SkASSERT(src);
313    SkASSERT(t >= 0 && t <= SK_Scalar1);
314
315    if (loc) {
316        *loc = to_point(SkCubicCoeff(src).eval(t));
317    }
318    if (tangent) {
319        // The derivative equation returns a zero tangent vector when t is 0 or 1, and the
320        // adjacent control point is equal to the end point. In this case, use the
321        // next control point or the end points to compute the tangent.
322        if ((t == 0 && src[0] == src[1]) || (t == 1 && src[2] == src[3])) {
323            if (t == 0) {
324                *tangent = src[2] - src[0];
325            } else {
326                *tangent = src[3] - src[1];
327            }
328            if (!tangent->fX && !tangent->fY) {
329                *tangent = src[3] - src[0];
330            }
331        } else {
332            *tangent = eval_cubic_derivative(src, t);
333        }
334    }
335    if (curvature) {
336        *curvature = eval_cubic_2ndDerivative(src, t);
337    }
338}
339
340/** Cubic'(t) = At^2 + Bt + C, where
341    A = 3(-a + 3(b - c) + d)
342    B = 6(a - 2b + c)
343    C = 3(b - a)
344    Solve for t, keeping only those that fit betwee 0 < t < 1
345*/
346int SkFindCubicExtrema(SkScalar a, SkScalar b, SkScalar c, SkScalar d,
347                       SkScalar tValues[2]) {
348    // we divide A,B,C by 3 to simplify
349    SkScalar A = d - a + 3*(b - c);
350    SkScalar B = 2*(a - b - b + c);
351    SkScalar C = b - a;
352
353    return SkFindUnitQuadRoots(A, B, C, tValues);
354}
355
356void SkChopCubicAt(const SkPoint src[4], SkPoint dst[7], SkScalar t) {
357    SkASSERT(t > 0 && t < SK_Scalar1);
358
359    Sk2s    p0 = from_point(src[0]);
360    Sk2s    p1 = from_point(src[1]);
361    Sk2s    p2 = from_point(src[2]);
362    Sk2s    p3 = from_point(src[3]);
363    Sk2s    tt(t);
364
365    Sk2s    ab = interp(p0, p1, tt);
366    Sk2s    bc = interp(p1, p2, tt);
367    Sk2s    cd = interp(p2, p3, tt);
368    Sk2s    abc = interp(ab, bc, tt);
369    Sk2s    bcd = interp(bc, cd, tt);
370    Sk2s    abcd = interp(abc, bcd, tt);
371
372    dst[0] = src[0];
373    dst[1] = to_point(ab);
374    dst[2] = to_point(abc);
375    dst[3] = to_point(abcd);
376    dst[4] = to_point(bcd);
377    dst[5] = to_point(cd);
378    dst[6] = src[3];
379}
380
381/*  http://code.google.com/p/skia/issues/detail?id=32
382
383    This test code would fail when we didn't check the return result of
384    valid_unit_divide in SkChopCubicAt(... tValues[], int roots). The reason is
385    that after the first chop, the parameters to valid_unit_divide are equal
386    (thanks to finite float precision and rounding in the subtracts). Thus
387    even though the 2nd tValue looks < 1.0, after we renormalize it, we end
388    up with 1.0, hence the need to check and just return the last cubic as
389    a degenerate clump of 4 points in the sampe place.
390
391    static void test_cubic() {
392        SkPoint src[4] = {
393            { 556.25000, 523.03003 },
394            { 556.23999, 522.96002 },
395            { 556.21997, 522.89001 },
396            { 556.21997, 522.82001 }
397        };
398        SkPoint dst[10];
399        SkScalar tval[] = { 0.33333334f, 0.99999994f };
400        SkChopCubicAt(src, dst, tval, 2);
401    }
402 */
403
404void SkChopCubicAt(const SkPoint src[4], SkPoint dst[],
405                   const SkScalar tValues[], int roots) {
406#ifdef SK_DEBUG
407    {
408        for (int i = 0; i < roots - 1; i++)
409        {
410            SkASSERT(is_unit_interval(tValues[i]));
411            SkASSERT(is_unit_interval(tValues[i+1]));
412            SkASSERT(tValues[i] < tValues[i+1]);
413        }
414    }
415#endif
416
417    if (dst) {
418        if (roots == 0) { // nothing to chop
419            memcpy(dst, src, 4*sizeof(SkPoint));
420        } else {
421            SkScalar    t = tValues[0];
422            SkPoint     tmp[4];
423
424            for (int i = 0; i < roots; i++) {
425                SkChopCubicAt(src, dst, t);
426                if (i == roots - 1) {
427                    break;
428                }
429
430                dst += 3;
431                // have src point to the remaining cubic (after the chop)
432                memcpy(tmp, dst, 4 * sizeof(SkPoint));
433                src = tmp;
434
435                // watch out in case the renormalized t isn't in range
436                if (!valid_unit_divide(tValues[i+1] - tValues[i],
437                                       SK_Scalar1 - tValues[i], &t)) {
438                    // if we can't, just create a degenerate cubic
439                    dst[4] = dst[5] = dst[6] = src[3];
440                    break;
441                }
442            }
443        }
444    }
445}
446
447void SkChopCubicAtHalf(const SkPoint src[4], SkPoint dst[7]) {
448    SkChopCubicAt(src, dst, 0.5f);
449}
450
451static void flatten_double_cubic_extrema(SkScalar coords[14]) {
452    coords[4] = coords[8] = coords[6];
453}
454
455/** Given 4 points on a cubic bezier, chop it into 1, 2, 3 beziers such that
456    the resulting beziers are monotonic in Y. This is called by the scan
457    converter.  Depending on what is returned, dst[] is treated as follows:
458    0   dst[0..3] is the original cubic
459    1   dst[0..3] and dst[3..6] are the two new cubics
460    2   dst[0..3], dst[3..6], dst[6..9] are the three new cubics
461    If dst == null, it is ignored and only the count is returned.
462*/
463int SkChopCubicAtYExtrema(const SkPoint src[4], SkPoint dst[10]) {
464    SkScalar    tValues[2];
465    int         roots = SkFindCubicExtrema(src[0].fY, src[1].fY, src[2].fY,
466                                           src[3].fY, tValues);
467
468    SkChopCubicAt(src, dst, tValues, roots);
469    if (dst && roots > 0) {
470        // we do some cleanup to ensure our Y extrema are flat
471        flatten_double_cubic_extrema(&dst[0].fY);
472        if (roots == 2) {
473            flatten_double_cubic_extrema(&dst[3].fY);
474        }
475    }
476    return roots;
477}
478
479int SkChopCubicAtXExtrema(const SkPoint src[4], SkPoint dst[10]) {
480    SkScalar    tValues[2];
481    int         roots = SkFindCubicExtrema(src[0].fX, src[1].fX, src[2].fX,
482                                           src[3].fX, tValues);
483
484    SkChopCubicAt(src, dst, tValues, roots);
485    if (dst && roots > 0) {
486        // we do some cleanup to ensure our Y extrema are flat
487        flatten_double_cubic_extrema(&dst[0].fX);
488        if (roots == 2) {
489            flatten_double_cubic_extrema(&dst[3].fX);
490        }
491    }
492    return roots;
493}
494
495/** http://www.faculty.idc.ac.il/arik/quality/appendixA.html
496
497    Inflection means that curvature is zero.
498    Curvature is [F' x F''] / [F'^3]
499    So we solve F'x X F''y - F'y X F''y == 0
500    After some canceling of the cubic term, we get
501    A = b - a
502    B = c - 2b + a
503    C = d - 3c + 3b - a
504    (BxCy - ByCx)t^2 + (AxCy - AyCx)t + AxBy - AyBx == 0
505*/
506int SkFindCubicInflections(const SkPoint src[4], SkScalar tValues[]) {
507    SkScalar    Ax = src[1].fX - src[0].fX;
508    SkScalar    Ay = src[1].fY - src[0].fY;
509    SkScalar    Bx = src[2].fX - 2 * src[1].fX + src[0].fX;
510    SkScalar    By = src[2].fY - 2 * src[1].fY + src[0].fY;
511    SkScalar    Cx = src[3].fX + 3 * (src[1].fX - src[2].fX) - src[0].fX;
512    SkScalar    Cy = src[3].fY + 3 * (src[1].fY - src[2].fY) - src[0].fY;
513
514    return SkFindUnitQuadRoots(Bx*Cy - By*Cx,
515                               Ax*Cy - Ay*Cx,
516                               Ax*By - Ay*Bx,
517                               tValues);
518}
519
520int SkChopCubicAtInflections(const SkPoint src[], SkPoint dst[10]) {
521    SkScalar    tValues[2];
522    int         count = SkFindCubicInflections(src, tValues);
523
524    if (dst) {
525        if (count == 0) {
526            memcpy(dst, src, 4 * sizeof(SkPoint));
527        } else {
528            SkChopCubicAt(src, dst, tValues, count);
529        }
530    }
531    return count + 1;
532}
533
534// See http://http.developer.nvidia.com/GPUGems3/gpugems3_ch25.html (from the book GPU Gems 3)
535// discr(I) = d0^2 * (3*d1^2 - 4*d0*d2)
536// Classification:
537// discr(I) > 0        Serpentine
538// discr(I) = 0        Cusp
539// discr(I) < 0        Loop
540// d0 = d1 = 0         Quadratic
541// d0 = d1 = d2 = 0    Line
542// p0 = p1 = p2 = p3   Point
543static SkCubicType classify_cubic(const SkPoint p[4], const SkScalar d[3]) {
544    if (p[0] == p[1] && p[0] == p[2] && p[0] == p[3]) {
545        return kPoint_SkCubicType;
546    }
547    const SkScalar discr = d[0] * d[0] * (3.f * d[1] * d[1] - 4.f * d[0] * d[2]);
548    if (discr > SK_ScalarNearlyZero) {
549        return kSerpentine_SkCubicType;
550    } else if (discr < -SK_ScalarNearlyZero) {
551        return kLoop_SkCubicType;
552    } else {
553        if (SkScalarAbs(d[0]) < SK_ScalarNearlyZero && SkScalarAbs(d[1]) < SK_ScalarNearlyZero) {
554            return ((SkScalarAbs(d[2]) < SK_ScalarNearlyZero) ? kLine_SkCubicType
555                                                              : kQuadratic_SkCubicType);
556        } else {
557            return kCusp_SkCubicType;
558        }
559    }
560}
561
562// Assumes the third component of points is 1.
563// Calcs p0 . (p1 x p2)
564static SkScalar calc_dot_cross_cubic(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) {
565    const SkScalar xComp = p0.fX * (p1.fY - p2.fY);
566    const SkScalar yComp = p0.fY * (p2.fX - p1.fX);
567    const SkScalar wComp = p1.fX * p2.fY - p1.fY * p2.fX;
568    return (xComp + yComp + wComp);
569}
570
571// Calc coefficients of I(s,t) where roots of I are inflection points of curve
572// I(s,t) = t*(3*d0*s^2 - 3*d1*s*t + d2*t^2)
573// d0 = a1 - 2*a2+3*a3
574// d1 = -a2 + 3*a3
575// d2 = 3*a3
576// a1 = p0 . (p3 x p2)
577// a2 = p1 . (p0 x p3)
578// a3 = p2 . (p1 x p0)
579// Places the values of d1, d2, d3 in array d passed in
580static void calc_cubic_inflection_func(const SkPoint p[4], SkScalar d[3]) {
581    SkScalar a1 = calc_dot_cross_cubic(p[0], p[3], p[2]);
582    SkScalar a2 = calc_dot_cross_cubic(p[1], p[0], p[3]);
583    SkScalar a3 = calc_dot_cross_cubic(p[2], p[1], p[0]);
584
585    // need to scale a's or values in later calculations will grow to high
586    SkScalar max = SkScalarAbs(a1);
587    max = SkMaxScalar(max, SkScalarAbs(a2));
588    max = SkMaxScalar(max, SkScalarAbs(a3));
589    max = 1.f/max;
590    a1 = a1 * max;
591    a2 = a2 * max;
592    a3 = a3 * max;
593
594    d[2] = 3.f * a3;
595    d[1] = d[2] - a2;
596    d[0] = d[1] - a2 + a1;
597}
598
599SkCubicType SkClassifyCubic(const SkPoint src[4], SkScalar d[3]) {
600    calc_cubic_inflection_func(src, d);
601    return classify_cubic(src, d);
602}
603
604template <typename T> void bubble_sort(T array[], int count) {
605    for (int i = count - 1; i > 0; --i)
606        for (int j = i; j > 0; --j)
607            if (array[j] < array[j-1])
608            {
609                T   tmp(array[j]);
610                array[j] = array[j-1];
611                array[j-1] = tmp;
612            }
613}
614
615/**
616 *  Given an array and count, remove all pair-wise duplicates from the array,
617 *  keeping the existing sorting, and return the new count
618 */
619static int collaps_duplicates(SkScalar array[], int count) {
620    for (int n = count; n > 1; --n) {
621        if (array[0] == array[1]) {
622            for (int i = 1; i < n; ++i) {
623                array[i - 1] = array[i];
624            }
625            count -= 1;
626        } else {
627            array += 1;
628        }
629    }
630    return count;
631}
632
633#ifdef SK_DEBUG
634
635#define TEST_COLLAPS_ENTRY(array)   array, SK_ARRAY_COUNT(array)
636
637static void test_collaps_duplicates() {
638    static bool gOnce;
639    if (gOnce) { return; }
640    gOnce = true;
641    const SkScalar src0[] = { 0 };
642    const SkScalar src1[] = { 0, 0 };
643    const SkScalar src2[] = { 0, 1 };
644    const SkScalar src3[] = { 0, 0, 0 };
645    const SkScalar src4[] = { 0, 0, 1 };
646    const SkScalar src5[] = { 0, 1, 1 };
647    const SkScalar src6[] = { 0, 1, 2 };
648    const struct {
649        const SkScalar* fData;
650        int fCount;
651        int fCollapsedCount;
652    } data[] = {
653        { TEST_COLLAPS_ENTRY(src0), 1 },
654        { TEST_COLLAPS_ENTRY(src1), 1 },
655        { TEST_COLLAPS_ENTRY(src2), 2 },
656        { TEST_COLLAPS_ENTRY(src3), 1 },
657        { TEST_COLLAPS_ENTRY(src4), 2 },
658        { TEST_COLLAPS_ENTRY(src5), 2 },
659        { TEST_COLLAPS_ENTRY(src6), 3 },
660    };
661    for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) {
662        SkScalar dst[3];
663        memcpy(dst, data[i].fData, data[i].fCount * sizeof(dst[0]));
664        int count = collaps_duplicates(dst, data[i].fCount);
665        SkASSERT(data[i].fCollapsedCount == count);
666        for (int j = 1; j < count; ++j) {
667            SkASSERT(dst[j-1] < dst[j]);
668        }
669    }
670}
671#endif
672
673static SkScalar SkScalarCubeRoot(SkScalar x) {
674    return SkScalarPow(x, 0.3333333f);
675}
676
677/*  Solve coeff(t) == 0, returning the number of roots that
678    lie withing 0 < t < 1.
679    coeff[0]t^3 + coeff[1]t^2 + coeff[2]t + coeff[3]
680
681    Eliminates repeated roots (so that all tValues are distinct, and are always
682    in increasing order.
683*/
684static int solve_cubic_poly(const SkScalar coeff[4], SkScalar tValues[3]) {
685    if (SkScalarNearlyZero(coeff[0])) {  // we're just a quadratic
686        return SkFindUnitQuadRoots(coeff[1], coeff[2], coeff[3], tValues);
687    }
688
689    SkScalar a, b, c, Q, R;
690
691    {
692        SkASSERT(coeff[0] != 0);
693
694        SkScalar inva = SkScalarInvert(coeff[0]);
695        a = coeff[1] * inva;
696        b = coeff[2] * inva;
697        c = coeff[3] * inva;
698    }
699    Q = (a*a - b*3) / 9;
700    R = (2*a*a*a - 9*a*b + 27*c) / 54;
701
702    SkScalar Q3 = Q * Q * Q;
703    SkScalar R2MinusQ3 = R * R - Q3;
704    SkScalar adiv3 = a / 3;
705
706    SkScalar*   roots = tValues;
707    SkScalar    r;
708
709    if (R2MinusQ3 < 0) { // we have 3 real roots
710        // the divide/root can, due to finite precisions, be slightly outside of -1...1
711        SkScalar theta = SkScalarACos(SkScalarPin(R / SkScalarSqrt(Q3), -1, 1));
712        SkScalar neg2RootQ = -2 * SkScalarSqrt(Q);
713
714        r = neg2RootQ * SkScalarCos(theta/3) - adiv3;
715        if (is_unit_interval(r)) {
716            *roots++ = r;
717        }
718        r = neg2RootQ * SkScalarCos((theta + 2*SK_ScalarPI)/3) - adiv3;
719        if (is_unit_interval(r)) {
720            *roots++ = r;
721        }
722        r = neg2RootQ * SkScalarCos((theta - 2*SK_ScalarPI)/3) - adiv3;
723        if (is_unit_interval(r)) {
724            *roots++ = r;
725        }
726        SkDEBUGCODE(test_collaps_duplicates();)
727
728        // now sort the roots
729        int count = (int)(roots - tValues);
730        SkASSERT((unsigned)count <= 3);
731        bubble_sort(tValues, count);
732        count = collaps_duplicates(tValues, count);
733        roots = tValues + count;    // so we compute the proper count below
734    } else {              // we have 1 real root
735        SkScalar A = SkScalarAbs(R) + SkScalarSqrt(R2MinusQ3);
736        A = SkScalarCubeRoot(A);
737        if (R > 0) {
738            A = -A;
739        }
740        if (A != 0) {
741            A += Q / A;
742        }
743        r = A - adiv3;
744        if (is_unit_interval(r)) {
745            *roots++ = r;
746        }
747    }
748
749    return (int)(roots - tValues);
750}
751
752/*  Looking for F' dot F'' == 0
753
754    A = b - a
755    B = c - 2b + a
756    C = d - 3c + 3b - a
757
758    F' = 3Ct^2 + 6Bt + 3A
759    F'' = 6Ct + 6B
760
761    F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
762*/
763static void formulate_F1DotF2(const SkScalar src[], SkScalar coeff[4]) {
764    SkScalar    a = src[2] - src[0];
765    SkScalar    b = src[4] - 2 * src[2] + src[0];
766    SkScalar    c = src[6] + 3 * (src[2] - src[4]) - src[0];
767
768    coeff[0] = c * c;
769    coeff[1] = 3 * b * c;
770    coeff[2] = 2 * b * b + c * a;
771    coeff[3] = a * b;
772}
773
774/*  Looking for F' dot F'' == 0
775
776    A = b - a
777    B = c - 2b + a
778    C = d - 3c + 3b - a
779
780    F' = 3Ct^2 + 6Bt + 3A
781    F'' = 6Ct + 6B
782
783    F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
784*/
785int SkFindCubicMaxCurvature(const SkPoint src[4], SkScalar tValues[3]) {
786    SkScalar coeffX[4], coeffY[4];
787    int      i;
788
789    formulate_F1DotF2(&src[0].fX, coeffX);
790    formulate_F1DotF2(&src[0].fY, coeffY);
791
792    for (i = 0; i < 4; i++) {
793        coeffX[i] += coeffY[i];
794    }
795
796    SkScalar    t[3];
797    int         count = solve_cubic_poly(coeffX, t);
798    int         maxCount = 0;
799
800    // now remove extrema where the curvature is zero (mins)
801    // !!!! need a test for this !!!!
802    for (i = 0; i < count; i++) {
803        // if (not_min_curvature())
804        if (t[i] > 0 && t[i] < SK_Scalar1) {
805            tValues[maxCount++] = t[i];
806        }
807    }
808    return maxCount;
809}
810
811int SkChopCubicAtMaxCurvature(const SkPoint src[4], SkPoint dst[13],
812                              SkScalar tValues[3]) {
813    SkScalar    t_storage[3];
814
815    if (tValues == nullptr) {
816        tValues = t_storage;
817    }
818
819    int count = SkFindCubicMaxCurvature(src, tValues);
820
821    if (dst) {
822        if (count == 0) {
823            memcpy(dst, src, 4 * sizeof(SkPoint));
824        } else {
825            SkChopCubicAt(src, dst, tValues, count);
826        }
827    }
828    return count + 1;
829}
830
831#include "../pathops/SkPathOpsCubic.h"
832
833typedef int (SkDCubic::*InterceptProc)(double intercept, double roots[3]) const;
834
835static bool cubic_dchop_at_intercept(const SkPoint src[4], SkScalar intercept, SkPoint dst[7],
836                                     InterceptProc method) {
837    SkDCubic cubic;
838    double roots[3];
839    int count = (cubic.set(src).*method)(intercept, roots);
840    if (count > 0) {
841        SkDCubicPair pair = cubic.chopAt(roots[0]);
842        for (int i = 0; i < 7; ++i) {
843            dst[i] = pair.pts[i].asSkPoint();
844        }
845        return true;
846    }
847    return false;
848}
849
850bool SkChopMonoCubicAtY(SkPoint src[4], SkScalar y, SkPoint dst[7]) {
851    return cubic_dchop_at_intercept(src, y, dst, &SkDCubic::horizontalIntersect);
852}
853
854bool SkChopMonoCubicAtX(SkPoint src[4], SkScalar x, SkPoint dst[7]) {
855    return cubic_dchop_at_intercept(src, x, dst, &SkDCubic::verticalIntersect);
856}
857
858///////////////////////////////////////////////////////////////////////////////
859//
860// NURB representation for conics.  Helpful explanations at:
861//
862// http://citeseerx.ist.psu.edu/viewdoc/
863//   download?doi=10.1.1.44.5740&rep=rep1&type=ps
864// and
865// http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/NURBS/RB-conics.html
866//
867// F = (A (1 - t)^2 + C t^2 + 2 B (1 - t) t w)
868//     ------------------------------------------
869//         ((1 - t)^2 + t^2 + 2 (1 - t) t w)
870//
871//   = {t^2 (P0 + P2 - 2 P1 w), t (-2 P0 + 2 P1 w), P0}
872//     ------------------------------------------------
873//             {t^2 (2 - 2 w), t (-2 + 2 w), 1}
874//
875
876// F' = 2 (C t (1 + t (-1 + w)) - A (-1 + t) (t (-1 + w) - w) + B (1 - 2 t) w)
877//
878//  t^2 : (2 P0 - 2 P2 - 2 P0 w + 2 P2 w)
879//  t^1 : (-2 P0 + 2 P2 + 4 P0 w - 4 P1 w)
880//  t^0 : -2 P0 w + 2 P1 w
881//
882//  We disregard magnitude, so we can freely ignore the denominator of F', and
883//  divide the numerator by 2
884//
885//    coeff[0] for t^2
886//    coeff[1] for t^1
887//    coeff[2] for t^0
888//
889static void conic_deriv_coeff(const SkScalar src[],
890                              SkScalar w,
891                              SkScalar coeff[3]) {
892    const SkScalar P20 = src[4] - src[0];
893    const SkScalar P10 = src[2] - src[0];
894    const SkScalar wP10 = w * P10;
895    coeff[0] = w * P20 - P20;
896    coeff[1] = P20 - 2 * wP10;
897    coeff[2] = wP10;
898}
899
900static bool conic_find_extrema(const SkScalar src[], SkScalar w, SkScalar* t) {
901    SkScalar coeff[3];
902    conic_deriv_coeff(src, w, coeff);
903
904    SkScalar tValues[2];
905    int roots = SkFindUnitQuadRoots(coeff[0], coeff[1], coeff[2], tValues);
906    SkASSERT(0 == roots || 1 == roots);
907
908    if (1 == roots) {
909        *t = tValues[0];
910        return true;
911    }
912    return false;
913}
914
915struct SkP3D {
916    SkScalar fX, fY, fZ;
917
918    void set(SkScalar x, SkScalar y, SkScalar z) {
919        fX = x; fY = y; fZ = z;
920    }
921
922    void projectDown(SkPoint* dst) const {
923        dst->set(fX / fZ, fY / fZ);
924    }
925};
926
927// We only interpolate one dimension at a time (the first, at +0, +3, +6).
928static void p3d_interp(const SkScalar src[7], SkScalar dst[7], SkScalar t) {
929    SkScalar ab = SkScalarInterp(src[0], src[3], t);
930    SkScalar bc = SkScalarInterp(src[3], src[6], t);
931    dst[0] = ab;
932    dst[3] = SkScalarInterp(ab, bc, t);
933    dst[6] = bc;
934}
935
936static void ratquad_mapTo3D(const SkPoint src[3], SkScalar w, SkP3D dst[]) {
937    dst[0].set(src[0].fX * 1, src[0].fY * 1, 1);
938    dst[1].set(src[1].fX * w, src[1].fY * w, w);
939    dst[2].set(src[2].fX * 1, src[2].fY * 1, 1);
940}
941
942// return false if infinity or NaN is generated; caller must check
943bool SkConic::chopAt(SkScalar t, SkConic dst[2]) const {
944    SkP3D tmp[3], tmp2[3];
945
946    ratquad_mapTo3D(fPts, fW, tmp);
947
948    p3d_interp(&tmp[0].fX, &tmp2[0].fX, t);
949    p3d_interp(&tmp[0].fY, &tmp2[0].fY, t);
950    p3d_interp(&tmp[0].fZ, &tmp2[0].fZ, t);
951
952    dst[0].fPts[0] = fPts[0];
953    tmp2[0].projectDown(&dst[0].fPts[1]);
954    tmp2[1].projectDown(&dst[0].fPts[2]); dst[1].fPts[0] = dst[0].fPts[2];
955    tmp2[2].projectDown(&dst[1].fPts[1]);
956    dst[1].fPts[2] = fPts[2];
957
958    // to put in "standard form", where w0 and w2 are both 1, we compute the
959    // new w1 as sqrt(w1*w1/w0*w2)
960    // or
961    // w1 /= sqrt(w0*w2)
962    //
963    // However, in our case, we know that for dst[0]:
964    //     w0 == 1, and for dst[1], w2 == 1
965    //
966    SkScalar root = SkScalarSqrt(tmp2[1].fZ);
967    dst[0].fW = tmp2[0].fZ / root;
968    dst[1].fW = tmp2[2].fZ / root;
969    SkASSERT(sizeof(dst[0]) == sizeof(SkScalar) * 7);
970    SkASSERT(0 == offsetof(SkConic, fPts[0].fX));
971    return SkScalarsAreFinite(&dst[0].fPts[0].fX, 7 * 2);
972}
973
974void SkConic::chopAt(SkScalar t1, SkScalar t2, SkConic* dst) const {
975    if (0 == t1 || 1 == t2) {
976        if (0 == t1 && 1 == t2) {
977            *dst = *this;
978            return;
979        } else {
980            SkConic pair[2];
981            if (this->chopAt(t1 ? t1 : t2, pair)) {
982                *dst = pair[SkToBool(t1)];
983                return;
984            }
985        }
986    }
987    SkConicCoeff coeff(*this);
988    Sk2s tt1(t1);
989    Sk2s aXY = coeff.fNumer.eval(tt1);
990    Sk2s aZZ = coeff.fDenom.eval(tt1);
991    Sk2s midTT((t1 + t2) / 2);
992    Sk2s dXY = coeff.fNumer.eval(midTT);
993    Sk2s dZZ = coeff.fDenom.eval(midTT);
994    Sk2s tt2(t2);
995    Sk2s cXY = coeff.fNumer.eval(tt2);
996    Sk2s cZZ = coeff.fDenom.eval(tt2);
997    Sk2s bXY = times_2(dXY) - (aXY + cXY) * Sk2s(0.5f);
998    Sk2s bZZ = times_2(dZZ) - (aZZ + cZZ) * Sk2s(0.5f);
999    dst->fPts[0] = to_point(aXY / aZZ);
1000    dst->fPts[1] = to_point(bXY / bZZ);
1001    dst->fPts[2] = to_point(cXY / cZZ);
1002    Sk2s ww = bZZ / (aZZ * cZZ).sqrt();
1003    dst->fW = ww[0];
1004}
1005
1006SkPoint SkConic::evalAt(SkScalar t) const {
1007    return to_point(SkConicCoeff(*this).eval(t));
1008}
1009
1010SkVector SkConic::evalTangentAt(SkScalar t) const {
1011    // The derivative equation returns a zero tangent vector when t is 0 or 1,
1012    // and the control point is equal to the end point.
1013    // In this case, use the conic endpoints to compute the tangent.
1014    if ((t == 0 && fPts[0] == fPts[1]) || (t == 1 && fPts[1] == fPts[2])) {
1015        return fPts[2] - fPts[0];
1016    }
1017    Sk2s p0 = from_point(fPts[0]);
1018    Sk2s p1 = from_point(fPts[1]);
1019    Sk2s p2 = from_point(fPts[2]);
1020    Sk2s ww(fW);
1021
1022    Sk2s p20 = p2 - p0;
1023    Sk2s p10 = p1 - p0;
1024
1025    Sk2s C = ww * p10;
1026    Sk2s A = ww * p20 - p20;
1027    Sk2s B = p20 - C - C;
1028
1029    return to_vector(SkQuadCoeff(A, B, C).eval(t));
1030}
1031
1032void SkConic::evalAt(SkScalar t, SkPoint* pt, SkVector* tangent) const {
1033    SkASSERT(t >= 0 && t <= SK_Scalar1);
1034
1035    if (pt) {
1036        *pt = this->evalAt(t);
1037    }
1038    if (tangent) {
1039        *tangent = this->evalTangentAt(t);
1040    }
1041}
1042
1043static SkScalar subdivide_w_value(SkScalar w) {
1044    return SkScalarSqrt(SK_ScalarHalf + w * SK_ScalarHalf);
1045}
1046
1047void SkConic::chop(SkConic * SK_RESTRICT dst) const {
1048    Sk2s scale = Sk2s(SkScalarInvert(SK_Scalar1 + fW));
1049    SkScalar newW = subdivide_w_value(fW);
1050
1051    Sk2s p0 = from_point(fPts[0]);
1052    Sk2s p1 = from_point(fPts[1]);
1053    Sk2s p2 = from_point(fPts[2]);
1054    Sk2s ww(fW);
1055
1056    Sk2s wp1 = ww * p1;
1057    Sk2s m = (p0 + times_2(wp1) + p2) * scale * Sk2s(0.5f);
1058
1059    dst[0].fPts[0] = fPts[0];
1060    dst[0].fPts[1] = to_point((p0 + wp1) * scale);
1061    dst[0].fPts[2] = dst[1].fPts[0] = to_point(m);
1062    dst[1].fPts[1] = to_point((wp1 + p2) * scale);
1063    dst[1].fPts[2] = fPts[2];
1064
1065    dst[0].fW = dst[1].fW = newW;
1066}
1067
1068/*
1069 *  "High order approximation of conic sections by quadratic splines"
1070 *      by Michael Floater, 1993
1071 */
1072#define AS_QUAD_ERROR_SETUP                                         \
1073    SkScalar a = fW - 1;                                            \
1074    SkScalar k = a / (4 * (2 + a));                                 \
1075    SkScalar x = k * (fPts[0].fX - 2 * fPts[1].fX + fPts[2].fX);    \
1076    SkScalar y = k * (fPts[0].fY - 2 * fPts[1].fY + fPts[2].fY);
1077
1078void SkConic::computeAsQuadError(SkVector* err) const {
1079    AS_QUAD_ERROR_SETUP
1080    err->set(x, y);
1081}
1082
1083bool SkConic::asQuadTol(SkScalar tol) const {
1084    AS_QUAD_ERROR_SETUP
1085    return (x * x + y * y) <= tol * tol;
1086}
1087
1088// Limit the number of suggested quads to approximate a conic
1089#define kMaxConicToQuadPOW2     5
1090
1091int SkConic::computeQuadPOW2(SkScalar tol) const {
1092    if (tol < 0 || !SkScalarIsFinite(tol)) {
1093        return 0;
1094    }
1095
1096    AS_QUAD_ERROR_SETUP
1097
1098    SkScalar error = SkScalarSqrt(x * x + y * y);
1099    int pow2;
1100    for (pow2 = 0; pow2 < kMaxConicToQuadPOW2; ++pow2) {
1101        if (error <= tol) {
1102            break;
1103        }
1104        error *= 0.25f;
1105    }
1106    // float version -- using ceil gives the same results as the above.
1107    if (false) {
1108        SkScalar err = SkScalarSqrt(x * x + y * y);
1109        if (err <= tol) {
1110            return 0;
1111        }
1112        SkScalar tol2 = tol * tol;
1113        if (tol2 == 0) {
1114            return kMaxConicToQuadPOW2;
1115        }
1116        SkScalar fpow2 = SkScalarLog2((x * x + y * y) / tol2) * 0.25f;
1117        int altPow2 = SkScalarCeilToInt(fpow2);
1118        if (altPow2 != pow2) {
1119            SkDebugf("pow2 %d altPow2 %d fbits %g err %g tol %g\n", pow2, altPow2, fpow2, err, tol);
1120        }
1121        pow2 = altPow2;
1122    }
1123    return pow2;
1124}
1125
1126// This was originally developed and tested for pathops: see SkOpTypes.h
1127// returns true if (a <= b <= c) || (a >= b >= c)
1128static bool between(SkScalar a, SkScalar b, SkScalar c) {
1129    return (a - b) * (c - b) <= 0;
1130}
1131
1132static SkPoint* subdivide(const SkConic& src, SkPoint pts[], int level) {
1133    SkASSERT(level >= 0);
1134
1135    if (0 == level) {
1136        memcpy(pts, &src.fPts[1], 2 * sizeof(SkPoint));
1137        return pts + 2;
1138    } else {
1139        SkConic dst[2];
1140        src.chop(dst);
1141        const SkScalar startY = src.fPts[0].fY;
1142        const SkScalar endY = src.fPts[2].fY;
1143        if (between(startY, src.fPts[1].fY, endY)) {
1144            // If the input is monotonic and the output is not, the scan converter hangs.
1145            // Ensure that the chopped conics maintain their y-order.
1146            SkScalar midY = dst[0].fPts[2].fY;
1147            if (!between(startY, midY, endY)) {
1148                // If the computed midpoint is outside the ends, move it to the closer one.
1149                SkScalar closerY = SkTAbs(midY - startY) < SkTAbs(midY - endY) ? startY : endY;
1150                dst[0].fPts[2].fY = dst[1].fPts[0].fY = closerY;
1151            }
1152            if (!between(startY, dst[0].fPts[1].fY, dst[0].fPts[2].fY)) {
1153                // If the 1st control is not between the start and end, put it at the start.
1154                // This also reduces the quad to a line.
1155                dst[0].fPts[1].fY = startY;
1156            }
1157            if (!between(dst[1].fPts[0].fY, dst[1].fPts[1].fY, endY)) {
1158                // If the 2nd control is not between the start and end, put it at the end.
1159                // This also reduces the quad to a line.
1160                dst[1].fPts[1].fY = endY;
1161            }
1162            // Verify that all five points are in order.
1163            SkASSERT(between(startY, dst[0].fPts[1].fY, dst[0].fPts[2].fY));
1164            SkASSERT(between(dst[0].fPts[1].fY, dst[0].fPts[2].fY, dst[1].fPts[1].fY));
1165            SkASSERT(between(dst[0].fPts[2].fY, dst[1].fPts[1].fY, endY));
1166        }
1167        --level;
1168        pts = subdivide(dst[0], pts, level);
1169        return subdivide(dst[1], pts, level);
1170    }
1171}
1172
1173int SkConic::chopIntoQuadsPOW2(SkPoint pts[], int pow2) const {
1174    SkASSERT(pow2 >= 0);
1175    *pts = fPts[0];
1176    SkDEBUGCODE(SkPoint* endPts);
1177    if (pow2 == kMaxConicToQuadPOW2) {  // If an extreme weight generates many quads ...
1178        SkConic dst[2];
1179        this->chop(dst);
1180        // check to see if the first chop generates a pair of lines
1181        if (dst[0].fPts[1].equalsWithinTolerance(dst[0].fPts[2])
1182                && dst[1].fPts[0].equalsWithinTolerance(dst[1].fPts[1])) {
1183            pts[1] = pts[2] = pts[3] = dst[0].fPts[1];  // set ctrl == end to make lines
1184            pts[4] = dst[1].fPts[2];
1185            pow2 = 1;
1186            SkDEBUGCODE(endPts = &pts[5]);
1187            goto commonFinitePtCheck;
1188        }
1189    }
1190    SkDEBUGCODE(endPts = ) subdivide(*this, pts + 1, pow2);
1191commonFinitePtCheck:
1192    const int quadCount = 1 << pow2;
1193    const int ptCount = 2 * quadCount + 1;
1194    SkASSERT(endPts - pts == ptCount);
1195    if (!SkPointsAreFinite(pts, ptCount)) {
1196        // if we generated a non-finite, pin ourselves to the middle of the hull,
1197        // as our first and last are already on the first/last pts of the hull.
1198        for (int i = 1; i < ptCount - 1; ++i) {
1199            pts[i] = fPts[1];
1200        }
1201    }
1202    return 1 << pow2;
1203}
1204
1205bool SkConic::findXExtrema(SkScalar* t) const {
1206    return conic_find_extrema(&fPts[0].fX, fW, t);
1207}
1208
1209bool SkConic::findYExtrema(SkScalar* t) const {
1210    return conic_find_extrema(&fPts[0].fY, fW, t);
1211}
1212
1213bool SkConic::chopAtXExtrema(SkConic dst[2]) const {
1214    SkScalar t;
1215    if (this->findXExtrema(&t)) {
1216        if (!this->chopAt(t, dst)) {
1217            // if chop can't return finite values, don't chop
1218            return false;
1219        }
1220        // now clean-up the middle, since we know t was meant to be at
1221        // an X-extrema
1222        SkScalar value = dst[0].fPts[2].fX;
1223        dst[0].fPts[1].fX = value;
1224        dst[1].fPts[0].fX = value;
1225        dst[1].fPts[1].fX = value;
1226        return true;
1227    }
1228    return false;
1229}
1230
1231bool SkConic::chopAtYExtrema(SkConic dst[2]) const {
1232    SkScalar t;
1233    if (this->findYExtrema(&t)) {
1234        if (!this->chopAt(t, dst)) {
1235            // if chop can't return finite values, don't chop
1236            return false;
1237        }
1238        // now clean-up the middle, since we know t was meant to be at
1239        // an Y-extrema
1240        SkScalar value = dst[0].fPts[2].fY;
1241        dst[0].fPts[1].fY = value;
1242        dst[1].fPts[0].fY = value;
1243        dst[1].fPts[1].fY = value;
1244        return true;
1245    }
1246    return false;
1247}
1248
1249void SkConic::computeTightBounds(SkRect* bounds) const {
1250    SkPoint pts[4];
1251    pts[0] = fPts[0];
1252    pts[1] = fPts[2];
1253    int count = 2;
1254
1255    SkScalar t;
1256    if (this->findXExtrema(&t)) {
1257        this->evalAt(t, &pts[count++]);
1258    }
1259    if (this->findYExtrema(&t)) {
1260        this->evalAt(t, &pts[count++]);
1261    }
1262    bounds->set(pts, count);
1263}
1264
1265void SkConic::computeFastBounds(SkRect* bounds) const {
1266    bounds->set(fPts, 3);
1267}
1268
1269#if 0  // unimplemented
1270bool SkConic::findMaxCurvature(SkScalar* t) const {
1271    // TODO: Implement me
1272    return false;
1273}
1274#endif
1275
1276SkScalar SkConic::TransformW(const SkPoint pts[], SkScalar w,
1277                             const SkMatrix& matrix) {
1278    if (!matrix.hasPerspective()) {
1279        return w;
1280    }
1281
1282    SkP3D src[3], dst[3];
1283
1284    ratquad_mapTo3D(pts, w, src);
1285
1286    matrix.mapHomogeneousPoints(&dst[0].fX, &src[0].fX, 3);
1287
1288    // w' = sqrt(w1*w1/w0*w2)
1289    SkScalar w0 = dst[0].fZ;
1290    SkScalar w1 = dst[1].fZ;
1291    SkScalar w2 = dst[2].fZ;
1292    w = SkScalarSqrt((w1 * w1) / (w0 * w2));
1293    return w;
1294}
1295
1296int SkConic::BuildUnitArc(const SkVector& uStart, const SkVector& uStop, SkRotationDirection dir,
1297                          const SkMatrix* userMatrix, SkConic dst[kMaxConicsForArc]) {
1298    // rotate by x,y so that uStart is (1.0)
1299    SkScalar x = SkPoint::DotProduct(uStart, uStop);
1300    SkScalar y = SkPoint::CrossProduct(uStart, uStop);
1301
1302    SkScalar absY = SkScalarAbs(y);
1303
1304    // check for (effectively) coincident vectors
1305    // this can happen if our angle is nearly 0 or nearly 180 (y == 0)
1306    // ... we use the dot-prod to distinguish between 0 and 180 (x > 0)
1307    if (absY <= SK_ScalarNearlyZero && x > 0 && ((y >= 0 && kCW_SkRotationDirection == dir) ||
1308                                                 (y <= 0 && kCCW_SkRotationDirection == dir))) {
1309        return 0;
1310    }
1311
1312    if (dir == kCCW_SkRotationDirection) {
1313        y = -y;
1314    }
1315
1316    // We decide to use 1-conic per quadrant of a circle. What quadrant does [xy] lie in?
1317    //      0 == [0  .. 90)
1318    //      1 == [90 ..180)
1319    //      2 == [180..270)
1320    //      3 == [270..360)
1321    //
1322    int quadrant = 0;
1323    if (0 == y) {
1324        quadrant = 2;        // 180
1325        SkASSERT(SkScalarAbs(x + SK_Scalar1) <= SK_ScalarNearlyZero);
1326    } else if (0 == x) {
1327        SkASSERT(absY - SK_Scalar1 <= SK_ScalarNearlyZero);
1328        quadrant = y > 0 ? 1 : 3; // 90 : 270
1329    } else {
1330        if (y < 0) {
1331            quadrant += 2;
1332        }
1333        if ((x < 0) != (y < 0)) {
1334            quadrant += 1;
1335        }
1336    }
1337
1338    const SkPoint quadrantPts[] = {
1339        { 1, 0 }, { 1, 1 }, { 0, 1 }, { -1, 1 }, { -1, 0 }, { -1, -1 }, { 0, -1 }, { 1, -1 }
1340    };
1341    const SkScalar quadrantWeight = SK_ScalarRoot2Over2;
1342
1343    int conicCount = quadrant;
1344    for (int i = 0; i < conicCount; ++i) {
1345        dst[i].set(&quadrantPts[i * 2], quadrantWeight);
1346    }
1347
1348    // Now compute any remaing (sub-90-degree) arc for the last conic
1349    const SkPoint finalP = { x, y };
1350    const SkPoint& lastQ = quadrantPts[quadrant * 2];  // will already be a unit-vector
1351    const SkScalar dot = SkVector::DotProduct(lastQ, finalP);
1352    SkASSERT(0 <= dot && dot <= SK_Scalar1 + SK_ScalarNearlyZero);
1353
1354    if (dot < 1) {
1355        SkVector offCurve = { lastQ.x() + x, lastQ.y() + y };
1356        // compute the bisector vector, and then rescale to be the off-curve point.
1357        // we compute its length from cos(theta/2) = length / 1, using half-angle identity we get
1358        // length = sqrt(2 / (1 + cos(theta)). We already have cos() when to computed the dot.
1359        // This is nice, since our computed weight is cos(theta/2) as well!
1360        //
1361        const SkScalar cosThetaOver2 = SkScalarSqrt((1 + dot) / 2);
1362        offCurve.setLength(SkScalarInvert(cosThetaOver2));
1363        if (!lastQ.equalsWithinTolerance(offCurve)) {
1364            dst[conicCount].set(lastQ, offCurve, finalP, cosThetaOver2);
1365            conicCount += 1;
1366        }
1367    }
1368
1369    // now handle counter-clockwise and the initial unitStart rotation
1370    SkMatrix    matrix;
1371    matrix.setSinCos(uStart.fY, uStart.fX);
1372    if (dir == kCCW_SkRotationDirection) {
1373        matrix.preScale(SK_Scalar1, -SK_Scalar1);
1374    }
1375    if (userMatrix) {
1376        matrix.postConcat(*userMatrix);
1377    }
1378    for (int i = 0; i < conicCount; ++i) {
1379        matrix.mapPoints(dst[i].fPts, 3);
1380    }
1381    return conicCount;
1382}
1383