blob: 7f7ea11d3bb3efa285bd271a78d7b1bbf906fa60 [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 */
caryclark27c015d2016-09-23 05:47:20 -07007#include "SkGeometry.h"
caryclark@google.com07393ca2013-04-08 11:47:37 +00008#include "SkReduceOrder.h"
9
10int SkReduceOrder::reduce(const SkDLine& line) {
11 fLine[0] = line[0];
12 int different = line[0] != line[1];
13 fLine[1] = line[different];
14 return 1 + different;
15}
16
caryclark@google.com07393ca2013-04-08 11:47:37 +000017static int coincident_line(const SkDQuad& quad, SkDQuad& reduction) {
18 reduction[0] = reduction[1] = quad[0];
19 return 1;
20}
21
22static int reductionLineCount(const SkDQuad& reduction) {
23 return 1 + !reduction[0].approximatelyEqual(reduction[1]);
24}
25
caryclark@google.com927b7022013-11-25 14:18:21 +000026static int vertical_line(const SkDQuad& quad, SkDQuad& reduction) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000027 reduction[0] = quad[0];
28 reduction[1] = quad[2];
caryclark@google.com07393ca2013-04-08 11:47:37 +000029 return reductionLineCount(reduction);
30}
31
caryclark@google.com927b7022013-11-25 14:18:21 +000032static int horizontal_line(const SkDQuad& quad, SkDQuad& reduction) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000033 reduction[0] = quad[0];
34 reduction[1] = quad[2];
caryclark@google.com07393ca2013-04-08 11:47:37 +000035 return reductionLineCount(reduction);
36}
37
caryclark@google.com927b7022013-11-25 14:18:21 +000038static int check_linear(const SkDQuad& quad,
caryclark@google.com07393ca2013-04-08 11:47:37 +000039 int minX, int maxX, int minY, int maxY, SkDQuad& reduction) {
caryclark2d9c59f2015-10-30 13:56:50 -070040 if (!quad.isLinear(0, 2)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000041 return 0;
42 }
43 // four are colinear: return line formed by outside
44 reduction[0] = quad[0];
45 reduction[1] = quad[2];
caryclark@google.com07393ca2013-04-08 11:47:37 +000046 return reductionLineCount(reduction);
47}
48
49// reduce to a quadratic or smaller
50// look for identical points
51// look for all four points in a line
52 // note that three points in a line doesn't simplify a cubic
53// look for approximation with single quadratic
54 // save approximation with multiple quadratics for later
caryclark@google.com927b7022013-11-25 14:18:21 +000055int SkReduceOrder::reduce(const SkDQuad& quad) {
caryclark@google.com07393ca2013-04-08 11:47:37 +000056 int index, minX, maxX, minY, maxY;
57 int minXSet, minYSet;
58 minX = maxX = minY = maxY = 0;
59 minXSet = minYSet = 0;
60 for (index = 1; index < 3; ++index) {
61 if (quad[minX].fX > quad[index].fX) {
62 minX = index;
63 }
64 if (quad[minY].fY > quad[index].fY) {
65 minY = index;
66 }
67 if (quad[maxX].fX < quad[index].fX) {
68 maxX = index;
69 }
70 if (quad[maxY].fY < quad[index].fY) {
71 maxY = index;
72 }
73 }
74 for (index = 0; index < 3; ++index) {
75 if (AlmostEqualUlps(quad[index].fX, quad[minX].fX)) {
76 minXSet |= 1 << index;
77 }
78 if (AlmostEqualUlps(quad[index].fY, quad[minY].fY)) {
79 minYSet |= 1 << index;
80 }
81 }
caryclark55888e42016-07-18 10:01:36 -070082 if ((minXSet & 0x05) == 0x5 && (minYSet & 0x05) == 0x5) { // test for degenerate
83 // this quad starts and ends at the same place, so never contributes
84 // to the fill
85 return coincident_line(quad, fQuad);
86 }
caryclark@google.com07393ca2013-04-08 11:47:37 +000087 if (minXSet == 0x7) { // test for vertical line
caryclark@google.com927b7022013-11-25 14:18:21 +000088 return vertical_line(quad, fQuad);
caryclark@google.com07393ca2013-04-08 11:47:37 +000089 }
caryclark65f55312014-11-13 06:58:52 -080090 if (minYSet == 0x7) { // test for horizontal line
caryclark@google.com927b7022013-11-25 14:18:21 +000091 return horizontal_line(quad, fQuad);
caryclark@google.com07393ca2013-04-08 11:47:37 +000092 }
caryclark@google.com927b7022013-11-25 14:18:21 +000093 int result = check_linear(quad, minX, maxX, minY, maxY, fQuad);
caryclark@google.com07393ca2013-04-08 11:47:37 +000094 if (result) {
95 return result;
96 }
97 fQuad = quad;
98 return 3;
99}
100
101////////////////////////////////////////////////////////////////////////////////////
102
caryclark@google.com07393ca2013-04-08 11:47:37 +0000103static int coincident_line(const SkDCubic& cubic, SkDCubic& reduction) {
104 reduction[0] = reduction[1] = cubic[0];
105 return 1;
106}
107
108static int reductionLineCount(const SkDCubic& reduction) {
109 return 1 + !reduction[0].approximatelyEqual(reduction[1]);
110}
111
caryclark@google.com927b7022013-11-25 14:18:21 +0000112static int vertical_line(const SkDCubic& cubic, SkDCubic& reduction) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000113 reduction[0] = cubic[0];
114 reduction[1] = cubic[3];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000115 return reductionLineCount(reduction);
116}
117
caryclark@google.com927b7022013-11-25 14:18:21 +0000118static int horizontal_line(const SkDCubic& cubic, SkDCubic& reduction) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000119 reduction[0] = cubic[0];
120 reduction[1] = cubic[3];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000121 return reductionLineCount(reduction);
122}
123
124// check to see if it is a quadratic or a line
125static int check_quadratic(const SkDCubic& cubic, SkDCubic& reduction) {
126 double dx10 = cubic[1].fX - cubic[0].fX;
127 double dx23 = cubic[2].fX - cubic[3].fX;
128 double midX = cubic[0].fX + dx10 * 3 / 2;
caryclark@google.com7ec5e392013-04-17 16:19:02 +0000129 double sideAx = midX - cubic[3].fX;
130 double sideBx = dx23 * 3 / 2;
131 if (approximately_zero(sideAx) ? !approximately_equal(sideAx, sideBx)
caryclarkb6693002015-12-16 12:28:35 -0800132 : !AlmostEqualUlps_Pin(sideAx, sideBx)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000133 return 0;
134 }
135 double dy10 = cubic[1].fY - cubic[0].fY;
136 double dy23 = cubic[2].fY - cubic[3].fY;
137 double midY = cubic[0].fY + dy10 * 3 / 2;
caryclark@google.com7ec5e392013-04-17 16:19:02 +0000138 double sideAy = midY - cubic[3].fY;
139 double sideBy = dy23 * 3 / 2;
140 if (approximately_zero(sideAy) ? !approximately_equal(sideAy, sideBy)
caryclarkb6693002015-12-16 12:28:35 -0800141 : !AlmostEqualUlps_Pin(sideAy, sideBy)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000142 return 0;
143 }
144 reduction[0] = cubic[0];
145 reduction[1].fX = midX;
146 reduction[1].fY = midY;
147 reduction[2] = cubic[3];
148 return 3;
149}
150
caryclark@google.com927b7022013-11-25 14:18:21 +0000151static int check_linear(const SkDCubic& cubic,
caryclark@google.com07393ca2013-04-08 11:47:37 +0000152 int minX, int maxX, int minY, int maxY, SkDCubic& reduction) {
caryclark2d9c59f2015-10-30 13:56:50 -0700153 if (!cubic.isLinear(0, 3)) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000154 return 0;
155 }
156 // four are colinear: return line formed by outside
157 reduction[0] = cubic[0];
158 reduction[1] = cubic[3];
caryclark@google.com07393ca2013-04-08 11:47:37 +0000159 return reductionLineCount(reduction);
160}
161
162/* food for thought:
163http://objectmix.com/graphics/132906-fast-precision-driven-cubic-quadratic-piecewise-degree-reduction-algos-2-a.html
164
165Given points c1, c2, c3 and c4 of a cubic Bezier, the points of the
166corresponding quadratic Bezier are (given in convex combinations of
167points):
168
169q1 = (11/13)c1 + (3/13)c2 -(3/13)c3 + (2/13)c4
170q2 = -c1 + (3/2)c2 + (3/2)c3 - c4
171q3 = (2/13)c1 - (3/13)c2 + (3/13)c3 + (11/13)c4
172
173Of course, this curve does not interpolate the end-points, but it would
174be interesting to see the behaviour of such a curve in an applet.
175
176--
177Kalle Rutanen
178http://kaba.hilvi.org
179
180*/
181
182// reduce to a quadratic or smaller
183// look for identical points
184// look for all four points in a line
185 // note that three points in a line doesn't simplify a cubic
186// look for approximation with single quadratic
187 // save approximation with multiple quadratics for later
caryclark@google.com927b7022013-11-25 14:18:21 +0000188int SkReduceOrder::reduce(const SkDCubic& cubic, Quadratics allowQuadratics) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000189 int index, minX, maxX, minY, maxY;
190 int minXSet, minYSet;
191 minX = maxX = minY = maxY = 0;
192 minXSet = minYSet = 0;
193 for (index = 1; index < 4; ++index) {
194 if (cubic[minX].fX > cubic[index].fX) {
195 minX = index;
196 }
197 if (cubic[minY].fY > cubic[index].fY) {
198 minY = index;
199 }
200 if (cubic[maxX].fX < cubic[index].fX) {
201 maxX = index;
202 }
203 if (cubic[maxY].fY < cubic[index].fY) {
204 maxY = index;
205 }
206 }
207 for (index = 0; index < 4; ++index) {
208 double cx = cubic[index].fX;
209 double cy = cubic[index].fY;
210 double denom = SkTMax(fabs(cx), SkTMax(fabs(cy),
211 SkTMax(fabs(cubic[minX].fX), fabs(cubic[minY].fY))));
212 if (denom == 0) {
213 minXSet |= 1 << index;
214 minYSet |= 1 << index;
215 continue;
216 }
217 double inv = 1 / denom;
218 if (approximately_equal_half(cx * inv, cubic[minX].fX * inv)) {
219 minXSet |= 1 << index;
220 }
221 if (approximately_equal_half(cy * inv, cubic[minY].fY * inv)) {
222 minYSet |= 1 << index;
223 }
224 }
225 if (minXSet == 0xF) { // test for vertical line
226 if (minYSet == 0xF) { // return 1 if all four are coincident
227 return coincident_line(cubic, fCubic);
228 }
caryclark@google.com927b7022013-11-25 14:18:21 +0000229 return vertical_line(cubic, fCubic);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000230 }
231 if (minYSet == 0xF) { // test for horizontal line
caryclark@google.com927b7022013-11-25 14:18:21 +0000232 return horizontal_line(cubic, fCubic);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000233 }
caryclark@google.com927b7022013-11-25 14:18:21 +0000234 int result = check_linear(cubic, minX, maxX, minY, maxY, fCubic);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000235 if (result) {
236 return result;
237 }
238 if (allowQuadratics == SkReduceOrder::kAllow_Quadratics
239 && (result = check_quadratic(cubic, fCubic))) {
240 return result;
241 }
242 fCubic = cubic;
243 return 4;
244}
245
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000246SkPath::Verb SkReduceOrder::Quad(const SkPoint a[3], SkPoint* reducePts) {
caryclark@google.com07393ca2013-04-08 11:47:37 +0000247 SkDQuad quad;
248 quad.set(a);
249 SkReduceOrder reducer;
caryclark@google.com927b7022013-11-25 14:18:21 +0000250 int order = reducer.reduce(quad);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000251 if (order == 2) { // quad became line
252 for (int index = 0; index < order; ++index) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000253 *reducePts++ = reducer.fLine[index].asSkPoint();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000254 }
255 }
reed@google.com277c3f82013-05-31 15:17:50 +0000256 return SkPathOpsPointsToVerb(order - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000257}
258
caryclark27c015d2016-09-23 05:47:20 -0700259SkPath::Verb SkReduceOrder::Conic(const SkConic& c, SkPoint* reducePts) {
260 SkPath::Verb verb = SkReduceOrder::Quad(c.fPts, reducePts);
261 if (verb > SkPath::kLine_Verb && c.fW == 1) {
caryclark1049f122015-04-20 08:31:59 -0700262 return SkPath::kQuad_Verb;
263 }
264 return verb == SkPath::kQuad_Verb ? SkPath::kConic_Verb : verb;
265}
266
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000267SkPath::Verb SkReduceOrder::Cubic(const SkPoint a[4], SkPoint* reducePts) {
caryclark54359292015-03-26 07:52:43 -0700268 if (SkDPoint::ApproximatelyEqual(a[0], a[1]) && SkDPoint::ApproximatelyEqual(a[0], a[2])
269 && SkDPoint::ApproximatelyEqual(a[0], a[3])) {
270 reducePts[0] = a[0];
271 return SkPath::kMove_Verb;
272 }
caryclark@google.com07393ca2013-04-08 11:47:37 +0000273 SkDCubic cubic;
274 cubic.set(a);
275 SkReduceOrder reducer;
caryclark@google.com927b7022013-11-25 14:18:21 +0000276 int order = reducer.reduce(cubic, kAllow_Quadratics);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000277 if (order == 2 || order == 3) { // cubic became line or quad
278 for (int index = 0; index < order; ++index) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000279 *reducePts++ = reducer.fQuad[index].asSkPoint();
caryclark@google.com07393ca2013-04-08 11:47:37 +0000280 }
281 }
reed@google.com277c3f82013-05-31 15:17:50 +0000282 return SkPathOpsPointsToVerb(order - 1);
caryclark@google.com07393ca2013-04-08 11:47:37 +0000283}