blob: 5eeaf19c9f349396637dccc08b547301bc573766 [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"
caryclark@google.com1304bb22013-03-13 20:29:41 +000051#include "TSearch.h"
caryclark@google.com73ca6242013-01-17 21:02:47 +000052
53const bool AVERAGE_END_POINTS = true; // results in better fitting curves
54
55#define USE_CUBIC_END_POINTS 1
caryclark@google.com6d0032a2013-01-04 19:41:13 +000056
caryclark@google.comd68bc302013-01-07 13:17:18 +000057static double calcTDiv(const Cubic& cubic, double precision, double start) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +000058 const double adjust = sqrt(3) / 36;
59 Cubic sub;
60 const Cubic* cPtr;
61 if (start == 0) {
62 cPtr = &cubic;
63 } else {
64 // OPTIMIZE: special-case half-split ?
65 sub_divide(cubic, start, 1, sub);
66 cPtr = &sub;
67 }
68 const Cubic& c = *cPtr;
69 double dx = c[3].x - 3 * (c[2].x - c[1].x) - c[0].x;
70 double dy = c[3].y - 3 * (c[2].y - c[1].y) - c[0].y;
71 double dist = sqrt(dx * dx + dy * dy);
caryclark@google.comd68bc302013-01-07 13:17:18 +000072 double tDiv3 = precision / (adjust * dist);
caryclark@google.com73ca6242013-01-17 21:02:47 +000073 double t = cube_root(tDiv3);
74 if (start > 0) {
75 t = start + (1 - start) * t;
76 }
77 return t;
caryclark@google.com6d0032a2013-01-04 19:41:13 +000078}
79
caryclark@google.com73ca6242013-01-17 21:02:47 +000080void demote_cubic_to_quad(const Cubic& cubic, Quadratic& quad) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +000081 quad[0] = cubic[0];
caryclark@google.com73ca6242013-01-17 21:02:47 +000082if (AVERAGE_END_POINTS) {
83 const _Point fromC1 = { (3 * cubic[1].x - cubic[0].x) / 2, (3 * cubic[1].y - cubic[0].y) / 2 };
84 const _Point fromC2 = { (3 * cubic[2].x - cubic[3].x) / 2, (3 * cubic[2].y - cubic[3].y) / 2 };
85 quad[1].x = (fromC1.x + fromC2.x) / 2;
86 quad[1].y = (fromC1.y + fromC2.y) / 2;
87} else {
88 lineIntersect((const _Line&) cubic[0], (const _Line&) cubic[2], quad[1]);
89}
caryclark@google.com6d0032a2013-01-04 19:41:13 +000090 quad[2] = cubic[3];
91}
92
caryclark@google.comd68bc302013-01-07 13:17:18 +000093int cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<Quadratic>& quadratics) {
caryclark@google.com73ca6242013-01-17 21:02:47 +000094 SkTDArray<double> ts;
95 cubic_to_quadratics(cubic, precision, ts);
96 int tsCount = ts.count();
97 double t1Start = 0;
98 int order = 0;
99 for (int idx = 0; idx <= tsCount; ++idx) {
100 double t1 = idx < tsCount ? ts[idx] : 1;
101 Cubic part;
102 sub_divide(cubic, t1Start, t1, part);
103 Quadratic q1;
104 demote_cubic_to_quad(part, q1);
105 Quadratic s1;
caryclark@google.com47d73da2013-02-17 01:41:25 +0000106 int o1 = reduceOrder(q1, s1, kReduceOrder_TreatAsFill);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000107 if (order < o1) {
108 order = o1;
109 }
110 memcpy(quadratics.append(), o1 < 2 ? s1 : q1, sizeof(Quadratic));
111 t1Start = t1;
112 }
113 return order;
114}
115
116static bool addSimpleTs(const Cubic& cubic, double precision, SkTDArray<double>& ts) {
117 double tDiv = calcTDiv(cubic, precision, 0);
118 if (tDiv >= 1) {
119 return true;
120 }
121 if (tDiv >= 0.5) {
122 *ts.append() = 0.5;
123 return true;
124 }
125 return false;
126}
127
128static void addTs(const Cubic& cubic, double precision, double start, double end,
129 SkTDArray<double>& ts) {
130 double tDiv = calcTDiv(cubic, precision, 0);
131 double parts = ceil(1.0 / tDiv);
132 for (double index = 0; index < parts; ++index) {
133 double newT = start + (index / parts) * (end - start);
134 if (newT > 0 && newT < 1) {
135 *ts.append() = newT;
136 }
137 }
138}
139
140// flavor that returns T values only, deferring computing the quads until they are needed
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000141// FIXME: when called from recursive intersect 2, this could take the original cubic
142// and do a more precise job when calling chop at and sub divide by computing the fractional ts.
143// it would still take the prechopped cubic for reduce order and find cubic inflections
caryclark@google.com73ca6242013-01-17 21:02:47 +0000144void cubic_to_quadratics(const Cubic& cubic, double precision, SkTDArray<double>& ts) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000145 Cubic reduced;
caryclark@google.com47d73da2013-02-17 01:41:25 +0000146 int order = reduceOrder(cubic, reduced, kReduceOrder_QuadraticsAllowed,
147 kReduceOrder_TreatAsFill);
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000148 if (order < 3) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000149 return;
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000150 }
caryclark@google.com1304bb22013-03-13 20:29:41 +0000151 double inflectT[5];
caryclark@google.com73ca6242013-01-17 21:02:47 +0000152 int inflections = find_cubic_inflections(cubic, inflectT);
153 SkASSERT(inflections <= 2);
caryclark@google.com1304bb22013-03-13 20:29:41 +0000154 if (!ends_are_extrema_in_x_or_y(cubic)) {
155 inflections += find_cubic_max_curvature(cubic, &inflectT[inflections]);
156 SkASSERT(inflections <= 5);
157 }
158 QSort<double>(inflectT, &inflectT[inflections - 1]);
159 // OPTIMIZATION: is this filtering common enough that it needs to be pulled out into its
160 // own subroutine?
161 while (inflections && approximately_less_than_zero(inflectT[0])) {
162 memcpy(inflectT, &inflectT[1], sizeof(inflectT[0]) * --inflections);
163 }
164 int start = 0;
165 do {
166 int next = start + 1;
167 if (next >= inflections) {
168 break;
169 }
170 if (!approximately_equal(inflectT[start], inflectT[next])) {
171 ++start;
172 continue;
173 }
174 memcpy(&inflectT[start], &inflectT[next], sizeof(inflectT[0]) * (--inflections - start));
175 } while (true);
176 while (inflections && approximately_greater_than_one(inflectT[inflections - 1])) {
177 --inflections;
178 }
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000179 CubicPair pair;
180 if (inflections == 1) {
181 chop_at(cubic, pair, inflectT[0]);
caryclark@google.com47d73da2013-02-17 01:41:25 +0000182 int orderP1 = reduceOrder(pair.first(), reduced, kReduceOrder_NoQuadraticsAllowed,
183 kReduceOrder_TreatAsFill);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000184 if (orderP1 < 2) {
185 --inflections;
186 } else {
caryclark@google.com47d73da2013-02-17 01:41:25 +0000187 int orderP2 = reduceOrder(pair.second(), reduced, kReduceOrder_NoQuadraticsAllowed,
188 kReduceOrder_TreatAsFill);
caryclark@google.com45a8fc62013-02-14 15:29:11 +0000189 if (orderP2 < 2) {
190 --inflections;
191 }
192 }
193 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000194 if (inflections == 0 && addSimpleTs(cubic, precision, ts)) {
195 return;
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000196 }
caryclark@google.com73ca6242013-01-17 21:02:47 +0000197 if (inflections == 1) {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000198 chop_at(cubic, pair, inflectT[0]);
199 addTs(pair.first(), precision, 0, inflectT[0], ts);
200 addTs(pair.second(), precision, inflectT[0], 1, ts);
201 return;
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000202 }
caryclark@google.com1304bb22013-03-13 20:29:41 +0000203 if (inflections > 1) {
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000204 Cubic part;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000205 sub_divide(cubic, 0, inflectT[0], part);
206 addTs(part, precision, 0, inflectT[0], ts);
caryclark@google.com1304bb22013-03-13 20:29:41 +0000207 int last = inflections - 1;
208 for (int idx = 0; idx < last; ++idx) {
209 sub_divide(cubic, inflectT[idx], inflectT[idx + 1], part);
210 addTs(part, precision, inflectT[idx], inflectT[idx + 1], ts);
211 }
212 sub_divide(cubic, inflectT[last], 1, part);
213 addTs(part, precision, inflectT[last], 1, ts);
caryclark@google.com73ca6242013-01-17 21:02:47 +0000214 return;
215 }
216 addTs(cubic, precision, 0, 1, ts);
caryclark@google.com6d0032a2013-01-04 19:41:13 +0000217}