blob: a89604f94cafa98ae9617868cf36bf86014151aa [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 {
commit-bot@chromium.org8cb1daa2014-04-25 12:59:11 +000097#if 0 // FIXME: enabling this fixes cubicOp114 but breaks cubicOp58d and cubicOp53d
98 double tValues[2];
99 // OPTIMIZATION : another case where caching the present of cubic inflections would be useful
100 return findInflections(tValues) > 1;
101#endif
caryclark@google.com07393ca2013-04-08 11:47:37 +0000102 if (!controlsContainedByEnds()) {
103 return false;
104 }
105 double wiggle = (fPts[0].fX - fPts[2].fX) * (fPts[0].fY + fPts[2].fY);
106 for (int idx = 0; idx < 2; ++idx) {
107 wiggle += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY);
108 }
109 double waggle = (fPts[1].fX - fPts[3].fX) * (fPts[1].fY + fPts[3].fY);
110 for (int idx = 1; idx < 3; ++idx) {
111 waggle += (fPts[idx + 1].fX - fPts[idx].fX) * (fPts[idx + 1].fY + fPts[idx].fY);
112 }
113 return wiggle * waggle < 0;
114}
115
116// cubic roots
117
118static const double PI = 3.141592653589793;
119
120// from SkGeometry.cpp (and Numeric Solutions, 5.6)
121int SkDCubic::RootsValidT(double A, double B, double C, double D, double t[3]) {
122 double s[3];
123 int realRoots = RootsReal(A, B, C, D, s);
124 int foundRoots = SkDQuad::AddValidTs(s, realRoots, t);
125 return foundRoots;
126}
127
128int SkDCubic::RootsReal(double A, double B, double C, double D, double s[3]) {
129#ifdef SK_DEBUG
130 // create a string mathematica understands
131 // GDB set print repe 15 # if repeated digits is a bother
132 // set print elements 400 # if line doesn't fit
133 char str[1024];
134 sk_bzero(str, sizeof(str));
135 SK_SNPRINTF(str, sizeof(str), "Solve[%1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]",
136 A, B, C, D);
caryclark@google.com570863f2013-09-16 15:55:01 +0000137 SkPathOpsDebug::MathematicaIze(str, sizeof(str));
caryclark@google.com07393ca2013-04-08 11:47:37 +0000138#if ONE_OFF_DEBUG && ONE_OFF_DEBUG_MATHEMATICA
139 SkDebugf("%s\n", str);
140#endif
141#endif
142 if (approximately_zero(A)
143 && approximately_zero_when_compared_to(A, B)
144 && approximately_zero_when_compared_to(A, C)
145 && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic
146 return SkDQuad::RootsReal(B, C, D, s);
147 }
148 if (approximately_zero_when_compared_to(D, A)
149 && approximately_zero_when_compared_to(D, B)
150 && approximately_zero_when_compared_to(D, C)) { // 0 is one root
151 int num = SkDQuad::RootsReal(A, B, C, s);
152 for (int i = 0; i < num; ++i) {
153 if (approximately_zero(s[i])) {
154 return num;
155 }
156 }
157 s[num++] = 0;
158 return num;
159 }
160 if (approximately_zero(A + B + C + D)) { // 1 is one root
161 int num = SkDQuad::RootsReal(A, A + B, -D, s);
162 for (int i = 0; i < num; ++i) {
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000163 if (AlmostDequalUlps(s[i], 1)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000164 return num;
165 }
166 }
167 s[num++] = 1;
168 return num;
169 }
170 double a, b, c;
171 {
172 double invA = 1 / A;
173 a = B * invA;
174 b = C * invA;
175 c = D * invA;
176 }
177 double a2 = a * a;
178 double Q = (a2 - b * 3) / 9;
179 double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54;
180 double R2 = R * R;
181 double Q3 = Q * Q * Q;
182 double R2MinusQ3 = R2 - Q3;
183 double adiv3 = a / 3;
184 double r;
185 double* roots = s;
186 if (R2MinusQ3 < 0) { // we have 3 real roots
187 double theta = acos(R / sqrt(Q3));
188 double neg2RootQ = -2 * sqrt(Q);
189
190 r = neg2RootQ * cos(theta / 3) - adiv3;
191 *roots++ = r;
192
193 r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3;
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000194 if (!AlmostDequalUlps(s[0], r)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000195 *roots++ = r;
196 }
197 r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3;
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000198 if (!AlmostDequalUlps(s[0], r) && (roots - s == 1 || !AlmostDequalUlps(s[1], r))) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000199 *roots++ = r;
200 }
201 } else { // we have 1 real root
202 double sqrtR2MinusQ3 = sqrt(R2MinusQ3);
203 double A = fabs(R) + sqrtR2MinusQ3;
204 A = SkDCubeRoot(A);
205 if (R > 0) {
206 A = -A;
207 }
208 if (A != 0) {
209 A += Q / A;
210 }
211 r = A - adiv3;
212 *roots++ = r;
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000213 if (AlmostDequalUlps(R2, Q3)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000214 r = -A / 2 - adiv3;
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000215 if (!AlmostDequalUlps(s[0], r)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000216 *roots++ = r;
217 }
218 }
219 }
220 return static_cast<int>(roots - s);
221}
222
223// from http://www.cs.sunysb.edu/~qin/courses/geometry/4.pdf
224// c(t) = a(1-t)^3 + 3bt(1-t)^2 + 3c(1-t)t^2 + dt^3
225// c'(t) = -3a(1-t)^2 + 3b((1-t)^2 - 2t(1-t)) + 3c(2t(1-t) - t^2) + 3dt^2
226// = 3(b-a)(1-t)^2 + 6(c-b)t(1-t) + 3(d-c)t^2
227static double derivative_at_t(const double* src, double t) {
228 double one_t = 1 - t;
229 double a = src[0];
230 double b = src[2];
231 double c = src[4];
232 double d = src[6];
233 return 3 * ((b - a) * one_t * one_t + 2 * (c - b) * t * one_t + (d - c) * t * t);
234}
235
236// OPTIMIZE? compute t^2, t(1-t), and (1-t)^2 and pass them to another version of derivative at t?
237SkDVector SkDCubic::dxdyAtT(double t) const {
238 SkDVector result = { derivative_at_t(&fPts[0].fX, t), derivative_at_t(&fPts[0].fY, t) };
239 return result;
240}
241
242// OPTIMIZE? share code with formulate_F1DotF2
243int SkDCubic::findInflections(double tValues[]) const {
244 double Ax = fPts[1].fX - fPts[0].fX;
245 double Ay = fPts[1].fY - fPts[0].fY;
246 double Bx = fPts[2].fX - 2 * fPts[1].fX + fPts[0].fX;
247 double By = fPts[2].fY - 2 * fPts[1].fY + fPts[0].fY;
248 double Cx = fPts[3].fX + 3 * (fPts[1].fX - fPts[2].fX) - fPts[0].fX;
249 double Cy = fPts[3].fY + 3 * (fPts[1].fY - fPts[2].fY) - fPts[0].fY;
250 return SkDQuad::RootsValidT(Bx * Cy - By * Cx, Ax * Cy - Ay * Cx, Ax * By - Ay * Bx, tValues);
251}
252
253static void formulate_F1DotF2(const double src[], double coeff[4]) {
254 double a = src[2] - src[0];
255 double b = src[4] - 2 * src[2] + src[0];
256 double c = src[6] + 3 * (src[2] - src[4]) - src[0];
257 coeff[0] = c * c;
258 coeff[1] = 3 * b * c;
259 coeff[2] = 2 * b * b + c * a;
260 coeff[3] = a * b;
261}
262
263/** SkDCubic'(t) = At^2 + Bt + C, where
264 A = 3(-a + 3(b - c) + d)
265 B = 6(a - 2b + c)
266 C = 3(b - a)
267 Solve for t, keeping only those that fit between 0 < t < 1
268*/
269int SkDCubic::FindExtrema(double a, double b, double c, double d, double tValues[2]) {
270 // we divide A,B,C by 3 to simplify
271 double A = d - a + 3*(b - c);
272 double B = 2*(a - b - b + c);
273 double C = b - a;
274
275 return SkDQuad::RootsValidT(A, B, C, tValues);
276}
277
278/* from SkGeometry.cpp
279 Looking for F' dot F'' == 0
280
281 A = b - a
282 B = c - 2b + a
283 C = d - 3c + 3b - a
284
285 F' = 3Ct^2 + 6Bt + 3A
286 F'' = 6Ct + 6B
287
288 F' dot F'' -> CCt^3 + 3BCt^2 + (2BB + CA)t + AB
289*/
290int SkDCubic::findMaxCurvature(double tValues[]) const {
291 double coeffX[4], coeffY[4];
292 int i;
293 formulate_F1DotF2(&fPts[0].fX, coeffX);
294 formulate_F1DotF2(&fPts[0].fY, coeffY);
295 for (i = 0; i < 4; i++) {
296 coeffX[i] = coeffX[i] + coeffY[i];
297 }
298 return RootsValidT(coeffX[0], coeffX[1], coeffX[2], coeffX[3], tValues);
299}
300
301SkDPoint SkDCubic::top(double startT, double endT) const {
302 SkDCubic sub = subDivide(startT, endT);
303 SkDPoint topPt = sub[0];
304 if (topPt.fY > sub[3].fY || (topPt.fY == sub[3].fY && topPt.fX > sub[3].fX)) {
305 topPt = sub[3];
306 }
307 double extremeTs[2];
308 if (!sub.monotonicInY()) {
309 int roots = FindExtrema(sub[0].fY, sub[1].fY, sub[2].fY, sub[3].fY, extremeTs);
310 for (int index = 0; index < roots; ++index) {
311 double t = startT + (endT - startT) * extremeTs[index];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000312 SkDPoint mid = ptAtT(t);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000313 if (topPt.fY > mid.fY || (topPt.fY == mid.fY && topPt.fX > mid.fX)) {
314 topPt = mid;
315 }
316 }
317 }
318 return topPt;
319}
320
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000321SkDPoint SkDCubic::ptAtT(double t) const {
322 if (0 == t) {
323 return fPts[0];
324 }
325 if (1 == t) {
326 return fPts[3];
327 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000328 double one_t = 1 - t;
329 double one_t2 = one_t * one_t;
330 double a = one_t2 * one_t;
331 double b = 3 * one_t2 * t;
332 double t2 = t * t;
333 double c = 3 * one_t * t2;
334 double d = t2 * t;
335 SkDPoint result = {a * fPts[0].fX + b * fPts[1].fX + c * fPts[2].fX + d * fPts[3].fX,
336 a * fPts[0].fY + b * fPts[1].fY + c * fPts[2].fY + d * fPts[3].fY};
337 return result;
338}
339
340/*
341 Given a cubic c, t1, and t2, find a small cubic segment.
342
343 The new cubic is defined as points A, B, C, and D, where
344 s1 = 1 - t1
345 s2 = 1 - t2
346 A = c[0]*s1*s1*s1 + 3*c[1]*s1*s1*t1 + 3*c[2]*s1*t1*t1 + c[3]*t1*t1*t1
347 D = c[0]*s2*s2*s2 + 3*c[1]*s2*s2*t2 + 3*c[2]*s2*t2*t2 + c[3]*t2*t2*t2
348
349 We don't have B or C. So We define two equations to isolate them.
350 First, compute two reference T values 1/3 and 2/3 from t1 to t2:
351
352 c(at (2*t1 + t2)/3) == E
353 c(at (t1 + 2*t2)/3) == F
354
355 Next, compute where those values must be if we know the values of B and C:
356
357 _12 = A*2/3 + B*1/3
358 12_ = A*1/3 + B*2/3
359 _23 = B*2/3 + C*1/3
360 23_ = B*1/3 + C*2/3
361 _34 = C*2/3 + D*1/3
362 34_ = C*1/3 + D*2/3
363 _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
364 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
365 _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
366 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
367 _1234 = (A*4/9 + B*4/9 + C*1/9)*2/3 + (B*4/9 + C*4/9 + D*1/9)*1/3
368 = A*8/27 + B*12/27 + C*6/27 + D*1/27
369 = E
370 1234_ = (A*1/9 + B*4/9 + C*4/9)*1/3 + (B*1/9 + C*4/9 + D*4/9)*2/3
371 = A*1/27 + B*6/27 + C*12/27 + D*8/27
372 = F
373 E*27 = A*8 + B*12 + C*6 + D
374 F*27 = A + B*6 + C*12 + D*8
375
376Group the known values on one side:
377
378 M = E*27 - A*8 - D = B*12 + C* 6
379 N = F*27 - A - D*8 = B* 6 + C*12
380 M*2 - N = B*18
381 N*2 - M = C*18
382 B = (M*2 - N)/18
383 C = (N*2 - M)/18
384 */
385
386static double interp_cubic_coords(const double* src, double t) {
387 double ab = SkDInterp(src[0], src[2], t);
388 double bc = SkDInterp(src[2], src[4], t);
389 double cd = SkDInterp(src[4], src[6], t);
390 double abc = SkDInterp(ab, bc, t);
391 double bcd = SkDInterp(bc, cd, t);
392 double abcd = SkDInterp(abc, bcd, t);
393 return abcd;
394}
395
396SkDCubic SkDCubic::subDivide(double t1, double t2) const {
caryclark@google.comd892bd82013-06-17 14:10:36 +0000397 if (t1 == 0 || t2 == 1) {
398 if (t1 == 0 && t2 == 1) {
399 return *this;
400 }
401 SkDCubicPair pair = chopAt(t1 == 0 ? t2 : t1);
402 SkDCubic dst = t1 == 0 ? pair.first() : pair.second();
403 return dst;
caryclark@google.com07393ca2013-04-08 11:47:37 +0000404 }
405 SkDCubic dst;
406 double ax = dst[0].fX = interp_cubic_coords(&fPts[0].fX, t1);
407 double ay = dst[0].fY = interp_cubic_coords(&fPts[0].fY, t1);
408 double ex = interp_cubic_coords(&fPts[0].fX, (t1*2+t2)/3);
409 double ey = interp_cubic_coords(&fPts[0].fY, (t1*2+t2)/3);
410 double fx = interp_cubic_coords(&fPts[0].fX, (t1+t2*2)/3);
411 double fy = interp_cubic_coords(&fPts[0].fY, (t1+t2*2)/3);
412 double dx = dst[3].fX = interp_cubic_coords(&fPts[0].fX, t2);
413 double dy = dst[3].fY = interp_cubic_coords(&fPts[0].fY, t2);
414 double mx = ex * 27 - ax * 8 - dx;
415 double my = ey * 27 - ay * 8 - dy;
416 double nx = fx * 27 - ax - dx * 8;
417 double ny = fy * 27 - ay - dy * 8;
418 /* bx = */ dst[1].fX = (mx * 2 - nx) / 18;
419 /* by = */ dst[1].fY = (my * 2 - ny) / 18;
420 /* cx = */ dst[2].fX = (nx * 2 - mx) / 18;
421 /* cy = */ dst[2].fY = (ny * 2 - my) / 18;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000422 // FIXME: call align() ?
caryclark@google.com07393ca2013-04-08 11:47:37 +0000423 return dst;
424}
425
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000426void SkDCubic::align(int endIndex, int ctrlIndex, SkDPoint* dstPt) const {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000427 if (fPts[endIndex].fX == fPts[ctrlIndex].fX) {
428 dstPt->fX = fPts[endIndex].fX;
429 }
430 if (fPts[endIndex].fY == fPts[ctrlIndex].fY) {
431 dstPt->fY = fPts[endIndex].fY;
432 }
433}
434
caryclark@google.com07393ca2013-04-08 11:47:37 +0000435void SkDCubic::subDivide(const SkDPoint& a, const SkDPoint& d,
436 double t1, double t2, SkDPoint dst[2]) const {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000437 SkASSERT(t1 != t2);
438#if 0
caryclark@google.com07393ca2013-04-08 11:47:37 +0000439 double ex = interp_cubic_coords(&fPts[0].fX, (t1 * 2 + t2) / 3);
440 double ey = interp_cubic_coords(&fPts[0].fY, (t1 * 2 + t2) / 3);
441 double fx = interp_cubic_coords(&fPts[0].fX, (t1 + t2 * 2) / 3);
442 double fy = interp_cubic_coords(&fPts[0].fY, (t1 + t2 * 2) / 3);
443 double mx = ex * 27 - a.fX * 8 - d.fX;
444 double my = ey * 27 - a.fY * 8 - d.fY;
445 double nx = fx * 27 - a.fX - d.fX * 8;
446 double ny = fy * 27 - a.fY - d.fY * 8;
447 /* bx = */ dst[0].fX = (mx * 2 - nx) / 18;
448 /* by = */ dst[0].fY = (my * 2 - ny) / 18;
449 /* cx = */ dst[1].fX = (nx * 2 - mx) / 18;
450 /* cy = */ dst[1].fY = (ny * 2 - my) / 18;
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000451#else
452 // this approach assumes that the control points computed directly are accurate enough
453 SkDCubic sub = subDivide(t1, t2);
454 dst[0] = sub[1] + (a - sub[0]);
455 dst[1] = sub[2] + (d - sub[3]);
456#endif
457 if (t1 == 0 || t2 == 0) {
458 align(0, 1, t1 == 0 ? &dst[0] : &dst[1]);
459 }
460 if (t1 == 1 || t2 == 1) {
461 align(3, 2, t1 == 1 ? &dst[0] : &dst[1]);
462 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000463 if (AlmostBequalUlps(dst[0].fX, a.fX)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000464 dst[0].fX = a.fX;
465 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000466 if (AlmostBequalUlps(dst[0].fY, a.fY)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000467 dst[0].fY = a.fY;
468 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000469 if (AlmostBequalUlps(dst[1].fX, d.fX)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000470 dst[1].fX = d.fX;
471 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000472 if (AlmostBequalUlps(dst[1].fY, d.fY)) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000473 dst[1].fY = d.fY;
474 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000475}
476
477/* classic one t subdivision */
478static void interp_cubic_coords(const double* src, double* dst, double t) {
479 double ab = SkDInterp(src[0], src[2], t);
480 double bc = SkDInterp(src[2], src[4], t);
481 double cd = SkDInterp(src[4], src[6], t);
482 double abc = SkDInterp(ab, bc, t);
483 double bcd = SkDInterp(bc, cd, t);
484 double abcd = SkDInterp(abc, bcd, t);
485
486 dst[0] = src[0];
487 dst[2] = ab;
488 dst[4] = abc;
489 dst[6] = abcd;
490 dst[8] = bcd;
491 dst[10] = cd;
492 dst[12] = src[6];
493}
494
495SkDCubicPair SkDCubic::chopAt(double t) const {
496 SkDCubicPair dst;
497 if (t == 0.5) {
498 dst.pts[0] = fPts[0];
499 dst.pts[1].fX = (fPts[0].fX + fPts[1].fX) / 2;
500 dst.pts[1].fY = (fPts[0].fY + fPts[1].fY) / 2;
501 dst.pts[2].fX = (fPts[0].fX + 2 * fPts[1].fX + fPts[2].fX) / 4;
502 dst.pts[2].fY = (fPts[0].fY + 2 * fPts[1].fY + fPts[2].fY) / 4;
503 dst.pts[3].fX = (fPts[0].fX + 3 * (fPts[1].fX + fPts[2].fX) + fPts[3].fX) / 8;
504 dst.pts[3].fY = (fPts[0].fY + 3 * (fPts[1].fY + fPts[2].fY) + fPts[3].fY) / 8;
505 dst.pts[4].fX = (fPts[1].fX + 2 * fPts[2].fX + fPts[3].fX) / 4;
506 dst.pts[4].fY = (fPts[1].fY + 2 * fPts[2].fY + fPts[3].fY) / 4;
507 dst.pts[5].fX = (fPts[2].fX + fPts[3].fX) / 2;
508 dst.pts[5].fY = (fPts[2].fY + fPts[3].fY) / 2;
509 dst.pts[6] = fPts[3];
510 return dst;
511 }
512 interp_cubic_coords(&fPts[0].fX, &dst.pts[0].fX, t);
513 interp_cubic_coords(&fPts[0].fY, &dst.pts[0].fY, t);
514 return dst;
515}