Debian Codegraph @

imagery/i.ortho.photo/i.ortho.rectify/angle.c

raw symbols SCIP
1 /* calculate camera angle to local surface
2 * 90 degrees: orthogonal to local surface
3 * 0 degrees: parallel to local surface
4 * < 0 degrees: not visible by camera
5 *
6 * Earth curvature is not considered, assuming that the extends of the
7 * imagery to be orthorectified are rather small
8 * Shadowing effects by ridges and peaks are not considered */
9
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13 #include <math.h>
14 #include "global.h"
15
16 int camera_angle(struct Ortho_Image_Group *group, char *name)
17 {
18 int row, col, nrows, ncols;
19 double XC = group->XC;
20 double YC = group->YC;
21 double ZC = group->ZC;
22 double c_angle, c_angle_min, c_alt, c_az, slope, aspect;
23 double radians_to_degrees = 180.0 / M_PI;
24
25 /* double degrees_to_radians = M_PI / 180.0; */
26 DCELL e1, e2, e3, e4, e5, e6, e7, e8, e9;
27 double factor, V, H, dx, dy, dz, key;
28 double north, south, east, west, ns_med;
29 FCELL *fbuf0, *fbuf1, *fbuf2, *tmpbuf, *outbuf;
30 int elevfd, outfd;
31 struct Cell_head cellhd;
32 struct Colors colr;
33 FCELL clr_min, clr_max;
34 struct History hist;
35 char *type;
36
37 G_message(_("Calculating camera angle to local surface..."));
38
39 select_target_env();
40
41 /* align target window to elevation map, otherwise we get artefacts
42 * like in r.slope.aspect -a */
43
44 Rast_get_cellhd(elev_name, elev_mapset, &cellhd);
45
46 Rast_align_window(&target_window, &cellhd);
47 Rast_set_window(&target_window);
48
49 elevfd = Rast_open_old(elev_name, elev_mapset);
50 if (elevfd < 0) {
51 G_fatal_error(_("Could not open elevation raster"));
52 return 1;
53 }
54
55 nrows = target_window.rows;
56 ncols = target_window.cols;
57
58 outfd = Rast_open_new(name, FCELL_TYPE);
59 fbuf0 = Rast_allocate_buf(FCELL_TYPE);
60 fbuf1 = Rast_allocate_buf(FCELL_TYPE);
61 fbuf2 = Rast_allocate_buf(FCELL_TYPE);
62 outbuf = Rast_allocate_buf(FCELL_TYPE);
63
64 /* give warning if location units are different from meters and zfactor=1 */
65 factor = G_database_units_to_meters_factor();
66 if (factor != 1.0)
67 G_warning(_("Converting units to meters, factor=%.6f"), factor);
68
69 G_begin_distance_calculations();
70 north = Rast_row_to_northing(0.5, &target_window);
71 ns_med = Rast_row_to_northing(1.5, &target_window);
72 south = Rast_row_to_northing(2.5, &target_window);
73 east = Rast_col_to_easting(2.5, &target_window);
74 west = Rast_col_to_easting(0.5, &target_window);
75 V = G_distance(east, north, east, south) * 4;
76 H = G_distance(east, ns_med, west, ns_med) * 4;
77
78 c_angle_min = 90;
79 Rast_get_row(elevfd, fbuf1, 0, FCELL_TYPE);
80 Rast_get_row(elevfd, fbuf2, 1, FCELL_TYPE);
81
82 for (row = 0; row < nrows; row++) {
83 G_percent(row, nrows, 2);
84
85 Rast_set_null_value(outbuf, ncols, FCELL_TYPE);
86
87 /* first and last row */
88 if (row == 0 || row == nrows - 1) {
89 Rast_put_row(outfd, outbuf, FCELL_TYPE);
90 continue;
91 }
92
93 tmpbuf = fbuf0;
94 fbuf0 = fbuf1;
95 fbuf1 = fbuf2;
96 fbuf2 = tmpbuf;
97
98 Rast_get_row(elevfd, fbuf2, row + 1, FCELL_TYPE);
99
100 north = Rast_row_to_northing(row + 0.5, &target_window);
101
102 for (col = 1; col < ncols - 1; col++) {
103
104 e1 = fbuf0[col - 1];
105 if (Rast_is_d_null_value(&e1))
106 continue;
107 e2 = fbuf0[col];
108 if (Rast_is_d_null_value(&e2))
109 continue;
110 e3 = fbuf0[col + 1];
111 if (Rast_is_d_null_value(&e3))
112 continue;
113 e4 = fbuf1[col - 1];
114 if (Rast_is_d_null_value(&e4))
115 continue;
116 e5 = fbuf1[col];
117 if (Rast_is_d_null_value(&e5))
118 continue;
119 e6 = fbuf1[col + 1];
120 if (Rast_is_d_null_value(&e6))
121 continue;
122 e7 = fbuf2[col - 1];
123 if (Rast_is_d_null_value(&e7))
124 continue;
125 e8 = fbuf2[col];
126 if (Rast_is_d_null_value(&e8))
127 continue;
128 e9 = fbuf2[col + 1];
129 if (Rast_is_d_null_value(&e9))
130 continue;
131
132 dx = ((e1 + e4 + e4 + e7) - (e3 + e6 + e6 + e9)) / H;
133 dy = ((e7 + e8 + e8 + e9) - (e1 + e2 + e2 + e3)) / V;
134
135 /* compute topographic parameters */
136 key = dx * dx + dy * dy;
137 /* slope in radians */
138 slope = atan(sqrt(key));
139
140 /* aspect in radians */
141 if (key == 0.)
142 aspect = 0.;
143 else if (dx == 0) {
144 if (dy > 0)
145 aspect = M_PI / 2;
146 else
147 aspect = 1.5 * M_PI;
148 }
149 else {
150 aspect = atan2(dy, dx);
151 if (aspect <= 0.)
152 aspect = 2 * M_PI + aspect;
153 }
154
155 /* camera altitude angle in radians */
156 east = Rast_col_to_easting(col + 0.5, &target_window);
157 dx = east - XC;
158 dy = north - YC;
159 dz = ZC - e5;
160 c_alt = atan(sqrt(dx * dx + dy * dy) / dz);
161
162 /* camera azimuth angle in radians */
163 c_az = atan(dy / dx);
164 if (east < XC && north != YC)
165 c_az += M_PI;
166 else if (north < YC && east > XC)
167 c_az += 2 * M_PI;
168
169 /* camera angle to real ground */
170 /* orthogonal to ground: 90 degrees */
171 /* parallel to ground: 0 degrees */
172 c_angle = asin(cos(c_alt) * cos(slope) -
173 sin(c_alt) * sin(slope) * cos(c_az - aspect));
174
175 outbuf[col] = c_angle * radians_to_degrees;
176 if (c_angle_min > outbuf[col])
177 c_angle_min = outbuf[col];
178 }
179 Rast_put_row(outfd, outbuf, FCELL_TYPE);
180 }
181 G_percent(row, nrows, 2);
182
183 Rast_close(elevfd);
184 Rast_close(outfd);
185 G_free(fbuf0);
186 G_free(fbuf1);
187 G_free(fbuf2);
188 G_free(outbuf);
189
190 type = "raster";
191 Rast_short_history(name, type, &hist);
192 Rast_command_history(&hist);
193 Rast_write_history(name, &hist);
194
195 Rast_init_colors(&colr);
196 if (c_angle_min < 0) {
197 clr_min = (FCELL)((int)(c_angle_min / 10 - 1)) * 10;
198 clr_max = 0;
199 Rast_add_f_color_rule(&clr_min, 0, 0, 0, &clr_max, 0, 0, 0, &colr);
200 }
201 clr_min = 0;
202 clr_max = 10;
203 Rast_add_f_color_rule(&clr_min, 0, 0, 0, &clr_max, 255, 0, 0, &colr);
204 clr_min = 10;
205 clr_max = 40;
206 Rast_add_f_color_rule(&clr_min, 255, 0, 0, &clr_max, 255, 255, 0, &colr);
207 clr_min = 40;
208 clr_max = 90;
209 Rast_add_f_color_rule(&clr_min, 255, 255, 0, &clr_max, 0, 255, 0, &colr);
210
211 Rast_write_colors(name, G_mapset(), &colr);
212
213 select_current_env();
214
215 return 1;
216 }