Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkReduceOrder.cpp
Go to the documentation of this file.
1/*
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 */
8
10#include "src/core/SkGeometry.h"
13
14#include <algorithm>
15#include <cmath>
16
18 fLine[0] = line[0];
19 int different = line[0] != line[1];
20 fLine[1] = line[different];
21 return 1 + different;
22}
23
24static int coincident_line(const SkDQuad& quad, SkDQuad& reduction) {
25 reduction[0] = reduction[1] = quad[0];
26 return 1;
27}
28
29static int reductionLineCount(const SkDQuad& reduction) {
30 return 1 + !reduction[0].approximatelyEqual(reduction[1]);
31}
32
33static int vertical_line(const SkDQuad& quad, SkDQuad& reduction) {
34 reduction[0] = quad[0];
35 reduction[1] = quad[2];
36 return reductionLineCount(reduction);
37}
38
39static int horizontal_line(const SkDQuad& quad, SkDQuad& reduction) {
40 reduction[0] = quad[0];
41 reduction[1] = quad[2];
42 return reductionLineCount(reduction);
43}
44
45static int check_linear(const SkDQuad& quad,
46 int minX, int maxX, int minY, int maxY, SkDQuad& reduction) {
47 if (!quad.isLinear(0, 2)) {
48 return 0;
49 }
50 // four are colinear: return line formed by outside
51 reduction[0] = quad[0];
52 reduction[1] = quad[2];
53 return reductionLineCount(reduction);
54}
55
56// reduce to a quadratic or smaller
57// look for identical points
58// look for all four points in a line
59 // note that three points in a line doesn't simplify a cubic
60// look for approximation with single quadratic
61 // save approximation with multiple quadratics for later
63 int index, minX, maxX, minY, maxY;
64 int minXSet, minYSet;
65 minX = maxX = minY = maxY = 0;
66 minXSet = minYSet = 0;
67 for (index = 1; index < 3; ++index) {
68 if (quad[minX].fX > quad[index].fX) {
69 minX = index;
70 }
71 if (quad[minY].fY > quad[index].fY) {
72 minY = index;
73 }
74 if (quad[maxX].fX < quad[index].fX) {
75 maxX = index;
76 }
77 if (quad[maxY].fY < quad[index].fY) {
78 maxY = index;
79 }
80 }
81 for (index = 0; index < 3; ++index) {
82 if (AlmostEqualUlps(quad[index].fX, quad[minX].fX)) {
83 minXSet |= 1 << index;
84 }
85 if (AlmostEqualUlps(quad[index].fY, quad[minY].fY)) {
86 minYSet |= 1 << index;
87 }
88 }
89 if ((minXSet & 0x05) == 0x5 && (minYSet & 0x05) == 0x5) { // test for degenerate
90 // this quad starts and ends at the same place, so never contributes
91 // to the fill
92 return coincident_line(quad, fQuad);
93 }
94 if (minXSet == 0x7) { // test for vertical line
95 return vertical_line(quad, fQuad);
96 }
97 if (minYSet == 0x7) { // test for horizontal line
98 return horizontal_line(quad, fQuad);
99 }
100 int result = check_linear(quad, minX, maxX, minY, maxY, fQuad);
101 if (result) {
102 return result;
103 }
104 fQuad = quad;
105 return 3;
106}
107
108////////////////////////////////////////////////////////////////////////////////////
109
110static int coincident_line(const SkDCubic& cubic, SkDCubic& reduction) {
111 reduction[0] = reduction[1] = cubic[0];
112 return 1;
113}
114
115static int reductionLineCount(const SkDCubic& reduction) {
116 return 1 + !reduction[0].approximatelyEqual(reduction[1]);
117}
118
119static int vertical_line(const SkDCubic& cubic, SkDCubic& reduction) {
120 reduction[0] = cubic[0];
121 reduction[1] = cubic[3];
122 return reductionLineCount(reduction);
123}
124
125static int horizontal_line(const SkDCubic& cubic, SkDCubic& reduction) {
126 reduction[0] = cubic[0];
127 reduction[1] = cubic[3];
128 return reductionLineCount(reduction);
129}
130
131// check to see if it is a quadratic or a line
132static int check_quadratic(const SkDCubic& cubic, SkDCubic& reduction) {
133 double dx10 = cubic[1].fX - cubic[0].fX;
134 double dx23 = cubic[2].fX - cubic[3].fX;
135 double midX = cubic[0].fX + dx10 * 3 / 2;
136 double sideAx = midX - cubic[3].fX;
137 double sideBx = dx23 * 3 / 2;
138 if (approximately_zero(sideAx) ? !approximately_equal(sideAx, sideBx)
139 : !AlmostEqualUlps_Pin(sideAx, sideBx)) {
140 return 0;
141 }
142 double dy10 = cubic[1].fY - cubic[0].fY;
143 double dy23 = cubic[2].fY - cubic[3].fY;
144 double midY = cubic[0].fY + dy10 * 3 / 2;
145 double sideAy = midY - cubic[3].fY;
146 double sideBy = dy23 * 3 / 2;
147 if (approximately_zero(sideAy) ? !approximately_equal(sideAy, sideBy)
148 : !AlmostEqualUlps_Pin(sideAy, sideBy)) {
149 return 0;
150 }
151 reduction[0] = cubic[0];
152 reduction[1].fX = midX;
153 reduction[1].fY = midY;
154 reduction[2] = cubic[3];
155 return 3;
156}
157
158static int check_linear(const SkDCubic& cubic,
159 int minX, int maxX, int minY, int maxY, SkDCubic& reduction) {
160 if (!cubic.isLinear(0, 3)) {
161 return 0;
162 }
163 // four are colinear: return line formed by outside
164 reduction[0] = cubic[0];
165 reduction[1] = cubic[3];
166 return reductionLineCount(reduction);
167}
168
169/* food for thought:
170http://objectmix.com/graphics/132906-fast-precision-driven-cubic-quadratic-piecewise-degree-reduction-algos-2-a.html
171
172Given points c1, c2, c3 and c4 of a cubic Bezier, the points of the
173corresponding quadratic Bezier are (given in convex combinations of
174points):
175
176q1 = (11/13)c1 + (3/13)c2 -(3/13)c3 + (2/13)c4
177q2 = -c1 + (3/2)c2 + (3/2)c3 - c4
178q3 = (2/13)c1 - (3/13)c2 + (3/13)c3 + (11/13)c4
179
180Of course, this curve does not interpolate the end-points, but it would
181be interesting to see the behaviour of such a curve in an applet.
182
183--
184Kalle Rutanen
185http://kaba.hilvi.org
186
187*/
188
189// reduce to a quadratic or smaller
190// look for identical points
191// look for all four points in a line
192 // note that three points in a line doesn't simplify a cubic
193// look for approximation with single quadratic
194 // save approximation with multiple quadratics for later
195int SkReduceOrder::reduce(const SkDCubic& cubic, Quadratics allowQuadratics) {
196 int index, minX, maxX, minY, maxY;
197 int minXSet, minYSet;
198 minX = maxX = minY = maxY = 0;
199 minXSet = minYSet = 0;
200 for (index = 1; index < 4; ++index) {
201 if (cubic[minX].fX > cubic[index].fX) {
202 minX = index;
203 }
204 if (cubic[minY].fY > cubic[index].fY) {
205 minY = index;
206 }
207 if (cubic[maxX].fX < cubic[index].fX) {
208 maxX = index;
209 }
210 if (cubic[maxY].fY < cubic[index].fY) {
211 maxY = index;
212 }
213 }
214 for (index = 0; index < 4; ++index) {
215 double cx = cubic[index].fX;
216 double cy = cubic[index].fY;
217 double denom = std::max(fabs(cx), std::max(fabs(cy),
218 std::max(fabs(cubic[minX].fX), fabs(cubic[minY].fY))));
219 if (denom == 0) {
220 minXSet |= 1 << index;
221 minYSet |= 1 << index;
222 continue;
223 }
224 double inv = 1 / denom;
225 if (approximately_equal_half(cx * inv, cubic[minX].fX * inv)) {
226 minXSet |= 1 << index;
227 }
228 if (approximately_equal_half(cy * inv, cubic[minY].fY * inv)) {
229 minYSet |= 1 << index;
230 }
231 }
232 if (minXSet == 0xF) { // test for vertical line
233 if (minYSet == 0xF) { // return 1 if all four are coincident
234 return coincident_line(cubic, fCubic);
235 }
236 return vertical_line(cubic, fCubic);
237 }
238 if (minYSet == 0xF) { // test for horizontal line
239 return horizontal_line(cubic, fCubic);
240 }
241 int result = check_linear(cubic, minX, maxX, minY, maxY, fCubic);
242 if (result) {
243 return result;
244 }
245 if (allowQuadratics == SkReduceOrder::kAllow_Quadratics
246 && (result = check_quadratic(cubic, fCubic))) {
247 return result;
248 }
249 fCubic = cubic;
250 return 4;
251}
252
254 SkDQuad quad;
255 quad.set(a);
256 SkReduceOrder reducer;
257 int order = reducer.reduce(quad);
258 if (order == 2) { // quad became line
259 for (int index = 0; index < order; ++index) {
260 *reducePts++ = reducer.fLine[index].asSkPoint();
261 }
262 }
263 return SkPathOpsPointsToVerb(order - 1);
264}
265
267 SkPath::Verb verb = SkReduceOrder::Quad(c.fPts, reducePts);
268 if (verb > SkPath::kLine_Verb && c.fW == 1) {
269 return SkPath::kQuad_Verb;
270 }
271 return verb == SkPath::kQuad_Verb ? SkPath::kConic_Verb : verb;
272}
273
276 && SkDPoint::ApproximatelyEqual(a[0], a[3])) {
277 reducePts[0] = a[0];
278 return SkPath::kMove_Verb;
279 }
280 SkDCubic cubic;
281 cubic.set(a);
282 SkReduceOrder reducer;
283 int order = reducer.reduce(cubic, kAllow_Quadratics);
284 if (order == 2 || order == 3) { // cubic became line or quad
285 for (int index = 0; index < order; ++index) {
286 *reducePts++ = reducer.fQuad[index].asSkPoint();
287 }
288 }
289 return SkPathOpsPointsToVerb(order - 1);
290}
static SkM44 inv(const SkM44 &m)
Definition 3d.cpp:26
static bool approximately_zero(double x)
Definition SkCubics.cpp:153
bool AlmostEqualUlps(const SkPoint &pt1, const SkPoint &pt2)
bool AlmostEqualUlps_Pin(float a, float b)
bool approximately_equal(double x, double y)
bool approximately_equal_half(double x, double y)
SkPath::Verb SkPathOpsPointsToVerb(int points)
static int coincident_line(const SkDQuad &quad, SkDQuad &reduction)
static int check_linear(const SkDQuad &quad, int minX, int maxX, int minY, int maxY, SkDQuad &reduction)
static int horizontal_line(const SkDQuad &quad, SkDQuad &reduction)
static int check_quadratic(const SkDCubic &cubic, SkDCubic &reduction)
static int reductionLineCount(const SkDQuad &reduction)
static int vertical_line(const SkDQuad &quad, SkDQuad &reduction)
@ kMove_Verb
Definition SkPath.h:1458
@ kConic_Verb
Definition SkPath.h:1461
@ kQuad_Verb
Definition SkPath.h:1460
@ kLine_Verb
Definition SkPath.h:1459
struct MyStruct a[10]
GAsyncResult * result
SkScalar fW
Definition SkGeometry.h:337
SkPoint fPts[3]
Definition SkGeometry.h:336
static bool ApproximatelyEqual(const SkPoint &a, const SkPoint &b)
const SkDQuad & set(const SkPoint pts[kPointCount] SkDEBUGPARAMS(SkOpGlobalState *state=nullptr))
bool isLinear(int startIndex, int endIndex) const
SkDCubic fCubic
static SkPath::Verb Cubic(const SkPoint pts[4], SkPoint *reducePts)
static SkPath::Verb Conic(const SkConic &conic, SkPoint *reducePts)
int reduce(const SkDCubic &cubic, Quadratics)
static SkPath::Verb Quad(const SkPoint pts[3], SkPoint *reducePts)