init.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* init.c */
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <grass/gis.h>
  6. #include <grass/raster.h>
  7. #include <grass/glocale.h>
  8. #include "ransurf.h"
  9. #include "local_proto.h"
  10. /* function prototypes */
  11. static int comp_array(const void *p1, const void *p2);
  12. void Init()
  13. {
  14. struct Cell_head Region;
  15. int Count;
  16. int FD, row, col;
  17. double MinRes;
  18. G_debug(2, "Init()");
  19. Rs = Rast_window_rows();
  20. Cs = Rast_window_cols();
  21. G_get_set_window(&Region);
  22. EW = Region.ew_res;
  23. NS = Region.ns_res;
  24. if (EW < NS)
  25. MinRes = EW;
  26. else
  27. MinRes = NS;
  28. CellBuffer = Rast_allocate_c_buf();
  29. /* Out = FlagCreate( Rs, Cs); */
  30. Out = (CELL **) G_malloc(sizeof(CELL *) * Rs);
  31. for (row = 0; row < Rs; row++) {
  32. Out[row] = Rast_allocate_c_buf();
  33. Rast_zero_buf(Out[row], CELL_TYPE);
  34. }
  35. Cells = FlagCreate(Rs, Cs);
  36. CellCount = 0;
  37. if (G_find_raster2("MASK", G_mapset())) {
  38. FD = Rast_open_old("MASK", G_mapset());
  39. {
  40. for (row = 0; row < Rs; row++) {
  41. Rast_get_c_row_nomask(FD, CellBuffer, row);
  42. for (col = 0; col < Cs; col++) {
  43. if (CellBuffer[col] && !Rast_is_c_null_value(&CellBuffer[col])) {
  44. FLAG_SET(Cells, row, col);
  45. CellCount++;
  46. }
  47. }
  48. }
  49. Rast_close(FD);
  50. }
  51. }
  52. else {
  53. for (row = 0; row < Rs; row++) {
  54. for (col = 0; col < Cs; col++) {
  55. FLAG_SET(Cells, row, col);
  56. }
  57. }
  58. CellCount = Rs * Cs;
  59. }
  60. sscanf(Distance->answer, "%lf", &MaxDist);
  61. if (MaxDist < 0.0)
  62. G_fatal_error(_("Distance must be >= 0.0"));
  63. G_debug(3, "(MaxDist):%.12lf", MaxDist);
  64. MaxDistSq = MaxDist * MaxDist;
  65. if (!SeedStuff->answer) {
  66. Seed = (int)getpid();
  67. }
  68. else {
  69. sscanf(SeedStuff->answer, "%d", &(Seed));
  70. }
  71. if (Seed > SEED_MAX) {
  72. Seed = Seed % SEED_MAX;
  73. }
  74. else if (Seed < SEED_MIN) {
  75. while (Seed < SEED_MIN)
  76. Seed += SEED_MAX - SEED_MIN;
  77. }
  78. G_message(_("Generating raster map <%s>..."),
  79. Output->answer);
  80. DoNext = (CELLSORTER *) G_malloc(CellCount * sizeof(CELLSORTER));
  81. Count = 0;
  82. for (row = 0; row < Rs; row++) {
  83. G_percent(row, Rs, 2);
  84. for (col = 0; col < Cs; col++) {
  85. if (0 != FlagGet(Cells, row, col)) {
  86. DoNext[Count].R = row;
  87. DoNext[Count].C = col;
  88. DoNext[Count].Value = GasDev();
  89. if (++Count == CellCount) {
  90. row = Rs;
  91. col = Cs;
  92. }
  93. }
  94. }
  95. }
  96. G_percent(1, 1, 1);
  97. qsort(DoNext, CellCount, sizeof(CELLSORTER), comp_array);
  98. }
  99. static int comp_array(const void *q1, const void *q2)
  100. {
  101. const CELLSORTER *p1 = q1;
  102. const CELLSORTER *p2 = q2;
  103. if (p1->Value < p2->Value)
  104. return (-1);
  105. if (p2->Value < p1->Value)
  106. return (1);
  107. return (0);
  108. }