-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tin.js
271 lines (238 loc) · 5.77 KB
/
Tin.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
/**
* Created with JetBrains WebStorm.
* User: zhouqi
* Date: 12-5-29
* Time: 下午12:58
* To change this template use File | Settings | File Templates.
*/
var kPrecision = 1e-6;
function TinNode() {
this.x = 0.0;
this.y = 0.0;
//this.edge = null;
}
function TriArea(a, b, c) {
return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
function CCW(a, b, c) {
return TriArea(a, b, c) > kPrecision;
}
function InCircle(c1, c2, c3, p) {
if (p == c1 || p == c2 || p == c3)
return false;
return (c1.x * c1.x + c1.y * c1.y) * TriArea(c2, c3, p)
- (c2.x * c2.x + c2.y * c2.y) * TriArea(c1, c3, p)
+ (c3.x * c3.x + c3.y * c3.y) * TriArea(c1, c2, p)
- (p.x * p.x + p.y * p.y) * TriArea(c1, c2, c3) > kPrecision;
}
function NodeCmp(n1, n2) {
var dx = n2.x - n1.x;
if (dx > kPrecision)
return -1;
if (dx < -kPrecision)
return 1;
var dy = n2.y - n1.y;
if (dy > kPrecision)
return -1;
if (dy < -kPrecision)
return 1;
return 0;
}
function TinEdge() {
this.next = null;
this.prev = null;
this.twin = null;
this.org = null;
this.IsPointAtLeft = function(n) {
return CCW(n, this.org, this.twin.org);
};
this.IsPointAtRight = function(n) {
return CCW(n, this.twin.org, this.org);
}
}
function SpliceEdge(e1, e2) {
e1.prev.next = e2;
e2.prev.next = e1;
var tmp = e1.prev;
e1.prev = e2.prev;
e2.prev = tmp;
}
function MaxEdge() {
this.le = null;
this.re = null;
}
function Tin() {
this.sorted = false;
this.nodes = new Array();
this.edges = new Array();
this.free_edge = null;
this.AddNode = function(x, y) {
var n = new TinNode();
n.x = x;
n.y = y;
this.nodes.push(n);
this.sorted = false;
};
this.Clear = function() {
this.nodes.length = 0;
this.edges.length = 0;
this.free_edge = null;
this.sorted = false;
};
this.SortNodes = function() {
this.nodes.sort(NodeCmp);
var ns = new Array();
var ln = this.nodes[0];
ns.push(ln);
for (var i = 1; i < this.nodes.length; i++) {
if (NodeCmp(this.nodes[i], ln) != 0) {
ns.push(this.nodes[i]);
ln = this.nodes[i];
}
}
this.nodes = ns;
this.sorted = true;
};
this.DeleteEdgePair = function(e) {
SpliceEdge(e, e.twin.next);
SpliceEdge(e.twin, e.next);
e.org = null;
e.twin.org = null;
e.next = e.twin;
e.twin.next = this.free_edge;
this.free_edge = e;
};
this.NewEdge = function() {
if (this.free_edge != null) {
var ret = this.free_edge;
this.free_edge = this.free_edge.next;
return ret;
}
var e = new TinEdge();
this.edges.push(e);
return e;
};
this.MakeEdge = function(org, dst) {
var e1 = this.NewEdge();
var e2 = this.NewEdge();
e1.twin = e2;
e2.twin = e1;
e1.next = e2;
e2.next = e1;
e1.prev = e2;
e2.prev = e1;
e1.org = org;
e2.org = dst;
return e1;
};
this.ConnectEdge = function(e1, e2) {
var e = this.MakeEdge(e1.twin.org, e2.org);
SpliceEdge(e, e1.next);
SpliceEdge(e.twin, e2);
return e;
};
this.Valid = function(e, basel) {
return CCW(e.twin.org, basel.twin.org, basel.org);
};
this.Build = function() {
if (this.nodes.length < 2)
return;
this.edges.length = 0;
if (!this.sorted)
this.SortNodes();
this.BuildBetween(0, this.nodes.length - 1);
var es = new Array();
for (var i = 0; i < this.edges.length; i++) {
var e = this.edges[i];
if (e.org != null) {
es.push(e);
}
}
this.edges = es;
this.free_edge = null;
};
this.BuildBetween = function(bi, ei) {
var nodeCount = ei - bi + 1;
var maxEdge = new MaxEdge();
if (nodeCount == 2) {
var e = this.MakeEdge(this.nodes[bi], this.nodes[ei]);
maxEdge.le = e;
maxEdge.re = e.twin;
return maxEdge;
} else if (nodeCount == 3) {
var e1 = this.MakeEdge(this.nodes[bi], this.nodes[bi + 1]);
var e2 = this.MakeEdge(this.nodes[bi + 1], this.nodes[ei]);
SpliceEdge(e1.twin, e2);
if (CCW(this.nodes[bi], this.nodes[bi + 1], this.nodes[ei])) {
this.ConnectEdge(e2, e1);
maxEdge.le = e1;
maxEdge.re = (e2.twin);
return maxEdge;
} else if (CCW(this.nodes[bi], this.nodes[ei], this.nodes[bi + 1])) {
var e3 = this.ConnectEdge(e2, e1);
maxEdge.le = e3.twin;
maxEdge.re = e3;
return maxEdge;
} else {
maxEdge.le = e1;
maxEdge.re = e2.twin;
return maxEdge;
}
} else {
var mid = Math.floor((bi + ei) / 2);
var leftRet = this.BuildBetween(bi, mid);
var rightRet = this.BuildBetween(mid + 1, ei);
var ldo = leftRet.le;
var ldi = leftRet.re;
var rdi = rightRet.le;
var rdo = rightRet.re;
while (true) {
if (ldi.IsPointAtLeft(rdi.org)) {
ldi = ldi.next;
} else if (rdi.IsPointAtRight(ldi.org)) {
rdi = rdi.twin.prev.twin;
} else {
break;
}
}
var basel = this.ConnectEdge(rdi.twin, ldi);
if (ldi.org == ldo.org) {
ldo = basel.twin;
}
if (rdi.org == rdo.org) {
rdo = basel;
}
while (true) {
var lcand = basel.twin.prev.twin;
var t = null;
if (this.Valid(lcand, basel)) {
while (InCircle(basel.twin.org, basel.org, lcand.twin.org, lcand.prev.org)) {
t = lcand.prev.twin;
this.DeleteEdgePair(lcand);
lcand = t;
}
}
// symmetrically, locate the first R point to be hit, and delete R edges
var rcand = (basel.twin.next);
if (this.Valid(rcand, basel)) {
while (InCircle(basel.twin.org, basel.org, rcand.twin.org, rcand.twin.next.twin.org)) {
t = rcand.twin.next;
this.DeleteEdgePair(rcand);
rcand = t;
}
}
if ((!this.Valid(lcand, basel)) && (!this.Valid(rcand, basel))) {
break;
}
if ((!this.Valid(lcand, basel)) || this.Valid(rcand, basel) && InCircle(lcand.twin.org, lcand.org, rcand.org, rcand.twin.org)) {
basel = this.ConnectEdge(rcand, basel.twin);
} else {
basel = this.ConnectEdge(basel.twin, lcand.twin);
}
}
maxEdge.le = ldo;
maxEdge.re = rdo;
return maxEdge;
}
};
}