-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathpossible_intersection.js
114 lines (98 loc) · 3.46 KB
/
possible_intersection.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
import divideSegment from './divide_segment';
import intersection from './segment_intersection';
import equals from './equals';
import compareEvents from './compare_events';
import {
NON_CONTRIBUTING,
SAME_TRANSITION,
DIFFERENT_TRANSITION
} from './edge_type';
/**
* @param {SweepEvent} se1
* @param {SweepEvent} se2
* @param {Queue} queue
* @return {Number}
*/
export default function possibleIntersection (se1, se2, queue) {
// that disallows self-intersecting polygons,
// did cost us half a day, so I'll leave it
// out of respect
// if (se1.isSubject === se2.isSubject) return;
const inter = intersection(
se1.point, se1.otherEvent.point,
se2.point, se2.otherEvent.point
);
const nintersections = inter ? inter.length : 0;
if (nintersections === 0) return 0; // no intersection
// the line segments intersect at an endpoint of both line segments
if ((nintersections === 1) &&
(equals(se1.point, se2.point) ||
equals(se1.otherEvent.point, se2.otherEvent.point))) {
return 0;
}
if (nintersections === 2 && se1.isSubject === se2.isSubject) {
// if(se1.contourId === se2.contourId){
// console.warn('Edges of the same polygon overlap',
// se1.point, se1.otherEvent.point, se2.point, se2.otherEvent.point);
// }
//throw new Error('Edges of the same polygon overlap');
return 0;
}
// The line segments associated to se1 and se2 intersect
if (nintersections === 1) {
// if the intersection point is not an endpoint of se1
if (!equals(se1.point, inter[0]) && !equals(se1.otherEvent.point, inter[0])) {
divideSegment(se1, inter[0], queue);
}
// if the intersection point is not an endpoint of se2
if (!equals(se2.point, inter[0]) && !equals(se2.otherEvent.point, inter[0])) {
divideSegment(se2, inter[0], queue);
}
return 1;
}
// The line segments associated to se1 and se2 overlap
const events = [];
let leftCoincide = false;
let rightCoincide = false;
if (equals(se1.point, se2.point)) {
leftCoincide = true; // linked
} else if (compareEvents(se1, se2) === 1) {
events.push(se2, se1);
} else {
events.push(se1, se2);
}
if (equals(se1.otherEvent.point, se2.otherEvent.point)) {
rightCoincide = true;
} else if (compareEvents(se1.otherEvent, se2.otherEvent) === 1) {
events.push(se2.otherEvent, se1.otherEvent);
} else {
events.push(se1.otherEvent, se2.otherEvent);
}
if ((leftCoincide && rightCoincide) || leftCoincide) {
// both line segments are equal or share the left endpoint
se2.type = NON_CONTRIBUTING;
se1.type = (se2.inOut === se1.inOut)
? SAME_TRANSITION : DIFFERENT_TRANSITION;
if (leftCoincide && !rightCoincide) {
// honestly no idea, but changing events selection from [2, 1]
// to [0, 1] fixes the overlapping self-intersecting polygons issue
divideSegment(events[1].otherEvent, events[0].point, queue);
}
return 2;
}
// the line segments share the right endpoint
if (rightCoincide) {
divideSegment(events[0], events[1].point, queue);
return 3;
}
// no line segment includes totally the other one
if (events[0] !== events[3].otherEvent) {
divideSegment(events[0], events[1].point, queue);
divideSegment(events[1], events[2].point, queue);
return 3;
}
// one line segment includes the other one
divideSegment(events[0], events[1].point, queue);
divideSegment(events[3].otherEvent, events[2].point, queue);
return 3;
}