blob: 66288b431643d4b47064679f0166ef9a2e6240eb [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
caryclark@google.com73ca6242013-01-17 21:02:47 +000043// 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.com6d0032a2013-01-04 19:41:13 +000045
46*/
47
48#include "CubicUtilities.h"
49#include "CurveIntersection.h"
caryclark@google.com73ca6242013-01-17 21:02:47 +000050#include "LineIntersection.h"
51
52const bool AVERAGE_END_POINTS = true; // results in better fitting curves
53
54#define USE_CUBIC_END_POINTS 1
caryclark@google.com6d0032a2013-01-04 19:41:13 +000055
caryclark@google.comd68bc302013-01-07 13:17:18 +000056static double calcTDiv(const Cubic& cubic, double precision, double start) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +000057 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 = &sub;
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.comd68bc302013-01-07 13:17:18 +000071 double tDiv3 = precision / (adjust * dist);
caryclark@google.com73ca6242013-01-17 21:02:47 +000072 double t = cube_root(tDiv3);
73 if (start > 0) {
74 t = start + (1 - start) * t;
75 }
76 return t;
caryclark@google.com6d0032a2013-01-04 19:41:13 +000077}
78
caryclark@google.com73ca6242013-01-17 21:02:47 +000079void demote_cubic_to_quad(const Cubic& cubic, Quadratic& quad) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +000080 quad[0] = cubic[0];
caryclark@google.com73ca6242013-01-17 21:02:47 +000081if (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.com6d0032a2013-01-04 19:41:13 +000089 quad[2] = cubic[3];
90}
91
caryclark@google.comd68bc302013-01-07 13:17:18 +000092int cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<Quadratic>& quadratics) {
caryclark@google.com73ca6242013-01-17 21:02:47 +000093 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;
105 int o1 = reduceOrder(q1, s1);
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
115static 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
127static 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.com05c4bad2013-01-19 13:22:39 +0000140// 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.com73ca6242013-01-17 21:02:47 +0000143void cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<double>& ts) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000144 Cubic reduced;
145 int order = reduceOrder(cubic, reduced, kReduceOrder_QuadraticsAllowed);
146 if (order < 3) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000147 return;
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000148 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000149 double inflectT[2];
150 int inflections = find_cubic_inflections(cubic, inflectT);
151 SkASSERT(inflections <= 2);
152 if (inflections == 0 && addSimpleTs(cubic, precision, ts)) {
153 return;
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000154 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000155 if (inflections == 1) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000156 CubicPair pair;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000157 chop_at(cubic, pair, inflectT[0]);
158 addTs(pair.first(), precision, 0, inflectT[0], ts);
159 addTs(pair.second(), precision, inflectT[0], 1, ts);
160 return;
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000161 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000162 if (inflections == 2) {
163 if (inflectT[0] > inflectT[1]) {
164 SkTSwap(inflectT[0], inflectT[1]);
165 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000166 Cubic part;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000167 sub_divide(cubic, 0, inflectT[0], part);
168 addTs(part, precision, 0, inflectT[0], ts);
169 sub_divide(cubic, inflectT[0], inflectT[1], part);
170 addTs(part, precision, inflectT[0], inflectT[1], ts);
171 sub_divide(cubic, inflectT[1], 1, part);
172 addTs(part, precision, inflectT[1], 1, ts);
173 return;
174 }
175 addTs(cubic, precision, 0, 1, ts);
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000176}