-
Notifications
You must be signed in to change notification settings - Fork 0
/
sweep.js
847 lines (656 loc) · 17.5 KB
/
sweep.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
//RectangleSweep.js
/**
* Given a set of rectangles, find a minimal covering set of concave polygons
* The rectangles are axis oriented
* They are given by the coordinate of their upper left corner, along with h and w
*/
/*window.onload= function(){
drawRects(base_set);
}*/
var sets = []
var currentSet = false;
prepCount=0;
//helper function THAT ASSUMES el is actually present in array
function removeFromArray(key, A){
var imin = 0, imax = A.length-1;
// continue searching while [imin,imax] is not empty
while (imax >= imin)
{
/* calculate the midpoint for roughly equal partition */
var imid = ~~((imin + imax)/2);
// determine which subarray to search
if (A[imid].x < key.x)
// change min index to search upper subarray
imin = imid +1;
else if (A[imid].x > key.x)
// change max index to search lower subarray
imax = imid-1;
else {
A.splice(imid, 1);
// key found at index imid
return imid;
}
}
// key not found
return null;
}
var test_set;
var NN = 20;
function init_test(){
test_set = [];
for(var n=0; n<NN; n++){
test_set.push({_x: ~~(Math.random()*100+1), _y: ~~(Math.random()*100+1), _w: 20, _h: 20});
}
return test_set;
}
function restart_test(){
//test_set = [];
for(var n=0; n<NN; n++){
test_set[n]._x = ~~(Math.random()*100) + 1 ;
test_set[n]._y = ~~(Math.random()*100) + 1;
}
return test_set;
test_set = [
{_x: 10, _y:10, _w: 10, _h:10},
{_x: 20, _y:10, _w:13, _h:10},
{_x: 20, _y:20, _w:10, _h:11},
{_x: 10, _y:20, _w:10, _h:10},
{_x: 105, _y:15, _w:10, _h:10},
{_x: 50, _y:50, _w:10, _h:20},
{_x: 70, _y:50, _w:10, _h:20},
{_x: 40, _y:55, _w:70, _h:10},
{_x: 10, _y:100, _w:10, _h:10},
{_x: 20, _y:110, _w:10, _h:10},
{_x: 50, _y:45, _w:100, _h:100},
/*{_x: 10, _y:10, _w: 10, _h:10},
{_x: 20, _y:10, _w:13, _h:10},
{_x: 20, _y:20, _w:10, _h:11},
{_x: 10, _y:20, _w:10, _h:10},
{_x: 105, _y:15, _w:10, _h:10},
{_x: 50, _y:50, _w:10, _h:20},
{_x: 70, _y:50, _w:10, _h:20},
{_x: 40, _y:55, _w:70, _h:10},
{_x: 10, _y:100, _w:10, _h:10},
{_x: 20, _y:110, _w:10, _h:10},
{_x: 50, _y:45, _w:100, _h:100},
{_x: 10, _y:10, _w: 10, _h:10},
{_x: 20, _y:10, _w:13, _h:10},
{_x: 20, _y:20, _w:10, _h:11},
{_x: 10, _y:20, _w:10, _h:10},
{_x: 105, _y:15, _w:10, _h:10},
{_x: 50, _y:50, _w:10, _h:20},
{_x: 70, _y:50, _w:10, _h:20},
{_x: 40, _y:55, _w:70, _h:10},
{_x: 10, _y:100, _w:10, _h:10},
{_x: 20, _y:110, _w:10, _h:10},
{_x: 50, _y:45, _w:100, _h:100}*/
];
return test_set;
}
test_set = init_test();
function newEvent(r, type){
if (type === 0)
return {type:type, r:r, y:r._y, top:null}
else
return {type:type, r:r, y:r._y + r._h, top:null}
}
function sortEvents(a, b){
/*if (a.y == b.y )
return (a.r._x - b.r._x)
else*/
return (a.y - b.y)
}
/*
* rectSet
* An object for tracking collections of intersecting rectangles and the path around them
*/
function rectSet(){
this.covers = [];
this.paths = [];
this.primed = null;
};
rectSet.prototype.merge = function (a){
if (!a.set){
a.set= this;
this.covers.push(a);;
if (a._x<this.x1)
this.x1 = a._x;
if (a._y < this.y1)
this.y1 = a._y;
if (a._y + a._h > this.y2)
this.y2 = a._y + a._h;
if (a._x + a._w > this.x2)
this.x2 = a._x + a._w;
} else {
// Push the lists from set a into this current set
var mset = a.set, l = mset.covers.length;
for (var i=0; i<l; ++i ){
mset.covers[i].set = this;
this.covers.push(mset.covers[i]);
}
// Use faster method on paths, since we don't need to set anything else
this.paths.push.apply(this.paths, mset.paths)
sets.splice( sets.indexOf(mset), 1)
//removeFromArray(mset, sets);
if (mset.x1 < this.x1)
this.x1 = mset.x1
if (mset.y1 < this.y1)
this.y1 = mset.y1;
if (mset.y2 > this.y2)
this.y2 = mset.y2;
if (mset.x2 > this.x2)
this.x2 = mset.x2;
}
};
rectSet.prototype.includes = function(e){
for (var i=0, l=this.covers.length; i<l; i++){
r = this.covers[i];
if (r._x < e._x + e._w && r._y < e._y + e._h
&& r._x + r._w > e._x && r._y + r._h > e._y)
return true
}
return false;
}
// This is the hard part
rectSet.prototype.addPoint = function(x, y){
//console.log("\n <><><> Adding point " + x + ", " + y)
//var p = this.newPoint(x, y);
var p = xEventStack.newPoint(x, y);
//Every other time, store the point to deal with later. (Points always come in connected pairs.)
if (this.primed === null){
this.primed = p;
return;
}
// If we make it here, we now have two points.
var q = this.primed;
this.primed = null;
//console.log("\n <><><> Adding segment " + q.x + ", " +q.y+ " | " +p.x + ", " + p.y + "")
// Attempt to attach the points.
// Returns either a push or an unshift function; null if the point doesn't attach anywhere.
// This can either be called or applied to an array to add points to the correct end.
// This is a little bit of voodoo, but by returning the required *functions* we avoid a lot of conditionals.
pAttach = this.findAttachment(p);
qAttach = this.findAttachment(q);
// If neither connnects, we have a new path
// If only one connects, add the other to its path
// If both connect, we need to merge the paths if there's more than 1 left
if(!pAttach && !qAttach){
this.paths.push([p, q]);
} else if (!qAttach){
pAttach.call(p.path, p);
pAttach.call(p.path, q);
} else if (!pAttach){
qAttach.call(q.path, q);
qAttach.call(q.path, p);
} else {
pAttach.call(p.path, p);
qAttach.call(q.path, q);
// Merge paths if they're different
// If they're matched up right, just attach one array to the other
// Otherwise, reverse one array so it *does* match up.
if (p.path !== q.path){
if(pAttach !== qAttach)
pAttach.apply(p.path, q.path)
else
pAttach.apply(p.path, q.path.reverse())
// Then remove the path we just joined to p.path
this.paths.splice( this.paths.indexOf(q.path), 1)
//removeFromArray(q.path, this.paths);
}
}
//delete p.path;
//delete q.path;
};
rectSet.prototype.findAttachment = function(p){
var i=0, path, paths=this.paths, px = p.x;
// Cycle through paths, checking first and last point to see if p is below any of them
while(path = paths[i++]){
// if p is the new beginning of the path, points need to be unshifted
if (px === path[0].x){
p.path=path;
return Array.prototype.unshift;
}
// p is the new end of the path, points need to be pushed
else if (px === path[path.length-1].x){
p.path=path;
return Array.prototype.push;
}
}
// If nothing is found, return null!
return null;
}
// Will be used to recycle objects
rectSet.prototype.newPoint = function(x, y){
return {x:x, y:y};
}
// Will be used to recycle objects
function makeSet(r){
var s= new rectSet();
r.set = s;
s.covers.push(r);
s.x1 = r._x;
s.x2 = r._x + r._w;
s.y1 = r._y;
s.y2 = r._y + r._h;
sets.push(s);
return s;
}
xEventStack = {
bin: [],
pointer: 0,
newEvent: function(x, r, type){
if (this.pointer < this.bin.length){
var p = this.bin[this.pointer++];
p.x = x;
p.r= r;
p.type = type;
return p;
} else{
var p = {x:x, r:r, type:type};
this.bin.push(p);
this.pointer++;
return p;
}
},
/*
function newEvent(r, type){
if (type === 0)
return {type:type, r:r, y:r._y, top:null}
else
return {type:type, r:r, y:r._y + r._h, top:null}
}
*/
newPoint: function(x, y){
if (this.pointer < this.bin.length){
var p = this.bin[this.pointer++];
p.x = x;
p.y = y;
return p;
} else
var p = {x:x, y:y};
this.bin.push(p);
this.pointer++;
return p;
},
reset: function(){
this.pointer = 0;
}
}
/* Object for handling events as we scan a line along the x axis */
var xEvents = {
events: [],
i:0,
next: function(){
if (this.i>=this.events.length)
return null
else
return this.events[this.i++];
},
nextMatch: function(e){
if (this.i < this.events.length && this.events[this.i].r === e.r ){
//console.log("match point at" + e.x + ", " + this.events[this.i].x)
return true
}else
return false
},
//TODO if this is the bottom of a rect, needs to remove the top corners
add: function(e){
//console.log("\nvvvv\n\nAdding " + e.r._x + ", " + (e.r._x + e.r._w));
//console.log("Current array: " + this.events.toSource());
var el = xEventStack.newEvent(e.r._x, e.r, 1) //this.newEvent(e.r._x, e.r, 1);
var er = xEventStack.newEvent(e.r._x + e.r._w, e.r, -1);
//var el = this.newEvent(e.r._x, e.r, 1) //this.newEvent(e.r._x, e.r, 1);
//var er = this.newEvent(e.r._x + e.r._w, e.r, -1);
var j=0, l = this.events.length, ev = this.events, ex = el.x;
while(j<l){
if (ex <= ev[j].x ){
ev.splice(j, 0, el);
break;
}
j++;
}
if (j>=l){
ev.push(el, er);
//console.log("New array: " + this.events.toSource() + "\n\n");
return;
}
ex = er.x;
while(j<l+1){
if (ex <= ev[j].x){
if (ex != ev[j].x || ev[j].type != 1 ){ // Make sure closing events are *after* opening events
ev.splice(j, 0, er);
return;
}
}
j++;
}
if (j>=l){
ev.push(er);
}
//console.log("New array: " + this.events.toSource() + "\n\n");
// Push open and close events
//this.events.push(, );
},
newEvent: function(x, r, type){
return {x:x, r:r, type:type}
},
remove: function(xe){
// Remove element
var j = this.events.indexOf(xe);
this.events.splice(j, 1);
//var j = removeFromArray(xe, this.events);
// If necessary, decrement counter, so that next() works properly.
if (j<= this.i)
this.i--;
},
prepare: function(){
//prepCount++;
this.i=0;
//this.events.sort(this.xsort)
},
reset: function(){
this.events.length = 0;
i=0;
},
};
// The algo works by sweeping downwards along the y axis.
// The beginning and end of a rectangle are added to a queue of events, which are sorted by y
// All the points added will have y coordinates along an event
function SweepMerge(rects){
var r, i, e, l;
var e0, e1;
var events = [];
sets=[];
for (var i=0, l=rects.length; i<l; ++i){
r = rects[i];
if (r.w <=0 || r._h <=0){
console.log("Impossible")
continue;
}
r.set = 0
e0 = newEvent(r, 0);
e1 = newEvent(r, 1);
e1.top = true;
events.push( e0, e1);
}
events.sort(sortEvents);
//console.log(events.length + " y events");
var i=0, j, k;
e = events[0]
//console.log("Starting y sweep")
xEvents.reset();
while (e){
//console.log("\n------------------\n")
//console.log("\n\nContinuing y sweep")
// start this set of events
if (!e.top)
xEvents.add(e);
currentY = e.y;
e = events[++i];
// as long as the next event shares the same y, add it as well.
while(e && e.y == currentY){
//console.log(e.toSource())
if (!e.top)
xEvents.add(e);
e = events[++i];
}
//Sort the events added
xEvents.prepare();
currentSet = false;
// Now proceed through the events in xEvents
// R is the number of open rectangles, L the number of open lines
//console.log("Starting x sweep along y=" + currentY)
//console.log(xEvents.events.toSource())
sweepX(currentY);
}
xEventStack.reset();
for (i=0, l=sets.length; i<l; i++){
s = sets[i];
s._x = s.x1;
s._y = s.y1;
s._w = s.x2 - s.x1;
s._h = s.y2 - s.y1;
}
//console.log("Returning sets #" + sets.length);
//console.log(sets.toSource())
return sets;
}
// Sweep along the x axis, merging overlapping rectangles and adding points
// Caveats: might be problems with rectangles of 0 height;
// Rectangles touching only at the corners might act a little weird
function sweepX(currentY){
var xe, r;
var currentX = false;
var U=0, B=0//, UL=false, BL=false; // Keep track of the number of rectangles above and below
var oldCorners = 0;
var newCorners;
xe = xEvents.next();
currentX = xe.x;
currentSet = false;
while(xe!==null){
//console.log("-> sweep tick " + xe.x)
// If necessary, find the currentSet, assigning a new one if empty
r = xe.r;
if (!currentSet){
if (!r.set) // could also check type of event?
makeSet(r);
currentSet = r.set
} else if (r.set !== currentSet){
//console.log("merging at x = " + currentX)
currentSet.merge(r);
}
// special case if rectangle has no events inside of it
// Process two events at once, no need to change U or B
if ( xEvents.nextMatch(xe) ){
if (U==0 || B==0){
if (U>0 || B>0){
currentSet.addPoint(xe.x, currentY);
xe=xEvents.next();
currentSet.addPoint(xe.x, currentY);
}
else if (currentY == r._y ){
currentSet.addPoint(xe.x, currentY);
xe=xEvents.next();
currentSet.addPoint(xe.x, currentY);
}else if ( U==0 && currentY == r._y + r._h ) {
currentSet.addPoint(xe.x, currentY);
xEvents.remove(xe);
xe=xEvents.next();
currentSet.addPoint(xe.x, currentY);
xEvents.remove(xe);
} else{
xEvents.next();
}
if (U + B == 0)
currentSet = false;
} else{
xEvents.next();
}
xe = xEvents.next();
if(xe)
currentX = xe.x;
continue;
}
//based on type, change the rect counters above and below currentY
if (r._y < currentY)
U += xe.type;
if (r._y + r._h > currentY)
B += xe.type;
else
xEvents.remove(xe);
// Increment
xe = xEvents.next();
// If we've moved along the x axis, stop to process the transition
// With some mild type coercion, we count the number of open quadrants around this point
// The current state (to the right) is tracked by U and B; the left state is maintained from the last time
// We add the point if there's on occupided quadrant (convex corner) or three (concave corner)
if (xe===null || (xe.x != currentX) ){
//console.log("pause to process at x = " + currentX + " and y = " + currentY)
//console.log(UL + " | " + U )
//console.log(BL + " | " + B )
//corners = UL + BL;
newCorners = (U>0) + (B>0);
//UL = U>0;
//BL = B>0;
if ( (oldCorners + newCorners) % 2 == 1)
currentSet.addPoint(currentX, currentY);
oldCorners = newCorners;
if (xe)
currentX = xe.x;
// close set if no rectangles are currently open
if (U + B == 0)
currentSet = false;
}
}
}
var lenna;
function Pre(){
drawRects(test_set);
}
function Run(){
TestSweep();
//Profile(SweepMerge);
console.log("Xeventstack bin size " + xEventStack.bin.length)
//console.log("Statico")
//Profile(staticoMerge);
}
function TestSweep(){
try{
//test_set = base_test;
SweepMerge(test_set);
//console.log("#sets " + sets.length);
drawBounds();
//var rs = staticoMerge(test_set);
//drawStatico(rs)
} catch(e){
console.log("Problem: " + e)
}
}
function Profile(merge_function){
/*lenna = new Image(); // Create new img element
lenna.onload=Run2;
lenna.src = 'Lenna.png'; // Set source path*/
//gem = new Image();
//gem.src = 'diamond.png'
console.log('hi')
t1 = +new Date();
for(var c=0; c<1000; ++c){
test_set = restart_test();
merge_function(test_set);
//oldMerge(test_set);
//staticoMerge(test_set);
}
t1 = +new Date() - t1;
console.log("Sets " + sets.length)
console.log("Time is " + t1)
console.log("Prep count is " + prepCount)
}
var path1 = [
{x:10, y:10},
{x:10, y:110},
{x:110, y:110},
{x:110, y:10}
]
var cut=50;
var path2 = [
{x:10, y:10},
{x:10, y:110},
{x:110-cut, y:110},
{x:110-cut, y:110-cut},
{x:110, y:110-cut},
{x:110, y:10}
]
var path3 = [
{x:10, y:10},
{x:10, y:110-cut},
{x:110-cut, y:110-cut},
{x:110-cut, y:10}
]
base_test = [
{_x: 10, _y:10, _w: 10, _h:10},
{_x: 20, _y:10, _w:13, _h:10},
{_x: 20, _y:20, _w:10, _h:11},
{_x: 10, _y:20, _w:10, _h:10},
{_x: 105, _y:15, _w:10, _h:10},
{_x: 50, _y:50, _w:10, _h:20},
{_x: 70, _y:50, _w:10, _h:20},
{_x: 40, _y:55, _w:70, _h:10},
{_x: 10, _y:100, _w:10, _h:10},
{_x: 20, _y:110, _w:10, _h:10},];
//{_x: 50, _y:45, _w:100, _h:100} ];
function testDraw(path, ctx, cc){
ctx.save();
ctx.beginPath();
//ctx.clearRect(10, 10, 100, 100);
var p = path[path.length-1];
ctx.moveTo(p.x, p.y)
for (var j=0; j<path.length; j++){
p = path[j];
ctx.lineTo(p.x, p.y)
}
//ctx.closePath()
ctx.clip();
ctx.globalAlpha=.05;
for (var k = 0 ; k<500; k++)
ctx.drawImage(lenna, -250+k%10, -250)
//ctx.stroke();
ctx.restore();
}
function drawRects(set){
var canvas = document.getElementById('canvas1');
var ctx = canvas.getContext('2d');
ctx.strokeStyle = "black";
for (var i=0, l=set.length; i<l; ++i){
console.log('draw')
r = set[i];
ctx.strokeRect(r._x, r._y, r._w, r._h)
}
//ctx.strokeRect(50,50,50,50);
}
function drawStatico(set){
var canvas = document.getElementById('canvas1');
var ctx = canvas.getContext('2d');
ctx.strokeStyle = "red";
ctx.lineWidth = 2;
for (var i=0, l=set.length; i<l; ++i){
console.log('draw')
r = set[i];
ctx.strokeRect(r._x, r._y, r._w, r._h)
}
//ctx.strokeRect(50,50,50,50);
}
function drawBounds(){
console.log("stroking")
var canvas = document.getElementById('canvas1');
var ctx = canvas.getContext('2d');
ctx.strokeStyle = "red";
ctx.lineWidth = "2"
ctx.globalAlpha = 1
for (var i=0, l=sets.length; i<l; ++i){
ctx.save();
console.log('draw')
var o = sets[i].paths[0];
console.log(o.toSource());
ctx.beginPath();
var p = o[o.length-1];
ctx.moveTo(p.x, p.y)
for (var j=0; j<o.length; j++){
p = o[j];
delete p.path;
console.log("move " + p.toSource())
ctx.lineTo(p.x, p.y)
}
ctx.stroke();
ctx.closePath()
ctx.strokeStyle = "orange";
ctx.lineWidth = "4";
o = sets[i];
console.log("right sicde " + o.x1)
ctx.strokeRect(o.x1, o.y1, o.x2-o.x1, o.y2-o.y1);
ctx.strokeStyle = "red";
ctx.lineWidth = "2"
//ctx.clip();
//ctx.drawImage(lenna, -250, -250)
ctx.restore();
}
}