1 | /******************************************************************************* |
2 | * * |
3 | * Author : Angus Johnson * |
4 | * Version : 6.4.2 * |
5 | * Date : 27 February 2017 * |
6 | * Website : http://www.angusj.com * |
7 | * Copyright : Angus Johnson 2010-2017 * |
8 | * * |
9 | * License: * |
10 | * Use, modification & distribution is subject to Boost Software License Ver 1. * |
11 | * http://www.boost.org/LICENSE_1_0.txt * |
12 | * * |
13 | * Attributions: * |
14 | * The code in this library is an extension of Bala Vatti's clipping algorithm: * |
15 | * "A generic solution to polygon clipping" * |
16 | * Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. * |
17 | * http://portal.acm.org/citation.cfm?id=129906 * |
18 | * * |
19 | * Computer graphics and geometric modeling: implementation and algorithms * |
20 | * By Max K. Agoston * |
21 | * Springer; 1 edition (January 4, 2005) * |
22 | * http://books.google.com/books?q=vatti+clipping+agoston * |
23 | * * |
24 | * See also: * |
25 | * "Polygon Offsetting by Computing Winding Numbers" * |
26 | * Paper no. DETC2005-85513 pp. 565-575 * |
27 | * ASME 2005 International Design Engineering Technical Conferences * |
28 | * and Computers and Information in Engineering Conference (IDETC/CIE2005) * |
29 | * September 24-28, 2005 , Long Beach, California, USA * |
30 | * http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf * |
31 | * * |
32 | *******************************************************************************/ |
33 | |
34 | /******************************************************************************* |
35 | * * |
36 | * This is a translation of the Delphi Clipper library and the naming style * |
37 | * used has retained a Delphi flavour. * |
38 | * * |
39 | *******************************************************************************/ |
40 | |
41 | #include "clipper.hpp" |
42 | #include <cmath> |
43 | #include <vector> |
44 | #include <algorithm> |
45 | #include <stdexcept> |
46 | #include <cstring> |
47 | #include <cstdlib> |
48 | #include <ostream> |
49 | #include <functional> |
50 | |
51 | //Explicitly disables exceptions handling for target platform |
52 | //#define CLIPPER_NOEXCEPTION |
53 | |
54 | #define CLIPPER_THROW(exception) std::abort() |
55 | #define CLIPPER_TRY if(true) |
56 | #define CLIPPER_CATCH(exception) if(false) |
57 | |
58 | #if defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND) |
59 | #ifndef CLIPPER_NOEXCEPTION |
60 | #undef CLIPPER_THROW |
61 | #define CLIPPER_THROW(exception) throw exception |
62 | #undef CLIPPER_TRY |
63 | #define CLIPPER_TRY try |
64 | #undef CLIPPER_CATCH |
65 | #define CLIPPER_CATCH(exception) catch(exception) |
66 | #endif |
67 | #endif |
68 | |
69 | //Optionally allows to override exception macros |
70 | #if defined(CLIPPER_THROW_USER) |
71 | #undef CLIPPER_THROW |
72 | #define CLIPPER_THROW CLIPPER_THROW_USER |
73 | #endif |
74 | #if defined(CLIPPER_TRY_USER) |
75 | #undef CLIPPER_TRY |
76 | #define CLIPPER_TRY CLIPPER_TRY_USER |
77 | #endif |
78 | #if defined(CLIPPER_CATCH_USER) |
79 | #undef CLIPPER_CATCH |
80 | #define CLIPPER_CATCH CLIPPER_CATCH_USER |
81 | #endif |
82 | |
83 | namespace ClipperLib { |
84 | |
85 | static double const pi = 3.141592653589793238; |
86 | static double const two_pi = pi *2; |
87 | static double const def_arc_tolerance = 0.25; |
88 | |
89 | enum Direction { dRightToLeft, dLeftToRight }; |
90 | |
91 | static int const Unassigned = -1; //edge not currently 'owning' a solution |
92 | static int const Skip = -2; //edge that would otherwise close a path |
93 | |
94 | #define HORIZONTAL (-1.0E+40) |
95 | #define TOLERANCE (1.0e-20) |
96 | #define NEAR_ZERO(val) (((val) > -TOLERANCE) && ((val) < TOLERANCE)) |
97 | |
98 | struct TEdge { |
99 | IntPoint Bot; |
100 | IntPoint Curr; //current (updated for every new scanbeam) |
101 | IntPoint Top; |
102 | double Dx; |
103 | PolyType PolyTyp; |
104 | EdgeSide Side; //side only refers to current side of solution poly |
105 | int WindDelta; //1 or -1 depending on winding direction |
106 | int WindCnt; |
107 | int WindCnt2; //winding count of the opposite polytype |
108 | int OutIdx; |
109 | TEdge *Next; |
110 | TEdge *Prev; |
111 | TEdge *NextInLML; |
112 | TEdge *NextInAEL; |
113 | TEdge *PrevInAEL; |
114 | TEdge *NextInSEL; |
115 | TEdge *PrevInSEL; |
116 | }; |
117 | |
118 | struct IntersectNode { |
119 | TEdge *Edge1; |
120 | TEdge *Edge2; |
121 | IntPoint Pt; |
122 | }; |
123 | |
124 | struct LocalMinimum { |
125 | cInt Y; |
126 | TEdge *LeftBound; |
127 | TEdge *RightBound; |
128 | }; |
129 | |
130 | struct OutPt; |
131 | |
132 | //OutRec: contains a path in the clipping solution. Edges in the AEL will |
133 | //carry a pointer to an OutRec when they are part of the clipping solution. |
134 | struct OutRec { |
135 | int Idx; |
136 | bool IsHole; |
137 | bool IsOpen; |
138 | OutRec *FirstLeft; //see comments in clipper.pas |
139 | PolyNode *PolyNd; |
140 | OutPt *Pts; |
141 | OutPt *BottomPt; |
142 | }; |
143 | |
144 | struct OutPt { |
145 | int Idx; |
146 | IntPoint Pt; |
147 | OutPt *Next; |
148 | OutPt *Prev; |
149 | }; |
150 | |
151 | struct Join { |
152 | OutPt *OutPt1; |
153 | OutPt *OutPt2; |
154 | IntPoint OffPt; |
155 | }; |
156 | |
157 | struct LocMinSorter |
158 | { |
159 | inline bool operator()(const LocalMinimum& locMin1, const LocalMinimum& locMin2) |
160 | { |
161 | return locMin2.Y < locMin1.Y; |
162 | } |
163 | }; |
164 | |
165 | //------------------------------------------------------------------------------ |
166 | //------------------------------------------------------------------------------ |
167 | |
168 | inline cInt Round(double val) |
169 | { |
170 | if ((val < 0)) return static_cast<cInt>(val - 0.5); |
171 | else return static_cast<cInt>(val + 0.5); |
172 | } |
173 | //------------------------------------------------------------------------------ |
174 | |
175 | inline cInt Abs(cInt val) |
176 | { |
177 | return val < 0 ? -val : val; |
178 | } |
179 | |
180 | //------------------------------------------------------------------------------ |
181 | // PolyTree methods ... |
182 | //------------------------------------------------------------------------------ |
183 | |
184 | void PolyTree::Clear() |
185 | { |
186 | for (PolyNodes::size_type i = 0; i < AllNodes.size(); ++i) |
187 | delete AllNodes[i]; |
188 | AllNodes.resize(0); |
189 | Childs.resize(0); |
190 | } |
191 | //------------------------------------------------------------------------------ |
192 | |
193 | PolyNode* PolyTree::GetFirst() const |
194 | { |
195 | if (!Childs.empty()) |
196 | return Childs[0]; |
197 | else |
198 | return 0; |
199 | } |
200 | //------------------------------------------------------------------------------ |
201 | |
202 | int PolyTree::Total() const |
203 | { |
204 | int result = (int)AllNodes.size(); |
205 | //with negative offsets, ignore the hidden outer polygon ... |
206 | if (result > 0 && Childs[0] != AllNodes[0]) result--; |
207 | return result; |
208 | } |
209 | |
210 | //------------------------------------------------------------------------------ |
211 | // PolyNode methods ... |
212 | //------------------------------------------------------------------------------ |
213 | |
214 | PolyNode::PolyNode(): Parent(0), Index(0), m_IsOpen(false) |
215 | { |
216 | } |
217 | //------------------------------------------------------------------------------ |
218 | |
219 | int PolyNode::ChildCount() const |
220 | { |
221 | return (int)Childs.size(); |
222 | } |
223 | //------------------------------------------------------------------------------ |
224 | |
225 | void PolyNode::AddChild(PolyNode& child) |
226 | { |
227 | unsigned cnt = (unsigned)Childs.size(); |
228 | Childs.push_back(&child); |
229 | child.Parent = this; |
230 | child.Index = cnt; |
231 | } |
232 | //------------------------------------------------------------------------------ |
233 | |
234 | PolyNode* PolyNode::GetNext() const |
235 | { |
236 | if (!Childs.empty()) |
237 | return Childs[0]; |
238 | else |
239 | return GetNextSiblingUp(); |
240 | } |
241 | //------------------------------------------------------------------------------ |
242 | |
243 | PolyNode* PolyNode::GetNextSiblingUp() const |
244 | { |
245 | if (!Parent) //protects against PolyTree.GetNextSiblingUp() |
246 | return 0; |
247 | else if (Index == Parent->Childs.size() - 1) |
248 | return Parent->GetNextSiblingUp(); |
249 | else |
250 | return Parent->Childs[Index + 1]; |
251 | } |
252 | //------------------------------------------------------------------------------ |
253 | |
254 | bool PolyNode::IsHole() const |
255 | { |
256 | bool result = true; |
257 | PolyNode* node = Parent; |
258 | while (node) |
259 | { |
260 | result = !result; |
261 | node = node->Parent; |
262 | } |
263 | return result; |
264 | } |
265 | //------------------------------------------------------------------------------ |
266 | |
267 | bool PolyNode::IsOpen() const |
268 | { |
269 | return m_IsOpen; |
270 | } |
271 | //------------------------------------------------------------------------------ |
272 | |
273 | #ifndef use_int32 |
274 | |
275 | //------------------------------------------------------------------------------ |
276 | // Int128 class (enables safe math on signed 64bit integers) |
277 | // eg Int128 val1((long64)9223372036854775807); //ie 2^63 -1 |
278 | // Int128 val2((long64)9223372036854775807); |
279 | // Int128 val3 = val1 * val2; |
280 | // val3.AsString => "85070591730234615847396907784232501249" (8.5e+37) |
281 | //------------------------------------------------------------------------------ |
282 | |
283 | class Int128 |
284 | { |
285 | public: |
286 | ulong64 lo; |
287 | long64 hi; |
288 | |
289 | Int128(long64 _lo = 0) |
290 | { |
291 | lo = (ulong64)_lo; |
292 | if (_lo < 0) hi = -1; else hi = 0; |
293 | } |
294 | |
295 | |
296 | Int128(const Int128 &val): lo(val.lo), hi(val.hi){} |
297 | |
298 | Int128(const long64& _hi, const ulong64& _lo): lo(_lo), hi(_hi){} |
299 | |
300 | Int128& operator = (const long64 &val) |
301 | { |
302 | lo = (ulong64)val; |
303 | if (val < 0) hi = -1; else hi = 0; |
304 | return *this; |
305 | } |
306 | |
307 | bool operator == (const Int128 &val) const |
308 | {return (hi == val.hi && lo == val.lo);} |
309 | |
310 | bool operator != (const Int128 &val) const |
311 | { return !(*this == val);} |
312 | |
313 | bool operator > (const Int128 &val) const |
314 | { |
315 | if (hi != val.hi) |
316 | return hi > val.hi; |
317 | else |
318 | return lo > val.lo; |
319 | } |
320 | |
321 | bool operator < (const Int128 &val) const |
322 | { |
323 | if (hi != val.hi) |
324 | return hi < val.hi; |
325 | else |
326 | return lo < val.lo; |
327 | } |
328 | |
329 | bool operator >= (const Int128 &val) const |
330 | { return !(*this < val);} |
331 | |
332 | bool operator <= (const Int128 &val) const |
333 | { return !(*this > val);} |
334 | |
335 | Int128& operator += (const Int128 &rhs) |
336 | { |
337 | hi += rhs.hi; |
338 | lo += rhs.lo; |
339 | if (lo < rhs.lo) hi++; |
340 | return *this; |
341 | } |
342 | |
343 | Int128 operator + (const Int128 &rhs) const |
344 | { |
345 | Int128 result(*this); |
346 | result+= rhs; |
347 | return result; |
348 | } |
349 | |
350 | Int128& operator -= (const Int128 &rhs) |
351 | { |
352 | *this += -rhs; |
353 | return *this; |
354 | } |
355 | |
356 | Int128 operator - (const Int128 &rhs) const |
357 | { |
358 | Int128 result(*this); |
359 | result -= rhs; |
360 | return result; |
361 | } |
362 | |
363 | Int128 operator-() const //unary negation |
364 | { |
365 | if (lo == 0) |
366 | return Int128(-hi, 0); |
367 | else |
368 | return Int128(~hi, ~lo + 1); |
369 | } |
370 | |
371 | operator double() const |
372 | { |
373 | const double shift64 = 18446744073709551616.0; //2^64 |
374 | if (hi < 0) |
375 | { |
376 | if (lo == 0) return (double)hi * shift64; |
377 | else return -(double)(~lo + ~hi * shift64); |
378 | } |
379 | else |
380 | return (double)(lo + hi * shift64); |
381 | } |
382 | |
383 | }; |
384 | //------------------------------------------------------------------------------ |
385 | |
386 | Int128 Int128Mul (long64 lhs, long64 rhs) |
387 | { |
388 | bool negate = (lhs < 0) != (rhs < 0); |
389 | |
390 | if (lhs < 0) lhs = -lhs; |
391 | ulong64 int1Hi = ulong64(lhs) >> 32; |
392 | ulong64 int1Lo = ulong64(lhs & 0xFFFFFFFF); |
393 | |
394 | if (rhs < 0) rhs = -rhs; |
395 | ulong64 int2Hi = ulong64(rhs) >> 32; |
396 | ulong64 int2Lo = ulong64(rhs & 0xFFFFFFFF); |
397 | |
398 | //nb: see comments in clipper.pas |
399 | ulong64 a = int1Hi * int2Hi; |
400 | ulong64 b = int1Lo * int2Lo; |
401 | ulong64 c = int1Hi * int2Lo + int1Lo * int2Hi; |
402 | |
403 | Int128 tmp; |
404 | tmp.hi = long64(a + (c >> 32)); |
405 | tmp.lo = long64(c << 32); |
406 | tmp.lo += long64(b); |
407 | if (tmp.lo < b) tmp.hi++; |
408 | if (negate) tmp = -tmp; |
409 | return tmp; |
410 | }; |
411 | #endif |
412 | |
413 | //------------------------------------------------------------------------------ |
414 | // Miscellaneous global functions |
415 | //------------------------------------------------------------------------------ |
416 | |
417 | bool Orientation(const Path &poly) |
418 | { |
419 | return Area(poly) >= 0; |
420 | } |
421 | //------------------------------------------------------------------------------ |
422 | |
423 | double Area(const Path &poly) |
424 | { |
425 | int size = (int)poly.size(); |
426 | if (size < 3) return 0; |
427 | |
428 | double a = 0; |
429 | for (int i = 0, j = size -1; i < size; ++i) |
430 | { |
431 | a += ((double)poly[j].X + poly[i].X) * ((double)poly[j].Y - poly[i].Y); |
432 | j = i; |
433 | } |
434 | return -a * 0.5; |
435 | } |
436 | //------------------------------------------------------------------------------ |
437 | |
438 | double Area(const OutPt *op) |
439 | { |
440 | const OutPt *startOp = op; |
441 | if (!op) return 0; |
442 | double a = 0; |
443 | do { |
444 | a += (double)(op->Prev->Pt.X + op->Pt.X) * (double)(op->Prev->Pt.Y - op->Pt.Y); |
445 | op = op->Next; |
446 | } while (op != startOp); |
447 | return a * 0.5; |
448 | } |
449 | //------------------------------------------------------------------------------ |
450 | |
451 | double Area(const OutRec &outRec) |
452 | { |
453 | return Area(outRec.Pts); |
454 | } |
455 | //------------------------------------------------------------------------------ |
456 | |
457 | bool PointIsVertex(const IntPoint &Pt, OutPt *pp) |
458 | { |
459 | OutPt *pp2 = pp; |
460 | do |
461 | { |
462 | if (pp2->Pt == Pt) return true; |
463 | pp2 = pp2->Next; |
464 | } |
465 | while (pp2 != pp); |
466 | return false; |
467 | } |
468 | //------------------------------------------------------------------------------ |
469 | |
470 | //See "The Point in Polygon Problem for Arbitrary Polygons" by Hormann & Agathos |
471 | //http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.88.5498&rep=rep1&type=pdf |
472 | int PointInPolygon(const IntPoint &pt, const Path &path) |
473 | { |
474 | //returns 0 if false, +1 if true, -1 if pt ON polygon boundary |
475 | int result = 0; |
476 | size_t cnt = path.size(); |
477 | if (cnt < 3) return 0; |
478 | IntPoint ip = path[0]; |
479 | for(size_t i = 1; i <= cnt; ++i) |
480 | { |
481 | IntPoint ipNext = (i == cnt ? path[0] : path[i]); |
482 | if (ipNext.Y == pt.Y) |
483 | { |
484 | if ((ipNext.X == pt.X) || (ip.Y == pt.Y && |
485 | ((ipNext.X > pt.X) == (ip.X < pt.X)))) return -1; |
486 | } |
487 | if ((ip.Y < pt.Y) != (ipNext.Y < pt.Y)) |
488 | { |
489 | if (ip.X >= pt.X) |
490 | { |
491 | if (ipNext.X > pt.X) result = 1 - result; |
492 | else |
493 | { |
494 | double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) - |
495 | (double)(ipNext.X - pt.X) * (ip.Y - pt.Y); |
496 | if (!d) return -1; |
497 | if ((d > 0) == (ipNext.Y > ip.Y)) result = 1 - result; |
498 | } |
499 | } else |
500 | { |
501 | if (ipNext.X > pt.X) |
502 | { |
503 | double d = (double)(ip.X - pt.X) * (ipNext.Y - pt.Y) - |
504 | (double)(ipNext.X - pt.X) * (ip.Y - pt.Y); |
505 | if (!d) return -1; |
506 | if ((d > 0) == (ipNext.Y > ip.Y)) result = 1 - result; |
507 | } |
508 | } |
509 | } |
510 | ip = ipNext; |
511 | } |
512 | return result; |
513 | } |
514 | //------------------------------------------------------------------------------ |
515 | |
516 | int PointInPolygon (const IntPoint &pt, OutPt *op) |
517 | { |
518 | //returns 0 if false, +1 if true, -1 if pt ON polygon boundary |
519 | int result = 0; |
520 | OutPt* startOp = op; |
521 | for(;;) |
522 | { |
523 | if (op->Next->Pt.Y == pt.Y) |
524 | { |
525 | if ((op->Next->Pt.X == pt.X) || (op->Pt.Y == pt.Y && |
526 | ((op->Next->Pt.X > pt.X) == (op->Pt.X < pt.X)))) return -1; |
527 | } |
528 | if ((op->Pt.Y < pt.Y) != (op->Next->Pt.Y < pt.Y)) |
529 | { |
530 | if (op->Pt.X >= pt.X) |
531 | { |
532 | if (op->Next->Pt.X > pt.X) result = 1 - result; |
533 | else |
534 | { |
535 | double d = (double)(op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) - |
536 | (double)(op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y); |
537 | if (!d) return -1; |
538 | if ((d > 0) == (op->Next->Pt.Y > op->Pt.Y)) result = 1 - result; |
539 | } |
540 | } else |
541 | { |
542 | if (op->Next->Pt.X > pt.X) |
543 | { |
544 | double d = (double)(op->Pt.X - pt.X) * (op->Next->Pt.Y - pt.Y) - |
545 | (double)(op->Next->Pt.X - pt.X) * (op->Pt.Y - pt.Y); |
546 | if (!d) return -1; |
547 | if ((d > 0) == (op->Next->Pt.Y > op->Pt.Y)) result = 1 - result; |
548 | } |
549 | } |
550 | } |
551 | op = op->Next; |
552 | if (startOp == op) break; |
553 | } |
554 | return result; |
555 | } |
556 | //------------------------------------------------------------------------------ |
557 | |
558 | bool Poly2ContainsPoly1(OutPt *OutPt1, OutPt *OutPt2) |
559 | { |
560 | OutPt* op = OutPt1; |
561 | do |
562 | { |
563 | //nb: PointInPolygon returns 0 if false, +1 if true, -1 if pt on polygon |
564 | int res = PointInPolygon(op->Pt, OutPt2); |
565 | if (res >= 0) return res > 0; |
566 | op = op->Next; |
567 | } |
568 | while (op != OutPt1); |
569 | return true; |
570 | } |
571 | //---------------------------------------------------------------------- |
572 | |
573 | bool SlopesEqual(const TEdge &e1, const TEdge &e2, bool UseFullInt64Range) |
574 | { |
575 | #ifndef use_int32 |
576 | if (UseFullInt64Range) |
577 | return Int128Mul(e1.Top.Y - e1.Bot.Y, e2.Top.X - e2.Bot.X) == |
578 | Int128Mul(e1.Top.X - e1.Bot.X, e2.Top.Y - e2.Bot.Y); |
579 | else |
580 | #endif |
581 | return (e1.Top.Y - e1.Bot.Y) * (e2.Top.X - e2.Bot.X) == |
582 | (e1.Top.X - e1.Bot.X) * (e2.Top.Y - e2.Bot.Y); |
583 | } |
584 | //------------------------------------------------------------------------------ |
585 | |
586 | bool SlopesEqual(const IntPoint pt1, const IntPoint pt2, |
587 | const IntPoint pt3, bool UseFullInt64Range) |
588 | { |
589 | #ifndef use_int32 |
590 | if (UseFullInt64Range) |
591 | return Int128Mul(pt1.Y-pt2.Y, pt2.X-pt3.X) == Int128Mul(pt1.X-pt2.X, pt2.Y-pt3.Y); |
592 | else |
593 | #endif |
594 | return (pt1.Y-pt2.Y)*(pt2.X-pt3.X) == (pt1.X-pt2.X)*(pt2.Y-pt3.Y); |
595 | } |
596 | //------------------------------------------------------------------------------ |
597 | |
598 | bool SlopesEqual(const IntPoint pt1, const IntPoint pt2, |
599 | const IntPoint pt3, const IntPoint pt4, bool UseFullInt64Range) |
600 | { |
601 | #ifndef use_int32 |
602 | if (UseFullInt64Range) |
603 | return Int128Mul(pt1.Y-pt2.Y, pt3.X-pt4.X) == Int128Mul(pt1.X-pt2.X, pt3.Y-pt4.Y); |
604 | else |
605 | #endif |
606 | return (pt1.Y-pt2.Y)*(pt3.X-pt4.X) == (pt1.X-pt2.X)*(pt3.Y-pt4.Y); |
607 | } |
608 | //------------------------------------------------------------------------------ |
609 | |
610 | inline bool IsHorizontal(TEdge &e) |
611 | { |
612 | return e.Dx == HORIZONTAL; |
613 | } |
614 | //------------------------------------------------------------------------------ |
615 | |
616 | inline double GetDx(const IntPoint pt1, const IntPoint pt2) |
617 | { |
618 | return (pt1.Y == pt2.Y) ? |
619 | HORIZONTAL : (double)(pt2.X - pt1.X) / (pt2.Y - pt1.Y); |
620 | } |
621 | //--------------------------------------------------------------------------- |
622 | |
623 | inline void SetDx(TEdge &e) |
624 | { |
625 | cInt dy = (e.Top.Y - e.Bot.Y); |
626 | if (dy == 0) e.Dx = HORIZONTAL; |
627 | else e.Dx = (double)(e.Top.X - e.Bot.X) / dy; |
628 | } |
629 | //--------------------------------------------------------------------------- |
630 | |
631 | inline void SwapSides(TEdge &Edge1, TEdge &Edge2) |
632 | { |
633 | EdgeSide Side = Edge1.Side; |
634 | Edge1.Side = Edge2.Side; |
635 | Edge2.Side = Side; |
636 | } |
637 | //------------------------------------------------------------------------------ |
638 | |
639 | inline void SwapPolyIndexes(TEdge &Edge1, TEdge &Edge2) |
640 | { |
641 | int OutIdx = Edge1.OutIdx; |
642 | Edge1.OutIdx = Edge2.OutIdx; |
643 | Edge2.OutIdx = OutIdx; |
644 | } |
645 | //------------------------------------------------------------------------------ |
646 | |
647 | inline cInt TopX(TEdge &edge, const cInt currentY) |
648 | { |
649 | return ( currentY == edge.Top.Y ) ? |
650 | edge.Top.X : edge.Bot.X + Round(edge.Dx *(currentY - edge.Bot.Y)); |
651 | } |
652 | //------------------------------------------------------------------------------ |
653 | |
654 | void IntersectPoint(TEdge &Edge1, TEdge &Edge2, IntPoint &ip) |
655 | { |
656 | #ifdef use_xyz |
657 | ip.Z = 0; |
658 | #endif |
659 | |
660 | double b1, b2; |
661 | if (Edge1.Dx == Edge2.Dx) |
662 | { |
663 | ip.Y = Edge1.Curr.Y; |
664 | ip.X = TopX(Edge1, ip.Y); |
665 | return; |
666 | } |
667 | else if (Edge1.Dx == 0) |
668 | { |
669 | ip.X = Edge1.Bot.X; |
670 | if (IsHorizontal(Edge2)) |
671 | ip.Y = Edge2.Bot.Y; |
672 | else |
673 | { |
674 | b2 = Edge2.Bot.Y - (Edge2.Bot.X / Edge2.Dx); |
675 | ip.Y = Round(ip.X / Edge2.Dx + b2); |
676 | } |
677 | } |
678 | else if (Edge2.Dx == 0) |
679 | { |
680 | ip.X = Edge2.Bot.X; |
681 | if (IsHorizontal(Edge1)) |
682 | ip.Y = Edge1.Bot.Y; |
683 | else |
684 | { |
685 | b1 = Edge1.Bot.Y - (Edge1.Bot.X / Edge1.Dx); |
686 | ip.Y = Round(ip.X / Edge1.Dx + b1); |
687 | } |
688 | } |
689 | else |
690 | { |
691 | b1 = Edge1.Bot.X - Edge1.Bot.Y * Edge1.Dx; |
692 | b2 = Edge2.Bot.X - Edge2.Bot.Y * Edge2.Dx; |
693 | double q = (b2-b1) / (Edge1.Dx - Edge2.Dx); |
694 | ip.Y = Round(q); |
695 | if (std::fabs(Edge1.Dx) < std::fabs(Edge2.Dx)) |
696 | ip.X = Round(Edge1.Dx * q + b1); |
697 | else |
698 | ip.X = Round(Edge2.Dx * q + b2); |
699 | } |
700 | |
701 | if (ip.Y < Edge1.Top.Y || ip.Y < Edge2.Top.Y) |
702 | { |
703 | if (Edge1.Top.Y > Edge2.Top.Y) |
704 | ip.Y = Edge1.Top.Y; |
705 | else |
706 | ip.Y = Edge2.Top.Y; |
707 | if (std::fabs(Edge1.Dx) < std::fabs(Edge2.Dx)) |
708 | ip.X = TopX(Edge1, ip.Y); |
709 | else |
710 | ip.X = TopX(Edge2, ip.Y); |
711 | } |
712 | //finally, don't allow 'ip' to be BELOW curr.Y (ie bottom of scanbeam) ... |
713 | if (ip.Y > Edge1.Curr.Y) |
714 | { |
715 | ip.Y = Edge1.Curr.Y; |
716 | //use the more vertical edge to derive X ... |
717 | if (std::fabs(Edge1.Dx) > std::fabs(Edge2.Dx)) |
718 | ip.X = TopX(Edge2, ip.Y); else |
719 | ip.X = TopX(Edge1, ip.Y); |
720 | } |
721 | } |
722 | //------------------------------------------------------------------------------ |
723 | |
724 | void ReversePolyPtLinks(OutPt *pp) |
725 | { |
726 | if (!pp) return; |
727 | OutPt *pp1, *pp2; |
728 | pp1 = pp; |
729 | do { |
730 | pp2 = pp1->Next; |
731 | pp1->Next = pp1->Prev; |
732 | pp1->Prev = pp2; |
733 | pp1 = pp2; |
734 | } while( pp1 != pp ); |
735 | } |
736 | //------------------------------------------------------------------------------ |
737 | |
738 | void DisposeOutPts(OutPt*& pp) |
739 | { |
740 | if (pp == 0) return; |
741 | pp->Prev->Next = 0; |
742 | while( pp ) |
743 | { |
744 | OutPt *tmpPp = pp; |
745 | pp = pp->Next; |
746 | delete tmpPp; |
747 | } |
748 | } |
749 | //------------------------------------------------------------------------------ |
750 | |
751 | inline void InitEdge(TEdge* e, TEdge* eNext, TEdge* ePrev, const IntPoint& Pt) |
752 | { |
753 | std::memset(e, 0, sizeof(TEdge)); |
754 | e->Next = eNext; |
755 | e->Prev = ePrev; |
756 | e->Curr = Pt; |
757 | e->OutIdx = Unassigned; |
758 | } |
759 | //------------------------------------------------------------------------------ |
760 | |
761 | void InitEdge2(TEdge& e, PolyType Pt) |
762 | { |
763 | if (e.Curr.Y >= e.Next->Curr.Y) |
764 | { |
765 | e.Bot = e.Curr; |
766 | e.Top = e.Next->Curr; |
767 | } else |
768 | { |
769 | e.Top = e.Curr; |
770 | e.Bot = e.Next->Curr; |
771 | } |
772 | SetDx(e); |
773 | e.PolyTyp = Pt; |
774 | } |
775 | //------------------------------------------------------------------------------ |
776 | |
777 | TEdge* RemoveEdge(TEdge* e) |
778 | { |
779 | //removes e from double_linked_list (but without removing from memory) |
780 | e->Prev->Next = e->Next; |
781 | e->Next->Prev = e->Prev; |
782 | TEdge* result = e->Next; |
783 | e->Prev = 0; //flag as removed (see ClipperBase.Clear) |
784 | return result; |
785 | } |
786 | //------------------------------------------------------------------------------ |
787 | |
788 | inline void ReverseHorizontal(TEdge &e) |
789 | { |
790 | //swap horizontal edges' Top and Bottom x's so they follow the natural |
791 | //progression of the bounds - ie so their xbots will align with the |
792 | //adjoining lower edge. [Helpful in the ProcessHorizontal() method.] |
793 | std::swap(e.Top.X, e.Bot.X); |
794 | #ifdef use_xyz |
795 | std::swap(e.Top.Z, e.Bot.Z); |
796 | #endif |
797 | } |
798 | //------------------------------------------------------------------------------ |
799 | |
800 | void SwapPoints(IntPoint &pt1, IntPoint &pt2) |
801 | { |
802 | IntPoint tmp = pt1; |
803 | pt1 = pt2; |
804 | pt2 = tmp; |
805 | } |
806 | //------------------------------------------------------------------------------ |
807 | |
808 | bool GetOverlapSegment(IntPoint pt1a, IntPoint pt1b, IntPoint pt2a, |
809 | IntPoint pt2b, IntPoint &pt1, IntPoint &pt2) |
810 | { |
811 | //precondition: segments are Collinear. |
812 | if (Abs(pt1a.X - pt1b.X) > Abs(pt1a.Y - pt1b.Y)) |
813 | { |
814 | if (pt1a.X > pt1b.X) SwapPoints(pt1a, pt1b); |
815 | if (pt2a.X > pt2b.X) SwapPoints(pt2a, pt2b); |
816 | if (pt1a.X > pt2a.X) pt1 = pt1a; else pt1 = pt2a; |
817 | if (pt1b.X < pt2b.X) pt2 = pt1b; else pt2 = pt2b; |
818 | return pt1.X < pt2.X; |
819 | } else |
820 | { |
821 | if (pt1a.Y < pt1b.Y) SwapPoints(pt1a, pt1b); |
822 | if (pt2a.Y < pt2b.Y) SwapPoints(pt2a, pt2b); |
823 | if (pt1a.Y < pt2a.Y) pt1 = pt1a; else pt1 = pt2a; |
824 | if (pt1b.Y > pt2b.Y) pt2 = pt1b; else pt2 = pt2b; |
825 | return pt1.Y > pt2.Y; |
826 | } |
827 | } |
828 | //------------------------------------------------------------------------------ |
829 | |
830 | bool FirstIsBottomPt(const OutPt* btmPt1, const OutPt* btmPt2) |
831 | { |
832 | OutPt *p = btmPt1->Prev; |
833 | while ((p->Pt == btmPt1->Pt) && (p != btmPt1)) p = p->Prev; |
834 | double dx1p = std::fabs(GetDx(btmPt1->Pt, p->Pt)); |
835 | p = btmPt1->Next; |
836 | while ((p->Pt == btmPt1->Pt) && (p != btmPt1)) p = p->Next; |
837 | double dx1n = std::fabs(GetDx(btmPt1->Pt, p->Pt)); |
838 | |
839 | p = btmPt2->Prev; |
840 | while ((p->Pt == btmPt2->Pt) && (p != btmPt2)) p = p->Prev; |
841 | double dx2p = std::fabs(GetDx(btmPt2->Pt, p->Pt)); |
842 | p = btmPt2->Next; |
843 | while ((p->Pt == btmPt2->Pt) && (p != btmPt2)) p = p->Next; |
844 | double dx2n = std::fabs(GetDx(btmPt2->Pt, p->Pt)); |
845 | |
846 | if (std::max(dx1p, dx1n) == std::max(dx2p, dx2n) && |
847 | std::min(dx1p, dx1n) == std::min(dx2p, dx2n)) |
848 | return Area(btmPt1) > 0; //if otherwise identical use orientation |
849 | else |
850 | return (dx1p >= dx2p && dx1p >= dx2n) || (dx1n >= dx2p && dx1n >= dx2n); |
851 | } |
852 | //------------------------------------------------------------------------------ |
853 | |
854 | OutPt* GetBottomPt(OutPt *pp) |
855 | { |
856 | OutPt* dups = 0; |
857 | OutPt* p = pp->Next; |
858 | while (p != pp) |
859 | { |
860 | if (p->Pt.Y > pp->Pt.Y) |
861 | { |
862 | pp = p; |
863 | dups = 0; |
864 | } |
865 | else if (p->Pt.Y == pp->Pt.Y && p->Pt.X <= pp->Pt.X) |
866 | { |
867 | if (p->Pt.X < pp->Pt.X) |
868 | { |
869 | dups = 0; |
870 | pp = p; |
871 | } else |
872 | { |
873 | if (p->Next != pp && p->Prev != pp) dups = p; |
874 | } |
875 | } |
876 | p = p->Next; |
877 | } |
878 | if (dups) |
879 | { |
880 | //there appears to be at least 2 vertices at BottomPt so ... |
881 | while (dups != p) |
882 | { |
883 | if (!FirstIsBottomPt(p, dups)) pp = dups; |
884 | dups = dups->Next; |
885 | while (dups->Pt != pp->Pt) dups = dups->Next; |
886 | } |
887 | } |
888 | return pp; |
889 | } |
890 | //------------------------------------------------------------------------------ |
891 | |
892 | bool Pt2IsBetweenPt1AndPt3(const IntPoint pt1, |
893 | const IntPoint pt2, const IntPoint pt3) |
894 | { |
895 | if ((pt1 == pt3) || (pt1 == pt2) || (pt3 == pt2)) |
896 | return false; |
897 | else if (pt1.X != pt3.X) |
898 | return (pt2.X > pt1.X) == (pt2.X < pt3.X); |
899 | else |
900 | return (pt2.Y > pt1.Y) == (pt2.Y < pt3.Y); |
901 | } |
902 | //------------------------------------------------------------------------------ |
903 | |
904 | bool HorzSegmentsOverlap(cInt seg1a, cInt seg1b, cInt seg2a, cInt seg2b) |
905 | { |
906 | if (seg1a > seg1b) std::swap(seg1a, seg1b); |
907 | if (seg2a > seg2b) std::swap(seg2a, seg2b); |
908 | return (seg1a < seg2b) && (seg2a < seg1b); |
909 | } |
910 | |
911 | //------------------------------------------------------------------------------ |
912 | // ClipperBase class methods ... |
913 | //------------------------------------------------------------------------------ |
914 | |
915 | ClipperBase::ClipperBase() //constructor |
916 | { |
917 | m_CurrentLM = m_MinimaList.begin(); //begin() == end() here |
918 | m_UseFullRange = false; |
919 | } |
920 | //------------------------------------------------------------------------------ |
921 | |
922 | ClipperBase::~ClipperBase() //destructor |
923 | { |
924 | Clear(); |
925 | } |
926 | //------------------------------------------------------------------------------ |
927 | |
928 | void RangeTest(const IntPoint& Pt, bool& useFullRange) |
929 | { |
930 | if (useFullRange) |
931 | { |
932 | if (Pt.X > hiRange || Pt.Y > hiRange || -Pt.X > hiRange || -Pt.Y > hiRange) |
933 | CLIPPER_THROW(clipperException("Coordinate outside allowed range" )); |
934 | } |
935 | else if (Pt.X > loRange|| Pt.Y > loRange || -Pt.X > loRange || -Pt.Y > loRange) |
936 | { |
937 | useFullRange = true; |
938 | RangeTest(Pt, useFullRange); |
939 | } |
940 | } |
941 | //------------------------------------------------------------------------------ |
942 | |
943 | TEdge* FindNextLocMin(TEdge* E) |
944 | { |
945 | for (;;) |
946 | { |
947 | while (E->Bot != E->Prev->Bot || E->Curr == E->Top) E = E->Next; |
948 | if (!IsHorizontal(*E) && !IsHorizontal(*E->Prev)) break; |
949 | while (IsHorizontal(*E->Prev)) E = E->Prev; |
950 | TEdge* E2 = E; |
951 | while (IsHorizontal(*E)) E = E->Next; |
952 | if (E->Top.Y == E->Prev->Bot.Y) continue; //ie just an intermediate horz. |
953 | if (E2->Prev->Bot.X < E->Bot.X) E = E2; |
954 | break; |
955 | } |
956 | return E; |
957 | } |
958 | //------------------------------------------------------------------------------ |
959 | |
960 | TEdge* ClipperBase::ProcessBound(TEdge* E, bool NextIsForward) |
961 | { |
962 | TEdge *Result = E; |
963 | TEdge *Horz = 0; |
964 | |
965 | if (E->OutIdx == Skip) |
966 | { |
967 | //if edges still remain in the current bound beyond the skip edge then |
968 | //create another LocMin and call ProcessBound once more |
969 | if (NextIsForward) |
970 | { |
971 | while (E->Top.Y == E->Next->Bot.Y) E = E->Next; |
972 | //don't include top horizontals when parsing a bound a second time, |
973 | //they will be contained in the opposite bound ... |
974 | while (E != Result && IsHorizontal(*E)) E = E->Prev; |
975 | } |
976 | else |
977 | { |
978 | while (E->Top.Y == E->Prev->Bot.Y) E = E->Prev; |
979 | while (E != Result && IsHorizontal(*E)) E = E->Next; |
980 | } |
981 | |
982 | if (E == Result) |
983 | { |
984 | if (NextIsForward) Result = E->Next; |
985 | else Result = E->Prev; |
986 | } |
987 | else |
988 | { |
989 | //there are more edges in the bound beyond result starting with E |
990 | if (NextIsForward) |
991 | E = Result->Next; |
992 | else |
993 | E = Result->Prev; |
994 | MinimaList::value_type locMin; |
995 | locMin.Y = E->Bot.Y; |
996 | locMin.LeftBound = 0; |
997 | locMin.RightBound = E; |
998 | E->WindDelta = 0; |
999 | Result = ProcessBound(E, NextIsForward); |
1000 | m_MinimaList.push_back(locMin); |
1001 | } |
1002 | return Result; |
1003 | } |
1004 | |
1005 | TEdge *EStart; |
1006 | |
1007 | if (IsHorizontal(*E)) |
1008 | { |
1009 | //We need to be careful with open paths because this may not be a |
1010 | //true local minima (ie E may be following a skip edge). |
1011 | //Also, consecutive horz. edges may start heading left before going right. |
1012 | if (NextIsForward) |
1013 | EStart = E->Prev; |
1014 | else |
1015 | EStart = E->Next; |
1016 | if (IsHorizontal(*EStart)) //ie an adjoining horizontal skip edge |
1017 | { |
1018 | if (EStart->Bot.X != E->Bot.X && EStart->Top.X != E->Bot.X) |
1019 | ReverseHorizontal(*E); |
1020 | } |
1021 | else if (EStart->Bot.X != E->Bot.X) |
1022 | ReverseHorizontal(*E); |
1023 | } |
1024 | |
1025 | EStart = E; |
1026 | if (NextIsForward) |
1027 | { |
1028 | while (Result->Top.Y == Result->Next->Bot.Y && Result->Next->OutIdx != Skip) |
1029 | Result = Result->Next; |
1030 | if (IsHorizontal(*Result) && Result->Next->OutIdx != Skip) |
1031 | { |
1032 | //nb: at the top of a bound, horizontals are added to the bound |
1033 | //only when the preceding edge attaches to the horizontal's left vertex |
1034 | //unless a Skip edge is encountered when that becomes the top divide |
1035 | Horz = Result; |
1036 | while (IsHorizontal(*Horz->Prev)) Horz = Horz->Prev; |
1037 | if (Horz->Prev->Top.X > Result->Next->Top.X) Result = Horz->Prev; |
1038 | } |
1039 | while (E != Result) |
1040 | { |
1041 | E->NextInLML = E->Next; |
1042 | if (IsHorizontal(*E) && E != EStart && |
1043 | E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E); |
1044 | E = E->Next; |
1045 | } |
1046 | if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Prev->Top.X) |
1047 | ReverseHorizontal(*E); |
1048 | Result = Result->Next; //move to the edge just beyond current bound |
1049 | } else |
1050 | { |
1051 | while (Result->Top.Y == Result->Prev->Bot.Y && Result->Prev->OutIdx != Skip) |
1052 | Result = Result->Prev; |
1053 | if (IsHorizontal(*Result) && Result->Prev->OutIdx != Skip) |
1054 | { |
1055 | Horz = Result; |
1056 | while (IsHorizontal(*Horz->Next)) Horz = Horz->Next; |
1057 | if (Horz->Next->Top.X == Result->Prev->Top.X || |
1058 | Horz->Next->Top.X > Result->Prev->Top.X) Result = Horz->Next; |
1059 | } |
1060 | |
1061 | while (E != Result) |
1062 | { |
1063 | E->NextInLML = E->Prev; |
1064 | if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X) |
1065 | ReverseHorizontal(*E); |
1066 | E = E->Prev; |
1067 | } |
1068 | if (IsHorizontal(*E) && E != EStart && E->Bot.X != E->Next->Top.X) |
1069 | ReverseHorizontal(*E); |
1070 | Result = Result->Prev; //move to the edge just beyond current bound |
1071 | } |
1072 | |
1073 | return Result; |
1074 | } |
1075 | //------------------------------------------------------------------------------ |
1076 | |
1077 | bool ClipperBase::AddPath(const Path &pg, PolyType PolyTyp, bool Closed) |
1078 | { |
1079 | #ifdef use_lines |
1080 | if (!Closed && PolyTyp == ptClip) |
1081 | CLIPPER_THROW(clipperException("AddPath: Open paths must be subject." )); |
1082 | #else |
1083 | if (!Closed) |
1084 | CLIPPER_THROW(clipperException("AddPath: Open paths have been disabled." )); |
1085 | #endif |
1086 | |
1087 | int highI = (int)pg.size() -1; |
1088 | if (Closed) while (highI > 0 && (pg[highI] == pg[0])) --highI; |
1089 | while (highI > 0 && (pg[highI] == pg[highI -1])) --highI; |
1090 | if ((Closed && highI < 2) || (!Closed && highI < 1)) return false; |
1091 | |
1092 | //create a new edge array ... |
1093 | TEdge *edges = new TEdge [highI +1]; |
1094 | |
1095 | bool IsFlat = true; |
1096 | //1. Basic (first) edge initialization ... |
1097 | CLIPPER_TRY |
1098 | { |
1099 | edges[1].Curr = pg[1]; |
1100 | RangeTest(pg[0], m_UseFullRange); |
1101 | RangeTest(pg[highI], m_UseFullRange); |
1102 | InitEdge(&edges[0], &edges[1], &edges[highI], pg[0]); |
1103 | InitEdge(&edges[highI], &edges[0], &edges[highI-1], pg[highI]); |
1104 | for (int i = highI - 1; i >= 1; --i) |
1105 | { |
1106 | RangeTest(pg[i], m_UseFullRange); |
1107 | InitEdge(&edges[i], &edges[i+1], &edges[i-1], pg[i]); |
1108 | } |
1109 | } |
1110 | CLIPPER_CATCH(...) |
1111 | { |
1112 | delete [] edges; |
1113 | CLIPPER_THROW(); //range test fails |
1114 | } |
1115 | TEdge *eStart = &edges[0]; |
1116 | |
1117 | //2. Remove duplicate vertices, and (when closed) collinear edges ... |
1118 | TEdge *E = eStart, *eLoopStop = eStart; |
1119 | for (;;) |
1120 | { |
1121 | //nb: allows matching start and end points when not Closed ... |
1122 | if (E->Curr == E->Next->Curr && (Closed || E->Next != eStart)) |
1123 | { |
1124 | if (E == E->Next) break; |
1125 | if (E == eStart) eStart = E->Next; |
1126 | E = RemoveEdge(E); |
1127 | eLoopStop = E; |
1128 | continue; |
1129 | } |
1130 | if (E->Prev == E->Next) |
1131 | break; //only two vertices |
1132 | else if (Closed && |
1133 | SlopesEqual(E->Prev->Curr, E->Curr, E->Next->Curr, m_UseFullRange) && |
1134 | (!m_PreserveCollinear || |
1135 | !Pt2IsBetweenPt1AndPt3(E->Prev->Curr, E->Curr, E->Next->Curr))) |
1136 | { |
1137 | //Collinear edges are allowed for open paths but in closed paths |
1138 | //the default is to merge adjacent collinear edges into a single edge. |
1139 | //However, if the PreserveCollinear property is enabled, only overlapping |
1140 | //collinear edges (ie spikes) will be removed from closed paths. |
1141 | if (E == eStart) eStart = E->Next; |
1142 | E = RemoveEdge(E); |
1143 | E = E->Prev; |
1144 | eLoopStop = E; |
1145 | continue; |
1146 | } |
1147 | E = E->Next; |
1148 | if ((E == eLoopStop) || (!Closed && E->Next == eStart)) break; |
1149 | } |
1150 | |
1151 | if ((!Closed && (E == E->Next)) || (Closed && (E->Prev == E->Next))) |
1152 | { |
1153 | delete [] edges; |
1154 | return false; |
1155 | } |
1156 | |
1157 | if (!Closed) |
1158 | { |
1159 | m_HasOpenPaths = true; |
1160 | eStart->Prev->OutIdx = Skip; |
1161 | } |
1162 | |
1163 | //3. Do second stage of edge initialization ... |
1164 | E = eStart; |
1165 | do |
1166 | { |
1167 | InitEdge2(*E, PolyTyp); |
1168 | E = E->Next; |
1169 | if (IsFlat && E->Curr.Y != eStart->Curr.Y) IsFlat = false; |
1170 | } |
1171 | while (E != eStart); |
1172 | |
1173 | //4. Finally, add edge bounds to LocalMinima list ... |
1174 | |
1175 | //Totally flat paths must be handled differently when adding them |
1176 | //to LocalMinima list to avoid endless loops etc ... |
1177 | if (IsFlat) |
1178 | { |
1179 | if (Closed) |
1180 | { |
1181 | delete [] edges; |
1182 | return false; |
1183 | } |
1184 | E->Prev->OutIdx = Skip; |
1185 | MinimaList::value_type locMin; |
1186 | locMin.Y = E->Bot.Y; |
1187 | locMin.LeftBound = 0; |
1188 | locMin.RightBound = E; |
1189 | locMin.RightBound->Side = esRight; |
1190 | locMin.RightBound->WindDelta = 0; |
1191 | for (;;) |
1192 | { |
1193 | if (E->Bot.X != E->Prev->Top.X) ReverseHorizontal(*E); |
1194 | if (E->Next->OutIdx == Skip) break; |
1195 | E->NextInLML = E->Next; |
1196 | E = E->Next; |
1197 | } |
1198 | m_MinimaList.push_back(locMin); |
1199 | m_edges.push_back(edges); |
1200 | return true; |
1201 | } |
1202 | |
1203 | m_edges.push_back(edges); |
1204 | bool leftBoundIsForward; |
1205 | TEdge* EMin = 0; |
1206 | |
1207 | //workaround to avoid an endless loop in the while loop below when |
1208 | //open paths have matching start and end points ... |
1209 | if (E->Prev->Bot == E->Prev->Top) E = E->Next; |
1210 | |
1211 | for (;;) |
1212 | { |
1213 | E = FindNextLocMin(E); |
1214 | if (E == EMin) break; |
1215 | else if (!EMin) EMin = E; |
1216 | |
1217 | //E and E.Prev now share a local minima (left aligned if horizontal). |
1218 | //Compare their slopes to find which starts which bound ... |
1219 | MinimaList::value_type locMin; |
1220 | locMin.Y = E->Bot.Y; |
1221 | if (E->Dx < E->Prev->Dx) |
1222 | { |
1223 | locMin.LeftBound = E->Prev; |
1224 | locMin.RightBound = E; |
1225 | leftBoundIsForward = false; //Q.nextInLML = Q.prev |
1226 | } else |
1227 | { |
1228 | locMin.LeftBound = E; |
1229 | locMin.RightBound = E->Prev; |
1230 | leftBoundIsForward = true; //Q.nextInLML = Q.next |
1231 | } |
1232 | |
1233 | if (!Closed) locMin.LeftBound->WindDelta = 0; |
1234 | else if (locMin.LeftBound->Next == locMin.RightBound) |
1235 | locMin.LeftBound->WindDelta = -1; |
1236 | else locMin.LeftBound->WindDelta = 1; |
1237 | locMin.RightBound->WindDelta = -locMin.LeftBound->WindDelta; |
1238 | |
1239 | E = ProcessBound(locMin.LeftBound, leftBoundIsForward); |
1240 | if (E->OutIdx == Skip) E = ProcessBound(E, leftBoundIsForward); |
1241 | |
1242 | TEdge* E2 = ProcessBound(locMin.RightBound, !leftBoundIsForward); |
1243 | if (E2->OutIdx == Skip) E2 = ProcessBound(E2, !leftBoundIsForward); |
1244 | |
1245 | if (locMin.LeftBound->OutIdx == Skip) |
1246 | locMin.LeftBound = 0; |
1247 | else if (locMin.RightBound->OutIdx == Skip) |
1248 | locMin.RightBound = 0; |
1249 | m_MinimaList.push_back(locMin); |
1250 | if (!leftBoundIsForward) E = E2; |
1251 | } |
1252 | return true; |
1253 | } |
1254 | //------------------------------------------------------------------------------ |
1255 | |
1256 | bool ClipperBase::AddPaths(const Paths &ppg, PolyType PolyTyp, bool Closed) |
1257 | { |
1258 | bool result = false; |
1259 | for (Paths::size_type i = 0; i < ppg.size(); ++i) |
1260 | if (AddPath(ppg[i], PolyTyp, Closed)) result = true; |
1261 | return result; |
1262 | } |
1263 | //------------------------------------------------------------------------------ |
1264 | |
1265 | void ClipperBase::Clear() |
1266 | { |
1267 | DisposeLocalMinimaList(); |
1268 | for (EdgeList::size_type i = 0; i < m_edges.size(); ++i) |
1269 | { |
1270 | TEdge* edges = m_edges[i]; |
1271 | delete [] edges; |
1272 | } |
1273 | m_edges.clear(); |
1274 | m_UseFullRange = false; |
1275 | m_HasOpenPaths = false; |
1276 | } |
1277 | //------------------------------------------------------------------------------ |
1278 | |
1279 | void ClipperBase::Reset() |
1280 | { |
1281 | m_CurrentLM = m_MinimaList.begin(); |
1282 | if (m_CurrentLM == m_MinimaList.end()) return; //ie nothing to process |
1283 | std::sort(m_MinimaList.begin(), m_MinimaList.end(), LocMinSorter()); |
1284 | |
1285 | m_Scanbeam = ScanbeamList(); //clears/resets priority_queue |
1286 | //reset all edges ... |
1287 | for (MinimaList::iterator lm = m_MinimaList.begin(); lm != m_MinimaList.end(); ++lm) |
1288 | { |
1289 | InsertScanbeam(lm->Y); |
1290 | TEdge* e = lm->LeftBound; |
1291 | if (e) |
1292 | { |
1293 | e->Curr = e->Bot; |
1294 | e->Side = esLeft; |
1295 | e->OutIdx = Unassigned; |
1296 | } |
1297 | |
1298 | e = lm->RightBound; |
1299 | if (e) |
1300 | { |
1301 | e->Curr = e->Bot; |
1302 | e->Side = esRight; |
1303 | e->OutIdx = Unassigned; |
1304 | } |
1305 | } |
1306 | m_ActiveEdges = 0; |
1307 | m_CurrentLM = m_MinimaList.begin(); |
1308 | } |
1309 | //------------------------------------------------------------------------------ |
1310 | |
1311 | void ClipperBase::DisposeLocalMinimaList() |
1312 | { |
1313 | m_MinimaList.clear(); |
1314 | m_CurrentLM = m_MinimaList.begin(); |
1315 | } |
1316 | //------------------------------------------------------------------------------ |
1317 | |
1318 | bool ClipperBase::PopLocalMinima(cInt Y, const LocalMinimum *&locMin) |
1319 | { |
1320 | if (m_CurrentLM == m_MinimaList.end() || (*m_CurrentLM).Y != Y) return false; |
1321 | locMin = &(*m_CurrentLM); |
1322 | ++m_CurrentLM; |
1323 | return true; |
1324 | } |
1325 | //------------------------------------------------------------------------------ |
1326 | |
1327 | IntRect ClipperBase::GetBounds() |
1328 | { |
1329 | IntRect result; |
1330 | MinimaList::iterator lm = m_MinimaList.begin(); |
1331 | if (lm == m_MinimaList.end()) |
1332 | { |
1333 | result.left = result.top = result.right = result.bottom = 0; |
1334 | return result; |
1335 | } |
1336 | result.left = lm->LeftBound->Bot.X; |
1337 | result.top = lm->LeftBound->Bot.Y; |
1338 | result.right = lm->LeftBound->Bot.X; |
1339 | result.bottom = lm->LeftBound->Bot.Y; |
1340 | while (lm != m_MinimaList.end()) |
1341 | { |
1342 | //todo - needs fixing for open paths |
1343 | result.bottom = std::max(result.bottom, lm->LeftBound->Bot.Y); |
1344 | TEdge* e = lm->LeftBound; |
1345 | for (;;) { |
1346 | TEdge* bottomE = e; |
1347 | while (e->NextInLML) |
1348 | { |
1349 | if (e->Bot.X < result.left) result.left = e->Bot.X; |
1350 | if (e->Bot.X > result.right) result.right = e->Bot.X; |
1351 | e = e->NextInLML; |
1352 | } |
1353 | result.left = std::min(result.left, e->Bot.X); |
1354 | result.right = std::max(result.right, e->Bot.X); |
1355 | result.left = std::min(result.left, e->Top.X); |
1356 | result.right = std::max(result.right, e->Top.X); |
1357 | result.top = std::min(result.top, e->Top.Y); |
1358 | if (bottomE == lm->LeftBound) e = lm->RightBound; |
1359 | else break; |
1360 | } |
1361 | ++lm; |
1362 | } |
1363 | return result; |
1364 | } |
1365 | //------------------------------------------------------------------------------ |
1366 | |
1367 | void ClipperBase::InsertScanbeam(const cInt Y) |
1368 | { |
1369 | m_Scanbeam.push(Y); |
1370 | } |
1371 | //------------------------------------------------------------------------------ |
1372 | |
1373 | bool ClipperBase::PopScanbeam(cInt &Y) |
1374 | { |
1375 | if (m_Scanbeam.empty()) return false; |
1376 | Y = m_Scanbeam.top(); |
1377 | m_Scanbeam.pop(); |
1378 | while (!m_Scanbeam.empty() && Y == m_Scanbeam.top()) { m_Scanbeam.pop(); } // Pop duplicates. |
1379 | return true; |
1380 | } |
1381 | //------------------------------------------------------------------------------ |
1382 | |
1383 | void ClipperBase::DisposeAllOutRecs(){ |
1384 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) |
1385 | DisposeOutRec(i); |
1386 | m_PolyOuts.clear(); |
1387 | } |
1388 | //------------------------------------------------------------------------------ |
1389 | |
1390 | void ClipperBase::DisposeOutRec(PolyOutList::size_type index) |
1391 | { |
1392 | OutRec *outRec = m_PolyOuts[index]; |
1393 | if (outRec->Pts) DisposeOutPts(outRec->Pts); |
1394 | delete outRec; |
1395 | m_PolyOuts[index] = 0; |
1396 | } |
1397 | //------------------------------------------------------------------------------ |
1398 | |
1399 | void ClipperBase::DeleteFromAEL(TEdge *e) |
1400 | { |
1401 | TEdge* AelPrev = e->PrevInAEL; |
1402 | TEdge* AelNext = e->NextInAEL; |
1403 | if (!AelPrev && !AelNext && (e != m_ActiveEdges)) return; //already deleted |
1404 | if (AelPrev) AelPrev->NextInAEL = AelNext; |
1405 | else m_ActiveEdges = AelNext; |
1406 | if (AelNext) AelNext->PrevInAEL = AelPrev; |
1407 | e->NextInAEL = 0; |
1408 | e->PrevInAEL = 0; |
1409 | } |
1410 | //------------------------------------------------------------------------------ |
1411 | |
1412 | OutRec* ClipperBase::CreateOutRec() |
1413 | { |
1414 | OutRec* result = new OutRec; |
1415 | result->IsHole = false; |
1416 | result->IsOpen = false; |
1417 | result->FirstLeft = 0; |
1418 | result->Pts = 0; |
1419 | result->BottomPt = 0; |
1420 | result->PolyNd = 0; |
1421 | m_PolyOuts.push_back(result); |
1422 | result->Idx = (int)m_PolyOuts.size() - 1; |
1423 | return result; |
1424 | } |
1425 | //------------------------------------------------------------------------------ |
1426 | |
1427 | void ClipperBase::SwapPositionsInAEL(TEdge *Edge1, TEdge *Edge2) |
1428 | { |
1429 | //check that one or other edge hasn't already been removed from AEL ... |
1430 | if (Edge1->NextInAEL == Edge1->PrevInAEL || |
1431 | Edge2->NextInAEL == Edge2->PrevInAEL) return; |
1432 | |
1433 | if (Edge1->NextInAEL == Edge2) |
1434 | { |
1435 | TEdge* Next = Edge2->NextInAEL; |
1436 | if (Next) Next->PrevInAEL = Edge1; |
1437 | TEdge* Prev = Edge1->PrevInAEL; |
1438 | if (Prev) Prev->NextInAEL = Edge2; |
1439 | Edge2->PrevInAEL = Prev; |
1440 | Edge2->NextInAEL = Edge1; |
1441 | Edge1->PrevInAEL = Edge2; |
1442 | Edge1->NextInAEL = Next; |
1443 | } |
1444 | else if (Edge2->NextInAEL == Edge1) |
1445 | { |
1446 | TEdge* Next = Edge1->NextInAEL; |
1447 | if (Next) Next->PrevInAEL = Edge2; |
1448 | TEdge* Prev = Edge2->PrevInAEL; |
1449 | if (Prev) Prev->NextInAEL = Edge1; |
1450 | Edge1->PrevInAEL = Prev; |
1451 | Edge1->NextInAEL = Edge2; |
1452 | Edge2->PrevInAEL = Edge1; |
1453 | Edge2->NextInAEL = Next; |
1454 | } |
1455 | else |
1456 | { |
1457 | TEdge* Next = Edge1->NextInAEL; |
1458 | TEdge* Prev = Edge1->PrevInAEL; |
1459 | Edge1->NextInAEL = Edge2->NextInAEL; |
1460 | if (Edge1->NextInAEL) Edge1->NextInAEL->PrevInAEL = Edge1; |
1461 | Edge1->PrevInAEL = Edge2->PrevInAEL; |
1462 | if (Edge1->PrevInAEL) Edge1->PrevInAEL->NextInAEL = Edge1; |
1463 | Edge2->NextInAEL = Next; |
1464 | if (Edge2->NextInAEL) Edge2->NextInAEL->PrevInAEL = Edge2; |
1465 | Edge2->PrevInAEL = Prev; |
1466 | if (Edge2->PrevInAEL) Edge2->PrevInAEL->NextInAEL = Edge2; |
1467 | } |
1468 | |
1469 | if (!Edge1->PrevInAEL) m_ActiveEdges = Edge1; |
1470 | else if (!Edge2->PrevInAEL) m_ActiveEdges = Edge2; |
1471 | } |
1472 | //------------------------------------------------------------------------------ |
1473 | |
1474 | void ClipperBase::UpdateEdgeIntoAEL(TEdge *&e) |
1475 | { |
1476 | if (!e->NextInLML) |
1477 | CLIPPER_THROW(clipperException("UpdateEdgeIntoAEL: invalid call" )); |
1478 | |
1479 | e->NextInLML->OutIdx = e->OutIdx; |
1480 | TEdge* AelPrev = e->PrevInAEL; |
1481 | TEdge* AelNext = e->NextInAEL; |
1482 | if (AelPrev) AelPrev->NextInAEL = e->NextInLML; |
1483 | else m_ActiveEdges = e->NextInLML; |
1484 | if (AelNext) AelNext->PrevInAEL = e->NextInLML; |
1485 | e->NextInLML->Side = e->Side; |
1486 | e->NextInLML->WindDelta = e->WindDelta; |
1487 | e->NextInLML->WindCnt = e->WindCnt; |
1488 | e->NextInLML->WindCnt2 = e->WindCnt2; |
1489 | e = e->NextInLML; |
1490 | e->Curr = e->Bot; |
1491 | e->PrevInAEL = AelPrev; |
1492 | e->NextInAEL = AelNext; |
1493 | if (!IsHorizontal(*e)) InsertScanbeam(e->Top.Y); |
1494 | } |
1495 | //------------------------------------------------------------------------------ |
1496 | |
1497 | bool ClipperBase::LocalMinimaPending() |
1498 | { |
1499 | return (m_CurrentLM != m_MinimaList.end()); |
1500 | } |
1501 | |
1502 | //------------------------------------------------------------------------------ |
1503 | // TClipper methods ... |
1504 | //------------------------------------------------------------------------------ |
1505 | |
1506 | Clipper::Clipper(int initOptions) : ClipperBase() //constructor |
1507 | { |
1508 | m_ExecuteLocked = false; |
1509 | m_UseFullRange = false; |
1510 | m_ReverseOutput = ((initOptions & ioReverseSolution) != 0); |
1511 | m_StrictSimple = ((initOptions & ioStrictlySimple) != 0); |
1512 | m_PreserveCollinear = ((initOptions & ioPreserveCollinear) != 0); |
1513 | m_HasOpenPaths = false; |
1514 | #ifdef use_xyz |
1515 | m_ZFill = 0; |
1516 | #endif |
1517 | } |
1518 | //------------------------------------------------------------------------------ |
1519 | |
1520 | #ifdef use_xyz |
1521 | void Clipper::ZFillFunction(ZFillCallback zFillFunc) |
1522 | { |
1523 | m_ZFill = zFillFunc; |
1524 | } |
1525 | //------------------------------------------------------------------------------ |
1526 | #endif |
1527 | |
1528 | bool Clipper::Execute(ClipType clipType, Paths &solution, PolyFillType fillType) |
1529 | { |
1530 | return Execute(clipType, solution, fillType, fillType); |
1531 | } |
1532 | //------------------------------------------------------------------------------ |
1533 | |
1534 | bool Clipper::Execute(ClipType clipType, PolyTree &polytree, PolyFillType fillType) |
1535 | { |
1536 | return Execute(clipType, polytree, fillType, fillType); |
1537 | } |
1538 | //------------------------------------------------------------------------------ |
1539 | |
1540 | bool Clipper::Execute(ClipType clipType, Paths &solution, |
1541 | PolyFillType subjFillType, PolyFillType clipFillType) |
1542 | { |
1543 | if( m_ExecuteLocked ) return false; |
1544 | if (m_HasOpenPaths) |
1545 | CLIPPER_THROW(clipperException("Error: PolyTree struct is needed for open path clipping." )); |
1546 | m_ExecuteLocked = true; |
1547 | solution.resize(0); |
1548 | m_SubjFillType = subjFillType; |
1549 | m_ClipFillType = clipFillType; |
1550 | m_ClipType = clipType; |
1551 | m_UsingPolyTree = false; |
1552 | bool succeeded = ExecuteInternal(); |
1553 | if (succeeded) BuildResult(solution); |
1554 | DisposeAllOutRecs(); |
1555 | m_ExecuteLocked = false; |
1556 | return succeeded; |
1557 | } |
1558 | //------------------------------------------------------------------------------ |
1559 | |
1560 | bool Clipper::Execute(ClipType clipType, PolyTree& polytree, |
1561 | PolyFillType subjFillType, PolyFillType clipFillType) |
1562 | { |
1563 | if( m_ExecuteLocked ) return false; |
1564 | m_ExecuteLocked = true; |
1565 | m_SubjFillType = subjFillType; |
1566 | m_ClipFillType = clipFillType; |
1567 | m_ClipType = clipType; |
1568 | m_UsingPolyTree = true; |
1569 | bool succeeded = ExecuteInternal(); |
1570 | if (succeeded) BuildResult2(polytree); |
1571 | DisposeAllOutRecs(); |
1572 | m_ExecuteLocked = false; |
1573 | return succeeded; |
1574 | } |
1575 | //------------------------------------------------------------------------------ |
1576 | |
1577 | void Clipper::FixHoleLinkage(OutRec &outrec) |
1578 | { |
1579 | //skip OutRecs that (a) contain outermost polygons or |
1580 | //(b) already have the correct owner/child linkage ... |
1581 | if (!outrec.FirstLeft || |
1582 | (outrec.IsHole != outrec.FirstLeft->IsHole && |
1583 | outrec.FirstLeft->Pts)) return; |
1584 | |
1585 | OutRec* orfl = outrec.FirstLeft; |
1586 | while (orfl && ((orfl->IsHole == outrec.IsHole) || !orfl->Pts)) |
1587 | orfl = orfl->FirstLeft; |
1588 | outrec.FirstLeft = orfl; |
1589 | } |
1590 | //------------------------------------------------------------------------------ |
1591 | |
1592 | bool Clipper::ExecuteInternal() |
1593 | { |
1594 | bool succeeded = true; |
1595 | CLIPPER_TRY { |
1596 | Reset(); |
1597 | m_Maxima = MaximaList(); |
1598 | m_SortedEdges = 0; |
1599 | |
1600 | succeeded = true; |
1601 | cInt botY, topY; |
1602 | if (!PopScanbeam(botY)) return false; |
1603 | InsertLocalMinimaIntoAEL(botY); |
1604 | while (PopScanbeam(topY) || LocalMinimaPending()) |
1605 | { |
1606 | ProcessHorizontals(); |
1607 | ClearGhostJoins(); |
1608 | if (!ProcessIntersections(topY)) |
1609 | { |
1610 | succeeded = false; |
1611 | break; |
1612 | } |
1613 | ProcessEdgesAtTopOfScanbeam(topY); |
1614 | botY = topY; |
1615 | InsertLocalMinimaIntoAEL(botY); |
1616 | } |
1617 | } |
1618 | CLIPPER_CATCH(...) |
1619 | { |
1620 | succeeded = false; |
1621 | } |
1622 | |
1623 | if (succeeded) |
1624 | { |
1625 | //fix orientations ... |
1626 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) |
1627 | { |
1628 | OutRec *outRec = m_PolyOuts[i]; |
1629 | if (!outRec->Pts || outRec->IsOpen) continue; |
1630 | if ((outRec->IsHole ^ m_ReverseOutput) == (Area(*outRec) > 0)) |
1631 | ReversePolyPtLinks(outRec->Pts); |
1632 | } |
1633 | |
1634 | if (!m_Joins.empty()) JoinCommonEdges(); |
1635 | |
1636 | //unfortunately FixupOutPolygon() must be done after JoinCommonEdges() |
1637 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) |
1638 | { |
1639 | OutRec *outRec = m_PolyOuts[i]; |
1640 | if (!outRec->Pts) continue; |
1641 | if (outRec->IsOpen) |
1642 | FixupOutPolyline(*outRec); |
1643 | else |
1644 | FixupOutPolygon(*outRec); |
1645 | } |
1646 | |
1647 | if (m_StrictSimple) DoSimplePolygons(); |
1648 | } |
1649 | |
1650 | ClearJoins(); |
1651 | ClearGhostJoins(); |
1652 | return succeeded; |
1653 | } |
1654 | //------------------------------------------------------------------------------ |
1655 | |
1656 | void Clipper::SetWindingCount(TEdge &edge) |
1657 | { |
1658 | TEdge *e = edge.PrevInAEL; |
1659 | //find the edge of the same polytype that immediately preceeds 'edge' in AEL |
1660 | while (e && ((e->PolyTyp != edge.PolyTyp) || (e->WindDelta == 0))) e = e->PrevInAEL; |
1661 | if (!e) |
1662 | { |
1663 | if (edge.WindDelta == 0) |
1664 | { |
1665 | PolyFillType pft = (edge.PolyTyp == ptSubject ? m_SubjFillType : m_ClipFillType); |
1666 | edge.WindCnt = (pft == pftNegative ? -1 : 1); |
1667 | } |
1668 | else |
1669 | edge.WindCnt = edge.WindDelta; |
1670 | edge.WindCnt2 = 0; |
1671 | e = m_ActiveEdges; //ie get ready to calc WindCnt2 |
1672 | } |
1673 | else if (edge.WindDelta == 0 && m_ClipType != ctUnion) |
1674 | { |
1675 | edge.WindCnt = 1; |
1676 | edge.WindCnt2 = e->WindCnt2; |
1677 | e = e->NextInAEL; //ie get ready to calc WindCnt2 |
1678 | } |
1679 | else if (IsEvenOddFillType(edge)) |
1680 | { |
1681 | //EvenOdd filling ... |
1682 | if (edge.WindDelta == 0) |
1683 | { |
1684 | //are we inside a subj polygon ... |
1685 | bool Inside = true; |
1686 | TEdge *e2 = e->PrevInAEL; |
1687 | while (e2) |
1688 | { |
1689 | if (e2->PolyTyp == e->PolyTyp && e2->WindDelta != 0) |
1690 | Inside = !Inside; |
1691 | e2 = e2->PrevInAEL; |
1692 | } |
1693 | edge.WindCnt = (Inside ? 0 : 1); |
1694 | } |
1695 | else |
1696 | { |
1697 | edge.WindCnt = edge.WindDelta; |
1698 | } |
1699 | edge.WindCnt2 = e->WindCnt2; |
1700 | e = e->NextInAEL; //ie get ready to calc WindCnt2 |
1701 | } |
1702 | else |
1703 | { |
1704 | //nonZero, Positive or Negative filling ... |
1705 | if (e->WindCnt * e->WindDelta < 0) |
1706 | { |
1707 | //prev edge is 'decreasing' WindCount (WC) toward zero |
1708 | //so we're outside the previous polygon ... |
1709 | if (Abs(e->WindCnt) > 1) |
1710 | { |
1711 | //outside prev poly but still inside another. |
1712 | //when reversing direction of prev poly use the same WC |
1713 | if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt; |
1714 | //otherwise continue to 'decrease' WC ... |
1715 | else edge.WindCnt = e->WindCnt + edge.WindDelta; |
1716 | } |
1717 | else |
1718 | //now outside all polys of same polytype so set own WC ... |
1719 | edge.WindCnt = (edge.WindDelta == 0 ? 1 : edge.WindDelta); |
1720 | } else |
1721 | { |
1722 | //prev edge is 'increasing' WindCount (WC) away from zero |
1723 | //so we're inside the previous polygon ... |
1724 | if (edge.WindDelta == 0) |
1725 | edge.WindCnt = (e->WindCnt < 0 ? e->WindCnt - 1 : e->WindCnt + 1); |
1726 | //if wind direction is reversing prev then use same WC |
1727 | else if (e->WindDelta * edge.WindDelta < 0) edge.WindCnt = e->WindCnt; |
1728 | //otherwise add to WC ... |
1729 | else edge.WindCnt = e->WindCnt + edge.WindDelta; |
1730 | } |
1731 | edge.WindCnt2 = e->WindCnt2; |
1732 | e = e->NextInAEL; //ie get ready to calc WindCnt2 |
1733 | } |
1734 | |
1735 | //update WindCnt2 ... |
1736 | if (IsEvenOddAltFillType(edge)) |
1737 | { |
1738 | //EvenOdd filling ... |
1739 | while (e != &edge) |
1740 | { |
1741 | if (e->WindDelta != 0) |
1742 | edge.WindCnt2 = (edge.WindCnt2 == 0 ? 1 : 0); |
1743 | e = e->NextInAEL; |
1744 | } |
1745 | } else |
1746 | { |
1747 | //nonZero, Positive or Negative filling ... |
1748 | while ( e != &edge ) |
1749 | { |
1750 | edge.WindCnt2 += e->WindDelta; |
1751 | e = e->NextInAEL; |
1752 | } |
1753 | } |
1754 | } |
1755 | //------------------------------------------------------------------------------ |
1756 | |
1757 | bool Clipper::IsEvenOddFillType(const TEdge& edge) const |
1758 | { |
1759 | if (edge.PolyTyp == ptSubject) |
1760 | return m_SubjFillType == pftEvenOdd; else |
1761 | return m_ClipFillType == pftEvenOdd; |
1762 | } |
1763 | //------------------------------------------------------------------------------ |
1764 | |
1765 | bool Clipper::IsEvenOddAltFillType(const TEdge& edge) const |
1766 | { |
1767 | if (edge.PolyTyp == ptSubject) |
1768 | return m_ClipFillType == pftEvenOdd; else |
1769 | return m_SubjFillType == pftEvenOdd; |
1770 | } |
1771 | //------------------------------------------------------------------------------ |
1772 | |
1773 | bool Clipper::IsContributing(const TEdge& edge) const |
1774 | { |
1775 | PolyFillType pft, pft2; |
1776 | if (edge.PolyTyp == ptSubject) |
1777 | { |
1778 | pft = m_SubjFillType; |
1779 | pft2 = m_ClipFillType; |
1780 | } else |
1781 | { |
1782 | pft = m_ClipFillType; |
1783 | pft2 = m_SubjFillType; |
1784 | } |
1785 | |
1786 | switch(pft) |
1787 | { |
1788 | case pftEvenOdd: |
1789 | //return false if a subj line has been flagged as inside a subj polygon |
1790 | if (edge.WindDelta == 0 && edge.WindCnt != 1) return false; |
1791 | break; |
1792 | case pftNonZero: |
1793 | if (Abs(edge.WindCnt) != 1) return false; |
1794 | break; |
1795 | case pftPositive: |
1796 | if (edge.WindCnt != 1) return false; |
1797 | break; |
1798 | default: //pftNegative |
1799 | if (edge.WindCnt != -1) return false; |
1800 | } |
1801 | |
1802 | switch(m_ClipType) |
1803 | { |
1804 | case ctIntersection: |
1805 | switch(pft2) |
1806 | { |
1807 | case pftEvenOdd: |
1808 | case pftNonZero: |
1809 | return (edge.WindCnt2 != 0); |
1810 | case pftPositive: |
1811 | return (edge.WindCnt2 > 0); |
1812 | default: |
1813 | return (edge.WindCnt2 < 0); |
1814 | } |
1815 | break; |
1816 | case ctUnion: |
1817 | switch(pft2) |
1818 | { |
1819 | case pftEvenOdd: |
1820 | case pftNonZero: |
1821 | return (edge.WindCnt2 == 0); |
1822 | case pftPositive: |
1823 | return (edge.WindCnt2 <= 0); |
1824 | default: |
1825 | return (edge.WindCnt2 >= 0); |
1826 | } |
1827 | break; |
1828 | case ctDifference: |
1829 | if (edge.PolyTyp == ptSubject) |
1830 | switch(pft2) |
1831 | { |
1832 | case pftEvenOdd: |
1833 | case pftNonZero: |
1834 | return (edge.WindCnt2 == 0); |
1835 | case pftPositive: |
1836 | return (edge.WindCnt2 <= 0); |
1837 | default: |
1838 | return (edge.WindCnt2 >= 0); |
1839 | } |
1840 | else |
1841 | switch(pft2) |
1842 | { |
1843 | case pftEvenOdd: |
1844 | case pftNonZero: |
1845 | return (edge.WindCnt2 != 0); |
1846 | case pftPositive: |
1847 | return (edge.WindCnt2 > 0); |
1848 | default: |
1849 | return (edge.WindCnt2 < 0); |
1850 | } |
1851 | break; |
1852 | case ctXor: |
1853 | if (edge.WindDelta == 0) //XOr always contributing unless open |
1854 | switch(pft2) |
1855 | { |
1856 | case pftEvenOdd: |
1857 | case pftNonZero: |
1858 | return (edge.WindCnt2 == 0); |
1859 | case pftPositive: |
1860 | return (edge.WindCnt2 <= 0); |
1861 | default: |
1862 | return (edge.WindCnt2 >= 0); |
1863 | } |
1864 | else |
1865 | return true; |
1866 | break; |
1867 | default: |
1868 | return true; |
1869 | } |
1870 | } |
1871 | //------------------------------------------------------------------------------ |
1872 | |
1873 | OutPt* Clipper::AddLocalMinPoly(TEdge *e1, TEdge *e2, const IntPoint &Pt) |
1874 | { |
1875 | OutPt* result; |
1876 | TEdge *e, *prevE; |
1877 | if (IsHorizontal(*e2) || ( e1->Dx > e2->Dx )) |
1878 | { |
1879 | result = AddOutPt(e1, Pt); |
1880 | e2->OutIdx = e1->OutIdx; |
1881 | e1->Side = esLeft; |
1882 | e2->Side = esRight; |
1883 | e = e1; |
1884 | if (e->PrevInAEL == e2) |
1885 | prevE = e2->PrevInAEL; |
1886 | else |
1887 | prevE = e->PrevInAEL; |
1888 | } else |
1889 | { |
1890 | result = AddOutPt(e2, Pt); |
1891 | e1->OutIdx = e2->OutIdx; |
1892 | e1->Side = esRight; |
1893 | e2->Side = esLeft; |
1894 | e = e2; |
1895 | if (e->PrevInAEL == e1) |
1896 | prevE = e1->PrevInAEL; |
1897 | else |
1898 | prevE = e->PrevInAEL; |
1899 | } |
1900 | |
1901 | if (prevE && prevE->OutIdx >= 0 && prevE->Top.Y < Pt.Y && e->Top.Y < Pt.Y) |
1902 | { |
1903 | cInt xPrev = TopX(*prevE, Pt.Y); |
1904 | cInt xE = TopX(*e, Pt.Y); |
1905 | if (xPrev == xE && (e->WindDelta != 0) && (prevE->WindDelta != 0) && |
1906 | SlopesEqual(IntPoint(xPrev, Pt.Y), prevE->Top, IntPoint(xE, Pt.Y), e->Top, m_UseFullRange)) |
1907 | { |
1908 | OutPt* outPt = AddOutPt(prevE, Pt); |
1909 | AddJoin(result, outPt, e->Top); |
1910 | } |
1911 | } |
1912 | return result; |
1913 | } |
1914 | //------------------------------------------------------------------------------ |
1915 | |
1916 | void Clipper::AddLocalMaxPoly(TEdge *e1, TEdge *e2, const IntPoint &Pt) |
1917 | { |
1918 | AddOutPt( e1, Pt ); |
1919 | if (e2->WindDelta == 0) AddOutPt(e2, Pt); |
1920 | if( e1->OutIdx == e2->OutIdx ) |
1921 | { |
1922 | e1->OutIdx = Unassigned; |
1923 | e2->OutIdx = Unassigned; |
1924 | } |
1925 | else if (e1->OutIdx < e2->OutIdx) |
1926 | AppendPolygon(e1, e2); |
1927 | else |
1928 | AppendPolygon(e2, e1); |
1929 | } |
1930 | //------------------------------------------------------------------------------ |
1931 | |
1932 | void Clipper::AddEdgeToSEL(TEdge *edge) |
1933 | { |
1934 | //SEL pointers in PEdge are reused to build a list of horizontal edges. |
1935 | //However, we don't need to worry about order with horizontal edge processing. |
1936 | if( !m_SortedEdges ) |
1937 | { |
1938 | m_SortedEdges = edge; |
1939 | edge->PrevInSEL = 0; |
1940 | edge->NextInSEL = 0; |
1941 | } |
1942 | else |
1943 | { |
1944 | edge->NextInSEL = m_SortedEdges; |
1945 | edge->PrevInSEL = 0; |
1946 | m_SortedEdges->PrevInSEL = edge; |
1947 | m_SortedEdges = edge; |
1948 | } |
1949 | } |
1950 | //------------------------------------------------------------------------------ |
1951 | |
1952 | bool Clipper::PopEdgeFromSEL(TEdge *&edge) |
1953 | { |
1954 | if (!m_SortedEdges) return false; |
1955 | edge = m_SortedEdges; |
1956 | DeleteFromSEL(m_SortedEdges); |
1957 | return true; |
1958 | } |
1959 | //------------------------------------------------------------------------------ |
1960 | |
1961 | void Clipper::CopyAELToSEL() |
1962 | { |
1963 | TEdge* e = m_ActiveEdges; |
1964 | m_SortedEdges = e; |
1965 | while ( e ) |
1966 | { |
1967 | e->PrevInSEL = e->PrevInAEL; |
1968 | e->NextInSEL = e->NextInAEL; |
1969 | e = e->NextInAEL; |
1970 | } |
1971 | } |
1972 | //------------------------------------------------------------------------------ |
1973 | |
1974 | void Clipper::AddJoin(OutPt *op1, OutPt *op2, const IntPoint OffPt) |
1975 | { |
1976 | Join* j = new Join; |
1977 | j->OutPt1 = op1; |
1978 | j->OutPt2 = op2; |
1979 | j->OffPt = OffPt; |
1980 | m_Joins.push_back(j); |
1981 | } |
1982 | //------------------------------------------------------------------------------ |
1983 | |
1984 | void Clipper::ClearJoins() |
1985 | { |
1986 | for (JoinList::size_type i = 0; i < m_Joins.size(); i++) |
1987 | delete m_Joins[i]; |
1988 | m_Joins.resize(0); |
1989 | } |
1990 | //------------------------------------------------------------------------------ |
1991 | |
1992 | void Clipper::ClearGhostJoins() |
1993 | { |
1994 | for (JoinList::size_type i = 0; i < m_GhostJoins.size(); i++) |
1995 | delete m_GhostJoins[i]; |
1996 | m_GhostJoins.resize(0); |
1997 | } |
1998 | //------------------------------------------------------------------------------ |
1999 | |
2000 | void Clipper::AddGhostJoin(OutPt *op, const IntPoint OffPt) |
2001 | { |
2002 | Join* j = new Join; |
2003 | j->OutPt1 = op; |
2004 | j->OutPt2 = 0; |
2005 | j->OffPt = OffPt; |
2006 | m_GhostJoins.push_back(j); |
2007 | } |
2008 | //------------------------------------------------------------------------------ |
2009 | |
2010 | void Clipper::InsertLocalMinimaIntoAEL(const cInt botY) |
2011 | { |
2012 | const LocalMinimum *lm; |
2013 | while (PopLocalMinima(botY, lm)) |
2014 | { |
2015 | TEdge* lb = lm->LeftBound; |
2016 | TEdge* rb = lm->RightBound; |
2017 | |
2018 | OutPt *Op1 = 0; |
2019 | if (!lb) |
2020 | { |
2021 | //nb: don't insert LB into either AEL or SEL |
2022 | InsertEdgeIntoAEL(rb, 0); |
2023 | SetWindingCount(*rb); |
2024 | if (IsContributing(*rb)) |
2025 | Op1 = AddOutPt(rb, rb->Bot); |
2026 | } |
2027 | else if (!rb) |
2028 | { |
2029 | InsertEdgeIntoAEL(lb, 0); |
2030 | SetWindingCount(*lb); |
2031 | if (IsContributing(*lb)) |
2032 | Op1 = AddOutPt(lb, lb->Bot); |
2033 | InsertScanbeam(lb->Top.Y); |
2034 | } |
2035 | else |
2036 | { |
2037 | InsertEdgeIntoAEL(lb, 0); |
2038 | InsertEdgeIntoAEL(rb, lb); |
2039 | SetWindingCount( *lb ); |
2040 | rb->WindCnt = lb->WindCnt; |
2041 | rb->WindCnt2 = lb->WindCnt2; |
2042 | if (IsContributing(*lb)) |
2043 | Op1 = AddLocalMinPoly(lb, rb, lb->Bot); |
2044 | InsertScanbeam(lb->Top.Y); |
2045 | } |
2046 | |
2047 | if (rb) |
2048 | { |
2049 | if (IsHorizontal(*rb)) |
2050 | { |
2051 | AddEdgeToSEL(rb); |
2052 | if (rb->NextInLML) |
2053 | InsertScanbeam(rb->NextInLML->Top.Y); |
2054 | } |
2055 | else InsertScanbeam( rb->Top.Y ); |
2056 | } |
2057 | |
2058 | if (!lb || !rb) continue; |
2059 | |
2060 | //if any output polygons share an edge, they'll need joining later ... |
2061 | if (Op1 && IsHorizontal(*rb) && |
2062 | m_GhostJoins.size() > 0 && (rb->WindDelta != 0)) |
2063 | { |
2064 | for (JoinList::size_type i = 0; i < m_GhostJoins.size(); ++i) |
2065 | { |
2066 | Join* jr = m_GhostJoins[i]; |
2067 | //if the horizontal Rb and a 'ghost' horizontal overlap, then convert |
2068 | //the 'ghost' join to a real join ready for later ... |
2069 | if (HorzSegmentsOverlap(jr->OutPt1->Pt.X, jr->OffPt.X, rb->Bot.X, rb->Top.X)) |
2070 | AddJoin(jr->OutPt1, Op1, jr->OffPt); |
2071 | } |
2072 | } |
2073 | |
2074 | if (lb->OutIdx >= 0 && lb->PrevInAEL && |
2075 | lb->PrevInAEL->Curr.X == lb->Bot.X && |
2076 | lb->PrevInAEL->OutIdx >= 0 && |
2077 | SlopesEqual(lb->PrevInAEL->Bot, lb->PrevInAEL->Top, lb->Curr, lb->Top, m_UseFullRange) && |
2078 | (lb->WindDelta != 0) && (lb->PrevInAEL->WindDelta != 0)) |
2079 | { |
2080 | OutPt *Op2 = AddOutPt(lb->PrevInAEL, lb->Bot); |
2081 | AddJoin(Op1, Op2, lb->Top); |
2082 | } |
2083 | |
2084 | if(lb->NextInAEL != rb) |
2085 | { |
2086 | |
2087 | if (rb->OutIdx >= 0 && rb->PrevInAEL->OutIdx >= 0 && |
2088 | SlopesEqual(rb->PrevInAEL->Curr, rb->PrevInAEL->Top, rb->Curr, rb->Top, m_UseFullRange) && |
2089 | (rb->WindDelta != 0) && (rb->PrevInAEL->WindDelta != 0)) |
2090 | { |
2091 | OutPt *Op2 = AddOutPt(rb->PrevInAEL, rb->Bot); |
2092 | AddJoin(Op1, Op2, rb->Top); |
2093 | } |
2094 | |
2095 | TEdge* e = lb->NextInAEL; |
2096 | if (e) |
2097 | { |
2098 | while( e != rb ) |
2099 | { |
2100 | //nb: For calculating winding counts etc, IntersectEdges() assumes |
2101 | //that param1 will be to the Right of param2 ABOVE the intersection ... |
2102 | IntersectEdges(rb , e , lb->Curr); //order important here |
2103 | e = e->NextInAEL; |
2104 | } |
2105 | } |
2106 | } |
2107 | |
2108 | } |
2109 | } |
2110 | //------------------------------------------------------------------------------ |
2111 | |
2112 | void Clipper::DeleteFromSEL(TEdge *e) |
2113 | { |
2114 | TEdge* SelPrev = e->PrevInSEL; |
2115 | TEdge* SelNext = e->NextInSEL; |
2116 | if( !SelPrev && !SelNext && (e != m_SortedEdges) ) return; //already deleted |
2117 | if( SelPrev ) SelPrev->NextInSEL = SelNext; |
2118 | else m_SortedEdges = SelNext; |
2119 | if( SelNext ) SelNext->PrevInSEL = SelPrev; |
2120 | e->NextInSEL = 0; |
2121 | e->PrevInSEL = 0; |
2122 | } |
2123 | //------------------------------------------------------------------------------ |
2124 | |
2125 | #ifdef use_xyz |
2126 | void Clipper::SetZ(IntPoint& pt, TEdge& e1, TEdge& e2) |
2127 | { |
2128 | if (pt.Z != 0 || !m_ZFill) return; |
2129 | else if (pt == e1.Bot) pt.Z = e1.Bot.Z; |
2130 | else if (pt == e1.Top) pt.Z = e1.Top.Z; |
2131 | else if (pt == e2.Bot) pt.Z = e2.Bot.Z; |
2132 | else if (pt == e2.Top) pt.Z = e2.Top.Z; |
2133 | else (*m_ZFill)(e1.Bot, e1.Top, e2.Bot, e2.Top, pt); |
2134 | } |
2135 | //------------------------------------------------------------------------------ |
2136 | #endif |
2137 | |
2138 | void Clipper::IntersectEdges(TEdge *e1, TEdge *e2, IntPoint &Pt) |
2139 | { |
2140 | bool e1Contributing = ( e1->OutIdx >= 0 ); |
2141 | bool e2Contributing = ( e2->OutIdx >= 0 ); |
2142 | |
2143 | #ifdef use_xyz |
2144 | SetZ(Pt, *e1, *e2); |
2145 | #endif |
2146 | |
2147 | #ifdef use_lines |
2148 | //if either edge is on an OPEN path ... |
2149 | if (e1->WindDelta == 0 || e2->WindDelta == 0) |
2150 | { |
2151 | //ignore subject-subject open path intersections UNLESS they |
2152 | //are both open paths, AND they are both 'contributing maximas' ... |
2153 | if (e1->WindDelta == 0 && e2->WindDelta == 0) return; |
2154 | |
2155 | //if intersecting a subj line with a subj poly ... |
2156 | else if (e1->PolyTyp == e2->PolyTyp && |
2157 | e1->WindDelta != e2->WindDelta && m_ClipType == ctUnion) |
2158 | { |
2159 | if (e1->WindDelta == 0) |
2160 | { |
2161 | if (e2Contributing) |
2162 | { |
2163 | AddOutPt(e1, Pt); |
2164 | if (e1Contributing) e1->OutIdx = Unassigned; |
2165 | } |
2166 | } |
2167 | else |
2168 | { |
2169 | if (e1Contributing) |
2170 | { |
2171 | AddOutPt(e2, Pt); |
2172 | if (e2Contributing) e2->OutIdx = Unassigned; |
2173 | } |
2174 | } |
2175 | } |
2176 | else if (e1->PolyTyp != e2->PolyTyp) |
2177 | { |
2178 | //toggle subj open path OutIdx on/off when Abs(clip.WndCnt) == 1 ... |
2179 | if ((e1->WindDelta == 0) && abs(e2->WindCnt) == 1 && |
2180 | (m_ClipType != ctUnion || e2->WindCnt2 == 0)) |
2181 | { |
2182 | AddOutPt(e1, Pt); |
2183 | if (e1Contributing) e1->OutIdx = Unassigned; |
2184 | } |
2185 | else if ((e2->WindDelta == 0) && (abs(e1->WindCnt) == 1) && |
2186 | (m_ClipType != ctUnion || e1->WindCnt2 == 0)) |
2187 | { |
2188 | AddOutPt(e2, Pt); |
2189 | if (e2Contributing) e2->OutIdx = Unassigned; |
2190 | } |
2191 | } |
2192 | return; |
2193 | } |
2194 | #endif |
2195 | |
2196 | //update winding counts... |
2197 | //assumes that e1 will be to the Right of e2 ABOVE the intersection |
2198 | if ( e1->PolyTyp == e2->PolyTyp ) |
2199 | { |
2200 | if ( IsEvenOddFillType( *e1) ) |
2201 | { |
2202 | int oldE1WindCnt = e1->WindCnt; |
2203 | e1->WindCnt = e2->WindCnt; |
2204 | e2->WindCnt = oldE1WindCnt; |
2205 | } else |
2206 | { |
2207 | if (e1->WindCnt + e2->WindDelta == 0 ) e1->WindCnt = -e1->WindCnt; |
2208 | else e1->WindCnt += e2->WindDelta; |
2209 | if ( e2->WindCnt - e1->WindDelta == 0 ) e2->WindCnt = -e2->WindCnt; |
2210 | else e2->WindCnt -= e1->WindDelta; |
2211 | } |
2212 | } else |
2213 | { |
2214 | if (!IsEvenOddFillType(*e2)) e1->WindCnt2 += e2->WindDelta; |
2215 | else e1->WindCnt2 = ( e1->WindCnt2 == 0 ) ? 1 : 0; |
2216 | if (!IsEvenOddFillType(*e1)) e2->WindCnt2 -= e1->WindDelta; |
2217 | else e2->WindCnt2 = ( e2->WindCnt2 == 0 ) ? 1 : 0; |
2218 | } |
2219 | |
2220 | PolyFillType e1FillType, e2FillType, e1FillType2, e2FillType2; |
2221 | if (e1->PolyTyp == ptSubject) |
2222 | { |
2223 | e1FillType = m_SubjFillType; |
2224 | e1FillType2 = m_ClipFillType; |
2225 | } else |
2226 | { |
2227 | e1FillType = m_ClipFillType; |
2228 | e1FillType2 = m_SubjFillType; |
2229 | } |
2230 | if (e2->PolyTyp == ptSubject) |
2231 | { |
2232 | e2FillType = m_SubjFillType; |
2233 | e2FillType2 = m_ClipFillType; |
2234 | } else |
2235 | { |
2236 | e2FillType = m_ClipFillType; |
2237 | e2FillType2 = m_SubjFillType; |
2238 | } |
2239 | |
2240 | cInt e1Wc, e2Wc; |
2241 | switch (e1FillType) |
2242 | { |
2243 | case pftPositive: e1Wc = e1->WindCnt; break; |
2244 | case pftNegative: e1Wc = -e1->WindCnt; break; |
2245 | default: e1Wc = Abs(e1->WindCnt); |
2246 | } |
2247 | switch(e2FillType) |
2248 | { |
2249 | case pftPositive: e2Wc = e2->WindCnt; break; |
2250 | case pftNegative: e2Wc = -e2->WindCnt; break; |
2251 | default: e2Wc = Abs(e2->WindCnt); |
2252 | } |
2253 | |
2254 | if ( e1Contributing && e2Contributing ) |
2255 | { |
2256 | if ((e1Wc != 0 && e1Wc != 1) || (e2Wc != 0 && e2Wc != 1) || |
2257 | (e1->PolyTyp != e2->PolyTyp && m_ClipType != ctXor) ) |
2258 | { |
2259 | AddLocalMaxPoly(e1, e2, Pt); |
2260 | } |
2261 | else |
2262 | { |
2263 | AddOutPt(e1, Pt); |
2264 | AddOutPt(e2, Pt); |
2265 | SwapSides( *e1 , *e2 ); |
2266 | SwapPolyIndexes( *e1 , *e2 ); |
2267 | } |
2268 | } |
2269 | else if ( e1Contributing ) |
2270 | { |
2271 | if (e2Wc == 0 || e2Wc == 1) |
2272 | { |
2273 | AddOutPt(e1, Pt); |
2274 | SwapSides(*e1, *e2); |
2275 | SwapPolyIndexes(*e1, *e2); |
2276 | } |
2277 | } |
2278 | else if ( e2Contributing ) |
2279 | { |
2280 | if (e1Wc == 0 || e1Wc == 1) |
2281 | { |
2282 | AddOutPt(e2, Pt); |
2283 | SwapSides(*e1, *e2); |
2284 | SwapPolyIndexes(*e1, *e2); |
2285 | } |
2286 | } |
2287 | else if ( (e1Wc == 0 || e1Wc == 1) && (e2Wc == 0 || e2Wc == 1)) |
2288 | { |
2289 | //neither edge is currently contributing ... |
2290 | |
2291 | cInt e1Wc2, e2Wc2; |
2292 | switch (e1FillType2) |
2293 | { |
2294 | case pftPositive: e1Wc2 = e1->WindCnt2; break; |
2295 | case pftNegative : e1Wc2 = -e1->WindCnt2; break; |
2296 | default: e1Wc2 = Abs(e1->WindCnt2); |
2297 | } |
2298 | switch (e2FillType2) |
2299 | { |
2300 | case pftPositive: e2Wc2 = e2->WindCnt2; break; |
2301 | case pftNegative: e2Wc2 = -e2->WindCnt2; break; |
2302 | default: e2Wc2 = Abs(e2->WindCnt2); |
2303 | } |
2304 | |
2305 | if (e1->PolyTyp != e2->PolyTyp) |
2306 | { |
2307 | AddLocalMinPoly(e1, e2, Pt); |
2308 | } |
2309 | else if (e1Wc == 1 && e2Wc == 1) |
2310 | switch( m_ClipType ) { |
2311 | case ctIntersection: |
2312 | if (e1Wc2 > 0 && e2Wc2 > 0) |
2313 | AddLocalMinPoly(e1, e2, Pt); |
2314 | break; |
2315 | case ctUnion: |
2316 | if ( e1Wc2 <= 0 && e2Wc2 <= 0 ) |
2317 | AddLocalMinPoly(e1, e2, Pt); |
2318 | break; |
2319 | case ctDifference: |
2320 | if (((e1->PolyTyp == ptClip) && (e1Wc2 > 0) && (e2Wc2 > 0)) || |
2321 | ((e1->PolyTyp == ptSubject) && (e1Wc2 <= 0) && (e2Wc2 <= 0))) |
2322 | AddLocalMinPoly(e1, e2, Pt); |
2323 | break; |
2324 | case ctXor: |
2325 | AddLocalMinPoly(e1, e2, Pt); |
2326 | } |
2327 | else |
2328 | SwapSides( *e1, *e2 ); |
2329 | } |
2330 | } |
2331 | //------------------------------------------------------------------------------ |
2332 | |
2333 | void Clipper::SetHoleState(TEdge *e, OutRec *outrec) |
2334 | { |
2335 | TEdge *e2 = e->PrevInAEL; |
2336 | TEdge *eTmp = 0; |
2337 | while (e2) |
2338 | { |
2339 | if (e2->OutIdx >= 0 && e2->WindDelta != 0) |
2340 | { |
2341 | if (!eTmp) eTmp = e2; |
2342 | else if (eTmp->OutIdx == e2->OutIdx) eTmp = 0; |
2343 | } |
2344 | e2 = e2->PrevInAEL; |
2345 | } |
2346 | if (!eTmp) |
2347 | { |
2348 | outrec->FirstLeft = 0; |
2349 | outrec->IsHole = false; |
2350 | } |
2351 | else |
2352 | { |
2353 | outrec->FirstLeft = m_PolyOuts[eTmp->OutIdx]; |
2354 | outrec->IsHole = !outrec->FirstLeft->IsHole; |
2355 | } |
2356 | } |
2357 | //------------------------------------------------------------------------------ |
2358 | |
2359 | OutRec* GetLowermostRec(OutRec *outRec1, OutRec *outRec2) |
2360 | { |
2361 | //work out which polygon fragment has the correct hole state ... |
2362 | if (!outRec1->BottomPt) |
2363 | outRec1->BottomPt = GetBottomPt(outRec1->Pts); |
2364 | if (!outRec2->BottomPt) |
2365 | outRec2->BottomPt = GetBottomPt(outRec2->Pts); |
2366 | OutPt *OutPt1 = outRec1->BottomPt; |
2367 | OutPt *OutPt2 = outRec2->BottomPt; |
2368 | if (OutPt1->Pt.Y > OutPt2->Pt.Y) return outRec1; |
2369 | else if (OutPt1->Pt.Y < OutPt2->Pt.Y) return outRec2; |
2370 | else if (OutPt1->Pt.X < OutPt2->Pt.X) return outRec1; |
2371 | else if (OutPt1->Pt.X > OutPt2->Pt.X) return outRec2; |
2372 | else if (OutPt1->Next == OutPt1) return outRec2; |
2373 | else if (OutPt2->Next == OutPt2) return outRec1; |
2374 | else if (FirstIsBottomPt(OutPt1, OutPt2)) return outRec1; |
2375 | else return outRec2; |
2376 | } |
2377 | //------------------------------------------------------------------------------ |
2378 | |
2379 | bool OutRec1RightOfOutRec2(OutRec* outRec1, OutRec* outRec2) |
2380 | { |
2381 | do |
2382 | { |
2383 | outRec1 = outRec1->FirstLeft; |
2384 | if (outRec1 == outRec2) return true; |
2385 | } while (outRec1); |
2386 | return false; |
2387 | } |
2388 | //------------------------------------------------------------------------------ |
2389 | |
2390 | OutRec* Clipper::GetOutRec(int Idx) |
2391 | { |
2392 | OutRec* outrec = m_PolyOuts[Idx]; |
2393 | while (outrec != m_PolyOuts[outrec->Idx]) |
2394 | outrec = m_PolyOuts[outrec->Idx]; |
2395 | return outrec; |
2396 | } |
2397 | //------------------------------------------------------------------------------ |
2398 | |
2399 | void Clipper::AppendPolygon(TEdge *e1, TEdge *e2) |
2400 | { |
2401 | //get the start and ends of both output polygons ... |
2402 | OutRec *outRec1 = m_PolyOuts[e1->OutIdx]; |
2403 | OutRec *outRec2 = m_PolyOuts[e2->OutIdx]; |
2404 | |
2405 | OutRec *holeStateRec; |
2406 | if (OutRec1RightOfOutRec2(outRec1, outRec2)) |
2407 | holeStateRec = outRec2; |
2408 | else if (OutRec1RightOfOutRec2(outRec2, outRec1)) |
2409 | holeStateRec = outRec1; |
2410 | else |
2411 | holeStateRec = GetLowermostRec(outRec1, outRec2); |
2412 | |
2413 | //get the start and ends of both output polygons and |
2414 | //join e2 poly onto e1 poly and delete pointers to e2 ... |
2415 | |
2416 | OutPt* p1_lft = outRec1->Pts; |
2417 | OutPt* p1_rt = p1_lft->Prev; |
2418 | OutPt* p2_lft = outRec2->Pts; |
2419 | OutPt* p2_rt = p2_lft->Prev; |
2420 | |
2421 | //join e2 poly onto e1 poly and delete pointers to e2 ... |
2422 | if( e1->Side == esLeft ) |
2423 | { |
2424 | if( e2->Side == esLeft ) |
2425 | { |
2426 | //z y x a b c |
2427 | ReversePolyPtLinks(p2_lft); |
2428 | p2_lft->Next = p1_lft; |
2429 | p1_lft->Prev = p2_lft; |
2430 | p1_rt->Next = p2_rt; |
2431 | p2_rt->Prev = p1_rt; |
2432 | outRec1->Pts = p2_rt; |
2433 | } else |
2434 | { |
2435 | //x y z a b c |
2436 | p2_rt->Next = p1_lft; |
2437 | p1_lft->Prev = p2_rt; |
2438 | p2_lft->Prev = p1_rt; |
2439 | p1_rt->Next = p2_lft; |
2440 | outRec1->Pts = p2_lft; |
2441 | } |
2442 | } else |
2443 | { |
2444 | if( e2->Side == esRight ) |
2445 | { |
2446 | //a b c z y x |
2447 | ReversePolyPtLinks(p2_lft); |
2448 | p1_rt->Next = p2_rt; |
2449 | p2_rt->Prev = p1_rt; |
2450 | p2_lft->Next = p1_lft; |
2451 | p1_lft->Prev = p2_lft; |
2452 | } else |
2453 | { |
2454 | //a b c x y z |
2455 | p1_rt->Next = p2_lft; |
2456 | p2_lft->Prev = p1_rt; |
2457 | p1_lft->Prev = p2_rt; |
2458 | p2_rt->Next = p1_lft; |
2459 | } |
2460 | } |
2461 | |
2462 | outRec1->BottomPt = 0; |
2463 | if (holeStateRec == outRec2) |
2464 | { |
2465 | if (outRec2->FirstLeft != outRec1) |
2466 | outRec1->FirstLeft = outRec2->FirstLeft; |
2467 | outRec1->IsHole = outRec2->IsHole; |
2468 | } |
2469 | outRec2->Pts = 0; |
2470 | outRec2->BottomPt = 0; |
2471 | outRec2->FirstLeft = outRec1; |
2472 | |
2473 | int OKIdx = e1->OutIdx; |
2474 | int ObsoleteIdx = e2->OutIdx; |
2475 | |
2476 | e1->OutIdx = Unassigned; //nb: safe because we only get here via AddLocalMaxPoly |
2477 | e2->OutIdx = Unassigned; |
2478 | |
2479 | TEdge* e = m_ActiveEdges; |
2480 | while( e ) |
2481 | { |
2482 | if( e->OutIdx == ObsoleteIdx ) |
2483 | { |
2484 | e->OutIdx = OKIdx; |
2485 | e->Side = e1->Side; |
2486 | break; |
2487 | } |
2488 | e = e->NextInAEL; |
2489 | } |
2490 | |
2491 | outRec2->Idx = outRec1->Idx; |
2492 | } |
2493 | //------------------------------------------------------------------------------ |
2494 | |
2495 | OutPt* Clipper::AddOutPt(TEdge *e, const IntPoint &pt) |
2496 | { |
2497 | if( e->OutIdx < 0 ) |
2498 | { |
2499 | OutRec *outRec = CreateOutRec(); |
2500 | outRec->IsOpen = (e->WindDelta == 0); |
2501 | OutPt* newOp = new OutPt; |
2502 | outRec->Pts = newOp; |
2503 | newOp->Idx = outRec->Idx; |
2504 | newOp->Pt = pt; |
2505 | newOp->Next = newOp; |
2506 | newOp->Prev = newOp; |
2507 | if (!outRec->IsOpen) |
2508 | SetHoleState(e, outRec); |
2509 | e->OutIdx = outRec->Idx; |
2510 | return newOp; |
2511 | } else |
2512 | { |
2513 | OutRec *outRec = m_PolyOuts[e->OutIdx]; |
2514 | //OutRec.Pts is the 'Left-most' point & OutRec.Pts.Prev is the 'Right-most' |
2515 | OutPt* op = outRec->Pts; |
2516 | |
2517 | bool ToFront = (e->Side == esLeft); |
2518 | if (ToFront && (pt == op->Pt)) return op; |
2519 | else if (!ToFront && (pt == op->Prev->Pt)) return op->Prev; |
2520 | |
2521 | OutPt* newOp = new OutPt; |
2522 | newOp->Idx = outRec->Idx; |
2523 | newOp->Pt = pt; |
2524 | newOp->Next = op; |
2525 | newOp->Prev = op->Prev; |
2526 | newOp->Prev->Next = newOp; |
2527 | op->Prev = newOp; |
2528 | if (ToFront) outRec->Pts = newOp; |
2529 | return newOp; |
2530 | } |
2531 | } |
2532 | //------------------------------------------------------------------------------ |
2533 | |
2534 | OutPt* Clipper::GetLastOutPt(TEdge *e) |
2535 | { |
2536 | OutRec *outRec = m_PolyOuts[e->OutIdx]; |
2537 | if (e->Side == esLeft) |
2538 | return outRec->Pts; |
2539 | else |
2540 | return outRec->Pts->Prev; |
2541 | } |
2542 | //------------------------------------------------------------------------------ |
2543 | |
2544 | void Clipper::ProcessHorizontals() |
2545 | { |
2546 | TEdge* horzEdge; |
2547 | while (PopEdgeFromSEL(horzEdge)) |
2548 | ProcessHorizontal(horzEdge); |
2549 | } |
2550 | //------------------------------------------------------------------------------ |
2551 | |
2552 | inline bool IsMinima(TEdge *e) |
2553 | { |
2554 | return e && (e->Prev->NextInLML != e) && (e->Next->NextInLML != e); |
2555 | } |
2556 | //------------------------------------------------------------------------------ |
2557 | |
2558 | inline bool IsMaxima(TEdge *e, const cInt Y) |
2559 | { |
2560 | return e && e->Top.Y == Y && !e->NextInLML; |
2561 | } |
2562 | //------------------------------------------------------------------------------ |
2563 | |
2564 | inline bool IsIntermediate(TEdge *e, const cInt Y) |
2565 | { |
2566 | return e->Top.Y == Y && e->NextInLML; |
2567 | } |
2568 | //------------------------------------------------------------------------------ |
2569 | |
2570 | TEdge *GetMaximaPair(TEdge *e) |
2571 | { |
2572 | if ((e->Next->Top == e->Top) && !e->Next->NextInLML) |
2573 | return e->Next; |
2574 | else if ((e->Prev->Top == e->Top) && !e->Prev->NextInLML) |
2575 | return e->Prev; |
2576 | else return 0; |
2577 | } |
2578 | //------------------------------------------------------------------------------ |
2579 | |
2580 | TEdge *GetMaximaPairEx(TEdge *e) |
2581 | { |
2582 | //as GetMaximaPair() but returns 0 if MaxPair isn't in AEL (unless it's horizontal) |
2583 | TEdge* result = GetMaximaPair(e); |
2584 | if (result && (result->OutIdx == Skip || |
2585 | (result->NextInAEL == result->PrevInAEL && !IsHorizontal(*result)))) return 0; |
2586 | return result; |
2587 | } |
2588 | //------------------------------------------------------------------------------ |
2589 | |
2590 | void Clipper::SwapPositionsInSEL(TEdge *Edge1, TEdge *Edge2) |
2591 | { |
2592 | if( !( Edge1->NextInSEL ) && !( Edge1->PrevInSEL ) ) return; |
2593 | if( !( Edge2->NextInSEL ) && !( Edge2->PrevInSEL ) ) return; |
2594 | |
2595 | if( Edge1->NextInSEL == Edge2 ) |
2596 | { |
2597 | TEdge* Next = Edge2->NextInSEL; |
2598 | if( Next ) Next->PrevInSEL = Edge1; |
2599 | TEdge* Prev = Edge1->PrevInSEL; |
2600 | if( Prev ) Prev->NextInSEL = Edge2; |
2601 | Edge2->PrevInSEL = Prev; |
2602 | Edge2->NextInSEL = Edge1; |
2603 | Edge1->PrevInSEL = Edge2; |
2604 | Edge1->NextInSEL = Next; |
2605 | } |
2606 | else if( Edge2->NextInSEL == Edge1 ) |
2607 | { |
2608 | TEdge* Next = Edge1->NextInSEL; |
2609 | if( Next ) Next->PrevInSEL = Edge2; |
2610 | TEdge* Prev = Edge2->PrevInSEL; |
2611 | if( Prev ) Prev->NextInSEL = Edge1; |
2612 | Edge1->PrevInSEL = Prev; |
2613 | Edge1->NextInSEL = Edge2; |
2614 | Edge2->PrevInSEL = Edge1; |
2615 | Edge2->NextInSEL = Next; |
2616 | } |
2617 | else |
2618 | { |
2619 | TEdge* Next = Edge1->NextInSEL; |
2620 | TEdge* Prev = Edge1->PrevInSEL; |
2621 | Edge1->NextInSEL = Edge2->NextInSEL; |
2622 | if( Edge1->NextInSEL ) Edge1->NextInSEL->PrevInSEL = Edge1; |
2623 | Edge1->PrevInSEL = Edge2->PrevInSEL; |
2624 | if( Edge1->PrevInSEL ) Edge1->PrevInSEL->NextInSEL = Edge1; |
2625 | Edge2->NextInSEL = Next; |
2626 | if( Edge2->NextInSEL ) Edge2->NextInSEL->PrevInSEL = Edge2; |
2627 | Edge2->PrevInSEL = Prev; |
2628 | if( Edge2->PrevInSEL ) Edge2->PrevInSEL->NextInSEL = Edge2; |
2629 | } |
2630 | |
2631 | if( !Edge1->PrevInSEL ) m_SortedEdges = Edge1; |
2632 | else if( !Edge2->PrevInSEL ) m_SortedEdges = Edge2; |
2633 | } |
2634 | //------------------------------------------------------------------------------ |
2635 | |
2636 | TEdge* GetNextInAEL(TEdge *e, Direction dir) |
2637 | { |
2638 | return dir == dLeftToRight ? e->NextInAEL : e->PrevInAEL; |
2639 | } |
2640 | //------------------------------------------------------------------------------ |
2641 | |
2642 | void GetHorzDirection(TEdge& HorzEdge, Direction& Dir, cInt& Left, cInt& Right) |
2643 | { |
2644 | if (HorzEdge.Bot.X < HorzEdge.Top.X) |
2645 | { |
2646 | Left = HorzEdge.Bot.X; |
2647 | Right = HorzEdge.Top.X; |
2648 | Dir = dLeftToRight; |
2649 | } else |
2650 | { |
2651 | Left = HorzEdge.Top.X; |
2652 | Right = HorzEdge.Bot.X; |
2653 | Dir = dRightToLeft; |
2654 | } |
2655 | } |
2656 | //------------------------------------------------------------------------ |
2657 | |
2658 | /******************************************************************************* |
2659 | * Notes: Horizontal edges (HEs) at scanline intersections (ie at the Top or * |
2660 | * Bottom of a scanbeam) are processed as if layered. The order in which HEs * |
2661 | * are processed doesn't matter. HEs intersect with other HE Bot.Xs only [#] * |
2662 | * (or they could intersect with Top.Xs only, ie EITHER Bot.Xs OR Top.Xs), * |
2663 | * and with other non-horizontal edges [*]. Once these intersections are * |
2664 | * processed, intermediate HEs then 'promote' the Edge above (NextInLML) into * |
2665 | * the AEL. These 'promoted' edges may in turn intersect [%] with other HEs. * |
2666 | *******************************************************************************/ |
2667 | |
2668 | void Clipper::ProcessHorizontal(TEdge *horzEdge) |
2669 | { |
2670 | Direction dir; |
2671 | cInt horzLeft, horzRight; |
2672 | bool IsOpen = (horzEdge->WindDelta == 0); |
2673 | |
2674 | GetHorzDirection(*horzEdge, dir, horzLeft, horzRight); |
2675 | |
2676 | TEdge* eLastHorz = horzEdge, *eMaxPair = 0; |
2677 | while (eLastHorz->NextInLML && IsHorizontal(*eLastHorz->NextInLML)) |
2678 | eLastHorz = eLastHorz->NextInLML; |
2679 | if (!eLastHorz->NextInLML) |
2680 | eMaxPair = GetMaximaPair(eLastHorz); |
2681 | |
2682 | MaximaList::const_iterator maxIt; |
2683 | MaximaList::const_reverse_iterator maxRit; |
2684 | if (m_Maxima.size() > 0) |
2685 | { |
2686 | //get the first maxima in range (X) ... |
2687 | if (dir == dLeftToRight) |
2688 | { |
2689 | maxIt = m_Maxima.begin(); |
2690 | while (maxIt != m_Maxima.end() && *maxIt <= horzEdge->Bot.X) maxIt++; |
2691 | if (maxIt != m_Maxima.end() && *maxIt >= eLastHorz->Top.X) |
2692 | maxIt = m_Maxima.end(); |
2693 | } |
2694 | else |
2695 | { |
2696 | maxRit = m_Maxima.rbegin(); |
2697 | while (maxRit != m_Maxima.rend() && *maxRit > horzEdge->Bot.X) maxRit++; |
2698 | if (maxRit != m_Maxima.rend() && *maxRit <= eLastHorz->Top.X) |
2699 | maxRit = m_Maxima.rend(); |
2700 | } |
2701 | } |
2702 | |
2703 | OutPt* op1 = 0; |
2704 | |
2705 | for (;;) //loop through consec. horizontal edges |
2706 | { |
2707 | |
2708 | bool IsLastHorz = (horzEdge == eLastHorz); |
2709 | TEdge* e = GetNextInAEL(horzEdge, dir); |
2710 | while(e) |
2711 | { |
2712 | |
2713 | //this code block inserts extra coords into horizontal edges (in output |
2714 | //polygons) whereever maxima touch these horizontal edges. This helps |
2715 | //'simplifying' polygons (ie if the Simplify property is set). |
2716 | if (m_Maxima.size() > 0) |
2717 | { |
2718 | if (dir == dLeftToRight) |
2719 | { |
2720 | while (maxIt != m_Maxima.end() && *maxIt < e->Curr.X) |
2721 | { |
2722 | if (horzEdge->OutIdx >= 0 && !IsOpen) |
2723 | AddOutPt(horzEdge, IntPoint(*maxIt, horzEdge->Bot.Y)); |
2724 | maxIt++; |
2725 | } |
2726 | } |
2727 | else |
2728 | { |
2729 | while (maxRit != m_Maxima.rend() && *maxRit > e->Curr.X) |
2730 | { |
2731 | if (horzEdge->OutIdx >= 0 && !IsOpen) |
2732 | AddOutPt(horzEdge, IntPoint(*maxRit, horzEdge->Bot.Y)); |
2733 | maxRit++; |
2734 | } |
2735 | } |
2736 | }; |
2737 | |
2738 | if ((dir == dLeftToRight && e->Curr.X > horzRight) || |
2739 | (dir == dRightToLeft && e->Curr.X < horzLeft)) break; |
2740 | |
2741 | //Also break if we've got to the end of an intermediate horizontal edge ... |
2742 | //nb: Smaller Dx's are to the right of larger Dx's ABOVE the horizontal. |
2743 | if (e->Curr.X == horzEdge->Top.X && horzEdge->NextInLML && |
2744 | e->Dx < horzEdge->NextInLML->Dx) break; |
2745 | |
2746 | if (horzEdge->OutIdx >= 0 && !IsOpen) //note: may be done multiple times |
2747 | { |
2748 | #ifdef use_xyz |
2749 | if (dir == dLeftToRight) SetZ(e->Curr, *horzEdge, *e); |
2750 | else SetZ(e->Curr, *e, *horzEdge); |
2751 | #endif |
2752 | op1 = AddOutPt(horzEdge, e->Curr); |
2753 | TEdge* eNextHorz = m_SortedEdges; |
2754 | while (eNextHorz) |
2755 | { |
2756 | if (eNextHorz->OutIdx >= 0 && |
2757 | HorzSegmentsOverlap(horzEdge->Bot.X, |
2758 | horzEdge->Top.X, eNextHorz->Bot.X, eNextHorz->Top.X)) |
2759 | { |
2760 | OutPt* op2 = GetLastOutPt(eNextHorz); |
2761 | AddJoin(op2, op1, eNextHorz->Top); |
2762 | } |
2763 | eNextHorz = eNextHorz->NextInSEL; |
2764 | } |
2765 | AddGhostJoin(op1, horzEdge->Bot); |
2766 | } |
2767 | |
2768 | //OK, so far we're still in range of the horizontal Edge but make sure |
2769 | //we're at the last of consec. horizontals when matching with eMaxPair |
2770 | if(e == eMaxPair && IsLastHorz) |
2771 | { |
2772 | if (horzEdge->OutIdx >= 0) |
2773 | AddLocalMaxPoly(horzEdge, eMaxPair, horzEdge->Top); |
2774 | DeleteFromAEL(horzEdge); |
2775 | DeleteFromAEL(eMaxPair); |
2776 | return; |
2777 | } |
2778 | |
2779 | if(dir == dLeftToRight) |
2780 | { |
2781 | IntPoint Pt = IntPoint(e->Curr.X, horzEdge->Curr.Y); |
2782 | IntersectEdges(horzEdge, e, Pt); |
2783 | } |
2784 | else |
2785 | { |
2786 | IntPoint Pt = IntPoint(e->Curr.X, horzEdge->Curr.Y); |
2787 | IntersectEdges( e, horzEdge, Pt); |
2788 | } |
2789 | TEdge* eNext = GetNextInAEL(e, dir); |
2790 | SwapPositionsInAEL( horzEdge, e ); |
2791 | e = eNext; |
2792 | } //end while(e) |
2793 | |
2794 | //Break out of loop if HorzEdge.NextInLML is not also horizontal ... |
2795 | if (!horzEdge->NextInLML || !IsHorizontal(*horzEdge->NextInLML)) break; |
2796 | |
2797 | UpdateEdgeIntoAEL(horzEdge); |
2798 | if (horzEdge->OutIdx >= 0) AddOutPt(horzEdge, horzEdge->Bot); |
2799 | GetHorzDirection(*horzEdge, dir, horzLeft, horzRight); |
2800 | |
2801 | } //end for (;;) |
2802 | |
2803 | if (horzEdge->OutIdx >= 0 && !op1) |
2804 | { |
2805 | op1 = GetLastOutPt(horzEdge); |
2806 | TEdge* eNextHorz = m_SortedEdges; |
2807 | while (eNextHorz) |
2808 | { |
2809 | if (eNextHorz->OutIdx >= 0 && |
2810 | HorzSegmentsOverlap(horzEdge->Bot.X, |
2811 | horzEdge->Top.X, eNextHorz->Bot.X, eNextHorz->Top.X)) |
2812 | { |
2813 | OutPt* op2 = GetLastOutPt(eNextHorz); |
2814 | AddJoin(op2, op1, eNextHorz->Top); |
2815 | } |
2816 | eNextHorz = eNextHorz->NextInSEL; |
2817 | } |
2818 | AddGhostJoin(op1, horzEdge->Top); |
2819 | } |
2820 | |
2821 | if (horzEdge->NextInLML) |
2822 | { |
2823 | if(horzEdge->OutIdx >= 0) |
2824 | { |
2825 | op1 = AddOutPt( horzEdge, horzEdge->Top); |
2826 | UpdateEdgeIntoAEL(horzEdge); |
2827 | if (horzEdge->WindDelta == 0) return; |
2828 | //nb: HorzEdge is no longer horizontal here |
2829 | TEdge* ePrev = horzEdge->PrevInAEL; |
2830 | TEdge* eNext = horzEdge->NextInAEL; |
2831 | if (ePrev && ePrev->Curr.X == horzEdge->Bot.X && |
2832 | ePrev->Curr.Y == horzEdge->Bot.Y && ePrev->WindDelta != 0 && |
2833 | (ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y && |
2834 | SlopesEqual(*horzEdge, *ePrev, m_UseFullRange))) |
2835 | { |
2836 | OutPt* op2 = AddOutPt(ePrev, horzEdge->Bot); |
2837 | AddJoin(op1, op2, horzEdge->Top); |
2838 | } |
2839 | else if (eNext && eNext->Curr.X == horzEdge->Bot.X && |
2840 | eNext->Curr.Y == horzEdge->Bot.Y && eNext->WindDelta != 0 && |
2841 | eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y && |
2842 | SlopesEqual(*horzEdge, *eNext, m_UseFullRange)) |
2843 | { |
2844 | OutPt* op2 = AddOutPt(eNext, horzEdge->Bot); |
2845 | AddJoin(op1, op2, horzEdge->Top); |
2846 | } |
2847 | } |
2848 | else |
2849 | UpdateEdgeIntoAEL(horzEdge); |
2850 | } |
2851 | else |
2852 | { |
2853 | if (horzEdge->OutIdx >= 0) AddOutPt(horzEdge, horzEdge->Top); |
2854 | DeleteFromAEL(horzEdge); |
2855 | } |
2856 | } |
2857 | //------------------------------------------------------------------------------ |
2858 | |
2859 | bool Clipper::ProcessIntersections(const cInt topY) |
2860 | { |
2861 | if( !m_ActiveEdges ) return true; |
2862 | CLIPPER_TRY { |
2863 | BuildIntersectList(topY); |
2864 | size_t IlSize = m_IntersectList.size(); |
2865 | if (IlSize == 0) return true; |
2866 | if (IlSize == 1 || FixupIntersectionOrder()) ProcessIntersectList(); |
2867 | else return false; |
2868 | } |
2869 | CLIPPER_CATCH(...) |
2870 | { |
2871 | m_SortedEdges = 0; |
2872 | DisposeIntersectNodes(); |
2873 | CLIPPER_THROW(clipperException("ProcessIntersections error" )); |
2874 | } |
2875 | m_SortedEdges = 0; |
2876 | return true; |
2877 | } |
2878 | //------------------------------------------------------------------------------ |
2879 | |
2880 | void Clipper::DisposeIntersectNodes() |
2881 | { |
2882 | for (size_t i = 0; i < m_IntersectList.size(); ++i ) |
2883 | delete m_IntersectList[i]; |
2884 | m_IntersectList.clear(); |
2885 | } |
2886 | //------------------------------------------------------------------------------ |
2887 | |
2888 | void Clipper::BuildIntersectList(const cInt topY) |
2889 | { |
2890 | if ( !m_ActiveEdges ) return; |
2891 | |
2892 | //prepare for sorting ... |
2893 | TEdge* e = m_ActiveEdges; |
2894 | m_SortedEdges = e; |
2895 | while( e ) |
2896 | { |
2897 | e->PrevInSEL = e->PrevInAEL; |
2898 | e->NextInSEL = e->NextInAEL; |
2899 | e->Curr.X = TopX( *e, topY ); |
2900 | e = e->NextInAEL; |
2901 | } |
2902 | |
2903 | //bubblesort ... |
2904 | bool isModified; |
2905 | do |
2906 | { |
2907 | isModified = false; |
2908 | e = m_SortedEdges; |
2909 | while( e->NextInSEL ) |
2910 | { |
2911 | TEdge *eNext = e->NextInSEL; |
2912 | IntPoint Pt; |
2913 | if(e->Curr.X > eNext->Curr.X) |
2914 | { |
2915 | IntersectPoint(*e, *eNext, Pt); |
2916 | if (Pt.Y < topY) Pt = IntPoint(TopX(*e, topY), topY); |
2917 | IntersectNode * newNode = new IntersectNode; |
2918 | newNode->Edge1 = e; |
2919 | newNode->Edge2 = eNext; |
2920 | newNode->Pt = Pt; |
2921 | m_IntersectList.push_back(newNode); |
2922 | |
2923 | SwapPositionsInSEL(e, eNext); |
2924 | isModified = true; |
2925 | } |
2926 | else |
2927 | e = eNext; |
2928 | } |
2929 | if( e->PrevInSEL ) e->PrevInSEL->NextInSEL = 0; |
2930 | else break; |
2931 | } |
2932 | while ( isModified ); |
2933 | m_SortedEdges = 0; //important |
2934 | } |
2935 | //------------------------------------------------------------------------------ |
2936 | |
2937 | |
2938 | void Clipper::ProcessIntersectList() |
2939 | { |
2940 | for (size_t i = 0; i < m_IntersectList.size(); ++i) |
2941 | { |
2942 | IntersectNode* iNode = m_IntersectList[i]; |
2943 | { |
2944 | IntersectEdges( iNode->Edge1, iNode->Edge2, iNode->Pt); |
2945 | SwapPositionsInAEL( iNode->Edge1 , iNode->Edge2 ); |
2946 | } |
2947 | delete iNode; |
2948 | } |
2949 | m_IntersectList.clear(); |
2950 | } |
2951 | //------------------------------------------------------------------------------ |
2952 | |
2953 | bool IntersectListSort(IntersectNode* node1, IntersectNode* node2) |
2954 | { |
2955 | return node2->Pt.Y < node1->Pt.Y; |
2956 | } |
2957 | //------------------------------------------------------------------------------ |
2958 | |
2959 | inline bool EdgesAdjacent(const IntersectNode &inode) |
2960 | { |
2961 | return (inode.Edge1->NextInSEL == inode.Edge2) || |
2962 | (inode.Edge1->PrevInSEL == inode.Edge2); |
2963 | } |
2964 | //------------------------------------------------------------------------------ |
2965 | |
2966 | bool Clipper::FixupIntersectionOrder() |
2967 | { |
2968 | //pre-condition: intersections are sorted Bottom-most first. |
2969 | //Now it's crucial that intersections are made only between adjacent edges, |
2970 | //so to ensure this the order of intersections may need adjusting ... |
2971 | CopyAELToSEL(); |
2972 | std::sort(m_IntersectList.begin(), m_IntersectList.end(), IntersectListSort); |
2973 | size_t cnt = m_IntersectList.size(); |
2974 | for (size_t i = 0; i < cnt; ++i) |
2975 | { |
2976 | if (!EdgesAdjacent(*m_IntersectList[i])) |
2977 | { |
2978 | size_t j = i + 1; |
2979 | while (j < cnt && !EdgesAdjacent(*m_IntersectList[j])) j++; |
2980 | if (j == cnt) return false; |
2981 | std::swap(m_IntersectList[i], m_IntersectList[j]); |
2982 | } |
2983 | SwapPositionsInSEL(m_IntersectList[i]->Edge1, m_IntersectList[i]->Edge2); |
2984 | } |
2985 | return true; |
2986 | } |
2987 | //------------------------------------------------------------------------------ |
2988 | |
2989 | void Clipper::DoMaxima(TEdge *e) |
2990 | { |
2991 | TEdge* eMaxPair = GetMaximaPairEx(e); |
2992 | if (!eMaxPair) |
2993 | { |
2994 | if (e->OutIdx >= 0) |
2995 | AddOutPt(e, e->Top); |
2996 | DeleteFromAEL(e); |
2997 | return; |
2998 | } |
2999 | |
3000 | TEdge* eNext = e->NextInAEL; |
3001 | while(eNext && eNext != eMaxPair) |
3002 | { |
3003 | IntersectEdges(e, eNext, e->Top); |
3004 | SwapPositionsInAEL(e, eNext); |
3005 | eNext = e->NextInAEL; |
3006 | } |
3007 | |
3008 | if(e->OutIdx == Unassigned && eMaxPair->OutIdx == Unassigned) |
3009 | { |
3010 | DeleteFromAEL(e); |
3011 | DeleteFromAEL(eMaxPair); |
3012 | } |
3013 | else if( e->OutIdx >= 0 && eMaxPair->OutIdx >= 0 ) |
3014 | { |
3015 | if (e->OutIdx >= 0) AddLocalMaxPoly(e, eMaxPair, e->Top); |
3016 | DeleteFromAEL(e); |
3017 | DeleteFromAEL(eMaxPair); |
3018 | } |
3019 | #ifdef use_lines |
3020 | else if (e->WindDelta == 0) |
3021 | { |
3022 | if (e->OutIdx >= 0) |
3023 | { |
3024 | AddOutPt(e, e->Top); |
3025 | e->OutIdx = Unassigned; |
3026 | } |
3027 | DeleteFromAEL(e); |
3028 | |
3029 | if (eMaxPair->OutIdx >= 0) |
3030 | { |
3031 | AddOutPt(eMaxPair, e->Top); |
3032 | eMaxPair->OutIdx = Unassigned; |
3033 | } |
3034 | DeleteFromAEL(eMaxPair); |
3035 | } |
3036 | #endif |
3037 | else CLIPPER_THROW(clipperException("DoMaxima error" )); |
3038 | } |
3039 | //------------------------------------------------------------------------------ |
3040 | |
3041 | void Clipper::ProcessEdgesAtTopOfScanbeam(const cInt topY) |
3042 | { |
3043 | TEdge* e = m_ActiveEdges; |
3044 | while( e ) |
3045 | { |
3046 | //1. process maxima, treating them as if they're 'bent' horizontal edges, |
3047 | // but exclude maxima with horizontal edges. nb: e can't be a horizontal. |
3048 | bool IsMaximaEdge = IsMaxima(e, topY); |
3049 | |
3050 | if(IsMaximaEdge) |
3051 | { |
3052 | TEdge* eMaxPair = GetMaximaPairEx(e); |
3053 | IsMaximaEdge = (!eMaxPair || !IsHorizontal(*eMaxPair)); |
3054 | } |
3055 | |
3056 | if(IsMaximaEdge) |
3057 | { |
3058 | if (m_StrictSimple) m_Maxima.push_back(e->Top.X); |
3059 | TEdge* ePrev = e->PrevInAEL; |
3060 | DoMaxima(e); |
3061 | if( !ePrev ) e = m_ActiveEdges; |
3062 | else e = ePrev->NextInAEL; |
3063 | } |
3064 | else |
3065 | { |
3066 | //2. promote horizontal edges, otherwise update Curr.X and Curr.Y ... |
3067 | if (IsIntermediate(e, topY) && IsHorizontal(*e->NextInLML)) |
3068 | { |
3069 | UpdateEdgeIntoAEL(e); |
3070 | if (e->OutIdx >= 0) |
3071 | AddOutPt(e, e->Bot); |
3072 | AddEdgeToSEL(e); |
3073 | } |
3074 | else |
3075 | { |
3076 | e->Curr.X = TopX( *e, topY ); |
3077 | e->Curr.Y = topY; |
3078 | #ifdef use_xyz |
3079 | e->Curr.Z = topY == e->Top.Y ? e->Top.Z : (topY == e->Bot.Y ? e->Bot.Z : 0); |
3080 | #endif |
3081 | } |
3082 | |
3083 | //When StrictlySimple and 'e' is being touched by another edge, then |
3084 | //make sure both edges have a vertex here ... |
3085 | if (m_StrictSimple) |
3086 | { |
3087 | TEdge* ePrev = e->PrevInAEL; |
3088 | if ((e->OutIdx >= 0) && (e->WindDelta != 0) && ePrev && (ePrev->OutIdx >= 0) && |
3089 | (ePrev->Curr.X == e->Curr.X) && (ePrev->WindDelta != 0)) |
3090 | { |
3091 | IntPoint pt = e->Curr; |
3092 | #ifdef use_xyz |
3093 | SetZ(pt, *ePrev, *e); |
3094 | #endif |
3095 | OutPt* op = AddOutPt(ePrev, pt); |
3096 | OutPt* op2 = AddOutPt(e, pt); |
3097 | AddJoin(op, op2, pt); //StrictlySimple (type-3) join |
3098 | } |
3099 | } |
3100 | |
3101 | e = e->NextInAEL; |
3102 | } |
3103 | } |
3104 | |
3105 | //3. Process horizontals at the Top of the scanbeam ... |
3106 | m_Maxima.sort(); |
3107 | ProcessHorizontals(); |
3108 | m_Maxima.clear(); |
3109 | |
3110 | //4. Promote intermediate vertices ... |
3111 | e = m_ActiveEdges; |
3112 | while(e) |
3113 | { |
3114 | if(IsIntermediate(e, topY)) |
3115 | { |
3116 | OutPt* op = 0; |
3117 | if( e->OutIdx >= 0 ) |
3118 | op = AddOutPt(e, e->Top); |
3119 | UpdateEdgeIntoAEL(e); |
3120 | |
3121 | //if output polygons share an edge, they'll need joining later ... |
3122 | TEdge* ePrev = e->PrevInAEL; |
3123 | TEdge* eNext = e->NextInAEL; |
3124 | if (ePrev && ePrev->Curr.X == e->Bot.X && |
3125 | ePrev->Curr.Y == e->Bot.Y && op && |
3126 | ePrev->OutIdx >= 0 && ePrev->Curr.Y > ePrev->Top.Y && |
3127 | SlopesEqual(e->Curr, e->Top, ePrev->Curr, ePrev->Top, m_UseFullRange) && |
3128 | (e->WindDelta != 0) && (ePrev->WindDelta != 0)) |
3129 | { |
3130 | OutPt* op2 = AddOutPt(ePrev, e->Bot); |
3131 | AddJoin(op, op2, e->Top); |
3132 | } |
3133 | else if (eNext && eNext->Curr.X == e->Bot.X && |
3134 | eNext->Curr.Y == e->Bot.Y && op && |
3135 | eNext->OutIdx >= 0 && eNext->Curr.Y > eNext->Top.Y && |
3136 | SlopesEqual(e->Curr, e->Top, eNext->Curr, eNext->Top, m_UseFullRange) && |
3137 | (e->WindDelta != 0) && (eNext->WindDelta != 0)) |
3138 | { |
3139 | OutPt* op2 = AddOutPt(eNext, e->Bot); |
3140 | AddJoin(op, op2, e->Top); |
3141 | } |
3142 | } |
3143 | e = e->NextInAEL; |
3144 | } |
3145 | } |
3146 | //------------------------------------------------------------------------------ |
3147 | |
3148 | void Clipper::FixupOutPolyline(OutRec &outrec) |
3149 | { |
3150 | OutPt *pp = outrec.Pts; |
3151 | OutPt *lastPP = pp->Prev; |
3152 | while (pp != lastPP) |
3153 | { |
3154 | pp = pp->Next; |
3155 | if (pp->Pt == pp->Prev->Pt) |
3156 | { |
3157 | if (pp == lastPP) lastPP = pp->Prev; |
3158 | OutPt *tmpPP = pp->Prev; |
3159 | tmpPP->Next = pp->Next; |
3160 | pp->Next->Prev = tmpPP; |
3161 | delete pp; |
3162 | pp = tmpPP; |
3163 | } |
3164 | } |
3165 | |
3166 | if (pp == pp->Prev) |
3167 | { |
3168 | DisposeOutPts(pp); |
3169 | outrec.Pts = 0; |
3170 | return; |
3171 | } |
3172 | } |
3173 | //------------------------------------------------------------------------------ |
3174 | |
3175 | void Clipper::FixupOutPolygon(OutRec &outrec) |
3176 | { |
3177 | //FixupOutPolygon() - removes duplicate points and simplifies consecutive |
3178 | //parallel edges by removing the middle vertex. |
3179 | OutPt *lastOK = 0; |
3180 | outrec.BottomPt = 0; |
3181 | OutPt *pp = outrec.Pts; |
3182 | bool preserveCol = m_PreserveCollinear || m_StrictSimple; |
3183 | |
3184 | for (;;) |
3185 | { |
3186 | if (pp->Prev == pp || pp->Prev == pp->Next) |
3187 | { |
3188 | DisposeOutPts(pp); |
3189 | outrec.Pts = 0; |
3190 | return; |
3191 | } |
3192 | |
3193 | //test for duplicate points and collinear edges ... |
3194 | if ((pp->Pt == pp->Next->Pt) || (pp->Pt == pp->Prev->Pt) || |
3195 | (SlopesEqual(pp->Prev->Pt, pp->Pt, pp->Next->Pt, m_UseFullRange) && |
3196 | (!preserveCol || !Pt2IsBetweenPt1AndPt3(pp->Prev->Pt, pp->Pt, pp->Next->Pt)))) |
3197 | { |
3198 | lastOK = 0; |
3199 | OutPt *tmp = pp; |
3200 | pp->Prev->Next = pp->Next; |
3201 | pp->Next->Prev = pp->Prev; |
3202 | pp = pp->Prev; |
3203 | delete tmp; |
3204 | } |
3205 | else if (pp == lastOK) break; |
3206 | else |
3207 | { |
3208 | if (!lastOK) lastOK = pp; |
3209 | pp = pp->Next; |
3210 | } |
3211 | } |
3212 | outrec.Pts = pp; |
3213 | } |
3214 | //------------------------------------------------------------------------------ |
3215 | |
3216 | int PointCount(OutPt *Pts) |
3217 | { |
3218 | if (!Pts) return 0; |
3219 | int result = 0; |
3220 | OutPt* p = Pts; |
3221 | do |
3222 | { |
3223 | result++; |
3224 | p = p->Next; |
3225 | } |
3226 | while (p != Pts); |
3227 | return result; |
3228 | } |
3229 | //------------------------------------------------------------------------------ |
3230 | |
3231 | void Clipper::BuildResult(Paths &polys) |
3232 | { |
3233 | polys.reserve(m_PolyOuts.size()); |
3234 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) |
3235 | { |
3236 | if (!m_PolyOuts[i]->Pts) continue; |
3237 | Path pg; |
3238 | OutPt* p = m_PolyOuts[i]->Pts->Prev; |
3239 | int cnt = PointCount(p); |
3240 | if (cnt < 2) continue; |
3241 | pg.reserve(cnt); |
3242 | for (int i = 0; i < cnt; ++i) |
3243 | { |
3244 | pg.push_back(p->Pt); |
3245 | p = p->Prev; |
3246 | } |
3247 | polys.push_back(pg); |
3248 | } |
3249 | } |
3250 | //------------------------------------------------------------------------------ |
3251 | |
3252 | void Clipper::BuildResult2(PolyTree& polytree) |
3253 | { |
3254 | polytree.Clear(); |
3255 | polytree.AllNodes.reserve(m_PolyOuts.size()); |
3256 | //add each output polygon/contour to polytree ... |
3257 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++) |
3258 | { |
3259 | OutRec* outRec = m_PolyOuts[i]; |
3260 | int cnt = PointCount(outRec->Pts); |
3261 | if ((outRec->IsOpen && cnt < 2) || (!outRec->IsOpen && cnt < 3)) continue; |
3262 | FixHoleLinkage(*outRec); |
3263 | PolyNode* pn = new PolyNode(); |
3264 | //nb: polytree takes ownership of all the PolyNodes |
3265 | polytree.AllNodes.push_back(pn); |
3266 | outRec->PolyNd = pn; |
3267 | pn->Parent = 0; |
3268 | pn->Index = 0; |
3269 | pn->Contour.reserve(cnt); |
3270 | OutPt *op = outRec->Pts->Prev; |
3271 | for (int j = 0; j < cnt; j++) |
3272 | { |
3273 | pn->Contour.push_back(op->Pt); |
3274 | op = op->Prev; |
3275 | } |
3276 | } |
3277 | |
3278 | //fixup PolyNode links etc ... |
3279 | polytree.Childs.reserve(m_PolyOuts.size()); |
3280 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); i++) |
3281 | { |
3282 | OutRec* outRec = m_PolyOuts[i]; |
3283 | if (!outRec->PolyNd) continue; |
3284 | if (outRec->IsOpen) |
3285 | { |
3286 | outRec->PolyNd->m_IsOpen = true; |
3287 | polytree.AddChild(*outRec->PolyNd); |
3288 | } |
3289 | else if (outRec->FirstLeft && outRec->FirstLeft->PolyNd) |
3290 | outRec->FirstLeft->PolyNd->AddChild(*outRec->PolyNd); |
3291 | else |
3292 | polytree.AddChild(*outRec->PolyNd); |
3293 | } |
3294 | } |
3295 | //------------------------------------------------------------------------------ |
3296 | |
3297 | void SwapIntersectNodes(IntersectNode &int1, IntersectNode &int2) |
3298 | { |
3299 | //just swap the contents (because fIntersectNodes is a single-linked-list) |
3300 | IntersectNode inode = int1; //gets a copy of Int1 |
3301 | int1.Edge1 = int2.Edge1; |
3302 | int1.Edge2 = int2.Edge2; |
3303 | int1.Pt = int2.Pt; |
3304 | int2.Edge1 = inode.Edge1; |
3305 | int2.Edge2 = inode.Edge2; |
3306 | int2.Pt = inode.Pt; |
3307 | } |
3308 | //------------------------------------------------------------------------------ |
3309 | |
3310 | inline bool E2InsertsBeforeE1(TEdge &e1, TEdge &e2) |
3311 | { |
3312 | if (e2.Curr.X == e1.Curr.X) |
3313 | { |
3314 | if (e2.Top.Y > e1.Top.Y) |
3315 | return e2.Top.X < TopX(e1, e2.Top.Y); |
3316 | else return e1.Top.X > TopX(e2, e1.Top.Y); |
3317 | } |
3318 | else return e2.Curr.X < e1.Curr.X; |
3319 | } |
3320 | //------------------------------------------------------------------------------ |
3321 | |
3322 | bool GetOverlap(const cInt a1, const cInt a2, const cInt b1, const cInt b2, |
3323 | cInt& Left, cInt& Right) |
3324 | { |
3325 | if (a1 < a2) |
3326 | { |
3327 | if (b1 < b2) {Left = std::max(a1,b1); Right = std::min(a2,b2);} |
3328 | else {Left = std::max(a1,b2); Right = std::min(a2,b1);} |
3329 | } |
3330 | else |
3331 | { |
3332 | if (b1 < b2) {Left = std::max(a2,b1); Right = std::min(a1,b2);} |
3333 | else {Left = std::max(a2,b2); Right = std::min(a1,b1);} |
3334 | } |
3335 | return Left < Right; |
3336 | } |
3337 | //------------------------------------------------------------------------------ |
3338 | |
3339 | inline void UpdateOutPtIdxs(OutRec& outrec) |
3340 | { |
3341 | OutPt* op = outrec.Pts; |
3342 | do |
3343 | { |
3344 | op->Idx = outrec.Idx; |
3345 | op = op->Prev; |
3346 | } |
3347 | while(op != outrec.Pts); |
3348 | } |
3349 | //------------------------------------------------------------------------------ |
3350 | |
3351 | void Clipper::InsertEdgeIntoAEL(TEdge *edge, TEdge* startEdge) |
3352 | { |
3353 | if(!m_ActiveEdges) |
3354 | { |
3355 | edge->PrevInAEL = 0; |
3356 | edge->NextInAEL = 0; |
3357 | m_ActiveEdges = edge; |
3358 | } |
3359 | else if(!startEdge && E2InsertsBeforeE1(*m_ActiveEdges, *edge)) |
3360 | { |
3361 | edge->PrevInAEL = 0; |
3362 | edge->NextInAEL = m_ActiveEdges; |
3363 | m_ActiveEdges->PrevInAEL = edge; |
3364 | m_ActiveEdges = edge; |
3365 | } |
3366 | else |
3367 | { |
3368 | if(!startEdge) startEdge = m_ActiveEdges; |
3369 | while(startEdge->NextInAEL && |
3370 | !E2InsertsBeforeE1(*startEdge->NextInAEL , *edge)) |
3371 | startEdge = startEdge->NextInAEL; |
3372 | edge->NextInAEL = startEdge->NextInAEL; |
3373 | if(startEdge->NextInAEL) startEdge->NextInAEL->PrevInAEL = edge; |
3374 | edge->PrevInAEL = startEdge; |
3375 | startEdge->NextInAEL = edge; |
3376 | } |
3377 | } |
3378 | //---------------------------------------------------------------------- |
3379 | |
3380 | OutPt* DupOutPt(OutPt* outPt, bool InsertAfter) |
3381 | { |
3382 | OutPt* result = new OutPt; |
3383 | result->Pt = outPt->Pt; |
3384 | result->Idx = outPt->Idx; |
3385 | if (InsertAfter) |
3386 | { |
3387 | result->Next = outPt->Next; |
3388 | result->Prev = outPt; |
3389 | outPt->Next->Prev = result; |
3390 | outPt->Next = result; |
3391 | } |
3392 | else |
3393 | { |
3394 | result->Prev = outPt->Prev; |
3395 | result->Next = outPt; |
3396 | outPt->Prev->Next = result; |
3397 | outPt->Prev = result; |
3398 | } |
3399 | return result; |
3400 | } |
3401 | //------------------------------------------------------------------------------ |
3402 | |
3403 | bool JoinHorz(OutPt* op1, OutPt* op1b, OutPt* op2, OutPt* op2b, |
3404 | const IntPoint Pt, bool DiscardLeft) |
3405 | { |
3406 | Direction Dir1 = (op1->Pt.X > op1b->Pt.X ? dRightToLeft : dLeftToRight); |
3407 | Direction Dir2 = (op2->Pt.X > op2b->Pt.X ? dRightToLeft : dLeftToRight); |
3408 | if (Dir1 == Dir2) return false; |
3409 | |
3410 | //When DiscardLeft, we want Op1b to be on the Left of Op1, otherwise we |
3411 | //want Op1b to be on the Right. (And likewise with Op2 and Op2b.) |
3412 | //So, to facilitate this while inserting Op1b and Op2b ... |
3413 | //when DiscardLeft, make sure we're AT or RIGHT of Pt before adding Op1b, |
3414 | //otherwise make sure we're AT or LEFT of Pt. (Likewise with Op2b.) |
3415 | if (Dir1 == dLeftToRight) |
3416 | { |
3417 | while (op1->Next->Pt.X <= Pt.X && |
3418 | op1->Next->Pt.X >= op1->Pt.X && op1->Next->Pt.Y == Pt.Y) |
3419 | op1 = op1->Next; |
3420 | if (DiscardLeft && (op1->Pt.X != Pt.X)) op1 = op1->Next; |
3421 | op1b = DupOutPt(op1, !DiscardLeft); |
3422 | if (op1b->Pt != Pt) |
3423 | { |
3424 | op1 = op1b; |
3425 | op1->Pt = Pt; |
3426 | op1b = DupOutPt(op1, !DiscardLeft); |
3427 | } |
3428 | } |
3429 | else |
3430 | { |
3431 | while (op1->Next->Pt.X >= Pt.X && |
3432 | op1->Next->Pt.X <= op1->Pt.X && op1->Next->Pt.Y == Pt.Y) |
3433 | op1 = op1->Next; |
3434 | if (!DiscardLeft && (op1->Pt.X != Pt.X)) op1 = op1->Next; |
3435 | op1b = DupOutPt(op1, DiscardLeft); |
3436 | if (op1b->Pt != Pt) |
3437 | { |
3438 | op1 = op1b; |
3439 | op1->Pt = Pt; |
3440 | op1b = DupOutPt(op1, DiscardLeft); |
3441 | } |
3442 | } |
3443 | |
3444 | if (Dir2 == dLeftToRight) |
3445 | { |
3446 | while (op2->Next->Pt.X <= Pt.X && |
3447 | op2->Next->Pt.X >= op2->Pt.X && op2->Next->Pt.Y == Pt.Y) |
3448 | op2 = op2->Next; |
3449 | if (DiscardLeft && (op2->Pt.X != Pt.X)) op2 = op2->Next; |
3450 | op2b = DupOutPt(op2, !DiscardLeft); |
3451 | if (op2b->Pt != Pt) |
3452 | { |
3453 | op2 = op2b; |
3454 | op2->Pt = Pt; |
3455 | op2b = DupOutPt(op2, !DiscardLeft); |
3456 | }; |
3457 | } else |
3458 | { |
3459 | while (op2->Next->Pt.X >= Pt.X && |
3460 | op2->Next->Pt.X <= op2->Pt.X && op2->Next->Pt.Y == Pt.Y) |
3461 | op2 = op2->Next; |
3462 | if (!DiscardLeft && (op2->Pt.X != Pt.X)) op2 = op2->Next; |
3463 | op2b = DupOutPt(op2, DiscardLeft); |
3464 | if (op2b->Pt != Pt) |
3465 | { |
3466 | op2 = op2b; |
3467 | op2->Pt = Pt; |
3468 | op2b = DupOutPt(op2, DiscardLeft); |
3469 | }; |
3470 | }; |
3471 | |
3472 | if ((Dir1 == dLeftToRight) == DiscardLeft) |
3473 | { |
3474 | op1->Prev = op2; |
3475 | op2->Next = op1; |
3476 | op1b->Next = op2b; |
3477 | op2b->Prev = op1b; |
3478 | } |
3479 | else |
3480 | { |
3481 | op1->Next = op2; |
3482 | op2->Prev = op1; |
3483 | op1b->Prev = op2b; |
3484 | op2b->Next = op1b; |
3485 | } |
3486 | return true; |
3487 | } |
3488 | //------------------------------------------------------------------------------ |
3489 | |
3490 | bool Clipper::JoinPoints(Join *j, OutRec* outRec1, OutRec* outRec2) |
3491 | { |
3492 | OutPt *op1 = j->OutPt1, *op1b; |
3493 | OutPt *op2 = j->OutPt2, *op2b; |
3494 | |
3495 | //There are 3 kinds of joins for output polygons ... |
3496 | //1. Horizontal joins where Join.OutPt1 & Join.OutPt2 are vertices anywhere |
3497 | //along (horizontal) collinear edges (& Join.OffPt is on the same horizontal). |
3498 | //2. Non-horizontal joins where Join.OutPt1 & Join.OutPt2 are at the same |
3499 | //location at the Bottom of the overlapping segment (& Join.OffPt is above). |
3500 | //3. StrictSimple joins where edges touch but are not collinear and where |
3501 | //Join.OutPt1, Join.OutPt2 & Join.OffPt all share the same point. |
3502 | bool isHorizontal = (j->OutPt1->Pt.Y == j->OffPt.Y); |
3503 | |
3504 | if (isHorizontal && (j->OffPt == j->OutPt1->Pt) && |
3505 | (j->OffPt == j->OutPt2->Pt)) |
3506 | { |
3507 | //Strictly Simple join ... |
3508 | if (outRec1 != outRec2) return false; |
3509 | op1b = j->OutPt1->Next; |
3510 | while (op1b != op1 && (op1b->Pt == j->OffPt)) |
3511 | op1b = op1b->Next; |
3512 | bool reverse1 = (op1b->Pt.Y > j->OffPt.Y); |
3513 | op2b = j->OutPt2->Next; |
3514 | while (op2b != op2 && (op2b->Pt == j->OffPt)) |
3515 | op2b = op2b->Next; |
3516 | bool reverse2 = (op2b->Pt.Y > j->OffPt.Y); |
3517 | if (reverse1 == reverse2) return false; |
3518 | if (reverse1) |
3519 | { |
3520 | op1b = DupOutPt(op1, false); |
3521 | op2b = DupOutPt(op2, true); |
3522 | op1->Prev = op2; |
3523 | op2->Next = op1; |
3524 | op1b->Next = op2b; |
3525 | op2b->Prev = op1b; |
3526 | j->OutPt1 = op1; |
3527 | j->OutPt2 = op1b; |
3528 | return true; |
3529 | } else |
3530 | { |
3531 | op1b = DupOutPt(op1, true); |
3532 | op2b = DupOutPt(op2, false); |
3533 | op1->Next = op2; |
3534 | op2->Prev = op1; |
3535 | op1b->Prev = op2b; |
3536 | op2b->Next = op1b; |
3537 | j->OutPt1 = op1; |
3538 | j->OutPt2 = op1b; |
3539 | return true; |
3540 | } |
3541 | } |
3542 | else if (isHorizontal) |
3543 | { |
3544 | //treat horizontal joins differently to non-horizontal joins since with |
3545 | //them we're not yet sure where the overlapping is. OutPt1.Pt & OutPt2.Pt |
3546 | //may be anywhere along the horizontal edge. |
3547 | op1b = op1; |
3548 | while (op1->Prev->Pt.Y == op1->Pt.Y && op1->Prev != op1b && op1->Prev != op2) |
3549 | op1 = op1->Prev; |
3550 | while (op1b->Next->Pt.Y == op1b->Pt.Y && op1b->Next != op1 && op1b->Next != op2) |
3551 | op1b = op1b->Next; |
3552 | if (op1b->Next == op1 || op1b->Next == op2) return false; //a flat 'polygon' |
3553 | |
3554 | op2b = op2; |
3555 | while (op2->Prev->Pt.Y == op2->Pt.Y && op2->Prev != op2b && op2->Prev != op1b) |
3556 | op2 = op2->Prev; |
3557 | while (op2b->Next->Pt.Y == op2b->Pt.Y && op2b->Next != op2 && op2b->Next != op1) |
3558 | op2b = op2b->Next; |
3559 | if (op2b->Next == op2 || op2b->Next == op1) return false; //a flat 'polygon' |
3560 | |
3561 | cInt Left, Right; |
3562 | //Op1 --> Op1b & Op2 --> Op2b are the extremites of the horizontal edges |
3563 | if (!GetOverlap(op1->Pt.X, op1b->Pt.X, op2->Pt.X, op2b->Pt.X, Left, Right)) |
3564 | return false; |
3565 | |
3566 | //DiscardLeftSide: when overlapping edges are joined, a spike will created |
3567 | //which needs to be cleaned up. However, we don't want Op1 or Op2 caught up |
3568 | //on the discard Side as either may still be needed for other joins ... |
3569 | IntPoint Pt; |
3570 | bool DiscardLeftSide; |
3571 | if (op1->Pt.X >= Left && op1->Pt.X <= Right) |
3572 | { |
3573 | Pt = op1->Pt; DiscardLeftSide = (op1->Pt.X > op1b->Pt.X); |
3574 | } |
3575 | else if (op2->Pt.X >= Left&& op2->Pt.X <= Right) |
3576 | { |
3577 | Pt = op2->Pt; DiscardLeftSide = (op2->Pt.X > op2b->Pt.X); |
3578 | } |
3579 | else if (op1b->Pt.X >= Left && op1b->Pt.X <= Right) |
3580 | { |
3581 | Pt = op1b->Pt; DiscardLeftSide = op1b->Pt.X > op1->Pt.X; |
3582 | } |
3583 | else |
3584 | { |
3585 | Pt = op2b->Pt; DiscardLeftSide = (op2b->Pt.X > op2->Pt.X); |
3586 | } |
3587 | j->OutPt1 = op1; j->OutPt2 = op2; |
3588 | return JoinHorz(op1, op1b, op2, op2b, Pt, DiscardLeftSide); |
3589 | } else |
3590 | { |
3591 | //nb: For non-horizontal joins ... |
3592 | // 1. Jr.OutPt1.Pt.Y == Jr.OutPt2.Pt.Y |
3593 | // 2. Jr.OutPt1.Pt > Jr.OffPt.Y |
3594 | |
3595 | //make sure the polygons are correctly oriented ... |
3596 | op1b = op1->Next; |
3597 | while ((op1b->Pt == op1->Pt) && (op1b != op1)) op1b = op1b->Next; |
3598 | bool Reverse1 = ((op1b->Pt.Y > op1->Pt.Y) || |
3599 | !SlopesEqual(op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange)); |
3600 | if (Reverse1) |
3601 | { |
3602 | op1b = op1->Prev; |
3603 | while ((op1b->Pt == op1->Pt) && (op1b != op1)) op1b = op1b->Prev; |
3604 | if ((op1b->Pt.Y > op1->Pt.Y) || |
3605 | !SlopesEqual(op1->Pt, op1b->Pt, j->OffPt, m_UseFullRange)) return false; |
3606 | }; |
3607 | op2b = op2->Next; |
3608 | while ((op2b->Pt == op2->Pt) && (op2b != op2))op2b = op2b->Next; |
3609 | bool Reverse2 = ((op2b->Pt.Y > op2->Pt.Y) || |
3610 | !SlopesEqual(op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange)); |
3611 | if (Reverse2) |
3612 | { |
3613 | op2b = op2->Prev; |
3614 | while ((op2b->Pt == op2->Pt) && (op2b != op2)) op2b = op2b->Prev; |
3615 | if ((op2b->Pt.Y > op2->Pt.Y) || |
3616 | !SlopesEqual(op2->Pt, op2b->Pt, j->OffPt, m_UseFullRange)) return false; |
3617 | } |
3618 | |
3619 | if ((op1b == op1) || (op2b == op2) || (op1b == op2b) || |
3620 | ((outRec1 == outRec2) && (Reverse1 == Reverse2))) return false; |
3621 | |
3622 | if (Reverse1) |
3623 | { |
3624 | op1b = DupOutPt(op1, false); |
3625 | op2b = DupOutPt(op2, true); |
3626 | op1->Prev = op2; |
3627 | op2->Next = op1; |
3628 | op1b->Next = op2b; |
3629 | op2b->Prev = op1b; |
3630 | j->OutPt1 = op1; |
3631 | j->OutPt2 = op1b; |
3632 | return true; |
3633 | } else |
3634 | { |
3635 | op1b = DupOutPt(op1, true); |
3636 | op2b = DupOutPt(op2, false); |
3637 | op1->Next = op2; |
3638 | op2->Prev = op1; |
3639 | op1b->Prev = op2b; |
3640 | op2b->Next = op1b; |
3641 | j->OutPt1 = op1; |
3642 | j->OutPt2 = op1b; |
3643 | return true; |
3644 | } |
3645 | } |
3646 | } |
3647 | //---------------------------------------------------------------------- |
3648 | |
3649 | static OutRec* ParseFirstLeft(OutRec* FirstLeft) |
3650 | { |
3651 | while (FirstLeft && !FirstLeft->Pts) |
3652 | FirstLeft = FirstLeft->FirstLeft; |
3653 | return FirstLeft; |
3654 | } |
3655 | //------------------------------------------------------------------------------ |
3656 | |
3657 | void Clipper::FixupFirstLefts1(OutRec* OldOutRec, OutRec* NewOutRec) |
3658 | { |
3659 | //tests if NewOutRec contains the polygon before reassigning FirstLeft |
3660 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) |
3661 | { |
3662 | OutRec* outRec = m_PolyOuts[i]; |
3663 | OutRec* firstLeft = ParseFirstLeft(outRec->FirstLeft); |
3664 | if (outRec->Pts && firstLeft == OldOutRec) |
3665 | { |
3666 | if (Poly2ContainsPoly1(outRec->Pts, NewOutRec->Pts)) |
3667 | outRec->FirstLeft = NewOutRec; |
3668 | } |
3669 | } |
3670 | } |
3671 | //---------------------------------------------------------------------- |
3672 | |
3673 | void Clipper::FixupFirstLefts2(OutRec* InnerOutRec, OutRec* OuterOutRec) |
3674 | { |
3675 | //A polygon has split into two such that one is now the inner of the other. |
3676 | //It's possible that these polygons now wrap around other polygons, so check |
3677 | //every polygon that's also contained by OuterOutRec's FirstLeft container |
3678 | //(including 0) to see if they've become inner to the new inner polygon ... |
3679 | OutRec* orfl = OuterOutRec->FirstLeft; |
3680 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) |
3681 | { |
3682 | OutRec* outRec = m_PolyOuts[i]; |
3683 | |
3684 | if (!outRec->Pts || outRec == OuterOutRec || outRec == InnerOutRec) |
3685 | continue; |
3686 | OutRec* firstLeft = ParseFirstLeft(outRec->FirstLeft); |
3687 | if (firstLeft != orfl && firstLeft != InnerOutRec && firstLeft != OuterOutRec) |
3688 | continue; |
3689 | if (Poly2ContainsPoly1(outRec->Pts, InnerOutRec->Pts)) |
3690 | outRec->FirstLeft = InnerOutRec; |
3691 | else if (Poly2ContainsPoly1(outRec->Pts, OuterOutRec->Pts)) |
3692 | outRec->FirstLeft = OuterOutRec; |
3693 | else if (outRec->FirstLeft == InnerOutRec || outRec->FirstLeft == OuterOutRec) |
3694 | outRec->FirstLeft = orfl; |
3695 | } |
3696 | } |
3697 | //---------------------------------------------------------------------- |
3698 | void Clipper::FixupFirstLefts3(OutRec* OldOutRec, OutRec* NewOutRec) |
3699 | { |
3700 | //reassigns FirstLeft WITHOUT testing if NewOutRec contains the polygon |
3701 | for (PolyOutList::size_type i = 0; i < m_PolyOuts.size(); ++i) |
3702 | { |
3703 | OutRec* outRec = m_PolyOuts[i]; |
3704 | OutRec* firstLeft = ParseFirstLeft(outRec->FirstLeft); |
3705 | if (outRec->Pts && firstLeft == OldOutRec) |
3706 | outRec->FirstLeft = NewOutRec; |
3707 | } |
3708 | } |
3709 | //---------------------------------------------------------------------- |
3710 | |
3711 | void Clipper::JoinCommonEdges() |
3712 | { |
3713 | for (JoinList::size_type i = 0; i < m_Joins.size(); i++) |
3714 | { |
3715 | Join* join = m_Joins[i]; |
3716 | |
3717 | OutRec *outRec1 = GetOutRec(join->OutPt1->Idx); |
3718 | OutRec *outRec2 = GetOutRec(join->OutPt2->Idx); |
3719 | |
3720 | if (!outRec1->Pts || !outRec2->Pts) continue; |
3721 | if (outRec1->IsOpen || outRec2->IsOpen) continue; |
3722 | |
3723 | //get the polygon fragment with the correct hole state (FirstLeft) |
3724 | //before calling JoinPoints() ... |
3725 | OutRec *holeStateRec; |
3726 | if (outRec1 == outRec2) holeStateRec = outRec1; |
3727 | else if (OutRec1RightOfOutRec2(outRec1, outRec2)) holeStateRec = outRec2; |
3728 | else if (OutRec1RightOfOutRec2(outRec2, outRec1)) holeStateRec = outRec1; |
3729 | else holeStateRec = GetLowermostRec(outRec1, outRec2); |
3730 | |
3731 | if (!JoinPoints(join, outRec1, outRec2)) continue; |
3732 | |
3733 | if (outRec1 == outRec2) |
3734 | { |
3735 | //instead of joining two polygons, we've just created a new one by |
3736 | //splitting one polygon into two. |
3737 | outRec1->Pts = join->OutPt1; |
3738 | outRec1->BottomPt = 0; |
3739 | outRec2 = CreateOutRec(); |
3740 | outRec2->Pts = join->OutPt2; |
3741 | |
3742 | //update all OutRec2.Pts Idx's ... |
3743 | UpdateOutPtIdxs(*outRec2); |
3744 | |
3745 | if (Poly2ContainsPoly1(outRec2->Pts, outRec1->Pts)) |
3746 | { |
3747 | //outRec1 contains outRec2 ... |
3748 | outRec2->IsHole = !outRec1->IsHole; |
3749 | outRec2->FirstLeft = outRec1; |
3750 | |
3751 | if (m_UsingPolyTree) FixupFirstLefts2(outRec2, outRec1); |
3752 | |
3753 | if ((outRec2->IsHole ^ m_ReverseOutput) == (Area(*outRec2) > 0)) |
3754 | ReversePolyPtLinks(outRec2->Pts); |
3755 | |
3756 | } else if (Poly2ContainsPoly1(outRec1->Pts, outRec2->Pts)) |
3757 | { |
3758 | //outRec2 contains outRec1 ... |
3759 | outRec2->IsHole = outRec1->IsHole; |
3760 | outRec1->IsHole = !outRec2->IsHole; |
3761 | outRec2->FirstLeft = outRec1->FirstLeft; |
3762 | outRec1->FirstLeft = outRec2; |
3763 | |
3764 | if (m_UsingPolyTree) FixupFirstLefts2(outRec1, outRec2); |
3765 | |
3766 | if ((outRec1->IsHole ^ m_ReverseOutput) == (Area(*outRec1) > 0)) |
3767 | ReversePolyPtLinks(outRec1->Pts); |
3768 | } |
3769 | else |
3770 | { |
3771 | //the 2 polygons are completely separate ... |
3772 | outRec2->IsHole = outRec1->IsHole; |
3773 | outRec2->FirstLeft = outRec1->FirstLeft; |
3774 | |
3775 | //fixup FirstLeft pointers that may need reassigning to OutRec2 |
3776 | if (m_UsingPolyTree) FixupFirstLefts1(outRec1, outRec2); |
3777 | } |
3778 | |
3779 | } else |
3780 | { |
3781 | //joined 2 polygons together ... |
3782 | |
3783 | outRec2->Pts = 0; |
3784 | outRec2->BottomPt = 0; |
3785 | outRec2->Idx = outRec1->Idx; |
3786 | |
3787 | outRec1->IsHole = holeStateRec->IsHole; |
3788 | if (holeStateRec == outRec2) |
3789 | outRec1->FirstLeft = outRec2->FirstLeft; |
3790 | outRec2->FirstLeft = outRec1; |
3791 | |
3792 | if (m_UsingPolyTree) FixupFirstLefts3(outRec2, outRec1); |
3793 | } |
3794 | } |
3795 | } |
3796 | |
3797 | //------------------------------------------------------------------------------ |
3798 | // ClipperOffset support functions ... |
3799 | //------------------------------------------------------------------------------ |
3800 | |
3801 | DoublePoint GetUnitNormal(const IntPoint &pt1, const IntPoint &pt2) |
3802 | { |
3803 | if(pt2.X == pt1.X && pt2.Y == pt1.Y) |
3804 | return DoublePoint(0, 0); |
3805 | |
3806 | double Dx = (double)(pt2.X - pt1.X); |
3807 | double dy = (double)(pt2.Y - pt1.Y); |
3808 | double f = 1 *1.0/ std::sqrt( Dx*Dx + dy*dy ); |
3809 | Dx *= f; |
3810 | dy *= f; |
3811 | return DoublePoint(dy, -Dx); |
3812 | } |
3813 | |
3814 | //------------------------------------------------------------------------------ |
3815 | // ClipperOffset class |
3816 | //------------------------------------------------------------------------------ |
3817 | |
3818 | ClipperOffset::ClipperOffset(double miterLimit, double arcTolerance) |
3819 | { |
3820 | this->MiterLimit = miterLimit; |
3821 | this->ArcTolerance = arcTolerance; |
3822 | m_lowest.X = -1; |
3823 | } |
3824 | //------------------------------------------------------------------------------ |
3825 | |
3826 | ClipperOffset::~ClipperOffset() |
3827 | { |
3828 | Clear(); |
3829 | } |
3830 | //------------------------------------------------------------------------------ |
3831 | |
3832 | void ClipperOffset::Clear() |
3833 | { |
3834 | for (int i = 0; i < m_polyNodes.ChildCount(); ++i) |
3835 | delete m_polyNodes.Childs[i]; |
3836 | m_polyNodes.Childs.clear(); |
3837 | m_lowest.X = -1; |
3838 | } |
3839 | //------------------------------------------------------------------------------ |
3840 | |
3841 | void ClipperOffset::AddPath(const Path& path, JoinType joinType, EndType endType) |
3842 | { |
3843 | int highI = (int)path.size() - 1; |
3844 | if (highI < 0) return; |
3845 | PolyNode* newNode = new PolyNode(); |
3846 | newNode->m_jointype = joinType; |
3847 | newNode->m_endtype = endType; |
3848 | |
3849 | //strip duplicate points from path and also get index to the lowest point ... |
3850 | if (endType == etClosedLine || endType == etClosedPolygon) |
3851 | while (highI > 0 && path[0] == path[highI]) highI--; |
3852 | newNode->Contour.reserve(highI + 1); |
3853 | newNode->Contour.push_back(path[0]); |
3854 | int j = 0, k = 0; |
3855 | for (int i = 1; i <= highI; i++) |
3856 | if (newNode->Contour[j] != path[i]) |
3857 | { |
3858 | j++; |
3859 | newNode->Contour.push_back(path[i]); |
3860 | if (path[i].Y > newNode->Contour[k].Y || |
3861 | (path[i].Y == newNode->Contour[k].Y && |
3862 | path[i].X < newNode->Contour[k].X)) k = j; |
3863 | } |
3864 | if (endType == etClosedPolygon && j < 2) |
3865 | { |
3866 | delete newNode; |
3867 | return; |
3868 | } |
3869 | m_polyNodes.AddChild(*newNode); |
3870 | |
3871 | //if this path's lowest pt is lower than all the others then update m_lowest |
3872 | if (endType != etClosedPolygon) return; |
3873 | if (m_lowest.X < 0) |
3874 | m_lowest = IntPoint(m_polyNodes.ChildCount() - 1, k); |
3875 | else |
3876 | { |
3877 | IntPoint ip = m_polyNodes.Childs[(int)m_lowest.X]->Contour[(int)m_lowest.Y]; |
3878 | if (newNode->Contour[k].Y > ip.Y || |
3879 | (newNode->Contour[k].Y == ip.Y && |
3880 | newNode->Contour[k].X < ip.X)) |
3881 | m_lowest = IntPoint(m_polyNodes.ChildCount() - 1, k); |
3882 | } |
3883 | } |
3884 | //------------------------------------------------------------------------------ |
3885 | |
3886 | void ClipperOffset::AddPaths(const Paths& paths, JoinType joinType, EndType endType) |
3887 | { |
3888 | for (Paths::size_type i = 0; i < paths.size(); ++i) |
3889 | AddPath(paths[i], joinType, endType); |
3890 | } |
3891 | //------------------------------------------------------------------------------ |
3892 | |
3893 | void ClipperOffset::FixOrientations() |
3894 | { |
3895 | //fixup orientations of all closed paths if the orientation of the |
3896 | //closed path with the lowermost vertex is wrong ... |
3897 | if (m_lowest.X >= 0 && |
3898 | !Orientation(m_polyNodes.Childs[(int)m_lowest.X]->Contour)) |
3899 | { |
3900 | for (int i = 0; i < m_polyNodes.ChildCount(); ++i) |
3901 | { |
3902 | PolyNode& node = *m_polyNodes.Childs[i]; |
3903 | if (node.m_endtype == etClosedPolygon || |
3904 | (node.m_endtype == etClosedLine && Orientation(node.Contour))) |
3905 | ReversePath(node.Contour); |
3906 | } |
3907 | } else |
3908 | { |
3909 | for (int i = 0; i < m_polyNodes.ChildCount(); ++i) |
3910 | { |
3911 | PolyNode& node = *m_polyNodes.Childs[i]; |
3912 | if (node.m_endtype == etClosedLine && !Orientation(node.Contour)) |
3913 | ReversePath(node.Contour); |
3914 | } |
3915 | } |
3916 | } |
3917 | //------------------------------------------------------------------------------ |
3918 | |
3919 | void ClipperOffset::Execute(Paths& solution, double delta) |
3920 | { |
3921 | solution.clear(); |
3922 | FixOrientations(); |
3923 | DoOffset(delta); |
3924 | |
3925 | //now clean up 'corners' ... |
3926 | Clipper clpr; |
3927 | clpr.AddPaths(m_destPolys, ptSubject, true); |
3928 | if (delta > 0) |
3929 | { |
3930 | clpr.Execute(ctUnion, solution, pftPositive, pftPositive); |
3931 | } |
3932 | else |
3933 | { |
3934 | IntRect r = clpr.GetBounds(); |
3935 | Path outer(4); |
3936 | outer[0] = IntPoint(r.left - 10, r.bottom + 10); |
3937 | outer[1] = IntPoint(r.right + 10, r.bottom + 10); |
3938 | outer[2] = IntPoint(r.right + 10, r.top - 10); |
3939 | outer[3] = IntPoint(r.left - 10, r.top - 10); |
3940 | |
3941 | clpr.AddPath(outer, ptSubject, true); |
3942 | clpr.ReverseSolution(true); |
3943 | clpr.Execute(ctUnion, solution, pftNegative, pftNegative); |
3944 | if (solution.size() > 0) solution.erase(solution.begin()); |
3945 | } |
3946 | } |
3947 | //------------------------------------------------------------------------------ |
3948 | |
3949 | void ClipperOffset::Execute(PolyTree& solution, double delta) |
3950 | { |
3951 | solution.Clear(); |
3952 | FixOrientations(); |
3953 | DoOffset(delta); |
3954 | |
3955 | //now clean up 'corners' ... |
3956 | Clipper clpr; |
3957 | clpr.AddPaths(m_destPolys, ptSubject, true); |
3958 | if (delta > 0) |
3959 | { |
3960 | clpr.Execute(ctUnion, solution, pftPositive, pftPositive); |
3961 | } |
3962 | else |
3963 | { |
3964 | IntRect r = clpr.GetBounds(); |
3965 | Path outer(4); |
3966 | outer[0] = IntPoint(r.left - 10, r.bottom + 10); |
3967 | outer[1] = IntPoint(r.right + 10, r.bottom + 10); |
3968 | outer[2] = IntPoint(r.right + 10, r.top - 10); |
3969 | outer[3] = IntPoint(r.left - 10, r.top - 10); |
3970 | |
3971 | clpr.AddPath(outer, ptSubject, true); |
3972 | clpr.ReverseSolution(true); |
3973 | clpr.Execute(ctUnion, solution, pftNegative, pftNegative); |
3974 | //remove the outer PolyNode rectangle ... |
3975 | if (solution.ChildCount() == 1 && solution.Childs[0]->ChildCount() > 0) |
3976 | { |
3977 | PolyNode* outerNode = solution.Childs[0]; |
3978 | solution.Childs.reserve(outerNode->ChildCount()); |
3979 | solution.Childs[0] = outerNode->Childs[0]; |
3980 | solution.Childs[0]->Parent = outerNode->Parent; |
3981 | for (int i = 1; i < outerNode->ChildCount(); ++i) |
3982 | solution.AddChild(*outerNode->Childs[i]); |
3983 | } |
3984 | else |
3985 | solution.Clear(); |
3986 | } |
3987 | } |
3988 | //------------------------------------------------------------------------------ |
3989 | |
3990 | void ClipperOffset::DoOffset(double delta) |
3991 | { |
3992 | m_destPolys.clear(); |
3993 | m_delta = delta; |
3994 | |
3995 | //if Zero offset, just copy any CLOSED polygons to m_p and return ... |
3996 | if (NEAR_ZERO(delta)) |
3997 | { |
3998 | m_destPolys.reserve(m_polyNodes.ChildCount()); |
3999 | for (int i = 0; i < m_polyNodes.ChildCount(); i++) |
4000 | { |
4001 | PolyNode& node = *m_polyNodes.Childs[i]; |
4002 | if (node.m_endtype == etClosedPolygon) |
4003 | m_destPolys.push_back(node.Contour); |
4004 | } |
4005 | return; |
4006 | } |
4007 | |
4008 | //see offset_triginometry3.svg in the documentation folder ... |
4009 | if (MiterLimit > 2) m_miterLim = 2/(MiterLimit * MiterLimit); |
4010 | else m_miterLim = 0.5; |
4011 | |
4012 | double y; |
4013 | if (ArcTolerance <= 0.0) y = def_arc_tolerance; |
4014 | else if (ArcTolerance > std::fabs(delta) * def_arc_tolerance) |
4015 | y = std::fabs(delta) * def_arc_tolerance; |
4016 | else y = ArcTolerance; |
4017 | //see offset_triginometry2.svg in the documentation folder ... |
4018 | double steps = pi / std::acos(1 - y / std::fabs(delta)); |
4019 | if (steps > std::fabs(delta) * pi) |
4020 | steps = std::fabs(delta) * pi; //ie excessive precision check |
4021 | m_sin = std::sin(two_pi / steps); |
4022 | m_cos = std::cos(two_pi / steps); |
4023 | m_StepsPerRad = steps / two_pi; |
4024 | if (delta < 0.0) m_sin = -m_sin; |
4025 | |
4026 | m_destPolys.reserve(m_polyNodes.ChildCount() * 2); |
4027 | for (int i = 0; i < m_polyNodes.ChildCount(); i++) |
4028 | { |
4029 | PolyNode& node = *m_polyNodes.Childs[i]; |
4030 | m_srcPoly = node.Contour; |
4031 | |
4032 | int len = (int)m_srcPoly.size(); |
4033 | if (len == 0 || (delta <= 0 && (len < 3 || node.m_endtype != etClosedPolygon))) |
4034 | continue; |
4035 | |
4036 | m_destPoly.clear(); |
4037 | if (len == 1) |
4038 | { |
4039 | if (node.m_jointype == jtRound) |
4040 | { |
4041 | double X = 1.0, Y = 0.0; |
4042 | for (cInt j = 1; j <= steps; j++) |
4043 | { |
4044 | m_destPoly.push_back(IntPoint( |
4045 | Round(m_srcPoly[0].X + X * delta), |
4046 | Round(m_srcPoly[0].Y + Y * delta))); |
4047 | double X2 = X; |
4048 | X = X * m_cos - m_sin * Y; |
4049 | Y = X2 * m_sin + Y * m_cos; |
4050 | } |
4051 | } |
4052 | else |
4053 | { |
4054 | double X = -1.0, Y = -1.0; |
4055 | for (int j = 0; j < 4; ++j) |
4056 | { |
4057 | m_destPoly.push_back(IntPoint( |
4058 | Round(m_srcPoly[0].X + X * delta), |
4059 | Round(m_srcPoly[0].Y + Y * delta))); |
4060 | if (X < 0) X = 1; |
4061 | else if (Y < 0) Y = 1; |
4062 | else X = -1; |
4063 | } |
4064 | } |
4065 | m_destPolys.push_back(m_destPoly); |
4066 | continue; |
4067 | } |
4068 | //build m_normals ... |
4069 | m_normals.clear(); |
4070 | m_normals.reserve(len); |
4071 | for (int j = 0; j < len - 1; ++j) |
4072 | m_normals.push_back(GetUnitNormal(m_srcPoly[j], m_srcPoly[j + 1])); |
4073 | if (node.m_endtype == etClosedLine || node.m_endtype == etClosedPolygon) |
4074 | m_normals.push_back(GetUnitNormal(m_srcPoly[len - 1], m_srcPoly[0])); |
4075 | else |
4076 | m_normals.push_back(DoublePoint(m_normals[len - 2])); |
4077 | |
4078 | if (node.m_endtype == etClosedPolygon) |
4079 | { |
4080 | int k = len - 1; |
4081 | for (int j = 0; j < len; ++j) |
4082 | OffsetPoint(j, k, node.m_jointype); |
4083 | m_destPolys.push_back(m_destPoly); |
4084 | } |
4085 | else if (node.m_endtype == etClosedLine) |
4086 | { |
4087 | int k = len - 1; |
4088 | for (int j = 0; j < len; ++j) |
4089 | OffsetPoint(j, k, node.m_jointype); |
4090 | m_destPolys.push_back(m_destPoly); |
4091 | m_destPoly.clear(); |
4092 | //re-build m_normals ... |
4093 | DoublePoint n = m_normals[len -1]; |
4094 | for (int j = len - 1; j > 0; j--) |
4095 | m_normals[j] = DoublePoint(-m_normals[j - 1].X, -m_normals[j - 1].Y); |
4096 | m_normals[0] = DoublePoint(-n.X, -n.Y); |
4097 | k = 0; |
4098 | for (int j = len - 1; j >= 0; j--) |
4099 | OffsetPoint(j, k, node.m_jointype); |
4100 | m_destPolys.push_back(m_destPoly); |
4101 | } |
4102 | else |
4103 | { |
4104 | int k = 0; |
4105 | for (int j = 1; j < len - 1; ++j) |
4106 | OffsetPoint(j, k, node.m_jointype); |
4107 | |
4108 | IntPoint pt1; |
4109 | if (node.m_endtype == etOpenButt) |
4110 | { |
4111 | int j = len - 1; |
4112 | pt1 = IntPoint((cInt)Round(m_srcPoly[j].X + m_normals[j].X * |
4113 | delta), (cInt)Round(m_srcPoly[j].Y + m_normals[j].Y * delta)); |
4114 | m_destPoly.push_back(pt1); |
4115 | pt1 = IntPoint((cInt)Round(m_srcPoly[j].X - m_normals[j].X * |
4116 | delta), (cInt)Round(m_srcPoly[j].Y - m_normals[j].Y * delta)); |
4117 | m_destPoly.push_back(pt1); |
4118 | } |
4119 | else |
4120 | { |
4121 | int j = len - 1; |
4122 | k = len - 2; |
4123 | m_sinA = 0; |
4124 | m_normals[j] = DoublePoint(-m_normals[j].X, -m_normals[j].Y); |
4125 | if (node.m_endtype == etOpenSquare) |
4126 | DoSquare(j, k); |
4127 | else |
4128 | DoRound(j, k); |
4129 | } |
4130 | |
4131 | //re-build m_normals ... |
4132 | for (int j = len - 1; j > 0; j--) |
4133 | m_normals[j] = DoublePoint(-m_normals[j - 1].X, -m_normals[j - 1].Y); |
4134 | m_normals[0] = DoublePoint(-m_normals[1].X, -m_normals[1].Y); |
4135 | |
4136 | k = len - 1; |
4137 | for (int j = k - 1; j > 0; --j) OffsetPoint(j, k, node.m_jointype); |
4138 | |
4139 | if (node.m_endtype == etOpenButt) |
4140 | { |
4141 | pt1 = IntPoint((cInt)Round(m_srcPoly[0].X - m_normals[0].X * delta), |
4142 | (cInt)Round(m_srcPoly[0].Y - m_normals[0].Y * delta)); |
4143 | m_destPoly.push_back(pt1); |
4144 | pt1 = IntPoint((cInt)Round(m_srcPoly[0].X + m_normals[0].X * delta), |
4145 | (cInt)Round(m_srcPoly[0].Y + m_normals[0].Y * delta)); |
4146 | m_destPoly.push_back(pt1); |
4147 | } |
4148 | else |
4149 | { |
4150 | k = 1; |
4151 | m_sinA = 0; |
4152 | if (node.m_endtype == etOpenSquare) |
4153 | DoSquare(0, 1); |
4154 | else |
4155 | DoRound(0, 1); |
4156 | } |
4157 | m_destPolys.push_back(m_destPoly); |
4158 | } |
4159 | } |
4160 | } |
4161 | //------------------------------------------------------------------------------ |
4162 | |
4163 | void ClipperOffset::OffsetPoint(int j, int& k, JoinType jointype) |
4164 | { |
4165 | //cross product ... |
4166 | m_sinA = (m_normals[k].X * m_normals[j].Y - m_normals[j].X * m_normals[k].Y); |
4167 | if (std::fabs(m_sinA * m_delta) < 1.0) |
4168 | { |
4169 | //dot product ... |
4170 | double cosA = (m_normals[k].X * m_normals[j].X + m_normals[j].Y * m_normals[k].Y ); |
4171 | if (cosA > 0) // angle => 0 degrees |
4172 | { |
4173 | m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[k].X * m_delta), |
4174 | Round(m_srcPoly[j].Y + m_normals[k].Y * m_delta))); |
4175 | return; |
4176 | } |
4177 | //else angle => 180 degrees |
4178 | } |
4179 | else if (m_sinA > 1.0) m_sinA = 1.0; |
4180 | else if (m_sinA < -1.0) m_sinA = -1.0; |
4181 | |
4182 | if (m_sinA * m_delta < 0) |
4183 | { |
4184 | m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[k].X * m_delta), |
4185 | Round(m_srcPoly[j].Y + m_normals[k].Y * m_delta))); |
4186 | m_destPoly.push_back(m_srcPoly[j]); |
4187 | m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + m_normals[j].X * m_delta), |
4188 | Round(m_srcPoly[j].Y + m_normals[j].Y * m_delta))); |
4189 | } |
4190 | else |
4191 | switch (jointype) |
4192 | { |
4193 | case jtMiter: |
4194 | { |
4195 | double r = 1 + (m_normals[j].X * m_normals[k].X + |
4196 | m_normals[j].Y * m_normals[k].Y); |
4197 | if (r >= m_miterLim) DoMiter(j, k, r); else DoSquare(j, k); |
4198 | break; |
4199 | } |
4200 | case jtSquare: DoSquare(j, k); break; |
4201 | case jtRound: DoRound(j, k); break; |
4202 | } |
4203 | k = j; |
4204 | } |
4205 | //------------------------------------------------------------------------------ |
4206 | |
4207 | void ClipperOffset::DoSquare(int j, int k) |
4208 | { |
4209 | double dx = std::tan(std::atan2(m_sinA, |
4210 | m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y) / 4); |
4211 | m_destPoly.push_back(IntPoint( |
4212 | Round(m_srcPoly[j].X + m_delta * (m_normals[k].X - m_normals[k].Y * dx)), |
4213 | Round(m_srcPoly[j].Y + m_delta * (m_normals[k].Y + m_normals[k].X * dx)))); |
4214 | m_destPoly.push_back(IntPoint( |
4215 | Round(m_srcPoly[j].X + m_delta * (m_normals[j].X + m_normals[j].Y * dx)), |
4216 | Round(m_srcPoly[j].Y + m_delta * (m_normals[j].Y - m_normals[j].X * dx)))); |
4217 | } |
4218 | //------------------------------------------------------------------------------ |
4219 | |
4220 | void ClipperOffset::DoMiter(int j, int k, double r) |
4221 | { |
4222 | double q = m_delta / r; |
4223 | m_destPoly.push_back(IntPoint(Round(m_srcPoly[j].X + (m_normals[k].X + m_normals[j].X) * q), |
4224 | Round(m_srcPoly[j].Y + (m_normals[k].Y + m_normals[j].Y) * q))); |
4225 | } |
4226 | //------------------------------------------------------------------------------ |
4227 | |
4228 | void ClipperOffset::DoRound(int j, int k) |
4229 | { |
4230 | double a = std::atan2(m_sinA, |
4231 | m_normals[k].X * m_normals[j].X + m_normals[k].Y * m_normals[j].Y); |
4232 | int steps = std::max((int)Round(m_StepsPerRad * std::fabs(a)), 1); |
4233 | |
4234 | double X = m_normals[k].X, Y = m_normals[k].Y, X2; |
4235 | for (int i = 0; i < steps; ++i) |
4236 | { |
4237 | m_destPoly.push_back(IntPoint( |
4238 | Round(m_srcPoly[j].X + X * m_delta), |
4239 | Round(m_srcPoly[j].Y + Y * m_delta))); |
4240 | X2 = X; |
4241 | X = X * m_cos - m_sin * Y; |
4242 | Y = X2 * m_sin + Y * m_cos; |
4243 | } |
4244 | m_destPoly.push_back(IntPoint( |
4245 | Round(m_srcPoly[j].X + m_normals[j].X * m_delta), |
4246 | Round(m_srcPoly[j].Y + m_normals[j].Y * m_delta))); |
4247 | } |
4248 | |
4249 | //------------------------------------------------------------------------------ |
4250 | // Miscellaneous public functions |
4251 | //------------------------------------------------------------------------------ |
4252 | |
4253 | void Clipper::DoSimplePolygons() |
4254 | { |
4255 | PolyOutList::size_type i = 0; |
4256 | while (i < m_PolyOuts.size()) |
4257 | { |
4258 | OutRec* outrec = m_PolyOuts[i++]; |
4259 | OutPt* op = outrec->Pts; |
4260 | if (!op || outrec->IsOpen) continue; |
4261 | do //for each Pt in Polygon until duplicate found do ... |
4262 | { |
4263 | OutPt* op2 = op->Next; |
4264 | while (op2 != outrec->Pts) |
4265 | { |
4266 | if ((op->Pt == op2->Pt) && op2->Next != op && op2->Prev != op) |
4267 | { |
4268 | //split the polygon into two ... |
4269 | OutPt* op3 = op->Prev; |
4270 | OutPt* op4 = op2->Prev; |
4271 | op->Prev = op4; |
4272 | op4->Next = op; |
4273 | op2->Prev = op3; |
4274 | op3->Next = op2; |
4275 | |
4276 | outrec->Pts = op; |
4277 | OutRec* outrec2 = CreateOutRec(); |
4278 | outrec2->Pts = op2; |
4279 | UpdateOutPtIdxs(*outrec2); |
4280 | if (Poly2ContainsPoly1(outrec2->Pts, outrec->Pts)) |
4281 | { |
4282 | //OutRec2 is contained by OutRec1 ... |
4283 | outrec2->IsHole = !outrec->IsHole; |
4284 | outrec2->FirstLeft = outrec; |
4285 | if (m_UsingPolyTree) FixupFirstLefts2(outrec2, outrec); |
4286 | } |
4287 | else |
4288 | if (Poly2ContainsPoly1(outrec->Pts, outrec2->Pts)) |
4289 | { |
4290 | //OutRec1 is contained by OutRec2 ... |
4291 | outrec2->IsHole = outrec->IsHole; |
4292 | outrec->IsHole = !outrec2->IsHole; |
4293 | outrec2->FirstLeft = outrec->FirstLeft; |
4294 | outrec->FirstLeft = outrec2; |
4295 | if (m_UsingPolyTree) FixupFirstLefts2(outrec, outrec2); |
4296 | } |
4297 | else |
4298 | { |
4299 | //the 2 polygons are separate ... |
4300 | outrec2->IsHole = outrec->IsHole; |
4301 | outrec2->FirstLeft = outrec->FirstLeft; |
4302 | if (m_UsingPolyTree) FixupFirstLefts1(outrec, outrec2); |
4303 | } |
4304 | op2 = op; //ie get ready for the Next iteration |
4305 | } |
4306 | op2 = op2->Next; |
4307 | } |
4308 | op = op->Next; |
4309 | } |
4310 | while (op != outrec->Pts); |
4311 | } |
4312 | } |
4313 | //------------------------------------------------------------------------------ |
4314 | |
4315 | void ReversePath(Path& p) |
4316 | { |
4317 | std::reverse(p.begin(), p.end()); |
4318 | } |
4319 | //------------------------------------------------------------------------------ |
4320 | |
4321 | void ReversePaths(Paths& p) |
4322 | { |
4323 | for (Paths::size_type i = 0; i < p.size(); ++i) |
4324 | ReversePath(p[i]); |
4325 | } |
4326 | //------------------------------------------------------------------------------ |
4327 | |
4328 | void SimplifyPolygon(const Path &in_poly, Paths &out_polys, PolyFillType fillType) |
4329 | { |
4330 | Clipper c; |
4331 | c.StrictlySimple(true); |
4332 | c.AddPath(in_poly, ptSubject, true); |
4333 | c.Execute(ctUnion, out_polys, fillType, fillType); |
4334 | } |
4335 | //------------------------------------------------------------------------------ |
4336 | |
4337 | void SimplifyPolygons(const Paths &in_polys, Paths &out_polys, PolyFillType fillType) |
4338 | { |
4339 | Clipper c; |
4340 | c.StrictlySimple(true); |
4341 | c.AddPaths(in_polys, ptSubject, true); |
4342 | c.Execute(ctUnion, out_polys, fillType, fillType); |
4343 | } |
4344 | //------------------------------------------------------------------------------ |
4345 | |
4346 | void SimplifyPolygons(Paths &polys, PolyFillType fillType) |
4347 | { |
4348 | SimplifyPolygons(polys, polys, fillType); |
4349 | } |
4350 | //------------------------------------------------------------------------------ |
4351 | |
4352 | inline double DistanceSqrd(const IntPoint& pt1, const IntPoint& pt2) |
4353 | { |
4354 | double Dx = ((double)pt1.X - pt2.X); |
4355 | double dy = ((double)pt1.Y - pt2.Y); |
4356 | return (Dx*Dx + dy*dy); |
4357 | } |
4358 | //------------------------------------------------------------------------------ |
4359 | |
4360 | double DistanceFromLineSqrd( |
4361 | const IntPoint& pt, const IntPoint& ln1, const IntPoint& ln2) |
4362 | { |
4363 | //The equation of a line in general form (Ax + By + C = 0) |
4364 | //given 2 points (x¹,y¹) & (x²,y²) is ... |
4365 | //(y¹ - y²)x + (x² - x¹)y + (y² - y¹)x¹ - (x² - x¹)y¹ = 0 |
4366 | //A = (y¹ - y²); B = (x² - x¹); C = (y² - y¹)x¹ - (x² - x¹)y¹ |
4367 | //perpendicular distance of point (x³,y³) = (Ax³ + By³ + C)/Sqrt(A² + B²) |
4368 | //see http://en.wikipedia.org/wiki/Perpendicular_distance |
4369 | double A = double(ln1.Y - ln2.Y); |
4370 | double B = double(ln2.X - ln1.X); |
4371 | double C = A * ln1.X + B * ln1.Y; |
4372 | C = A * pt.X + B * pt.Y - C; |
4373 | return (C * C) / (A * A + B * B); |
4374 | } |
4375 | //--------------------------------------------------------------------------- |
4376 | |
4377 | bool SlopesNearCollinear(const IntPoint& pt1, |
4378 | const IntPoint& pt2, const IntPoint& pt3, double distSqrd) |
4379 | { |
4380 | //this function is more accurate when the point that's geometrically |
4381 | //between the other 2 points is the one that's tested for distance. |
4382 | //ie makes it more likely to pick up 'spikes' ... |
4383 | if (Abs(pt1.X - pt2.X) > Abs(pt1.Y - pt2.Y)) |
4384 | { |
4385 | if ((pt1.X > pt2.X) == (pt1.X < pt3.X)) |
4386 | return DistanceFromLineSqrd(pt1, pt2, pt3) < distSqrd; |
4387 | else if ((pt2.X > pt1.X) == (pt2.X < pt3.X)) |
4388 | return DistanceFromLineSqrd(pt2, pt1, pt3) < distSqrd; |
4389 | else |
4390 | return DistanceFromLineSqrd(pt3, pt1, pt2) < distSqrd; |
4391 | } |
4392 | else |
4393 | { |
4394 | if ((pt1.Y > pt2.Y) == (pt1.Y < pt3.Y)) |
4395 | return DistanceFromLineSqrd(pt1, pt2, pt3) < distSqrd; |
4396 | else if ((pt2.Y > pt1.Y) == (pt2.Y < pt3.Y)) |
4397 | return DistanceFromLineSqrd(pt2, pt1, pt3) < distSqrd; |
4398 | else |
4399 | return DistanceFromLineSqrd(pt3, pt1, pt2) < distSqrd; |
4400 | } |
4401 | } |
4402 | //------------------------------------------------------------------------------ |
4403 | |
4404 | bool PointsAreClose(IntPoint pt1, IntPoint pt2, double distSqrd) |
4405 | { |
4406 | double Dx = (double)pt1.X - pt2.X; |
4407 | double dy = (double)pt1.Y - pt2.Y; |
4408 | return ((Dx * Dx) + (dy * dy) <= distSqrd); |
4409 | } |
4410 | //------------------------------------------------------------------------------ |
4411 | |
4412 | OutPt* ExcludeOp(OutPt* op) |
4413 | { |
4414 | OutPt* result = op->Prev; |
4415 | result->Next = op->Next; |
4416 | op->Next->Prev = result; |
4417 | result->Idx = 0; |
4418 | return result; |
4419 | } |
4420 | //------------------------------------------------------------------------------ |
4421 | |
4422 | void CleanPolygon(const Path& in_poly, Path& out_poly, double distance) |
4423 | { |
4424 | //distance = proximity in units/pixels below which vertices |
4425 | //will be stripped. Default ~= sqrt(2). |
4426 | |
4427 | size_t size = in_poly.size(); |
4428 | |
4429 | if (size == 0) |
4430 | { |
4431 | out_poly.clear(); |
4432 | return; |
4433 | } |
4434 | |
4435 | OutPt* outPts = new OutPt[size]; |
4436 | for (size_t i = 0; i < size; ++i) |
4437 | { |
4438 | outPts[i].Pt = in_poly[i]; |
4439 | outPts[i].Next = &outPts[(i + 1) % size]; |
4440 | outPts[i].Next->Prev = &outPts[i]; |
4441 | outPts[i].Idx = 0; |
4442 | } |
4443 | |
4444 | double distSqrd = distance * distance; |
4445 | OutPt* op = &outPts[0]; |
4446 | while (op->Idx == 0 && op->Next != op->Prev) |
4447 | { |
4448 | if (PointsAreClose(op->Pt, op->Prev->Pt, distSqrd)) |
4449 | { |
4450 | op = ExcludeOp(op); |
4451 | size--; |
4452 | } |
4453 | else if (PointsAreClose(op->Prev->Pt, op->Next->Pt, distSqrd)) |
4454 | { |
4455 | ExcludeOp(op->Next); |
4456 | op = ExcludeOp(op); |
4457 | size -= 2; |
4458 | } |
4459 | else if (SlopesNearCollinear(op->Prev->Pt, op->Pt, op->Next->Pt, distSqrd)) |
4460 | { |
4461 | op = ExcludeOp(op); |
4462 | size--; |
4463 | } |
4464 | else |
4465 | { |
4466 | op->Idx = 1; |
4467 | op = op->Next; |
4468 | } |
4469 | } |
4470 | |
4471 | if (size < 3) size = 0; |
4472 | out_poly.resize(size); |
4473 | for (size_t i = 0; i < size; ++i) |
4474 | { |
4475 | out_poly[i] = op->Pt; |
4476 | op = op->Next; |
4477 | } |
4478 | delete [] outPts; |
4479 | } |
4480 | //------------------------------------------------------------------------------ |
4481 | |
4482 | void CleanPolygon(Path& poly, double distance) |
4483 | { |
4484 | CleanPolygon(poly, poly, distance); |
4485 | } |
4486 | //------------------------------------------------------------------------------ |
4487 | |
4488 | void CleanPolygons(const Paths& in_polys, Paths& out_polys, double distance) |
4489 | { |
4490 | out_polys.resize(in_polys.size()); |
4491 | for (Paths::size_type i = 0; i < in_polys.size(); ++i) |
4492 | CleanPolygon(in_polys[i], out_polys[i], distance); |
4493 | } |
4494 | //------------------------------------------------------------------------------ |
4495 | |
4496 | void CleanPolygons(Paths& polys, double distance) |
4497 | { |
4498 | CleanPolygons(polys, polys, distance); |
4499 | } |
4500 | //------------------------------------------------------------------------------ |
4501 | |
4502 | void Minkowski(const Path& poly, const Path& path, |
4503 | Paths& solution, bool isSum, bool isClosed) |
4504 | { |
4505 | int delta = (isClosed ? 1 : 0); |
4506 | size_t polyCnt = poly.size(); |
4507 | size_t pathCnt = path.size(); |
4508 | Paths pp; |
4509 | pp.reserve(pathCnt); |
4510 | if (isSum) |
4511 | for (size_t i = 0; i < pathCnt; ++i) |
4512 | { |
4513 | Path p; |
4514 | p.reserve(polyCnt); |
4515 | for (size_t j = 0; j < poly.size(); ++j) |
4516 | p.push_back(IntPoint(path[i].X + poly[j].X, path[i].Y + poly[j].Y)); |
4517 | pp.push_back(p); |
4518 | } |
4519 | else |
4520 | for (size_t i = 0; i < pathCnt; ++i) |
4521 | { |
4522 | Path p; |
4523 | p.reserve(polyCnt); |
4524 | for (size_t j = 0; j < poly.size(); ++j) |
4525 | p.push_back(IntPoint(path[i].X - poly[j].X, path[i].Y - poly[j].Y)); |
4526 | pp.push_back(p); |
4527 | } |
4528 | |
4529 | solution.clear(); |
4530 | solution.reserve((pathCnt + delta) * (polyCnt + 1)); |
4531 | for (size_t i = 0; i < pathCnt - 1 + delta; ++i) |
4532 | for (size_t j = 0; j < polyCnt; ++j) |
4533 | { |
4534 | Path quad; |
4535 | quad.reserve(4); |
4536 | quad.push_back(pp[i % pathCnt][j % polyCnt]); |
4537 | quad.push_back(pp[(i + 1) % pathCnt][j % polyCnt]); |
4538 | quad.push_back(pp[(i + 1) % pathCnt][(j + 1) % polyCnt]); |
4539 | quad.push_back(pp[i % pathCnt][(j + 1) % polyCnt]); |
4540 | if (!Orientation(quad)) ReversePath(quad); |
4541 | solution.push_back(quad); |
4542 | } |
4543 | } |
4544 | //------------------------------------------------------------------------------ |
4545 | |
4546 | void MinkowskiSum(const Path& pattern, const Path& path, Paths& solution, bool pathIsClosed) |
4547 | { |
4548 | Minkowski(pattern, path, solution, true, pathIsClosed); |
4549 | Clipper c; |
4550 | c.AddPaths(solution, ptSubject, true); |
4551 | c.Execute(ctUnion, solution, pftNonZero, pftNonZero); |
4552 | } |
4553 | //------------------------------------------------------------------------------ |
4554 | |
4555 | void TranslatePath(const Path& input, Path& output, const IntPoint delta) |
4556 | { |
4557 | //precondition: input != output |
4558 | output.resize(input.size()); |
4559 | for (size_t i = 0; i < input.size(); ++i) |
4560 | output[i] = IntPoint(input[i].X + delta.X, input[i].Y + delta.Y); |
4561 | } |
4562 | //------------------------------------------------------------------------------ |
4563 | |
4564 | void MinkowskiSum(const Path& pattern, const Paths& paths, Paths& solution, bool pathIsClosed) |
4565 | { |
4566 | Clipper c; |
4567 | for (size_t i = 0; i < paths.size(); ++i) |
4568 | { |
4569 | Paths tmp; |
4570 | Minkowski(pattern, paths[i], tmp, true, pathIsClosed); |
4571 | c.AddPaths(tmp, ptSubject, true); |
4572 | if (pathIsClosed) |
4573 | { |
4574 | Path tmp2; |
4575 | TranslatePath(paths[i], tmp2, pattern[0]); |
4576 | c.AddPath(tmp2, ptClip, true); |
4577 | } |
4578 | } |
4579 | c.Execute(ctUnion, solution, pftNonZero, pftNonZero); |
4580 | } |
4581 | //------------------------------------------------------------------------------ |
4582 | |
4583 | void MinkowskiDiff(const Path& poly1, const Path& poly2, Paths& solution) |
4584 | { |
4585 | Minkowski(poly1, poly2, solution, false, true); |
4586 | Clipper c; |
4587 | c.AddPaths(solution, ptSubject, true); |
4588 | c.Execute(ctUnion, solution, pftNonZero, pftNonZero); |
4589 | } |
4590 | //------------------------------------------------------------------------------ |
4591 | |
4592 | enum NodeType {ntAny, ntOpen, ntClosed}; |
4593 | |
4594 | void AddPolyNodeToPaths(const PolyNode& polynode, NodeType nodetype, Paths& paths) |
4595 | { |
4596 | bool match = true; |
4597 | if (nodetype == ntClosed) match = !polynode.IsOpen(); |
4598 | else if (nodetype == ntOpen) return; |
4599 | |
4600 | if (!polynode.Contour.empty() && match) |
4601 | paths.push_back(polynode.Contour); |
4602 | for (int i = 0; i < polynode.ChildCount(); ++i) |
4603 | AddPolyNodeToPaths(*polynode.Childs[i], nodetype, paths); |
4604 | } |
4605 | //------------------------------------------------------------------------------ |
4606 | |
4607 | void PolyTreeToPaths(const PolyTree& polytree, Paths& paths) |
4608 | { |
4609 | paths.resize(0); |
4610 | paths.reserve(polytree.Total()); |
4611 | AddPolyNodeToPaths(polytree, ntAny, paths); |
4612 | } |
4613 | //------------------------------------------------------------------------------ |
4614 | |
4615 | void ClosedPathsFromPolyTree(const PolyTree& polytree, Paths& paths) |
4616 | { |
4617 | paths.resize(0); |
4618 | paths.reserve(polytree.Total()); |
4619 | AddPolyNodeToPaths(polytree, ntClosed, paths); |
4620 | } |
4621 | //------------------------------------------------------------------------------ |
4622 | |
4623 | void OpenPathsFromPolyTree(PolyTree& polytree, Paths& paths) |
4624 | { |
4625 | paths.resize(0); |
4626 | paths.reserve(polytree.Total()); |
4627 | //Open paths are top level only, so ... |
4628 | for (int i = 0; i < polytree.ChildCount(); ++i) |
4629 | if (polytree.Childs[i]->IsOpen()) |
4630 | paths.push_back(polytree.Childs[i]->Contour); |
4631 | } |
4632 | //------------------------------------------------------------------------------ |
4633 | |
4634 | std::ostream& operator <<(std::ostream &s, const IntPoint &p) |
4635 | { |
4636 | s << "(" << p.X << "," << p.Y << ")" ; |
4637 | return s; |
4638 | } |
4639 | //------------------------------------------------------------------------------ |
4640 | |
4641 | std::ostream& operator <<(std::ostream &s, const Path &p) |
4642 | { |
4643 | if (p.empty()) return s; |
4644 | Path::size_type last = p.size() -1; |
4645 | for (Path::size_type i = 0; i < last; i++) |
4646 | s << "(" << p[i].X << "," << p[i].Y << "), " ; |
4647 | s << "(" << p[last].X << "," << p[last].Y << ")\n" ; |
4648 | return s; |
4649 | } |
4650 | //------------------------------------------------------------------------------ |
4651 | |
4652 | std::ostream& operator <<(std::ostream &s, const Paths &p) |
4653 | { |
4654 | for (Paths::size_type i = 0; i < p.size(); i++) |
4655 | s << p[i]; |
4656 | s << "\n" ; |
4657 | return s; |
4658 | } |
4659 | //------------------------------------------------------------------------------ |
4660 | |
4661 | } //ClipperLib namespace |
4662 | |