support.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * r.in.xyz support fns.
  3. * Copyright 2006 by M. Hamish Bowman, and The GRASS Development Team
  4. * Author: M. Hamish Bowman, University of Otago, Dunedin, New Zealand
  5. *
  6. * This program is free software licensed under the GPL (>=v2).
  7. * Read the COPYING file that comes with GRASS for details.
  8. *
  9. */
  10. #include <grass/gis.h>
  11. #include <grass/raster.h>
  12. #include "local_proto.h"
  13. static void *get_cell_ptr(void *array, int cols, int row, int col,
  14. RASTER_MAP_TYPE map_type)
  15. {
  16. return G_incr_void_ptr(array,
  17. ((row * (size_t) cols) +
  18. col) * Rast_cell_size(map_type));
  19. }
  20. int blank_array(void *array, int nrows, int ncols, RASTER_MAP_TYPE map_type,
  21. int value)
  22. {
  23. /* flood fill initialize the array to either 0 or NULL */
  24. /* "value" can be either 0 (for 0.0) or -1 (for NULL) */
  25. int row, col;
  26. void *ptr;
  27. ptr = array;
  28. switch (value) {
  29. case 0:
  30. /* fill with 0 */
  31. /* simpler to use Rast_raster_cpy() or similar ?? */
  32. for (row = 0; row < nrows; row++) {
  33. for (col = 0; col < ncols; col++) {
  34. Rast_set_c_value(ptr, 0, map_type);
  35. ptr = G_incr_void_ptr(ptr, Rast_cell_size(map_type));
  36. }
  37. }
  38. break;
  39. case -1:
  40. /* fill with NULL */
  41. /* alloc for col+1, do we come up (nrows) short? no. */
  42. Rast_set_null_value(array, nrows * ncols, map_type);
  43. break;
  44. default:
  45. return -1;
  46. }
  47. return 0;
  48. }
  49. /* make all these fns return void? */
  50. int update_n(void *array, int cols, int row, int col)
  51. {
  52. void *ptr = get_cell_ptr(array, cols, row, col, CELL_TYPE);
  53. CELL old_n;
  54. old_n = Rast_get_c_value(ptr, CELL_TYPE);
  55. Rast_set_c_value(ptr, (1 + old_n), CELL_TYPE);
  56. return 0;
  57. }
  58. int update_min(void *array, int cols, int row, int col,
  59. RASTER_MAP_TYPE map_type, double value)
  60. {
  61. void *ptr = get_cell_ptr(array, cols, row, col, map_type);
  62. DCELL old_val;
  63. if (Rast_is_null_value(ptr, map_type))
  64. Rast_set_d_value(ptr, (DCELL) value, map_type);
  65. else {
  66. old_val = Rast_get_d_value(ptr, map_type);
  67. if (value < old_val)
  68. Rast_set_d_value(ptr, (DCELL) value, map_type);
  69. }
  70. return 0;
  71. }
  72. int update_max(void *array, int cols, int row, int col,
  73. RASTER_MAP_TYPE map_type, double value)
  74. {
  75. void *ptr = get_cell_ptr(array, cols, row, col, map_type);
  76. DCELL old_val;
  77. if (Rast_is_null_value(ptr, map_type))
  78. Rast_set_d_value(ptr, (DCELL) value, map_type);
  79. else {
  80. old_val = Rast_get_d_value(ptr, map_type);
  81. if (value > old_val)
  82. Rast_set_d_value(ptr, (DCELL) value, map_type);
  83. }
  84. return 0;
  85. }
  86. int update_sum(void *array, int cols, int row, int col,
  87. RASTER_MAP_TYPE map_type, double value)
  88. {
  89. void *ptr = get_cell_ptr(array, cols, row, col, map_type);
  90. DCELL old_val;
  91. old_val = Rast_get_d_value(ptr, map_type);
  92. Rast_set_d_value(ptr, value + old_val, map_type);
  93. return 0;
  94. }
  95. int update_sumsq(void *array, int cols, int row, int col,
  96. RASTER_MAP_TYPE map_type, double value)
  97. {
  98. void *ptr = get_cell_ptr(array, cols, row, col, map_type);
  99. DCELL old_val;
  100. old_val = Rast_get_d_value(ptr, map_type);
  101. Rast_set_d_value(ptr, (value * value) + old_val, map_type);
  102. return 0;
  103. }