Debian Codegraph @

src/pathops/SkPathOpsSimplify.cpp

raw
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 */
7 #include "include/core/SkPath.h"
8 #include "include/core/SkPathTypes.h"
9 #include "include/core/SkTypes.h"
10 #include "include/pathops/SkPathOps.h"
11 #include "include/private/base/SkPoint_impl.h"
12 #include "include/private/base/SkTDArray.h"
13 #include "src/base/SkArenaAlloc.h"
14 #include "src/pathops/SkAddIntersections.h"
15 #include "src/pathops/SkOpCoincidence.h"
16 #include "src/pathops/SkOpContour.h"
17 #include "src/pathops/SkOpEdgeBuilder.h"
18 #include "src/pathops/SkOpSegment.h"
19 #include "src/pathops/SkOpSpan.h"
20 #include "src/pathops/SkPathOpsCommon.h"
21 #include "src/pathops/SkPathOpsTypes.h"
22 #include "src/pathops/SkPathWriter.h"
23
24 static bool bridgeWinding(SkOpContourHead* contourList, SkPathWriter* writer) {
25 bool unsortable = false;
26 do {
27 SkOpSpan* span = FindSortableTop(contourList);
28 if (!span) {
29 break;
30 }
31 SkOpSegment* current = span->segment();
32 SkOpSpanBase* start = span->next();
33 SkOpSpanBase* end = span;
34 SkTDArray<SkOpSpanBase*> chase;
35 do {
36 if (current->activeWinding(start, end)) {
37 do {
38 if (!unsortable && current->done()) {
39 break;
40 }
41 SkASSERT(unsortable || !current->done());
42 SkOpSpanBase* nextStart = start;
43 SkOpSpanBase* nextEnd = end;
44 SkOpSegment* next = current->findNextWinding(&chase, &nextStart, &nextEnd,
45 &unsortable);
46 if (!next) {
47 break;
48 }
49 #if DEBUG_FLOW
50 SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__,
51 current->debugID(), start->pt().fX, start->pt().fY,
52 end->pt().fX, end->pt().fY);
53 #endif
54 if (!current->addCurveTo(start, end, writer)) {
55 return false;
56 }
57 current = next;
58 start = nextStart;
59 end = nextEnd;
60 } while (!writer->isClosed() && (!unsortable || !start->starter(end)->done()));
61 if (current->activeWinding(start, end) && !writer->isClosed()) {
62 SkOpSpan* spanStart = start->starter(end);
63 if (!spanStart->done()) {
64 if (!current->addCurveTo(start, end, writer)) {
65 return false;
66 }
67 current->markDone(spanStart);
68 }
69 }
70 writer->finishContour();
71 } else {
72 SkOpSpanBase* last;
73 if (!current->markAndChaseDone(start, end, &last)) {
74 return false;
75 }
76 if (last && !last->chased()) {
77 last->setChased(true);
78 SkASSERT(!SkPathOpsDebug::ChaseContains(chase, last));
79 *chase.append() = last;
80 #if DEBUG_WINDING
81 SkDebugf("%s chase.append id=%d", __FUNCTION__, last->segment()->debugID());
82 if (!last->final()) {
83 SkDebugf(" windSum=%d", last->upCast()->windSum());
84 }
85 SkDebugf("\n");
86 #endif
87 }
88 }
89 current = FindChase(&chase, &start, &end);
90 SkPathOpsDebug::ShowActiveSpans(contourList);
91 if (!current) {
92 break;
93 }
94 } while (true);
95 } while (true);
96 return true;
97 }
98
99 // returns true if all edges were processed
100 static bool bridgeXor(SkOpContourHead* contourList, SkPathWriter* writer) {
101 bool unsortable = false;
102 int safetyNet = 1000000;
103 do {
104 SkOpSpan* span = FindUndone(contourList);
105 if (!span) {
106 break;
107 }
108 SkOpSegment* current = span->segment();
109 SkOpSpanBase* start = span->next();
110 SkOpSpanBase* end = span;
111 do {
112 if (--safetyNet < 0) {
113 return false;
114 }
115 if (!unsortable && current->done()) {
116 break;
117 }
118 SkASSERT(unsortable || !current->done());
119 SkOpSpanBase* nextStart = start;
120 SkOpSpanBase* nextEnd = end;
121 SkOpSegment* next = current->findNextXor(&nextStart, &nextEnd,
122 &unsortable);
123 if (!next) {
124 break;
125 }
126 #if DEBUG_FLOW
127 SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__,
128 current->debugID(), start->pt().fX, start->pt().fY,
129 end->pt().fX, end->pt().fY);
130 #endif
131 if (!current->addCurveTo(start, end, writer)) {
132 return false;
133 }
134 current = next;
135 start = nextStart;
136 end = nextEnd;
137 } while (!writer->isClosed() && (!unsortable || !start->starter(end)->done()));
138 if (!writer->isClosed()) {
139 SkOpSpan* spanStart = start->starter(end);
140 if (!spanStart->done()) {
141 return false;
142 }
143 }
144 writer->finishContour();
145 SkPathOpsDebug::ShowActiveSpans(contourList);
146 } while (true);
147 return true;
148 }
149
150 static bool path_is_trivial(const SkPath& path) {
151 SkPath::Iter iter(path, true);
152
153 class Trivializer {
154 SkPoint prevPt{0,0};
155 SkVector prevVec{0,0};
156 public:
157 void moveTo(const SkPoint& currPt) {
158 prevPt = currPt;
159 prevVec = {0, 0};
160 }
161 bool addTrivialContourPoint(const SkPoint& currPt) {
162 if (currPt == prevPt) {
163 return true;
164 }
165 // There are more numericaly stable ways of determining if many points are co-linear.
166 // However, this mirrors SkPath's Convexicator for consistency.
167 SkVector currVec = currPt - prevPt;
168 if (SkPoint::CrossProduct(prevVec, currVec) != 0) {
169 return false;
170 }
171 prevVec = currVec;
172 prevPt = currPt;
173 return true;
174 }
175 } triv;
176
177 while (auto rec = iter.next()) {
178 SkSpan<const SkPoint> points = rec->fPoints;
179 switch (rec->fVerb) {
180 case SkPathVerb::kMove:
181 triv.moveTo(points[0]);
182 break;
183 case SkPathVerb::kCubic:
184 if (!triv.addTrivialContourPoint(points[3])) { return false; }
185 [[fallthrough]];
186 case SkPathVerb::kConic:
187 case SkPathVerb::kQuad:
188 if (!triv.addTrivialContourPoint(points[2])) { return false; }
189 [[fallthrough]];
190 case SkPathVerb::kLine:
191 if (!triv.addTrivialContourPoint(points[1])) { return false; }
192 if (!triv.addTrivialContourPoint(points[0])) { return false; }
193 break;
194 case SkPathVerb::kClose:
195 break;
196 }
197 }
198 return true;
199 }
200
201 std::optional<SkPath> SimplifyDebug(const SkPath& path SkDEBUGPARAMS(bool skipAssert)
202 SkDEBUGPARAMS(const char* testName)) {
203 // returns 1 for evenodd, -1 for winding, regardless of inverse-ness
204 SkPathFillType fillType = path.isInverseFillType() ? SkPathFillType::kInverseEvenOdd
205 : SkPathFillType::kEvenOdd;
206
207 if (path.isConvex()) {
208 SkPath result;
209 // If the path is trivially convex, simplify to empty, else copy
210 if (!path_is_trivial(path)) {
211 result = path;
212 }
213 return result.makeFillType(fillType);
214 }
215 // turn path into list of segments
216 SkSTArenaAlloc<4096> allocator; // FIXME: constant-ize, tune
217 SkOpContour contour;
218 SkOpContourHead* contourList = static_cast<SkOpContourHead*>(&contour);
219 SkOpGlobalState globalState(contourList, &allocator
220 SkDEBUGPARAMS(skipAssert) SkDEBUGPARAMS(testName));
221 SkOpCoincidence coincidence(&globalState);
222 #if DEBUG_DUMP_VERIFY
223 #ifndef SK_DEBUG
224 const char* testName = "release";
225 #endif
226 if (SkPathOpsDebug::gDumpOp) {
227 DumpSimplify(path, testName);
228 }
229 #endif
230 #if DEBUG_SORT
231 SkPathOpsDebug::gSortCount = SkPathOpsDebug::gSortCountDefault;
232 #endif
233 SkOpEdgeBuilder builder(path, contourList, &globalState);
234 if (!builder.finish()) {
235 return {};
236 }
237 #if DEBUG_DUMP_SEGMENTS
238 contour.dumpSegments();
239 #endif
240 if (!SortContourList(&contourList, false, false)) {
241 return SkPath().makeFillType(fillType);
242 }
243 // find all intersections between segments
244 SkOpContour* current = contourList;
245 do {
246 SkOpContour* next = current;
247 while (AddIntersectTs(current, next, &coincidence)
248 && (next = next->next()));
249 } while ((current = current->next()));
250 #if DEBUG_VALIDATE
251 globalState.setPhase(SkOpPhase::kWalking);
252 #endif
253 bool success = HandleCoincidence(contourList, &coincidence);
254 #if DEBUG_COIN
255 globalState.debugAddToGlobalCoinDicts();
256 #endif
257 if (!success) {
258 return {};
259 }
260 #if DEBUG_DUMP_ALIGNMENT
261 contour.dumpSegments("aligned");
262 #endif
263 // construct closed contours
264 SkPathWriter wrapper(fillType);
265 if (builder.xorMask() == kWinding_PathOpsMask ? !bridgeWinding(contourList, &wrapper)
266 : !bridgeXor(contourList, &wrapper)) {
267 return {};
268 }
269 wrapper.assemble(); // if some edges could not be resolved, assemble remaining
270 return wrapper.nativePath();
271 }
272
273 std::optional<SkPath> Simplify(const SkPath& path) {
274 auto result = SimplifyDebug(path SkDEBUGPARAMS(true) SkDEBUGPARAMS(nullptr));
275 #if DEBUG_DUMP_VERIFY
276 if (SkPathOpsDebug::gVerifyOp) {
277 if (result.has_value()) {
278 VerifySimplify(path, *result);
279 } else {
280 ReportSimplifyFail(path);
281 }
282 }
283 #endif
284 return result;
285 }