-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspread.c
2110 lines (1873 loc) · 74 KB
/
spread.c
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
/*******************************************************************************
MODULE: spread.c
CONTAINING SYSTEM: SLEUTH-3r (based on SLEUTH Model 3.0 Beta)
(Slope, Land-cover, Exclusion, Urbanization,
Transportation, and Hillshade)
also known as UGM 3.0 Beta (for Urban Growth Model)
VERSION: SLEUTH-3r [Includes Version D features]
REVISION DATE: August 31, 2006a
[Annotations added August 19, 2009]
PURPOSE:
This module models the spread of urbanization for a particular
year within a particular iteration (Monte Carlo run) for a
particular combination of input coefficients.
NOTES:
MODIFICATIONS:
TO DO:
**************************************************************************/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include "igrid_obj.h"
#include "pgrid_obj.h"
#include "landclass_obj.h"
#include "globals.h"
#include "random.h"
#include "utilities.h"
#include "memory_obj.h"
#include "igrid_obj.h"
#include "ugm_macros.h"
#include "coeff_obj.h"
#include "timer_obj.h"
#include "proc_obj.h"
#include "scenario_obj.h"
#include "stats_obj.h"
/*VerD*/
extern float aux_diffusion_coeff;
extern float aux_breed_coeff;
extern float aux_diffusion_mult;
float road_growth_breed_coefficient;
float road_growth_diffusion_coefficient;
/*VerD*/
/*****************************************************************************\
*******************************************************************************
** **
** MACROS **
** **
*******************************************************************************
\*****************************************************************************/
#define SPREAD_MODULE
#define SWGHT_TYPE float
#define SLOPE_WEIGHT_ARRAY_SZ 256
/*** D.D. July 28, 2006 (Begin) ***/
#define WCOL(rr,kk) rpocol_ptr[rporow_ptrIdx[rr]+kk]
#define MINCOL(rr) rpocol_ptr[rporow_ptrMin[rr]]
#define MAXCOL(rr) rpocol_ptr[rporow_ptrIdx[rr]+rporow_ptrNum[rr]-1]
/******************* D.D. July 28, 2006 (End) ******************/
/*****************************************************************************\
*******************************************************************************
** **
** STATIC MEMORY FOR THIS OBJECT **
** **
*******************************************************************************
\*****************************************************************************/
static int int_road_gravity;
/** D. Donato 08/18/2006 Variable for checking whether to initialize delta.***
Commented out 8/29/2006
static GRID_P delta_previous;
*** D. Donato 08/18/2006 Variable for checking whether to initialize delta.**/
/*** D.D. July 28, 2006 (Begin) **/
/*** "int *" changed to "short *" 8/10/2006 **/
/*** "short *" corrected back to "int *" for rporow_ptrIdx" 8/14/2006 **/
static short *rporow_ptrNum;
static short *rporow_ptrMin;
static short *rporow_ptrMax;
static int *rporow_ptrIdx;
static short *rpocol_ptr;
static int tfoundN, tfoundRow, tfoundCol;
static int foundN, foundRow, foundCol;
/******************* D.D. July 28, 2006 (End) ******************/
/*** D. Donato Aug. 14, 2006 Moved to module level from spr_phase5 **/
static int growth_count;
static short *growth_row;
static short *growth_col;
static int road_expansion_count;
static int *road_expansion_row;
static int *road_expansion_col;
/* The following two lines were replaced by D. Donato on 06/21/2006
**** D. Donato Aug. 14, 2006 **/
/** D. Donato 8/17/2006 Added to deal with cumulative growth **/
static short *zgrwth_row;
static short *zgrwth_col;
static int zgrwth_count;
/** D. Donato 8/17/2006 Added to deal with cumulative growth **/
/*****************************************************************************\
*******************************************************************************
** **
** STATIC FUNCTION PROTOTYPES **
** **
*******************************************************************************
\*****************************************************************************/
static void
spr_LogSlopeWeights (FILE * fp, int array_size, SWGHT_TYPE * lut);
static void
spr_phase1n3 (COEFF_TYPE diffusion_coefficient, /* IN */
COEFF_TYPE breed_coefficient, /* IN */
GRID_P z, /* IN */
GRID_P delta, /* IN/OUT */
GRID_P slp, /* IN */
GRID_P excld, /* IN */
SWGHT_TYPE * swght, /* IN */
int *sng, /* IN/OUT */
int *sdc); /* IN/OUT */
static void
spr_phase4 (COEFF_TYPE spread_coefficient, /* IN */
GRID_P z, /* IN */
GRID_P excld, /* IN */
GRID_P delta, /* IN/OUT */
GRID_P slp, /* IN */
SWGHT_TYPE * swght, /* IN */
int *og); /* IN/OUT */
static void
spr_phase5 (COEFF_TYPE road_gravity, /* IN */
COEFF_TYPE diffusion_coefficient, /* IN */
COEFF_TYPE breed_coefficient, /* IN */
GRID_P z, /* IN */
GRID_P delta, /* IN/OUT */
GRID_P slp, /* IN */
GRID_P excld, /* IN */
GRID_P roads, /* IN */
SWGHT_TYPE * swght, /* IN */
int *rt); /* IN/OUT */
/* D.D. Changed July 24, 2006 - No longer passing workspace
int *rt, ** IN/OUT **
GRID_P workspace); ** MOD **
*/
static void
spr_get_slp_weights (int array_size, /* IN */
SWGHT_TYPE * lut); /* OUT */
static BOOLEAN spr_road_search (short i_grwth_center, /* IN */
short j_grwth_center, /* IN */
int *i_road, /* OUT */
int *j_road, /* OUT */
int max_search_index, /* IN */
GRID_P roads); /* IN */
static
BOOLEAN spr_road_walk (int i_road_start, /* IN */
int j_road_start, /* IN */
int *i_road_end, /* OUT */
int *j_road_end, /* OUT */
GRID_P roads, /* IN */
double diffusion_coefficient); /* IN */
static
BOOLEAN spr_urbanize_nghbr (int i, /* IN */
int j, /* IN */
int *i_nghbr, /* OUT */
int *j_nghbr, /* OUT */
GRID_P z, /* IN */
GRID_P delta, /* IN */
GRID_P slp, /* IN */
GRID_P excld, /* IN */
SWGHT_TYPE * swght, /* IN */
PIXEL pixel_value, /* IN */
int *stat); /* OUT */
static
void spr_get_neighbor (int i_in, /* IN */
int j_in, /* IN */
int *i_out, /* OUT */
int *j_out); /* OUT */
static BOOLEAN
spr_urbanize (int row, /* IN */
int col, /* IN */
GRID_P z, /* IN */
GRID_P delta, /* IN */
GRID_P slp, /* IN */
GRID_P excld, /* IN */
SWGHT_TYPE * swght, /* IN */
PIXEL pixel_value, /* IN */
int *stat); /* OUT */
static COEFF_TYPE
spr_GetDiffusionValue (COEFF_TYPE diffusion_coeff); /* IN */
static COEFF_TYPE
spr_GetRoadGravValue (COEFF_TYPE rg_coeff); /* IN */
/*** D.D. July 28, 2006 (Begin) **/
int max( int, int );
/******************* D.D. July 28, 2006 (End) ******************/
/*****************************************************************************\
*******************************************************************************
** **
** SCCS ID **
** **
*******************************************************************************
\*****************************************************************************/
char spread_c_sccs_id[] = "@(#)spread.c 1.427 12/4/00";
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_phase1n3
** PURPOSE: perform phase 1 & 3 growth types
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
spr_phase1n3 (COEFF_TYPE diffusion_coefficient, /* IN */
COEFF_TYPE breed_coefficient, /* IN */
GRID_P z, /* IN */
GRID_P delta, /* IN/OUT */
GRID_P slp, /* IN */
GRID_P excld, /* IN */
SWGHT_TYPE * swght, /* IN */
int *sng, /* IN/OUT */
int *sdc) /* IN/OUT */
{
char func[] = "spr_phase1n3";
int i;
int j;
int i_out;
int j_out;
int k;
int count;
int tries;
int max_tries;
COEFF_TYPE diffusion_value;
BOOLEAN urbanized;
FUNC_INIT;
assert (MIN_DIFFUSION_VALUE <= diffusion_coefficient);
assert (diffusion_coefficient <= MAX_DIFFUSION_VALUE);
assert (MIN_BREED_VALUE <= breed_coefficient);
assert (breed_coefficient <= MAX_BREED_VALUE);
assert (z != NULL);
assert (delta != NULL);
assert (slp != NULL);
assert (excld != NULL);
assert (swght != NULL);
assert (sng != NULL);
assert (sdc != NULL);
diffusion_value = spr_GetDiffusionValue (diffusion_coefficient);
for (k = 0; k < 1 + (int) diffusion_value; k++)
{
i = RANDOM_ROW;
j = RANDOM_COL;
if (INTERIOR_PT (i, j))
{
if (spr_urbanize (i, /* IN */
j, /* IN */
z, /* IN */
delta, /* IN/OUT */
slp, /* IN */
excld, /* IN */
swght, /* IN */
PHASE1G, /* IN */
sng)) /* IN/OUT */
{
if (RANDOM_INT (101) < (int) breed_coefficient)
{
count = 0;
max_tries = 8;
for (tries = 0; tries < max_tries; tries++)
{
urbanized = FALSE;
urbanized =
spr_urbanize_nghbr (i, /* IN */
j, /* IN */
&i_out, /* OUT */
&j_out, /* OUT */
z, /* IN */
delta, /* IN/OUT */
slp, /* IN */
excld, /* IN */
swght, /* IN */
PHASE3G, /* IN */
sdc); /* IN/OUT */
if (urbanized)
{
count++;
if (count == MIN_NGHBR_TO_SPREAD)
{
break;
}
}
}
}
}
}
}
FUNC_END;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_phase4
** PURPOSE: perform phase 4 growth
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
spr_phase4 (COEFF_TYPE spread_coefficient, /* IN */
GRID_P z, /* IN */
GRID_P excld, /* IN */
GRID_P delta, /* IN/OUT */
GRID_P slp, /* IN */
SWGHT_TYPE * swght, /* IN */
int *og) /* IN/OUT */
{
char func[] = "spr_phase4";
/* D.D. 08/18/2006 */
int i;
/* D.D. 08/18/2006 */
int row;
int col;
int row_nghbr;
int col_nghbr;
int pixel;
int walkabout_row[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int walkabout_col[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int urb_count;
int nrows;
int ncols;
FUNC_INIT;
assert (z != NULL);
assert (excld != NULL);
assert (delta != NULL);
assert (slp != NULL);
assert (swght != NULL);
assert (og != NULL);
nrows = igrid_GetNumRows ();
ncols = igrid_GetNumCols ();
assert (nrows > 0);
assert (ncols > 0);
/*
*
* LOOP OVER THE INTERIOR PIXELS LOOKING FOR URBAN FROM WHICH
* TO PERFORM ORGANIC GROWTH
*
*/
/** D. Donato 08/18/2006 Use the zgrwth arrays to loop over interior points **
*** more efficiently. **
for (row = 1; row < nrows - 1; row++)
{
for (col = 1; col < ncols - 1; col++)
{
*** */
for (int i=0; i< zgrwth_count; i++)
{
row = zgrwth_row[i];
col = zgrwth_col[i];
/*
*
* D.D. 8/18/2006 -- IS THIS AN INTERIOR PIXEL?
*
*/
if (row < 1 || row >= nrows - 1) continue;
if (col < 1 || col >= ncols - 1) continue;
/*
*
* IS THIS AN URBAN PIXEL AND DO WE PASS THE RANDOM
* SPREAD COEFFICIENT TEST
*
*/
if ((z[OFFSET (row, col)] > 0) &&
(RANDOM_INT (101) < spread_coefficient))
{
/*
* EXAMINE THE EIGHT CELL NEIGHBORS
* SPREAD AT RANDOM IF AT LEAST TWO ARE URBAN
* PIXEL ITSELF MUST BE URBAN (3)
*
*/
urb_count = util_count_neighbors (z, row, col, GT, 0);
if ((urb_count >= 2) && (urb_count < 8))
{
pixel = RANDOM_INT (8);
row_nghbr = row + walkabout_row[pixel];
col_nghbr = col + walkabout_col[pixel];
spr_urbanize (row_nghbr, /* IN */
col_nghbr, /* IN */
z, /* IN */
delta, /* IN/OUT */
slp, /* IN */
excld, /* IN */
swght, /* IN */
PHASE4G, /* IN */
og); /* IN/OUT */
}
}
}
/* D.D. 08/18/2006 **
}
** D.D. 08/18/2006 */
FUNC_END;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_compute_line
** PURPOSE: Returns pixel coordinates drawing a line between input coordinates
** AUTHOR: Irenee Dubourg
** PROGRAMMER: Irenee Dubourg, ESTP Institut de Recherche en Constructibilite
** CREATION DATE: 06/07/2022
** DESCRIPTION:
**
**
*/
static void
spr_compute_line( int pointA_row, /* IN */
int pointA_col, /* IN */
int pointB_row, /* IN */
int pointB_col, /* IN */
int *lineRow, /* OUT */
int *lineCol, /* OUT */
int *pixel_count /* OUT */ )
{
char func[] = "spr_compute_line";
FUNC_INIT;
int x0, x1, y0, y1;
x0 = pointA_row;
y0 = pointA_col;
x1 = pointB_row;
y1 = pointB_col;
(*pixel_count) = 0;
// from http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#C
int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
int dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
int err = (dx > dy ? dx : -dy) / 2, e2;
for (int i=0; TRUE; i++) {
lineRow[i] = x0;
lineCol[i] = y0;
(*pixel_count) = i+1;
if (x0 == x1 && y0 == y1) break;
e2 = err;
if (e2 > -dx) { err -= dy; x0 += sx; }
if (e2 < dy) { err += dx; y0 += sy; }
}
FUNC_END;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_build_new_road
** PURPOSE: creates a new road between two cells
** AUTHOR: Irenee Dubourg
** PROGRAMMER: Irenee Dubourg, ESTP Institut de Recherche en Constructibilite
** CREATION DATE: 05/23/2022
** DESCRIPTION:
**
**
*/
static void
spr_build_new_road(GRID_P road_ptr, int cellRow, int cellCol, int roadRow, int roadCol) {
char func[] = "spr_build_new_road";
FUNC_INIT;
// pick a random road value between half and full existing value
PIXEL road_value = road_ptr[OFFSET(roadRow, roadCol)];
//PIXEL road_value = 100;
int roadStatePixelCount;
int *roadLineRowsPtr;
int *roadLineColsPtr;
int roadLinePixelCount;
roadLinePixelCount = 0;
roadLineRowsPtr = mem_GetroadLineRowsPtr();
roadLineColsPtr = mem_GetroadLineColsPtr();
spr_compute_line(cellRow, cellCol, roadRow, roadCol, roadLineRowsPtr, roadLineColsPtr, &roadLinePixelCount);
roadStatePixelCount = pgrid_GetRoadStatePixelCount();
for (int i=0; i< roadLinePixelCount; i++) {
int x = roadLineRowsPtr[i];
int y = roadLineColsPtr[i];
road_ptr[OFFSET(x, y)] = road_value;
road_expansion_row[road_expansion_count] = x;
road_expansion_col[road_expansion_count] = y;
road_expansion_count++;
}
pgrid_SetRoadStatePixelCount(roadStatePixelCount + roadLinePixelCount);
FUNC_END;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_isInConnectionRange
** PURPOSE: returns true if the given point corresponds to connection range based on newly created road segment
** AUTHOR: Irenee Dubourg
** PROGRAMMER: Irenee Dubourg, ESTP Institut de Recherche en Constructibilite
** CREATION DATE: 06/07/2022
** DESCRIPTION:
**
**
*/
static BOOLEAN spr_isInConnectionRange( int cellRow, int cellCol, int roadRow, int roadCol,
int connection_row, int connection_col) {
int vX = cellRow - roadRow;
int vY = cellCol - roadCol;
int wX = connection_row - cellRow;
int wY = connection_col - cellCol;
int dotProduct = vX * wX + vY * wY;
return (dotProduct >= 0);
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_max_norm()
** PURPOSE: returns the max norm of the vector defined by given integer coordinates
** AUTHOR: Irenee Dubourg
** PROGRAMMER: Irenee Dubourg, ESTP Institut de Recherche en Constructibilite
** CREATION DATE: 06/02/2022
** DESCRIPTION:
**
**
*/
int
spr_max_norm(int x0, int y0, int x1, int y1)
{
return max(abs(x1 - x0), abs(y1 - y0));
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_road_interconnection
** PURPOSE: Look for a potential interconnection with a newly created road segment
** AUTHOR: Irenee Dubourg
** PROGRAMMER: Irenee Dubourg, ESTP Institut de Recherche en Constructibilite
** CREATION DATE: 06/07/2022
** DESCRIPTION: Based on spr_road_search code (copied and modified code)
**
**
*/
static BOOLEAN
spr_road_interconnection(GRID_P roads, /* IN */
int new_growth_cell_row, /* IN */
int new_growth_cell_col, /* IN */
int connected_road_row, /* IN */
int connected_road_col, /* IN */
int *interconnection_road_row, /* OUT */
int *interconnection_road_col, /* OUT */
int max_search_index /* IN */)
{
char func[] = "spr_road_search";
int i;
int j;
int i_offset;
int j_offset;
BOOLEAN road_found = FALSE;
BOOLEAN isInConnectionRange = FALSE;
int srch_index;
/*** D.D. July 28, 2006 (Begin) **/
/*** New variables for the new road-search algorithm **/
BOOLEAN bn_found;
int bn, tcount, total, N, k, kmax, kmin, ktest, n, r;
int crow, ccol, trow, brow, lcol, rcol, nrows, ncols, srow;
FILE *DD01DBG;
/******************* D.D. July 28, 2006 (End) ******************/
FUNC_INIT;
assert(interconnection_road_row != NULL);
assert(interconnection_road_col != NULL);
assert(max_search_index >= 0);
/*** D.D. July 28, 2006 (Begin) **/
/*** Set the variables to define the search area. **/
nrows = igrid_GetNumRows();
ncols = igrid_GetNumCols();
crow = new_growth_cell_row;
ccol = new_growth_cell_col;
/*** Find the maximal search radius bn. Set N = bn. ***/
N = MAX(max_search_index, nrows);
N = MAX(max_search_index, ncols);
n = ((int)(sqrt((float)(N / 4))) - 1);/** Choose an efficient starting **/
if (n < 1) { n = 1; } /** value for iteration to find bn.**/
bn_found = FALSE;
for (bn = n; bn < MAX(ncols, nrows); bn++)
{
total = 4 * ((1 + bn) * bn);
if (total > N)
{
bn_found = TRUE;
break;
}
}
if (!bn_found)
{
sprintf(msg_buf, "Unable to find road search band bn in new RS algorithm.");
LOG_ERROR(msg_buf);
EXIT(1);
}
else
{
N = bn;
}
///* Compute road interconnection search area*/
//int *lineRow = mem_GetroadLineRowsPtr();
//int *lineCol = mem_GetroadLineColsPtr();
//int linePixelCount;
//int dir_x, dir_y;
//spr_road_interconnection_area(new_growth_cell_row, new_growth_cell_col, connected_road_row, connected_road_col,
// N, lineRow, lineCol, &linePixelCount, &dir_x, &dir_y);
/** Set trow, brow, lcol, and rcol based on N. **/
if (crow >= N) { trow = crow - N; }
else { trow = 0; }
if (crow <= (nrows - 1 - N)) { brow = crow + N; }
else { brow = nrows - 1; }
if (ccol >= N) { lcol = ccol - N; }
else { lcol = 0; }
if (ccol <= ncols - 1 - N) { rcol = ccol + N; }
else { rcol = ncols - 1; }
/******************* D.D. July 28, 2006 (End) ******************/
/*** D.D. July 28, 2006 (Begin) **/
/*** Search for road pixels **/
foundN = -1; /** Set the "road-pixel-found" indicator to "none found". **/
for (srow = 0; srow <= N; srow++)
{
tfoundN = -1; /** Pre-set the temporary indicator to "not found". **/
for (i = 1; i <= 2; i++) /** Process the rows srow above and srow below crow. **/
{
if (i == 1) { r = crow - srow; }
else { r = crow + srow; }
/** Process the first row containing the search center only once. **/
if (srow == 0 && i == 2) { break; }
/** Don't waste time processing a row if it is above or below the
search area; if it contains no road pixels; or if there is no
overlap between the section with road pixels and the search area. **/
if (r < trow || r > brow) { continue; }
if (rporow_ptrNum[r] == 0) { continue; }
if (rporow_ptrMin[r] > rcol) { continue; }
if (rporow_ptrMax[r] < lcol) { continue; }
/** Perform a binary search to find the road pixel in this row
which is closest to the search center. **/
kmin = 0; kmax = rporow_ptrNum[r] - 1;
tcount = 0;
while ((kmax - kmin) > 1)
{
ktest = (kmax + kmin) / 2;
if (WCOL(r, ktest) <= ccol) { kmin = ktest; }
else { kmax = ktest; }
tcount++;
}
if ((WCOL(r, kmax) - ccol) < (ccol - WCOL(r, kmin)))
{
k = kmax;
}
else { k = kmin; }
tfoundN = MAX(srow, abs(WCOL(r, k) - ccol));
tfoundRow = r; tfoundCol = WCOL(r, k);
/** Now save the best point found in this row if it is
closer to the center than the previous best point. ***/
isInConnectionRange = spr_isInConnectionRange(new_growth_cell_row, new_growth_cell_col, connected_road_row, connected_road_col,
tfoundRow, tfoundCol);
if (((foundN < 0 && tfoundN > 0) || (foundN > 0 && tfoundN > 0 && tfoundN < foundN)) && isInConnectionRange)
{
foundN = tfoundN; foundRow = tfoundRow; foundCol = tfoundCol;
}
}
/** If the point found is in the current search band (n = srow) then
this point is the closest road pixel to the search center
and there is no need to continue iterating through the remaining
rows. ***/
if (foundN == srow) { break; }
}
//Check if it is inside searching area
isInConnectionRange = spr_isInConnectionRange(new_growth_cell_row, new_growth_cell_col, connected_road_row, connected_road_col,
foundRow, foundCol);
if (foundN >= 0 && foundN <= N && isInConnectionRange) {
road_found = TRUE;
//check if there is any closer road pixel in this iteration's expanded road pixels
int minDistance = spr_max_norm(foundRow, foundCol, new_growth_cell_row, new_growth_cell_col);
for (int road_index = 0; road_index < road_expansion_count; road_index++) {
int roadRow = road_expansion_row[road_index];
int roadCol = road_expansion_col[road_index];
int distanceToNewRoad = spr_max_norm(roadRow, roadCol, new_growth_cell_row, new_growth_cell_col);
isInConnectionRange = spr_isInConnectionRange(new_growth_cell_row, new_growth_cell_col, connected_road_row, connected_road_col,
roadRow, roadCol);
if (minDistance > distanceToNewRoad && isInConnectionRange && distanceToNewRoad != 0) {
/*fprintf(stdout, "\nCloser new road pixel to (%i, %i) found, d(%i, %i)=%f instead of d(%i, %i)=%f \n",
i_grwth_center, j_grwth_center,
roadRow, roadCol, distanceToNewRoad,
foundRow, foundCol, minDistance);*/
foundRow = roadRow;
foundCol = roadCol;
minDistance = distanceToNewRoad;
}
}
/*fprintf(stdout, "Found interconnection. row = %i, col = %i.\n", foundRow, foundCol);*/
}
(*interconnection_road_row) = foundRow; (*interconnection_road_col) = foundCol;
FUNC_END;
return road_found;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_is_road_suitable
** PURPOSE: true if the road passes the suitability filtering and should be build
** AUTHOR: Irenee Dubourg
** PROGRAMMER: Irenee Dubourg, ESTP Institut de Recherche en Constructibilite
** CREATION DATE: 06/08/2022
** DESCRIPTION:
**
**
*/
BOOLEAN
spr_is_road_suitable(int cellRow, int cellCol, int roadRow, int roadCol)
{
int nrows = igrid_GetNumRows();
int ncols = igrid_GetNumCols();
BOOLEAN long_enough = (spr_max_norm(cellRow, cellCol, roadRow, roadCol) >= (int)(max(nrows, ncols)/40.0));
return long_enough;
}
/******************************************************************************
*******************************************************************************
** FUNCTION NAME: spr_phase5
** PURPOSE: perform phase 5 growth
** AUTHOR: Keith Clarke
** PROGRAMMER: Tommy E. Cathey of NESC (919)541-1500
** CREATION DATE: 11/11/1999
** DESCRIPTION:
**
**
*/
static void
spr_phase5 (COEFF_TYPE road_gravity, /* IN */
COEFF_TYPE diffusion_coefficient, /* IN */
COEFF_TYPE breed_coefficient, /* IN */
GRID_P z, /* IN */
GRID_P delta, /* IN/OUT */
GRID_P slp, /* IN */
GRID_P excld, /* IN */
GRID_P roads, /* IN */
SWGHT_TYPE * swght, /* IN */
int *rt) /* IN/OUT */
/* D.D. Changed July 24, 2006 - No longer passing workspace
int *rt, ** IN/OUT **
GRID_P workspace) ** MOD **
*/
{
char func[] = "spr_phase5";
int iii;
/** D. Donato Aug. 14, 2006 Following line moved to module level **
int growth_count;
*** D. Donato Aug. 14, 2006 */
/* D.D. The following two lines were restored July 24, 2006 */
/* On 8/10/2006 "int *" was changed to "short *". */
/* On 8/14/2006 these two lines were moved to module level. **
short *growth_row;
short *growth_col;
** D. Donato 8/14/2006 */
/* The following two lines were replaced by D. Donato on 06/21/2006
int *growth_row;
int *growth_col;
*/
/* D.D. The following two lines were rescinded July 24, 2006.
GRID_P growth_row;
GRID_P growth_col;
*/
int max_search_index;
int growth_index;
BOOLEAN road_found;
int i_rd_start;
int j_rd_start;
int max_tries;
BOOLEAN spread;
BOOLEAN urbanized;
BOOLEAN connectable;
int i_rd_end;
int j_rd_end;
int i_rd_end_nghbr;
int j_rd_end_nghbr;
int i_rd_end_nghbr_nghbr;
int j_rd_end_nghbr_nghbr;
int tries;
int nrows;
int ncols;
int total_pixels;
float temp4;
int growth_count_fixed;
FUNC_INIT;
assert (road_gravity >= 0.0);
assert (diffusion_coefficient >= 0.0);
assert (breed_coefficient >= 0.0);
assert (z != NULL);
assert (delta != NULL);
assert (slp != NULL);
assert (excld != NULL);
assert (roads != NULL);
assert (swght != NULL);
assert (rt != NULL);
/** D.D. Following line disabled. **
assert (workspace != NULL);
*** **/
nrows = igrid_GetNumRows ();
ncols = igrid_GetNumCols ();
assert (nrows > 0);
assert (ncols > 0);
total_pixels = mem_GetTotalPixels ();
assert (total_pixels > 0);
/*
*
* SET UP WORKSPACE
*
*/
/* The following two lines were replaced by D. Donato 6/21/2006
growth_row = (int *) workspace;
growth_col = (int *) workspace + (nrows);
*/
/* D.D. The following two lines were replaced July 24, 2006
growth_row = (GRID_P) workspace;
growth_col = (GRID_P) workspace + (nrows);
On 8/10/2006 "int *" was changed to "short *".
*/
/* the following two lines were moved to spr_spread 8/14/2006 **
growth_row = (short *) mem_GetGRCrowptr();
growth_col = (short *) mem_GetGRCcolptr();
** D. Donato 8/14/2006 */
/*
*
* DETERMINE THE TOTAL GROWTH COUNT AND SAVE THE
* ROW AND COL LOCATIONS OF THE NEW GROWTH
*
*/
/** D. Donato Aug. 14, 2006 - Moved to spr_spread ***
growth_count = 0;
*** D. Donato Aug. 14, 2006 **/
#ifdef CRAY_C90
#pragma _CRI ivdep
#endif
/** D. Donato Aug. 14, 2006 - The following loop was made unnecessary **
*** by adding statements to track growth each time a pixel in delta **
*** is changed. This should save quite a bit of time since for large **
*** images this loop requires tens of millions of iterations. **
for (iii = 0; iii < total_pixels; iii++)
{
if (delta[iii] > 0)
{
growth_row[growth_count] = iii / ncols;
growth_col[growth_count] = iii % ncols;
growth_count++;
}
}
*** D. Donato Aug. 14, 2006 */
/*
*
* PHASE 5: ROAD TRIPS
* IF THERE IS NEW GROWTH, BEGIN PROCESSING ROAD TRIPS
*
*/
/*VerD*/
/*
fprintf(stdout,"total_pixels = %d\n",total_pixels);
fprintf(stdout,"growth_count = %d\n",growth_count);
*/
/*VerD*/
if (growth_count > 0)
{
/*VerD*/
if (aux_breed_coeff >= 0)
{
road_growth_breed_coefficient = aux_breed_coeff;
}
else
{
road_growth_breed_coefficient = - aux_breed_coeff * breed_coefficient;
}
/*
for (iii = 0; iii < 1 + (int) (breed_coefficient); iii++)
*/
growth_count_fixed=growth_count;
for (iii = 0; iii < 1 + (int) (road_growth_breed_coefficient); iii++)
/*VerD*/
{
/*
*
* DETERMINE THE MAX INDEX INTO THE GLB_RD_SEARCH_INDICES ARRAY
* for road_gravity of 1 we have 8 values
* for road_gravity of 2 we have 16 values
* for road_gravity of 3 we have 24 values
* and so on....
*
* if we need to cover N road_gravity values, then total number of
* indexed values would be
* 8 + 16 + 24 + ... + 8*N = 8*(1+2+3+...+N) = 8*(N(1+N))/2
*
*/
int_road_gravity = spr_GetRoadGravValue (road_gravity);
max_search_index = 4 * (int_road_gravity * (1 + int_road_gravity));
max_search_index = MAX (max_search_index, nrows);
max_search_index = MAX (max_search_index, ncols);
/*
*
* RANDOMLY SELECT A GROWTH PIXEL TO START SEARCH
* FOR ROAD
*
*/
temp4=RANDOM_FLOAT;
growth_index = (int) ((double) growth_count_fixed * temp4);
/*
*
* SEARCH FOR ROAD ABOUT THIS GROWTH POINT
*
*/