visibility.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. #ifndef visibility_h
  36. #define visibility_h
  37. #include <grass/config.h>
  38. #include <grass/iostream/ami.h>
  39. #include "grid.h"
  40. /* default max distance */
  41. #define INFINITY_DISTANCE -1
  42. /* File/directory name lengths for GRASS compatibility */
  43. #define GNAME_MAX 256
  44. #define GPATH_MAX 4096
  45. typedef struct viewpoint_
  46. {
  47. dimensionType row, col;
  48. float elev;
  49. float target_offset;
  50. } Viewpoint;
  51. typedef enum
  52. {
  53. VISIBLE = 1,
  54. INVISIBLE = -1,
  55. /*boolean values for output */
  56. BOOL_VISIBLE = 1,
  57. BOOL_INVISIBLE = 0
  58. } VisMode;
  59. typedef struct visCell_
  60. {
  61. dimensionType row;
  62. dimensionType col;
  63. /* VisMode vis; */
  64. float angle;
  65. } VisCell;
  66. typedef enum outputMode_
  67. {
  68. OUTPUT_ANGLE = 0,
  69. OUTPUT_BOOL = 1,
  70. OUTPUT_ELEV = 2
  71. } OutputMode;
  72. typedef struct viewOptions_
  73. {
  74. /* the name of the input raster */
  75. char inputfname[GNAME_MAX];
  76. /* the name of the output raster */
  77. char outputfname[GNAME_MAX];
  78. float obsElev;
  79. /* observer elevation above the terrain */
  80. float tgtElev;
  81. /* target elevation offset above the terrain */
  82. float maxDist;
  83. /* points that are farther than this distance from the viewpoint are
  84. not visible */
  85. OutputMode outputMode;
  86. /* The mode the viewshed is output;
  87. - in angle mode, the values recorded are {NODATA, INVISIBLE, angle}
  88. - in boolean mode, the values recorded are {BOOL_INVISIBLE, BOOL_VISIBLE}
  89. - in elev mode, the values recorded are {NODATA, INVISIBLE, elevation}
  90. */
  91. int doCurv;
  92. /*determines if the curvature of the earth should be considered
  93. when calculating. Only implemented for GRASS version. */
  94. int doRefr;
  95. double refr_coef;
  96. /*determines if atmospheric refraction should be considered
  97. when calculating. Only implemented for GRASS version. */
  98. double ellps_a; /* the parameter of the ellipsoid */
  99. float cellsize; /* the cell resolution */
  100. char streamdir[GPATH_MAX]; /* directory for tmp files */
  101. } ViewOptions;
  102. /*memory visibility grid */
  103. typedef struct memory_visibility_grid_
  104. {
  105. Grid *grid;
  106. Viewpoint *vp;
  107. } MemoryVisibilityGrid;
  108. /*io-efficient visibility grid */
  109. typedef struct IOvisibility_grid_
  110. {
  111. GridHeader *hd;
  112. Viewpoint *vp;
  113. AMI_STREAM < VisCell > *visStr;
  114. } IOVisibilityGrid;
  115. /* ------------------------------------------------------------ */
  116. /* visibility output functions */
  117. /* The following functions are used to convert the visibility results
  118. recorded during the viewshed computation into the output grid into
  119. the format required by the user. x is assumed to be the
  120. visibility angle computed for a cell during the viewshed
  121. computation.
  122. The value passed to this function is the following: x is NODATA if the
  123. cell is NODATA; x is INVISIBLE if the cell is invisible; x is the
  124. vertical angle of the cell wrt the viewpoint if the cell is
  125. visible---the angle is a value in (0,180).
  126. */
  127. /* these functions assume that x is a value computed during the
  128. viewshed computation; right now x represents the vertical angle of a
  129. visible point wrt to the viewpoint; INVISIBLE if invisible; NODATA if
  130. nodata. They return true if x is visible, invisible but nodata,
  131. andnodata, respectively */
  132. int is_visible(float x);
  133. int is_invisible_not_nodata(float x);
  134. int is_invisible_nodata(float x);
  135. /* This function is called when the program runs in
  136. viewOptions.outputMode == OUTPUT_BOOL. */
  137. float booleanVisibilityOutput(float x);
  138. /* This function is called when the program runs in
  139. viewOptions.outputMode == OUTPUT_ANGLE. */
  140. float angleVisibilityOutput(float x);
  141. /* ------------------------------------------------------------ */
  142. /* viewpoint functions */
  143. void print_viewpoint(Viewpoint vp);
  144. /*copy from b to a */
  145. void copy_viewpoint(Viewpoint * a, Viewpoint b);
  146. void
  147. set_viewpoint_coord(Viewpoint * vp, dimensionType row, dimensionType col);
  148. void set_viewpoint_elev(Viewpoint * vp, float elev);
  149. /* ------------------------------------------------------------ */
  150. /* MemoryVisibilityGrid functions */
  151. MemoryVisibilityGrid *create_inmem_visibilitygrid(GridHeader hd,
  152. Viewpoint vp);
  153. void free_inmem_visibilitygrid(MemoryVisibilityGrid * visgrid);
  154. void set_inmem_visibilitygrid(MemoryVisibilityGrid * visgrid, float val);
  155. void add_result_to_inmem_visibilitygrid(MemoryVisibilityGrid * visgrid,
  156. dimensionType i, dimensionType j,
  157. float val);
  158. void save_inmem_visibilitygrid(MemoryVisibilityGrid * vigrid,
  159. ViewOptions viewopt, Viewpoint vp);
  160. /* ------------------------------------------------------------ */
  161. /* IOVisibilityGrid functions */
  162. /*create grid from given header and viewpoint */
  163. IOVisibilityGrid *init_io_visibilitygrid(GridHeader hd, Viewpoint vp);
  164. /*frees a visibility grid */
  165. void free_io_visibilitygrid(IOVisibilityGrid * grid);
  166. /*write cell to stream */
  167. void add_result_to_io_visibilitygrid(IOVisibilityGrid * visgrid,
  168. VisCell * cell);
  169. /*void
  170. addResult(IOVisibilityGrid* visgrid, DimensionType row, DimensionType col,
  171. VisMode vis);
  172. */
  173. /* write visibility grid. assume all cells that are not in stream are
  174. NOT visible. assume stream is sorted. */
  175. void
  176. save_io_visibilitygrid(IOVisibilityGrid * visgrid,
  177. ViewOptions viewoptions, Viewpoint vp);
  178. /*sort stream in grid (i,j) order */
  179. void sort_io_visibilitygrid(IOVisibilityGrid * visGrid);
  180. class IJCompare
  181. {
  182. public:
  183. int compare(const VisCell &, const VisCell &);
  184. };
  185. #endif