blob: 9d70d58ec15d7a5219637e2eff3f7b869b9f2b59 [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"
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000012#include "SkTSort.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +000013
14const int SkDCubic::gPrecisionUnit = 256; // FIXME: test different values in test framework
15
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +000016// give up when changing t no longer moves point
17// also, copy point rather than recompute it when it does change
18double SkDCubic::binarySearch(double min, double max, double axisIntercept,
19 SearchAxis xAxis) const {
20 double t = (min + max) / 2;
21 double step = (t - min) / 2;
22 SkDPoint cubicAtT = ptAtT(t);
23 double calcPos = (&cubicAtT.fX)[xAxis];
24 double calcDist = calcPos - axisIntercept;
25 do {
26 double priorT = t - step;
27 SkASSERT(priorT >= min);
28 SkDPoint lessPt = ptAtT(priorT);
29 if (approximately_equal(lessPt.fX, cubicAtT.fX)
30 && approximately_equal(lessPt.fY, cubicAtT.fY)) {
31 return -1; // binary search found no point at this axis intercept
32 }
33 double lessDist = (&lessPt.fX)[xAxis] - axisIntercept;
34#if DEBUG_CUBIC_BINARY_SEARCH
35 SkDebugf("t=%1.9g calc=%1.9g dist=%1.9g step=%1.9g less=%1.9g\n", t, calcPos, calcDist,
36 step, lessDist);
37#endif
38 double lastStep = step;
39 step /= 2;
40 if (calcDist > 0 ? calcDist > lessDist : calcDist < lessDist) {
41 t = priorT;
42 } else {
43 double nextT = t + lastStep;
44 SkASSERT(nextT <= max);
45 SkDPoint morePt = ptAtT(nextT);
46 if (approximately_equal(morePt.fX, cubicAtT.fX)
47 && approximately_equal(morePt.fY, cubicAtT.fY)) {
48 return -1; // binary search found no point at this axis intercept
49 }
50 double moreDist = (&morePt.fX)[xAxis] - axisIntercept;
51 if (calcDist > 0 ? calcDist <= moreDist : calcDist >= moreDist) {
52 continue;
53 }
54 t = nextT;
55 }
56 SkDPoint testAtT = ptAtT(t);
57 cubicAtT = testAtT;
58 calcPos = (&cubicAtT.fX)[xAxis];
59 calcDist = calcPos - axisIntercept;
60 } while (!approximately_equal(calcPos, axisIntercept));
61 return t;
62}
63
caryclark@google.com07393ca2013-04-08 11:47:37 +000064// FIXME: cache keep the bounds and/or precision with the caller?
65double SkDCubic::calcPrecision() const {
66 SkDRect dRect;
67 dRect.setBounds(*this); // OPTIMIZATION: just use setRawBounds ?
68 double width = dRect.fRight - dRect.fLeft;
69 double height = dRect.fBottom - dRect.fTop;
70 return (width > height ? width : height) / gPrecisionUnit;
71}
72
73bool SkDCubic::clockwise() const {
74 double sum = (fPts[0].fX - fPts[3].fX) * (fPts[0].fY + fPts[3].fY);
75 for (int idx = 0; idx < 3; ++idx) {
76 sum += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY);
77 }
78 return sum <= 0;
79}
80
81void SkDCubic::Coefficients(const double* src, double* A, double* B, double* C, double* D) {
82 *A = src[6]; // d
83 *B = src[4] * 3; // 3*c
84 *C = src[2] * 3; // 3*b
85 *D = src[0]; // a
86 *A -= *D - *C + *B; // A = -a + 3*b - 3*c + d
87 *B += 3 * *D - 2 * *C; // B = 3*a - 6*b + 3*c
88 *C -= 3 * *D; // C = -3*a + 3*b
89}
90
91bool SkDCubic::controlsContainedByEnds() const {
92 SkDVector startTan = fPts[1] - fPts[0];
93 if (startTan.fX == 0 && startTan.fY == 0) {
94 startTan = fPts[2] - fPts[0];
95 }
96 SkDVector endTan = fPts[2] - fPts[3];
97 if (endTan.fX == 0 && endTan.fY == 0) {
98 endTan = fPts[1] - fPts[3];
99 }
100 if (startTan.dot(endTan) >= 0) {
101 return false;
102 }
103 SkDLine startEdge = {{fPts[0], fPts[0]}};
104 startEdge[1].fX -= startTan.fY;
105 startEdge[1].fY += startTan.fX;
106 SkDLine endEdge = {{fPts[3], fPts[3]}};
107 endEdge[1].fX -= endTan.fY;
108 endEdge[1].fY += endTan.fX;
109 double leftStart1 = startEdge.isLeft(fPts[1]);
110 if (leftStart1 * startEdge.isLeft(fPts[2]) < 0) {
111 return false;
112 }
113 double leftEnd1 = endEdge.isLeft(fPts[1]);
114 if (leftEnd1 * endEdge.isLeft(fPts[2]) < 0) {
115 return false;
116 }
117 return leftStart1 * leftEnd1 >= 0;
118}
119
120bool SkDCubic::endsAreExtremaInXOrY() const {
121 return (between(fPts[0].fX, fPts[1].fX, fPts[3].fX)
122 && between(fPts[0].fX, fPts[2].fX, fPts[3].fX))
123 || (between(fPts[0].fY, fPts[1].fY, fPts[3].fY)
124 && between(fPts[0].fY, fPts[2].fY, fPts[3].fY));
125}
126
127bool SkDCubic::isLinear(int startIndex, int endIndex) const {
128 SkLineParameters lineParameters;
129 lineParameters.cubicEndPoints(*this, startIndex, endIndex);
130 // FIXME: maybe it's possible to avoid this and compare non-normalized
131 lineParameters.normalize();
132 double distance = lineParameters.controlPtDistance(*this, 1);
133 if (!approximately_zero(distance)) {
134 return false;
135 }
136 distance = lineParameters.controlPtDistance(*this, 2);
137 return approximately_zero(distance);
138}
139
140bool SkDCubic::monotonicInY() const {
141 return between(fPts[0].fY, fPts[1].fY, fPts[3].fY)
142 && between(fPts[0].fY, fPts[2].fY, fPts[3].fY);
143}
144
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000145int SkDCubic::searchRoots(double extremeTs[6], int extrema, double axisIntercept,
146 SearchAxis xAxis, double* validRoots) const {
147 extrema += findInflections(&extremeTs[extrema]);
148 extremeTs[extrema++] = 0;
149 extremeTs[extrema] = 1;
150 SkTQSort(extremeTs, extremeTs + extrema);
151 int validCount = 0;
152 for (int index = 0; index < extrema; ) {
153 double min = extremeTs[index];
154 double max = extremeTs[++index];
155 if (min == max) {
156 continue;
157 }
158 double newT = binarySearch(min, max, axisIntercept, xAxis);
159 if (newT >= 0) {
160 validRoots[validCount++] = newT;
161 }
162 }
163 return validCount;
164}
165
caryclark@google.com07393ca2013-04-08 11:47:37 +0000166bool SkDCubic::serpentine() const {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +0000167#if 0 // FIXME: enabling this fixes cubicOp114 but breaks cubicOp58d and cubicOp53d
168 double tValues[2];
169 // OPTIMIZATION : another case where caching the present of cubic inflections would be useful
170 return findInflections(tValues) > 1;
171#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000172 if (!controlsContainedByEnds()) {
173 return false;
174 }
175 double wiggle = (fPts[0].fX - fPts[2].fX) * (fPts[0].fY + fPts[2].fY);
176 for (int idx = 0; idx < 2; ++idx) {
177 wiggle += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY);
178 }
179 double waggle = (fPts[1].fX - fPts[3].fX) * (fPts[1].fY + fPts[3].fY);
180 for (int idx = 1; idx < 3; ++idx) {
181 waggle += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY);
182 }
183 return wiggle * waggle < 0;
184}
185
186// cubic roots
187
188static const double PI = 3.141592653589793;
189
190// from SkGeometry.cpp (and Numeric Solutions, 5.6)
191int SkDCubic::RootsValidT(double A, double B, double C, double D, double t[3]) {
192 double s[3];
193 int realRoots = RootsReal(A, B, C, D, s);
194 int foundRoots = SkDQuad::AddValidTs(s, realRoots, t);
195 return foundRoots;
196}
197
198int SkDCubic::RootsReal(double A, double B, double C, double D, double s[3]) {
199#ifdef SK_DEBUG
200 // create a string mathematica understands
201 // GDB set print repe 15 # if repeated digits is a bother
202 // set print elements 400 # if line doesn't fit
203 char str[1024];
204 sk_bzero(str, sizeof(str));
205 SK_SNPRINTF(str, sizeof(str), "Solve[%1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]",
206 A, B, C, D);
caryclark@google.com570863f2013-09-16 15:55:01 +0000207 SkPathOpsDebug::MathematicaIze(str, sizeof(str));
caryclark@google.com07393ca2013-04-08 11:47:37 +0000208#if ONE_OFF_DEBUG && ONE_OFF_DEBUG_MATHEMATICA
209 SkDebugf("%s\n", str);
210#endif
211#endif
212 if (approximately_zero(A)
213 && approximately_zero_when_compared_to(A, B)
214 && approximately_zero_when_compared_to(A, C)
215 && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic
216 return SkDQuad::RootsReal(B, C, D, s);
217 }
218 if (approximately_zero_when_compared_to(D, A)
219 && approximately_zero_when_compared_to(D, B)
220 && approximately_zero_when_compared_to(D, C)) { // 0 is one root
221 int num = SkDQuad::RootsReal(A, B, C, s);
222 for (int i = 0; i < num; ++i) {
223 if (approximately_zero(s[i])) {
224 return num;
225 }
226 }
227 s[num++] = 0;
228 return num;
229 }
230 if (approximately_zero(A + B + C + D)) { // 1 is one root
231 int num = SkDQuad::RootsReal(A, A + B, -D, s);
232 for (int i = 0; i < num; ++i) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000233 if (AlmostDequalUlps(s[i], 1)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000234 return num;
235 }
236 }
237 s[num++] = 1;
238 return num;
239 }
240 double a, b, c;
241 {
242 double invA = 1 / A;
243 a = B * invA;
244 b = C * invA;
245 c = D * invA;
246 }
247 double a2 = a * a;
248 double Q = (a2 - b * 3) / 9;
249 double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
250 double R2 = R * R;
251 double Q3 = Q * Q * Q;
252 double R2MinusQ3 = R2 - Q3;
253 double adiv3 = a / 3;
254 double r;
255 double* roots = s;
256 if (R2MinusQ3 < 0) { // we have 3 real roots
257 double theta = acos(R / sqrt(Q3));
258 double neg2RootQ = -2 * sqrt(Q);
259
260 r = neg2RootQ * cos(theta / 3) - adiv3;
261 *roots++ = r;
262
263 r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3;
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000264 if (!AlmostDequalUlps(s[0], r)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000265 *roots++ = r;
266 }
267 r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3;
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000268 if (!AlmostDequalUlps(s[0], r) && (roots - s == 1 || !AlmostDequalUlps(s[1], r))) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000269 *roots++ = r;
270 }
271 } else { // we have 1 real root
272 double sqrtR2MinusQ3 = sqrt(R2MinusQ3);
273 double A = fabs(R) + sqrtR2MinusQ3;
274 A = SkDCubeRoot(A);
275 if (R > 0) {
276 A = -A;
277 }
278 if (A != 0) {
279 A += Q / A;
280 }
281 r = A - adiv3;
282 *roots++ = r;
commit-bot@chromium.org2db7fe72014-05-07 15:31:40 +0000283 if (AlmostDequalUlps((double) R2, (double) Q3)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000284 r = -A / 2 - adiv3;
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000285 if (!AlmostDequalUlps(s[0], r)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000286 *roots++ = r;
287 }
288 }
289 }
290 return static_cast<int>(roots - s);
291}
292
293// from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf
294// c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3
295// c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2
296// = 3(b-a)(1-t)^2 + 6(c-b)t(1-t) + 3(d-c)t^2
297static double derivative_at_t(const double* src, double t) {
298 double one_t = 1 - t;
299 double a = src[0];
300 double b = src[2];
301 double c = src[4];
302 double d = src[6];
303 return 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t);
304}
305
306// OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t?
307SkDVector SkDCubic::dxdyAtT(double t) const {
308 SkDVector result = { derivative_at_t(&fPts[0].fX, t), derivative_at_t(&fPts[0].fY, t) };
309 return result;
310}
311
312// OPTIMIZE? share code with formulate_F1DotF2
313int SkDCubic::findInflections(double tValues[]) const {
314 double Ax = fPts[1].fX - fPts[0].fX;
315 double Ay = fPts[1].fY - fPts[0].fY;
316 double Bx = fPts[2].fX - 2 * fPts[1].fX + fPts[0].fX;
317 double By = fPts[2].fY - 2 * fPts[1].fY + fPts[0].fY;
318 double Cx = fPts[3].fX + 3 * (fPts[1].fX - fPts[2].fX) - fPts[0].fX;
319 double Cy = fPts[3].fY + 3 * (fPts[1].fY - fPts[2].fY) - fPts[0].fY;
320 return SkDQuad::RootsValidT(Bx * Cy - By * Cx, Ax * Cy - Ay * Cx, Ax * By - Ay * Bx, tValues);
321}
322
323static void formulate_F1DotF2(const double src[], double coeff[4]) {
324 double a = src[2] - src[0];
325 double b = src[4] - 2 * src[2] + src[0];
326 double c = src[6] + 3 * (src[2] - src[4]) - src[0];
327 coeff[0] = c * c;
328 coeff[1] = 3 * b * c;
329 coeff[2] = 2 * b * b + c * a;
330 coeff[3] = a * b;
331}
332
333/** SkDCubic'(t) = At^2 + Bt + C, where
334 A = 3(-a + 3(b - c) + d)
335 B = 6(a - 2b + c)
336 C = 3(b - a)
337 Solve for t, keeping only those that fit between 0 < t < 1
338*/
339int SkDCubic::FindExtrema(double a, double b, double c, double d, double tValues[2]) {
340 // we divide A,B,C by 3 to simplify
341 double A = d - a + 3*(b - c);
342 double B = 2*(a - b - b + c);
343 double C = b - a;
344
345 return SkDQuad::RootsValidT(A, B, C, tValues);
346}
347
348/* from SkGeometry.cpp
349 Looking for F' dot F'' == 0
350
351 A = b - a
352 B = c - 2b + a
353 C = d - 3c + 3b - a
354
355 F' = 3Ct^2 + 6Bt + 3A
356 F'' = 6Ct + 6B
357
358 F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
359*/
360int SkDCubic::findMaxCurvature(double tValues[]) const {
361 double coeffX[4], coeffY[4];
362 int i;
363 formulate_F1DotF2(&fPts[0].fX, coeffX);
364 formulate_F1DotF2(&fPts[0].fY, coeffY);
365 for (i = 0; i < 4; i++) {
366 coeffX[i] = coeffX[i] + coeffY[i];
367 }
368 return RootsValidT(coeffX[0], coeffX[1], coeffX[2], coeffX[3], tValues);
369}
370
371SkDPoint SkDCubic::top(double startT, double endT) const {
372 SkDCubic sub = subDivide(startT, endT);
373 SkDPoint topPt = sub[0];
374 if (topPt.fY > sub[3].fY || (topPt.fY == sub[3].fY && topPt.fX > sub[3].fX)) {
375 topPt = sub[3];
376 }
377 double extremeTs[2];
378 if (!sub.monotonicInY()) {
379 int roots = FindExtrema(sub[0].fY, sub[1].fY, sub[2].fY, sub[3].fY, extremeTs);
380 for (int index = 0; index < roots; ++index) {
381 double t = startT + (endT - startT) * extremeTs[index];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000382 SkDPoint mid = ptAtT(t);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000383 if (topPt.fY > mid.fY || (topPt.fY == mid.fY && topPt.fX > mid.fX)) {
384 topPt = mid;
385 }
386 }
387 }
388 return topPt;
389}
390
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000391SkDPoint SkDCubic::ptAtT(double t) const {
392 if (0 == t) {
393 return fPts[0];
394 }
395 if (1 == t) {
396 return fPts[3];
397 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000398 double one_t = 1 - t;
399 double one_t2 = one_t * one_t;
400 double a = one_t2 * one_t;
401 double b = 3 * one_t2 * t;
402 double t2 = t * t;
403 double c = 3 * one_t * t2;
404 double d = t2 * t;
405 SkDPoint result = {a * fPts[0].fX + b * fPts[1].fX + c * fPts[2].fX + d * fPts[3].fX,
406 a * fPts[0].fY + b * fPts[1].fY + c * fPts[2].fY + d * fPts[3].fY};
407 return result;
408}
409
410/*
411 Given a cubic c, t1, and t2, find a small cubic segment.
412
413 The new cubic is defined as points A, B, C, and D, where
414 s1 = 1 - t1
415 s2 = 1 - t2
416 A = c[0]*s1*s1*s1 + 3*c[1]*s1*s1*t1 + 3*c[2]*s1*t1*t1 + c[3]*t1*t1*t1
417 D = c[0]*s2*s2*s2 + 3*c[1]*s2*s2*t2 + 3*c[2]*s2*t2*t2 + c[3]*t2*t2*t2
418
419 We don't have B or C. So We define two equations to isolate them.
420 First, compute two reference T values 1/3 and 2/3 from t1 to t2:
421
422 c(at (2*t1 + t2)/3) == E
423 c(at (t1 + 2*t2)/3) == F
424
425 Next, compute where those values must be if we know the values of B and C:
426
427 _12 = A*2/3 + B*1/3
428 12_ = A*1/3 + B*2/3
429 _23 = B*2/3 + C*1/3
430 23_ = B*1/3 + C*2/3
431 _34 = C*2/3 + D*1/3
432 34_ = C*1/3 + D*2/3
433 _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
434 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
435 _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
436 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
437 _1234 = (A*4/9 + B*4/9 + C*1/9)*2/3 + (B*4/9 + C*4/9 + D*1/9)*1/3
438 = A*8/27 + B*12/27 + C*6/27 + D*1/27
439 = E
440 1234_ = (A*1/9 + B*4/9 + C*4/9)*1/3 + (B*1/9 + C*4/9 + D*4/9)*2/3
441 = A*1/27 + B*6/27 + C*12/27 + D*8/27
442 = F
443 E*27 = A*8 + B*12 + C*6 + D
444 F*27 = A + B*6 + C*12 + D*8
445
446Group the known values on one side:
447
448 M = E*27 - A*8 - D = B*12 + C* 6
449 N = F*27 - A - D*8 = B* 6 + C*12
450 M*2 - N = B*18
451 N*2 - M = C*18
452 B = (M*2 - N)/18
453 C = (N*2 - M)/18
454 */
455
456static double interp_cubic_coords(const double* src, double t) {
457 double ab = SkDInterp(src[0], src[2], t);
458 double bc = SkDInterp(src[2], src[4], t);
459 double cd = SkDInterp(src[4], src[6], t);
460 double abc = SkDInterp(ab, bc, t);
461 double bcd = SkDInterp(bc, cd, t);
462 double abcd = SkDInterp(abc, bcd, t);
463 return abcd;
464}
465
466SkDCubic SkDCubic::subDivide(double t1, double t2) const {
caryclark@google.comd892bd82013-06-17 14:10:36 +0000467 if (t1 == 0 || t2 == 1) {
468 if (t1 == 0 && t2 == 1) {
469 return *this;
470 }
471 SkDCubicPair pair = chopAt(t1 == 0 ? t2 : t1);
472 SkDCubic dst = t1 == 0 ? pair.first() : pair.second();
473 return dst;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000474 }
475 SkDCubic dst;
476 double ax = dst[0].fX = interp_cubic_coords(&fPts[0].fX, t1);
477 double ay = dst[0].fY = interp_cubic_coords(&fPts[0].fY, t1);
478 double ex = interp_cubic_coords(&fPts[0].fX, (t1*2+t2)/3);
479 double ey = interp_cubic_coords(&fPts[0].fY, (t1*2+t2)/3);
480 double fx = interp_cubic_coords(&fPts[0].fX, (t1+t2*2)/3);
481 double fy = interp_cubic_coords(&fPts[0].fY, (t1+t2*2)/3);
482 double dx = dst[3].fX = interp_cubic_coords(&fPts[0].fX, t2);
483 double dy = dst[3].fY = interp_cubic_coords(&fPts[0].fY, t2);
484 double mx = ex * 27 - ax * 8 - dx;
485 double my = ey * 27 - ay * 8 - dy;
486 double nx = fx * 27 - ax - dx * 8;
487 double ny = fy * 27 - ay - dy * 8;
488 /* bx = */ dst[1].fX = (mx * 2 - nx) / 18;
489 /* by = */ dst[1].fY = (my * 2 - ny) / 18;
490 /* cx = */ dst[2].fX = (nx * 2 - mx) / 18;
491 /* cy = */ dst[2].fY = (ny * 2 - my) / 18;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000492 // FIXME: call align() ?
caryclark@google.com07393ca2013-04-08 11:47:37 +0000493 return dst;
494}
495
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000496void SkDCubic::align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000497 if (fPts[endIndex].fX == fPts[ctrlIndex].fX) {
498 dstPt->fX = fPts[endIndex].fX;
499 }
500 if (fPts[endIndex].fY == fPts[ctrlIndex].fY) {
501 dstPt->fY = fPts[endIndex].fY;
502 }
503}
504
caryclark@google.com07393ca2013-04-08 11:47:37 +0000505void SkDCubic::subDivide(const SkDPoint& a, const SkDPoint& d,
506 double t1, double t2, SkDPoint dst[2]) const {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000507 SkASSERT(t1 != t2);
508#if 0
caryclark@google.com07393ca2013-04-08 11:47:37 +0000509 double ex = interp_cubic_coords(&fPts[0].fX, (t1 * 2 + t2) / 3);
510 double ey = interp_cubic_coords(&fPts[0].fY, (t1 * 2 + t2) / 3);
511 double fx = interp_cubic_coords(&fPts[0].fX, (t1 + t2 * 2) / 3);
512 double fy = interp_cubic_coords(&fPts[0].fY, (t1 + t2 * 2) / 3);
513 double mx = ex * 27 - a.fX * 8 - d.fX;
514 double my = ey * 27 - a.fY * 8 - d.fY;
515 double nx = fx * 27 - a.fX - d.fX * 8;
516 double ny = fy * 27 - a.fY - d.fY * 8;
517 /* bx = */ dst[0].fX = (mx * 2 - nx) / 18;
518 /* by = */ dst[0].fY = (my * 2 - ny) / 18;
519 /* cx = */ dst[1].fX = (nx * 2 - mx) / 18;
520 /* cy = */ dst[1].fY = (ny * 2 - my) / 18;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000521#else
522 // this approach assumes that the control points computed directly are accurate enough
523 SkDCubic sub = subDivide(t1, t2);
524 dst[0] = sub[1] + (a - sub[0]);
525 dst[1] = sub[2] + (d - sub[3]);
526#endif
527 if (t1 == 0 || t2 == 0) {
528 align(0, 1, t1 == 0 ? &dst[0] : &dst[1]);
529 }
530 if (t1 == 1 || t2 == 1) {
531 align(3, 2, t1 == 1 ? &dst[0] : &dst[1]);
532 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000533 if (AlmostBequalUlps(dst[0].fX, a.fX)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000534 dst[0].fX = a.fX;
535 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000536 if (AlmostBequalUlps(dst[0].fY, a.fY)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000537 dst[0].fY = a.fY;
538 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000539 if (AlmostBequalUlps(dst[1].fX, d.fX)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000540 dst[1].fX = d.fX;
541 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000542 if (AlmostBequalUlps(dst[1].fY, d.fY)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000543 dst[1].fY = d.fY;
544 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000545}
546
547/* classic one t subdivision */
548static void interp_cubic_coords(const double* src, double* dst, double t) {
549 double ab = SkDInterp(src[0], src[2], t);
550 double bc = SkDInterp(src[2], src[4], t);
551 double cd = SkDInterp(src[4], src[6], t);
552 double abc = SkDInterp(ab, bc, t);
553 double bcd = SkDInterp(bc, cd, t);
554 double abcd = SkDInterp(abc, bcd, t);
555
556 dst[0] = src[0];
557 dst[2] = ab;
558 dst[4] = abc;
559 dst[6] = abcd;
560 dst[8] = bcd;
561 dst[10] = cd;
562 dst[12] = src[6];
563}
564
565SkDCubicPair SkDCubic::chopAt(double t) const {
566 SkDCubicPair dst;
567 if (t == 0.5) {
568 dst.pts[0] = fPts[0];
569 dst.pts[1].fX = (fPts[0].fX + fPts[1].fX) / 2;
570 dst.pts[1].fY = (fPts[0].fY + fPts[1].fY) / 2;
571 dst.pts[2].fX = (fPts[0].fX + 2 * fPts[1].fX + fPts[2].fX) / 4;
572 dst.pts[2].fY = (fPts[0].fY + 2 * fPts[1].fY + fPts[2].fY) / 4;
573 dst.pts[3].fX = (fPts[0].fX + 3 * (fPts[1].fX + fPts[2].fX) + fPts[3].fX) / 8;
574 dst.pts[3].fY = (fPts[0].fY + 3 * (fPts[1].fY + fPts[2].fY) + fPts[3].fY) / 8;
575 dst.pts[4].fX = (fPts[1].fX + 2 * fPts[2].fX + fPts[3].fX) / 4;
576 dst.pts[4].fY = (fPts[1].fY + 2 * fPts[2].fY + fPts[3].fY) / 4;
577 dst.pts[5].fX = (fPts[2].fX + fPts[3].fX) / 2;
578 dst.pts[5].fY = (fPts[2].fY + fPts[3].fY) / 2;
579 dst.pts[6] = fPts[3];
580 return dst;
581 }
582 interp_cubic_coords(&fPts[0].fX, &dst.pts[0].fX, t);
583 interp_cubic_coords(&fPts[0].fY, &dst.pts[0].fY, t);
584 return dst;
585}