-
Notifications
You must be signed in to change notification settings - Fork 14
/
isoband.cpp
1628 lines (1525 loc) · 54.2 KB
/
isoband.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
// This file implements the 2D isoline and isoband algorithms described
// here: https://en.wikipedia.org/wiki/Marching_squares
// Includes merging of line segments and polygons.
// Written by Claus O. Wilke
// [[Rcpp::plugins(cpp11)]]
#include <Rcpp.h>
using namespace Rcpp;
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;
#include "polygon.h" // for point
// point in abstract grid space
enum point_type {
grid, // point on the original data grid
hintersect_lo, // intersection with horizontal edge, low value
hintersect_hi, // intersection with horizontal edge, high value
vintersect_lo, // intersection with vertical edge, low value
vintersect_hi // intersection with vertical edge, high value
};
struct grid_point {
int r, c; // row and column
point_type type; // point type
// default constructor; negative values indicate non-existing point off grid
grid_point(double r_in = -1, double c_in = -1, point_type type_in = grid) : r(r_in), c(c_in), type(type_in) {}
// copy constructor
grid_point(const grid_point &p) : r(p.r), c(p.c), type(p.type) {}
};
// hash function for grid_point
struct grid_point_hasher {
size_t operator()(const grid_point& p) const
{
// this should work up to about 100,000,000 rows/columns
return hash<long long>()(
(static_cast<long long>(p.r) << 30) ^
(static_cast<long long>(p.c) << 3) ^
static_cast<long long>(p.type));
}
};
bool operator==(const grid_point &p1, const grid_point &p2) {
return (p1.r == p2.r) && (p1.c == p2.c) && (p1.type == p2.type);
}
ostream & operator<<(ostream &out, const grid_point &p) {
out << "(" << p.c << ", " << p.r << ", " << p.type << ")";
return out;
}
// connection between points in grid space
struct point_connect {
grid_point prev, next; // previous and next points in polygon
grid_point prev2, next2; // alternative previous and next, when two separate polygons have vertices on the same grid point
bool altpoint; // does this connection hold an alternative point?
bool collected, collected2; // has this connection been collected into a final polygon?
point_connect() : altpoint(false), collected(false), collected2(false) {};
};
ostream & operator<<(ostream &out, const point_connect &pc) {
out << "prev: " << pc.prev << "; next: " << pc.next << " ";
if (pc.altpoint) {
out << "AP prev: " << pc.prev2 << "; next2: " << pc.next2 << " ";
}
return out;
}
class isobander {
protected:
int nrow, ncol; // numbers of rows and columns
const NumericVector &grid_x,&grid_y;
const NumericMatrix &grid_z;
double vlo, vhi; // low and high cutoff values
grid_point tmp_poly[8]; // temp storage for elementary polygons; none has more than 8 vertices
point_connect tmp_point_connect[8];
int tmp_poly_size; // current number of elements in tmp_poly
typedef unordered_map<grid_point, point_connect, grid_point_hasher> gridmap;
gridmap polygon_grid;
void reset_grid() {
polygon_grid.clear();
for (int i=0; i<8; i++) {
tmp_point_connect[i] = point_connect();
}
}
// internal member functions
double central_value(int r, int c) {// calculates the central value of a given cell
return (grid_z(r, c) + grid_z(r, c + 1) + grid_z(r + 1, c) + grid_z(r + 1, c + 1))/4;
}
void poly_start(int r, int c, point_type type) { // start a new elementary polygon
tmp_poly[0].r = r;
tmp_poly[0].c = c;
tmp_poly[0].type = type;
tmp_poly_size = 1;
}
void poly_add(int r, int c, point_type type) { // add point to elementary polygon
tmp_poly[tmp_poly_size].r = r;
tmp_poly[tmp_poly_size].c = c;
tmp_poly[tmp_poly_size].type = type;
tmp_poly_size++;
}
void poly_merge() { // merge current elementary polygon to prior polygons
//cout << "before merging:" << endl;
bool to_delete[] = {false, false, false, false, false, false, false, false};
// first, we figure out the right connections for current polygon
for (int i = 0; i < tmp_poly_size; i++) {
// create defined state in tmp_point_connect[]
// for each point, find previous and next point in polygon
tmp_point_connect[i].altpoint = false;
tmp_point_connect[i].next = tmp_poly[(i+1<tmp_poly_size) ? i+1 : 0];
tmp_point_connect[i].prev = tmp_poly[(i-1>=0) ? i-1 : tmp_poly_size-1];
//cout << tmp_poly[i] << ": " << tmp_point_connect[i] << endl;
// now merge with existing polygons if needed
const grid_point &p = tmp_poly[i];
if (polygon_grid.count(p) > 0) { // point has been used before, need to merge polygons
if (!polygon_grid[p].altpoint) {
// basic scenario, no alternative point at this location
int score = 2 * (tmp_point_connect[i].next == polygon_grid[p].prev) + (tmp_point_connect[i].prev == polygon_grid[p].next);
switch (score) {
case 3: // 11
// both prev and next cancel, point can be deleted
to_delete[i] = true;
break;
case 2: // 10
// merge in "next" direction
tmp_point_connect[i].next = polygon_grid[p].next;
break;
case 1: // 01
// merge in "prev" direction
tmp_point_connect[i].prev = polygon_grid[p].prev;
break;
default: // 00
// if we get here, we have two polygon vertices sharing the same grid location
// in an unmergable configuration; need to store both
tmp_point_connect[i].prev2 = polygon_grid[p].prev;
tmp_point_connect[i].next2 = polygon_grid[p].next;
tmp_point_connect[i].altpoint = true;
}
} else {
// case with alternative point at this location
int score =
8 * (tmp_point_connect[i].next == polygon_grid[p].prev2) + 4 * (tmp_point_connect[i].prev == polygon_grid[p].next2) +
2 * (tmp_point_connect[i].next == polygon_grid[p].prev) + (tmp_point_connect[i].prev == polygon_grid[p].next);
switch (score) {
case 9: // 1001
// three-way merge
tmp_point_connect[i].next = polygon_grid[p].next2;
tmp_point_connect[i].prev = polygon_grid[p].prev;
break;
case 6: // 0110
// three-way merge
tmp_point_connect[i].next = polygon_grid[p].next;
tmp_point_connect[i].prev = polygon_grid[p].prev2;
break;
case 8: // 1000
// two-way merge with alt point only
// set up merged alt point
tmp_point_connect[i].next2 = polygon_grid[p].next2;
tmp_point_connect[i].prev2 = tmp_point_connect[i].prev;
// copy over existing point as is
tmp_point_connect[i].prev = polygon_grid[p].prev;
tmp_point_connect[i].next = polygon_grid[p].next;
tmp_point_connect[i].altpoint = true;
break;
case 4: // 0100
// two-way merge with alt point only
// set up merged alt point
tmp_point_connect[i].prev2 = polygon_grid[p].prev2;
tmp_point_connect[i].next2 = tmp_point_connect[i].next;
// copy over existing point as is
tmp_point_connect[i].prev = polygon_grid[p].prev;
tmp_point_connect[i].next = polygon_grid[p].next;
tmp_point_connect[i].altpoint = true;
break;
case 2: // 0010
// two-way merge with original point only
// merge point
tmp_point_connect[i].next = polygon_grid[p].next;
// copy over existing alt point as is
tmp_point_connect[i].prev2 = polygon_grid[p].prev2;
tmp_point_connect[i].next2 = polygon_grid[p].next2;
tmp_point_connect[i].altpoint = true;
break;
case 1: // 0100
// two-way merge with original point only
// merge point
tmp_point_connect[i].prev = polygon_grid[p].prev;
// copy over existing alt point as is
tmp_point_connect[i].prev2 = polygon_grid[p].prev2;
tmp_point_connect[i].next2 = polygon_grid[p].next2;
tmp_point_connect[i].altpoint = true;
break;
default:
stop("undefined merging configuration: %i\n", score);
}
}
}
}
//cout << "after merging:" << endl;
// then we copy the connections into the polygon matrix
for (int i = 0; i < tmp_poly_size; i++) {
const grid_point &p = tmp_poly[i];
if (to_delete[i]) { // delete point if needed
polygon_grid.erase(p);
} else { // otherwise, copy
polygon_grid[p] = tmp_point_connect[i];
}
//cout << p << ": " << tmp_point_connect[i] << endl;
}
//cout << "new grid:" << endl;
//print_polygons_state();
}
void print_polygons_state() {
for (auto it = polygon_grid.begin(); it != polygon_grid.end(); it++) {
cout << it->first << ": " << it->second << endl;
}
cout << endl;
}
// linear interpolation of boundary intersections
double interpolate(double x0, double x1, double z0, double z1, double value) {
double d = (value - z0) / (z1 - z0);
double x = x0 + d * (x1 - x0);
return x;
}
// calculate output coordinates for a given grid point
point calc_point_coords(const grid_point &p) {
switch(p.type) {
case grid:
return point(grid_x[p.c], grid_y[p.r]);
case hintersect_lo: // intersection with horizontal edge, low value
return point(interpolate(grid_x[p.c], grid_x[p.c+1], grid_z(p.r, p.c), grid_z(p.r, p.c+1), vlo), grid_y[p.r]);
case hintersect_hi: // intersection with horizontal edge, high value
return point(interpolate(grid_x[p.c], grid_x[p.c+1], grid_z(p.r, p.c), grid_z(p.r, p.c+1), vhi), grid_y[p.r]);
case vintersect_lo: // intersection with vertical edge, low value
return point(grid_x[p.c], interpolate(grid_y[p.r], grid_y[p.r+1], grid_z(p.r, p.c), grid_z(p.r+1, p.c), vlo));
case vintersect_hi: // intersection with vertical edge, high value
return point(grid_x[p.c], interpolate(grid_y[p.r], grid_y[p.r+1], grid_z(p.r, p.c), grid_z(p.r+1, p.c), vhi));
default:
return point(0, 0); // should never get here
}
}
public:
isobander(const NumericVector &x, const NumericVector &y, const NumericMatrix &z, double value_low = 0, double value_high = 0) :
grid_x(x), grid_y(y), grid_z(z), vlo(value_low), vhi(value_high)
{
nrow = grid_z.nrow();
ncol = grid_z.ncol();
if (grid_x.size() != ncol) {stop("Number of x coordinates must match number of columns in density matrix.");}
if (grid_y.size() != nrow) {stop("Number of y coordinates must match number of rows in density matrix.");}
}
virtual ~isobander() {}
void set_value(double value_low, double value_high) {
vlo = value_low;
vhi = value_high;
}
virtual void calculate_contour() {
// clear polygon grid and associated internal variables
reset_grid();
// setup matrix of ternarized cell representations
IntegerVector v(nrow*ncol);
IntegerVector::iterator iv = v.begin();
for (NumericMatrix::const_iterator it = grid_z.begin(); it != grid_z.end(); it++) {
*iv = (*it >= vlo && *it < vhi) + 2*(*it >= vhi);
iv++;
}
IntegerMatrix ternarized(nrow, ncol, v.begin());
IntegerMatrix cells(nrow - 1, ncol - 1);
for (int r = 0; r < nrow-1; r++) {
for (int c = 0; c < ncol-1; c++) {
int index;
if (NumericMatrix::is_na(grid_z(r, c)) || NumericMatrix::is_na(grid_z(r, c + 1)) ||
NumericMatrix::is_na(grid_z(r + 1, c)) || NumericMatrix::is_na(grid_z(r + 1, c + 1))) {
// we don't draw any contours if at least one of the corners is NA
index = 0;
} else {
index = 27*ternarized(r, c) + 9*ternarized(r, c + 1) + 3*ternarized(r + 1, c + 1) + ternarized(r + 1, c);
}
cells(r, c) = index;
//cout << index << " ";
}
//cout << endl;
}
checkUserInterrupt();
// all polygons must be drawn clockwise for proper merging
for (int r = 0; r < nrow-1; r++) {
for (int c = 0; c < ncol-1; c++) {
//cout << r << " " << c << " " << cells(r, c) << endl;
switch(cells(r, c)) {
// doing cases out of order, sorted by type, is easier to keep track of
// no contour
case 0: break;
case 80: break;
// single triangle
case 1: // 0001
poly_start(r, c, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, grid);
poly_merge();
break;
case 3: // 0010
poly_start(r, c+1, vintersect_lo);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_merge();
break;
case 9: // 0100
poly_start(r, c, hintersect_lo);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_merge();
break;
case 27: // 1000
poly_start(r, c, vintersect_lo);
poly_add(r, c, grid);
poly_add(r, c, hintersect_lo);
poly_merge();
break;
case 79: // 2221
poly_start(r, c, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, grid);
poly_merge();
break;
case 77: // 2212
poly_start(r, c+1, vintersect_hi);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_merge();
break;
case 71: // 2122
poly_start(r, c, hintersect_hi);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_merge();
break;
case 53: // 1222
poly_start(r, c, vintersect_hi);
poly_add(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_merge();
break;
// single trapezoid
case 78: // 2220
poly_start(r, c, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
case 74: // 2202
poly_start(r+1, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_merge();
break;
case 62: // 2022
poly_start(r, c+1, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_merge();
break;
case 26: // 0222
poly_start(r, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_add(r, c, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_merge();
break;
case 2: // 0002
poly_start(r, c, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 6: // 0020
poly_start(r+1, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_merge();
break;
case 18: // 0200
poly_start(r, c+1, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_merge();
break;
case 54: // 2000
poly_start(r, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_merge();
break;
// single rectangle
case 4: // 0011
poly_start(r, c, vintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, grid);
poly_merge();
break;
case 12: // 0110
poly_start(r, c, hintersect_lo);
poly_add(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_merge();
break;
case 36: // 1100
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
case 28: // 1001
poly_start(r, c, hintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, grid);
poly_add(r, c, grid);
poly_merge();
break;
case 76: // 2211
poly_start(r, c, vintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, grid);
poly_merge();
break;
case 68: // 2112
poly_start(r, c, hintersect_hi);
poly_add(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_merge();
break;
case 44: // 1122
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 52: // 1221
poly_start(r, c, hintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, grid);
poly_add(r, c, grid);
poly_merge();
break;
case 72: // 2200
poly_start(r, c, vintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
case 56: // 2002
poly_start(r, c, hintersect_hi);
poly_add(r, c, hintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, hintersect_hi);
poly_merge();
break;
case 8: // 0022
poly_start(r, c, vintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 24: // 0220
poly_start(r, c, hintersect_lo);
poly_add(r, c, hintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, hintersect_lo);
poly_merge();
break;
// single square
case 40: // 1111
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, grid);
poly_merge();
break;
// single pentagon
case 49: // 1211
poly_start(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, grid);
poly_merge();
break;
case 67: // 2111
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_merge();
break;
case 41: // 1112
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 43: // 1121
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, grid);
poly_merge();
break;
case 31: // 1011
poly_start(r, c, grid);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, grid);
poly_merge();
break;
case 13: // 0111
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_merge();
break;
case 39: // 1110
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
case 37: // 1101
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, grid);
poly_merge();
break;
case 45: // 1200
poly_start(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
case 15: // 0120
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, hintersect_lo);
poly_merge();
break;
case 5: // 0012
poly_start(r, c, vintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 55: // 2001
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c, hintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_merge();
break;
case 35: // 1022
poly_start(r, c, grid);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 65: // 2102
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, hintersect_hi);
poly_merge();
break;
case 75: // 2210
poly_start(r, c, vintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
case 25: // 0221
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_add(r, c, hintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_merge();
break;
case 29: // 1002
poly_start(r, c, grid);
poly_add(r, c, hintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 63: // 2100
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c, vintersect_lo);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_merge();
break;
case 21: // 0210
poly_start(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, hintersect_lo);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_merge();
break;
case 7: // 0021
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_merge();
break;
case 51: // 1220
poly_start(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
case 17: // 0122
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c, vintersect_hi);
poly_add(r, c, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_merge();
break;
case 59: // 2012
poly_start(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_merge();
break;
case 73: // 2201
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_merge();
break;
// single hexagon
case 22: // 0211
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c+1, grid);
poly_merge();
break;
case 66: // 2110
poly_start(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_merge();
break;
case 38: // 1102
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 34: // 1021
poly_start(r, c, grid);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, grid);
poly_merge();
break;
case 58: // 2011
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c+1, grid);
poly_merge();
break;
case 14: // 0112
poly_start(r, c+1, grid);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_add(r, c, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_merge();
break;
case 42: // 1120
poly_start(r, c, grid);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
case 46: // 1201
poly_start(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_add(r+1, c, grid);
poly_merge();
break;
case 64: // 2101
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_merge();
break;
case 16: // 0121
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, grid);
poly_add(r, c, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_merge();
break;
case 32: // 1012
poly_start(r, c, grid);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
break;
case 48: // 1210
poly_start(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
break;
// 6-sided saddle
case 10: // 0101
{
double vc = central_value(r, c);
if (vc < vlo) {
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_merge();
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_merge();
} else {
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_lo);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_merge();
}
}
break;
case 30: // 1010
{
double vc = central_value(r, c);
if (vc < vlo) {
poly_start(r, c, grid);
poly_add(r, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
poly_start(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_merge();
} else {
poly_start(r, c, grid);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
}
}
break;
case 70: // 2121
{
double vc = central_value(r, c);
if (vc >= vhi) {
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_merge();
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_merge();
} else {
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_merge();
}
}
break;
case 50: // 1212
{
double vc = central_value(r, c);
if (vc >= vhi) {
poly_start(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
poly_start(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_merge();
} else {
poly_start(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c+1, grid);
poly_add(r+1, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
}
}
break;
// 7-sided saddle
case 69: // 2120
{
double vc = central_value(r, c);
if (vc >= vhi) {
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_merge();
poly_start(r, c, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_merge();
} else {
poly_start(r, c+1, grid);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_add(r+1, c, hintersect_lo);
poly_add(r, c, vintersect_lo);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_merge();
}
}
break;
case 61: // 2021
{
double vc = central_value(r, c);
if (vc >= vhi) {
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_merge();
poly_start(r, c+1, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_merge();
} else {
poly_start(r+1, c, grid);
poly_add(r, c, vintersect_hi);
poly_add(r, c, hintersect_hi);
poly_add(r, c, hintersect_lo);
poly_add(r, c+1, vintersect_lo);
poly_add(r, c+1, vintersect_hi);
poly_add(r+1, c, hintersect_hi);
poly_merge();
}
}
break;
case 47: // 1202
{
double vc = central_value(r, c);
if (vc >= vhi) {
poly_start(r, c, grid);
poly_add(r, c, hintersect_hi);
poly_add(r, c, vintersect_hi);
poly_merge();
poly_start(r+1, c, hintersect_hi);
poly_add(r, c+1, vintersect_hi);
poly_add(r, c+1, vintersect_lo);
poly_add(r+1, c, hintersect_lo);
poly_merge();
} else {