Debian Codegraph @

imagery/i.ortho.photo/lib/orthoref.c

raw symbols SCIP
1 /****************************************************************************
2 *
3 * MODULE: orthophoto rectification program
4 * AUTHOR(S): Mike Baba of DBA Systems, Fairfax, VA for CERL
5 * PURPOSE: ortho-rectification of aerial photographs
6 * COPYRIGHT: (C) 1999 by the GRASS Development Team
7 *
8 * This program is free software under the GNU General Public
9 * License (>=v2). Read the file COPYING that comes with GRASS
10 * for details.
11 *
12 *****************************************************************************/
13
14 /* orthoref.c */
15
16 /***********************************************************************
17 * I_compute_ortho_equations()
18 * I_orthoref()
19 * I_inverse_orthoref()
20 ***********************************************************************/
21 /*
22 Main algorithm reference:
23
24 Elements of Photogrammetry, With Air Photo Interpretation and Remote Sensing
25 by Paul R. Wolf, 562 pages
26 Publisher: McGraw Hill Text; 2nd edition (January 1983)
27 */
28
29 #include <signal.h>
30 #include <stdio.h>
31 #include <math.h>
32 #include <grass/imagery.h>
33 #include <grass/glocale.h>
34 #include "orthophoto.h"
35 #include "matrixdefs.h"
36 #include "local_proto.h"
37
38 #define MAX_ITERS 1000 /* Max iteration is normal equation solution */
39 #define CONV_LIMIT 0.1 /* meters */
40
41 #ifdef DEBUG
42 FILE *debug;
43 char msg[120];
44 #endif
45
46 static int panorama = 0;
47 static double ellps_a = 0;
48
49 /* enable panorama camera correction,
50 * e.g. for CORONA KH-4A/B */
51 void I_ortho_panorama(void)
52 {
53 panorama = 1;
54 }
55
56 /* Compute the ortho rectification parameters */
57 /* Camera position: XC, YC, ZC */
58 /* Camera attitude: Omega, Phi, Kappa */
59 int I_compute_ortho_equations(struct Ortho_Control_Points *cpz,
60 struct Ortho_Camera_File_Ref *cam_info,
61 struct Ortho_Camera_Exp_Init *init_info,
62 double *XC, double *YC, double *ZC, double *Omega,
63 double *Phi, double *Kappa, MATRIX *MO,
64 MATRIX *MT)
65 {
66 MATRIX delta, epsilon, B, BT, C, E, N, CC, NN, UVW, XYZ, M, WT1;
67 double x, y, /* z, */ X, Y, Z, Xp, Yp, CFL;
68 double lam, mu, nu, U, V, W;
69 double xbar, ybar;
70 double m11, m12, m13, m21, m22, m23, m31, m32, m33;
71 double sw, cw, sp, cp, sk, ck;
72 double dx, dy, dz, dd;
73 double Q1;
74 double kappa1, kappa2, XC_var, YC_var, ZC_var;
75 double omega_var, phi_var, kappa_var;
76 int i, iter, max_iters, n;
77 int first, active;
78 double e_a, e2;
79
80 Q1 = (double)1.0;
81
82 if (!G_get_ellipsoid_parameters(&e_a, &e2))
83 G_fatal_error(_("No ellpsoid parameters available"));
84 ellps_a = e_a;
85
86 /* DEBUG */
87 #ifdef DEBUG
88 debug = fopen("ortho_compute.rst", "w");
89 if (debug == NULL) {
90 snprintf(msg, sizeof(msg),
91 "Cannot open debug file ortho_compute.rst\n");
92 G_fatal_error(msg);
93 }
94 #endif
95
96 /* Need at least 4 active control points,
97 * or user-provided camera position */
98 active = 0;
99 if (cpz) {
100 for (i = 0; i < cpz->count; i++) {
101 if (cpz->status[i] > 0)
102 active++;
103 }
104 }
105
106 /* Initialize (and zero out) all matrices needed */
107 /* Format is delta = [XC,YC,ZC,Omega,Phi,Kappa]-transpose */
108
109 /* Normal Equation Matrix */
110 N.nrows = 6;
111 N.ncols = 6;
112 zero(&N);
113 /* Sum of Normal Equation Matrix */
114 NN.nrows = 6;
115 NN.ncols = 6;
116 zero(&NN);
117 /* Partial derivates of observation equations */
118 B.nrows = 2;
119 B.ncols = 6;
120 zero(&B);
121 /* Transpose of B */
122 BT.nrows = 6;
123 BT.ncols = 2;
124 zero(&BT);
125 /* Partial solution matrix */
126 C.nrows = 6;
127 C.ncols = 1;
128 zero(&C);
129 /* Sum of Partial solution matrix */
130 CC.nrows = 6;
131 CC.ncols = 1;
132 zero(&CC);
133 /* Residual matrix */
134 E.nrows = 2;
135 E.ncols = 1;
136 zero(&E);
137 /* delta Matrix - [XC,YC,ZC,Omega,Phi,Kappa]-transpose */
138 delta.nrows = 6;
139 delta.ncols = 1;
140 zero(&delta);
141 /* corrections to delta matrix */
142 epsilon.nrows = 6;
143 epsilon.ncols = 1;
144 zero(&epsilon);
145 /* Object Space Coordinates (X,Y,Z) */
146 XYZ.nrows = 3;
147 XYZ.ncols = 1;
148 zero(&XYZ);
149 /* Image Space coordinates (u,v,w) */
150 UVW.nrows = 3;
151 UVW.ncols = 1;
152 zero(&UVW);
153 /* Orientation Matrix M=[3,3] functions of (omega,phi,kappa) */
154 M.nrows = 3;
155 M.ncols = 3;
156 zero(&M);
157 /* Weight Matrix for delta */
158 /* Weights set to identity matrix unless */
159 WT1.nrows = 6;
160 WT1.ncols = 6;
161 zero(&WT1);
162
163 /******************** Start the solution *****************************/
164
165 /* set Xp, Yp, and CFL from cam_info */
166 Xp = cam_info->Xp;
167 Yp = cam_info->Yp;
168 CFL = cam_info->CFL;
169
170 #ifdef DEBUG
171 fprintf(debug, "CAMERA INFO:\n");
172 fprintf(debug, "\txp = %f \typ = %f \tCFL = %f \n", Xp, Yp, CFL);
173 #endif
174
175 /* use initial estimates for XC,YC,ZC,omega,phi,kappa
176 * and initial standard deviations if provided by i.ortho.photo
177 *
178 * otherwise set from mean value of all active control points
179 * - init_info is generated by photo.init (file INIT_EXP)
180 * - init_info->status allows for disactivation of defined values
181 */
182 if ((init_info != NULL) && (init_info->status == 1)) {
183 G_verbose_message("Using provided values for XC,YC,ZC,omega,phi,kappa");
184 /* Have initial values */
185 *XC = init_info->XC_init;
186 *YC = init_info->YC_init;
187 *ZC = init_info->ZC_init;
188 *Omega = init_info->omega_init;
189 *Phi = init_info->phi_init;
190 *Kappa = init_info->kappa_init;
191
192 /* weight matrix computed from initial standard variances */
193 WT1.x[0][0] = (Q1 / (init_info->XC_var * init_info->XC_var));
194 WT1.x[1][1] = (Q1 / (init_info->YC_var * init_info->YC_var));
195 WT1.x[2][2] = (Q1 / (init_info->ZC_var * init_info->ZC_var));
196 WT1.x[3][3] = (Q1 / (init_info->omega_var * init_info->omega_var));
197 WT1.x[4][4] = (Q1 / (init_info->phi_var * init_info->phi_var));
198 WT1.x[5][5] = (Q1 / (init_info->kappa_var * init_info->kappa_var));
199 }
200 else if (active >= 4) { /* set from mean values of active control points */
201 double dist_grnd, dist_photo;
202 double meanx, meany, meanX, meanY, meanZ;
203
204 G_verbose_message("Estimating values for XC,YC,ZC,omega,phi,kappa");
205 /* set initial XC and YC from mean values of control points */
206 meanx = meany = meanX = meanY = meanZ = 0;
207 x = y = X = Y = 0;
208 n = 0;
209 first = 1;
210 for (i = 0; i < cpz->count; i++) {
211 if (cpz->status[i] <= 0)
212 continue;
213
214 /* set initial XC, YC */
215 if (first) {
216 X = cpz->e2[i];
217 x = cpz->e1[i];
218 Y = cpz->n2[i];
219 y = cpz->n1[i];
220 first = 0;
221 }
222 else {
223 /* set initial ZC from: */
224 /* scale ~= dist_photo/dist_grnd ~= (CFL)/(Z - ZC) */
225 /* ZC ~= Z + CFL(dist_grnd)/(dist_photo) */
226 dx = cpz->e1[i] - x;
227 dy = cpz->n1[i] - y;
228 dist_photo = sqrt(dx * dx + dy * dy);
229 dx = cpz->e2[i] - X;
230 dy = cpz->n2[i] - Y;
231 dist_grnd = sqrt(dx * dx + dy * dy);
232
233 if (dist_photo != 0 && dist_grnd != 0) {
234 meanZ += cpz->z2[i] + (CFL * (dist_grnd) / (dist_photo));
235 meanx += cpz->e1[i];
236 meany += cpz->n1[i];
237 meanX += cpz->e2[i];
238 meanY += cpz->n2[i];
239
240 n++;
241 }
242 }
243 }
244 if (!n) /* Poorly placed Control Points */
245 return -1;
246
247 meanx /= n;
248 meany /= n;
249 meanX /= n;
250 meanY /= n;
251 meanZ /= n;
252 *XC = meanX;
253 *YC = meanY;
254 *ZC = meanZ;
255
256 /* set initial rotations to zero (radians) */
257 *Omega = *Phi = 0.0;
258
259 /* find an initial kappa */
260 kappa1 = atan2((meany - y), (meanx - x));
261 kappa2 = atan2((meanY - Y), (meanX - X));
262 *Kappa = (kappa2 - kappa1);
263
264 /* set initial weights */
265 XC_var = INITIAL_X_VAR;
266 YC_var = INITIAL_Y_VAR;
267 ZC_var = INITIAL_Z_VAR;
268 omega_var = INITIAL_OMEGA_VAR;
269 phi_var = INITIAL_PHI_VAR;
270 kappa_var = INITIAL_KAPPA_VAR;
271
272 WT1.x[0][0] = (Q1 / (XC_var * XC_var));
273 WT1.x[1][1] = (Q1 / (YC_var * YC_var));
274 WT1.x[2][2] = (Q1 / (ZC_var * ZC_var));
275 WT1.x[3][3] = (Q1 / (omega_var * omega_var));
276 WT1.x[4][4] = (Q1 / (phi_var * phi_var));
277 WT1.x[5][5] = (Q1 / (kappa_var * kappa_var));
278 }
279 else {
280 G_warning(_("Camera position not available"));
281 #ifdef DEBUG
282 fclose(debug);
283 #endif
284 return (0);
285 }
286
287 G_verbose_message("INITIAL CAMERA POSITION:");
288 G_verbose_message("XC: %.2f, YC: %.2f, ZC: %.2f", *XC, *YC, *ZC);
289 G_verbose_message("Omega %.2f, Phi %.2f, Kappa: %.2f", *Omega * 180. / M_PI,
290 *Phi * 180. / M_PI, *Kappa * 180. / M_PI);
291
292 #ifdef DEBUG
293 fprintf(debug, "\nINITIAL CAMERA POSITION:\n");
294 fprintf(debug, "\tXC = %f \tYC = %f \tZC = %f \n", *XC, *YC, *ZC);
295 fprintf(debug, "\tOMEGA = %f \tPHI = %f \tKAPPA = %f \n", *Omega, *Phi,
296 *Kappa);
297 #endif
298
299 /* set initial parameters into epsilon matrix */
300 epsilon.x[0][0] = *XC;
301 epsilon.x[1][0] = *YC;
302 epsilon.x[2][0] = *ZC;
303 epsilon.x[3][0] = *Omega;
304 epsilon.x[4][0] = *Phi;
305 epsilon.x[5][0] = *Kappa;
306
307 /* adjust camera position only if enough control points are available */
308 max_iters = MAX_ITERS;
309 if (active < 4)
310 max_iters = -1;
311
312 /************************** Start Iterations *****************************/
313 /* itererate until convergence */
314
315 for (iter = 0; iter < max_iters; iter++) {
316 /* break if converged */
317 dx = delta.x[0][0];
318 dy = delta.x[1][0];
319 dz = delta.x[2][0];
320 dd = ((dx * dx) + (dy * dy) + (dz * dz));
321
322 if ((iter > 0) && (dd <= CONV_LIMIT))
323 break;
324
325 #ifdef DEBUG
326 fprintf(debug, "\n\tITERATION = %d \n", iter);
327 #endif
328
329 /* value of parameters at this iteration */
330 *XC = epsilon.x[0][0];
331 *YC = epsilon.x[1][0];
332 *ZC = epsilon.x[2][0];
333 *Omega = epsilon.x[3][0];
334 *Phi = epsilon.x[4][0];
335 *Kappa = epsilon.x[5][0];
336
337 #ifdef DEBUG
338 fprintf(debug,
339 "\n\tepsilon:\n\t\tXC = \t%f \n\t\tYC = \t%f \n\t\tZC = \t%f "
340 "\n\t\tomega = \t%f \n\t\tphi = \t%f \n\t\tkappa = \t%f \n\n",
341 *XC, *YC, *ZC, *Omega, *Phi, *Kappa);
342 #endif
343
344 /* clear NN, CC */
345 zero(&NN);
346 zero(&CC);
347
348 /* Set Orientation Matrix from latest vales (Omega, Phi, Kappa); */
349 sw = sin(*Omega);
350 cw = cos(*Omega);
351 sp = sin(*Phi);
352 cp = cos(*Phi);
353 sk = sin(*Kappa);
354 ck = cos(*Kappa);
355
356 /* see WOLF 1983, Appendix, p. 591 */
357
358 /* ground -> photo */
359 M.x[0][0] = (cp * ck);
360 M.x[0][1] = (cw * sk) + (sw * sp * ck);
361 M.x[0][2] = (sw * sk) - (cw * sp * ck);
362 M.x[1][0] = (-cp * sk);
363 M.x[1][1] = (cw * ck) - (sw * sp * sk);
364 M.x[1][2] = (sw * ck) + (cw * sp * sk);
365 M.x[2][0] = sp;
366 M.x[2][1] = (-sw * cp);
367 M.x[2][2] = (cw * cp);
368
369 /* just an abbreviation of M */
370 m11 = M.x[0][0];
371 m12 = M.x[0][1];
372 m13 = M.x[0][2];
373 m21 = M.x[1][0];
374 m22 = M.x[1][1];
375 m23 = M.x[1][2];
376 m31 = M.x[2][0];
377 m32 = M.x[2][1];
378 m33 = M.x[2][2];
379
380 /* Form Normal equations by summation of all active control points */
381 for (i = 0; i < cpz->count; i++) {
382 #ifdef DEBUG
383 fprintf(debug, "\n\t\t\tIn Summation count = %d \n", i);
384 #endif
385
386 if (cpz->status[i] <= 0)
387 continue;
388
389 x = cpz->e1[i];
390 y = cpz->n1[i];
391 /* z = cpz->z1[i]; */
392 X = cpz->e2[i];
393 Y = cpz->n2[i];
394 Z = cpz->z2[i];
395
396 /* adjust for earth curvature */
397 dx = X - *XC;
398 dy = Y - *YC;
399 dd = (dx * dx) + (dy * dy);
400 Z -= dd / (2.0 * ellps_a);
401
402 #ifdef DEBUG
403 fprintf(debug,
404 "\n\t\t\timage:\n \t\t\tx = \t%f \n\t\t\ty = \t%f "
405 "\n\t\t\tz = \t%f \n\t\t\tobject:\n \t\t\tX = \t%f "
406 "\n\t\t\tY = \t%f \n\t\t\tZ = \t%f \n",
407 x, y, z, X, Y, Z);
408 #endif
409
410 /* Compute Obj. Space coordinates */
411 XYZ.x[0][0] = X - *XC;
412 XYZ.x[1][0] = Y - *YC;
413 XYZ.x[2][0] = Z - *ZC;
414
415 /* just an abbreviation */
416 lam = XYZ.x[0][0];
417 mu = XYZ.x[1][0];
418 nu = XYZ.x[2][0];
419
420 /* Compute image space coordinates */
421 m_mult(&M, &XYZ, &UVW);
422
423 /* just an abbreviation */
424 U = UVW.x[0][0];
425 V = UVW.x[1][0];
426 W = UVW.x[2][0];
427
428 /* panorama correction
429 * in theory either for U, V or for xbar, ybar
430 * U, V is recommended because U, V are only used
431 * for the residuals
432 * correcting U, V also gives a slightly smaller RMSE */
433 if (panorama) {
434 double a, epan;
435
436 epan = U;
437 if (epan < 0) {
438 a = atan2(-epan, -W);
439 epan = -a * -W;
440 }
441 else {
442 a = atan2(epan, -W);
443 epan = a * -W;
444 }
445 U = epan;
446
447 V *= cos(a);
448
449 UVW.x[0][0] = U;
450 UVW.x[1][0] = V;
451 }
452
453 /* Form Partial derivatives of Normal Equations */
454 xbar = x - Xp;
455 ybar = y - Yp;
456
457 B.x[0][0] = (-Q1 / W) * ((xbar * m31) + (CFL * m11));
458 B.x[0][1] = (-Q1 / W) * ((xbar * m32) + (CFL * m12));
459 B.x[0][2] = (-Q1 / W) * ((xbar * m33) + (CFL * m13));
460
461 B.x[0][3] = (Q1 / W) * ((xbar * ((-m33 * mu) + (m32 * nu))) +
462 (CFL * ((-m13 * mu) + (m12 * nu))));
463 B.x[0][4] =
464 (Q1 / W) *
465 ((xbar * ((cp * lam) + (sw * sp * mu) + (-cw * sp * nu))) +
466 (CFL * ((-sp * ck * lam) + (sw * cp * ck * mu) +
467 (-cw * cp * ck * nu))));
468 B.x[0][5] =
469 (Q1 / W) * (CFL * ((m21 * lam) + (m22 * mu) + (m23 * nu)));
470
471 B.x[1][0] = (-Q1 / W) * ((ybar * m31) + (CFL * m21));
472 B.x[1][1] = (-Q1 / W) * ((ybar * m32) + (CFL * m22));
473 B.x[1][2] = (-Q1 / W) * ((ybar * m33) + (CFL * m23));
474
475 B.x[1][3] = (Q1 / W) * ((ybar * ((-m33 * mu) + (m32 * nu))) +
476 (CFL * ((-m23 * mu) + (m22 * nu))));
477 B.x[1][4] =
478 (Q1 / W) *
479 ((ybar * ((cp * lam) + (sw * sp * mu) + (-cw * sp * nu))) +
480 (CFL * ((sp * sk * lam) + (-sw * cp * sk * mu) +
481 (cw * cp * sk * nu))));
482 B.x[1][5] =
483 (Q1 / W) * (CFL * ((-m11 * lam) + (-m12 * mu) + (-m13 * nu)));
484
485 /* residuals of image coordinates */
486 E.x[0][0] = (-Q1) * (xbar + (CFL * (U / W)));
487 E.x[1][0] = (-Q1) * (ybar + (CFL * (V / W)));
488
489 #ifdef DEBUG
490 fprintf(debug,
491 "\n\t\t\tresidual:\n \t\t\tE1 = \t%f \n\t\t\tE2 = \t%f \n",
492 E.x[0][0], E.x[1][0]);
493 #endif
494
495 /* do the summation into Normal equation and solution matrices */
496 /* Find B transpose */
497 transpose(&B, &BT);
498 /* N = BT*B */
499 m_mult(&BT, &B, &N);
500 /* NN += N */
501 m_add(&NN, &N, &NN);
502 /* C = BT*E */
503 m_mult(&BT, &E, &C);
504 /* CC += C */
505 m_add(&CC, &C, &CC);
506 } /* end summation loop over all active control points */
507
508 #ifdef DEBUG
509 fprintf(debug, "\n\tN: \n");
510 for (i = 0; i < 6; i++)
511 fprintf(debug, "\t%f \t%f \t%f \t%f \t%f \t%f \n", NN.x[i][0],
512 NN.x[i][1], NN.x[i][2], NN.x[i][3], NN.x[i][4], NN.x[i][5]);
513
514 fprintf(debug, "\n\tC: \n");
515 fprintf(debug, "\t%f \t%f \t%f \t%f \t%f \t%f \n", CC.x[0][0],
516 CC.x[1][0], CC.x[2][0], CC.x[3][0], CC.x[4][0], CC.x[5][0]);
517 #endif
518
519 /* Add weight matrix of unknowns to NN */
520 m_add(&NN, &WT1, &NN);
521 /* Solve for delta */
522
523 /* similar but not identical to solvemat in lib/imagery/georef.c */
524 if (!inverse(&NN, &N)) { /* control point status becomes zero if matrix
525 is non-invertable */
526 error("Matrix Inversion (Control Points status modified)");
527 for (i = 0; i < cpz->count; i++)
528 cpz->status[i] = 0;
529 return (0);
530 }
531
532 /* delta = N-1 * CC */
533 m_mult(&N, &CC, &delta);
534 /* epsilon += delta */
535 m_add(&epsilon, &delta, &epsilon);
536
537 #ifdef DEBUG
538 fprintf(debug,
539 "\ndelta:\n \n\t\tXC = \t%f \n\t\tYC = \t%f \n\t\tZC = \t%f "
540 "\n\t\tomega = \t%f \n\t\tphi = \t%f \n\t\tkappa = \t%f \n",
541 delta.x[0][0], delta.x[1][0], delta.x[2][0], delta.x[3][0],
542 delta.x[4][0], delta.x[5][0]);
543 #endif
544
545 } /* end ITERATION loop */
546 G_verbose_message("%d iterations to refine camera position", iter);
547
548 /* This is the solution */
549 *XC = epsilon.x[0][0];
550 *YC = epsilon.x[1][0];
551 *ZC = epsilon.x[2][0];
552 *Omega = epsilon.x[3][0];
553 *Phi = epsilon.x[4][0];
554 *Kappa = epsilon.x[5][0];
555
556 G_verbose_message("FINAL CAMERA POSITION:");
557 G_verbose_message("XC: %.2f, YC: %.2f, ZC: %.2f", *XC, *YC, *ZC);
558 G_verbose_message("Omega %.2f, Phi %.2f, Kappa: %.2f", *Omega * 180. / M_PI,
559 *Phi * 180. / M_PI, *Kappa * 180. / M_PI);
560
561 if (*ZC < 0)
562 G_warning(_("Potential BUG in ortholib: camera altitude < 0"));
563
564 /* Compute Orientation Matrix from Omega, Phi, Kappa */
565 /* ground -> photo */
566 sw = sin(*Omega);
567 cw = cos(*Omega);
568 sp = sin(*Phi);
569 cp = cos(*Phi);
570 sk = sin(*Kappa);
571 ck = cos(*Kappa);
572
573 MO->nrows = 3;
574 MO->ncols = 3;
575 zero(MO);
576
577 MO->x[0][0] = cp * ck;
578 MO->x[0][1] = cw * sk + (sw * sp * ck);
579 MO->x[0][2] = sw * sk - (cw * sp * ck);
580 MO->x[1][0] = -(cp * sk);
581 MO->x[1][1] = cw * ck - (sw * sp * sk);
582 MO->x[1][2] = sw * ck + (cw * sp * sk);
583 MO->x[2][0] = sp;
584 MO->x[2][1] = -(sw * cp);
585 MO->x[2][2] = cw * cp;
586
587 /* Compute Transposed Orientation Matrix from Omega, Phi, Kappa */
588 /* photo -> ground */
589
590 MT->nrows = 3;
591 MT->ncols = 3;
592 zero(MT);
593
594 /* Transposed Matrix */
595 MT->x[0][0] = cp * ck;
596 MT->x[1][0] = cw * sk + (sw * sp * ck);
597 MT->x[2][0] = sw * sk - (cw * sp * ck);
598 MT->x[0][1] = -(cp * sk);
599 MT->x[1][1] = cw * ck - (sw * sp * sk);
600 MT->x[2][1] = sw * ck + (cw * sp * sk);
601 MT->x[0][2] = sp;
602 MT->x[1][2] = -(sw * cp);
603 MT->x[2][2] = cw * cp;
604
605 #ifdef DEBUG
606 fclose(debug);
607 #endif
608
609 return (1);
610 }
611
612 /* given ground coordinates (e1,n1,z1) and the solution from above */
613 /* compute the photo coordinate (e2,n2) position */
614 int I_ortho_ref(double e1, double n1, double z1, double *e2, double *n2,
615 double *z2 G_UNUSED, struct Ortho_Camera_File_Ref *cam_info,
616 double XC, double YC, double ZC, MATRIX M)
617 {
618 MATRIX UVW, XYZ;
619 double U, V, W;
620 double /* Xp, Yp, */ CFL;
621 double dx, dy, dd;
622
623 /* Initialize and zero the matrices */
624 /* Object Space Coordinates */
625 XYZ.nrows = 3;
626 XYZ.ncols = 1;
627 zero(&XYZ);
628 /* Image Space coordinates */
629 UVW.nrows = 3;
630 UVW.ncols = 1;
631 zero(&UVW);
632
633 /************************ Start the work ******************************/
634 /* Set Xp, Yp, and CFL from cam_info */
635 /* Xp = cam_info->Xp; */
636 /* Yp = cam_info->Yp; */
637 CFL = cam_info->CFL;
638
639 /* adjust for earth curvature */
640 dx = e1 - XC;
641 dy = n1 - YC;
642 dd = (dx * dx) + (dy * dy);
643 z1 -= dd / (2.0 * ellps_a);
644
645 /* Object Space (&XYZ, XC,YC,ZC, X,Y,Z); */
646 XYZ.x[0][0] = e1 - XC;
647 XYZ.x[1][0] = n1 - YC;
648 XYZ.x[2][0] = z1 - ZC;
649
650 m_mult(&M, &XYZ, &UVW);
651
652 /* Image Space */
653 U = UVW.x[0][0];
654 V = UVW.x[1][0];
655 W = UVW.x[2][0];
656
657 /* panorama correction could also be done for e2, n2 below
658 * results are identical */
659 if (panorama) {
660 double a, epan;
661
662 epan = U;
663 if (epan < 0) {
664 a = atan2(-epan, -W);
665 epan = -a * -W;
666 }
667 else {
668 a = atan2(epan, -W);
669 epan = a * -W;
670 }
671 U = epan;
672
673 V *= cos(a);
674 }
675
676 /* This is the solution */
677 *e2 = (-CFL) * (U / W);
678 *n2 = (-CFL) * (V / W);
679
680 return (1);
681 }
682
683 /* given the photo coordinates (e1,n1) and elevation z2 */
684 /* and the solution from I_compute_ortho_equation */
685 /* compute ground position (e2,n2) */
686 int I_inverse_ortho_ref(double e1, double n1, double z1, double *e2, double *n2,
687 double *z2 G_UNUSED,
688 struct Ortho_Camera_File_Ref *cam_info, double XC,
689 double YC, double ZC, MATRIX M)
690 {
691 MATRIX UVW, XYZ;
692 double lam, mu, nu;
693 double Xp, Yp, CFL;
694
695 /* Initialize and zero matrices */
696 /* Object Space Coordinates */
697 XYZ.nrows = 3;
698 XYZ.ncols = 1;
699 zero(&XYZ);
700 /* Image Space coordinates */
701 UVW.nrows = 3;
702 UVW.ncols = 1;
703 zero(&UVW);
704
705 /********************** Start the work ********************************/
706 /* Set Xp, Yp, and CFL from cam_info */
707 Xp = cam_info->Xp;
708 Yp = cam_info->Yp;
709 CFL = cam_info->CFL;
710
711 /* Image Space */
712 UVW.x[0][0] = e1 - Xp;
713 UVW.x[1][0] = n1 - Yp;
714 UVW.x[2][0] = -CFL;
715
716 if (panorama) {
717 double a, epan;
718
719 epan = UVW.x[0][0];
720 if (epan < 0) {
721 a = -epan / CFL;
722 epan = -CFL * tan(a);
723 }
724 else {
725 a = epan / CFL;
726 epan = CFL * tan(a);
727 }
728 UVW.x[0][0] = epan;
729
730 UVW.x[1][0] /= cos(a);
731 }
732
733 m_mult(&M, &UVW, &XYZ);
734
735 /* Object Space */
736 lam = XYZ.x[0][0];
737 mu = XYZ.x[1][0];
738 nu = XYZ.x[2][0];
739
740 /* This is the solution */
741 *e2 = ((z1 - ZC) * (lam / nu)) + XC;
742 *n2 = ((z1 - ZC) * (mu / nu)) + YC;
743
744 return (1);
745 }
746
747 int matrix_error(char *s)
748 {
749 fprintf(stderr, "WARNING: %s", s);
750 #ifdef DEBUG
751 fclose(debug);
752 #endif
753 return 0;
754 }