-
Notifications
You must be signed in to change notification settings - Fork 0
/
hull1.cpp
2295 lines (1896 loc) · 69.6 KB
/
hull1.cpp
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 "hull1.h"
#define LEFT 1
#define RIGHT 0
std::string gen_name() {
std::srand(time(NULL)); // setting RNG seed to current time
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used (Mersenne-Twister in this case)
std::uniform_int_distribution<int> uni(0, 1000000000);
int t = uni(rng);
return std::to_string(t);
}
double distance(double x1, double y1, double x2, double y2){
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
};
std::vector<int> color_hull(int a) {
std::vector<int> rgb;
while(a > 8) {
a = a - 8;
}
switch(a) {
case 1: rgb = {255, 255, 0}; break;
case 2: rgb = {255, 0, 255}; break;
case 3: rgb = {0, 255, 0}; break;
case 4: rgb = {0, 0, 255}; break;
case 5: rgb = {255, 0, 255}; break;
case 6: rgb = {255, 0, 0}; break;
case 7: rgb = {255, 255, 255}; break;
case 8: rgb = {0, 0, 0}; break;
default: rgb = {0, 255, 255}; break;
}
return rgb;
}
Hull::Hull(CMGrid grid, std::vector<Key> c) : g(grid), grid_size(grid.grid_size) {
for (float i = 0; i < grid_size; i += 0.5) {
for (float j = 0; j < grid_size; j += 0.5) {
Key k(i, j);
fine_grid.push_back(k);
fine_sides[k] = (i == 0 || i == grid_size - 1) + (j == 0 || j == grid_size - 1);
std::vector<Key> nb;
if (i != 0) nb.push_back(Key(i-0.5, j));
if (j != 0) nb.push_back(Key(i, j-0.5));
if (j != grid_size-1) nb.push_back(Key(i, j+0.5));
if (i != grid_size-1) nb.push_back(Key(i+0.5, j));
fine_neighbours_list[k] = nb;
nb.clear();
if (i != 0 && i != 0.5) nb.push_back(Key(i-1, j));
if (i != grid_size-1 && i != grid_size-1.5) nb.push_back(Key(i+1, j));
if (j != 0 && j != 0.5) nb.push_back(Key(i, j-1));
if (j != grid_size-1 && j != grid_size-1.5) nb.push_back(Key(i, j+1));
outer_neighbours_list[k] = nb;
}
}
for (auto const& k: fine_grid) {
for (auto const& n: fine_neighbours_list[k]) {
g.bond_parameters[make_valid_bond(k, n)] = 0;
}
}
// we extend the cluster map to the finer grid
for (auto const& k: fine_grid) cluster_map[k] = false;
for (auto const& k: c) {
cluster_map[k] = true;
for (auto const& n: g.neighbours_list[k]) {
if (g.bond_parameters.at(make_valid_bond(k, n)) == 1 && cluster_map[n]) {
Key u((k.i+n.i)/2, (k.j+n.j)/2);
g.bond_parameters[make_valid_bond(k, u)] = 1;
g.bond_parameters[make_valid_bond(n, u)] = 1;
cluster_map[u] = true;
}
}
}
// we create a copy of bond_parameters where all pairs of points in the cluster are closed off
dense_cluster_map = cluster_map;
dense_cluster_bond_parameters = g.bond_parameters;
for (auto const& k: fine_grid) for (auto const& n: fine_neighbours_list[k]) {
dense_cluster_bond_parameters[make_valid_bond(k, n)] = false;
}
for (auto const& [k, in]: cluster_map) if (in) {
for (auto const& n: outer_neighbours_list[k]) if (cluster_map[n]) {
dense_cluster_bond_parameters[make_valid_bond(k, n)] = true;
dense_cluster_bond_parameters[make_valid_half_bond(k, n)] = true;
dense_cluster_bond_parameters[make_valid_half_bond(n, k)] = true;
Key u((k.i+n.i)/2, (k.j+n.j)/2);
dense_cluster_map[u] = true;
}
}
}
/*
Bond Hull::make_valid_bond(Key k1, Key k2) {
return k1.j < k2.j || k1.i < k2.i ? Bond(k1, k2) : Bond(k2, k1);
}
*/
Bond Hull::make_valid_bond(Key k1, Key k2) {
if (k1.j < k2.j) return Bond(k1, k2);
else if (k1.j == k2.j && k1.i < k2.i) return Bond(k1, k2);
return Bond(k2, k1);
}
Bond Hull::make_valid_half_bond(Key k1, Key k2) {
Key n((k1.i+k2.i)/2, (k1.j+k2.j)/2);
if (k1.i < k2.i || k1.j < k2.j) return Bond(k1, n);
return Bond(n, k1);
}
std::vector<Key> Hull::square(Key k, float r) {
if (r == 0) return fine_neighbours_list[k];
std::vector<Key> points;
for (float i = k.i - r; i <= k.i + r; i+=0.25) {
for (float j = k.j - r; j <= k.j + r; j+=0.25) {
if (i > 0 && i < grid_size && j > 0 && j < grid_size) points.push_back(Key(i, j));
}
}
return points;
}
float Hull::bond_length(Bond b) {
Key k1(b.a), k2(b.b);
return sqrt((k1.i-k2.i)*(k1.i-k2.i)+(k1.j-k2.j)*(k1.j-k2.j));
}
float Hull::eucl_dist(Key k1, Key k2) {
return sqrt((k1.i-k2.i)*(k1.i-k2.i)+(k1.j-k2.j)*(k1.j-k2.j));
}
float Hull::angle(Key k, Key n, bool counterclockwise) {
float comp_vec_x = k.i - n.i, comp_vec_y = k.j - n.j;;
float comp_angle;
if (counterclockwise) {
if (n.j < k.j) comp_angle = acos(comp_vec_x/(sqrt(comp_vec_x*comp_vec_x+comp_vec_y*comp_vec_y)));
else comp_angle = 2*M_PI - acos(comp_vec_x/(sqrt(comp_vec_x*comp_vec_x+comp_vec_y*comp_vec_y)));
}
else {
if (n.j > k.j) comp_angle = acos(comp_vec_x/(sqrt(comp_vec_x*comp_vec_x+comp_vec_y*comp_vec_y)));
else comp_angle = 2*M_PI - acos(comp_vec_x/(sqrt(comp_vec_x*comp_vec_x+comp_vec_y*comp_vec_y)));
}
return comp_angle;
}
bool Hull::parallel_aligned(Bond b1, Bond b2) { // only works for totally vertical or horizontal pairs
b1 = make_valid_bond(b1.a, b1.b), b2 = make_valid_bond(b2.a, b2.b);
if (b1.a.i == b1.b.i && b2.a.i == b2.b.i) { // means it's horizontal
if (b1.a.j == b2.a.j && b1.b.j == b2.b.j) return true;
}
else if (b1.a.j == b1.b.j && b2.a.j == b2.b.j) { // means it's vertical
if (b1.a.i == b2.a.i && b1.b.i == b2.b.i) return true;
}
return false;
}
bool Hull::perpendicular(Bond b1, Bond b2) {
float x1 = b1.a.i - b1.b.i, y1 = b1.a.j-b1.b.j;
float x2 = b2.a.i - b2.b.i, y2 = b2.a.j-b2.b.j;
if (x1*x2 + y1*y2 == 0) return true;
return false;
}
bool Hull::on_segment(Bond b, Key k) {
if (eucl_dist(b.a, k) <= bond_length(b) && eucl_dist(b.b, k) <= bond_length(b)) return 1;
return 0;
}
int Hull::crossing_link(Bond b, Key center, float r) {
// to take care of the case where the circle crosses a bond twice and the two endpoint keys are outside
// we subdivide the bond with 3 points in between, and we test whether some of those are inside the circle
Key k1 = b.a, k2 = b.b;
float bl = bond_length(b), d1 = eucl_dist(k1, center), d2 = eucl_dist(k2, center);
if (std::max(d1, d2) > r+bl) return 0; // preemptively exclude all bonds that CAN'T intersect the circle
if (std::max(d1, d2) < r) return 0; // bonds with keys both inside the circle can't intersect
int crossings = 0;
std::vector<Key> subdivisions;
for (float t = 0; t <= 1; t += 0.02) subdivisions.push_back(Key(t*k1.i+(1-t)*k2.i, t*k1.j+(1-t)*k2.j));
for (int i = 1; i < subdivisions.size(); i++) {
float dist1 = eucl_dist(subdivisions[i-1], center), dist2 = eucl_dist(subdivisions[i], center);
if ((dist1 >= r && dist2 <= r) || (dist1 <= r && dist2 >= r)) crossings++;
}
return crossings;
}
Key Hull::crossing_point(Bond b, Key center, float r) {
// divides b into 100 subsegments, and approximates the crossing point that way
// in the unlikely case that the circle cuts the segment in 2 spots, we only return one of those
Key k1 = b.a, k2 = b.b;
std::vector<Key> subdivisions;
for (float t = 0; t <= 1; t += 0.001) subdivisions.push_back(Key(t*k1.i+(1-t)*k2.i, t*k1.j+(1-t)*k2.j));
subdivisions.push_back(k1);
for (int i = 1; i < subdivisions.size(); i++) {
float dist1 = eucl_dist(subdivisions[i-1], center), dist2 = eucl_dist(subdivisions[i], center);
if ((dist1 >= r && dist2 <= r) || (dist1 <= r && dist2 >= r)) return subdivisions.at(i);
}
}
int Hull::intercrossing_link(Bond b, Key center, float r) {
// to take care of the case where the circle crosses a bond twice and the two endpoint keys are outside
// we subdivide the bond with 3 points in between, and we test whether some of those are inside the circle
Key k1 = b.a, k2 = b.b;
float bl = bond_length(b), d1 = eucl_dist(k1, center), d2 = eucl_dist(k2, center);
if (std::max(d1, d2) > r+bl+1) return 0; // preemptively exclude all bonds that CAN'T intersect the circle
float dx = k2.j - k1.j, dy = k1.i - k2.i, dr2 = pow(dx,2)+pow(dy,2);
float D = k2.j*k1.i-k1.j*k2.i, Delta = pow(r, 2)*dr2 - pow(D, 2);
if (std::abs(Delta) < 1.0e-5) { // if discriminant is (almost) zero, tangent -> one intersection point
float x = D*dy/dr2, y = -D*dx/dr2;
if (on_segment(b, Key(center.i - y, center.j + x))) return 1;
}
int crossings = 0;
std::vector<Key> subdivisions;
for (float t = 0; t <= 1; t += 0.02) subdivisions.push_back(Key(t*k1.i+(1-t)*k2.i, t*k1.j+(1-t)*k2.j));
for (int i = 1; i < subdivisions.size(); i++) {
float dist1 = eucl_dist(subdivisions[i-1], center), dist2 = eucl_dist(subdivisions[i], center);
if ((dist1 >= r && dist2 <= r) || (dist1 <= r && dist2 >= r)) crossings++;
}
return crossings;
}
Key Hull::intercrossing_point(Bond b, Key center, float r) {
// divides b into 100 subsegments, and approximates the crossing point that way
// in the unlikely case that the circle cuts the segment in 2 spots, we only return one of those
Key k1 = b.a, k2 = b.b;
float dx = k2.j - k1.j, dy = k1.i - k2.i, dr2 = pow(dx,2)+pow(dy,2);
float D = k2.j*k1.i-k1.j*k2.i, Delta = pow(r, 2)*dr2 - pow(D, 2);
if (std::abs(Delta) < 1.0e-5) { // if discriminant is (almost) zero, tangent -> one intersection point
float x = D*dy/dr2, y = -D*dx/dr2;
if (on_segment(b, Key(center.i - y, center.j + x))) return Key(center.i - y, center.j + x);
}
std::vector<Key> subdivisions;
for (float t = 0; t <= 1; t += 0.001) subdivisions.push_back(Key(t*k1.i+(1-t)*k2.i, t*k1.j+(1-t)*k2.j));
subdivisions.push_back(k1);
for (int i = 1; i < subdivisions.size(); i++) {
float dist1 = eucl_dist(subdivisions[i-1], center), dist2 = eucl_dist(subdivisions[i], center);
if ((dist1 >= r && dist2 <= r) || (dist1 <= r && dist2 >= r)) return subdivisions.at(i);
}
}
/*
std::vector<Key> Hull::crossing_test(Bond b, Key center, float r) {
// keep in mind: i corresponds to y, and j corresponds to -x
Key k1 = b.a, k2 = b.b;
float bl = bond_length(b), d1 = eucl_dist(k1, center), d2 = eucl_dist(k2, center);
if (std::max(d1, d2) > r+bl) return {}; // preemptively exclude all bonds that CAN'T intersect the circle
if (std::max(d1, d2) < r) return {}; // bonds both inside the circle can't intersect
float dx = k2.j - k1.j, dy = k1.i - k2.i, dr2 = pow(dx,2)+pow(dy,2);
float D = k2.j*k1.i-k1.j*k2.i, Delta = pow(r, 2)*dr2 - pow(D, 2);
if (Delta < 0) return {}; // if discriminant is negative, doesn't intersect
else if (Delta == 0) {// if discriminant is zero, tangent -> one intersection point
float x = D*dy/dr2, y = -D*dx/dr2;
if (on_segment(b, Key(-y,x))) return {Key(center.i+(-1)*y, center.j+x)};
else return {};
}
else if (Delta > 0 && std::min(d1, d2) < r) // if one bond key is inside the circle, it intersects it once
{
std::cout << "min<r " << b << std::endl;
float x1 = (D*dy+sign(dy)*dx*sqrt(Delta))/dr2, y1 = (-D*dx+std::abs(dy)*sqrt(Delta))/dr2;
float x2 = (D*dy-sign(dy)*dx*sqrt(Delta))/dr2, y2 = (-D*dx-std::abs(dy)*sqrt(Delta))/dr2;
Key p1(center.i+(-1)*y1, center.j + x1), p2(center.i+(-1)*y2, center.j+x2);
if (on_segment(b, p1)) return {p1};
else return {p2};
}
else if (Delta > 0 && std::min(d1, d2) >= r) // if both bond keys are outside (or on) the circle, it intersects it twice
{
float x1 = (D*dy+sign(dy)*dx*sqrt(Delta))/dr2, y1 = (-D*dx+std::abs(dy)*sqrt(Delta))/dr2;
float x2 = (D*dy-sign(dy)*dx*sqrt(Delta))/dr2, y2 = (-D*dx-std::abs(dy)*sqrt(Delta))/dr2;
Key p1(center.i+(-1)*y1, center.j + x1), p2(center.i+(-1)*y2, center.j+x2);
std::cout << "p1: " << p1 << ", p2: " << p2 << std::endl;
if (on_segment(b, p1) && on_segment(b, p2)) {
// return the one that's closer in terms of hull-distance
Key first = ordered_hull_keys[b.a] < ordered_hull_keys[b.b] ? b.a : b.b;
Key p = eucl_dist(p1, first) < eucl_dist(p2, first) ? p1 : p2;
return {p};
}
else return {};
}
}
int Hull::sign(float n) {
if (n < 0) return -1;
return 1;
}
*/
/*
Key Hull::crossing_point(Bond bond, Key center, float r) {
float x_a = bond.a.j, x_b = bond.b.j, x_c = center.j;
float y_a = bond.a.i, y_b = bond.b.i, y_c = center.i;
float a = -x_a*x_a + 2*x_a*x_b - x_b*x_b - y_a*y_a + 2*y_a*y_b - y_b*y_b;
float b = -2*x_a*x_b + 2*x_a*x_c + 2*x_b*x_b - 2*x_b*x_c - 2*y_a*y_b + 2*y_a*y_c + 2*y_b*y_b - 2*y_b*y_c;
float c = r*r - x_b*x_b + 2*x_b*x_c - x_c*x_c - y_b*y_b + 2*y_b*y_c - y_c*y_c;
float t1 = (-b + sqrt(b*b - 4*a*c))/(2*a);
float t2 = (-b - sqrt(b*b - 4*a*c))/(2*a);
if (t1 >= 0 && t1 <= 1) return Key(t1*x_a + (1-t1)*x_b, t1*y_a + (1-t1)*y_b);
else if (t2 >= 0 && t2 <= 1) return Key(t2*x_a + (1-t2)*x_b, t2*y_a + (1-t2)*y_b);
}
*/
bool Hull::common_element(std::vector<Key> v1, std::vector<Key> v2) {
for (auto const& k1: v1) for (auto const& k2: v2) {
if (k1 == k2) return true;
}
return false;
}
bool Hull::inbetween_key(Key k1, Key k2, std::map<Key, bool> list) {
if (k1.i == k2.i) { //segment is horizontal
for (auto const& [k, exists]: list) if (exists) {
if (k.i == k1.i) {
if ((k.j > k1.j && k.j < k2.j) || (k.j < k1.j && k.j > k2.j)) return true;
}
}
}
if (k1.j == k2.j) {
for (auto const& [k, exists]: list) if (exists) {
if (k.j == k1.j) {
if ((k.i > k1.i && k.i < k2.i) || (k.i < k1.i && k.i > k2.i)) return true;
}
}
}
return false;
}
bool Hull::permitted_move(Key start, Key end) {
if (cluster_map[end] && cluster_map[start]) return 0;
else if (cluster_map[end]) return 0;
else return 1;
}
bool Hull::dense_permitted_move(Key start, Key end) {
if (dense_cluster_map[end] && dense_cluster_map[start]) return 0;
else if (dense_cluster_map[end]) return 0;
else return 1;
}
std::vector<Bond> Hull::determine_hull() {
std::cout << "CALCULATING HULL..." << std::endl << std::flush;
if (!hull.empty()) hull.clear();
std::vector<Bond> h;
Key start(0, grid_size/2);
if (cluster_map[start]) start = Key(0, grid_size/2-1);
std::map<Key, bool> visited;
std::stack<Key> stack;
stack.push(start);
while (!stack.empty()) {
// Pop a vertex from stack
Key s = stack.top();
stack.pop();
if (visited[s] != true) visited[s] = true;
std::vector<Key> nb = fine_neighbours_list[s];
for (auto const& v: nb) {
if (visited[v] != true && permitted_move(s, v)) stack.push(v);
if (cluster_map[v]) {
Key u((v.i+s.i)/2, (v.j+s.j)/2);
h.push_back(make_valid_bond(u, v));
}
}
}
std::cout << "DONE" << std::endl << std::flush;
hull = h;
return h;
}
std::vector<Bond> Hull::determine_ap() {
std::cout << "CALCULATING AP..." << std::endl << std::flush;
Key start(0, floor(grid_size/2));
if (cluster_map[start]) start = Key(0, floor(grid_size/2)-1);
std::map<Key, bool> visited;
std::stack<Key> stack;
stack.push(start);
while (!stack.empty()) {
// Pop a vertex from stack
Key s = stack.top();
stack.pop();
if (visited[s] != true) visited[s] = true;
std::vector<Key> nb = fine_neighbours_list[s];
for (auto const& v: nb) {
if (visited[v] != true && dense_permitted_move(s, v)) stack.push(v);
if (dense_cluster_map[v]) {
Key u((v.i+s.i)/2, (v.j+s.j)/2);
ap.push_back(make_valid_bond(u, v));
}
}
}
std::cout << "DONE" << std::endl << std::flush;
return ap;
}
std::vector<Bond> Hull::old_link_ap() {
std::cout << "STARTING AP LINKAGE..." << std::endl << std::flush;
time_t tstart, tend;
tstart = time(0);
if (ap.empty()) determine_ap();
std::vector<Bond> ap_graph;
std::map<Bond, bool> ap_map;
std::map<Bond, bool> visited;
std::map<Key, std::vector<Key>> support; // takes an AP key and returns its corresponding supporting key(s)
// find the non-cluster points
for (auto const& b: ap) {
if (!dense_cluster_map[b.a] == true) {
ap_points[b.a] = true;
support[b.a].push_back(b.b);
}
else if (!dense_cluster_map[b.b] == true) {
ap_points[b.b] = true;
support[b.b].push_back(b.a);
}
ap_map[b] = true;
}
for (auto const& [k, v_s]: support) {
std::vector<Key> neighbourhood;
std::vector<Bond> candidates;
for (auto const& n: square(k, 0.75)) if (ap_points[n] && k != n) neighbourhood.push_back(n);
for (auto const& n: neighbourhood) {
std::vector<Key> v_s_n = support[n];
for (auto const& s: v_s) for (auto const& s_n: v_s_n) {
if (dense_cluster_bond_parameters[make_valid_bond(s_n, s)]) {
if (!visited[make_valid_bond(k, n)] && parallel_aligned(make_valid_bond(s_n, s), make_valid_bond(k, n))) {
if (inbetween_key(k, n, ap_points) == false) {
if (!common_element(v_s, v_s_n)) {
ap_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
}
}
}
else if (!visited[make_valid_bond(k, n)] && perpendicular(make_valid_bond(s_n, n), make_valid_bond(s, k))) {
if (s_n == s && k != n) {
ap_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
else {
std::vector<Key> clust_k, clust_n;
for (auto const& u: fine_neighbours_list[s]) if (dense_cluster_map[u]) clust_k.push_back(u);
for (auto const& u: fine_neighbours_list[s_n]) if (dense_cluster_map[u]) clust_n.push_back(u);
float dist = eucl_dist(k, n);
if (common_element(clust_k, clust_n) && dist < 0.36) { // only diagonal links we want are the 0.25*sqrt(2)-length ones
ap_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
}
}
}
}
}
Key first(0, 0), last(0, 0), current(0, 0);
int index = 1;
for (auto const& [k, exists]: ap_points) if (exists) {
if (k.i == 0) {
first = k, current = first;
ordered_ap_keys[k] = index;
index++;
}
else if (k.i == grid_size-1) last = k;
}
while (current != last) {
for (auto& [b, exists]: visited) if (exists) {
if (b.a == current) {
ordered_ap_keys[b.b] = index;
current = b.b;
exists = false;
index++;
break;
}
else if (b.b == current) {
ordered_ap_keys[b.a] = index;
current = b.a;
exists = false;
index++;
break;
}
}
}
std::cout << "DONE" << std::endl << std::flush;
tend = time(0);
std::cout << "It took " << difftime(tend, tstart) << " to compute and link the external hull." << std::endl;
linked_ap = ap_graph;
return ap_graph;
}
std::vector<Bond> Hull::link_ap() {
std::cout << "STARTING AP LINKAGE..." << std::endl << std::flush;
time_t tstart, tend;
tstart = time(0);
if (ap.empty()) determine_ap();
std::vector<Bond> ap_graph;
std::map<Bond, bool> ap_map;
std::map<Bond, bool> visited;
std::map<Key, std::vector<Key>> support; // takes an AP key and returns its corresponding supporting key(s)
// find the non-cluster points
for (auto const& b: ap) {
if (!dense_cluster_map[b.a] == true) {
ap_points[b.a] = true;
support[b.a].push_back(b.b);
}
else if (!dense_cluster_map[b.b] == true) {
ap_points[b.b] = true;
support[b.b].push_back(b.a);
}
ap_map[b] = true;
}
for (auto const& [k, v_s]: support) {
std::vector<Key> neighbourhood;
std::vector<Bond> candidates;
for (auto const& n: square(k, 0.75)) if (ap_points[n] && k != n) {
if (eucl_dist(k, n) == 0.5) neighbourhood.push_back(n);
else if (eucl_dist(k, n) < 0.36 && eucl_dist(k, n) > 0.35) neighbourhood.push_back(n);
}
for (auto const& n: neighbourhood) {
std::vector<Key> v_s_n = support[n];
for (auto const& s: v_s) for (auto const& s_n: v_s_n) {
if (eucl_dist(k, n) == 0.5) {
if (!visited[make_valid_bond(k, n)] && parallel_aligned(make_valid_bond(s_n, s), make_valid_bond(k, n))) {
ap_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
}
else if (eucl_dist(k, n) < 0.36 && eucl_dist(k, n) > 0.35) {
std::vector<Key> clust_k, clust_n;
for (auto const& u: fine_neighbours_list[s]) if (dense_cluster_map[u]) clust_k.push_back(u);
for (auto const& u: fine_neighbours_list[s_n]) if (dense_cluster_map[u]) clust_n.push_back(u);
float dist = eucl_dist(k, n);
if (!visited[make_valid_bond(k, n)] && common_element(clust_k, clust_n) && dist < 0.36) { // only diagonal links we want are the 0.25*sqrt(2)-length ones
ap_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
}
}
}
}
Key first(0, 0), last(0, 0), current(0, 0);
float total_distance = 0;
for (auto const& [k, exists]: ap_points) if (exists) {
if (k.i == 0) {
first = k, current = first;
ordered_hull_keys[k] = total_distance;
}
else if (k.i == grid_size-1) last = k;
}
while (current != last) {
for (auto& [b, exists]: visited) if (exists) {
if (b.a == current) {
total_distance += eucl_dist(b.b, current);
ordered_ap_keys[b.a] = total_distance;
ordered_ap.push_back(b.b);
current = b.b;
exists = false;
break;
}
else if (b.b == current) {
total_distance += eucl_dist(b.a, current);
ordered_ap_keys[b.a] = total_distance;
ordered_ap.push_back(b.a);
current = b.a;
exists = false;
break;
}
}
}
std::cout << "DONE" << std::endl << std::flush;
tend = time(0);
std::cout << "It took " << difftime(tend, tstart) << " to compute and link the external hull." << std::endl;
linked_ap = ap_graph;
if (g.grid_size > 50) write_ap();
return ap_graph;
}
std::vector<Bond> Hull::old_link_hull() {
std::cout << "STARTING HULL LINKAGE..." << std::endl << std::flush;
time_t tstart, tend;
tstart = time(0);
if (hull.empty()) determine_hull();
std::vector<Bond> hull_graph;
std::map<Bond, bool> hull_map;
std::map<Bond, bool> visited;
std::map<Key, std::vector<Key>> support; // takes an AP key and returns its corresponding supporting key(s)
// find the non-cluster points
for (auto const& b: hull) {
if (!cluster_map[b.a] == true) {
hull_points[b.a] = true;
support[b.a].push_back(b.b);
}
else if (!cluster_map[b.b] == true) {
hull_points[b.b] = true;
support[b.b].push_back(b.a);
}
hull_map[b] = true;
}
// for each point of the hull, find its natural neighbours using the white scaffolding
for (auto const& [k, v_s]: support) {
std::vector<Key> neighbourhood;
std::vector<Bond> candidates;
for (auto const& n: square(k, 0.75)) if (hull_points[n] && k != n) {
neighbourhood.push_back(n);
//if (eucl_dist(k, n) == 0.5) neighbourhood.push_back(n);
//else if (eucl_dist(k, n) < 0.36 && eucl_dist(k, n) > 0.35) neighbourhood.push_back(n);
}
for (auto const& n: neighbourhood) {
std::vector<Key> v_s_n = support[n];
for (auto const& s: v_s) for (auto const& s_n: v_s_n) {
if (g.bond_parameters[make_valid_bond(s_n, s)]) {
if (!visited[make_valid_bond(k, n)] && parallel_aligned(make_valid_bond(s_n, s), make_valid_bond(k, n))) {
if (inbetween_key(k, n, hull_points) == false) {
if (!common_element(v_s, v_s_n)) {
hull_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
}
}
}
else if (!visited[make_valid_bond(k, n)] && perpendicular(make_valid_bond(s_n, n), make_valid_bond(s, k))) {
if (s_n == s && k != n) {
hull_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
else {
std::vector<Key> clust_k, clust_n;
for (auto const& u: fine_neighbours_list[s]) if (cluster_map[u]) clust_k.push_back(u);
for (auto const& u: fine_neighbours_list[s_n]) if (cluster_map[u]) clust_n.push_back(u);
float dist = eucl_dist(k, n);
if (common_element(clust_k, clust_n) && dist < 0.36) { // only diagonal links we want are the 0.25*sqrt(2)-length ones
hull_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
}
}
}
}
}
// order out all of the hull keys using the previously generated graph
Key first(0, 0), last(0, 0), current(0, 0);
float total_distance = 0;
for (auto const& [k, exists]: hull_points) if (exists) {
if (k.i == 0) {
first = k, current = first;
ordered_hull_keys[k] = total_distance;
}
else if (k.i == grid_size-1) last = k;
}
while (current != last) {
for (auto& [b, exists]: visited) if (exists) {
if (b.a == current) {
total_distance += eucl_dist(b.b, current);
ordered_hull_keys[b.b] = total_distance;
current = b.b;
exists = false;
break;
}
else if (b.b == current) {
total_distance += eucl_dist(b.a, current);
ordered_hull_keys[b.a] = total_distance;
current = b.a;
exists = false;
break;
}
}
}
std::cout << "DONE" << std::endl << std::flush;
tend = time(0);
std::cout << "It took " << difftime(tend, tstart) << " to compute and link the complete hull." << std::endl;
linked_hull = hull_graph;
return linked_hull;
}
std::vector<Bond> Hull::link_hull() {
std::cout << "STARTING HULL LINKAGE..." << std::endl << std::flush;
time_t tstart, tend;
tstart = time(0);
if (hull.empty()) determine_hull();
std::vector<Bond> hull_graph;
std::map<Bond, bool> hull_map;
std::map<Bond, bool> visited;
std::map<Key, std::vector<Key>> support; // takes an AP key and returns its corresponding supporting key(s)
// find the non-cluster points
for (auto const& b: hull) {
if (!cluster_map[b.a] == true) {
hull_points[b.a] = true;
support[b.a].push_back(b.b);
}
else if (!cluster_map[b.b] == true) {
hull_points[b.b] = true;
support[b.b].push_back(b.a);
}
hull_map[b] = true;
}
// for each point of the hull, find its natural neighbours using the white scaffolding
for (auto const& [k, v_s]: support) {
std::vector<Key> neighbourhood;
std::vector<Bond> candidates;
for (auto const& n: square(k, 0.75)) if (hull_points[n] && k != n) {
// make a list of the eligible neighbours (only 0.5 vert/hor and 0.353 diagonal)
if (eucl_dist(k, n) == 0.5) neighbourhood.push_back(n);
else if (eucl_dist(k, n) < 0.36 && eucl_dist(k, n) > 0.35) neighbourhood.push_back(n);
}
for (auto const& n: neighbourhood) {
std::vector<Key> v_s_n = support[n];
for (auto const& s: v_s) for (auto const& s_n: v_s_n) {
if (eucl_dist(k, n) == 0.5) {
// for 0.5 vert/hor links, they have to be non-visited and perpendicular to their scaffolds
if (!visited[make_valid_bond(k, n)] && parallel_aligned(make_valid_bond(s_n, s), make_valid_bond(k, n))) {
hull_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
}
// otherwise for diagonal links, we distinguish 2 cases
else if (eucl_dist(k, n) < 0.36 && eucl_dist(k, n) > 0.35) {
std::vector<Key> clust_k, clust_n;
for (auto const& u: fine_neighbours_list[s]) if (cluster_map[u]) clust_k.push_back(u);
for (auto const& u: fine_neighbours_list[s_n]) if (cluster_map[u]) clust_n.push_back(u);
if (!visited[make_valid_bond(k, n)] && common_element(clust_k, clust_n)) {
hull_graph.push_back(make_valid_bond(k, n));
visited[make_valid_bond(k, n)] = true;
}
}
}
}
}
// order out all of the hull keys using the previously generated graph
Key first(0, 0), last(0, 0), current(0, 0);
float total_distance = 0;
for (auto const& [k, exists]: hull_points) if (exists) {
if (k.i == 0) {
first = k, current = first;
ordered_hull_keys[k] = total_distance;
ordered_hull.push_back(k);
}
else if (k.i == grid_size-1) last = k;
}
while (current != last) {
for (auto& [b, exists]: visited) if (exists) {
if (b.a == current) {
total_distance += eucl_dist(b.b, current);
ordered_hull_keys[b.b] = total_distance;
ordered_hull.push_back(b.b);
current = b.b;
exists = false;
break;
}
else if (b.b == current) {
total_distance += eucl_dist(b.a, current);
ordered_hull_keys[b.a] = total_distance;
ordered_hull.push_back(b.a);
current = b.a;
exists = false;
break;
}
}
}
std::cout << "DONE" << std::endl << std::flush;
tend = time(0);
std::cout << "It took " << difftime(tend, tstart) << " to compute and link the complete hull." << std::endl;
linked_hull = hull_graph;
if (g.grid_size > 50) write_hull();
return linked_hull;
}
void Hull::write_hull() {
if (linked_hull.empty()) link_hull();
//namespace fs = std::filesystem;
//fs::create_directory("data/hull/q="+ std::to_string(q) +"/"+ std::to_string(gs));
std::string name = gen_name();
std::ofstream hfile;
int q = static_cast<int>(g.q), gs = static_cast<int>(g.grid_size);
hfile.open("raw_grids/hull/"+name+"_gs=" + std::to_string(gs) + ".csv", std::ios_base::app);
hfile << "#Ns= " << ordered_hull.size()-1 << std::endl;
hfile << "# Hull of a fixed-boundary cluster" << std::endl;
hfile << "#gs=" << grid_size << std::endl << "#q=" << q << std::endl << "#ne=" << g.evolve_steps;
for (int i = 0; i < ordered_hull.size()-1; i++) {
Key k1 = ordered_hull[i], k2 = ordered_hull[i+1];
hfile << k1.j << " " << k1.i << " " << k2.j << " " << k2.i << std::endl;
}
hfile.close();
}
void Hull::write_ap() {
std::string name = gen_name();
std::ofstream apfile;
int q = static_cast<int>(g.q), gs = static_cast<int>(g.grid_size);
apfile.open("raw_grids/ap/"+name+"_gs=" + std::to_string(gs) + ".csv", std::ios_base::app);
apfile << "#Ns= " << ordered_ap.size()-1 << std::endl;
apfile << "# Accessible perimeter of a fixed-boundary cluster" << std::endl;
apfile << "#gs=" << grid_size << std::endl << "#q=" << q << std::endl << "#ne=" << g.evolve_steps;
for (int i = 0; i < ordered_ap.size()-1; i++) apfile << ordered_ap[i].j << std::endl;
apfile.close();
}
float Hull::hull_length() {
if (linked_hull.empty()) link_hull();
float dist = 0;
for (auto const& b: linked_hull) {
dist += bond_length(b);
}
return dist;
}
float Hull::hull_width() {
if (linked_hull.empty()) link_hull();
float width = 0, mean_col = 0;;
for (auto const& k: ordered_hull) mean_col += k.j;
mean_col = mean_col/hull.size();
for (auto const& k: ordered_hull) {
width += pow(mean_col - k.j, 2);
}
return sqrt(width/hull.size());
}
float Hull::ap_length() {
if (linked_ap.empty()) link_ap();
float dist = 0;
for (auto const& b: linked_ap) dist += bond_length(b);
return dist;
}
float Hull::ap_width() {
if (ap.empty()) determine_ap();
float width = 0, mean_col = 0;;
for (auto const& b: ap) {
Key k = cluster_map[b.a] ? b.b : b.a;
mean_col += k.j;
}
mean_col = mean_col/ap.size();
for (auto const& b: ap) {
Key k = cluster_map[b.a] ? b.b : b.a;
width += std::abs(mean_col - k.j);
}
return width/ap.size();
}
std::vector<std::map<double, double>> Hull::yardstick_ap(std::vector<double> radii) {
std::map<double, double> hull_data, ap_data;
time_t tstart, tend;
tstart = time(0);
if (hull.empty()) determine_hull();
if (linked_hull.empty()) link_hull();
if (ap.empty()) determine_ap();
if (linked_ap.empty()) link_ap();
Key first(0, 0), last(0, 0), current(0, 0);
for (auto const& [k, exists]: hull_points) if (exists) {
if (k.i == 0) first = k;
else if (k.i == grid_size-1) last = k;
}
for (auto const& r: radii) {
std::map<Bond, double> candidate_crossings; // maps the crossed bond and the smallest graph length of its two keys
double total_distance = 0;
double current_length = 0;
current = first;
do {
candidate_crossings.clear();
for (auto const& b: linked_hull) {
bool crossed = intercrossing_link(b, current, r);
if (crossed) {
Key closest = ordered_hull_keys[b.a] < ordered_hull_keys[b.b] ? b.a : b.b;
double distance = ordered_hull_keys[closest];
if (distance > current_length) candidate_crossings[b] = distance;
}
}
// treating the case of no crossings because we've reached the end
if (candidate_crossings.empty() && eucl_dist(current, last) <= r) total_distance += eucl_dist(current, last);
else if (candidate_crossings.empty() && eucl_dist(current, last) > r) {
std::cout << "No candidate crossings, but distance to end is larger than " << r << " (from " << current << ") " << std::endl;
display_yardstick_h("nocrossyardstick.png", r);
}
else {
Bond crossed_bond = candidate_crossings.begin()->first;
double min_length = candidate_crossings.begin()->second;
Key temp_current(0, 0);
//std::cout << "Candidates: " << candidate_crossings.size();
for (auto const& [b, len]: candidate_crossings) {
//std::cout << b << " :: " << len << std::endl;
if (len <= min_length) {
min_length = len;