~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to raster/r.viewshed/grid.cpp

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
40
#include <stdio.h>
 
41
#include <math.h>
 
42
#include <stdlib.h>
 
43
#include <assert.h>
 
44
 
 
45
extern "C"
 
46
{
 
47
#include <grass/config.h>
 
48
#include <grass/gis.h>
 
49
#include <grass/glocale.h>
 
50
}
 
51
 
 
52
#include "grid.h"
 
53
 
 
54
 
 
55
 
 
56
 
 
57
/* ------------------------------------------------------------ */
 
58
/*copy from b to a */
 
59
void copy_header(GridHeader * a, GridHeader b)
 
60
{
 
61
    assert(a);
 
62
    a->nrows = b.nrows;
 
63
    a->ncols = b.ncols;
 
64
    a->xllcorner = b.xllcorner;
 
65
    a->yllcorner = b.yllcorner;
 
66
    a->ns_res = b.ns_res;
 
67
    a->ew_res = b.ew_res;
 
68
    a->nodata_value = b.nodata_value;
 
69
    return;
 
70
}
 
71
 
 
72
 
 
73
/* ------------------------------------------------------------ */
 
74
/*returns 1 if value is Nodata, 0 if it is not */
 
75
int is_nodata(GridHeader * hd, float value)
 
76
{
 
77
    assert(hd);
 
78
 
 
79
    return Rast_is_null_value(&value, G_SURFACE_TYPE);
 
80
 
 
81
}
 
82
 
 
83
/* ------------------------------------------------------------ */
 
84
/*returns 1 if value is Nodata, 0 if it is not */
 
85
int is_nodata(Grid * grid, float value)
 
86
{
 
87
    assert(grid);
 
88
    return is_nodata(grid->hd, value);
 
89
}
 
90
 
 
91
 
 
92
 
 
93
/* ------------------------------------------------------------ */
 
94
/* create an empty grid and return it. The header and the data are set
 
95
   to NULL.  */
 
96
Grid *create_empty_grid()
 
97
{
 
98
 
 
99
    Grid *ptr_grid = (Grid *) G_malloc(sizeof(Grid));
 
100
 
 
101
    assert(ptr_grid);
 
102
 
 
103
    /*initialize structure */
 
104
    ptr_grid->hd = NULL;
 
105
    ptr_grid->grid_data = NULL;
 
106
 
 
107
#ifdef _DEBUG_ON
 
108
    printf("**DEBUG: createEmptyGrid \n");
 
109
    fflush(stdout);
 
110
#endif
 
111
 
 
112
    return ptr_grid;
 
113
}
 
114
 
 
115
 
 
116
 
 
117
 
 
118
/* ------------------------------------------------------------ */
 
119
/* allocate memroy for grid_data; grid must have a header that gives
 
120
   the dimensions */
 
121
void alloc_grid_data(Grid * pgrid)
 
122
{
 
123
    assert(pgrid);
 
124
    assert(pgrid->hd);
 
125
 
 
126
    pgrid->grid_data = (float **)G_malloc(pgrid->hd->nrows * sizeof(float *));
 
127
 
 
128
    assert(pgrid->grid_data);
 
129
 
 
130
    dimensionType i;
 
131
 
 
132
    for (i = 0; i < pgrid->hd->nrows; i++) {
 
133
        pgrid->grid_data[i] =
 
134
            (float *)G_malloc(pgrid->hd->ncols * sizeof(float));
 
135
 
 
136
        assert(pgrid->grid_data[i]);
 
137
    }
 
138
 
 
139
#ifdef _DEBUG_ON
 
140
    printf("**DEBUG: allocGridData\n");
 
141
    fflush(stdout);
 
142
#endif
 
143
 
 
144
    return;
 
145
}
 
146
 
 
147
 
 
148
/* ------------------------------------------------------------ */
 
149
/*destroy the structure and reclaim all memory allocated */
 
150
void destroy_grid(Grid * grid)
 
151
{
 
152
    assert(grid);
 
153
 
 
154
    /*free grid data if its allocated */
 
155
    if (grid->grid_data) {
 
156
        dimensionType i;
 
157
 
 
158
        for (i = 0; i < grid->hd->nrows; i++) {
 
159
            if (!grid->grid_data[i])
 
160
                G_free((float *)grid->grid_data[i]);
 
161
        }
 
162
 
 
163
        G_free((float **)grid->grid_data);
 
164
    }
 
165
 
 
166
    G_free(grid->hd);
 
167
    G_free(grid);
 
168
 
 
169
#ifdef _DEBUG_ON
 
170
    printf("**DEBUG: grid destroyed.\n");
 
171
    fflush(stdout);
 
172
#endif
 
173
 
 
174
    return;
 
175
}