-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapSim.js
3750 lines (3738 loc) · 147 KB
/
MapSim.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
/*!
Copyright (C) 2010-2013 Raymond Hill: https://github.com/gorhill/Javascript-Voronoi
MIT License: See https://github.com/gorhill/Javascript-Voronoi/LICENSE.md
*/
/*
Author: Raymond Hill ([email protected])
Contributor: Jesse Morgan ([email protected])
Contributor (conversion to typescript): Sid Datta (https://github.com/dattasid)
File: rhill-voronoi-core.js
Version: 0.98
Date: January 21, 2013
Description: This is my personal Javascript implementation of
Steven Fortune's algorithm to compute Voronoi diagrams.
License: See https://github.com/gorhill/Javascript-Voronoi/LICENSE.md
Credits: See https://github.com/gorhill/Javascript-Voronoi/CREDITS.md
History: See https://github.com/gorhill/Javascript-Voronoi/CHANGELOG.md
## Usage:
var sites = [{x:300,y:300}, {x:100,y:100}, {x:200,y:500}, {x:250,y:450}, {x:600,y:150}];
// xl, xr means x left, x right
// yt, yb means y top, y bottom
var bbox = {xl:0, xr:800, yt:0, yb:600};
var voronoi = new Voronoi();
// pass an object which exhibits xl, xr, yt, yb properties. The bounding
// box will be used to connect unbound edges, and to close open cells
result = voronoi.compute(sites, bbox);
// render, further analyze, etc.
Return value:
An object with the following properties:
result.vertices = an array of unordered, unique Voronoi.Vertex objects making
up the Voronoi diagram.
result.edges = an array of unordered, unique Voronoi.Edge objects making up
the Voronoi diagram.
result.cells = an array of Voronoi.Cell object making up the Voronoi diagram.
A Cell object might have an empty array of halfedges, meaning no Voronoi
cell could be computed for a particular cell.
result.execTime = the time it took to compute the Voronoi diagram, in
milliseconds.
Voronoi.Vertex object:
x: The x position of the vertex.
y: The y position of the vertex.
Voronoi.Edge object:
lSite: the Voronoi site object at the left of this Voronoi.Edge object.
rSite: the Voronoi site object at the right of this Voronoi.Edge object (can
be null).
va: an object with an 'x' and a 'y' property defining the start point
(relative to the Voronoi site on the left) of this Voronoi.Edge object.
vb: an object with an 'x' and a 'y' property defining the end point
(relative to Voronoi site on the left) of this Voronoi.Edge object.
For edges which are used to close open cells (using the supplied bounding
box), the rSite property will be null.
Voronoi.Cell object:
site: the Voronoi site object associated with the Voronoi cell.
halfedges: an array of Voronoi.Halfedge objects, ordered counterclockwise,
defining the polygon for this Voronoi cell.
Voronoi.Halfedge object:
site: the Voronoi site object owning this Voronoi.Halfedge object.
edge: a reference to the unique Voronoi.Edge object underlying this
Voronoi.Halfedge object.
getStartpoint(): a method returning an object with an 'x' and a 'y' property
for the start point of this halfedge. Keep in mind halfedges are always
countercockwise.
getEndpoint(): a method returning an object with an 'x' and a 'y' property
for the end point of this halfedge. Keep in mind halfedges are always
countercockwise.
TODO: Identify opportunities for performance improvement.
TODO: Let the user close the Voronoi cells, do not do it automatically. Not only let
him close the cells, but also allow him to close more than once using a different
bounding box for the same Voronoi diagram.
*/
define("rhill-voronoi-core", ["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/*global Math */
// ---------------------------------------------------------------------------
class RBNode {
constructor() {
this.rbPrevious = null;
this.rbNext = null;
this.rbLeft = null;
this.rbRight = null;
this.rbParent = null;
this.rbRed = false;
}
}
class RBTree {
constructor() {
this.root = null;
this.rbRotateRight = function (node) {
var p = node, q = node.rbLeft, // can't be null
parent = p.rbParent;
if (parent) {
if (parent.rbLeft === p) {
parent.rbLeft = q;
}
else {
parent.rbRight = q;
}
}
else {
this.root = q;
}
q.rbParent = parent;
p.rbParent = q;
p.rbLeft = q.rbRight;
if (p.rbLeft) {
p.rbLeft.rbParent = p;
}
q.rbRight = p;
};
}
rbInsertSuccessor(node, successor) {
var parent;
if (node) {
// >>> rhill 2011-05-27: Performance: cache previous/next nodes
successor.rbPrevious = node;
successor.rbNext = node.rbNext;
if (node.rbNext) {
node.rbNext.rbPrevious = successor;
}
node.rbNext = successor;
// <<<
if (node.rbRight) {
// in-place expansion of node.rbRight.getFirst();
node = node.rbRight;
while (node.rbLeft) {
node = node.rbLeft;
}
node.rbLeft = successor;
}
else {
node.rbRight = successor;
}
parent = node;
}
else if (this.root) {
node = this.getFirst(this.root);
// >>> Performance: cache previous/next nodes
successor.rbPrevious = null;
successor.rbNext = node;
node.rbPrevious = successor;
// <<<
node.rbLeft = successor;
parent = node;
}
else {
// >>> Performance: cache previous/next nodes
successor.rbPrevious = successor.rbNext = null;
// <<<
this.root = successor;
parent = null;
}
successor.rbLeft = successor.rbRight = null;
successor.rbParent = parent;
successor.rbRed = true;
// Fixup the modified tree by recoloring nodes and performing
// rotations (2 at most) hence the red-black tree properties are
// preserved.
var grandpa, uncle;
node = successor;
while (parent && parent.rbRed) {
grandpa = parent.rbParent;
if (parent === grandpa.rbLeft) {
uncle = grandpa.rbRight;
if (uncle && uncle.rbRed) {
parent.rbRed = uncle.rbRed = false;
grandpa.rbRed = true;
node = grandpa;
}
else {
if (node === parent.rbRight) {
this.rbRotateLeft(parent);
node = parent;
parent = node.rbParent;
}
parent.rbRed = false;
grandpa.rbRed = true;
this.rbRotateRight(grandpa);
}
}
else {
uncle = grandpa.rbLeft;
if (uncle && uncle.rbRed) {
parent.rbRed = uncle.rbRed = false;
grandpa.rbRed = true;
node = grandpa;
}
else {
if (node === parent.rbLeft) {
this.rbRotateRight(parent);
node = parent;
parent = node.rbParent;
}
parent.rbRed = false;
grandpa.rbRed = true;
this.rbRotateLeft(grandpa);
}
}
parent = node.rbParent;
}
this.root.rbRed = false;
}
rbRemoveNode(node) {
// >>> rhill 2011-05-27: Performance: cache previous/next nodes
if (node.rbNext) {
node.rbNext.rbPrevious = node.rbPrevious;
}
if (node.rbPrevious) {
node.rbPrevious.rbNext = node.rbNext;
}
node.rbNext = node.rbPrevious = null;
// <<<
var parent = node.rbParent, left = node.rbLeft, right = node.rbRight, next;
if (!left) {
next = right;
}
else if (!right) {
next = left;
}
else {
next = this.getFirst(right);
}
if (parent) {
if (parent.rbLeft === node) {
parent.rbLeft = next;
}
else {
parent.rbRight = next;
}
}
else {
this.root = next;
}
// enforce red-black rules
var isRed;
if (left && right) {
isRed = next.rbRed;
next.rbRed = node.rbRed;
next.rbLeft = left;
left.rbParent = next;
if (next !== right) {
parent = next.rbParent;
next.rbParent = node.rbParent;
node = next.rbRight;
parent.rbLeft = node;
next.rbRight = right;
right.rbParent = next;
}
else {
next.rbParent = parent;
parent = next;
node = next.rbRight;
}
}
else {
isRed = node.rbRed;
node = next;
}
// 'node' is now the sole successor's child and 'parent' its
// new parent (since the successor can have been moved)
if (node) {
node.rbParent = parent;
}
// the 'easy' cases
if (isRed) {
return;
}
if (node && node.rbRed) {
node.rbRed = false;
return;
}
// the other cases
var sibling;
do {
if (node === this.root) {
break;
}
if (node === parent.rbLeft) {
sibling = parent.rbRight;
if (sibling.rbRed) {
sibling.rbRed = false;
parent.rbRed = true;
this.rbRotateLeft(parent);
sibling = parent.rbRight;
}
if ((sibling.rbLeft && sibling.rbLeft.rbRed) || (sibling.rbRight && sibling.rbRight.rbRed)) {
if (!sibling.rbRight || !sibling.rbRight.rbRed) {
sibling.rbLeft.rbRed = false;
sibling.rbRed = true;
this.rbRotateRight(sibling);
sibling = parent.rbRight;
}
sibling.rbRed = parent.rbRed;
parent.rbRed = sibling.rbRight.rbRed = false;
this.rbRotateLeft(parent);
node = this.root;
break;
}
}
else {
sibling = parent.rbLeft;
if (sibling.rbRed) {
sibling.rbRed = false;
parent.rbRed = true;
this.rbRotateRight(parent);
sibling = parent.rbLeft;
}
if ((sibling.rbLeft && sibling.rbLeft.rbRed) || (sibling.rbRight && sibling.rbRight.rbRed)) {
if (!sibling.rbLeft || !sibling.rbLeft.rbRed) {
sibling.rbRight.rbRed = false;
sibling.rbRed = true;
this.rbRotateLeft(sibling);
sibling = parent.rbLeft;
}
sibling.rbRed = parent.rbRed;
parent.rbRed = sibling.rbLeft.rbRed = false;
this.rbRotateRight(parent);
node = this.root;
break;
}
}
sibling.rbRed = true;
node = parent;
parent = parent.rbParent;
} while (!node.rbRed);
if (node) {
node.rbRed = false;
}
}
rbRotateLeft(node) {
var p = node, q = node.rbRight, // can't be null
parent = p.rbParent;
if (parent) {
if (parent.rbLeft === p) {
parent.rbLeft = q;
}
else {
parent.rbRight = q;
}
}
else {
this.root = q;
}
q.rbParent = parent;
p.rbParent = q;
p.rbRight = q.rbLeft;
if (p.rbRight) {
p.rbRight.rbParent = p;
}
q.rbLeft = p;
}
getFirst(node) {
while (node.rbLeft) {
node = node.rbLeft;
}
return node;
}
getLast(node) {
while (node.rbRight) {
node = node.rbRight;
}
return node;
}
}
class BBox {
}
exports.BBox = BBox;
class Cell {
constructor(site) {
this.site = site;
this.halfedges = [];
this.closeMe = false;
}
init(site) {
this.site = site;
this.halfedges = [];
this.closeMe = false;
return this;
}
prepareHalfedges() {
var halfedges = this.halfedges, iHalfedge = halfedges.length, edge;
// get rid of unused halfedges
// rhill 2011-05-27: Keep it simple, no point here in trying
// to be fancy: dangling edges are a typically a minority.
while (iHalfedge--) {
edge = halfedges[iHalfedge].edge;
if (!edge.vb || !edge.va) {
halfedges.splice(iHalfedge, 1);
}
}
// rhill 2011-05-26: I tried to use a binary search at insertion
// time to keep the array sorted on-the-fly (in Cell.addHalfedge()).
// There was no real benefits in doing so, performance on
// Firefox 3.6 was improved marginally, while performance on
// Opera 11 was penalized marginally.
halfedges.sort(function (a, b) { return b.angle - a.angle; });
return halfedges.length;
}
// Return a list of the neighbor Ids
getNeighborIds() {
var neighbors = [], iHalfedge = this.halfedges.length, edge;
while (iHalfedge--) {
edge = this.halfedges[iHalfedge].edge;
if (edge.lSite !== null && edge.lSite.voronoiId != this.site.voronoiId) {
neighbors.push(edge.lSite.voronoiId);
}
else if (edge.rSite !== null && edge.rSite.voronoiId != this.site.voronoiId) {
neighbors.push(edge.rSite.voronoiId);
}
}
return neighbors;
}
// Compute bounding box
//
getBbox() {
let halfedges = this.halfedges, iHalfedge = halfedges.length, xmin = Infinity, ymin = Infinity, xmax = -Infinity, ymax = -Infinity, v, vx, vy;
while (iHalfedge--) {
v = halfedges[iHalfedge].getStartpoint();
vx = v.x;
vy = v.y;
if (vx < xmin) {
xmin = vx;
}
if (vy < ymin) {
ymin = vy;
}
if (vx > xmax) {
xmax = vx;
}
if (vy > ymax) {
ymax = vy;
}
// we dont need to take into account end point,
// since each end point matches a start point
}
return {
xl: xmin,
yt: ymin,
xr: xmax,
yb: ymax
// width: xmax-xmin,
// height: ymax-ymin
};
}
// Return whether a point is inside, on, or outside the cell:
// -1: point is outside the perimeter of the cell
// 0: point is on the perimeter of the cell
// 1: point is inside the perimeter of the cell
//
pointIntersection(x, y) {
// Check if point in polygon. Since all polygons of a Voronoi
// diagram are convex, then:
// http://paulbourke.net/geometry/polygonmesh/
// Solution 3 (2D):
// "If the polygon is convex then one can consider the polygon
// "as a 'path' from the first vertex. A point is on the interior
// "of this polygons if it is always on the same side of all the
// "line segments making up the path. ...
// "(y - y0) (x1 - x0) - (x - x0) (y1 - y0)
// "if it is less than 0 then P is to the right of the line segment,
// "if greater than 0 it is to the left, if equal to 0 then it lies
// "on the line segment"
let halfedges = this.halfedges, iHalfedge = halfedges.length, halfedge, p0, p1, r;
while (iHalfedge--) {
halfedge = halfedges[iHalfedge];
p0 = halfedge.getStartpoint();
p1 = halfedge.getEndpoint();
r = (y - p0.y) * (p1.x - p0.x) - (x - p0.x) * (p1.y - p0.y);
if (!r) {
return 0;
}
if (r > 0) {
return -1;
}
}
return 1;
}
}
class Vertex {
// readonly id = Vertex._id++
constructor(x, y) {
this.x = x;
this.y = y;
}
}
exports.Vertex = Vertex;
class Site {
constructor() {
this.x = 0;
this.y = 0;
this.voronoiId = 0;
}
}
exports.Site = Site;
class Edge {
constructor(lSite, rSite) {
this.lSite = lSite;
this.rSite = rSite;
this.va = this.vb = null;
}
}
exports.Edge = Edge;
class Halfedge {
constructor(edge, lSite, rSite) {
this.site = null;
this.edge = null;
this.site = lSite;
this.edge = edge;
// 'angle' is a value to be used for properly sorting the
// halfsegments counterclockwise. By convention, we will
// use the angle of the line defined by the 'site to the left'
// to the 'site to the right'.
// However, border edges have no 'site to the right': thus we
// use the angle of line perpendicular to the halfsegment (the
// edge should have both end points defined in such case.)
if (rSite) {
this.angle = Math.atan2(rSite.y - lSite.y, rSite.x - lSite.x);
}
else {
var va = edge.va, vb = edge.vb;
// rhill 2011-05-31: used to call getStartpoint()/getEndpoint(),
// but for performance purpose, these are expanded in place here.
this.angle = edge.lSite === lSite ?
Math.atan2(vb.x - va.x, va.y - vb.y) :
Math.atan2(va.x - vb.x, vb.y - va.y);
}
}
getStartpoint() {
return this.edge.lSite === this.site ? this.edge.va : this.edge.vb;
}
getEndpoint() {
return this.edge.lSite === this.site ? this.edge.vb : this.edge.va;
}
}
// ---------------------------------------------------------------------------
// Beachline methods
// rhill 2011-06-07: For some reasons, performance suffers significantly
// when instanciating a literal object instead of an empty ctor
class Beachsection extends RBNode {
constructor() {
super();
this.site = null;
this.circleEvent = null;
this.edge = null;
}
}
;
// rhill 2011-06-07: For some reasons, performance suffers significantly
// when instanciating a literal object instead of an empty ctor
class CircleEvent extends RBNode {
constructor() {
super();
this.site = null;
// rhill 2013-10-12: it helps to state exactly what we are at ctor time.
this.arc = null;
this.rbLeft = null;
this.rbNext = null;
this.rbParent = null;
this.rbPrevious = null;
this.rbRed = false;
this.rbRight = null;
this.site = null;
this.x = this.y = this.ycenter = 0;
}
}
// ---------------------------------------------------------------------------
// Diagram methods
class Diagram {
constructor() {
}
}
exports.Diagram = Diagram;
class Voronoi {
constructor() {
this.vertices = null;
this.edges = null;
this.cells = null;
this.toRecycle = null;
this.beachsectionJunkyard = [];
this.circleEventJunkyard = [];
this.vertexJunkyard = [];
this.edgeJunkyard = [];
this.cellJunkyard = [];
this.beachline = null;
this.circleEvents = null;
this.firstCircleEvent = null;
this.sqrt = Math.sqrt;
this.abs = Math.abs;
this.equalWithEpsilon = function (a, b) { return this.abs(a - b) < Voronoi.ε; };
this.greaterThanWithEpsilon = function (a, b) { return a - b > Voronoi.ε; };
this.greaterThanOrEqualWithEpsilon = function (a, b) { return b - a < Voronoi.ε; };
this.lessThanWithEpsilon = function (a, b) { return b - a > Voronoi.ε; };
this.lessThanOrEqualWithEpsilon = function (a, b) { return a - b < Voronoi.ε; };
// calculate the right break point of a particular beach section,
// given a particular directrix
this.rightBreakPoint = function (arc, directrix) {
var rArc = arc.rbNext;
if (rArc) {
return this.leftBreakPoint(rArc, directrix);
}
var site = arc.site;
return site.y === directrix ? site.x : Infinity;
};
}
// ---------------------------------------------------------------------------
reset() {
if (!this.beachline) {
this.beachline = new RBTree();
}
// Move leftover beachsections to the beachsection junkyard.
if (this.beachline.root) {
var beachsection = this.beachline.getFirst(this.beachline.root);
while (beachsection) {
this.beachsectionJunkyard.push(beachsection); // mark for reuse
beachsection = beachsection.rbNext;
}
}
this.beachline.root = null;
if (!this.circleEvents) {
this.circleEvents = new RBTree();
}
this.circleEvents.root = this.firstCircleEvent = null;
this.vertices = [];
this.edges = [];
this.cells = [];
}
// ---------------------------------------------------------------------------
// Red-Black tree code (based on C version of "rbtree" by Franck Bui-Huu
// https://github.com/fbuihuu/libtree/blob/master/rb.c
// ---------------------------------------------------------------------------
// Cell methods
createCell(site) {
var cell = this.cellJunkyard.pop();
if (cell) {
return cell.init(site);
}
return new Cell(site);
}
// ---------------------------------------------------------------------------
// Edge methods
//
createHalfedge(edge, lSite, rSite) {
return new Halfedge(edge, lSite, rSite);
}
// this create and add a vertex to the internal collection
createVertex(x, y) {
var v = this.vertexJunkyard.pop();
if (!v) {
v = new Vertex(x, y);
}
else {
v.x = x;
v.y = y;
}
this.vertices.push(v);
// TODO: Figure out why same vertex repeated multiple times
// Note: Only Vertices on bounding box are repeated. They are
// created from connectEdge and closeCells.
// Since its only vertices on BB, a) They can be resolved by comparing
// x and y simply enough b) Voronoi edge graph should be easy enough
// to make for most purposes.
// console.log(v)
// console.trace()
return v;
}
// this create and add an edge to internal collection, and also create
// two halfedges which are added to each site's counterclockwise array
// of halfedges.
createEdge(lSite, rSite, va, vb) {
var edge = this.edgeJunkyard.pop();
if (!edge) {
edge = new Edge(lSite, rSite);
}
else {
edge.lSite = lSite;
edge.rSite = rSite;
edge.va = edge.vb = null;
}
this.edges.push(edge);
if (va) {
this.setEdgeStartpoint(edge, lSite, rSite, va);
}
if (vb) {
this.setEdgeEndpoint(edge, lSite, rSite, vb);
}
this.cells[lSite.voronoiId].halfedges.push(this.createHalfedge(edge, lSite, rSite));
this.cells[rSite.voronoiId].halfedges.push(this.createHalfedge(edge, rSite, lSite));
return edge;
}
createBorderEdge(lSite, va, vb) {
var edge = this.edgeJunkyard.pop();
if (!edge) {
edge = new Edge(lSite, null);
}
else {
edge.lSite = lSite;
edge.rSite = null;
}
edge.va = va;
edge.vb = vb;
this.edges.push(edge);
return edge;
}
setEdgeStartpoint(edge, lSite, rSite, vertex) {
if (!edge.va && !edge.vb) {
edge.va = vertex;
edge.lSite = lSite;
edge.rSite = rSite;
}
else if (edge.lSite === rSite) {
edge.vb = vertex;
}
else {
edge.va = vertex;
}
}
setEdgeEndpoint(edge, lSite, rSite, vertex) {
this.setEdgeStartpoint(edge, rSite, lSite, vertex);
}
// rhill 2011-06-02: A lot of Beachsection instanciations
// occur during the computation of the Voronoi diagram,
// somewhere between the number of sites and twice the
// number of sites, while the number of Beachsections on the
// beachline at any given time is comparatively low. For this
// reason, we reuse already created Beachsections, in order
// to avoid new memory allocation. This resulted in a measurable
// performance gain.
createBeachsection(site) {
var beachsection = this.beachsectionJunkyard.pop();
if (!beachsection) {
beachsection = new Beachsection();
}
beachsection.site = site;
return beachsection;
}
// calculate the left break point of a particular beach section,
// given a particular sweep line
leftBreakPoint(arc, directrix) {
// http://en.wikipedia.org/wiki/Parabola
// http://en.wikipedia.org/wiki/Quadratic_equation
// h1 = x1,
// k1 = (y1+directrix)/2,
// h2 = x2,
// k2 = (y2+directrix)/2,
// p1 = k1-directrix,
// a1 = 1/(4*p1),
// b1 = -h1/(2*p1),
// c1 = h1*h1/(4*p1)+k1,
// p2 = k2-directrix,
// a2 = 1/(4*p2),
// b2 = -h2/(2*p2),
// c2 = h2*h2/(4*p2)+k2,
// x = (-(b2-b1) + Math.sqrt((b2-b1)*(b2-b1) - 4*(a2-a1)*(c2-c1))) / (2*(a2-a1))
// When x1 become the x-origin:
// h1 = 0,
// k1 = (y1+directrix)/2,
// h2 = x2-x1,
// k2 = (y2+directrix)/2,
// p1 = k1-directrix,
// a1 = 1/(4*p1),
// b1 = 0,
// c1 = k1,
// p2 = k2-directrix,
// a2 = 1/(4*p2),
// b2 = -h2/(2*p2),
// c2 = h2*h2/(4*p2)+k2,
// x = (-b2 + Math.sqrt(b2*b2 - 4*(a2-a1)*(c2-k1))) / (2*(a2-a1)) + x1
// change code below at your own risk: care has been taken to
// reduce errors due to computers' finite arithmetic precision.
// Maybe can still be improved, will see if any more of this
// kind of errors pop up again.
var site = arc.site, rfocx = site.x, rfocy = site.y, pby2 = rfocy - directrix;
// parabola in degenerate case where focus is on directrix
if (!pby2) {
return rfocx;
}
var lArc = arc.rbPrevious;
if (!lArc) {
return -Infinity;
}
site = lArc.site;
var lfocx = site.x, lfocy = site.y, plby2 = lfocy - directrix;
// parabola in degenerate case where focus is on directrix
if (!plby2) {
return lfocx;
}
var hl = lfocx - rfocx, aby2 = 1 / pby2 - 1 / plby2, b = hl / plby2;
if (aby2) {
return (-b + this.sqrt(b * b - 2 * aby2 * (hl * hl / (-2 * plby2) - lfocy + plby2 / 2 + rfocy - pby2 / 2))) / aby2 + rfocx;
}
// both parabolas have same distance to directrix, thus break point is midway
return (rfocx + lfocx) / 2;
}
detachBeachsection(beachsection) {
this.detachCircleEvent(beachsection); // detach potentially attached circle event
this.beachline.rbRemoveNode(beachsection); // remove from RB-tree
this.beachsectionJunkyard.push(beachsection); // mark for reuse
}
;
removeBeachsection(beachsection) {
var circle = beachsection.circleEvent, x = circle.x, y = circle.ycenter, vertex = this.createVertex(x, y), previous = beachsection.rbPrevious, next = beachsection.rbNext, disappearingTransitions = [beachsection], abs_fn = Math.abs;
// remove collapsed beachsection from beachline
this.detachBeachsection(beachsection);
// there could be more than one empty arc at the deletion point, this
// happens when more than two edges are linked by the same vertex,
// so we will collect all those edges by looking up both sides of
// the deletion point.
// by the way, there is *always* a predecessor/successor to any collapsed
// beach section, it's just impossible to have a collapsing first/last
// beach sections on the beachline, since they obviously are unconstrained
// on their left/right side.
// look left
var lArc = previous;
while (lArc.circleEvent && abs_fn(x - lArc.circleEvent.x) < 1e-9 && abs_fn(y - lArc.circleEvent.ycenter) < 1e-9) {
previous = lArc.rbPrevious;
disappearingTransitions.unshift(lArc);
this.detachBeachsection(lArc); // mark for reuse
lArc = previous;
}
// even though it is not disappearing, I will also add the beach section
// immediately to the left of the left-most collapsed beach section, for
// convenience, since we need to refer to it later as this beach section
// is the 'left' site of an edge for which a start point is set.
disappearingTransitions.unshift(lArc);
this.detachCircleEvent(lArc);
// look right
var rArc = next;
while (rArc.circleEvent && abs_fn(x - rArc.circleEvent.x) < 1e-9 && abs_fn(y - rArc.circleEvent.ycenter) < 1e-9) {
next = rArc.rbNext;
disappearingTransitions.push(rArc);
this.detachBeachsection(rArc); // mark for reuse
rArc = next;
}
// we also have to add the beach section immediately to the right of the
// right-most collapsed beach section, since there is also a disappearing
// transition representing an edge's start point on its left.
disappearingTransitions.push(rArc);
this.detachCircleEvent(rArc);
// walk through all the disappearing transitions between beach sections and
// set the start point of their (implied) edge.
var nArcs = disappearingTransitions.length, iArc;
for (iArc = 1; iArc < nArcs; iArc++) {
rArc = disappearingTransitions[iArc];
lArc = disappearingTransitions[iArc - 1];
this.setEdgeStartpoint(rArc.edge, lArc.site, rArc.site, vertex);
}
// create a new edge as we have now a new transition between
// two beach sections which were previously not adjacent.
// since this edge appears as a new vertex is defined, the vertex
// actually define an end point of the edge (relative to the site
// on the left)
lArc = disappearingTransitions[0];
rArc = disappearingTransitions[nArcs - 1];
rArc.edge = this.createEdge(lArc.site, rArc.site, undefined, vertex);
// create circle events if any for beach sections left in the beachline
// adjacent to collapsed sections
this.attachCircleEvent(lArc);
this.attachCircleEvent(rArc);
}
addBeachsection(site) {
var x = site.x, directrix = site.y;
// find the left and right beach sections which will surround the newly
// created beach section.
// rhill 2011-06-01: This loop is one of the most often executed,
// hence we expand in-place the comparison-against-epsilon calls.
var lArc, rArc, dxl, dxr, node = this.beachline.root;
while (node) {
dxl = this.leftBreakPoint(node, directrix) - x;
// x lessThanWithEpsilon xl => falls somewhere before the left edge of the beachsection
if (dxl > 1e-9) {
// this case should never happen
// if (!node.rbLeft) {
// rArc = node.rbLeft;
// break;
// }
node = node.rbLeft;
}
else {
dxr = x - this.rightBreakPoint(node, directrix);
// x greaterThanWithEpsilon xr => falls somewhere after the right edge of the beachsection
if (dxr > 1e-9) {
if (!node.rbRight) {
lArc = node;
break;
}
node = node.rbRight;
}
else {
// x equalWithEpsilon xl => falls exactly on the left edge of the beachsection
if (dxl > -1e-9) {
lArc = node.rbPrevious;
rArc = node;
}
else if (dxr > -1e-9) {
lArc = node;
rArc = node.rbNext;
}
else {
lArc = rArc = node;
}
break;
}
}
}
// at this point, keep in mind that lArc and/or rArc could be
// undefined or null.
// create a new beach section object for the site and add it to RB-tree
var newArc = this.createBeachsection(site);
this.beachline.rbInsertSuccessor(lArc, newArc);
// cases:
//
// [null,null]
// least likely case: new beach section is the first beach section on the
// beachline.
// This case means:
// no new transition appears
// no collapsing beach section
// new beachsection become root of the RB-tree
if (!lArc && !rArc) {
return;
}
// [lArc,rArc] where lArc == rArc
// most likely case: new beach section split an existing beach
// section.
// This case means:
// one new transition appears
// the left and right beach section might be collapsing as a result
// two new nodes added to the RB-tree
if (lArc === rArc) {
// invalidate circle event of split beach section
this.detachCircleEvent(lArc);
// split the beach section into two separate beach sections
rArc = this.createBeachsection(lArc.site);
this.beachline.rbInsertSuccessor(newArc, rArc);
// since we have a new transition between two beach sections,
// a new edge is born
newArc.edge = rArc.edge = this.createEdge(lArc.site, newArc.site);
// check whether the left and right beach sections are collapsing
// and if so create circle events, to be notified when the point of
// collapse is reached.
this.attachCircleEvent(lArc);
this.attachCircleEvent(rArc);
return;
}
// [lArc,null]
// even less likely case: new beach section is the *last* beach section
// on the beachline -- this can happen *only* if *all* the previous beach
// sections currently on the beachline share the same y value as
// the new beach section.
// This case means:
// one new transition appears
// no collapsing beach section as a result
// new beach section become right-most node of the RB-tree
if (lArc && !rArc) {
newArc.edge = this.createEdge(lArc.site, newArc.site);
return;
}
// [null,rArc]
// impossible case: because sites are strictly processed from top to bottom,
// and left to right, which guarantees that there will always be a beach section
// on the left -- except of course when there are no beach section at all on
// the beach line, which case was handled above.
// rhill 2011-06-02: No point testing in non-debug version
//if (!lArc && rArc) {
// throw "Voronoi.addBeachsection(): What is this I don't even";
// }
// [lArc,rArc] where lArc != rArc
// somewhat less likely case: new beach section falls *exactly* in between two
// existing beach sections
// This case means:
// one transition disappears
// two new transitions appear
// the left and right beach section might be collapsing as a result
// only one new node added to the RB-tree
if (lArc !== rArc) {
// invalidate circle events of left and right sites
this.detachCircleEvent(lArc);
this.detachCircleEvent(rArc);
// an existing transition disappears, meaning a vertex is defined at
// the disappearance point.
// since the disappearance is caused by the new beachsection, the
// vertex is at the center of the circumscribed circle of the left,
// new and right beachsections.
// http://mathforum.org/library/drmath/view/55002.html
// Except that I bring the origin at A to simplify
// calculation
var lSite = lArc.site, ax = lSite.x, ay = lSite.y, bx = site.x - ax, by = site.y - ay, rSite = rArc.site, cx = rSite.x - ax, cy = rSite.y - ay, d = 2 * (bx * cy - by * cx), hb = bx * bx + by * by, hc = cx * cx + cy * cy, vertex = this.createVertex((cy * hb - by * hc) / d + ax, (bx * hc - cx * hb) / d + ay);
// one transition disappear
this.setEdgeStartpoint(rArc.edge, lSite, rSite, vertex);