blob: e58a9e69f268fec30b756886cb4948ec25c28a5b [file] [log] [blame]
caryclark@google.com235f56a2012-09-14 14:19:30 +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 */
skia.committer@gmail.comb0a327e2012-11-21 02:02:25 +00007
caryclark@google.com235f56a2012-09-14 14:19:30 +00008#include "Simplify.h"
9
10namespace Op {
11
caryclark@google.com7ba591e2012-11-20 14:21:54 +000012#define INCLUDED_BY_SHAPE_OPS 1
13
caryclark@google.com235f56a2012-09-14 14:19:30 +000014#include "Simplify.cpp"
15
caryclark@google.com31143cf2012-11-09 22:14:19 +000016// FIXME: this and find chase should be merge together, along with
17// other code that walks winding in angles
18// OPTIMIZATION: Probably, the walked winding should be rolled into the angle structure
19// so it isn't duplicated by walkers like this one
caryclark@google.com7fce0de2012-11-29 14:31:50 +000020static Segment* findChaseOp(SkTDArray<Span*>& chase, int& nextStart, int& nextEnd) {
caryclark@google.com31143cf2012-11-09 22:14:19 +000021 while (chase.count()) {
22 Span* span;
23 chase.pop(&span);
24 const Span& backPtr = span->fOther->span(span->fOtherIndex);
25 Segment* segment = backPtr.fOther;
caryclark@google.com7fce0de2012-11-29 14:31:50 +000026 nextStart = backPtr.fOtherIndex;
caryclark@google.com31143cf2012-11-09 22:14:19 +000027 SkTDArray<Angle> angles;
28 int done = 0;
caryclark@google.com7fce0de2012-11-29 14:31:50 +000029 if (segment->activeAngle(nextStart, done, angles)) {
caryclark@google.com31143cf2012-11-09 22:14:19 +000030 Angle* last = angles.end() - 1;
caryclark@google.com7fce0de2012-11-29 14:31:50 +000031 nextStart = last->start();
32 nextEnd = last->end();
caryclark@google.com31143cf2012-11-09 22:14:19 +000033 #if TRY_ROTATE
34 *chase.insert(0) = span;
35 #else
36 *chase.append() = span;
37 #endif
38 return last->segment();
39 }
40 if (done == angles.count()) {
41 continue;
42 }
43 SkTDArray<Angle*> sorted;
44 bool sortable = Segment::SortAngles(angles, sorted);
caryclark@google.com7fce0de2012-11-29 14:31:50 +000045 int angleCount = sorted.count();
caryclark@google.com31143cf2012-11-09 22:14:19 +000046#if DEBUG_SORT
caryclark@google.com7fce0de2012-11-29 14:31:50 +000047 sorted[0]->segment()->debugShowSort(__FUNCTION__, sorted, 0);
caryclark@google.com31143cf2012-11-09 22:14:19 +000048#endif
49 if (!sortable) {
50 continue;
51 }
52 // find first angle, initialize winding to computed fWindSum
53 int firstIndex = -1;
54 const Angle* angle;
caryclark@google.com31143cf2012-11-09 22:14:19 +000055 do {
56 angle = sorted[++firstIndex];
57 segment = angle->segment();
caryclark@google.com7fce0de2012-11-29 14:31:50 +000058 } while (segment->windSum(angle) == SK_MinS32);
caryclark@google.com31143cf2012-11-09 22:14:19 +000059 #if DEBUG_SORT
caryclark@google.com7fce0de2012-11-29 14:31:50 +000060 segment->debugShowSort(__FUNCTION__, sorted, firstIndex);
caryclark@google.com31143cf2012-11-09 22:14:19 +000061 #endif
caryclark@google.com7fce0de2012-11-29 14:31:50 +000062 int sumMiWinding = segment->updateWindingReverse(angle);
63 int sumSuWinding = segment->updateOppWindingReverse(angle);
64 if (segment->operand()) {
65 SkTSwap<int>(sumMiWinding, sumSuWinding);
66 }
67 int nextIndex = firstIndex + 1;
68 int lastIndex = firstIndex != 0 ? firstIndex : angleCount;
69 Segment* first = NULL;
caryclark@google.com31143cf2012-11-09 22:14:19 +000070 do {
71 SkASSERT(nextIndex != firstIndex);
72 if (nextIndex == angleCount) {
73 nextIndex = 0;
74 }
75 angle = sorted[nextIndex];
76 segment = angle->segment();
caryclark@google.com7fce0de2012-11-29 14:31:50 +000077 int start = angle->start();
78 int end = angle->end();
caryclark@google.comdb0b3e02012-12-21 21:34:36 +000079 int maxWinding, sumWinding, oppMaxWinding, oppSumWinding;
caryclark@google.com7fce0de2012-11-29 14:31:50 +000080 segment->setUpWindings(start, end, sumMiWinding, sumSuWinding,
81 maxWinding, sumWinding, oppMaxWinding, oppSumWinding);
82 if (!segment->done(angle)) {
83 if (!first) {
84 first = segment;
85 nextStart = start;
86 nextEnd = end;
caryclark@google.com31143cf2012-11-09 22:14:19 +000087 }
caryclark@google.com7fce0de2012-11-29 14:31:50 +000088 (void) segment->markAngle(maxWinding, sumWinding, oppMaxWinding,
89 oppSumWinding, true, angle);
caryclark@google.com31143cf2012-11-09 22:14:19 +000090 }
91 } while (++nextIndex != lastIndex);
caryclark@google.com7fce0de2012-11-29 14:31:50 +000092 if (first) {
93 #if TRY_ROTATE
94 *chase.insert(0) = span;
95 #else
96 *chase.append() = span;
97 #endif
98 return first;
99 }
caryclark@google.com31143cf2012-11-09 22:14:19 +0000100 }
101 return NULL;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000102}
103
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000104/*
caryclark@google.com57cff8d2012-11-14 21:14:56 +0000105static bool windingIsActive(int winding, int oppWinding, int spanWinding, int oppSpanWinding,
caryclark@google.com31143cf2012-11-09 22:14:19 +0000106 bool windingIsOp, ShapeOp op) {
107 bool active = windingIsActive(winding, spanWinding);
108 if (!active) {
109 return false;
110 }
caryclark@google.com57cff8d2012-11-14 21:14:56 +0000111 if (oppSpanWinding && windingIsActive(oppWinding, oppSpanWinding)) {
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000112 switch (op) {
113 case kIntersect_Op:
114 case kUnion_Op:
115 return true;
116 case kDifference_Op: {
117 int absSpan = abs(spanWinding);
118 int absOpp = abs(oppSpanWinding);
119 return windingIsOp ? absSpan < absOpp : absSpan > absOpp;
120 }
121 case kXor_Op:
122 return spanWinding != oppSpanWinding;
123 default:
124 SkASSERT(0);
125 }
caryclark@google.com57cff8d2012-11-14 21:14:56 +0000126 }
caryclark@google.com31143cf2012-11-09 22:14:19 +0000127 bool opActive = oppWinding != 0;
128 return gOpLookup[op][opActive][windingIsOp];
129}
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000130*/
caryclark@google.com57cff8d2012-11-14 21:14:56 +0000131
caryclark@google.com31143cf2012-11-09 22:14:19 +0000132static bool bridgeOp(SkTDArray<Contour*>& contourList, const ShapeOp op,
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000133 const int xorMask, const int xorOpMask, PathWrapper& simple) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000134 bool firstContour = true;
caryclark@google.com31143cf2012-11-09 22:14:19 +0000135 bool unsortable = false;
caryclark@google.come7bd5f42012-12-13 19:47:53 +0000136 bool topUnsortable = false;
caryclark@google.comf839c032012-10-26 21:03:50 +0000137 SkPoint topLeft = {SK_ScalarMin, SK_ScalarMin};
caryclark@google.com235f56a2012-09-14 14:19:30 +0000138 do {
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000139 int index, endIndex;
caryclark@google.comdb0b3e02012-12-21 21:34:36 +0000140 bool done;
caryclark@google.com3586ece2012-12-27 18:46:58 +0000141 Segment* current = findSortableTop(contourList, firstContour, index, endIndex, topLeft,
caryclark@google.comdb0b3e02012-12-21 21:34:36 +0000142 topUnsortable, done, true);
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000143 if (!current) {
caryclark@google.comdb0b3e02012-12-21 21:34:36 +0000144 if (topUnsortable || !done) {
caryclark@google.come7bd5f42012-12-13 19:47:53 +0000145 topUnsortable = false;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000146 SkASSERT(topLeft.fX != SK_ScalarMin && topLeft.fY != SK_ScalarMin);
caryclark@google.come7bd5f42012-12-13 19:47:53 +0000147 topLeft.fX = topLeft.fY = SK_ScalarMin;
148 continue;
149 }
caryclark@google.comfb51afb2012-10-19 15:54:16 +0000150 break;
151 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000152 SkTDArray<Span*> chaseArray;
153 do {
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000154 if (current->activeOp(index, endIndex, xorMask, xorOpMask, op)) {
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000155 do {
156 #if DEBUG_ACTIVE_SPANS
157 if (!unsortable && current->done()) {
158 debugShowActiveSpans(contourList);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000159 }
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000160 #endif
161 SkASSERT(unsortable || !current->done());
162 int nextStart = index;
163 int nextEnd = endIndex;
164 Segment* next = current->findNextOp(chaseArray, nextStart, nextEnd,
165 unsortable, op, xorMask, xorOpMask);
166 if (!next) {
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000167 if (!unsortable && simple.hasMove()
168 && current->verb() != SkPath::kLine_Verb
169 && !simple.isClosed()) {
170 current->addCurveTo(index, endIndex, simple, true);
171 SkASSERT(simple.isClosed());
172 }
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000173 break;
174 }
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000175 #if DEBUG_FLOW
176 SkDebugf("%s current id=%d from=(%1.9g,%1.9g) to=(%1.9g,%1.9g)\n", __FUNCTION__,
177 current->debugID(), current->xyAtT(index).fX, current->xyAtT(index).fY,
178 current->xyAtT(endIndex).fX, current->xyAtT(endIndex).fY);
179 #endif
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000180 current->addCurveTo(index, endIndex, simple, true);
181 current = next;
182 index = nextStart;
183 endIndex = nextEnd;
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000184 } while (!simple.isClosed() && ((!unsortable)
185 || !current->done(SkMin32(index, endIndex))));
186 if (current->activeWinding(index, endIndex) && !simple.isClosed()) {
caryclark@google.com31143cf2012-11-09 22:14:19 +0000187 SkASSERT(unsortable);
188 int min = SkMin32(index, endIndex);
189 if (!current->done(min)) {
190 current->addCurveTo(index, endIndex, simple, true);
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000191 current->markDoneBinary(min);
caryclark@google.com31143cf2012-11-09 22:14:19 +0000192 }
caryclark@google.com31143cf2012-11-09 22:14:19 +0000193 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000194 simple.close();
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000195 } else {
196 Span* last = current->markAndChaseDoneBinary(index, endIndex);
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000197 if (last && !last->fLoop) {
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000198 *chaseArray.append() = last;
199 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000200 }
caryclark@google.com31143cf2012-11-09 22:14:19 +0000201 current = findChaseOp(chaseArray, index, endIndex);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000202 #if DEBUG_ACTIVE_SPANS
203 debugShowActiveSpans(contourList);
204 #endif
205 if (!current) {
206 break;
207 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000208 } while (true);
209 } while (true);
caryclark@google.com4aaaaea2013-02-28 16:12:39 +0000210 return simple.someAssemblyRequired();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000211}
212
213} // end of Op namespace
214
215
216void operate(const SkPath& one, const SkPath& two, ShapeOp op, SkPath& result) {
caryclark@google.com1304bb22013-03-13 20:29:41 +0000217#if DEBUG_SORT || DEBUG_SWAP_TOP
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000218 Op::gDebugSortCount = Op::gDebugSortCountDefault;
219#endif
caryclark@google.com235f56a2012-09-14 14:19:30 +0000220 result.reset();
221 result.setFillType(SkPath::kEvenOdd_FillType);
222 // turn path into list of segments
223 SkTArray<Op::Contour> contours;
224 // FIXME: add self-intersecting cubics' T values to segment
225 Op::EdgeBuilder builder(one, contours);
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000226 const int xorMask = builder.xorMask();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000227 builder.addOperand(two);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000228 builder.finish();
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000229 const int xorOpMask = builder.xorMask();
caryclark@google.com235f56a2012-09-14 14:19:30 +0000230 SkTDArray<Op::Contour*> contourList;
caryclark@google.com4eeda372012-12-06 21:47:48 +0000231 makeContourList(contours, contourList, xorMask == kEvenOdd_Mask,
232 xorOpMask == kEvenOdd_Mask);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000233 Op::Contour** currentPtr = contourList.begin();
234 if (!currentPtr) {
235 return;
236 }
237 Op::Contour** listEnd = contourList.end();
238 // find all intersections between segments
239 do {
240 Op::Contour** nextPtr = currentPtr;
241 Op::Contour* current = *currentPtr++;
caryclark@google.comc83c70e2013-02-22 21:50:07 +0000242 if (current->containsCubics()) {
243 addSelfIntersectTs(current);
244 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000245 Op::Contour* next;
246 do {
247 next = *nextPtr++;
248 } while (addIntersectTs(current, next) && nextPtr != listEnd);
249 } while (currentPtr != listEnd);
250 // eat through coincident edges
skia.committer@gmail.comb0a327e2012-11-21 02:02:25 +0000251
caryclark@google.com7ba591e2012-11-20 14:21:54 +0000252 int total = 0;
253 int index;
254 for (index = 0; index < contourList.count(); ++index) {
255 total += contourList[index]->segments().count();
256 }
caryclark@google.com729e1c42012-11-21 21:36:34 +0000257#if DEBUG_SHOW_WINDING
caryclark@google.com7ba591e2012-11-20 14:21:54 +0000258 Op::Contour::debugShowWindingValues(contourList);
259#endif
caryclark@google.com4eeda372012-12-06 21:47:48 +0000260 coincidenceCheck(contourList, total);
caryclark@google.com729e1c42012-11-21 21:36:34 +0000261#if DEBUG_SHOW_WINDING
caryclark@google.com7ba591e2012-11-20 14:21:54 +0000262 Op::Contour::debugShowWindingValues(contourList);
263#endif
caryclark@google.com235f56a2012-09-14 14:19:30 +0000264 fixOtherTIndex(contourList);
caryclark@google.com31143cf2012-11-09 22:14:19 +0000265 sortSegments(contourList);
266#if DEBUG_ACTIVE_SPANS
267 debugShowActiveSpans(contourList);
268#endif
caryclark@google.com235f56a2012-09-14 14:19:30 +0000269 // construct closed contours
caryclark@google.comf839c032012-10-26 21:03:50 +0000270 Op::PathWrapper wrapper(result);
caryclark@google.com7fce0de2012-11-29 14:31:50 +0000271 bridgeOp(contourList, op, xorMask, xorOpMask, wrapper);
caryclark@google.com1304bb22013-03-13 20:29:41 +0000272 { // if some edges could not be resolved, assemble remaining fragments
273 SkPath temp;
274 temp.setFillType(SkPath::kEvenOdd_FillType);
275 Op::PathWrapper assembled(temp);
276 assemble(wrapper, assembled);
277 result = *assembled.nativePath();
278 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000279}