-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathmexDinf.c
More file actions
3471 lines (2867 loc) · 116 KB
/
mexDinf.c
File metadata and controls
3471 lines (2867 loc) · 116 KB
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
/*
* =============================================================
* mexDinf.c - C MEX file to compute drainage area
*
* usage: [A, minima, flooded, W] = mexDinf(M,dy/dx,sinks,bvec,flood)
*
* Input arguments:
* M: a K x J matrix of elevations
* dy/dx: ratio of grid spacings in the y and x directions
* sinks: a K x J matrix of points where flow paths should terminate
* bvec: a vector of codes for the [left, right, upper, lower]
* boundary conditions (0=fixed, 1=mirror, 2=periodic)
* flood: a scalar. if 1, program will route flow through local
* minima in the grid that do not drain to a sink.
*
* Return arguments:
* A: a K x J matrix of total contributing areas in units of
* cells, calculated using the multi-slope algorithm.
* minima: a K x J matrix that is 1 where there are local minima or
* flat areas, zero otherwise.
* flooded:a matrix that is 1 where M was flooded, zero otherwise.
* W: a K x J x 8 array of flow routing weights
*
* Important notes:
* A must be multiplied by dx*dy to give the correct dimensions for
* the contributing area.
*
* Fixed boundaries do not contribute flow.
*
* This is a MEX-file for MATLAB.
* Copyright (c) 2006-2011 Taylor Perron
* =============================================================
*/
#include "mex.h"
#include "matrix.h"
#include <math.h>
#define FIXED 0
#define MIRROR 1
#define PERIODIC 2
#define INF 1.0E10
#define MAX_NEIGHBOR_BASINS 1000
typedef struct {
int nnb, lpp, inb[MAX_NEIGHBOR_BASINS], ipp[MAX_NEIGHBOR_BASINS]; // number of neighboring basins, list position of the lowest pp in this structure, index of neighboring basin, index of pp
double zpp[MAX_NEIGHBOR_BASINS]; // elevation of pp
} pourpoint;
// Calculate D8 (steepest descent) drainage directions
void D8Dir(const int K, const int J, double M[], double D[], double minima[], double minimaIdx[], int *numMin, double *a, double bdy[], double sinks[])
{
int i, j, k, Kj, theD;
int di[8], di0[] = {0,-1,-1,-1,0,1,1,1};
int dj[8], dj0[] = {1,1,0,-1,-1,-1,0,1};
int skip[]={0,0,0,0,0,0,0,0};
int bl, br, bu, bd;
double invdx, invdy, invdiag, theS;
double e, s;
double invd[8];
invdx=1;
invdy=1/(*a);
invdiag=1/sqrt(1+(*a)*(*a));
invd[0] = invdx;
invd[1] = invdiag;
invd[2] = invdy;
invd[3] = invdiag;
invd[4] = invdx;
invd[5] = invdiag;
invd[6] = invdy;
invd[7] = invdiag;
// left
if (bdy[0] == 0) {
bl=FIXED;
} else if (bdy[0] == 1) {
bl=MIRROR;
} else if (bdy[0] == 2) {
bl=PERIODIC;
}
// right
if (bdy[1] == 0) {
br=FIXED;
} else if (bdy[1] == 1) {
br=MIRROR;
} else if (bdy[1] == 2) {
br=PERIODIC;
}
// upper
if (bdy[2] == 0) {
bu=FIXED;
} else if (bdy[2] == 1) {
bu=MIRROR;
} else if (bdy[2] == 2) {
bu=PERIODIC;
}
// lower
if (bdy[3] == 0) {
bd=FIXED;
} else if (bdy[3] == 1) {
bd=MIRROR;
} else if (bdy[3] == 2) {
bd=PERIODIC;
}
/* INTERIOR POINTS */
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (j=1; j<(J-1); j++) { // Loop through columns
Kj=K*j;
for (i=1; i<(K-1); i++) { // Loop through rows
if (!sinks[Kj+i]) { /* weights of sinks (which includes fixed boundaries) remain zero. Also, sinks and fixed boundaries are not defined as minima */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k]; // Could make this faster by only calculating slope if it's a downslope point
if (s>theS) {
theS=s;
D[Kj+i]=k+1; // could make this faster by just recording k-steepest and only assigning D at the end
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
}
}
/* BOUNDARIES, EXCLUDING CORNERS */
/* left */
j = 0;
Kj=K*j;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (bl) {
case FIXED: // fixed left
case MIRROR: // mirror left
skip[3]=1;
skip[4]=1;
skip[5]=1;
break;
case PERIODIC: // periodic left
dj[3]=J-1;
dj[4]=J-1;
dj[5]=J-1;
break;
}
for (i=1; i<(K-1); i++) {
if (!sinks[Kj+i]) { /* weights of sinks remain zero */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
if (!skip[k]) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k];
if (s>theS) {
theS=s;
D[Kj+i]=k+1;
}
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
}
/* right */
j = J-1;
Kj=K*j;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (br) {
case FIXED: // fixed right
case MIRROR: // mirror right
skip[7]=1;
skip[0]=1;
skip[1]=1;
break;
case PERIODIC: // periodic right
dj[7]=1-J;
dj[0]=1-J;
dj[1]=1-J;
break;
}
for (i=1; i<(K-1); i++) {
if (!sinks[Kj+i]) { /* weights of sinks remain zero */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
if (!skip[k]) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k];
if (s>theS) {
theS=s;
D[Kj+i]=k+1;
}
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
}
/* upper */
i = 0;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (bu) {
case FIXED: // fixed upper
case MIRROR: // mirror upper
skip[1]=1;
skip[2]=1;
skip[3]=1;
break;
case PERIODIC: // periodic upper
di[1]=K-1;
di[2]=K-1;
di[3]=K-1;
break;
}
for (j=1; j<(J-1); j++) {
Kj = K*j;
if (!sinks[Kj+i]) { /* weights of sinks remain zero */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
if (!skip[k]) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k];
if (s>theS) {
theS=s;
D[Kj+i]=k+1;
}
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
}
/* lower */
i = K-1;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (bd) {
case FIXED: // fixed lower
case MIRROR: // mirror lower
skip[5]=1;
skip[6]=1;
skip[7]=1;
break;
case PERIODIC: // periodic lower
di[5]=1-K;
di[6]=1-K;
di[7]=1-K;
break;
}
for (j=1; j<(J-1); j++) {
Kj = K*j;
if (!sinks[Kj+i]) { /* weights of sinks remain zero */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
if (!skip[k]) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k];
if (s>theS) {
theS=s;
D[Kj+i]=k+1;
}
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
}
/* CORNERS */
/* UL */
i=0;
j=0;
Kj=K*j;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (bl) {
case FIXED: // fixed left
case MIRROR: // mirror left
skip[3]=1;
skip[4]=1;
skip[5]=1;
break;
case PERIODIC: // periodic left
dj[3]=J-1;
dj[4]=J-1;
dj[5]=J-1;
break;
}
switch (bu) {
case FIXED: // fixed upper
case MIRROR: // mirror upper
skip[1]=1;
skip[2]=1;
skip[3]=1;
break;
case PERIODIC: // periodic upper
di[1]=K-1;
di[2]=K-1;
di[3]=K-1;
break;
}
if (!sinks[Kj+i]) { /* weights of sinks remain zero */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
if (!skip[k]) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k];
if (s>theS) {
theS=s;
D[Kj+i]=k+1;
}
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
/* UR */
i=0;
j=J-1;
Kj=K*j;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (br) {
case FIXED: // fixed right
case MIRROR: // mirror right
skip[7]=1;
skip[0]=1;
skip[1]=1;
break;
case PERIODIC: // periodic right
dj[7]=1-J;
dj[0]=1-J;
dj[1]=1-J;
break;
}
switch (bu) {
case FIXED: // fixed upper
case MIRROR: // mirror upper
skip[1]=1;
skip[2]=1;
skip[3]=1;
break;
case PERIODIC: // periodic upper
di[1]=K-1;
di[2]=K-1;
di[3]=K-1;
break;
}
if (!sinks[Kj+i]) { /* weights of sinks remain zero */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
if (!skip[k]) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k];
if (s>theS) {
theS=s;
D[Kj+i]=k+1;
}
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
/* LL */
i=K-1;
j=0;
Kj=K*j;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (bl) {
case FIXED: // fixed left
case MIRROR: // mirror left
skip[3]=1;
skip[4]=1;
skip[5]=1;
break;
case PERIODIC: // periodic left
dj[3]=J-1;
dj[4]=J-1;
dj[5]=J-1;
break;
}
switch (bd) {
case FIXED: // fixed lower
case MIRROR: // mirror lower
skip[5]=1;
skip[6]=1;
skip[7]=1;
break;
case PERIODIC: // periodic lower
di[5]=1-K;
di[6]=1-K;
di[7]=1-K;
break;
}
if (!sinks[Kj+i]) { /* weights of sinks remain zero */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
if (!skip[k]) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k];
if (s>theS) {
theS=s;
D[Kj+i]=k+1;
}
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
/* LR */
i=K-1;
j=J-1;
Kj=K*j;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (br) {
case FIXED: // fixed right
case MIRROR: // mirror right
skip[7]=1;
skip[0]=1;
skip[1]=1;
break;
case PERIODIC: // periodic right
dj[7]=1-J;
dj[0]=1-J;
dj[1]=1-J;
break;
}
switch (bd) {
case FIXED: // fixed lower
case MIRROR: // mirror lower
skip[5]=1;
skip[6]=1;
skip[7]=1;
break;
case PERIODIC: // periodic lower
di[5]=1-K;
di[6]=1-K;
di[7]=1-K;
break;
}
if (!sinks[Kj+i]) { /* weights of sinks remain zero */
e=M[Kj+i];
theS=0;
for (k=0; k<8; k++) {
if (!skip[k]) {
s = (e - M[K*(j+dj[k])+(i+di[k])])*invd[k];
if (s>theS) {
theS=s;
D[Kj+i]=k+1;
}
}
}
if (!theS) {
minima[Kj+i] = *numMin;
minimaIdx[*numMin] = Kj+i; // Add this location to list of indices of minima
(*numMin)++;
}
}
} /* end D8Dir function */
void GetBasin(const int i, const int j, const int label, const int K, const int J, double D[], double Basin[], double bdy[])
{
int k, p, q, bl, br, bu, bd;
int r[] = {5,6,7,8,1,2,3,4};
int di[] = {0,-1,-1,-1,0,1,1,1};
int dj[] = {1,1,0,-1,-1,-1,0,1};
int skip[] = {0,0,0,0,0,0,0,0};
if (Basin[K*j+i]==label) {return;} // Bail out if this cell is already part of the basin
Basin[K*j+i]=label; // Now it is part of the basin
// left
if (bdy[0] == 0) {
bl=FIXED;
} else if (bdy[0] == 1) {
bl=MIRROR;
} else if (bdy[0] == 2) {
bl=PERIODIC;
}
// right
if (bdy[1] == 0) {
br=FIXED;
} else if (bdy[1] == 1) {
br=MIRROR;
} else if (bdy[1] == 2) {
br=PERIODIC;
}
// upper
if (bdy[2] == 0) {
bu=FIXED;
} else if (bdy[2] == 1) {
bu=MIRROR;
} else if (bdy[2] == 2) {
bu=PERIODIC;
}
// lower
if (bdy[3] == 0) {
bd=FIXED;
} else if (bdy[3] == 1) {
bd=MIRROR;
} else if (bdy[3] == 2) {
bd=PERIODIC;
}
if (i==0) {
switch (bu) {
case FIXED: // fixed upper
case MIRROR: // mirror upper
skip[1]=1;
skip[2]=1;
skip[3]=1;
break;
case PERIODIC: // periodic upper
di[1]=K-1;
di[2]=K-1;
di[3]=K-1;
break;
}
} else if (i==K-1) {
switch (bd) {
case FIXED: // fixed lower
case MIRROR: // mirror lower
skip[5]=1;
skip[6]=1;
skip[7]=1;
break;
case PERIODIC: // periodic lower
di[5]=1-K;
di[6]=1-K;
di[7]=1-K;
break;
}
}
if (j==0) {
switch (bl) {
case FIXED: // fixed left
case MIRROR: // mirror left
skip[3]=1;
skip[4]=1;
skip[5]=1;
break;
case PERIODIC: // periodic left
dj[3]=J-1;
dj[4]=J-1;
dj[5]=J-1;
break;
}
} else if (j==J-1) {
switch (br) {
case FIXED: // fixed right
case MIRROR: // mirror right
skip[7]=1;
skip[0]=1;
skip[1]=1;
break;
case PERIODIC: // periodic right
dj[7]=1-J;
dj[0]=1-J;
dj[1]=1-J;
break;
}
}
/* loop through neighbors */
for (k=0; k<8; k++) {
if (!skip[k]) {
p = i + di[k];
q = j + dj[k];
if (D[K*q+p] == r[k]) { // if neighbor k drains to current cell
GetBasin(p,q,label,K,J,D,Basin,bdy);
}
}
}
} // End GetBasin
void Repaint(const int i, const int j, const int nold, const int nnew, const int K, const int J, double Basin[], double bdy[])
{
int k, p, q, bl, br, bu, bd;
int r[] = {5,6,7,8,1,2,3,4};
int di[] = {0,-1,-1,-1,0,1,1,1};
int dj[] = {1,1,0,-1,-1,-1,0,1};
int skip[] = {0,0,0,0,0,0,0,0};
Basin[K*j+i] = nnew; // Now it is part of the new basin
// left
if (bdy[0] == 0) {
bl=FIXED;
} else if (bdy[0] == 1) {
bl=MIRROR;
} else if (bdy[0] == 2) {
bl=PERIODIC;
}
// right
if (bdy[1] == 0) {
br=FIXED;
} else if (bdy[1] == 1) {
br=MIRROR;
} else if (bdy[1] == 2) {
br=PERIODIC;
}
// upper
if (bdy[2] == 0) {
bu=FIXED;
} else if (bdy[2] == 1) {
bu=MIRROR;
} else if (bdy[2] == 2) {
bu=PERIODIC;
}
// lower
if (bdy[3] == 0) {
bd=FIXED;
} else if (bdy[3] == 1) {
bd=MIRROR;
} else if (bdy[3] == 2) {
bd=PERIODIC;
}
if (i==0) {
switch (bu) {
case FIXED: // fixed upper
case MIRROR: // mirror upper
skip[1]=1;
skip[2]=1;
skip[3]=1;
break;
case PERIODIC: // periodic upper
di[1]=K-1;
di[2]=K-1;
di[3]=K-1;
break;
}
} else if (i==K-1) {
switch (bd) {
case FIXED: // fixed lower
case MIRROR: // mirror lower
skip[5]=1;
skip[6]=1;
skip[7]=1;
break;
case PERIODIC: // periodic lower
di[5]=1-K;
di[6]=1-K;
di[7]=1-K;
break;
}
}
if (j==0) {
switch (bl) {
case FIXED: // fixed left
case MIRROR: // mirror left
skip[3]=1;
skip[4]=1;
skip[5]=1;
break;
case PERIODIC: // periodic left
dj[3]=J-1;
dj[4]=J-1;
dj[5]=J-1;
break;
}
} else if (j==J-1) {
switch (br) {
case FIXED: // fixed right
case MIRROR: // mirror right
skip[7]=1;
skip[0]=1;
skip[1]=1;
break;
case PERIODIC: // periodic right
dj[7]=1-J;
dj[0]=1-J;
dj[1]=1-J;
break;
}
}
/* loop through neighbors */
for (k=0; k<8; k++) {
if (!skip[k]) {
p = i + di[k];
q = j + dj[k];
if (Basin[K*q+p] == nold) { // if neighbor k is part of the old basin
Repaint(p,q,nold,nnew,K,J,Basin,bdy);
}
}
}
} // End Repaint
void MemCheck(pourpoint pp[], int n) {
if (pp[n].nnb > MAX_NEIGHBOR_BASINS) {
mexErrMsgTxt("Memory overrun in mexDms. Try increasing MAX_NEIGHBOR_BASINS and recompiling, or using a smaller grid with fewer local minima.");
}
} // End MemCheck
void GetPourPoints(pourpoint pp[],const int K, const int J, double M[], double Basin[], double minima[], double minimaIdx[], int *numMin, double sinks[], double bdy[])
{
int i, j, k, Kj, p, q, c, bl, br, bu, bd;
int bas, nbas, ncand, midx, nmidx, lowestppyet, ppexists, pploc;
int di[8], di0[] = {0,-1,-1,-1,0,1,1,1};
int dj[8], dj0[] = {1,1,0,-1,-1,-1,0,1};
int skip[]={0,0,0,0,0,0,0,0};
// left
if (bdy[0] == 0) {
bl=FIXED;
} else if (bdy[0] == 1) {
bl=MIRROR;
} else if (bdy[0] == 2) {
bl=PERIODIC;
}
// right
if (bdy[1] == 0) {
br=FIXED;
} else if (bdy[1] == 1) {
br=MIRROR;
} else if (bdy[1] == 2) {
br=PERIODIC;
}
// upper
if (bdy[2] == 0) {
bu=FIXED;
} else if (bdy[2] == 1) {
bu=MIRROR;
} else if (bdy[2] == 2) {
bu=PERIODIC;
}
// lower
if (bdy[3] == 0) {
bd=FIXED;
} else if (bdy[3] == 1) {
bd=MIRROR;
} else if (bdy[3] == 2) {
bd=PERIODIC;
}
/* INTERIOR POINTS */
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (j=1; j<(J-1); j++) { // Loop through columns
Kj=K*j;
for (i=1; i<(K-1); i++) { // Loop through rows
bas = (int)Basin[K*j+i]; // the basin that this point drains to, which is also the index of the point that it ultimately drains to
if (!sinks[bas]) { // only process points in closed basins
for (k=0; k<8; k++) { // loop through neighbors
p = i + di[k];
q = j + dj[k];
nbas = (int)Basin[K*q+p];
if (nbas != bas) { // if the neighbor is part of a different basin
if (M[K*q+p] > M[K*j+i]) { // if the neighbor has higher elevation
ncand = K*q+p; // the neighbor is a candidate pour point
} else { // the neighbor has equal or lower elevation
ncand = K*j+i; // the present point is a candidate pour point
}
// compare the candidate point to existing PP to this neighbor basin, if it exists. If it's lower or there isn't one yet, make it the PP to this neighbor
midx = (int)minima[bas]; // the list position of the minimum that this basin drains to
lowestppyet = 0;
ppexists = 0;
for (c=0; c<pp[midx].nnb; c++) { // the loop shouldn't even execute once if there are no pp's yet for this basin (pp[midx].nnb == 0)
if (pp[midx].inb[c] == nbas) { // if this is an existing entry for the neighboring basin in question
ppexists = 1;
pploc = c;
if (M[ncand] < pp[midx].zpp[c]) { // if the candidate PP is lower than the existing one
lowestppyet = 1;
}
break;
}
}
if (!ppexists) { // if no pp exists yet for this pair of basins, record the candidate as the pp
pp[midx].inb[pp[midx].nnb] = nbas;
pp[midx].ipp[pp[midx].nnb] = ncand;
pp[midx].zpp[pp[midx].nnb] = M[ncand];
(pp[midx].nnb)++; MemCheck(pp,midx);
// also record this as the PP from the neighbor basin to this one, provided the neighbor isn't a sink basin
if (!sinks[nbas]) {
nmidx = (int)minima[nbas];
pp[nmidx].inb[pp[nmidx].nnb] = bas;
pp[nmidx].ipp[pp[nmidx].nnb] = ncand;
pp[nmidx].zpp[pp[nmidx].nnb] = M[ncand];
(pp[nmidx].nnb)++; MemCheck(pp,nmidx);
}
} else if (lowestppyet) { // if there was an existing PP, but the candidate is lower, replace the existing one
pp[midx].inb[pploc] = nbas;
pp[midx].ipp[pploc] = ncand;
pp[midx].zpp[pploc] = M[ncand];
// also record this as the PP from the neighbor basin to this one, provided the neighbor isn't a sink basin
if (!sinks[nbas]) {
nmidx = (int)minima[nbas];
// search for the position of the existing PP in the neighbor basin's list
c = 0;
while (pp[nmidx].inb[c] != bas) { c++; }
pp[nmidx].inb[c] = bas;
pp[nmidx].ipp[c] = ncand;
pp[nmidx].zpp[c] = M[ncand];
}
} // otherwise, there was an existing pour point that was lower than the candidate; do nothing
}
}
}
}
}
/* BOUNDARIES, EXCLUDING CORNERS */
/* left */
j = 0;
Kj=K*j;
for (k=0; k<8; k++) {di[k]=di0[k];}
for (k=0; k<8; k++) {dj[k]=dj0[k];}
for (k=0; k<8; k++) {skip[k]=0;}
switch (bl) {
case FIXED: // fixed left
case MIRROR: // mirror left
skip[3]=1;
skip[4]=1;
skip[5]=1;
break;
case PERIODIC: // periodic left
dj[3]=J-1;
dj[4]=J-1;