2
/****************************************************************************
6
* AUTHOR(S): Laura Toma, Bowdoin College - ltoma@bowdoin.edu
7
* Yi Zhuang - yzhuang@bowdoin.edu
9
* Ported to GRASS by William Richard -
10
* wkrichar@bowdoin.edu or willster3021@gmail.com
11
* Markus Metz: surface interpolation
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.
31
* COPYRIGHT: (C) 2008 by the GRASS Development Team
33
* This program is free software under the GNU General Public License
34
* (>=v2). Read the file COPYING that comes with GRASS for details.
36
*****************************************************************************/
41
#include <grass/config.h>
42
#include <grass/iostream/ami.h>
49
/* default max distance */
50
#define INFINITY_DISTANCE -1
52
/* File/directory name lengths for GRASS compatibility */
54
#define GPATH_MAX 4096
57
typedef struct viewpoint_
59
dimensionType row, col;
70
/*boolean values for output */
76
typedef struct visCell_
86
typedef enum outputMode_
94
typedef struct viewOptions_
97
/* the name of the input raster */
98
char inputfname[GNAME_MAX];
100
/* the name of the output raster */
101
char outputfname[GNAME_MAX];
104
/* observer elevation above the terrain */
107
/* target elevation offset above the terrain */
110
/* points that are farther than this distance from the viewpoint are
113
OutputMode outputMode;
114
/* The mode the viewshed is output;
115
- in angle mode, the values recorded are {NODATA, INVISIBLE, angle}
116
- in boolean mode, the values recorded are {BOOL_INVISIBLE, BOOL_VISIBLE}
117
- in elev mode, the values recorded are {NODATA, INVISIBLE, elevation}
121
/*determines if the curvature of the earth should be considered
122
when calculating. Only implemented for GRASS version. */
126
/*determines if atmospheric refraction should be considered
127
when calculating. Only implemented for GRASS version. */
129
double ellps_a; /* the parameter of the ellipsoid */
130
float cellsize; /* the cell resolution */
131
char streamdir[GPATH_MAX]; /* directory for tmp files */
137
/*memory visibility grid */
138
typedef struct memory_visibility_grid_
142
} MemoryVisibilityGrid;
145
/*io-efficient visibility grid */
146
typedef struct IOvisibility_grid_
150
AMI_STREAM < VisCell > *visStr;
156
/* ------------------------------------------------------------ */
157
/* visibility output functions */
159
/* The following functions are used to convert the visibility results
160
recorded during the viewshed computation into the output grid into
161
the format required by the user. x is assumed to be the
162
visibility angle computed for a cell during the viewshed
165
The value passed to this function is the following: x is NODATA if the
166
cell is NODATA; x is INVISIBLE if the cell is invisible; x is the
167
vertical angle of the cell wrt the viewpoint if the cell is
168
visible---the angle is a value in (0,180).
170
/* these functions assume that x is a value computed during the
171
viewshed computation; right now x represents the vertical angle of a
172
visible point wrt to the viewpoint; INVISIBLE if invisible; NODATA if
173
nodata. They return true if x is visible, invisible but nodata,
174
andnodata, respectively */
175
int is_visible(float x);
176
int is_invisible_not_nodata(float x);
177
int is_invisible_nodata(float x);
179
/* This function is called when the program runs in
180
viewOptions.outputMode == OUTPUT_BOOL. */
181
float booleanVisibilityOutput(float x);
183
/* This function is called when the program runs in
184
viewOptions.outputMode == OUTPUT_ANGLE. */
185
float angleVisibilityOutput(float x);
191
/* ------------------------------------------------------------ */
192
/* viewpoint functions */
194
void print_viewpoint(Viewpoint vp);
196
/*copy from b to a */
197
void copy_viewpoint(Viewpoint * a, Viewpoint b);
200
set_viewpoint_coord(Viewpoint * vp, dimensionType row, dimensionType col);
202
void set_viewpoint_elev(Viewpoint * vp, float elev);
206
/* ------------------------------------------------------------ */
207
/* MemoryVisibilityGrid functions */
209
MemoryVisibilityGrid *create_inmem_visibilitygrid(GridHeader hd,
212
void free_inmem_visibilitygrid(MemoryVisibilityGrid * visgrid);
214
void set_inmem_visibilitygrid(MemoryVisibilityGrid * visgrid, float val);
216
void add_result_to_inmem_visibilitygrid(MemoryVisibilityGrid * visgrid,
217
dimensionType i, dimensionType j,
220
void save_inmem_visibilitygrid(MemoryVisibilityGrid * vigrid,
221
ViewOptions viewopt, Viewpoint vp);
224
/* ------------------------------------------------------------ */
225
/* IOVisibilityGrid functions */
227
/*create grid from given header and viewpoint */
228
IOVisibilityGrid *init_io_visibilitygrid(GridHeader hd, Viewpoint vp);
230
/*frees a visibility grid */
231
void free_io_visibilitygrid(IOVisibilityGrid * grid);
233
/*write cell to stream */
234
void add_result_to_io_visibilitygrid(IOVisibilityGrid * visgrid,
238
addResult(IOVisibilityGrid* visgrid, DimensionType row, DimensionType col,
243
/* write visibility grid. assume all cells that are not in stream are
244
NOT visible. assume stream is sorted. */
246
save_io_visibilitygrid(IOVisibilityGrid * visgrid,
247
ViewOptions viewoptions, Viewpoint vp);
250
/*sort stream in grid (i,j) order */
251
void sort_io_visibilitygrid(IOVisibilityGrid * visGrid);
256
int compare(const VisCell &, const VisCell &);