-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdist.js
1624 lines (1449 loc) · 45.9 KB
/
dist.js
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
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
window.customturf = {
union: require('turf-union'),
intersect: require('turf-intersect'),
difference: require('turf-difference')
};
},{"turf-difference":2,"turf-intersect":13,"turf-union":24}],2:[function(require,module,exports){
var gh = require('gh-clipping-algorithm');
/**
* Finds the difference between two {@link Polygon|polygons} by clipping the second
* polygon from the first.
*
* @module turf/difference
* @category transformation
* @param {Feature<Polygon>} poly1 input Polygon feaure
* @param {Feature<Polygon>} poly2 Polygon feature to difference from `poly1`
* @return {Feature<Polygon>} a Polygon feature showing the area of `poly1` excluding the area of `poly2`
* @example
* var poly1 = {
* "type": "Feature",
* "properties": {
* "fill": "#0f0"
* },
* "geometry": {
* "type": "Polygon",
* "coordinates": [[
* [-46.738586, -23.596711],
* [-46.738586, -23.458207],
* [-46.560058, -23.458207],
* [-46.560058, -23.596711],
* [-46.738586, -23.596711]
* ]]
* }
* };
* var poly2 = {
* "type": "Feature",
* "properties": {
* "fill": "#00f"
* },
* "geometry": {
* "type": "Polygon",
* "coordinates": [[
* [-46.650009, -23.631314],
* [-46.650009, -23.5237],
* [-46.509246, -23.5237],
* [-46.509246, -23.631314],
* [-46.650009, -23.631314]
* ]]
* }
* };
*
* var differenced = turf.difference(poly1, poly2);
* differenced.properties.fill = '#f00';
*
* var polygons = {
* "type": "FeatureCollection",
* "features": [poly1, poly2]
* };
*
* //=polygons
*
* //=differenced
*/
module.exports = function(poly1, poly2) {
// console.log(poly1);
var a = poly1.coordinates ? poly1.coordinates : poly1.geometry.coordinates;
var b = poly2.coordinates ? poly2.coordinates : poly2.geometry.coordinates;
var u = gh.subtract(a, b);
var feature = {
"type": "Feature",
"properties": {},
"geometry": {}
};
if (!u || u.length == 0) {
return undefined;
}
if (gh.utils.isMultiPolygon(u)) {
if (u.length > 1) {
feature.geometry.type = "MultiPolygon";
feature.geometry.coordinates = u;
} else {
feature.geometry.type = "Polygon";
feature.geometry.coordinates = u[0];
}
} else if (gh.utils.isPolygon(u)) {
feature.geometry.type = "Polygon";
feature.geometry.coordinates = u;
}
if (poly1.properties) {
feature.properties = poly1.properties;
}
return feature;
};
},{"gh-clipping-algorithm":3}],3:[function(require,module,exports){
module.exports = {
union: require('./lib/union'),
intersect: require('./lib/intersect'),
subtract: require('./lib/subtract'),
// include utils to make things easier
utils: require('./lib/util')
};
},{"./lib/intersect":5,"./lib/subtract":7,"./lib/union":8,"./lib/util":9}],4:[function(require,module,exports){
var Ring = require('./ring');
var Vertex = require('./vertex');
var pip = require('point-in-polygon');
var util = require('./util');
/**
* Greiner-Hormann clipping uses 3 phases:
*
* 1. Find intersection vertices, build data structure
* 2. Mark vertices as entry/exit points
* 3. Build Polygons
*
* We additionally add in some special case handling
* after intersection detection, because GH won't handle
* cases where one of the polygons fully encloses the other,
* or the two polygons are totally disjoint
*/
module.exports = function(subject, clipper, s_forward, c_forward) {
s_forward = !!s_forward;
c_forward = !!c_forward;
var mode = detectMode(s_forward, c_forward);
var sPoints = Ring.fromArray(subject);
var cPoints = Ring.fromArray(clipper);
/**
* PHASE ONE: Identify Intersections
*/
var vertices = buildIntersectionLists(sPoints, cPoints, subject, clipper);
markDegensAsIntersect(sPoints);
/**
* OPTIMIZATION / EDGE CASES: check for known cases where we can bail out early
*/
var maybeResult;
if (maybeResult = checkQuitCases(sPoints, cPoints, subject, clipper, mode)) {
return maybeResult;
}
/**
* PHASE TWO: Identify Entry/Exit (Includes Degeneracy labelling logic)
*/
setEntryExit(sPoints);
/**
* PHASE THREE: Build clipped polys
*/
return buildPolygons(sPoints, s_forward, c_forward);
}
/**
* s_forward and c_forward can be manipulated to change the operation
* applied to the subject / clipper. This method provides a string
* representation of the mode for easy reference.
*
* Operation | s_forward | c_forward
* -----------------------------------------------
* Union | false | false
* Intersect | true | true
* Subtract (A - B) | false | true
* Subtract (B - A) | true | false
*
* @param {bool} s_forward whether to traverse the subject polygon in forward order
* @param {bool} c_forward whether to traverse the clip polygon in forward order
* @return {string} the string description of the selected clip mode
*/
function detectMode(s_forward, c_forward) {
if (!s_forward && !c_forward) {
mode = 'union';
} else if (s_forward && c_forward) {
mode = 'intersect';
} else if (!s_forward && c_forward) {
mode = 'subtractB'; // A - B
} else if (s_forward && !c_forward) {
mode = 'subtractA'; // B - A
}
return mode;
}
/**
* Handles some cases here we can bail out without fully computing the intersection.
*
* @return {array|null} the result polygons, if an edge case was handled
*/
function checkQuitCases(sPoints, cPoints, subject, clipper, mode) {
var totalS = sPoints.count();
var totalC = cPoints.count();
// No intersections exist
if (sPoints.count('intersect', true) === 0) {
switch (mode) {
case "union":
if (sPoints.count('type', 'in') == totalS) {
return [[clipper]];
} else if (cPoints.count('type', 'in') == totalC) {
return [[subject]];
}
// Return both shapes as a multipolygon
return [[subject], [clipper]];
break;
case "intersect":
// There's no intersection, return nothing.
return [];
break;
case "subtractB":
// If B is inside of A, it's a hole.
if (cPoints.first.type == "in") {
return [[subject, clipper]];
}
if (sPoints.count('type', 'in') == totalS) {
return [];
}
// Otherwise it's disjoint, so we ignore it.
return [[subject]]
break;
case "subtractA":
// If A is inside of B, it's a hole.
if (sPoints.first.type == "in") {
return [[clipper, subject]];
}
if (cPoints.first.type == "in") {
return [];
}
// Otherwise it's disjoint, so we ignore it.
return [[clipper]];
break;
}
}
// All points are degenerate. The shapes may be spatially equal.
// The intersect === 1 is a dumb hack for certain cases where (probably because of
// floating point errors) a single point of a polygon we generated is touching
// an edge. This is probably a sign of a bigger issue in intersection detection,
// but we'll wait and see how that goes
if (totalS == sPoints.count('degenerate', true) || sPoints.count("intersect", true) == 1) {
switch (mode) {
case "subtractA":
// If all points in the clip are also degenerate, these shapes
// are equal.
if (totalC == cPoints.count('degenerate', true)) {
return [];
}
return [[clipper]];
break;
case "subtractB":
// If all points in the clip are also degenerate, these shapes
// are equal.
if (totalC == cPoints.count('degenerate', true)) {
return [];
}
return [[subject]];
break;
default:
return [[subject]];
}
}
}
/**
* Builds the list of Polygon(s) representing the desired overlap of
* the subject/clipper.
*
* @param {[type]} sPoints [description]
* @return {[type]} [description]
*/
function buildPolygons(sPoints, s_forward, c_forward) {
var curr = sPoints.first;
var polylist = [];
var onclip = false;
var endir = 'next';
var exdir = 'prev';
while (curr = sPoints.firstIntersect()) {
var poly = [[curr.x, curr.y]];
do {
if (onclip) {
endir = c_forward ? 'next' : 'prev';
exdir = c_forward ? 'prev' : 'next';
} else {
endir = s_forward ? 'next' : 'prev';
exdir = s_forward ? 'prev' : 'next';
}
curr.checked = true;
if (curr.neighbor) {
curr.neighbor.checked = true;
}
if (curr.entry) {
do {
curr = curr[endir];
poly.push([curr.x, curr.y]);
} while (!curr.intersect);
} else {
do {
curr = curr[exdir];
poly.push([curr.x, curr.y]);
} while (!curr.intersect);
}
// Jump to the other list
curr = curr.neighbor;
onclip = !onclip;
} while (!curr.checked);
if (!util.pointsEqual(poly[0], poly[poly.length - 1])) {
poly.push(poly[0]);
} else if (poly.length < 4) {
continue;
}
polylist.push({geom: poly, is_hole: false})
}
// Generate a graph of which polygons own which other polygons (detect holes)
var result = [];
var graph = {};
for (var i = 0; i < polylist.length; i++) {
if (!graph[i]) { graph[i] = []; }
for (var j = 0; j < polylist.length; j++) {
if (i == j) {
continue;
}
// Because we just generated the intersections, we know that
// none of these results can intersect eachother, so we only need to
// run PIP on a single point of each poly.
if (pip(polylist[j].geom[0], polylist[i].geom)) {
polylist[j].is_hole = true;
graph[i].push(j);
}
}
}
// Construct polys with their holes
for (var key in graph) {
if (polylist[key].is_hole) {
continue;
}
var poly = [polylist[key].geom];
graph[key].forEach(function (idx) {
poly.push(polylist[idx].geom);
});
result.push(poly);
}
return result;
}
/**
* Builds vertex lists for the subject and clipper. Essentially
* the way this will work is that it will detect intersections by
* comparing each pair of lines between the subject / clipper, then
* injecting intersection vertices (marked by the "intersects" property)
* in the appropriate spots in each coordinate list.
*
* Once this is complete, our subject and clipper coordinate lists will
* each contain, in traversable order, every vertex, including ones for
* each point where the other polygon intersected.
*
* @param {[type]} sPoints [description]
* @param {[type]} cPoints [description]
* @return {[type]} [description]
*/
function buildIntersectionLists(sPoints, cPoints, sPoly, cPoly) {
var sCurr = sPoints.first;
do {
setPointRelativeLocation(sCurr, cPoly)
var cCurr = cPoints.first;
if (!sCurr.intersect) {
do {
setPointRelativeLocation(cCurr, sPoly)
if (!cCurr.intersect) {
var sEnd = sPoints.nextNonIntersect(sCurr.next);
var cEnd = cPoints.nextNonIntersect(cCurr.next);
var intersect = lineIntersects(sCurr, sEnd, cCurr, cEnd);
if (intersect) {
cCurr = handleIntersection(sPoints, cPoints, sCurr, sEnd, cCurr, cEnd, intersect);
}
}
cCurr = cCurr.next;
} while (cCurr !== cPoints.first);
}
sCurr = sCurr.next;
} while (sCurr !== sPoints.first);
}
/**
* Loop back through, ensuring that all degenerate vertices
* are marked as intersections.
*
* @param {Ring} points [description]
* @return {[type]} [description]
*/
function markDegensAsIntersect(points) {
var curr = points.first;
do {
if (curr.degenerate) {
curr.intersect = true;
curr.neighbor.intersect = true;
}
curr = curr.next
} while (curr != points.first)
}
/**
* Handle inserting / replacing points appropriately for
* a found intersection
*
* @param {Ring} sPoints Subject Ring
* @param {Ring} cPoints Clip Ring
* @param {Vertex} sCurr Start of the Subject line
* @param {Vertex} sEnd End of the Subject line
* @param {Vertex} cCurr Start of the Clip line
* @param {Vertex} cEnd End of the Clip line
* @param {Object} intersect Object representing an intersection
* @return {[type]} [description]
*/
function handleIntersection(sPoints, cPoints, sCurr, sEnd, cCurr, cEnd, intersect) {
var sPt, cPt;
var s_between = 0 < intersect.alphaA && intersect.alphaA < 1;
var c_between = 0 < intersect.alphaB && intersect.alphaB < 1;
if (s_between && c_between) {
sPt = new Vertex(intersect.x, intersect.y, intersect.alphaA, true);
cPt = new Vertex(intersect.x, intersect.y, intersect.alphaB, true);
sPoints.insert(sPt, sCurr, sEnd);
cPoints.insert(cPt, cCurr, cEnd);
} else {
// Handle various degeneracy cases for the subject point
if (s_between) {
sPt = new Vertex(intersect.x, intersect.y, intersect.alphaA, true, true);
sPoints.insert(sPt, sCurr, sPoints.nextNonIntersect(sCurr.next));
} else if (intersect.alphaA == 0) {
sCurr.intersect = true;
sCurr.degenerate = true;
sCurr.alpha = intersect.alphaA;
sPt = sCurr;
} else if (intersect.alphaA == 1) {
// End points get marked as degenerate but don't get marked as intersects.
// This allows us to catch them later, and still use them for generating
// lines to test against the other polygon
sEnd.intersect = false;
sEnd.degenerate = true;
sEnd.alpha = intersect.alphaA;
sPt = sEnd;
}
// Handle various degeneracy cases for the clip point
if (c_between) {
cPt = new Vertex(intersect.x, intersect.y, intersect.alphaB, true, true);
cPoints.insert(cPt, cCurr, cPoints.nextNonIntersect(cCurr.next));
} else if (intersect.alphaB == 0) {
cCurr.intersect = true;
cCurr.degenerate = true;
cCurr.alpha = intersect.alphaB;
cPt = cCurr;
} else if (intersect.alphaB == 1) {
// End points get marked as degenerate but don't get marked as intersects.
// This allows us to catch them later, and still use them for generating
// lines to test against the other polygon
cEnd.intersect = false;
cEnd.degenerate = true;
cEnd.alpha = intersect.alphaB;
cPt = cEnd;
if (cCurr.next != cPoints.first) {
cCurr = cCurr.next;
}
}
}
// Neighbors are used to jump back and forth between the lists
if (sPt && cPt) {
sPt.neighbor = cPt;
cPt.neighbor = sPt;
// Intersections are always "on" a line
sPt.type = "on";
cPt.type = "on";
}
return cCurr;
}
/**
* Set a point in or out compared to the other polygon:
* - if it's a subject point, compare to the clip polygon,
* - if it's a clip point, compare to the subject polygon
*
* @param {Vertex} pt Point to check against the poly
* @param {Polygon} poly Check if pt is within this poly
*/
function setPointRelativeLocation(pt, poly) {
if (!pt.type) {
if (pip([pt.x, pt.y], poly)) {
pt.type = "in";
} else {
pt.type = "out";
}
}
}
/**
* Handle setting entry/exit flags for each intersection. This is
* where a large part of degeneracy handling happens - the original
* GH algorithm uses very simple entry/exit handling, which won't work
* for our degenerate cases.
*
* http://arxiv-web3.library.cornell.edu/pdf/1211.3376v1.pdf
*
* @param {Ring} sPoints The subject polygon's vertices
*/
function setEntryExit(sPoints) {
var first = sPoints.first;
var curr = first;
do {
if (curr.intersect && curr.neighbor) {
handleEnEx(curr);
handleEnEx(curr.neighbor);
// If this and the neighbor share the same entry / exit flag values
// we need to throw them out and relabel
switch (curr.entryPair()) {
case "en/en":
curr.remove = true;
curr.type = "in";
curr.intersect = false;
curr.neighbor.intersect = false;
break;
case "ex/ex":
curr.remove = true;
curr.type = "out";
curr.intersect = false;
curr.neighbor.intersect = false;
break;
}
}
curr = curr.next;
} while (curr != first)
}
/**
* Handles deciding the entry / exit flag setting for a given point.
* This is probably where most of the things could be wrong
*
* @param {Vertex} curr The vertex to flag
*/
function handleEnEx(curr) {
var cp = curr.pairing();
switch (cp) {
case "in/out":
case "on/out":
case "in/on":
curr.entry = false;
break;
case "out/in":
case "on/in":
case "out/on":
curr.entry = true;
break;
case "out/out":
case "in/in":
case "on/on":
var np = curr.neighbor.pairing();
if (np == "out/out" || np == "in/in" || np == "on/on" || (cp == "on/on" && np == "on/out")) {
curr.remove = true;
curr.neighbor.remove = true;
curr.neighbor.intersect = false;
curr.intersect = false;
} else {
handleEnEx(curr.neighbor);
curr.entry = !curr.neighbor.entry;
}
break;
default:
// This shouldn't ever happen - It's here to confirm nothing stupid is happening.
console.error("UNKNOWN TYPE", curr.pairing())
}
}
/**
* Take two lines (each represented by the respective
* start and end), and tells you where they intersect,
* as well as the intersection alphas
*
* @param {Vertex} start1 [description]
* @param {Vertex} end1 [description]
* @param {Vertex} start2 [description]
* @param {Vertex} end2 [description]
* @return {[type]} [description]
*/
function lineIntersects(start1, end1, start2, end2) {
// if the lines intersect, the result contains the x and y of the intersection (treating the lines as infinite) and booleans for whether line segment 1 or line segment 2 contain the point
var denominator, a, b, numerator1, numerator2, result = {
x: null,
y: null,
onLine1: false,
onLine2: false,
alphaA: null,
alphaB: null,
};
denominator = ((end2.y - start2.y) * (end1.x - start1.x)) - ((end2.x - start2.x) * (end1.y - start1.y));
if (denominator == 0) {
if (start1.equals(start2)) {
result.x = start1.x
result.y = start1.y
result.alphaA = 0;
result.alphaB = 0;
return result;
}
return false;
}
a = start1.y - start2.y;
b = start1.x - start2.x;
numerator1 = ((end2.x - start2.x) * a) - ((end2.y - start2.y) * b);
numerator2 = ((end1.x - start1.x) * a) - ((end1.y - start1.y) * b);
a = numerator1 / denominator;
b = numerator2 / denominator;
// if we cast these lines infinitely in both directions, they intersect here:
result.x = start1.x + (a * (end1.x - start1.x));
result.y = start1.y + (a * (end1.y - start1.y));
result.alphaA = a;
result.alphaB = b;
// TODO: any better way to handle this?
if (result.alphaA > 0.99999999999999) {
result.alphaA = 1;
}
if (result.alphaB > 0.99999999999999) {
result.alphaB = 1;
}
if (result.alphaA < 0.00000000000001) {
result.alphaA = 0;
}
if (result.alphaB < 0.00000000000001) {
result.alphaB = 0;
}
// if line1 is a segment and line2 is infinite, they intersect if:
if (0 <= a && a <= 1) {
result.onLine1 = true;
}
// if line2 is a segment and line1 is infinite, they intersect if:
if (0 <= b && b <= 1) {
result.onLine2 = true;
}
// if line1 and line2 are segments, they intersect if both of the above are true
if(result.onLine1 && result.onLine2){
return result;
}
else {
return false;
}
}
/**
* Utility method for logging points
*
* @param {[type]} sPoints [description]
* @param {[type]} cPoints [description]
* @return {[type]} [description]
*/
function logPoints(sPoints, cPoints) {
console.log("POINTS")
console.log("-----------------")
var curr = sPoints.first
do {
curr.log();
curr = curr.next
} while (curr != sPoints.first)
console.log("-----------------")
if (!cPoints) return;
var curr = cPoints.first
do {
curr.log();
curr = curr.next
} while (curr != cPoints.first)
}
/**
* Utility method for logging intersecions and degenerate points
*
* @param {[type]} sPoints [description]
* @return {[type]} [description]
*/
function logIntersections(sPoints) {
console.log("-------------------")
console.log("INTERSECTION LIST: ")
console.log("-------------------")
var curr = sPoints.first
do {
if (curr.intersect || curr.degenerate) {
curr.log()
}
curr = curr.next
} while (curr != sPoints.first)
console.log("")
}
},{"./ring":6,"./util":9,"./vertex":10,"point-in-polygon":11}],5:[function(require,module,exports){
var ghClipping = require('./greiner-hormann');
var union = require('./union');
var utils = require('./util');
var subtract = require('./subtract');
module.exports = function(subject, clipper) {
var subject = utils.clone(subject);
var clipper = utils.clone(clipper);
var s_hulls = utils.outerHulls(subject);
var c_hulls = utils.outerHulls(clipper);
var holes = utils.holes(subject).concat(utils.holes(clipper));
var result = [];
for (var i = 0; i < s_hulls.length; i++) {
for (var j = 0; j < c_hulls.length; j++) {
var test = ghClipping(s_hulls[i], c_hulls[j], true, true);
if (Array.isArray(test)) {
result = result.concat(test);
}
}
}
// Union all the holes then subtract them rom the result
if (holes.length > 0) {
var holeUnion = union(utils.wrapToPolygons(holes));
return subtract(result, utils.wrapToPolygons(utils.outerHulls(holeUnion)));
}
return result;
}
},{"./greiner-hormann":4,"./subtract":7,"./union":8,"./util":9}],6:[function(require,module,exports){
var Vertex = require('./vertex');
var clockwise = require('turf-is-clockwise');
/**
* Ring is a circular doubly-linked list; Every node
* has a next and a prev, even if it's the only node in the list.
*
* This supports some search methods that need to wrap back to the start of the list.
*/
function Ring () {
this.first = null;
}
Ring.prototype.count = function(countkey, countval) {
var curr = this.first;
var count = 0;
while (true) {
if (countkey) {
if (curr[countkey] === countval) {
count++;
}
} else {
count++;
}
curr = curr.next;
if (curr == this.first) {
break;
}
}
return count;
}
/**
* Takes an array of coordinates and constructs a Ring
*
* @param {array} coordinates the array of coordinates to convert to a Ring
* @return {Ring}
*/
Ring.fromArray = function(coordinates) {
var ring = new Ring()
if (!clockwise(coordinates)) {
coordinates = coordinates.reverse();
}
for (var i = 0; i < (coordinates.length - 1); i++) {
var elem = coordinates[i];
ring.push(new Vertex(elem[0], elem[1]));
}
return ring;
}
/**
* Push a vertex into the ring's list. This
* just updates pointers to put the point at
* the end of the list
*
* @param {Vertex} vertex the vertex to push
*/
Ring.prototype.push = function(vertex) {
if (!this.first) {
this.first = vertex;
this.first.prev = vertex;
this.first.next = vertex;
} else {
next = this.first;
prev = next.prev;
next.prev = vertex;
vertex.next = next;
vertex.prev = prev;
prev.next = vertex;
}
}
/**
* Insert a vertex between specific vertices
*
* If there are intersection points, inbetween
* start and end, the new vertex is inserted
* based on it's alpha value
*
* @param {Vertex} vertex the vertex to insert
* @param {Vertex} start the "leftmost" vertex this point could be inserted next to
* @param {Vertex} end the "rightmost" vertex this could could be inserted next to
*/
Ring.prototype.insert = function(vertex, start, end) {
var curr = start.next;
while (curr != end && curr.alpha < vertex.alpha) {
curr = curr.next;
}
// Insert just before the "curr" value
vertex.next = curr;
prev = curr.prev;
vertex.prev = prev;
prev.next = vertex;
curr.prev = vertex;
}
/**
* Start at the start vertex, and get the next
* point that isn't an intersection
*
* @param {Vertex} start the vertex to start searching at
* @return {Vertex} the next non-intersect
*/
Ring.prototype.nextNonIntersect = function (start) {
var curr = start;
while (curr.intersect && curr != this.first) {
curr = curr.next
}
return curr;
}
/**
* Returns the first unchecked intersection in the list
*
* @return {Vertex|bool}
*/
Ring.prototype.firstIntersect = function () {
var curr = this.first;
while (true) {
if (curr.intersect && !curr.checked) {
return curr;
}
curr = curr.next;
if (curr == this.first) {
break;
}
}
return false;
}
/**
* Converts the Ring into an array
*
* @return {array} array representation of the ring
*/
Ring.prototype.toArray = function () {
var curr = this.first;
var points = [];
do {
points.push([curr.x, curr.y]);
curr = curr.next;
} while (curr != this.first);
return points;
}
module.exports = Ring;
},{"./vertex":10,"turf-is-clockwise":12}],7:[function(require,module,exports){
var util = require('./util');
var ghClipping = require('./greiner-hormann');
function intersect(hulls, holes) {
var result = [];
for (var i = 0; i < hulls.length; i++) {
for (var j = 0; j < holes.length; j++) {
var test = ghClipping(hulls[i], holes[j], true, true);
if (Array.isArray(test)) {
result = result.concat(test);
}
}
}
return result;1
}
function subtract(subject, clip, skiploop) {
skiploop = !!skiploop;
subject = util.clone(subject);
clip = util.clone(clip);
var s_hulls = util.outerHulls(subject);
var c_hulls = util.outerHulls(clip);
var c_holes = util.holes(clip);
// TODO:
// If there are any holes in the subject, subtract those as well
// If there are any holes in the clip, which overlap the original
// subject, these should be unioned in. This means we'll have to
// check polygon within-ness for each clip hole
// first intersect any holes in clip against the hulls in the subject.
// If there are results from these, then these holes overlap the subject,
// meaning they (or some part of them) will be saved in the final output as
// outer hulls
if (c_holes.length > 0) {
var c_holes = intersect(s_hulls, c_holes);
}
if (util.isPolygon(subject)) {
subject = [subject];
} else {
subject = util.wrapToPolygons(subject);
}
for (var i = 0; i < c_hulls.length; i++) {
var ilen = subject.length;
for (var j = 0; j < subject.length; j++) {
var test = ghClipping(subject[j][0], c_hulls[i], false, true);
if (test.length == 0) {
subject.splice(j, 1);
j--;
continue;
}
subject[j][0] = test[0][0];
// Copy in each hole (if there were any) for the primary hull
for (var k = 1; k < test[0].length; k++) {
subject[j].push(test[0][k]);
}
// If there are any additional polygons in the result,
// they were newly created by intersecting this hole.
// Push the new polygon (hull and any holes) straight
// into the intersect list
for (var k = 1; k < test.length; k++) {
subject.push(test[k]);
}
}
// If the length has changed, this hole created
// new intersection polygons, which means it's not a hole
// anymore (it crossed the polys, now it's boundary is
// part of the new intersection hulls). Therefore, we
// remove this hole from the list so it won't be returned.
if (ilen != subject.length) {
c_hulls.splice(i, 1)