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