16d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com/*
26d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comhttp://stackoverflow.com/questions/2009160/how-do-i-convert-the-2-control-points-of-a-cubic-curve-to-the-single-control-poi
36d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com*/
46d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
56d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com/*
68ae714b186ae5f4eaddee239281fbfe7282320c9skia.committer@gmail.comLet's call the control points of the cubic Q0..Q3 and the control points of the quadratic P0..P2.
76d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comThen for degree elevation, the equations are:
86d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
96d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comQ0 = P0
106d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comQ1 = 1/3 P0 + 2/3 P1
116d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comQ2 = 2/3 P1 + 1/3 P2
126d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comQ3 = P2
136d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comIn your case you have Q0..Q3 and you're solving for P0..P2. There are two ways to compute P1 from
146d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com the equations above:
156d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
166d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comP1 = 3/2 Q1 - 1/2 Q0
176d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comP1 = 3/2 Q2 - 1/2 Q3
186d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comIf this is a degree-elevated cubic, then both equations will give the same answer for P1. Since
196d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com it's likely not, your best bet is to average them. So,
206d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
216d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comP1 = -1/4 Q0 + 3/4 Q1 + 3/4 Q2 - 1/4 Q3
226d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
236d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
246d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comCubic defined by: P1/2 - anchor points, C1/C2 control points
256d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com|x| is the euclidean norm of x
266d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.commid-point approx of cubic: a quad that shares the same anchors with the cubic and has the
276d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com control point at C = (3·C2 - P2 + 3·C1 - P1)/4
288ae714b186ae5f4eaddee239281fbfe7282320c9skia.committer@gmail.com
296d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comAlgorithm
306d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
316d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.compick an absolute precision (prec)
328ae714b186ae5f4eaddee239281fbfe7282320c9skia.committer@gmail.comCompute the Tdiv as the root of (cubic) equation
336d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comsqrt(3)/18 · |P2 - 3·C2 + 3·C1 - P1|/2 · Tdiv ^ 3 = prec
346d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comif Tdiv < 0.5 divide the cubic at Tdiv. First segment [0..Tdiv] can be approximated with by a
356d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com quadratic, with a defect less than prec, by the mid-point approximation.
366d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com Repeat from step 2 with the second resulted segment (corresponding to 1-Tdiv)
376d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com0.5<=Tdiv<1 - simply divide the cubic in two. The two halves can be approximated by the mid-point
386d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com approximation
396d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comTdiv>=1 - the entire cubic can be approximated by the mid-point approximation
406d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
416d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comconfirmed by (maybe stolen from)
426d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.comhttp://www.caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html
4373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com// maybe in turn derived from  http://www.cccg.ca/proceedings/2004/36.pdf
4473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com// also stored at http://www.cis.usouthal.edu/~hain/general/Publications/Bezier/bezier%20cccg04%20paper.pdf
456d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
466d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com*/
476d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
486d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com#include "CubicUtilities.h"
496d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com#include "CurveIntersection.h"
5073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com#include "LineIntersection.h"
511304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com#include "TSearch.h"
5273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com
5373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comconst bool AVERAGE_END_POINTS = true; // results in better fitting curves
5473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com
5573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com#define USE_CUBIC_END_POINTS 1
566d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
57d68bc309bbc3f0f4c3107cf4fa60ce1ff4847b75caryclark@google.comstatic double calcTDiv(const Cubic& cubic, double precision, double start) {
586d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    const double adjust = sqrt(3) / 36;
596d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    Cubic sub;
606d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    const Cubic* cPtr;
616d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    if (start == 0) {
626d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com        cPtr = &cubic;
636d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    } else {
646d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com        // OPTIMIZE: special-case half-split ?
656d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com        sub_divide(cubic, start, 1, sub);
666d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com        cPtr = &sub;
676d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    }
686d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    const Cubic& c = *cPtr;
696d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    double dx = c[3].x - 3 * (c[2].x - c[1].x) - c[0].x;
706d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    double dy = c[3].y - 3 * (c[2].y - c[1].y) - c[0].y;
716d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    double dist = sqrt(dx * dx + dy * dy);
72d68bc309bbc3f0f4c3107cf4fa60ce1ff4847b75caryclark@google.com    double tDiv3 = precision / (adjust * dist);
7373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    double t = cube_root(tDiv3);
7473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    if (start > 0) {
7573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        t = start + (1 - start) * t;
7673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    }
7773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    return t;
786d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com}
796d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
8073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comvoid demote_cubic_to_quad(const Cubic& cubic, Quadratic& quad) {
816d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    quad[0] = cubic[0];
8273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comif (AVERAGE_END_POINTS) {
8373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    const _Point fromC1 = { (3 * cubic[1].x - cubic[0].x) / 2, (3 * cubic[1].y - cubic[0].y) / 2 };
8473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    const _Point fromC2 = { (3 * cubic[2].x - cubic[3].x) / 2, (3 * cubic[2].y - cubic[3].y) / 2 };
8573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    quad[1].x = (fromC1.x + fromC2.x) / 2;
8673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    quad[1].y = (fromC1.y + fromC2.y) / 2;
8773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com} else {
8873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    lineIntersect((const _Line&) cubic[0], (const _Line&) cubic[2], quad[1]);
8973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com}
906d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    quad[2] = cubic[3];
916d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com}
926d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com
93d68bc309bbc3f0f4c3107cf4fa60ce1ff4847b75caryclark@google.comint cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<Quadratic>& quadratics) {
9473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    SkTDArray<double> ts;
9573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    cubic_to_quadratics(cubic, precision, ts);
9673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    int tsCount = ts.count();
9773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    double t1Start = 0;
9873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    int order = 0;
9973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    for (int idx = 0; idx <= tsCount; ++idx) {
10073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        double t1 = idx < tsCount ? ts[idx] : 1;
10173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        Cubic part;
10273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        sub_divide(cubic, t1Start, t1, part);
10373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        Quadratic q1;
10473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        demote_cubic_to_quad(part, q1);
10573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        Quadratic s1;
10647d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com        int o1 = reduceOrder(q1, s1, kReduceOrder_TreatAsFill);
10773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        if (order < o1) {
10873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com            order = o1;
10973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        }
11073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        memcpy(quadratics.append(), o1 < 2 ? s1 : q1, sizeof(Quadratic));
11173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        t1Start = t1;
11273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    }
11373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    return order;
11473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com}
11573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com
11673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comstatic bool addSimpleTs(const Cubic& cubic, double precision, SkTDArray<double>& ts) {
11773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    double tDiv = calcTDiv(cubic, precision, 0);
11873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    if (tDiv >= 1) {
11973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        return true;
12073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    }
12173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    if (tDiv >= 0.5) {
12273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        *ts.append() = 0.5;
12373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        return true;
12473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    }
12573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    return false;
12673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com}
12773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com
12873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comstatic void addTs(const Cubic& cubic, double precision, double start, double end,
12973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        SkTDArray<double>& ts) {
13073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    double tDiv = calcTDiv(cubic, precision, 0);
13173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    double parts = ceil(1.0 / tDiv);
13273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    for (double index = 0; index < parts; ++index) {
13373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        double newT = start + (index / parts) * (end - start);
13473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        if (newT > 0 && newT < 1) {
13573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com            *ts.append() = newT;
13673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        }
13773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    }
13873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com}
13973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com
14073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com// flavor that returns T values only, deferring computing the quads until they are needed
14105c4bad470722bc4e5e6ae3d79aa8bcf9e732f06caryclark@google.com// FIXME: when called from recursive intersect 2, this could take the original cubic
14205c4bad470722bc4e5e6ae3d79aa8bcf9e732f06caryclark@google.com// and do a more precise job when calling chop at and sub divide by computing the fractional ts.
14305c4bad470722bc4e5e6ae3d79aa8bcf9e732f06caryclark@google.com// it would still take the prechopped cubic for reduce order and find cubic inflections
14473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.comvoid cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<double>& ts) {
1456d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    Cubic reduced;
14647d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com    int order = reduceOrder(cubic, reduced, kReduceOrder_QuadraticsAllowed,
14747d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com            kReduceOrder_TreatAsFill);
1486d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    if (order < 3) {
14973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        return;
1506d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    }
1511304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    double inflectT[5];
15273ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    int inflections = find_cubic_inflections(cubic, inflectT);
15373ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    SkASSERT(inflections <= 2);
1541304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    if (!ends_are_extrema_in_x_or_y(cubic)) {
1551304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        inflections += find_cubic_max_curvature(cubic, &inflectT[inflections]);
1561304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        SkASSERT(inflections <= 5);
1571304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    }
1581304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    QSort<double>(inflectT, &inflectT[inflections - 1]);
1591304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    // OPTIMIZATION: is this filtering common enough that it needs to be pulled out into its
1601304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    // own subroutine?
1611304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    while (inflections && approximately_less_than_zero(inflectT[0])) {
1621304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        memcpy(inflectT, &inflectT[1], sizeof(inflectT[0]) * --inflections);
1631304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    }
1641304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    int start = 0;
1651304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    do {
1661304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        int next = start + 1;
1671304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        if (next >= inflections) {
1681304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com            break;
1691304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        }
1701304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        if (!approximately_equal(inflectT[start], inflectT[next])) {
1711304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com            ++start;
1721304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com            continue;
1731304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        }
1741304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        memcpy(&inflectT[start], &inflectT[next], sizeof(inflectT[0]) * (--inflections - start));
1751304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    } while (true);
1761304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    while (inflections && approximately_greater_than_one(inflectT[inflections - 1])) {
1771304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        --inflections;
1781304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    }
17945a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com    CubicPair pair;
18045a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com    if (inflections == 1) {
18145a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com        chop_at(cubic, pair, inflectT[0]);
18247d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com        int orderP1 = reduceOrder(pair.first(), reduced, kReduceOrder_NoQuadraticsAllowed,
18347d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com                kReduceOrder_TreatAsFill);
18445a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com        if (orderP1 < 2) {
18545a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com            --inflections;
18645a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com        } else {
18747d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com            int orderP2 = reduceOrder(pair.second(), reduced, kReduceOrder_NoQuadraticsAllowed,
18847d73daa7a971e7eee5822def7922f7d43b2dc47caryclark@google.com                    kReduceOrder_TreatAsFill);
18945a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com            if (orderP2 < 2) {
19045a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com                --inflections;
19145a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com            }
19245a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com        }
19345a8fc6a8b00451f807783f2a6ec640e9bcc7256caryclark@google.com    }
19473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    if (inflections == 0 && addSimpleTs(cubic, precision, ts)) {
19573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        return;
1966d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    }
19773ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    if (inflections == 1) {
19873ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        chop_at(cubic, pair, inflectT[0]);
19973ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        addTs(pair.first(), precision, 0, inflectT[0], ts);
20073ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        addTs(pair.second(), precision, inflectT[0], 1, ts);
20173ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        return;
2026d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com    }
2031304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com    if (inflections > 1) {
20473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        Cubic part;
20573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        sub_divide(cubic, 0, inflectT[0], part);
20673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        addTs(part, precision, 0, inflectT[0], ts);
2071304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        int last = inflections - 1;
2081304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        for (int idx = 0; idx < last; ++idx) {
2091304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com            sub_divide(cubic, inflectT[idx], inflectT[idx + 1], part);
2101304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com            addTs(part, precision, inflectT[idx], inflectT[idx + 1], ts);
2111304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        }
2121304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        sub_divide(cubic, inflectT[last], 1, part);
2131304bb25aa3b0baa61fc2e2900fabcef88801b59caryclark@google.com        addTs(part, precision, inflectT[last], 1, ts);
21473ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com        return;
21573ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    }
21673ca6243b31e225e9fd5b75a96cbc82d62557de6caryclark@google.com    addTs(cubic, precision, 0, 1, ts);
2176d0032a8ec680221c2a704cac2391f2a2d77546fcaryclark@google.com}
218