-
Notifications
You must be signed in to change notification settings - Fork 0
/
SweepLineClosure.js
155 lines (111 loc) · 3.23 KB
/
SweepLineClosure.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
var SweepLine = function() {
const LO = false;
const HI = true;
var queueHead = null;
var objectNodeMap = new Map();
function queueNode( object, loHi, x ) {
this.object = object;
this.loHi = loHi;
this.x = x;
this.prev = null;
this.next = null;
if ( queueHead ) queueHead.prev = this;
this.next = queueHead;
queueHead = this;
sortNode( this );
}
function add( object, loVal, hiVal ) {
let hiNode = new queueNode( object, HI, hiVal );
let loNode = new queueNode( object, LO, loVal );
objectNodeMap.set( object, {loNode: loNode, hiNode: hiNode} );
}
function update( object, loVal, hiVal ) {
let n = objectNodeMap.get( object );
if ( n ) {
n.hiNode.x = hiVal || n.hiNode.x;
sortNode( n.hiNode );
n.loNode.x = loVal || n.loNode.x;
sortNode( n.loNode );
}
}
function del( object ) {
let n = objectNodeMap.get( object );
if ( n ) {
deleteNode( n.hiNode );
deleteNode( n.loNode );
objectNodeMap.delete( object );
}
}
function sortNode( node ) {
function moveNode() {
// Remove node from current position in queue.
deleteNode( node );
// Add node to new position in queue.
if ( newLocation === null ) {
node.prev = null;
node.next = queueHead;
queueHead = node;
} else {
node.prev = newLocation;
node.next = newLocation.next;
if ( newLocation.next ) newLocation.next.prev = node;
newLocation.next = node;
}
}
// Walk the queue, moving node into the
// proper spot of the queue based on the x
// value. First check against the 'prev' queue
// node...
let newLocation = node.prev;
while (newLocation && node.x < newLocation.x ) {
newLocation = newLocation.prev;
}
if (newLocation !== node.prev) moveNode();
// ...then against the 'next' queue node.
newLocation = node;
while (newLocation.next && newLocation.next.x < node.x ) {
newLocation = newLocation.next;
}
if (newLocation !== node) moveNode();
}
function deleteNode( node ) {
if ( node.prev === null ) queueHead = node.next;
if ( node.prev ) node.prev.next = node.next;
if ( node.next ) node.next.prev = node.prev;
}
function findCollisions( collisionFunction ) {
let collision = [];
let activeObjects = new Set();
var node = queueHead;
while ( node ) {
if ( node.loHi === LO ) {
let object = node.object;
for ( let ao of activeObjects ) {
//const ao = activeObjects[ o ];
if ( collisionFunction( object, ao )) {
collision.push( [ object, ao ] );
}
}
activeObjects.add( object );
} else { // node.loHi === HI
activeObjects.delete( node.object );
}
node = node.next;
}
return collision;
}
function queuePrint( printFunction ) {
var n = queueHead;
while (n) {
printFunction( n.object, n.loHi, n.x )
n = n.next;
}
}
return {
add,
update,
del,
findCollisions,
queuePrint
}
}