grid.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /****************************************************************************
  2. *
  3. * MODULE: r.viewshed
  4. *
  5. * AUTHOR(S): Laura Toma, Bowdoin College - ltoma@bowdoin.edu
  6. * Yi Zhuang - yzhuang@bowdoin.edu
  7. * Ported to GRASS by William Richard -
  8. * wkrichar@bowdoin.edu or willster3021@gmail.com
  9. * Markus Metz: surface interpolation
  10. *
  11. * Date: april 2011
  12. *
  13. * PURPOSE: To calculate the viewshed (the visible cells in the
  14. * raster) for the given viewpoint (observer) location. The
  15. * visibility model is the following: Two points in the raster are
  16. * considered visible to each other if the cells where they belong are
  17. * visible to each other. Two cells are visible to each other if the
  18. * line-of-sight that connects their centers does not intersect the
  19. * terrain. The terrain is NOT viewed as a tesselation of flat cells,
  20. * i.e. if the line-of-sight does not pass through the cell center,
  21. * elevation is determined using bilinear interpolation.
  22. * The viewshed algorithm is efficient both in
  23. * terms of CPU operations and I/O operations. It has worst-case
  24. * complexity O(n lg n) in the RAM model and O(sort(n)) in the
  25. * I/O-model. For the algorithm and all the other details see the
  26. * paper: "Computing Visibility on * Terrains in External Memory" by
  27. * Herman Haverkort, Laura Toma and Yi Zhuang.
  28. *
  29. * COPYRIGHT: (C) 2008 by the GRASS Development Team
  30. *
  31. * This program is free software under the GNU General Public License
  32. * (>=v2). Read the file COPYING that comes with GRASS for details.
  33. *
  34. *****************************************************************************/
  35. #include <stdio.h>
  36. #include <math.h>
  37. #include <stdlib.h>
  38. #include <assert.h>
  39. extern "C"
  40. {
  41. #include <grass/config.h>
  42. #include <grass/gis.h>
  43. #include <grass/glocale.h>
  44. }
  45. #include "grid.h"
  46. /* ------------------------------------------------------------ */
  47. /*copy from b to a */
  48. void copy_header(GridHeader * a, GridHeader b)
  49. {
  50. assert(a);
  51. a->nrows = b.nrows;
  52. a->ncols = b.ncols;
  53. a->xllcorner = b.xllcorner;
  54. a->yllcorner = b.yllcorner;
  55. a->ns_res = b.ns_res;
  56. a->ew_res = b.ew_res;
  57. a->nodata_value = b.nodata_value;
  58. return;
  59. }
  60. /* ------------------------------------------------------------ */
  61. /*returns 1 if value is Nodata, 0 if it is not */
  62. int is_nodata(GridHeader * hd, float value)
  63. {
  64. assert(hd);
  65. return Rast_is_null_value(&value, G_SURFACE_TYPE);
  66. }
  67. /* ------------------------------------------------------------ */
  68. /*returns 1 if value is Nodata, 0 if it is not */
  69. int is_nodata(Grid * grid, float value)
  70. {
  71. assert(grid);
  72. return is_nodata(grid->hd, value);
  73. }
  74. /* ------------------------------------------------------------ */
  75. /* create an empty grid and return it. The header and the data are set
  76. to NULL. */
  77. Grid *create_empty_grid()
  78. {
  79. Grid *ptr_grid = (Grid *) G_malloc(sizeof(Grid));
  80. assert(ptr_grid);
  81. /*initialize structure */
  82. ptr_grid->hd = NULL;
  83. ptr_grid->grid_data = NULL;
  84. #ifdef _DEBUG_ON
  85. printf("**DEBUG: createEmptyGrid \n");
  86. fflush(stdout);
  87. #endif
  88. return ptr_grid;
  89. }
  90. /* ------------------------------------------------------------ */
  91. /* allocate memroy for grid_data; grid must have a header that gives
  92. the dimensions */
  93. void alloc_grid_data(Grid * pgrid)
  94. {
  95. assert(pgrid);
  96. assert(pgrid->hd);
  97. pgrid->grid_data = (float **)G_malloc(pgrid->hd->nrows * sizeof(float *));
  98. assert(pgrid->grid_data);
  99. dimensionType i;
  100. for (i = 0; i < pgrid->hd->nrows; i++) {
  101. pgrid->grid_data[i] =
  102. (float *)G_malloc(pgrid->hd->ncols * sizeof(float));
  103. assert(pgrid->grid_data[i]);
  104. }
  105. #ifdef _DEBUG_ON
  106. printf("**DEBUG: allocGridData\n");
  107. fflush(stdout);
  108. #endif
  109. return;
  110. }
  111. /* ------------------------------------------------------------ */
  112. /*destroy the structure and reclaim all memory allocated */
  113. void destroy_grid(Grid * grid)
  114. {
  115. assert(grid);
  116. /*free grid data if its allocated */
  117. if (grid->grid_data) {
  118. dimensionType i;
  119. for (i = 0; i < grid->hd->nrows; i++) {
  120. if (!grid->grid_data[i])
  121. G_free((float *)grid->grid_data[i]);
  122. }
  123. G_free((float **)grid->grid_data);
  124. }
  125. G_free(grid->hd);
  126. G_free(grid);
  127. #ifdef _DEBUG_ON
  128. printf("**DEBUG: grid destroyed.\n");
  129. fflush(stdout);
  130. #endif
  131. return;
  132. }