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 |
| 43 | |
| 44 | */ |
| 45 | |
| 46 | #include "CubicUtilities.h" |
| 47 | #include "CurveIntersection.h" |
| 48 | |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 49 | static double calcTDiv(const Cubic& cubic, double precision, double start) { |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 50 | const double adjust = sqrt(3) / 36; |
| 51 | Cubic sub; |
| 52 | const Cubic* cPtr; |
| 53 | if (start == 0) { |
| 54 | cPtr = &cubic; |
| 55 | } else { |
| 56 | // OPTIMIZE: special-case half-split ? |
| 57 | sub_divide(cubic, start, 1, sub); |
| 58 | cPtr = ⊂ |
| 59 | } |
| 60 | const Cubic& c = *cPtr; |
| 61 | double dx = c[3].x - 3 * (c[2].x - c[1].x) - c[0].x; |
| 62 | double dy = c[3].y - 3 * (c[2].y - c[1].y) - c[0].y; |
| 63 | double dist = sqrt(dx * dx + dy * dy); |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 64 | double tDiv3 = precision / (adjust * dist); |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 65 | return cube_root(tDiv3); |
| 66 | } |
| 67 | |
| 68 | static void demote(const Cubic& cubic, Quadratic& quad) { |
| 69 | quad[0] = cubic[0]; |
| 70 | quad[1].x = (cubic[3].x - 3 * (cubic[2].x - cubic[1].x) - cubic[0].x) / 4; |
| 71 | quad[1].y = (cubic[3].y - 3 * (cubic[2].y - cubic[1].y) - cubic[0].y) / 4; |
| 72 | quad[2] = cubic[3]; |
| 73 | } |
| 74 | |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 75 | int cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<Quadratic>& quadratics) { |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 76 | quadratics.setCount(1); // FIXME: every place I have setCount(), I also want setReserve() |
| 77 | Cubic reduced; |
| 78 | int order = reduceOrder(cubic, reduced, kReduceOrder_QuadraticsAllowed); |
| 79 | if (order < 3) { |
| 80 | memcpy(quadratics[0], reduced, order * sizeof(_Point)); |
| 81 | return order; |
| 82 | } |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 83 | double tDiv = calcTDiv(cubic, precision, 0); |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 84 | if (approximately_greater_than_one(tDiv)) { |
| 85 | demote(cubic, quadratics[0]); |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 86 | return 3; |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 87 | } |
| 88 | if (tDiv >= 0.5) { |
| 89 | CubicPair pair; |
| 90 | chop_at(cubic, pair, 0.5); |
| 91 | quadratics.setCount(2); |
| 92 | demote(pair.first(), quadratics[0]); |
| 93 | demote(pair.second(), quadratics[1]); |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 94 | return 3; |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 95 | } |
| 96 | double start = 0; |
| 97 | int index = -1; |
| 98 | // if we iterate but make little progress, check to see if the curve is flat |
| 99 | // or if the control points are tangent to each other |
| 100 | do { |
| 101 | Cubic part; |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 102 | sub_divide(cubic, start, tDiv, part); |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 103 | quadratics.append(); |
| 104 | demote(part, quadratics[++index]); |
| 105 | if (tDiv == 1) { |
| 106 | break; |
| 107 | } |
| 108 | start = tDiv; |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 109 | tDiv = calcTDiv(cubic, precision, start); |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 110 | if (tDiv > 1) { |
| 111 | tDiv = 1; |
| 112 | } |
| 113 | } while (true); |
caryclark@google.com | d68bc30 | 2013-01-07 13:17:18 +0000 | [diff] [blame] | 114 | return 3; |
caryclark@google.com | 6d0032a | 2013-01-04 19:41:13 +0000 | [diff] [blame] | 115 | } |