blob: 424836c633b12e9173dc837cbc9ebb396f3cac97 [file] [log] [blame]
caryclark@google.com6d0032a2013-01-04 19:41:13 +00001/*
2http://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.com8ae714b2013-01-05 02:02:05 +00006Let's call the control points of the cubic Q0..Q3 and the control points of the quadratic P0..P2.
caryclark@google.com6d0032a2013-01-04 19:41:13 +00007Then for degree elevation, the equations are:
8
9Q0 = P0
10Q1 = 1/3 P0 + 2/3 P1
11Q2 = 2/3 P1 + 1/3 P2
12Q3 = P2
13In 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
16P1 = 3/2 Q1 - 1/2 Q0
17P1 = 3/2 Q2 - 1/2 Q3
18If 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
21P1 = -1/4 Q0 + 3/4 Q1 + 3/4 Q2 - 1/4 Q3
22
23
24Cubic defined by: P1/2 - anchor points, C1/C2 control points
25|x| is the euclidean norm of x
26mid-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.com8ae714b2013-01-05 02:02:05 +000028
caryclark@google.com6d0032a2013-01-04 19:41:13 +000029Algorithm
30
31pick an absolute precision (prec)
skia.committer@gmail.com8ae714b2013-01-05 02:02:05 +000032Compute the Tdiv as the root of (cubic) equation
caryclark@google.com6d0032a2013-01-04 19:41:13 +000033sqrt(3)/18 · |P2 - 3·C2 + 3·C1 - P1|/2 · Tdiv ^ 3 = prec
34if 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)
370.5<=Tdiv<1 - simply divide the cubic in two. The two halves can be approximated by the mid-point
38 approximation
39Tdiv>=1 - the entire cubic can be approximated by the mid-point approximation
40
41confirmed by (maybe stolen from)
42http://www.caffeineowl.com/graphics/2d/vectorial/cubic2quad01.html
43
44*/
45
46#include "CubicUtilities.h"
47#include "CurveIntersection.h"
48
caryclark@google.comd68bc302013-01-07 13:17:18 +000049static double calcTDiv(const Cubic& cubic, double precision, double start) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +000050 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 = &sub;
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.comd68bc302013-01-07 13:17:18 +000064 double tDiv3 = precision / (adjust * dist);
caryclark@google.com6d0032a2013-01-04 19:41:13 +000065 return cube_root(tDiv3);
66}
67
68static 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.comd68bc302013-01-07 13:17:18 +000075int cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<Quadratic>& quadratics) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +000076 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.comd68bc302013-01-07 13:17:18 +000083 double tDiv = calcTDiv(cubic, precision, 0);
caryclark@google.com6d0032a2013-01-04 19:41:13 +000084 if (approximately_greater_than_one(tDiv)) {
85 demote(cubic, quadratics[0]);
caryclark@google.comd68bc302013-01-07 13:17:18 +000086 return 3;
caryclark@google.com6d0032a2013-01-04 19:41:13 +000087 }
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.comd68bc302013-01-07 13:17:18 +000094 return 3;
caryclark@google.com6d0032a2013-01-04 19:41:13 +000095 }
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.comd68bc302013-01-07 13:17:18 +0000102 sub_divide(cubic, start, tDiv, part);
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000103 quadratics.append();
104 demote(part, quadratics[++index]);
105 if (tDiv == 1) {
106 break;
107 }
108 start = tDiv;
caryclark@google.comd68bc302013-01-07 13:17:18 +0000109 tDiv = calcTDiv(cubic, precision, start);
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000110 if (tDiv > 1) {
111 tDiv = 1;
112 }
113 } while (true);
caryclark@google.comd68bc302013-01-07 13:17:18 +0000114 return 3;
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000115}