forked from StagPoint/clipperlib-refactored
/
ClipperSupportClasses.cs
606 lines (492 loc) · 14.1 KB
/
ClipperSupportClasses.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
/*******************************************************************************
* *
* Author : Angus Johnson *
* Version : 6.4.2 *
* Date : 27 February 2017 *
* Website : http://www.angusj.com *
* Copyright : Angus Johnson 2010-2017 *
* *
* License: *
* Use, modification & distribution is subject to Boost Software License Ver 1. *
* http://www.boost.org/LICENSE_1_0.txt *
* *
* Attributions: *
* The code in this library is an extension of Bala Vatti's clipping algorithm: *
* "A generic solution to polygon clipping" *
* Communications of the ACM, Vol 35, Issue 7 (July 1992) pp 56-63. *
* http://portal.acm.org/citation.cfm?id=129906 *
* *
* Computer graphics and geometric modeling: implementation and algorithms *
* By Max K. Agoston *
* Springer; 1 edition (January 4, 2005) *
* http://books.google.com/books?q=vatti+clipping+agoston *
* *
* See also: *
* "Polygon Offsetting by Computing Winding Numbers" *
* Paper no. DETC2005-85513 pp. 565-575 *
* ASME 2005 International Design Engineering Technical Conferences *
* and Computers and Information in Engineering Conference (IDETC/CIE2005) *
* September 24-28, 2005 , Long Beach, California, USA *
* http://www.me.berkeley.edu/~mcmains/pubs/DAC05OffsetPolygon.pdf *
* *
*******************************************************************************/
/*******************************************************************************
* *
* This is a translation of the Delphi Clipper library and the naming style *
* used has retained a Delphi flavour. *
* *
*******************************************************************************/
/*******************************************************************************
*
* NOTE: This version of Clipper has been heavily modified by StagPoint Software
* to best match the current project and use cases. The original single-file
* library has been split into multiple files, any classes or functions not
* used by StagPoint have been deleted, and numerous (mostly insignificant) changes
* have been made to the codebase.
*
*******************************************************************************/
namespace ClipperLib
{
using System;
using System.Collections.Generic;
using cInt = System.Int64;
public class Path : List<IntPoint>
{
public Path()
: base()
{
}
public Path( int capacity )
: base( capacity )
{
}
public override string ToString()
{
return string.Format( "Count: {0}", this.Count );
}
}
internal class EdgeList : List<TEdge>
{
public EdgeList()
: base()
{
}
public EdgeList( int capacity )
: base( capacity )
{
}
public override string ToString()
{
return string.Format( "Count: {0}", this.Count );
}
}
public class PolyTree : PolyNode
{
internal List<PolyNode> AllPolygons = new List<PolyNode>();
public override void Clear()
{
for( int i = 0; i < AllPolygons.Count; i++ )
{
AllPolygons[ i ] = null;
}
AllPolygons.Clear();
base.Clear();
}
public PolyNode GetFirst()
{
if( m_childNodes.Count > 0 )
return m_childNodes[ 0 ];
else
return null;
}
public int Total
{
get
{
int result = AllPolygons.Count;
//with negative offsets, ignore the hidden outer polygon ...
if( result > 0 && m_childNodes[ 0 ] != AllPolygons[ 0 ] )
result--;
return result;
}
}
}
public class PolyNode : IPooledObject
{
protected PolyNode m_parent;
protected Path m_polygon = new Path();
protected int m_index;
protected List<PolyNode> m_childNodes = new List<PolyNode>();
public List<PolyNode> ChildNodes
{
get { return m_childNodes; }
}
public PolyNode Parent
{
get { return m_parent; }
}
public bool IsHole
{
get { return IsHoleNode(); }
}
public bool IsOpen { get; set; }
public int ChildCount
{
get { return m_childNodes.Count; }
}
public Path Contour
{
get { return m_polygon; }
}
public void AddChild( PolyNode Child )
{
int cnt = m_childNodes.Count;
m_childNodes.Add( Child );
Child.m_parent = this;
Child.m_index = cnt;
}
public PolyNode GetNext()
{
if( m_childNodes.Count > 0 )
return m_childNodes[ 0 ];
else
return GetNextSiblingUp();
}
internal PolyNode GetNextSiblingUp()
{
if( m_parent == null )
return null;
else if( m_index == m_parent.m_childNodes.Count - 1 )
return m_parent.GetNextSiblingUp();
else
return m_parent.m_childNodes[ m_index + 1 ];
}
private bool IsHoleNode()
{
bool result = true;
PolyNode node = m_parent;
while( node != null )
{
result = !result;
node = node.m_parent;
}
return result;
}
public virtual void Clear()
{
for( int i = 0; i < m_childNodes.Count; i++ )
{
m_childNodes[ i ].Clear();
}
m_childNodes.Clear();
m_parent = null;
m_polygon.Clear();
}
internal void EnsureChildCapacity( int count )
{
m_childNodes.Capacity = Math.Max( m_childNodes.Capacity, count );
}
internal void EnsurePolygonCapacity( int count )
{
m_polygon.Capacity = Math.Max( m_polygon.Capacity, count );
}
internal void AddToPolygon( ref IntPoint intPoint )
{
m_polygon.Add( intPoint );
}
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
Clear();
}
#endregion
}
internal class TEdge : IPooledObject
{
internal IntPoint Bot;
internal IntPoint Curr; //current (updated for every new scanbeam)
internal IntPoint Top;
internal IntPoint Delta;
internal double Dx;
internal PolyType PolyTyp;
internal EdgeSide Side; //side only refers to current side of solution poly
internal int WindDelta; //1 or -1 depending on winding direction
internal int WindCnt;
internal int WindCnt2; //winding count of the opposite polytype
internal int OutIdx;
internal TEdge Next;
internal TEdge Prev;
internal TEdge NextInLML;
internal TEdge NextInAEL;
internal TEdge PrevInAEL;
internal TEdge NextInSEL;
internal TEdge PrevInSEL;
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
PolyTyp = PolyType.Subject;
Side = EdgeSide.Left;
Bot = Curr = Top = Delta = new IntPoint();
Dx = 0;
WindDelta = 0;
WindCnt = 0;
WindCnt2 = 0;
OutIdx = 0;
Next = Prev = null;
PrevInAEL = PrevInSEL = null;
NextInLML = NextInAEL = NextInSEL = null;
}
#endregion
#region Enforced object pooling
private TEdge()
{
// Force instantiation to occur through the object pool
}
internal class Pool : LocalObjectPool<TEdge>
{
protected override TEdge AllocateNewInstance()
{
return new TEdge();
}
}
#endregion
}
internal class LocalMinima : IPooledObject
{
internal cInt Y;
internal TEdge LeftBound;
internal TEdge RightBound;
internal LocalMinima Next;
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
Y = new cInt();
LeftBound = RightBound = null;
Next = null;
}
#endregion
#region Enforced object pooling
private LocalMinima()
{
// Force instantiation to occur through the object pool
}
internal class Pool : LocalObjectPool<LocalMinima>
{
protected override LocalMinima AllocateNewInstance()
{
return new LocalMinima();
}
}
#endregion
}
internal class Scanbeam : IPooledObject
{
internal cInt Y;
internal Scanbeam Next;
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
Y = new cInt();
Next = null;
}
#endregion
#region Enforced object pooling
private Scanbeam()
{
// Force instantiation to occur through the object pool
}
internal class Pool : LocalObjectPool<Scanbeam>
{
protected override Scanbeam AllocateNewInstance()
{
return new Scanbeam();
}
}
#endregion
}
internal class Maxima : IPooledObject
{
internal cInt X;
internal Maxima Next;
internal Maxima Prev;
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
X = new cInt();
Next = Prev = null;
}
#endregion
#region Enforced object pooling
private Maxima()
{
// Force instantiation to occur through the object pool
}
internal class Pool : LocalObjectPool<Maxima>
{
protected override Maxima AllocateNewInstance()
{
return new Maxima();
}
}
#endregion
}
//OutRec: contains a path in the clipping solution. Edges in the AEL will
//carry a pointer to an OutRec when they are part of the clipping solution.
internal class OutRec : IPooledObject
{
internal int Idx;
internal bool IsHole;
internal bool IsOpen;
internal OutRec FirstLeft; //see comments in clipper.pas
internal OutPt Pts;
internal OutPt BottomPt;
internal PolyNode PolyNode;
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
Idx = 0;
IsHole = IsOpen = false;
FirstLeft = null;
Pts = BottomPt = null;
PolyNode = null;
}
#endregion
#region Enforced object pooling
private OutRec()
{
// Force instantiation to occur through the object pool
}
internal class Pool : LocalObjectPool<OutRec>
{
protected override OutRec AllocateNewInstance()
{
return new OutRec();
}
}
#endregion
}
internal class OutPt : IPooledObject
{
internal int Idx;
internal IntPoint Pt;
internal OutPt Next;
internal OutPt Prev;
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
Idx = 0;
Pt = new IntPoint();
Next = Prev = null;
}
#endregion
#region Enforced object pooling
private OutPt()
{
// Force instantiation to occur through the object pool
}
internal class Pool : LocalObjectPool<OutPt>
{
protected override OutPt AllocateNewInstance()
{
return new OutPt();
}
}
#endregion
}
internal class Join : IPooledObject
{
internal OutPt OutPt1;
internal OutPt OutPt2;
internal IntPoint OffPt;
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
OutPt1 = null;
OutPt2 = null;
OffPt = new IntPoint();
}
#endregion
#region Enforced object pooling
private Join()
{
// Force instantiation to occur through the object pool
}
internal class Pool : LocalObjectPool<Join>
{
protected override Join AllocateNewInstance()
{
return new Join();
}
}
#endregion
}
internal class IntersectNode : IPooledObject
{
internal TEdge Edge1;
internal TEdge Edge2;
internal IntPoint Pt;
#region IPooledObject Members
/// <summary>
/// Reset all fields to default values in preparation for object recycling
/// </summary>
public void PrepareForRecycle()
{
Edge1 = null;
Edge2 = null;
Pt = new IntPoint();
}
#endregion
#region Nested types
public struct Comparer : IComparer<IntersectNode>
{
public int Compare( IntersectNode node1, IntersectNode node2 )
{
cInt i = node2.Pt.Y - node1.Pt.Y;
if( i > 0 )
return 1;
else if( i < 0 )
return -1;
else
return 0;
}
}
#endregion
#region Enforced object pooling
private IntersectNode()
{
// Force instantiation to occur through the object pool
}
internal class Pool : LocalObjectPool<IntersectNode>
{
protected override IntersectNode AllocateNewInstance()
{
return new IntersectNode();
}
}
#endregion
}
}