blob: c644a67117ecdd6674ce8f17fec0274e0bc38c9a [file] [log] [blame]
caryclark@google.com07393ca2013-04-08 11:47:37 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7#include "SkLineParameters.h"
8#include "SkPathOpsCubic.h"
9#include "SkPathOpsLine.h"
10#include "SkPathOpsQuad.h"
11#include "SkPathOpsRect.h"
12
13const int SkDCubic::gPrecisionUnit = 256; // FIXME: test different values in test framework
14
15// FIXME: cache keep the bounds and/or precision with the caller?
16double SkDCubic::calcPrecision() const {
17 SkDRect dRect;
18 dRect.setBounds(*this); // OPTIMIZATION: just use setRawBounds ?
19 double width = dRect.fRight - dRect.fLeft;
20 double height = dRect.fBottom - dRect.fTop;
21 return (width > height ? width : height) / gPrecisionUnit;
22}
23
24bool SkDCubic::clockwise() const {
25 double sum = (fPts[0].fX - fPts[3].fX) * (fPts[0].fY + fPts[3].fY);
26 for (int idx = 0; idx < 3; ++idx) {
27 sum += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY);
28 }
29 return sum <= 0;
30}
31
32void SkDCubic::Coefficients(const double* src, double* A, double* B, double* C, double* D) {
33 *A = src[6]; // d
34 *B = src[4] * 3; // 3*c
35 *C = src[2] * 3; // 3*b
36 *D = src[0]; // a
37 *A -= *D - *C + *B; // A = -a + 3*b - 3*c + d
38 *B += 3 * *D - 2 * *C; // B = 3*a - 6*b + 3*c
39 *C -= 3 * *D; // C = -3*a + 3*b
40}
41
42bool SkDCubic::controlsContainedByEnds() const {
43 SkDVector startTan = fPts[1] - fPts[0];
44 if (startTan.fX == 0 && startTan.fY == 0) {
45 startTan = fPts[2] - fPts[0];
46 }
47 SkDVector endTan = fPts[2] - fPts[3];
48 if (endTan.fX == 0 && endTan.fY == 0) {
49 endTan = fPts[1] - fPts[3];
50 }
51 if (startTan.dot(endTan) >= 0) {
52 return false;
53 }
54 SkDLine startEdge = {{fPts[0], fPts[0]}};
55 startEdge[1].fX -= startTan.fY;
56 startEdge[1].fY += startTan.fX;
57 SkDLine endEdge = {{fPts[3], fPts[3]}};
58 endEdge[1].fX -= endTan.fY;
59 endEdge[1].fY += endTan.fX;
60 double leftStart1 = startEdge.isLeft(fPts[1]);
61 if (leftStart1 * startEdge.isLeft(fPts[2]) < 0) {
62 return false;
63 }
64 double leftEnd1 = endEdge.isLeft(fPts[1]);
65 if (leftEnd1 * endEdge.isLeft(fPts[2]) < 0) {
66 return false;
67 }
68 return leftStart1 * leftEnd1 >= 0;
69}
70
71bool SkDCubic::endsAreExtremaInXOrY() const {
72 return (between(fPts[0].fX, fPts[1].fX, fPts[3].fX)
73 && between(fPts[0].fX, fPts[2].fX, fPts[3].fX))
74 || (between(fPts[0].fY, fPts[1].fY, fPts[3].fY)
75 && between(fPts[0].fY, fPts[2].fY, fPts[3].fY));
76}
77
78bool SkDCubic::isLinear(int startIndex, int endIndex) const {
79 SkLineParameters lineParameters;
80 lineParameters.cubicEndPoints(*this, startIndex, endIndex);
81 // FIXME: maybe it's possible to avoid this and compare non-normalized
82 lineParameters.normalize();
83 double distance = lineParameters.controlPtDistance(*this, 1);
84 if (!approximately_zero(distance)) {
85 return false;
86 }
87 distance = lineParameters.controlPtDistance(*this, 2);
88 return approximately_zero(distance);
89}
90
91bool SkDCubic::monotonicInY() const {
92 return between(fPts[0].fY, fPts[1].fY, fPts[3].fY)
93 && between(fPts[0].fY, fPts[2].fY, fPts[3].fY);
94}
95
96bool SkDCubic::serpentine() const {
97 if (!controlsContainedByEnds()) {
98 return false;
99 }
100 double wiggle = (fPts[0].fX - fPts[2].fX) * (fPts[0].fY + fPts[2].fY);
101 for (int idx = 0; idx < 2; ++idx) {
102 wiggle += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY);
103 }
104 double waggle = (fPts[1].fX - fPts[3].fX) * (fPts[1].fY + fPts[3].fY);
105 for (int idx = 1; idx < 3; ++idx) {
106 waggle += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY);
107 }
108 return wiggle * waggle < 0;
109}
110
111// cubic roots
112
113static const double PI = 3.141592653589793;
114
115// from SkGeometry.cpp (and Numeric Solutions, 5.6)
116int SkDCubic::RootsValidT(double A, double B, double C, double D, double t[3]) {
117 double s[3];
118 int realRoots = RootsReal(A, B, C, D, s);
119 int foundRoots = SkDQuad::AddValidTs(s, realRoots, t);
120 return foundRoots;
121}
122
123int SkDCubic::RootsReal(double A, double B, double C, double D, double s[3]) {
124#ifdef SK_DEBUG
125 // create a string mathematica understands
126 // GDB set print repe 15 # if repeated digits is a bother
127 // set print elements 400 # if line doesn't fit
128 char str[1024];
129 sk_bzero(str, sizeof(str));
130 SK_SNPRINTF(str, sizeof(str), "Solve[%1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]",
131 A, B, C, D);
132 mathematica_ize(str, sizeof(str));
133#if ONE_OFF_DEBUG && ONE_OFF_DEBUG_MATHEMATICA
134 SkDebugf("%s\n", str);
135#endif
136#endif
137 if (approximately_zero(A)
138 && approximately_zero_when_compared_to(A, B)
139 && approximately_zero_when_compared_to(A, C)
140 && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic
141 return SkDQuad::RootsReal(B, C, D, s);
142 }
143 if (approximately_zero_when_compared_to(D, A)
144 && approximately_zero_when_compared_to(D, B)
145 && approximately_zero_when_compared_to(D, C)) { // 0 is one root
146 int num = SkDQuad::RootsReal(A, B, C, s);
147 for (int i = 0; i < num; ++i) {
148 if (approximately_zero(s[i])) {
149 return num;
150 }
151 }
152 s[num++] = 0;
153 return num;
154 }
155 if (approximately_zero(A + B + C + D)) { // 1 is one root
156 int num = SkDQuad::RootsReal(A, A + B, -D, s);
157 for (int i = 0; i < num; ++i) {
158 if (AlmostEqualUlps(s[i], 1)) {
159 return num;
160 }
161 }
162 s[num++] = 1;
163 return num;
164 }
165 double a, b, c;
166 {
167 double invA = 1 / A;
168 a = B * invA;
169 b = C * invA;
170 c = D * invA;
171 }
172 double a2 = a * a;
173 double Q = (a2 - b * 3) / 9;
174 double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
175 double R2 = R * R;
176 double Q3 = Q * Q * Q;
177 double R2MinusQ3 = R2 - Q3;
178 double adiv3 = a / 3;
179 double r;
180 double* roots = s;
181 if (R2MinusQ3 < 0) { // we have 3 real roots
182 double theta = acos(R / sqrt(Q3));
183 double neg2RootQ = -2 * sqrt(Q);
184
185 r = neg2RootQ * cos(theta / 3) - adiv3;
186 *roots++ = r;
187
188 r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3;
189 if (!AlmostEqualUlps(s[0], r)) {
190 *roots++ = r;
191 }
192 r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3;
193 if (!AlmostEqualUlps(s[0], r) && (roots - s == 1 || !AlmostEqualUlps(s[1], r))) {
194 *roots++ = r;
195 }
196 } else { // we have 1 real root
197 double sqrtR2MinusQ3 = sqrt(R2MinusQ3);
198 double A = fabs(R) + sqrtR2MinusQ3;
199 A = SkDCubeRoot(A);
200 if (R > 0) {
201 A = -A;
202 }
203 if (A != 0) {
204 A += Q / A;
205 }
206 r = A - adiv3;
207 *roots++ = r;
208 if (AlmostEqualUlps(R2, Q3)) {
209 r = -A / 2 - adiv3;
210 if (!AlmostEqualUlps(s[0], r)) {
211 *roots++ = r;
212 }
213 }
214 }
215 return static_cast<int>(roots - s);
216}
217
218// from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf
219// c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3
220// c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2
221// = 3(b-a)(1-t)^2 + 6(c-b)t(1-t) + 3(d-c)t^2
222static double derivative_at_t(const double* src, double t) {
223 double one_t = 1 - t;
224 double a = src[0];
225 double b = src[2];
226 double c = src[4];
227 double d = src[6];
228 return 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t);
229}
230
231// OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t?
232SkDVector SkDCubic::dxdyAtT(double t) const {
233 SkDVector result = { derivative_at_t(&fPts[0].fX, t), derivative_at_t(&fPts[0].fY, t) };
234 return result;
235}
236
237// OPTIMIZE? share code with formulate_F1DotF2
238int SkDCubic::findInflections(double tValues[]) const {
239 double Ax = fPts[1].fX - fPts[0].fX;
240 double Ay = fPts[1].fY - fPts[0].fY;
241 double Bx = fPts[2].fX - 2 * fPts[1].fX + fPts[0].fX;
242 double By = fPts[2].fY - 2 * fPts[1].fY + fPts[0].fY;
243 double Cx = fPts[3].fX + 3 * (fPts[1].fX - fPts[2].fX) - fPts[0].fX;
244 double Cy = fPts[3].fY + 3 * (fPts[1].fY - fPts[2].fY) - fPts[0].fY;
245 return SkDQuad::RootsValidT(Bx * Cy - By * Cx, Ax * Cy - Ay * Cx, Ax * By - Ay * Bx, tValues);
246}
247
248static void formulate_F1DotF2(const double src[], double coeff[4]) {
249 double a = src[2] - src[0];
250 double b = src[4] - 2 * src[2] + src[0];
251 double c = src[6] + 3 * (src[2] - src[4]) - src[0];
252 coeff[0] = c * c;
253 coeff[1] = 3 * b * c;
254 coeff[2] = 2 * b * b + c * a;
255 coeff[3] = a * b;
256}
257
258/** SkDCubic'(t) = At^2 + Bt + C, where
259 A = 3(-a + 3(b - c) + d)
260 B = 6(a - 2b + c)
261 C = 3(b - a)
262 Solve for t, keeping only those that fit between 0 < t < 1
263*/
264int SkDCubic::FindExtrema(double a, double b, double c, double d, double tValues[2]) {
265 // we divide A,B,C by 3 to simplify
266 double A = d - a + 3*(b - c);
267 double B = 2*(a - b - b + c);
268 double C = b - a;
269
270 return SkDQuad::RootsValidT(A, B, C, tValues);
271}
272
273/* from SkGeometry.cpp
274 Looking for F' dot F'' == 0
275
276 A = b - a
277 B = c - 2b + a
278 C = d - 3c + 3b - a
279
280 F' = 3Ct^2 + 6Bt + 3A
281 F'' = 6Ct + 6B
282
283 F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
284*/
285int SkDCubic::findMaxCurvature(double tValues[]) const {
286 double coeffX[4], coeffY[4];
287 int i;
288 formulate_F1DotF2(&fPts[0].fX, coeffX);
289 formulate_F1DotF2(&fPts[0].fY, coeffY);
290 for (i = 0; i < 4; i++) {
291 coeffX[i] = coeffX[i] + coeffY[i];
292 }
293 return RootsValidT(coeffX[0], coeffX[1], coeffX[2], coeffX[3], tValues);
294}
295
296SkDPoint SkDCubic::top(double startT, double endT) const {
297 SkDCubic sub = subDivide(startT, endT);
298 SkDPoint topPt = sub[0];
299 if (topPt.fY > sub[3].fY || (topPt.fY == sub[3].fY && topPt.fX > sub[3].fX)) {
300 topPt = sub[3];
301 }
302 double extremeTs[2];
303 if (!sub.monotonicInY()) {
304 int roots = FindExtrema(sub[0].fY, sub[1].fY, sub[2].fY, sub[3].fY, extremeTs);
305 for (int index = 0; index < roots; ++index) {
306 double t = startT + (endT - startT) * extremeTs[index];
307 SkDPoint mid = xyAtT(t);
308 if (topPt.fY > mid.fY || (topPt.fY == mid.fY && topPt.fX > mid.fX)) {
309 topPt = mid;
310 }
311 }
312 }
313 return topPt;
314}
315
316SkDPoint SkDCubic::xyAtT(double t) const {
317 double one_t = 1 - t;
318 double one_t2 = one_t * one_t;
319 double a = one_t2 * one_t;
320 double b = 3 * one_t2 * t;
321 double t2 = t * t;
322 double c = 3 * one_t * t2;
323 double d = t2 * t;
324 SkDPoint result = {a * fPts[0].fX + b * fPts[1].fX + c * fPts[2].fX + d * fPts[3].fX,
325 a * fPts[0].fY + b * fPts[1].fY + c * fPts[2].fY + d * fPts[3].fY};
326 return result;
327}
328
329/*
330 Given a cubic c, t1, and t2, find a small cubic segment.
331
332 The new cubic is defined as points A, B, C, and D, where
333 s1 = 1 - t1
334 s2 = 1 - t2
335 A = c[0]*s1*s1*s1 + 3*c[1]*s1*s1*t1 + 3*c[2]*s1*t1*t1 + c[3]*t1*t1*t1
336 D = c[0]*s2*s2*s2 + 3*c[1]*s2*s2*t2 + 3*c[2]*s2*t2*t2 + c[3]*t2*t2*t2
337
338 We don't have B or C. So We define two equations to isolate them.
339 First, compute two reference T values 1/3 and 2/3 from t1 to t2:
340
341 c(at (2*t1 + t2)/3) == E
342 c(at (t1 + 2*t2)/3) == F
343
344 Next, compute where those values must be if we know the values of B and C:
345
346 _12 = A*2/3 + B*1/3
347 12_ = A*1/3 + B*2/3
348 _23 = B*2/3 + C*1/3
349 23_ = B*1/3 + C*2/3
350 _34 = C*2/3 + D*1/3
351 34_ = C*1/3 + D*2/3
352 _123 = (A*2/3 + B*1/3)*2/3 + (B*2/3 + C*1/3)*1/3 = A*4/9 + B*4/9 + C*1/9
353 123_ = (A*1/3 + B*2/3)*1/3 + (B*1/3 + C*2/3)*2/3 = A*1/9 + B*4/9 + C*4/9
354 _234 = (B*2/3 + C*1/3)*2/3 + (C*2/3 + D*1/3)*1/3 = B*4/9 + C*4/9 + D*1/9
355 234_ = (B*1/3 + C*2/3)*1/3 + (C*1/3 + D*2/3)*2/3 = B*1/9 + C*4/9 + D*4/9
356 _1234 = (A*4/9 + B*4/9 + C*1/9)*2/3 + (B*4/9 + C*4/9 + D*1/9)*1/3
357 = A*8/27 + B*12/27 + C*6/27 + D*1/27
358 = E
359 1234_ = (A*1/9 + B*4/9 + C*4/9)*1/3 + (B*1/9 + C*4/9 + D*4/9)*2/3
360 = A*1/27 + B*6/27 + C*12/27 + D*8/27
361 = F
362 E*27 = A*8 + B*12 + C*6 + D
363 F*27 = A + B*6 + C*12 + D*8
364
365Group the known values on one side:
366
367 M = E*27 - A*8 - D = B*12 + C* 6
368 N = F*27 - A - D*8 = B* 6 + C*12
369 M*2 - N = B*18
370 N*2 - M = C*18
371 B = (M*2 - N)/18
372 C = (N*2 - M)/18
373 */
374
375static double interp_cubic_coords(const double* src, double t) {
376 double ab = SkDInterp(src[0], src[2], t);
377 double bc = SkDInterp(src[2], src[4], t);
378 double cd = SkDInterp(src[4], src[6], t);
379 double abc = SkDInterp(ab, bc, t);
380 double bcd = SkDInterp(bc, cd, t);
381 double abcd = SkDInterp(abc, bcd, t);
382 return abcd;
383}
384
385SkDCubic SkDCubic::subDivide(double t1, double t2) const {
386 if (t1 == 0 && t2 == 1) {
387 return *this;
388 }
389 SkDCubic dst;
390 double ax = dst[0].fX = interp_cubic_coords(&fPts[0].fX, t1);
391 double ay = dst[0].fY = interp_cubic_coords(&fPts[0].fY, t1);
392 double ex = interp_cubic_coords(&fPts[0].fX, (t1*2+t2)/3);
393 double ey = interp_cubic_coords(&fPts[0].fY, (t1*2+t2)/3);
394 double fx = interp_cubic_coords(&fPts[0].fX, (t1+t2*2)/3);
395 double fy = interp_cubic_coords(&fPts[0].fY, (t1+t2*2)/3);
396 double dx = dst[3].fX = interp_cubic_coords(&fPts[0].fX, t2);
397 double dy = dst[3].fY = interp_cubic_coords(&fPts[0].fY, t2);
398 double mx = ex * 27 - ax * 8 - dx;
399 double my = ey * 27 - ay * 8 - dy;
400 double nx = fx * 27 - ax - dx * 8;
401 double ny = fy * 27 - ay - dy * 8;
402 /* bx = */ dst[1].fX = (mx * 2 - nx) / 18;
403 /* by = */ dst[1].fY = (my * 2 - ny) / 18;
404 /* cx = */ dst[2].fX = (nx * 2 - mx) / 18;
405 /* cy = */ dst[2].fY = (ny * 2 - my) / 18;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000406 // FIXME: call align() ?
caryclark@google.com07393ca2013-04-08 11:47:37 +0000407 return dst;
408}
409
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000410void SkDCubic::align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const {
411 if (fPts[endIndex].fX == fPts[ctrlIndex].fX) {
412 dstPt->fX = fPts[endIndex].fX;
413 }
414 if (fPts[endIndex].fY == fPts[ctrlIndex].fY) {
415 dstPt->fY = fPts[endIndex].fY;
416 }
417}
418
caryclark@google.com07393ca2013-04-08 11:47:37 +0000419void SkDCubic::subDivide(const SkDPoint& a, const SkDPoint& d,
420 double t1, double t2, SkDPoint dst[2]) const {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000421 SkASSERT(t1 != t2);
422#if 0
caryclark@google.com07393ca2013-04-08 11:47:37 +0000423 double ex = interp_cubic_coords(&fPts[0].fX, (t1 * 2 + t2) / 3);
424 double ey = interp_cubic_coords(&fPts[0].fY, (t1 * 2 + t2) / 3);
425 double fx = interp_cubic_coords(&fPts[0].fX, (t1 + t2 * 2) / 3);
426 double fy = interp_cubic_coords(&fPts[0].fY, (t1 + t2 * 2) / 3);
427 double mx = ex * 27 - a.fX * 8 - d.fX;
428 double my = ey * 27 - a.fY * 8 - d.fY;
429 double nx = fx * 27 - a.fX - d.fX * 8;
430 double ny = fy * 27 - a.fY - d.fY * 8;
431 /* bx = */ dst[0].fX = (mx * 2 - nx) / 18;
432 /* by = */ dst[0].fY = (my * 2 - ny) / 18;
433 /* cx = */ dst[1].fX = (nx * 2 - mx) / 18;
434 /* cy = */ dst[1].fY = (ny * 2 - my) / 18;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000435#else
436 // this approach assumes that the control points computed directly are accurate enough
437 SkDCubic sub = subDivide(t1, t2);
438 dst[0] = sub[1] + (a - sub[0]);
439 dst[1] = sub[2] + (d - sub[3]);
440#endif
441 if (t1 == 0 || t2 == 0) {
442 align(0, 1, t1 == 0 ? &dst[0] : &dst[1]);
443 }
444 if (t1 == 1 || t2 == 1) {
445 align(3, 2, t1 == 1 ? &dst[0] : &dst[1]);
446 }
447 if (precisely_subdivide_equal(dst[0].fX, a.fX)) {
448 dst[0].fX = a.fX;
449 }
450 if (precisely_subdivide_equal(dst[0].fY, a.fY)) {
451 dst[0].fY = a.fY;
452 }
453 if (precisely_subdivide_equal(dst[1].fX, d.fX)) {
454 dst[1].fX = d.fX;
455 }
456 if (precisely_subdivide_equal(dst[1].fY, d.fY)) {
457 dst[1].fY = d.fY;
458 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000459}
460
461/* classic one t subdivision */
462static void interp_cubic_coords(const double* src, double* dst, double t) {
463 double ab = SkDInterp(src[0], src[2], t);
464 double bc = SkDInterp(src[2], src[4], t);
465 double cd = SkDInterp(src[4], src[6], t);
466 double abc = SkDInterp(ab, bc, t);
467 double bcd = SkDInterp(bc, cd, t);
468 double abcd = SkDInterp(abc, bcd, t);
469
470 dst[0] = src[0];
471 dst[2] = ab;
472 dst[4] = abc;
473 dst[6] = abcd;
474 dst[8] = bcd;
475 dst[10] = cd;
476 dst[12] = src[6];
477}
478
479SkDCubicPair SkDCubic::chopAt(double t) const {
480 SkDCubicPair dst;
481 if (t == 0.5) {
482 dst.pts[0] = fPts[0];
483 dst.pts[1].fX = (fPts[0].fX + fPts[1].fX) / 2;
484 dst.pts[1].fY = (fPts[0].fY + fPts[1].fY) / 2;
485 dst.pts[2].fX = (fPts[0].fX + 2 * fPts[1].fX + fPts[2].fX) / 4;
486 dst.pts[2].fY = (fPts[0].fY + 2 * fPts[1].fY + fPts[2].fY) / 4;
487 dst.pts[3].fX = (fPts[0].fX + 3 * (fPts[1].fX + fPts[2].fX) + fPts[3].fX) / 8;
488 dst.pts[3].fY = (fPts[0].fY + 3 * (fPts[1].fY + fPts[2].fY) + fPts[3].fY) / 8;
489 dst.pts[4].fX = (fPts[1].fX + 2 * fPts[2].fX + fPts[3].fX) / 4;
490 dst.pts[4].fY = (fPts[1].fY + 2 * fPts[2].fY + fPts[3].fY) / 4;
491 dst.pts[5].fX = (fPts[2].fX + fPts[3].fX) / 2;
492 dst.pts[5].fY = (fPts[2].fY + fPts[3].fY) / 2;
493 dst.pts[6] = fPts[3];
494 return dst;
495 }
496 interp_cubic_coords(&fPts[0].fX, &dst.pts[0].fX, t);
497 interp_cubic_coords(&fPts[0].fY, &dst.pts[0].fY, t);
498 return dst;
499}