forked from GustavLindberg99/NuclideChart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
8792 lines (7644 loc) · 281 KB
/
main.mjs
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
/**
* Global constant CCW defines counterclockwise direction of arc
* @type {boolean}
*/
const CCW = true;
/**
* Global constant CW defines clockwise direction of arc
* @type {boolean}
*/
const CW = false;
/**
* Defines orientation for face of the polygon: clockwise, counterclockwise
* or not orientable in the case of self-intersection
* @type {{CW: number, CCW: number, NOT_ORIENTABLE: number}}
*/
const ORIENTATION = {CCW:-1, CW:1, NOT_ORIENTABLE: 0};
const PIx2 = 2 * Math.PI;
const INSIDE$2 = 1;
const OUTSIDE$1 = 0;
const BOUNDARY$1 = 2;
const CONTAINS = 3;
const INTERLACE = 4;
const OVERLAP_SAME$1 = 1;
const OVERLAP_OPPOSITE$1 = 2;
const NOT_VERTEX$1 = 0;
const START_VERTEX$1 = 1;
const END_VERTEX$1 = 2;
var Constants = /*#__PURE__*/Object.freeze({
__proto__: null,
BOUNDARY: BOUNDARY$1,
CCW: CCW,
CONTAINS: CONTAINS,
CW: CW,
END_VERTEX: END_VERTEX$1,
INSIDE: INSIDE$2,
INTERLACE: INTERLACE,
NOT_VERTEX: NOT_VERTEX$1,
ORIENTATION: ORIENTATION,
OUTSIDE: OUTSIDE$1,
OVERLAP_OPPOSITE: OVERLAP_OPPOSITE$1,
OVERLAP_SAME: OVERLAP_SAME$1,
PIx2: PIx2,
START_VERTEX: START_VERTEX$1
});
/**
* Created by Alex Bol on 2/18/2017.
*/
/**
* Floating point comparison tolerance.
* Default value is 0.000001 (10e-6)
* @type {number}
*/
let DP_TOL = 0.000001;
/**
* Set new floating point comparison tolerance
* @param {number} tolerance
*/
function setTolerance(tolerance) {DP_TOL = tolerance;}
/**
* Get floating point comparison tolerance
* @returns {number}
*/
function getTolerance() {return DP_TOL;}
const DECIMALS = 3;
/**
* Returns *true* if value comparable to zero
* @param {number} x
* @param {number} y
* @return {boolean}
*/
function EQ_0(x) {
return (x < DP_TOL && x > -DP_TOL);
}
/**
* Returns *true* if two values are equal up to DP_TOL
* @param {number} x
* @param {number} y
* @return {boolean}
*/
function EQ(x, y) {
return (x - y < DP_TOL && x - y > -DP_TOL);
}
/**
* Returns *true* if first argument greater than second argument up to DP_TOL
* @param {number} x
* @param {number} y
* @return {boolean}
*/
function GT(x, y) {
return (x - y > DP_TOL);
}
/**
* Returns *true* if first argument greater than or equal to second argument up to DP_TOL
* @param {number} x
* @param {number} y
* @returns {boolean}
*/
function GE(x, y) {
return (x - y > -DP_TOL);
}
/**
* Returns *true* if first argument less than second argument up to DP_TOL
* @param {number} x
* @param {number} y
* @return {boolean}
*/
function LT(x, y) {
return (x - y < -DP_TOL)
}
/**
* Returns *true* if first argument less than or equal to second argument up to DP_TOL
* @param {number} x
* @param {number} y
* @return {boolean}
*/
function LE(x, y) {
return (x - y < DP_TOL);
}
var Utils$1 = /*#__PURE__*/Object.freeze({
__proto__: null,
DECIMALS: DECIMALS,
EQ: EQ,
EQ_0: EQ_0,
GE: GE,
GT: GT,
LE: LE,
LT: LT,
getTolerance: getTolerance,
setTolerance: setTolerance
});
let Flatten = {
Utils: Utils$1,
Errors: undefined,
Matrix: undefined,
Planar_set: undefined,
Point: undefined,
Vector: undefined,
Line: undefined,
Circle: undefined,
Segment: undefined,
Arc: undefined,
Box: undefined,
Edge: undefined,
Face: undefined,
Ray: undefined,
Ray_shooting: undefined,
Multiline: undefined,
Polygon: undefined,
Distance: undefined,
Inversion: undefined
};
for (let c in Constants) {Flatten[c] = Constants[c];}
Object.defineProperty(Flatten, 'DP_TOL', {
get:function(){return getTolerance()},
set:function(value){setTolerance(value);}
});
/**
* Created by Alex Bol on 2/19/2017.
*/
/**
* Class of system errors
*/
class Errors {
/**
* Throw error ILLEGAL_PARAMETERS when cannot instantiate from given parameter
* @returns {ReferenceError}
*/
static get ILLEGAL_PARAMETERS() {
return new ReferenceError('Illegal Parameters');
}
/**
* Throw error ZERO_DIVISION to catch situation of zero division
* @returns {Error}
*/
static get ZERO_DIVISION() {
return new Error('Zero division');
}
/**
* Error to throw from BooleanOperations module in case when fixBoundaryConflicts not capable to fix it
* @returns {Error}
*/
static get UNRESOLVED_BOUNDARY_CONFLICT() {
return new Error('Unresolved boundary conflict in boolean operation');
}
/**
* Error to throw from LinkedList:testInfiniteLoop static method
* in case when circular loop detected in linked list
* @returns {Error}
*/
static get INFINITE_LOOP() {
return new Error('Infinite loop');
}
static get CANNOT_COMPLETE_BOOLEAN_OPERATION() {
return new Error('Cannot complete boolean operation')
}
static get CANNOT_INVOKE_ABSTRACT_METHOD() {
return new Error('Abstract method cannot be invoked');
}
static get OPERATION_IS_NOT_SUPPORTED() {
return new Error('Operation is not supported')
}
}
Flatten.Errors = Errors;
/**
* Class implements bidirectional non-circular linked list. <br/>
* LinkedListElement - object of any type that has properties next and prev.
*/
class LinkedList {
constructor(first, last) {
this.first = first;
this.last = last || this.first;
}
[Symbol.iterator]() {
let value = undefined;
return {
next: () => {
value = value ? value.next : this.first;
return {value: value, done: value === undefined};
}
};
};
/**
* Return number of elements in the list
* @returns {number}
*/
get size() {
let counter = 0;
for (let edge of this) {
counter++;
}
return counter;
}
/**
* Return array of elements from start to end,
* If start or end not defined, take first as start, last as end
* @returns {Array}
*/
toArray(start=undefined, end=undefined) {
let elements = [];
let from = start || this.first;
let to = end || this.last;
let element = from;
if (element === undefined) return elements;
do {
elements.push(element);
element = element.next;
} while (element !== to.next);
return elements;
}
/**
* Append new element to the end of the list
* @param {LinkedListElement} element
* @returns {LinkedList}
*/
append(element) {
if (this.isEmpty()) {
this.first = element;
} else {
element.prev = this.last;
this.last.next = element;
}
// update edge to be last
this.last = element;
// nullify non-circular links
this.last.next = undefined;
this.first.prev = undefined;
return this;
}
/**
* Insert new element to the list after elementBefore
* @param {LinkedListElement} newElement
* @param {LinkedListElement} elementBefore
* @returns {LinkedList}
*/
insert(newElement, elementBefore) {
if (this.isEmpty()) {
this.first = newElement;
this.last = newElement;
}
else if (elementBefore === null || elementBefore === undefined) {
newElement.next = this.first;
this.first.prev = newElement;
this.first = newElement;
}
else {
/* set links to new element */
let elementAfter = elementBefore.next;
elementBefore.next = newElement;
if (elementAfter) elementAfter.prev = newElement;
/* set links from new element */
newElement.prev = elementBefore;
newElement.next = elementAfter;
/* extend list if new element added after the last element */
if (this.last === elementBefore)
this.last = newElement;
}
// nullify non-circular links
this.last.next = undefined;
this.first.prev = undefined;
return this;
}
/**
* Remove element from the list
* @param {LinkedListElement} element
* @returns {LinkedList}
*/
remove(element) {
// special case if last edge removed
if (element === this.first && element === this.last) {
this.first = undefined;
this.last = undefined;
} else {
// update linked list
if (element.prev) element.prev.next = element.next;
if (element.next) element.next.prev = element.prev;
// update first if need
if (element === this.first) {
this.first = element.next;
}
// update last if need
if (element === this.last) {
this.last = element.prev;
}
}
return this;
}
/**
* Return true if list is empty
* @returns {boolean}
*/
isEmpty() {
return this.first === undefined;
}
/**
* Throw an error if circular loop detected in the linked list
* @param {LinkedListElement} first element to start iteration
* @throws {Errors.INFINITE_LOOP}
*/
static testInfiniteLoop(first) {
let edge = first;
let controlEdge = first;
do {
if (edge != first && edge === controlEdge) {
throw Errors.INFINITE_LOOP; // new Error("Infinite loop")
}
edge = edge.next;
controlEdge = controlEdge.next.next;
} while (edge != first)
}
}
/*
Smart intersections describe intersection points that refers to the edges they intersect
This function are supposed for internal usage by morphing and relation methods between
*/
function addToIntPoints(edge, pt, int_points)
{
let id = int_points.length;
let shapes = edge.shape.split(pt);
// if (shapes.length < 2) return;
if (shapes.length === 0) return; // Point does not belong to edge ?
let len = 0;
if (shapes[0] === null) { // point incident to edge start vertex
len = 0;
}
else if (shapes[1] === null) { // point incident to edge end vertex
len = edge.shape.length;
}
else { // Edge was split into to edges
len = shapes[0].length;
}
let is_vertex = NOT_VERTEX$1;
if (EQ(len, 0)) {
is_vertex |= START_VERTEX$1;
}
if (EQ(len, edge.shape.length)) {
is_vertex |= END_VERTEX$1;
}
// Fix intersection point which is end point of the last edge
let arc_length = (is_vertex & END_VERTEX$1) && edge.next.arc_length === 0 ? 0 : edge.arc_length + len;
int_points.push({
id: id,
pt: pt,
arc_length: arc_length,
edge_before: edge,
edge_after: undefined,
face: edge.face,
is_vertex: is_vertex
});
}
function sortIntersections(intersections)
{
// if (intersections.int_points1.length === 0) return;
// augment intersections with new sorted arrays
// intersections.int_points1_sorted = intersections.int_points1.slice().sort(compareFn);
// intersections.int_points2_sorted = intersections.int_points2.slice().sort(compareFn);
intersections.int_points1_sorted = getSortedArray(intersections.int_points1);
intersections.int_points2_sorted = getSortedArray(intersections.int_points2);
}
function getSortedArray(int_points)
{
let faceMap = new Map;
let id = 0;
// Create integer id's for faces
for (let ip of int_points) {
if (!faceMap.has(ip.face)) {
faceMap.set(ip.face, id);
id++;
}
}
// Augment intersection points with face id's
for (let ip of int_points) {
ip.faceId = faceMap.get(ip.face);
}
// Clone and sort
let int_points_sorted = int_points.slice().sort(compareFn);
return int_points_sorted;
}
function compareFn(ip1, ip2)
{
// compare face id's
if (ip1.faceId < ip2.faceId) {
return -1;
}
if (ip1.faceId > ip2.faceId) {
return 1;
}
// same face - compare arc_length
if (ip1.arc_length < ip2.arc_length) {
return -1;
}
if (ip1.arc_length > ip2.arc_length) {
return 1;
}
return 0;
}
function getSortedArrayOnLine(line, int_points) {
return int_points.slice().sort( (int_point1, int_point2) => {
if (line.coord(int_point1.pt) < line.coord(int_point2.pt)) {
return -1;
}
if (line.coord(int_point1.pt) > line.coord(int_point2.pt)) {
return 1;
}
return 0;
})
}
function filterDuplicatedIntersections(intersections)
{
if (intersections.int_points1.length < 2) return;
let do_squeeze = false;
let int_point_ref1;
let int_point_ref2;
let int_point_cur1;
let int_point_cur2;
for (let i = 0; i < intersections.int_points1_sorted.length; i++) {
if (intersections.int_points1_sorted[i].id === -1)
continue;
int_point_ref1 = intersections.int_points1_sorted[i];
int_point_ref2 = intersections.int_points2[int_point_ref1.id];
for (let j=i+1; j < intersections.int_points1_sorted.length; j++) {
int_point_cur1 = intersections.int_points1_sorted[j];
if (!EQ(int_point_cur1.arc_length, int_point_ref1.arc_length)) {
break;
}
if (int_point_cur1.id === -1)
continue;
int_point_cur2 = intersections.int_points2[int_point_cur1.id];
if (int_point_cur2.id === -1)
continue;
if (int_point_cur1.edge_before === int_point_ref1.edge_before &&
int_point_cur1.edge_after === int_point_ref1.edge_after &&
int_point_cur2.edge_before === int_point_ref2.edge_before &&
int_point_cur2.edge_after === int_point_ref2.edge_after) {
int_point_cur1.id = -1;
/* to be deleted */
int_point_cur2.id = -1;
/* to be deleted */
do_squeeze = true;
}
}
}
int_point_ref2 = intersections.int_points2_sorted[0];
int_point_ref1 = intersections.int_points1[int_point_ref2.id];
for (let i = 1; i < intersections.int_points2_sorted.length; i++) {
let int_point_cur2 = intersections.int_points2_sorted[i];
if (int_point_cur2.id == -1) continue;
/* already deleted */
if (int_point_ref2.id == -1 || /* can't be reference if already deleted */
!(EQ(int_point_cur2.arc_length, int_point_ref2.arc_length))) {
int_point_ref2 = int_point_cur2;
int_point_ref1 = intersections.int_points1[int_point_ref2.id];
continue;
}
let int_point_cur1 = intersections.int_points1[int_point_cur2.id];
if (int_point_cur1.edge_before === int_point_ref1.edge_before &&
int_point_cur1.edge_after === int_point_ref1.edge_after &&
int_point_cur2.edge_before === int_point_ref2.edge_before &&
int_point_cur2.edge_after === int_point_ref2.edge_after) {
int_point_cur1.id = -1;
/* to be deleted */
int_point_cur2.id = -1;
/* to be deleted */
do_squeeze = true;
}
}
if (do_squeeze) {
intersections.int_points1 = intersections.int_points1.filter((int_point) => int_point.id >= 0);
intersections.int_points2 = intersections.int_points2.filter((int_point) => int_point.id >= 0);
// update id's
intersections.int_points1.forEach((int_point, index) => int_point.id = index);
intersections.int_points2.forEach((int_point, index) => int_point.id = index);
}
}
function initializeInclusionFlags(int_points)
{
for (let int_point of int_points) {
int_point.edge_before.bvStart = undefined;
int_point.edge_before.bvEnd = undefined;
int_point.edge_before.bv = undefined;
int_point.edge_before.overlap = undefined;
int_point.edge_after.bvStart = undefined;
int_point.edge_after.bvEnd = undefined;
int_point.edge_after.bv = undefined;
int_point.edge_after.overlap = undefined;
}
for (let int_point of int_points) {
int_point.edge_before.bvEnd = BOUNDARY$1;
int_point.edge_after.bvStart = BOUNDARY$1;
}
}
function calculateInclusionFlags(int_points, polygon)
{
for (let int_point of int_points) {
int_point.edge_before.setInclusion(polygon);
int_point.edge_after.setInclusion(polygon);
}
}
function setOverlappingFlags(intersections)
{
let cur_face = undefined;
let first_int_point_in_face_id = undefined;
let next_int_point1 = undefined;
let num_int_points = intersections.int_points1.length;
for (let i = 0; i < num_int_points; i++) {
let cur_int_point1 = intersections.int_points1_sorted[i];
// Find boundary chain in the polygon1
if (cur_int_point1.face !== cur_face) { // next chain started
first_int_point_in_face_id = i; // cur_int_point1;
cur_face = cur_int_point1.face;
}
// Skip duplicated points with same <x,y> in "cur_int_point1" pool
let int_points_cur_pool_start = i;
let int_points_cur_pool_num = intPointsPoolCount(intersections.int_points1_sorted, i, cur_face);
let next_int_point_id;
if (int_points_cur_pool_start + int_points_cur_pool_num < num_int_points &&
intersections.int_points1_sorted[int_points_cur_pool_start + int_points_cur_pool_num].face === cur_face) {
next_int_point_id = int_points_cur_pool_start + int_points_cur_pool_num;
} else { // get first point from the same face
next_int_point_id = first_int_point_in_face_id;
}
// From all points with same ,x,y. in 'next_int_point1' pool choose one that
// has same face both in res_poly and in wrk_poly
let int_points_next_pool_num = intPointsPoolCount(intersections.int_points1_sorted, next_int_point_id, cur_face);
next_int_point1 = null;
for (let j=next_int_point_id; j < next_int_point_id + int_points_next_pool_num; j++) {
let next_int_point1_tmp = intersections.int_points1_sorted[j];
if (next_int_point1_tmp.face === cur_face &&
intersections.int_points2[next_int_point1_tmp.id].face === intersections.int_points2[cur_int_point1.id].face) {
next_int_point1 = next_int_point1_tmp;
break;
}
}
if (next_int_point1 === null)
continue;
let edge_from1 = cur_int_point1.edge_after;
let edge_to1 = next_int_point1.edge_before;
if (!(edge_from1.bv === BOUNDARY$1 && edge_to1.bv === BOUNDARY$1)) // not a boundary chain - skip
continue;
if (edge_from1 !== edge_to1) // one edge chain TODO: support complex case
continue;
/* Find boundary chain in polygon2 between same intersection points */
let cur_int_point2 = intersections.int_points2[cur_int_point1.id];
let next_int_point2 = intersections.int_points2[next_int_point1.id];
let edge_from2 = cur_int_point2.edge_after;
let edge_to2 = next_int_point2.edge_before;
/* if [edge_from2..edge_to2] is not a boundary chain, invert it */
/* check also that chain consist of one or two edges */
if (!(edge_from2.bv === BOUNDARY$1 && edge_to2.bv === BOUNDARY$1 && edge_from2 === edge_to2)) {
cur_int_point2 = intersections.int_points2[next_int_point1.id];
next_int_point2 = intersections.int_points2[cur_int_point1.id];
edge_from2 = cur_int_point2.edge_after;
edge_to2 = next_int_point2.edge_before;
}
if (!(edge_from2.bv === BOUNDARY$1 && edge_to2.bv === BOUNDARY$1 && edge_from2 === edge_to2))
continue; // not an overlapping chain - skip TODO: fix boundary conflict
// Set overlapping flag - one-to-one case
edge_from1.setOverlap(edge_from2);
}
}
function intPointsPoolCount(int_points, cur_int_point_num, cur_face)
{
let int_point_current;
let int_point_next;
let int_points_pool_num = 1;
if (int_points.length == 1) return 1;
int_point_current = int_points[cur_int_point_num];
for (let i = cur_int_point_num + 1; i < int_points.length; i++) {
if (int_point_current.face != cur_face) { /* next face started */
break;
}
int_point_next = int_points[i];
if (!(int_point_next.pt.equalTo(int_point_current.pt) &&
int_point_next.edge_before === int_point_current.edge_before &&
int_point_next.edge_after === int_point_current.edge_after)) {
break; /* next point is different - break and exit */
}
int_points_pool_num++; /* duplicated intersection point - increase counter */
}
return int_points_pool_num;
}
function splitByIntersections(polygon, int_points)
{
if (!int_points) return;
for (let int_point of int_points) {
let edge = int_point.edge_before;
// recalculate vertex flag: it may be changed after previous split
int_point.is_vertex = NOT_VERTEX$1;
if (edge.shape.start && edge.shape.start.equalTo(int_point.pt)) {
int_point.is_vertex |= START_VERTEX$1;
}
if (edge.shape.end && edge.shape.end.equalTo(int_point.pt)) {
int_point.is_vertex |= END_VERTEX$1;
}
if (int_point.is_vertex & START_VERTEX$1) { // nothing to split
int_point.edge_before = edge.prev;
int_point.is_vertex = END_VERTEX$1;
continue;
}
if (int_point.is_vertex & END_VERTEX$1) { // nothing to split
continue;
}
let newEdge = polygon.addVertex(int_point.pt, edge);
int_point.edge_before = newEdge;
}
for (let int_point of int_points) {
int_point.edge_after = int_point.edge_before.next;
}
}
function insertBetweenIntPoints(int_point1, int_point2, new_edge) {
let edge_before = int_point1.edge_before;
let edge_after = int_point2.edge_after;
edge_before.next = new_edge;
new_edge.prev = edge_before;
new_edge.next = edge_after;
edge_after.prev = new_edge;
}
var smart_intersections = /*#__PURE__*/Object.freeze({
__proto__: null,
addToIntPoints: addToIntPoints,
calculateInclusionFlags: calculateInclusionFlags,
filterDuplicatedIntersections: filterDuplicatedIntersections,
getSortedArray: getSortedArray,
getSortedArrayOnLine: getSortedArrayOnLine,
initializeInclusionFlags: initializeInclusionFlags,
insertBetweenIntPoints: insertBetweenIntPoints,
intPointsPoolCount: intPointsPoolCount,
setOverlappingFlags: setOverlappingFlags,
sortIntersections: sortIntersections,
splitByIntersections: splitByIntersections
});
/**
* Created by Alex Bol on 12/02/2018.
*/
/**
* @module BooleanOperations
*/
const {INSIDE: INSIDE$1, OUTSIDE, BOUNDARY, OVERLAP_SAME, OVERLAP_OPPOSITE} = Constants;
const {NOT_VERTEX, START_VERTEX, END_VERTEX} = Constants;
const BOOLEAN_UNION = 1;
const BOOLEAN_INTERSECT = 2;
const BOOLEAN_SUBTRACT = 3;
/**
* Unify two polygons polygons and returns new polygon. <br/>
* Point belongs to the resulted polygon if it belongs to the first OR to the second polygon
* @param {Polygon} polygon1 - first operand
* @param {Polygon} polygon2 - second operand
* @returns {Polygon}
*/
function unify(polygon1, polygon2) {
let [res_poly, wrk_poly] = booleanOpBinary(polygon1, polygon2, BOOLEAN_UNION, true);
return res_poly;
}
/**
* Subtract second polygon from the first and returns new polygon
* Point belongs to the resulted polygon if it belongs to the first polygon AND NOT to the second polygon
* @param {Polygon} polygon1 - first operand
* @param {Polygon} polygon2 - second operand
* @returns {Polygon}
*/
function subtract(polygon1, polygon2) {
let polygon2_tmp = polygon2.clone();
let polygon2_reversed = polygon2_tmp.reverse();
let [res_poly, wrk_poly] = booleanOpBinary(polygon1, polygon2_reversed, BOOLEAN_SUBTRACT, true);
return res_poly;
}
/**
* Intersect two polygons and returns new polygon
* Point belongs to the resulted polygon is it belongs to the first AND to the second polygon
* @param {Polygon} polygon1 - first operand
* @param {Polygon} polygon2 - second operand
* @returns {Polygon}
*/
function intersect$1(polygon1, polygon2) {
let [res_poly, wrk_poly] = booleanOpBinary(polygon1, polygon2, BOOLEAN_INTERSECT, true);
return res_poly;
}
/**
* Returns boundary of intersection between two polygons as two arrays of shapes (Segments/Arcs) <br/>
* The first array are shapes from the first polygon, the second array are shapes from the second
* @param {Polygon} polygon1 - first operand
* @param {Polygon} polygon2 - second operand
* @returns {Shape[][]}
*/
function innerClip(polygon1, polygon2) {
let [res_poly, wrk_poly] = booleanOpBinary(polygon1, polygon2, BOOLEAN_INTERSECT, false);
let clip_shapes1 = [];
for (let face of res_poly.faces) {
clip_shapes1 = [...clip_shapes1, ...[...face.edges].map(edge => edge.shape)];
}
let clip_shapes2 = [];
for (let face of wrk_poly.faces) {
clip_shapes2 = [...clip_shapes2, ...[...face.edges].map(edge => edge.shape)];
}
return [clip_shapes1, clip_shapes2];
}
/**
* Returns boundary of subtraction of the second polygon from first polygon as array of shapes
* @param {Polygon} polygon1 - first operand
* @param {Polygon} polygon2 - second operand
* @returns {Shape[]}
*/
function outerClip(polygon1, polygon2) {
let [res_poly, wrk_poly] = booleanOpBinary(polygon1, polygon2, BOOLEAN_SUBTRACT, false);
let clip_shapes1 = [];
for (let face of res_poly.faces) {
clip_shapes1 = [...clip_shapes1, ...[...face.edges].map(edge => edge.shape)];
}
return clip_shapes1;
}
/**
* Returns intersection points between boundaries of two polygons as two array of points <br/>
* Points in the first array belong to first polygon, points from the second - to the second.
* Points in each array are ordered according to the direction of the correspondent polygon
* @param {Polygon} polygon1 - first operand
* @param {Polygon} polygon2 - second operand
* @returns {Point[][]}
*/
function calculateIntersections(polygon1, polygon2) {
let res_poly = polygon1.clone();
let wrk_poly = polygon2.clone();
// get intersection points
let intersections = getIntersections(res_poly, wrk_poly);
// sort intersection points
sortIntersections(intersections);
// split by intersection points
splitByIntersections(res_poly, intersections.int_points1_sorted);
splitByIntersections(wrk_poly, intersections.int_points2_sorted);
// filter duplicated intersection points
filterDuplicatedIntersections(intersections);
// sort intersection points again after filtering
sortIntersections(intersections);
let ip_sorted1 = intersections.int_points1_sorted.map( int_point => int_point.pt);
let ip_sorted2 = intersections.int_points2_sorted.map( int_point => int_point.pt);
return [ip_sorted1, ip_sorted2];
}
function filterNotRelevantEdges(res_poly, wrk_poly, intersections, op) {
// keep not intersected faces for further remove and merge
let notIntersectedFacesRes = getNotIntersectedFaces(res_poly, intersections.int_points1);
let notIntersectedFacesWrk = getNotIntersectedFaces(wrk_poly, intersections.int_points2);
// calculate inclusion flag for not intersected faces
calcInclusionForNotIntersectedFaces(notIntersectedFacesRes, wrk_poly);
calcInclusionForNotIntersectedFaces(notIntersectedFacesWrk, res_poly);
// initialize inclusion flags for edges incident to intersections
initializeInclusionFlags(intersections.int_points1);
initializeInclusionFlags(intersections.int_points2);
// calculate inclusion flags only for edges incident to intersections
calculateInclusionFlags(intersections.int_points1, wrk_poly);
calculateInclusionFlags(intersections.int_points2, res_poly);
// fix boundary conflicts
while (fixBoundaryConflicts(res_poly, wrk_poly, intersections.int_points1, intersections.int_points1_sorted, intersections.int_points2, intersections));
// while (fixBoundaryConflicts(wrk_poly, res_poly, intersections.int_points2, intersections.int_points2_sorted, intersections.int_points1, intersections));
// Set overlapping flags for boundary chains: SAME or OPPOSITE
setOverlappingFlags(intersections);
// remove not relevant chains between intersection points
removeNotRelevantChains(res_poly, op, intersections.int_points1_sorted, true);
removeNotRelevantChains(wrk_poly, op, intersections.int_points2_sorted, false);
// remove not relevant not intersected faces from res_polygon and wrk_polygon
// if op == UNION, remove faces that are included in wrk_polygon without intersection
// if op == INTERSECT, remove faces that are not included into wrk_polygon
removeNotRelevantNotIntersectedFaces(res_poly, notIntersectedFacesRes, op, true);
removeNotRelevantNotIntersectedFaces(wrk_poly, notIntersectedFacesWrk, op, false);
}
function swapLinksAndRestore(res_poly, wrk_poly, intersections, op) {
// add edges of wrk_poly into the edge container of res_poly
copyWrkToRes(res_poly, wrk_poly, op, intersections.int_points2);
// swap links from res_poly to wrk_poly and vice versa
swapLinks(res_poly, wrk_poly, intersections);
// remove old faces
removeOldFaces(res_poly, intersections.int_points1);
removeOldFaces(wrk_poly, intersections.int_points2);
// restore faces
restoreFaces(res_poly, intersections.int_points1, intersections.int_points2);
restoreFaces(res_poly, intersections.int_points2, intersections.int_points1);
// merge relevant not intersected faces from wrk_polygon to res_polygon
// mergeRelevantNotIntersectedFaces(res_poly, wrk_poly);
}
function booleanOpBinary(polygon1, polygon2, op, restore)
{
let res_poly = polygon1.clone();
let wrk_poly = polygon2.clone();
// get intersection points
let intersections = getIntersections(res_poly, wrk_poly);
// sort intersection points
sortIntersections(intersections);
// split by intersection points
splitByIntersections(res_poly, intersections.int_points1_sorted);
splitByIntersections(wrk_poly, intersections.int_points2_sorted);
// filter duplicated intersection points
filterDuplicatedIntersections(intersections);
// sort intersection points again after filtering
sortIntersections(intersections);
// calculate inclusion and remove not relevant edges
filterNotRelevantEdges(res_poly, wrk_poly, intersections, op);
if (restore) {
swapLinksAndRestore(res_poly, wrk_poly, intersections, op);
}
return [res_poly, wrk_poly];
}
function getIntersections(polygon1, polygon2)
{
let intersections = {
int_points1: [],
int_points2: []
};
// calculate intersections
for (let edge1 of polygon1.edges) {
// request edges of polygon2 in the box of edge1
let resp = polygon2.edges.search(edge1.box);
// for each edge2 in response