caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | http://stackoverflow.com/questions/2009160/how-do-i-convert-the-2-control-points-of-a-cubic-curve-to-the-single-control-poi |
| 3 | */ |
| 4 | |
| 5 | /* |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 6 | Let's call the control points of the cubic Q0..Q3 and the control points of the quadratic P0..P2. |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 7 | Then for degree elevation, the equations are: |
| 8 | |
| 9 | Q0 = P0 |
| 10 | Q1 = 1/3 P0 + 2/3 P1 |
| 11 | Q2 = 2/3 P1 + 1/3 P2 |
| 12 | Q3 = P2 |
| 13 | In your case you have Q0..Q3 and you're solving for P0..P2. There are two ways to compute P1 from |
| 14 | the equations above: |
| 15 | |
| 16 | P1 = 3/2 Q1 - 1/2 Q0 |
| 17 | P1 = 3/2 Q2 - 1/2 Q3 |
| 18 | If this is a degree-elevated cubic, then both equations will give the same answer for P1. Since |
| 19 | it's likely not, your best bet is to average them. So, |
| 20 | |
| 21 | P1 = -1/4 Q0 + 3/4 Q1 + 3/4 Q2 - 1/4 Q3 |
| 22 | |
| 23 | |
| 24 | Cubic defined by: P1/2 - anchor points, C1/C2 control points |
| 25 | |x| is the euclidean norm of x |
| 26 | mid-point approx of cubic: a quad that shares the same anchors with the cubic and has the |
| 27 | control point at C = (3·C2 - P2 + 3·C1 - P1)/4 |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 28 | |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 29 | Algorithm |
| 30 | |
| 31 | pick an absolute precision (prec) |
skia.committer@gmail.com | 8ae714b | 2013-01-05 02:02:05 +0000 | [diff] [blame] | 32 | Compute the Tdiv as the root of (cubic) equation |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 33 | sqrt(3)/18 · |P2 - 3·C2 + 3·C1 - P1|/2 · Tdiv ^ 3 = prec |
| 34 | if Tdiv < 0.5 divide the cubic at Tdiv. First segment [0..Tdiv] can be approximated with by a |
| 35 | quadratic, with a defect less than prec, by the mid-point approximation. |
| 36 | Repeat from step 2 with the second resulted segment (corresponding to 1-Tdiv) |
| 37 | 0.5<=Tdiv<1 - simply divide the cubic in two. The two halves can be approximated by the mid-point |
| 38 | approximation |
| 39 | Tdiv>=1 - the entire cubic can be approximated by the mid-point approximation |
| 40 | |
| 41 | confirmed by (maybe stolen from) |
| 42 | http://www.caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 43 | // maybe in turn derived from http://www.cccg.ca/proceedings/2004/36.pdf |
| 44 | // also stored at http://www.cis.usouthal.edu/~hain/general/Publications/Bezier/bezier%20cccg04%20paper.pdf |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 45 | |
| 46 | */ |
| 47 | |
| 48 | #include "CubicUtilities.h" |
| 49 | #include "CurveIntersection.h" |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 50 | #include "LineIntersection.h" |
| 51 | |
| 52 | const bool AVERAGE_END_POINTS = true; // results in better fitting curves |
| 53 | |
| 54 | #define USE_CUBIC_END_POINTS 1 |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 55 | |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 56 | static double calcTDiv(const Cubic& cubic, double precision, double start) { |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 57 | const double adjust = sqrt(3) / 36; |
| 58 | Cubic sub; |
| 59 | const Cubic* cPtr; |
| 60 | if (start == 0) { |
| 61 | cPtr = &cubic; |
| 62 | } else { |
| 63 | // OPTIMIZE: special-case half-split ? |
| 64 | sub_divide(cubic, start, 1, sub); |
| 65 | cPtr = ⊂ |
| 66 | } |
| 67 | const Cubic& c = *cPtr; |
| 68 | double dx = c[3].x - 3 * (c[2].x - c[1].x) - c[0].x; |
| 69 | double dy = c[3].y - 3 * (c[2].y - c[1].y) - c[0].y; |
| 70 | double dist = sqrt(dx * dx + dy * dy); |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 71 | double tDiv3 = precision / (adjust * dist); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 72 | double t = cube_root(tDiv3); |
| 73 | if (start > 0) { |
| 74 | t = start + (1 - start) * t; |
| 75 | } |
| 76 | return t; |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 77 | } |
| 78 | |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 79 | void demote_cubic_to_quad(const Cubic& cubic, Quadratic& quad) { |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 80 | quad[0] = cubic[0]; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 81 | if (AVERAGE_END_POINTS) { |
| 82 | const _Point fromC1 = { (3 * cubic[1].x - cubic[0].x) / 2, (3 * cubic[1].y - cubic[0].y) / 2 }; |
| 83 | const _Point fromC2 = { (3 * cubic[2].x - cubic[3].x) / 2, (3 * cubic[2].y - cubic[3].y) / 2 }; |
| 84 | quad[1].x = (fromC1.x + fromC2.x) / 2; |
| 85 | quad[1].y = (fromC1.y + fromC2.y) / 2; |
| 86 | } else { |
| 87 | lineIntersect((const _Line&) cubic[0], (const _Line&) cubic[2], quad[1]); |
| 88 | } |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 89 | quad[2] = cubic[3]; |
| 90 | } |
| 91 | |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 92 | int cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<Quadratic>& quadratics) { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 93 | SkTDArray<double> ts; |
| 94 | cubic_to_quadratics(cubic, precision, ts); |
| 95 | int tsCount = ts.count(); |
| 96 | double t1Start = 0; |
| 97 | int order = 0; |
| 98 | for (int idx = 0; idx <= tsCount; ++idx) { |
| 99 | double t1 = idx < tsCount ? ts[idx] : 1; |
| 100 | Cubic part; |
| 101 | sub_divide(cubic, t1Start, t1, part); |
| 102 | Quadratic q1; |
| 103 | demote_cubic_to_quad(part, q1); |
| 104 | Quadratic s1; |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame^] | 105 | int o1 = reduceOrder(q1, s1, kReduceOrder_TreatAsFill); |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 106 | if (order < o1) { |
| 107 | order = o1; |
| 108 | } |
| 109 | memcpy(quadratics.append(), o1 < 2 ? s1 : q1, sizeof(Quadratic)); |
| 110 | t1Start = t1; |
| 111 | } |
| 112 | return order; |
| 113 | } |
| 114 | |
| 115 | static bool addSimpleTs(const Cubic& cubic, double precision, SkTDArray<double>& ts) { |
| 116 | double tDiv = calcTDiv(cubic, precision, 0); |
| 117 | if (tDiv >= 1) { |
| 118 | return true; |
| 119 | } |
| 120 | if (tDiv >= 0.5) { |
| 121 | *ts.append() = 0.5; |
| 122 | return true; |
| 123 | } |
| 124 | return false; |
| 125 | } |
| 126 | |
| 127 | static void addTs(const Cubic& cubic, double precision, double start, double end, |
| 128 | SkTDArray<double>& ts) { |
| 129 | double tDiv = calcTDiv(cubic, precision, 0); |
| 130 | double parts = ceil(1.0 / tDiv); |
| 131 | for (double index = 0; index < parts; ++index) { |
| 132 | double newT = start + (index / parts) * (end - start); |
| 133 | if (newT > 0 && newT < 1) { |
| 134 | *ts.append() = newT; |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // flavor that returns T values only, deferring computing the quads until they are needed |
caryclark@google.com | 05c4bad | 2013-01-19 13:22:39 +0000 | [diff] [blame] | 140 | // FIXME: when called from recursive intersect 2, this could take the original cubic |
| 141 | // and do a more precise job when calling chop at and sub divide by computing the fractional ts. |
| 142 | // it would still take the prechopped cubic for reduce order and find cubic inflections |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 143 | void cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<double>& ts) { |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 144 | Cubic reduced; |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame^] | 145 | int order = reduceOrder(cubic, reduced, kReduceOrder_QuadraticsAllowed, |
| 146 | kReduceOrder_TreatAsFill); |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 147 | if (order < 3) { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 148 | return; |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 149 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 150 | double inflectT[2]; |
| 151 | int inflections = find_cubic_inflections(cubic, inflectT); |
| 152 | SkASSERT(inflections <= 2); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 153 | CubicPair pair; |
| 154 | if (inflections == 1) { |
| 155 | chop_at(cubic, pair, inflectT[0]); |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame^] | 156 | int orderP1 = reduceOrder(pair.first(), reduced, kReduceOrder_NoQuadraticsAllowed, |
| 157 | kReduceOrder_TreatAsFill); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 158 | if (orderP1 < 2) { |
| 159 | --inflections; |
| 160 | } else { |
caryclark@google.com | 47d73da | 2013-02-17 01:41:25 +0000 | [diff] [blame^] | 161 | int orderP2 = reduceOrder(pair.second(), reduced, kReduceOrder_NoQuadraticsAllowed, |
| 162 | kReduceOrder_TreatAsFill); |
caryclark@google.com | 45a8fc6 | 2013-02-14 15:29:11 +0000 | [diff] [blame] | 163 | if (orderP2 < 2) { |
| 164 | --inflections; |
| 165 | } |
| 166 | } |
| 167 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 168 | if (inflections == 0 && addSimpleTs(cubic, precision, ts)) { |
| 169 | return; |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 170 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 171 | if (inflections == 1) { |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 172 | chop_at(cubic, pair, inflectT[0]); |
| 173 | addTs(pair.first(), precision, 0, inflectT[0], ts); |
| 174 | addTs(pair.second(), precision, inflectT[0], 1, ts); |
| 175 | return; |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 176 | } |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 177 | if (inflections == 2) { |
| 178 | if (inflectT[0] > inflectT[1]) { |
| 179 | SkTSwap(inflectT[0], inflectT[1]); |
| 180 | } |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 181 | Cubic part; |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 182 | sub_divide(cubic, 0, inflectT[0], part); |
| 183 | addTs(part, precision, 0, inflectT[0], ts); |
| 184 | sub_divide(cubic, inflectT[0], inflectT[1], part); |
| 185 | addTs(part, precision, inflectT[0], inflectT[1], ts); |
| 186 | sub_divide(cubic, inflectT[1], 1, part); |
| 187 | addTs(part, precision, inflectT[1], 1, ts); |
| 188 | return; |
| 189 | } |
| 190 | addTs(cubic, precision, 0, 1, ts); |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 191 | } |