Debian Codegraph @

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

raw symbols SCIP
1 #include <stdlib.h>
2 #include <string.h>
3 #include <math.h>
4 #include "global.h"
5
6 int get_ref_window(struct Ref *ref, int *ref_list, struct Cell_head *cellhd)
7 {
8 int i;
9 int count;
10 struct Cell_head win;
11
12 /* from all the files in the group, get max extends and min resolutions */
13 count = 0;
14 for (i = 0; i < ref->nfiles; i++) {
15 if (!ref_list[i])
16 continue;
17
18 if (count++ == 0) {
19 Rast_get_cellhd(ref->file[i].name, ref->file[i].mapset, cellhd);
20 }
21 else {
22 Rast_get_cellhd(ref->file[i].name, ref->file[i].mapset, &win);
23 /* max extends */
24 if (cellhd->north < win.north)
25 cellhd->north = win.north;
26 if (cellhd->south > win.south)
27 cellhd->south = win.south;
28 if (cellhd->west > win.west)
29 cellhd->west = win.west;
30 if (cellhd->east < win.east)
31 cellhd->east = win.east;
32 /* min resolution */
33 if (cellhd->ns_res > win.ns_res)
34 cellhd->ns_res = win.ns_res;
35 if (cellhd->ew_res > win.ew_res)
36 cellhd->ew_res = win.ew_res;
37 }
38 }
39
40 /* if the north-south is not multiple of the resolution,
41 * round the south downward
42 */
43 cellhd->rows = (cellhd->north - cellhd->south) / cellhd->ns_res + 0.5;
44 cellhd->south = cellhd->north - cellhd->rows * cellhd->ns_res;
45
46 /* do the same for the west */
47 cellhd->cols = (cellhd->east - cellhd->west) / cellhd->ew_res + 0.5;
48 cellhd->west = cellhd->east - cellhd->cols * cellhd->ew_res;
49
50 return 1;
51 }
52
53 int georef_window(struct Ortho_Image_Group *group, struct Cell_head *w1,
54 struct Cell_head *w2, double res)
55 {
56 double n, e, z1, ad;
57 double n0, e0;
58 double aver_z;
59 struct _corner {
60 double n, e;
61 } nw, ne, se, sw;
62
63 /* get an average elevation from the active control points */
64 /* for mountainous areas this is a very rough approximation
65 * which would become more accurate only if actual elevation
66 * values are used */
67 get_aver_elev(&group->control_points, &aver_z);
68 G_debug(1, "Aver elev = %f", aver_z);
69
70 /* compute ortho ref of all corners */
71
72 I_georef(w1->west, w1->north, &e0, &n0, group->E12, group->N12, 1);
73 I_inverse_ortho_ref(e0, n0, aver_z, &e, &n, &z1, &group->camera_ref,
74 group->XC, group->YC, group->ZC, group->MI);
75
76 G_debug(1, "NORTH WEST CORNER");
77 G_debug(1, "group->E12 = %f %f %f,", group->E12[0], group->E12[1],
78 group->E12[2]);
79 G_debug(1, "group->N12 = %f %f %f,", group->N12[0], group->N12[1],
80 group->N12[2]);
81 G_debug(1, "image x = %f y = %f, photo x = %f y = %f", w1->west, w1->north,
82 e0, n0);
83 G_debug(1, "target x = %f y = %f", e, n);
84
85 w2->north = w2->south = n;
86 w2->west = w2->east = e;
87 nw.n = n;
88 nw.e = e;
89
90 I_georef(w1->east, w1->north, &e0, &n0, group->E12, group->N12, 1);
91 I_inverse_ortho_ref(e0, n0, aver_z, &e, &n, &z1, &group->camera_ref,
92 group->XC, group->YC, group->ZC, group->MI);
93
94 G_debug(1, "NORTH EAST CORNER");
95 G_debug(1, "image x = %f y = %f, photo x = %f y = %f", w1->east, w1->north,
96 e0, n0);
97 G_debug(1, "target x = %f y = %f", e, n);
98
99 ne.n = n;
100 ne.e = e;
101 if (n > w2->north)
102 w2->north = n;
103 if (n < w2->south)
104 w2->south = n;
105 if (e > w2->east)
106 w2->east = e;
107 if (e < w2->west)
108 w2->west = e;
109
110 I_georef(w1->west, w1->south, &e0, &n0, group->E12, group->N12, 1);
111 I_inverse_ortho_ref(e0, n0, aver_z, &e, &n, &z1, &group->camera_ref,
112 group->XC, group->YC, group->ZC, group->MI);
113
114 G_debug(1, "SOUTH WEST CORNER");
115 G_debug(1, "image x = %f y = %f, photo x = %f y = %f", w1->west, w1->south,
116 e0, n0);
117 G_debug(1, "target x = %f y = %f", e, n);
118
119 sw.n = n;
120 sw.e = e;
121 if (n > w2->north)
122 w2->north = n;
123 if (n < w2->south)
124 w2->south = n;
125 if (e > w2->east)
126 w2->east = e;
127 if (e < w2->west)
128 w2->west = e;
129
130 I_georef(w1->east, w1->south, &e0, &n0, group->E12, group->N12, 1);
131 I_inverse_ortho_ref(e0, n0, aver_z, &e, &n, &z1, &group->camera_ref,
132 group->XC, group->YC, group->ZC, group->MI);
133
134 G_debug(1, "SOUTH EAST CORNER");
135 G_debug(1, "image x = %f y = %f, photo x = %f y = %f", w1->east, w1->south,
136 e0, n0);
137 G_debug(1, "target x = %f y = %f", e, n);
138
139 se.n = n;
140 se.e = e;
141 if (n > w2->north)
142 w2->north = n;
143 if (n < w2->south)
144 w2->south = n;
145 if (e > w2->east)
146 w2->east = e;
147 if (e < w2->west)
148 w2->west = e;
149
150 /* resolution */
151 if (res > 0)
152 w2->ew_res = w2->ns_res = res;
153 else {
154 /* this results in ugly res values, and ns_res != ew_res */
155 /* and is no good for rotation */
156 /*
157 w2->ns_res = (w2->north - w2->south) / w1->rows;
158 w2->ew_res = (w2->east - w2->west) / w1->cols;
159 */
160
161 /* alternative: account for rotation and order > 1 */
162
163 /* N-S extends along western and eastern edge */
164 w2->ns_res = (sqrt((nw.n - sw.n) * (nw.n - sw.n) +
165 (nw.e - sw.e) * (nw.e - sw.e)) +
166 sqrt((ne.n - se.n) * (ne.n - se.n) +
167 (ne.e - se.e) * (ne.e - se.e))) /
168 (2.0 * w1->rows);
169
170 /* E-W extends along northern and southern edge */
171 w2->ew_res = (sqrt((nw.n - ne.n) * (nw.n - ne.n) +
172 (nw.e - ne.e) * (nw.e - ne.e)) +
173 sqrt((sw.n - se.n) * (sw.n - se.n) +
174 (sw.e - se.e) * (sw.e - se.e))) /
175 (2.0 * w1->cols);
176
177 /* make ew_res = ns_res */
178 w2->ns_res = (w2->ns_res + w2->ew_res) / 2.0;
179 w2->ew_res = w2->ns_res;
180
181 /* nice round values */
182 if (w2->ns_res > 1) {
183 if (w2->ns_res < 10) {
184 /* round to first decimal */
185 w2->ns_res = (int)(w2->ns_res * 10 + 0.5) / 10.0;
186 w2->ew_res = w2->ns_res;
187 }
188 else {
189 /* round to integer */
190 w2->ns_res = (int)(w2->ns_res + 0.5);
191 w2->ew_res = w2->ns_res;
192 }
193 }
194 }
195
196 /* adjust extents */
197 ad = w2->north > 0 ? 0.5 : -0.5;
198 w2->north = (int)(ceil(w2->north / w2->ns_res) + ad) * w2->ns_res;
199 ad = w2->south > 0 ? 0.5 : -0.5;
200 w2->south = (int)(floor(w2->south / w2->ns_res) + ad) * w2->ns_res;
201 ad = w2->east > 0 ? 0.5 : -0.5;
202 w2->east = (int)(ceil(w2->east / w2->ew_res) + ad) * w2->ew_res;
203 ad = w2->west > 0 ? 0.5 : -0.5;
204 w2->west = (int)(floor(w2->west / w2->ew_res) + ad) * w2->ew_res;
205
206 w2->rows = (w2->north - w2->south + w2->ns_res / 2.0) / w2->ns_res;
207 w2->cols = (w2->east - w2->west + w2->ew_res / 2.0) / w2->ew_res;
208
209 G_debug(1, "FINAL");
210 G_debug(1, "east = %f \n west = %f \n north = %f \n south = %f", w2->east,
211 w2->west, w2->north, w2->south);
212 G_debug(1, "RESOLUTION");
213 G_debug(1, "EW = %f", w2->ew_res);
214 G_debug(1, "NS = %f", w2->ns_res);
215
216 return 0;
217 }