1 
2 /****************************************************************************
3  *
4  * MODULE:       r.viewshed
5  *
6  * AUTHOR(S):    Laura Toma, Bowdoin College - ltoma@bowdoin.edu
7  *               Yi Zhuang - yzhuang@bowdoin.edu
8 
9  *               Ported to GRASS by William Richard -
10  *               wkrichar@bowdoin.edu or willster3021@gmail.com
11  *               Markus Metz: surface interpolation
12  *
13  * Date:         april 2011
14  *
15  * PURPOSE: To calculate the viewshed (the visible cells in the
16  * raster) for the given viewpoint (observer) location.  The
17  * visibility model is the following: Two points in the raster are
18  * considered visible to each other if the cells where they belong are
19  * visible to each other.  Two cells are visible to each other if the
20  * line-of-sight that connects their centers does not intersect the
21  * terrain. The terrain is NOT viewed as a tesselation of flat cells,
22  * i.e. if the line-of-sight does not pass through the cell center,
23  * elevation is determined using bilinear interpolation.
24  * The viewshed algorithm is efficient both in
25  * terms of CPU operations and I/O operations. It has worst-case
26  * complexity O(n lg n) in the RAM model and O(sort(n)) in the
27  * I/O-model.  For the algorithm and all the other details see the
28  * paper: "Computing Visibility on * Terrains in External Memory" by
29  * Herman Haverkort, Laura Toma and Yi Zhuang.
30  *
31  * COPYRIGHT: (C) 2008 by the GRASS Development Team
32  *
33  * This program is free software under the GNU General Public License
34  * (>=v2). Read the file COPYING that comes with GRASS for details.
35  *
36  *****************************************************************************/
37 
38 
39 #ifndef _GRASS_H
40 #define _GRASS_H
41 
42 #include <math.h>
43 extern "C"
44 {
45 #include <grass/gis.h>
46 #include <grass/raster.h>
47 #include <grass/glocale.h>
48 }
49 
50 #include "eventlist.h"
51 #include "grid.h"
52 #include "visibility.h"
53 
54 
55 /* ------------------------------------------------------------ */
56 /* if viewOptions.doCurv is on then adjust the passed height for
57    curvature of the earth; otherwise return the passed height
58    unchanged.
59  */
60 float adjust_for_curvature(Viewpoint vp, double row,
61 			   double col, float h,
62 			   ViewOptions viewOptions);
63 
64 
65 /* helper function to deal with GRASS writing to a row buffer */
66 void writeValue(void *ptr, int j, double x, RASTER_MAP_TYPE data_type);
67 void writeNodataValue(void *ptr, int j, RASTER_MAP_TYPE data_type);
68 
69 
70 
71 /*return a GridHeader with all the relevant data filled in from GRASS */
72 GridHeader *read_header(char *rastName, Cell_head * region);
73 
74 /* calculate ENTER and EXIT event elevation */
75 surface_type calculate_event_elevation(AEvent e, int nrows, int ncols,
76                                        dimensionType vprow, dimensionType vpcol,
77 				       G_SURFACE_T **inrast, RASTER_MAP_TYPE data_type);
78 
79 
80 /*  ************************************************************ */
81 /* input: an array capable to hold the max number of events, a raster
82    name, a viewpoint and the viewOptions; action: figure out all events
83    in the input file, and write them to the event list. data is
84    allocated and initialized with all the cells on the same row as the
85    viewpoint. it returns the number of events. initialize and fill
86    AEvent* with all the events for the map.  Used when solving in
87    memory, so the AEvent* should fit in memory.  */
88 size_t
89 init_event_list_in_memory(AEvent * eventList, char *rastName,
90 				Viewpoint * vp, GridHeader * hd,
91 				ViewOptions viewOptions, surface_type ***data,
92 				MemoryVisibilityGrid * visgrid);
93 
94 
95 
96 /* ************************************************************ */
97 /* input: an arcascii file, a grid header and a viewpoint; action:
98    figure out all events in the input file, and write them to the
99    stream.  It assumes the file pointer is positioned rigth after the
100    grid header so that this function can read all grid elements.
101 
102    if data is not NULL, it creates an array that stores all events on
103    the same row as the viewpoint.
104  */
105 AMI_STREAM < AEvent > *init_event_list(char *rastName, Viewpoint * vp,
106 					     GridHeader * hd,
107 					     ViewOptions viewOptions,
108 					     surface_type ***data,
109 					     IOVisibilityGrid * visgrid);
110 
111 
112 /* ************************************************************ */
113 /*  saves the grid into a GRASS raster.  Loops through all elements x
114    in row-column order and writes fun(x) to file. */
115 void
116 save_grid_to_GRASS(Grid * grid, char *filename, RASTER_MAP_TYPE type,
117 		   OutputMode mode);
118 
119 
120 /* ************************************************************ */
121 /*  using the visibility information recorded in visgrid, it creates an
122    output viewshed raster with name outfname; for every point p that
123    is visible in the grid, the corresponding value in the output
124    raster is elevation(p) - viewpoint_elevation(p); the elevation
125    values are read from elevfname raster */
126 
127 void
128 save_vis_elev_to_GRASS(Grid * visgrid, char *elevfname, char *visfname,
129 		       float vp_elev);
130 
131 
132 /* ************************************************************ */
133 /* write the visibility grid to GRASS. assume all cells that are not
134    in stream are NOT visible. assume stream is sorted in (i,j) order.
135    for each value x it writes to grass fun(x) */
136 void
137 save_io_visibilitygrid_to_GRASS(IOVisibilityGrid * visgrid,
138 				char *outfname, RASTER_MAP_TYPE type,
139 				float (*fun) (float),
140 				OutputMode mode);
141 
142 
143 
144 /* ************************************************************ */
145 /*  using the visibility information recorded in visgrid, it creates
146    an output viewshed raster with name outfname; for every point p
147    that is visible in the grid, the corresponding value in the output
148    raster is elevation(p) - viewpoint_elevation(p); the elevation
149    values are read from elevfname raster. assume stream is sorted in
150    (i,j) order. */
151 void
152 save_io_vis_and_elev_to_GRASS(IOVisibilityGrid * visgrid, char *elevfname,
153 			      char *visfname, float vp_elev);
154 
155 #endif/*_GRASS_H*/
156