/
trap_grid.cc
1773 lines (1584 loc) · 58.7 KB
/
trap_grid.cc
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
#include <deque>
#include <list>
#include <cmath>
#include <cstdlib>
#include "graphics.h"
#include "trap_grid.h"
/***************************************************************************
* TrapGrid functions
****************************************************************************/
//blank TG
TrapGrid::TrapGrid() {
}
//set the lower left and upper right, decide how many pixels to have,
//clear all the pixels, set their centers, clear all the connected components
void TrapGrid::reset_grid(cpx ll, cpx ur, int np) {
lower_left = ll;
upper_right = ur;
box_width = ur.real() - ll.real();
num_pixels = np;
//build the blank grid
grid = std::vector<std::vector<GridPixel> >(num_pixels, std::vector<GridPixel>(num_pixels));
//reset the connected components
z_components.resize(0); z_cut_by_w_components.resize(0);
w_components.resize(0); w_cut_by_z_components.resize(0);
intersection_components.resize(0);
pixel_diameter = box_width/double(num_pixels);
if (pixel_diameter < 1e-10) std::cout << "Grid precision [" << ll << "," << ur << "] is too low!\n";
//std::cout << "Box width: " << box_width << " and pixel diameter: " << pixel_diameter << "\n";
for (int i=0; i<num_pixels; ++i) {
double rp = lower_left.real() + (double(i)+0.5)*pixel_diameter;
for (int j=0; j<num_pixels; ++j) {
grid[i][j].center = cpx(rp, lower_left.imag() + (double(j)+0.5)*pixel_diameter);
grid[i][j].z_ball_status = 0;
grid[i][j].closest_z_ball = -1;
grid[i][j].z_ball_distance = -1;
grid[i][j].w_ball_status = 0;
grid[i][j].closest_w_ball = -1;
grid[i][j].w_ball_distance = -1;
grid[i][j].good_pixel = -1;
}
}
}
//fill the pixels for a given ball
//this just naively checks everything in a square
void TrapGrid::fill_pixels(const std::vector<Ball>& balls) {
int nb = (int)balls.size();
for (int bi=0; bi<nb; ++bi) {
double r = balls[bi].radius;
const cpx c = balls[bi].center;
//std::cout << "Filling pixels for ball at " << c << ", " << r << "\n";
int ll_touching_i, ll_touching_j;
int ur_touching_i, ur_touching_j;
double pixel_radius = 0.5*pixel_diameter;
pixel_indices(ll_touching_i, ll_touching_j, cpx(c.real()-r, c.imag()-r));
pixel_indices(ur_touching_i, ur_touching_j, cpx(c.real()+r, c.imag()+r));
for (int i=ll_touching_i; i<=ur_touching_i; ++i) {
if (i >= num_pixels) break;
else if (i < 0) continue;
for (int j=ll_touching_j; j<=ur_touching_j; ++j) {
if (j >= num_pixels) break;
else if (j < 0) continue;
//determine the quadrant that grid[i][j] is in relative to the center
//first, the four most likely things:
cpx p_c = grid[i][j].center;
cpx p_ur(p_c.real() + pixel_radius, p_c.imag() + pixel_radius);
cpx p_lr(p_c.real() + pixel_radius, p_c.imag() - pixel_radius);
cpx p_ul(p_c.real() - pixel_radius, p_c.imag() + pixel_radius);
cpx p_ll(p_c.real() - pixel_radius, p_c.imag() - pixel_radius);
bool touching = false;
bool containing = false;
if (p_ur.real() < c.real() && p_ur.imag() < c.imag()) {
//pixel is to the lower left of the center
//it suffices to check the upper right for touching and lower left for containing
if (abs(c-p_ll) < r) containing = true;
else if (abs(c-p_ur) < r) touching = true;
} else if (p_lr.real() < c.real() && p_lr.imag() > c.imag()) {
//pixel is to the upper left
//it suffices to check lr for touching and upper left for containing
if (abs(c-p_ul) < r) containing = true;
else if (abs(c-p_lr) < r) touching = true;
} else if (p_ll.real() > c.real() && p_ll.imag() > c.imag()) {
//pixel is to the upper right
//suffices to check ll for touching and ur for containing
if (abs(c-p_ur) < r) containing = true;
else if (abs(c-p_ll) < r) touching = true;
} else if (p_ul.real() > c.real() && p_ul.imag() < c.imag()) {
//pixel is to the lower right
//it suffices to check ul for touching and lr for containing
if (abs(c-p_lr) < r) containing = true;
else if (abs(c-p_ul) < r) touching = true;
} else {
//those were the four easier cases
if (p_ur.real() < c.real()) {
//the pixel is to the left
//it suffices to check that p_lr.real() > c.real() - r for touching
//and that it contains p_ll and p_ul for containing
if (abs(c-p_ll) < r && abs(c-p_ul)<r) containing = true;
else if (p_lr.real() > c.real() - r) touching = true;
} else if (p_ll.imag() > c.imag()) {
//the pixel is above
//it suffices to check that p_ll.imag() < c.imag() + r for touching
//and that it contains the top ones for containing
if (abs(c-p_ur)<r && abs(c-p_ul)<r) containing = true;
else if (p_ll.imag() < c.imag() + r) touching = true;
} else if (p_ll.real() > c.real()) {
//the pixel is to the right
//it suffices to check that p_ll.real() < c.real() + r for touching
//and that it contains the right ones for containing
if (abs(c-p_ur)<r && abs(c-p_lr)<r) containing = true;
else if (p_ll.real() < c.real() + r) touching = true;
} else if (p_ur.imag() < c.imag()) {
//the pixel is below
//it suffices to check that p_ur.imag() > c.imag() - r for touching and
//that it contains the bottom ones for containing
if (abs(c-p_ll)<r && abs(c-p_lr)<r) containing = true;
else if (p_ur.imag() > c.imag() - r) touching = true;
} else {
//the pixel is directly over the center of the circle
//it *is* touching, and its containing if all the corners are contained
if (abs(c-p_ll)<r && abs(c-p_lr)<r && abs(c-p_ur)<r && abs(c-p_ul)<r) containing = true;
else touching = true;
}
}
//std::cout << "Found pixel " << i << "," << j << " with center " << p_c << " is: ";
//if (containing) std::cout << " containing\n";
//else if (touching) std::cout << "touching\n";
//else std::cout << "neither\n";
if (containing) touching = true;
if (balls[bi].last_gen_index() == 0) { //it's in the z set
if (containing) grid[i][j].z_ball_status = 2; //might as well overwrite it
if (touching) {
if (grid[i][j].z_ball_status == 0) grid[i][j].z_ball_status = 1;
double d = abs(p_c-c);
if (grid[i][j].closest_z_ball<0 || d < grid[i][j].z_ball_distance) {
grid[i][j].closest_z_ball = bi;
grid[i][j].z_ball_distance = d;
}
}
} else { //it's in the w set
if (containing) grid[i][j].w_ball_status = 2; //might as well overwrite it
if (touching) {
if (grid[i][j].w_ball_status == 0) grid[i][j].w_ball_status = 1;
double d = abs(p_c-c);
if (grid[i][j].closest_w_ball<0 || d < grid[i][j].w_ball_distance) {
grid[i][j].closest_w_ball = bi;
grid[i][j].w_ball_distance = d;
}
}
}
}
}//<-- end of loop over pixels
}//<-- end of loop over balls
}
//populate the z_distance and w_distance on the pixels
//here it is distance to a pixel which is just touched by w or z
//this sets the appropriate pixels to have distance 0 and then
//does a breadth-first search (it's important that it's breadth-first)
void TrapGrid::compute_distances() {
std::deque<Point2d<int> > z_stack(0);
std::deque<Point2d<int> > w_stack(0);
//clear the distances
for (int i=0; i<num_pixels; ++i) {
for (int j=0; j<num_pixels; ++j) {
grid[i][j].z_distance = grid[i][j].w_distance = -1;
}
}
//initialize the ones of length 0
for (int i=0; i<num_pixels; ++i) {
for (int j=0; j<num_pixels; ++j) {
if (grid[i][j].z_ball_status > 0) {
grid[i][j].z_distance = 0;
z_stack.push_front(Point2d<int>(i,j));
}
if (grid[i][j].w_ball_status > 0) {
grid[i][j].w_distance = 0;
w_stack.push_front(Point2d<int>(i,j));
}
}
}
//now do the stacks; if we encounter anything new, set its length
//to be 1+ours and push it on the stack
while (z_stack.size() > 0) {
Point2d<int> p = z_stack.back();
z_stack.pop_back();
if (p.y > 0 && grid[p.x][p.y-1].z_distance == -1) {
grid[p.x][p.y-1].z_distance = grid[p.x][p.y].z_distance + 1;
z_stack.push_front(Point2d<int>(p.x, p.y-1));
}
if (p.x < num_pixels-1 && grid[p.x+1][p.y].z_distance == -1) {
grid[p.x+1][p.y].z_distance = grid[p.x][p.y].z_distance + 1;
z_stack.push_front(Point2d<int>(p.x+1, p.y));
}
if (p.y < num_pixels-1 && grid[p.x][p.y+1].z_distance == -1) {
grid[p.x][p.y+1].z_distance = grid[p.x][p.y].z_distance + 1;
z_stack.push_front(Point2d<int>(p.x, p.y+1));
}
if (p.x > 0 && grid[p.x-1][p.y].z_distance == -1) {
grid[p.x-1][p.y].z_distance = grid[p.x][p.y].z_distance + 1;
z_stack.push_front(Point2d<int>(p.x-1, p.y));
}
}
while (w_stack.size() > 0) {
Point2d<int> p = w_stack.back();
w_stack.pop_back();
if (p.y > 0 && grid[p.x][p.y-1].w_distance == -1) {
grid[p.x][p.y-1].w_distance = grid[p.x][p.y].w_distance + 1;
w_stack.push_front(Point2d<int>(p.x, p.y-1));
}
if (p.x < num_pixels-1 && grid[p.x+1][p.y].w_distance == -1) {
grid[p.x+1][p.y].w_distance = grid[p.x][p.y].w_distance + 1;
w_stack.push_front(Point2d<int>(p.x+1, p.y));
}
if (p.y < num_pixels-1 && grid[p.x][p.y+1].w_distance == -1) {
grid[p.x][p.y+1].w_distance = grid[p.x][p.y].w_distance + 1;
w_stack.push_front(Point2d<int>(p.x, p.y+1));
}
if (p.x > 0 && grid[p.x-1][p.y].w_distance == -1) {
grid[p.x-1][p.y].w_distance = grid[p.x][p.y].w_distance + 1;
w_stack.push_front(Point2d<int>(p.x-1, p.y));
}
}
}
void TrapGrid::compute_boundary(std::vector<Point3d<int> >& b) {
b.resize(0);
Point2d<int> start_pixel;
int start_direction = 0;
Point2d<int> current_pixel;
int current_direction = 0; //we come from the bottom; 0,1,2,3 goes ccw around starting at the bottom
int offset_i[4] = {0,1,0,-1}; // this is the offset to i in a given direction
int offset_j[4] = {-1,0,1,0}; // this is the offset to j
//find some place to start
for (int i=0; i<num_pixels; ++i) {
for (int j=0; j<num_pixels; ++j) {
if (grid[i][j].z_ball_status > 0 || grid[i][j].w_ball_status > 0) {
start_pixel = current_pixel = Point2d<int>(i,j);
goto DOUBLEBREAK;
}
}
}
return; //if we didn't find any pixels, there is no boundary
DOUBLEBREAK: //sorry
//push the first one on to the boundary
int i = start_pixel.x;
int j = start_pixel.y;
if (grid[i][j].z_ball_status > 0 && grid[i][j].w_ball_status > 0) {
b.push_back(Point3d<int>(i,j,0));
} else if (grid[i][j].z_ball_status > 0) {
b.push_back(Point3d<int>(i,j, grid[i][j].w_distance));
} else {
b.push_back(Point3d<int>(i,j, -grid[i][j].z_distance));
}
do {
i = current_pixel.x;
j = current_pixel.y;
int ii = i + offset_i[current_direction];
int jj = j + offset_j[current_direction];
//find the next pixel
if (ii < 0 || ii >= num_pixels || jj < 0 || jj >= num_pixels ||
(grid[ii][jj].z_ball_status == 0 && grid[ii][jj].w_ball_status == 0)) {
current_direction = (current_direction+1)%4;
continue;
}
//push the next pixel on to the boundary
if (grid[ii][jj].z_ball_status > 0 && grid[ii][jj].w_ball_status > 0) {
b.push_back(Point3d<int>(ii,jj,0));
} else if (grid[ii][jj].z_ball_status > 0) {
b.push_back(Point3d<int>(ii,jj, grid[ii][jj].w_distance));
} else {
b.push_back(Point3d<int>(ii,jj, -grid[ii][jj].z_distance));
}
//and move to it
current_pixel.x = ii; current_pixel.y = jj;
current_direction = (current_direction + 3)%4; //-1 mod 4
} while (current_pixel != start_pixel || current_direction != start_direction);
}
int sgn(int x) {
return (x > 0 ? 1 : (x < 0 ? -1 : 0));
}
bool TrapGrid::prune_boundary(const ifs& IFS,
const std::vector<Ball>& balls,
std::vector<Point3d<int> >& boundary,
std::vector<Ball>& trap_balls) {
//find a place where a different boundary starts
if (boundary.size() == 0) return false;
int first_gen_s = (boundary[0].z > 0 ? 1 : (boundary[0].z < 0 ? -1 : 0));
int i=0;
for (i=1; i<(int)boundary.size(); ++i) {
int s = (boundary[i].z > 0 ? 1 : (boundary[i].z < 0 ? -1 : 0));
if (s != first_gen_s) break;
}
if (i==(int)boundary.size()) return false;
//so boundary[i:] + boundary[:i] begins on a junction
std::vector<Point3d<int> > bd(boundary.begin() + i, boundary.end());
bd.insert(bd.end(), boundary.begin(), boundary.begin()+i);
//this list records the balls and stuff
typedef std::pair<Point3d<int>, Ball> PB;
std::list<PB> pruned_boundary(0);
std::list<PB>::iterator it;
std::list<PB>::iterator it2;
//for each run, try to find a good disk
std::vector<bool> good_pixel(bd.size(), true);
i=0;
while (i < (int)bd.size()) {
if (bd[i].z == 0) { //get rid of intersections
good_pixel[i] = false;
++i;
continue;
}
int run_begin = i;
int run_s = sgn(bd[i].z);
int run_end = run_begin+1;
while (run_end < (int)bd.size() && sgn(bd[run_end].z) == run_s)
++run_end;
while (true) {
//find the one farthest
int max_dist_p = -1;
int max_dist = -1;
for (int j=run_begin; j<run_end; ++j) {
if (!good_pixel[j]) continue;
if (abs(bd[j].z) > max_dist) {
max_dist_p = j;
max_dist = abs(bd[j].z);
}
}
if (max_dist_p == -1) break;
//try to put a ball inside it
Ball temp_ball;
int ii = bd[max_dist_p].x; int jj = bd[max_dist_p].y;
Ball to_put_inside = (bd[max_dist_p].z > 0 ? balls[grid[ii][jj].closest_z_ball]
: balls[grid[ii][jj].closest_w_ball]);
if (put_ball_inside_good_pixel(IFS,
to_put_inside,
(bd[max_dist_p].z > 0 ? 0 : 1),
temp_ball)) {
pruned_boundary.push_back( std::make_pair( bd[max_dist_p], temp_ball ) );
break;
}
good_pixel[max_dist_p] = false; //didn't find a ball
}
i = run_end;
}
//std::cout << "Found good boundary: \n";
//it = pruned_boundary.begin();
//while (it != pruned_boundary.end()) {
// std::cout << it->first << " " << it->second << "\n";
// ++it;
//}
//make sure the boundary list has more than one component in it
bool saw_1 = false;
bool saw_0 = false;
it = pruned_boundary.begin();
while (it != pruned_boundary.end()) {
if (sgn(it->first.z) == 1) {
saw_0 = true;
} else {
saw_1 = true;
}
if (saw_0 && saw_1) break;
++it;
}
if (!saw_0 || !saw_1) {
return false;
}
//now we have the boundary list, but we want to iteratively
//get rid of things that are redundant or small
while (true) {
//first rotate so it the boundary is on a division
while ( sgn(pruned_boundary.front().first.z) ==
sgn(pruned_boundary.back().first.z) ) {
pruned_boundary.insert(pruned_boundary.end(), pruned_boundary.front());
(void)pruned_boundary.erase(pruned_boundary.begin());
}
//std::cout << "Rotated: \n";
//it = pruned_boundary.begin();
//while (it != pruned_boundary.end()) {
// std::cout << it->first << " " << it->second << "\n";
// ++it;
//}
//now get rid of anything which is next to something similar
//(prefer the one that is farther away)
it = pruned_boundary.begin();
while (it != pruned_boundary.end()) {
it2 = it; ++it2;
if (it2 == pruned_boundary.end()) break;
if ((it->first.z > 0 ? 1 : -1) != (it2->first.z > 0 ? 1 : -1)) {
++it;
continue;
}
int dis = abs(it->first.z);
int dis2 = abs(it2->first.z);
if (dis >= dis2) {
(void)pruned_boundary.erase(it2);
} else {
it = pruned_boundary.erase(it);
if (it != pruned_boundary.begin()) --it;
}
}
//std::cout << "Got rid of similar stuff:\n";
//it = pruned_boundary.begin();
//while (it != pruned_boundary.end()) {
// std::cout << it->first << " " << it->second << "\n";
// ++it;
//}
//if there's <= 4 things, we're almost done
if ((int)pruned_boundary.size() <= 4) break;
//otherwise, find the smallest (closest to other balls), and take it away
it = pruned_boundary.begin();
int min_dist = -1;
while (it != pruned_boundary.end()) {
if (min_dist<0 || abs(it->first.z) < min_dist) {
min_dist = abs(it->first.z);
it2 = it;
}
++it;
}
//std::cout << "Removing smallest entry " << it2->first << "\n";
pruned_boundary.erase(it2);
}
if ((int)pruned_boundary.size() < 4) return false;
//make these the trap balls and return true
it = pruned_boundary.begin();
for (i=0; i<4; ++i) {
trap_balls[i] = it->second;
++it;
}
//copy back to the boundary
boundary.resize(0);
it = pruned_boundary.begin();
for (i=0; i<4; ++i) {
boundary.push_back(it->first);
++it;
}
return true;
}
//put a ball inside a pixel which is in the component z_or_w
//it figures out the ball radius and must be contained in a pixel which is
//at least ball radius many away from the other component
bool TrapGrid::put_ball_inside_good_pixel(const ifs& IFS,
const Ball& initial_ball,
int z_or_w,
Ball& b) {
std::vector<Ball> bs(1,initial_ball);
Ball temp_ball;
Point2d<int> p;
while ((int)bs.size() > 0) {
temp_ball = bs.back();
bs.pop_back();
//where is the ball centered?
pixel_indices(p.x,p.y,temp_ball.center);
//is this a good pixel?
if ( z_or_w != grid[p.x][p.y].good_pixel ) {
continue;
}
if (ball_contained_in_pixel(temp_ball, p)) {
b = temp_ball;
return true;
}
//if it's not contained in it, but it is good, subdivide
bs.push_back(IFS.act_on_right(0, temp_ball));
bs.push_back(IFS.act_on_right(1, temp_ball));
}
return false;
}
bool TrapGrid::ball_touches_pixel(const Ball& b, const Point2d<int>& p) {
cpx bc = b.center;
double bx = bc.real();
double by = bc.imag();
double br = b.radius;
cpx pc = pixel_center(p.x,p.y);
double px = pc.real();
double py = pc.imag();
double pr = pixel_diameter/2.0;
if (bx > px+pr && by > py+pr) { //upper right
return abs(bc - cpx(px+pr,py+pr)) < br;
} else if (bx < px-pr && by > py+pr) { //upper left
return abs(bc - cpx(px-pr,py+pr)) < br;
} else if (bx < px-pr && by < py-pr) { //lower left
return abs(bc - cpx(px-pr, py-pr)) < br;
} else if (bx > px+pr && by < py-pr) { //lower right
return abs(bc - cpx(px+pr, py-pr)) < br;
} else if (bx > px+pr) { //middle right
return bx-br < px+pr;
} else if (by > py+pr) { //middle top
return by-br < py+pr;
} else if (bx < px-pr) { //middle left
return bx+br > px-pr;
} else if (by < py-pr) { //middle bottom
return by+br > py-pr;
}
return true; //the ball's center is in the pixel
}
bool TrapGrid::ball_contained_in_pixel(const Ball& b, const Point2d<int>& p) {
cpx pc = pixel_center(p.x, p.y);
double pr = pixel_diameter/2.0;
cpx bc = b.center;
double br = b.radius;
return (bc.real() + br < pc.real() + pr) &&
(bc.real() - br > pc.real() - pr) &&
(bc.imag() + br < pc.imag() + pr) &&
(bc.imag() - br > pc.imag() - pr);
}
//anything on the boundary is good, and anything completely contained in only
//one component is good
void TrapGrid::compute_good_pixels(const std::vector<Point3d<int> >& boundary) {
std::vector<Point2d<int> > stack(0);
//add in the boundary
for (int i=0; i<(int)boundary.size(); ++i) {
if (boundary[i].z != 0) {
grid[boundary[i].x][boundary[i].y].good_pixel = (boundary[i].z > 0 ? 0 : 1);
stack.push_back(Point2d<int>(boundary[i].x, boundary[i].y));
}
}
//now propogate
int i_offset[4] = {0,1,0,-1};
int j_offset[4] = {-1,0,1,0};
while ((int)stack.size() > 0) {
Point2d<int> current = stack.back();
stack.pop_back();
for (int i=0; i<4; ++i) {
int ii = current.x + i_offset[i];
if (ii<0 || ii>=num_pixels) continue;
int jj = current.y + j_offset[i];
if (jj<0 || jj>=num_pixels) continue;
if (grid[ii][jj].good_pixel != -1) continue;
int current_goodness = grid[current.x][current.y].good_pixel;
if ( current_goodness==0 &&
grid[ii][jj].z_ball_status == 2 &&
grid[ii][jj].w_ball_status == 0 ) {
grid[ii][jj].good_pixel = 0;
stack.push_back(Point2d<int>(ii,jj));
} else if (current_goodness==1 &&
grid[ii][jj].z_ball_status==0 &&
grid[ii][jj].w_ball_status==2) {
grid[ii][jj].good_pixel = 1;
stack.push_back(Point2d<int>(ii,jj));
}
}
}
}
//decide if the ball b is disjoint from z or w pixels
//unfortunately, this seems like it needs to be just as complicated
//as the function above
bool TrapGrid::disjoint_from_z_or_w(const Ball& b, int z_or_w) {
double r = b.radius;
const cpx c = b.center;
//std::cout << "Filling pixels for ball at " << c << ", " << r << "\n";
int ll_touching_i, ll_touching_j;
int ur_touching_i, ur_touching_j;
double pixel_radius = 0.5*pixel_diameter;
pixel_indices(ll_touching_i, ll_touching_j, cpx(c.real()-r, c.imag()-r));
pixel_indices(ur_touching_i, ur_touching_j, cpx(c.real()+r, c.imag()+r));
for (int i=ll_touching_i; i<=ur_touching_i; ++i) {
if (i >= num_pixels) break;
else if (i < 0) continue;
for (int j=ll_touching_j; j<=ur_touching_j; ++j) {
if (j >= num_pixels) break;
else if (j < 0) continue;
cpx p_c = grid[i][j].center;
cpx p_ur(p_c.real() + pixel_radius, p_c.imag() + pixel_radius);
cpx p_lr(p_c.real() + pixel_radius, p_c.imag() - pixel_radius);
cpx p_ul(p_c.real() - pixel_radius, p_c.imag() + pixel_radius);
cpx p_ll(p_c.real() - pixel_radius, p_c.imag() - pixel_radius);
if (p_ur.real() < c.real() && p_ur.imag() < c.imag()) {
if (abs(c-p_ur) < r) {
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
} else if (p_lr.real() < c.real() && p_lr.imag() > c.imag()) {
//pixel is to the upper left
//it suffices to check lr for touching and upper left for containing
if (abs(c-p_lr) < r) {
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
} else if (p_ll.real() > c.real() && p_ll.imag() > c.imag()) {
//pixel is to the upper right
//suffices to check ll for touching and ur for containing
if (abs(c-p_ll) < r) {
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
} else if (p_ul.real() > c.real() && p_ul.imag() < c.imag()) {
//pixel is to the lower right
//it suffices to check ul for touching and lr for containing
if (abs(c-p_ul) < r) {
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
} else {
//those were the four easier cases
if (p_ur.real() < c.real()) {
//the pixel is to the left
//it suffices to check that p_lr.real() > c.real() - r for touching
//and that it contains p_ll and p_ul for containing
if (p_lr.real() > c.real() - r) {
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
} else if (p_ll.imag() > c.imag()) {
//the pixel is above
//it suffices to check that p_ll.imag() < c.imag() + r for touching
//and that it contains the top ones for containing
if (p_ll.imag() < c.imag() + r) {
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
} else if (p_ll.real() > c.real()) {
//the pixel is to the right
//it suffices to check that p_ll.real() < c.real() + r for touching
//and that it contains the right ones for containing
if (p_ll.real() < c.real() + r) {
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
} else if (p_ur.imag() < c.imag()) {
//the pixel is below
//it suffices to check that p_ur.imag() > c.imag() - r for touching and
//that it contains the bottom ones for containing
if (p_ur.imag() > c.imag() - r) {
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
} else {
//the pixel is directly over the center of the circle
//it *is* touching, and its containing if all the corners are contained
if ((z_or_w == 0 && grid[i][j].z_ball_status > 0) ||
(z_or_w == 1 && grid[i][j].w_ball_status > 0)) {
return false;
}
}
}
}
}//<-- end of loop over pixels
return true;
}
//fill out the connected components
//during this computation, the grid[i][j].*comp is -1 if nothing
//has touched it, -2 if it's been stacked, and >=0 if it records
//which component
void TrapGrid::compute_connected_components() {
//reset the connected components
z_components.resize(0); z_cut_by_w_components.resize(0);
w_components.resize(0); w_cut_by_z_components.resize(0);
intersection_components.resize(0);
for (int i=0; i<num_pixels; ++i) {
for (int j=0; j<num_pixels; ++j) {
grid[i][j].z_comp = grid[i][j].w_comp =
grid[i][j].z_cut_by_w_comp = grid[i][j].w_cut_by_z_comp =
grid[i][j].i_comp = -1;
}
}
//go through and pursue all the components we encounter
for (int i=0; i<num_pixels; ++i) {
for (int j=0; j<num_pixels; ++j) {
//does it form a new z component?
if (grid[i][j].z_ball_status==2 && grid[i][j].z_comp == -1) {
z_components.push_back(std::vector<Point2d<int> >(0));
pursue_z_comp(i,j, z_components.size()-1);
}
if (grid[i][j].z_ball_status==2 &&
grid[i][j].z_cut_by_w_comp == -1 &&
grid[i][j].w_ball_status==0) {
z_cut_by_w_components.push_back(std::vector<Point2d<int> >(0));
pursue_z_cut_by_w_comp(i,j,z_cut_by_w_components.size()-1);
}
//how about a w component?
if (grid[i][j].w_ball_status==2 && grid[i][j].w_comp == -1) {
w_components.push_back(std::vector<Point2d<int> >(0));
pursue_w_comp(i,j, w_components.size()-1);
}
if (grid[i][j].w_ball_status==2 &&
grid[i][j].w_cut_by_z_comp == -1 &&
grid[i][j].z_ball_status==0) {
w_cut_by_z_components.push_back(std::vector<Point2d<int> >(0));
pursue_w_cut_by_z_comp(i,j,w_cut_by_z_components.size()-1);
}
//or intersection component
if (grid[i][j].z_ball_status >0 &&
grid[i][j].w_ball_status >0 &&
grid[i][j].i_comp == -1) {
intersection_components.push_back(std::vector<Point2d<int> >(0));
pursue_intersection_comp(i,j,intersection_components.size()-1);
}
}
}
}
//These are all the component-finding functions.
//There are slight differences, so there are multiple functions
//I guess these will use breadth-first search?
void TrapGrid::pursue_z_comp(int i, int j, int ind) {
std::deque<Point2d<int> > stack(1, Point2d<int>(i,j));
grid[i][j].z_comp = -2;
while (stack.size() > 0) {
Point2d<int> p = stack.back();
stack.pop_back();
//mark p as done
grid[p.x][p.y].z_comp = ind;
z_components[ind].push_back(p);
//go around to the others
for (int ii=-1; ii<=1; ++ii) {
int iii = ii + p.x;
if (iii < 0) continue;
else if (iii >= num_pixels) break;
for (int jj=-1; jj<=1; ++jj) {
if (jj == 0 && ii == 0) continue;
int jjj = jj + p.y;
if (jjj < 0) continue;
else if (jjj >= num_pixels) break;
if (grid[iii][jjj].z_comp == -1 && grid[iii][jjj].z_ball_status == 2) {
grid[iii][jjj].z_comp = -2;
stack.push_front(Point2d<int>(iii,jjj));
}
}
}
}
}
void TrapGrid::pursue_w_comp(int i, int j, int ind) {
std::deque<Point2d<int> > stack(1, Point2d<int>(i,j));
grid[i][j].w_comp = -2;
while (stack.size() > 0) {
Point2d<int> p = stack.back();
stack.pop_back();
//mark p as done
grid[p.x][p.y].w_comp = ind;
w_components[ind].push_back(p);
//go around to the others
for (int ii=-1; ii<=1; ++ii) {
int iii = ii + p.x;
if (iii < 0) continue;
else if (iii >= num_pixels) break;
for (int jj=-1; jj<=1; ++jj) {
if (jj == 0 && ii == 0) continue;
int jjj = jj + p.y;
if (jjj < 0) continue;
else if (jjj >= num_pixels) break;
if (grid[iii][jjj].w_comp == -1 && grid[iii][jjj].w_ball_status == 2) {
grid[iii][jjj].w_comp = -2;
stack.push_front(Point2d<int>(iii,jjj));
}
}
}
}
}
//this finds components of z-contained pixels, where the components
//aren't allowed to go through w-contained pixels.
//because it is safer to assume that things *are* connected,
//we *allow* these components to go through pixels which are
//touched by w (including going diagonally when one side is contained
//in w and one side is touched by w)
void TrapGrid::pursue_z_cut_by_w_comp(int i, int j, int ind) {
std::deque<Point2d<int> > stack(1, Point2d<int>(i,j));
grid[i][j].z_cut_by_w_comp = -2;
int ii,jj;
while (stack.size() > 0) {
Point2d<int> p = stack.back();
stack.pop_back();
//mark p as done
grid[p.x][p.y].z_cut_by_w_comp = ind;
z_cut_by_w_components[ind].push_back(p);
//figure out which sides run against the edge of the pixels
bool right_max, top_max, left_min, bottom_min;
if (p.x == num_pixels - 1) {
right_max = true;
left_min = false;
} else if (p.x == 0) {
left_min = true;
right_max = false;
} else {
left_min = right_max = false;
}
if (p.y == num_pixels - 1) {
top_max = true;
bottom_min = false;
} else if (p.y == 0) {
top_max = false;
bottom_min = true;
} else {
top_max = bottom_min = false;
}
//right column
if (!right_max) {
ii = p.x+1;
//bottom
if (!bottom_min) {
jj=p.y-1;
if (grid[ii][jj].z_ball_status == 2
&& grid[ii][jj].z_cut_by_w_comp == -1
&& grid[ii][jj].w_ball_status < 2
&& (grid[ii-1][jj].w_ball_status < 2 || grid[ii][jj+1].w_ball_status < 2)) {
grid[ii][jj].z_cut_by_w_comp = -2;
stack.push_front(Point2d<int>(ii,jj));
}
}
//middle
jj = p.y;
if (grid[ii][jj].z_ball_status == 2
&& grid[ii][jj].w_ball_status < 2
&& grid[ii][jj].z_cut_by_w_comp == -1) {
grid[ii][jj].z_cut_by_w_comp = -2;
stack.push_front(Point2d<int>(ii,jj));
}
//right top
if (!top_max) {
jj = p.y+1;
if (grid[ii][jj].z_ball_status == 2
&& grid[ii][jj].z_cut_by_w_comp == -1
&& grid[ii][jj].w_ball_status < 2
&& (grid[ii-1][jj].w_ball_status < 2 || grid[ii][jj-1].w_ball_status < 2)) {
grid[ii][jj].z_cut_by_w_comp = -2;
stack.push_front(Point2d<int>(ii,jj));
}
}
}
//top spot
if (!top_max) {
ii = p.x;
jj = p.y + 1;
if (grid[ii][jj].z_ball_status == 2
&& grid[ii][jj].z_cut_by_w_comp == -1
&& grid[ii][jj].w_ball_status < 2) {
grid[ii][jj].z_cut_by_w_comp = -2;
stack.push_front(Point2d<int>(ii,jj));
}
}
//left column
if (!left_min) {
ii = p.x-1;
//top
if (!top_max) {
jj = p.y+1;
if (grid[ii][jj].z_ball_status == 2
&& grid[ii][jj].z_cut_by_w_comp == -1
&& grid[ii][jj].w_ball_status < 2
&& (grid[ii+1][jj].w_ball_status < 2 || grid[ii][jj-1].w_ball_status < 2)) {
grid[ii][jj].z_cut_by_w_comp = -2;
stack.push_front(Point2d<int>(ii,jj));
}
}
//middle
jj = p.y;
if (grid[ii][jj].z_ball_status == 2
&& grid[ii][jj].w_ball_status < 2
&& grid[ii][jj].z_cut_by_w_comp == -1) {
grid[ii][jj].z_cut_by_w_comp = -2;
stack.push_front(Point2d<int>(ii,jj));
}
//bottom
if (!bottom_min) {
jj=p.y-1;
if (grid[ii][jj].z_ball_status == 2
&& grid[ii][jj].z_cut_by_w_comp == -1
&& grid[ii][jj].w_ball_status < 2
&& (grid[ii+1][jj].w_ball_status < 2 || grid[ii][jj+1].w_ball_status < 2)) {
grid[ii][jj].z_cut_by_w_comp = -2;
stack.push_front(Point2d<int>(ii,jj));
}
}
}
//bottom middle
if (!bottom_min) {
ii = p.x;
jj = p.y - 1;
if (grid[ii][jj].z_ball_status == 2
&& grid[ii][jj].z_cut_by_w_comp == -1