-
Notifications
You must be signed in to change notification settings - Fork 0
/
DgIDGG.cpp
1245 lines (997 loc) · 38.6 KB
/
DgIDGG.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
////////////////////////////////////////////////////////////////////////////////
//
// DgIDGG.cpp: DgIDGG class implementation
//
// Version 6.1 - Kevin Sahr, 5/23/13
//
////////////////////////////////////////////////////////////////////////////////
#include <cmath>
#include <limits>
#include <cstdint>
#include "DgUtil.h"
#include "DgIDGG.h"
#include "DgDmdD4Grid2DS.h"
#include "DgHexGrid2DS.h"
#include "DgTriGrid2DS.h"
#include "DgSeriesConverter.h"
#include "DgRadixString.h"
#include "DgBoundedIDGG.h"
#include "DgProjISEA.h"
#include "DgProjFuller.h"
const DgQ2DICoord DgQ2DICoord::undefDgQ2DICoord(-1,
DgIVec2D(std::numeric_limits<std::int64_t>::max(), std::numeric_limits<std::int64_t>::max()));
const DgQ2DDCoord DgQ2DDCoord::undefDgQ2DDCoord(-1,
DgDVec2D(std::numeric_limits<long double>::max(), std::numeric_limits<long double>::max()));
////////////////////////////////////////////////////////////////////////////////
DgVertTriVals DgVertex2DDRF::vertTable_[12][6] = {
{ // vert 0
DgVertTriVals( 2, 1, 0, true, DgDVec2D(-M_HALF, -M_SIN60), 3 ),
DgVertTriVals( 1, 0, 1, true, DgDVec2D(-M_ONE, M_ZERO ), 2 ),
DgVertTriVals( 5, 4, 2, true, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( -1, -1, 3, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 4, 3, 4, true, DgDVec2D( M_ONE, M_ZERO ), -1 ),
DgVertTriVals( 3, 2, 5, true, DgDVec2D( M_HALF, -M_SIN60), -2 )
},
{ // vert 1
DgVertTriVals( 1, 0, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 1, 5, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 10, 14, 2, true, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( -1, -1, 3, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 5, 9, 4, true, DgDVec2D( M_ZERO, M_ZERO), 3 ),
DgVertTriVals( 5, 4, 5, true, DgDVec2D( M_ONE, M_ZERO), 0 )
},
{ // vert 2
DgVertTriVals( 2, 1, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 2, 6, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 10, 10, 2, true, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( -1, -1, 3, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 1, 5, 4, true, DgDVec2D( M_ZERO, M_ZERO), 3 ),
DgVertTriVals( 1, 0, 5, true, DgDVec2D( M_ONE, M_ZERO), 0 )
},
{ // vert 3
DgVertTriVals( 3, 2, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 3, 7, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 7, 11, 2, true, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( -1, -1, 3, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 2, 6, 4, true, DgDVec2D( M_ZERO, M_ZERO), 3 ),
DgVertTriVals( 2, 1, 5, true, DgDVec2D( M_ONE, M_ZERO), 0 )
},
{ // vert 4
DgVertTriVals( 4, 3, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 4, 8, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 8, 12, 2, true, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( -1, -1, 3, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 3, 7, 4, true, DgDVec2D( M_ZERO, M_ZERO), 3 ),
DgVertTriVals( 3, 2, 5, true, DgDVec2D( M_ONE, M_ZERO), 0 )
},
{ // vert 5
DgVertTriVals( 5, 4, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 5, 9, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 9, 13, 2, true, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( -1, -1, 3, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 4, 8, 4, true, DgDVec2D( M_ZERO, M_ZERO), 3 ),
DgVertTriVals( 4, 3, 5, true, DgDVec2D( M_ONE, M_ZERO), 0 )
},
{ // vert 6
DgVertTriVals( 6, 10, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 6, 15, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 10, 19, 2, true, DgDVec2D( M_ZERO, M_ZERO), -1 ),
DgVertTriVals( 10, 14, 3, true, DgDVec2D(-M_HALF, M_SIN60), 2 ),
DgVertTriVals( -1, -1, 4, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 1, 5, 5, true, DgDVec2D( M_HALF, -M_SIN60), 4 )
},
{ // vert 7
DgVertTriVals( 7, 11, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 7, 16, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 6, 15, 2, true, DgDVec2D( M_ZERO, M_ZERO), -1 ),
DgVertTriVals( 6, 10, 3, true, DgDVec2D(-M_HALF, M_SIN60), 2 ),
DgVertTriVals( -1, -1, 4, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 2, 6, 5, true, DgDVec2D( M_HALF, -M_SIN60), 4 )
},
{ // vert 8
DgVertTriVals( 8, 12, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 8, 17, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 7, 16, 2, true, DgDVec2D( M_ZERO, M_ZERO), -1 ),
DgVertTriVals( 7, 11, 3, true, DgDVec2D(-M_HALF, M_SIN60), 2 ),
DgVertTriVals( -1, -1, 4, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 3, 7, 5, true, DgDVec2D( M_HALF, -M_SIN60), 4 )
},
{ // vert 9
DgVertTriVals( 9, 13, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 9, 18, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 8, 17, 2, true, DgDVec2D( M_ZERO, M_ZERO), -1 ),
DgVertTriVals( 8, 12, 3, true, DgDVec2D(-M_HALF, M_SIN60), 2 ),
DgVertTriVals( -1, -1, 4, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 4, 8, 5, true, DgDVec2D( M_HALF, -M_SIN60), 4 )
},
{ // vert 10
DgVertTriVals( 10, 14, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 10, 19, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 9, 18, 2, true, DgDVec2D( M_ZERO, M_ZERO), -1),
DgVertTriVals( 9, 13, 3, true, DgDVec2D(-M_HALF, M_SIN60), 2 ),
DgVertTriVals( -1, -1, 4, false, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 5, 9, 5, true, DgDVec2D( M_HALF, -M_SIN60), 4 )
},
{ // vert 11
DgVertTriVals( 8, 17, 0, true, DgDVec2D(-M_HALF, -M_SIN60), 3 ),
DgVertTriVals( 9, 18, 1, true, DgDVec2D(-M_ONE, M_ZERO), 2 ),
DgVertTriVals( 10, 19, 2, true, DgDVec2D(-M_HALF, M_SIN60), 1 ),
DgVertTriVals( 6, 15, 3, true, DgDVec2D( M_HALF, M_SIN60), 0 ),
DgVertTriVals( -1, -1, 4, false, DgDVec2D( M_ZERO, M_ZERO), 0 ),
DgVertTriVals( 7, 16, 5, true, DgDVec2D( M_HALF, -M_SIN60), -2 )
}
};
////////////////////////////////////////////////////////////////////////////////
const DgVertTriVals DgVertex2DDRF::triTable_[20] = {
DgVertTriVals( 1, 0, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 2, 1, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 3, 2, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 4, 3, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 5, 4, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 1, 5, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 2, 6, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 3, 7, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 4, 8, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 5, 9, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 6, 10, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 7, 11, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 8, 12, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 9, 13, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 10, 14, 0, true, DgDVec2D( M_ZERO, M_ZERO), 1 ),
DgVertTriVals( 6, 15, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 7, 16, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 8, 17, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 9, 18, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
DgVertTriVals( 10, 19, 1, true, DgDVec2D(-M_HALF, -M_SIN60), 4 ),
};
////////////////////////////////////////////////////////////////////////////////
const DgQuadEdgeCells DgIDGG::edgeTable_[12] = {
DgQuadEdgeCells(0, true, 0, 0, 0), // quad 0 should never occur
DgQuadEdgeCells(1, true, 0, 2, 6),
DgQuadEdgeCells(2, true, 0, 3, 7),
DgQuadEdgeCells(3, true, 0, 4, 8),
DgQuadEdgeCells(4, true, 0, 5, 9),
DgQuadEdgeCells(5, true, 0, 1, 10),
DgQuadEdgeCells(6, false, 11, 2, 7),
DgQuadEdgeCells(7, false, 11, 3, 8),
DgQuadEdgeCells(8, false, 11, 4, 9),
DgQuadEdgeCells(9, false, 11, 5, 10),
DgQuadEdgeCells(10, false, 11, 1, 6),
DgQuadEdgeCells(11, false, 11, 0, 0) // quad 11 should never occur
};
////////////////////////////////////////////////////////////////////////////////
const char*
DgQ2DDRF::str2add (DgQ2DDCoord* add, const char* str, char delimiter) const
{
if (!add) add = new DgQ2DDCoord();
char delimStr[2];
delimStr[0] = delimiter;
delimStr[1] = '\0';
char* tmpStr = new char[strlen(str) + 1];
strcpy(tmpStr, str);
// get the quadNum
char* tok = strtok(tmpStr, delimStr);
int q;
if (sscanf(tok, "%d", &q) != 1)
{
::report("DgQ2DDRF::fromString() invalid value in string " + string(tok),
DgBase::Fatal);
}
const char* tmp = &(str[strlen(tok) + 1]);
DgDVec2D vec;
tmp = vec.fromString(tmp, delimiter);
*add = DgQ2DDCoord(q, vec);
return tmp;
} // const char* DgQ2DDRF::str2add
////////////////////////////////////////////////////////////////////////////////
const char*
DgVertex2DDRF::str2add (DgVertex2DDCoord* add, const char* str, char delimiter) const
{
if (!add) add = new DgVertex2DDCoord();
char delimStr[2];
delimStr[0] = delimiter;
delimStr[1] = '\0';
char* tmpStr = new char[strlen(str) + 1];
strcpy(tmpStr, str);
// get the vertNum
char* tok = strtok(tmpStr, delimStr);
int vNum;
if (sscanf(tok, "%d", &vNum) != 1)
{
::report("DgVertex2DDCoord::fromString() invalid value in string " +
string(tok), DgBase::Fatal);
}
// get the triNum
tok = strtok(NULL, delimStr);
int tNum;
if (sscanf(tok, "%d", &tNum) != 1)
{
::report("DgVertex2DDCoord::fromString() invalid value in string " +
string(tok), DgBase::Fatal);
}
// get the keeper
tok = strtok(NULL, delimStr);
bool keep = true;;
if (strcmp(tok, "keep") == 0)
keep = true;
else if (strcmp(tok, "nokeep") == 0)
keep = false;
else
{
::report("DgVertex2DDCoord::fromString() invalid value in string " +
string(tok), DgBase::Fatal);
}
const char* tmp = &(str[(tok - tmpStr) + strlen(tok) + 1]);
DgDVec2D vec;
tmp = vec.fromString(tmp, delimiter);
*add = DgVertex2DDCoord(keep, vNum, tNum, vec);
return tmp;
} // const char* DgVertex2DDRF::str2add
////////////////////////////////////////////////////////////////////////////////
const char*
DgInterleaveRF::str2add (DgInterleaveCoord* add, const char* str,
char delimiter) const
{
if (!add) add = new DgInterleaveCoord();
char delimStr[2];
delimStr[0] = delimiter;
delimStr[1] = '\0';
char* tmpStr = new char[strlen(str) + 1];
strcpy(tmpStr, str);
char* tok = strtok(tmpStr, delimStr);
add->setValString(tok);
delete[] tmpStr;
unsigned int offset = strlen(tok) + 1;
if (offset >= strlen(str)) return 0;
else return &str[offset];
} // const char* DgInterleaveRF::str2add
////////////////////////////////////////////////////////////////////////////////
const char*
DgIDGG::str2add (DgQ2DICoord* add, const char* str, char delimiter) const
{
if (!add) add = new DgQ2DICoord();
char delimStr[2];
delimStr[0] = delimiter;
delimStr[1] = '\0';
char* tmpStr = new char[strlen(str) + 1];
strcpy(tmpStr, str);
// get the quadNum
char* tok = strtok(tmpStr, delimStr);
int q;
if (sscanf(tok, "%d", &q) != 1)
{
::report("DgQ2DIRF::fromString() invalid value in string " +
string(tok), DgBase::Fatal);
}
const char* tmp = &(str[strlen(tok) + 1]);
DgIVec2D vec;
tmp = vec.fromString(tmp, delimiter);
*add = DgQ2DICoord(q, vec);
return tmp;
} // const char* DgIDGG::str2add
////////////////////////////////////////////////////////////////////////////////
DgQ2DDtoIConverter::DgQ2DDtoIConverter (
const DgRF<DgQ2DDCoord, long double>& from, const DgRF<DgQ2DICoord, std::int64_t>& to)
: DgConverter<DgQ2DDCoord, long double, DgQ2DICoord, std::int64_t>(from, to),
pIDGG_ (0)
{
pIDGG_ = dynamic_cast<const DgIDGG*>(&toFrame());
if (!pIDGG_)
{
report("DgQ2DDtoIConverter::DgQ2DDtoIConverter(): "
" toFrame not of type DgIDGG", DgBase::Fatal);
}
} // DgQ2DDtoIConverter::DgQ2DDtoIConverter
////////////////////////////////////////////////////////////////////////////////
DgQ2DICoord
DgQ2DDtoIConverter::convertTypedAddress (const DgQ2DDCoord& addIn) const
{
DgLocation* loc = IDGG().ccFrame().makeLocation(addIn.coord());
#if DGDEBUG
cout << "DgQ2DDtoIConverter::convertTypedAddress loc: " << *loc << endl;
#endif
IDGG().grid2D().convert(loc);
#if DGDEBUG
cout << " ---> " << *loc << endl;
#endif
DgIVec2D coord = *IDGG().grid2D().getAddress(*loc);
delete loc;
int quadNum = addIn.quadNum();
const long double nudge = 0.0000001L;
if (coord.i() < 0 || coord.j() < 0) // maybe round-off error?
{
DgDVec2D tmp(addIn.coord());
tmp.setX(tmp.x() + nudge);
tmp.setY(tmp.y() + nudge);
loc = IDGG().ccFrame().makeLocation(tmp);
IDGG().grid2D().convert(loc);
coord = *IDGG().grid2D().getAddress(*loc);
delete loc;
}
std::int64_t edgeI = IDGG().maxI() + 1;
std::int64_t edgeJ = IDGG().maxJ() + 1;
if (coord.i() > edgeI || coord.j() > edgeJ) // maybe round-off error?
{
DgDVec2D tmp(addIn.coord());
tmp.setX(tmp.x() - nudge);
tmp.setY(tmp.y() - nudge);
loc = IDGG().ccFrame().makeLocation(tmp);
IDGG().grid2D().convert(loc);
coord = *IDGG().grid2D().getAddress(*loc);
delete loc;
}
if (coord.i() < 0 || coord.j() < 0 ||
coord.i() > edgeI || coord.j() > edgeJ)
{
report("DgQ2DDtoIConverter::convertTypedAddress(): "
" coordinate out of range: " + (string) coord, DgBase::Fatal);
}
else if (coord.i() == edgeI || coord.j() == edgeJ)
{
const DgQuadEdgeCells& ec = IDGG().edgeTable(quadNum);
if (ec.isType0())
{
if (coord.j() == edgeJ)
{
if (coord.i() == 0)
{
quadNum = ec.loneVert();
coord = DgIVec2D(0, 0);
}
else
{
quadNum = ec.upQuad();
coord = DgIVec2D(0, edgeI - coord.i());
}
}
else // i == edgeI
{
quadNum = ec.rightQuad();
coord.setI(0);
}
}
else // type 1
{
if (coord.i() == edgeI)
{
if (coord.j() == 0)
{
quadNum = ec.loneVert();
coord = DgIVec2D(0, 0);
}
else
{
quadNum = ec.rightQuad();
coord = DgIVec2D(edgeJ - coord.j(), 0);
}
}
else // j == edgeJ
{
quadNum = ec.upQuad();
coord.setJ(0);
}
}
}
DgQ2DICoord result(quadNum, coord);
return result;
} // DgQ2DICoord DgQ2DDtoIConverter::convertTypedAddress
////////////////////////////////////////////////////////////////////////////////
DgQ2DItoDConverter::DgQ2DItoDConverter (const DgRF<DgQ2DICoord, std::int64_t>& from,
const DgRF<DgQ2DDCoord, long double>& to)
: DgConverter<DgQ2DICoord, std::int64_t, DgQ2DDCoord, long double> (from, to),
pIDGG_ (NULL)
{
pIDGG_ = dynamic_cast<const DgIDGG*>(&fromFrame());
if (!pIDGG_)
{
report("DgQ2DItoDConverter::DgQ2DItoDConverter(): "
" fromFrame not of type DgIDGG", DgBase::Fatal);
}
} // DgQ2DItoDConverter::DgQ2DItoDConverter
////////////////////////////////////////////////////////////////////////////////
DgQ2DDCoord
DgQ2DItoDConverter::convertTypedAddress (const DgQ2DICoord& addIn) const
{
DgLocation* loc = IDGG().grid2D().makeLocation(addIn.coord());
IDGG().ccFrame().convert(loc);
DgDVec2D coord = *IDGG().ccFrame().getAddress(*loc);
delete loc;
DgQ2DDCoord result(addIn.quadNum(), coord);
return result;
} // DgQ2DDCoord DgQ2DItoDConverter::convertTypedAddress
////////////////////////////////////////////////////////////////////////////////
DgQ2DItoInterleaveConverter::DgQ2DItoInterleaveConverter
(const DgRF<DgQ2DICoord, std::int64_t>& from,
const DgRF<DgInterleaveCoord, std::int64_t>& to)
: DgConverter<DgQ2DICoord, std::int64_t, DgInterleaveCoord, std::int64_t> (from, to),
pIDGG_ (NULL)
{
pIDGG_ = dynamic_cast<const DgIDGG*>(&fromFrame());
if (!pIDGG_)
{
report("DgQ2DItoInterleaveConverter::DgQ2DItoInterleaveConverter(): "
" fromFrame not of type DgIDGG", DgBase::Fatal);
}
} // DgQ2DItoInterleaveConverter::DgQ2DItoInterleaveConverter
////////////////////////////////////////////////////////////////////////////////
DgInterleaveCoord
DgQ2DItoInterleaveConverter::convertTypedAddress
(const DgQ2DICoord& addIn) const
{
string qstr = dgg::util::to_string(addIn.quadNum(), 2);
int effRes = IDGG().res(); // effective resolution
int effRadix = IDGG().radix(); // effective radix
if (IDGG().aperture() == 3)
{
effRadix = 3;
effRes = (effRes + 1) / 2;
}
if (IDGG().gridTopo() == string("TRIANGLE")) effRes++; // adjust for long double j
//cout << "addIn " << addIn << endl;
DgRadixString rs1(effRadix, addIn.coord().i(), effRes);
DgRadixString rs2(effRadix, addIn.coord().j(), effRes);
//cout << "rs1 " << rs1 << endl;
//cout << "rs2 " << rs2 << endl;
string addstr = qstr;
if (IDGG().aperture() == 3)
{
if (IDGG().isClassI()) addstr = addstr + string("0");
else addstr = addstr + string("1");
}
addstr = addstr + DgRadixString::digitInterleave(rs1, rs2);
//cout << "addstr " << addstr << endl;
DgInterleaveCoord res;
res.setValString(addstr);
//cout << "res " << res << endl;
return res;
} // DgInterleaveCoord DgQ2DItoInterleaveConverter::convertTypedAddress
////////////////////////////////////////////////////////////////////////////////
DgVertex2DDToQ2DDConverter::DgVertex2DDToQ2DDConverter (
const DgRF<DgVertex2DDCoord, long double>& from,
const DgRF<DgQ2DDCoord, long double>& to)
: DgConverter<DgVertex2DDCoord, long double, DgQ2DDCoord, long double>(from, to)
{
} // DgVertex2DDToQ2DDConverter::DgVertex2DDToQ2DDConverter
////////////////////////////////////////////////////////////////////////////////
DgQ2DDCoord
DgVertex2DDToQ2DDConverter::convertTypedAddress
(const DgVertex2DDCoord& addIn) const
{
DgQ2DDCoord result(addIn.vertNum(), addIn.coord());
return result;
} // DgQ2DDCoord DgVertex2DDToQ2DDConverter::convertTypedAddress
////////////////////////////////////////////////////////////////////////////////
DgQ2DDtoVertex2DDConverter::DgQ2DDtoVertex2DDConverter
(const DgRF<DgQ2DDCoord, long double>& from,
const DgRF<DgVertex2DDCoord, long double>& to)
: DgConverter<DgQ2DDCoord, long double, DgVertex2DDCoord, long double>(from, to)
{
} // DgQ2DDtoVertex2DDConverter::DgQ2DDtoVertex2DDConverter
////////////////////////////////////////////////////////////////////////////////
// Private helper method to compute subtriangle region:
int
DgQ2DDtoVertex2DDConverter::compute_subtriangle
(const long double& x, const long double& y) const
{
const long double tol = 0.000000000000001L;
long double xs = M_SQRT3 * x;
long double xpp = xs + tol,
xmp = -xs + tol,
xpm = xs - tol,
xmm = -xs - tol;
if (y >= xmm && y > xpp)
{
return 0;
}
if ((fabs(y) <= tol && fabs(x) <= tol) ||
(y <= xpp && y >= (0.0 - tol)))
{
return 1;
}
if (y < (0.0 - tol) && y > xmp)
{
return 2;
}
if (y <= xmp && y < xpm)
{
return 3;
}
if (y >= xpm && y < (0.0 - tol))
{
return 4;
}
if (y >= (0.0 - tol) && y < xmm)
{
return 5;
}
report("Dg2DDtoVertex2DDConverter value out of hex", DgBase::Fatal);
return -1;
}
////////////////////////////////////////////////////////////////////////////////
DgVertex2DDCoord
DgQ2DDtoVertex2DDConverter::convertTypedAddress (const DgQ2DDCoord& addIn) const
{
int subTri = compute_subtriangle(addIn.coord().x(), addIn.coord().y());
const DgVertTriVals& st = DgVertex2DDRF::vertTable(addIn.quadNum(), subTri);
DgVertex2DDCoord newCoord(st.keep());
if (newCoord.keep())
{
newCoord.setVertNum(addIn.quadNum());
newCoord.setTriNum(subTri);
newCoord.setCoord(addIn.coord());
}
else
{
newCoord.setVertNum(-1);
newCoord.setTriNum(-1);
}
return newCoord;
} // DgVertex2DDCoord DgQ2DDtoVertex2DDConverter::convertTypedAddress
////////////////////////////////////////////////////////////////////////////////
DgVertex2DDCoord
DgProjTriToVertex2DD::convertTypedAddress (const DgProjTriCoord& addIn) const
{
const DgVertTriVals& triVals = DgVertex2DDRF::triTable(addIn.triNum());
DgVertex2DDCoord newCoord(true, triVals.quadNum(), addIn.triNum());
DgDVec2D tmpCoord(addIn.coord());
tmpCoord.rotate(triVals.rot60() * 60.0L);
tmpCoord -= triVals.trans();
newCoord.setCoord(tmpCoord);
return newCoord;
} // DgVertex2DDCoord DgProjTriToVertex2DD::convertTypedAddress
////////////////////////////////////////////////////////////////////////////////
DgProjTriCoord
DgVertex2DDtoProjTri::convertTypedAddress (const DgVertex2DDCoord& addIn) const
{
const DgVertTriVals& triVal =
DgVertex2DDRF::vertTable(addIn.vertNum(), addIn.triNum());
//const DgVertTriVals& triVals = DgVertex2DDRF::triTable(addIn.triNum());
DgDVec2D tmpCoord(addIn.coord());
tmpCoord += triVal.trans();
tmpCoord.rotate(triVal.rot60() * -60.0L);
DgProjTriCoord newCoord(triVal.triNum(), tmpCoord);
return newCoord;
} // DgProjTriCoord DgVertex2DDtoProjTri::convertTypedAddress
////////////////////////////////////////////////////////////////////////////////
DgPlaneTriProj::DgPlaneTriProj (const DgRF<DgProjTriCoord, long double>& from,
const DgRF<DgDVec2D, long double>& to)
: DgConverter<DgProjTriCoord, long double, DgDVec2D, long double>(from, to),
pPlaneTriRF_ (0)
{
pPlaneTriRF_= dynamic_cast<const DgPlaneTriRF*>(&toFrame());
if (!pPlaneTriRF_)
{
report("DgPlaneTriProj::DgPlaneTriProj(): "
" toFrame not of type DgPlaneTriRF", DgBase::Fatal);
}
} // DgPlaneTriProj::DgPlaneTriProj
////////////////////////////////////////////////////////////////////////////////
DgDVec2D
DgPlaneTriProj::convertTypedAddress (const DgProjTriCoord& addIn) const
{
//DgDVec2D scaledPt(addIn.coord().x() / snyedglen,
//addIn.coord().y() / snyedglen);
DgDVec2D newPt(addIn.coord());
planeTriRF().icosaMap().translate(addIn.triNum(), &newPt);
return newPt;
} // DgDVec2D DgPlaneTriProj::convertTypedAddress
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
DgIDGG::DgIDGG (const DgGeoSphRF& geoRF, const DgGeoCoord& vert0,
long double azDegs, unsigned int aperture, int res, const string& name,
const string& gridTopo, const string& projType, bool isMixed43,
int numAp4, bool isSuperfund, int sfRes, unsigned int precision)
:
DgDiscRF<DgQ2DICoord, DgGeoCoord, long double>(geoRF.network(), geoRF, name),
sphIcosa_(0), geoRF_(geoRF), vert0_(vert0), azDegs_(azDegs),
gridTopo_(gridTopo), projType_(projType), aperture_(aperture), res_(res),
isMixed43_(isMixed43), numAp4_(numAp4), isSuperfund_(isSuperfund),
sfRes_ (sfRes), precision_(precision), grid2D_(0), grid2DS_(0), ccFrame_(0),
projTriRF_(0), vertexRF_(0), q2ddRF_(0), bndRF_(0), intRF_(0), planeRF_(0)
{
initialize();
} // DgIDGG::DgIDGG
////////////////////////////////////////////////////////////////////////////////
DgIDGG::DgIDGG (const DgIDGG& rfIn)
: DgDiscRF<DgQ2DICoord, DgGeoCoord, long double> (rfIn),
sphIcosa_(0), geoRF_(rfIn.geoRF()), vert0_(rfIn.vert0()),
azDegs_(rfIn.azDegs()), gridTopo_(rfIn.gridTopo()),
projType_ (rfIn.projType()), aperture_(rfIn.aperture()),
res_(rfIn.res()), isMixed43_(rfIn.isMixed43()), numAp4_(rfIn.numAp4()),
isSuperfund_(rfIn.isSuperfund()), sfRes_(rfIn.sfRes()),
precision_(rfIn.precision()), grid2D_(0), grid2DS_(0), ccFrame_(0),
projTriRF_(0), vertexRF_(0), q2ddRF_(0), bndRF_(0), intRF_(0),
planeRF_(0)
{
initialize();
} // DgIDGG::DgIDGG
DgIDGG::~DgIDGG()
{
/* Notice that we do not delete:
grid2D_, grid2DS_
...this is because they are freed elsewhere (by DgHexGrid2DS.C), and a segfault
will result. It would be good to resolve the "ambiguity" -JFW */
delete sphIcosa_;
delete ccFrame_;
delete projTriRF_;
delete vertexRF_;
delete q2ddRF_;
delete bndRF_;
delete intRF_;
delete planeRF_;
}
////////////////////////////////////////////////////////////////////////////////
void
DgIDGG::initialize (void)
{
// verify parameter validity
string apErrStr = string("DgIDGG::initialize(): invalid aperture " +
dgg::util::to_string(aperture()) +
string(" for grid topo ") + gridTopo());
if (gridTopo() == "HEXAGON")
{
if (aperture() != 3 && aperture() != 4)
report(apErrStr, DgBase::Fatal);
}
else if (gridTopo() == "TRIANGLE" || gridTopo() == "DIAMOND")
{
if (aperture() != 4) report(apErrStr, DgBase::Fatal);
}
else
report("DgIDGG::initialize(): invalid grid topo '" + gridTopo() + "'",
DgBase::Fatal);
if (isMixed43())
aperture_ = (res() > numAp4()) ? 3 : 4;
// create some internal data structures
undefLoc_ = makeLocation(undefAddress());
sphIcosa_ = new DgSphIcosa(vert0(), azDegs());
// setup some parameters
isClassI_ = true; // default
radix_ = (int) sqrt((long double) aperture());
adjRes_ = res();
if (gridTopo() == "HEXAGON")
{
isAligned_ = true;
isCongruent_ = false;
if (!isMixed43())
{
if (aperture() == 4)
{
isClassI_ = true;
maxD_ = (std::int64_t) pow(2.0L, res()) - 1;
}
else // aperture 3
{
isClassI_ = !(res() % 2);
if (!isClassI()) adjRes_ = res() + 1;
maxD_ = (std::int64_t) pow(3.0L, (adjRes() / 2)) - 1;
}
}
else // mixed43
{
if (res() <= numAp4())
{
isClassI_ = true;
maxD_ = (std::int64_t) pow(2.0L, res()) - 1;
}
else
{
isClassI_ = !((res() - numAp4()) % 2);
if (!isClassI()) adjRes_ = res() + 1;
maxD_ = (std::int64_t) (pow(2.0L, numAp4()) *
pow(3.0L, ((adjRes() - numAp4()) / 2))) - 1;
}
// cout << "MAXD: " << maxD_ << endl;
}
maxI_ = maxD();
maxJ_ = maxD();
mag_ = maxD() + 1;
firstAdd_ = DgQ2DICoord(0, DgIVec2D(0, 0));
lastAdd_ = DgQ2DICoord(11, DgIVec2D(0, 0));
}
else
{
maxD_ = (std::int64_t) pow((long double) radix(), res()) - 1;
maxI_ = maxD();
maxJ_ = maxD();
mag_ = maxD() + 1;
if (gridTopo() == "TRIANGLE")
{
isAligned_ = true; // only for aperture 4
isCongruent_ = true;
maxJ_ = (mag() * 2) - 1;
}
else // topo is DIAMOND
{
isAligned_ = false;
isCongruent_ = true;
}
firstAdd_ = DgQ2DICoord(1, DgIVec2D(0, 0));
lastAdd_ = DgQ2DICoord(10, DgIVec2D(maxI(), maxJ()));
}
// set-up local network to scale so that quad (and consequently tri) edge
// length is 1.0
ccFrame_ = new DgContCartRF(locNet_, name() + "CC1");
if (gridTopo() == "DIAMOND")
{
grid2DS_ = new DgDmdD4Grid2DS(locNet_, ccFrame(), res() + 1, aperture(),
isCongruent(), isAligned());
}
else if (gridTopo() == "HEXAGON")
{
grid2DS_ = new DgHexGrid2DS(locNet_, ccFrame(), adjRes() + 1, aperture(),
isCongruent(), isAligned(), "H2DS", isMixed43(), numAp4(), isSuperfund());
}
else if (gridTopo() == "TRIANGLE")
{
grid2DS_ = new DgTriGrid2DS(locNet_, ccFrame(), res() + 1, aperture(),
isCongruent(), isAligned());
}
grid2D_ = dynamic_cast<DgDiscRF2D*>(grid2DS().grids()[res()]);
bndRF_ = new DgBoundedIDGG(*this);
// create the intermediate RFs
projTriRF_ = new DgProjTriRF(network(), name() + string("projTri"),
sphIcosa_);
vertexRF_ = new DgVertex2DDRF(network(), name() + string("vertex"));
q2ddRF_ = new DgQ2DDRF(network(), name() + string("q2dd"));
intRF_ = new DgInterleaveRF(network(), name() + string("int"));
planeRF_ = new DgPlaneTriRF(network(), name() + string("plane"));
// create the converters; for convenience use where they are in overall
// sequence for name
DgIcosaProj* icosaProj = NULL;
if (projType() == "ISEA")
icosaProj = new DgProjISEA(geoRF(), projTriRF());
else if (projType() == "FULLER")
icosaProj = new DgProjFuller(geoRF(), projTriRF());
else
report("DgIDGG::initialize(): invalid projection type " + projType(),
DgBase::Fatal);
const DgConverterBase* c1to2 = &(icosaProj->forward());
const DgConverterBase* c2to3 = new DgProjTriToVertex2DD(projTriRF(), vertexRF());
const DgConverterBase* c3to4 = new DgVertex2DDToQ2DDConverter(vertexRF(), q2ddRF());
const DgConverterBase* c4to5 = new DgQ2DDtoIConverter(q2ddRF(), *this);
const DgConverterBase* c5to4 = new DgQ2DItoDConverter(*this, q2ddRF());
const DgConverterBase* c4to3 = new DgQ2DDtoVertex2DDConverter(q2ddRF(), vertexRF());
const DgConverterBase* c3to2 = new DgVertex2DDtoProjTri(vertexRF(), projTriRF());
const DgConverterBase* c2to1 = &(icosaProj->inverse());
DgConverterBase* toInt = new DgQ2DItoInterleaveConverter(*this, intRF());
DgConverterBase* toPlane = new DgPlaneTriProj(projTriRF(), planeRF());
// create the series converters that will replace the default DgDiscRF
// converters
vector<const DgConverterBase*> sc;
sc.push_back(c1to2);
sc.push_back(c2to3);
sc.push_back(c3to4);
sc.push_back(c4to5);
new DgSeriesConverter(sc, true);
sc.resize(0);
sc.push_back(c5to4);
sc.push_back(c4to3);
sc.push_back(c3to2);
sc.push_back(c2to1);
new DgSeriesConverter(sc, true);
sc.resize(0);
//// now fill in the other connections; begin with from vertexRF and then
//// connect to it as needed
// vertexRF -> geoRF
sc.push_back(c3to2);
sc.push_back(c2to1);
new DgSeriesConverter(sc, true);
sc.resize(0);
// vertexRF -> Q2DI
sc.push_back(c3to4);
sc.push_back(c4to5);
new DgSeriesConverter(sc, true);
sc.resize(0);
// vertexRF -> projTriRF is c3to2 above
// vertexRF -> planeRF
sc.push_back(c3to2);
sc.push_back(toPlane);
new DgSeriesConverter(sc, true);
sc.resize(0);
// vertexRF -> Q2DD is c3to4 above
// vertexRF -> intRF
sc.push_back(c3to4);
sc.push_back(c4to5);
sc.push_back(toInt);
new DgSeriesConverter(sc, true);
sc.resize(0);
/// now do from projTriRF
// projTriRF -> geoRF is c2to1 above
// projTriRF -> Q2DI