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

« back to all changes in this revision

Viewing changes to imagery/i.ortho.photo/i.photo.2target/analyze.c

  • 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
 
/* analyze.c */
2
 
#include <math.h>
3
 
#include <unistd.h>
4
 
#include <string.h>
5
 
#include <grass/raster.h>
6
 
#include "globals.h"
7
 
#include "local_proto.h"
8
 
 
9
 
#define NLINES 18
10
 
struct box
11
 
{
12
 
    int top, bottom, left, right;
13
 
};
14
 
 
15
 
static int which;
16
 
static struct box more, less, report;
17
 
static int height, size, edge, nlines;
18
 
static int curp, first_point, i;
19
 
static double rms;
20
 
static double *xres, *yres, *gnd;
21
 
static int pager;
22
 
static int xmax, ymax, gmax;
23
 
static char buf[300];
24
 
static int uparrow(struct box *, int);
25
 
static int downarrow(struct box *, int);
26
 
static int pick(int, int);
27
 
static int done(void);
28
 
static int cancel_which(void);
29
 
static int inbox(struct box *, int, int);
30
 
static int dotext(char *, int, int, int, int, int, int);
31
 
static int compute_transformation(void);
32
 
static int debug(char *);
33
 
static int to_file(void);
34
 
static int askfile(void);
35
 
static int to_printer(void);
36
 
static int do_report(FILE *);
37
 
static int printcentered(FILE *, char *, int);
38
 
static int show_point(int, int);
39
 
 
40
 
#define FMT0(buf,n) \
41
 
        sprintf (buf, "%3d ", n)
42
 
#define FMT1(buf,xres,yres,gnd) \
43
 
        sprintf (buf, "%5.1f %5.1f %6.1f ", xres,yres,gnd)
44
 
#define LHEAD1 "            error            "
45
 
#define LHEAD2 "  #   east  north   target   "
46
 
 
47
 
#define FMT2(buf,e1,n1,e2,n2,z2) \
48
 
        sprintf (buf, "%9.1f %9.1f %9.1f %9.1f %9.1f ", e1,n1,e2,n2,z2)
49
 
#define RHEAD1 "         image                   target            "
50
 
#define RHEAD2 "    east      north      east     north       elev."
51
 
 
52
 
#define BACKGROUND GREY
53
 
 
54
 
int analyze(void)
55
 
{
56
 
    static int use = 1;
57
 
    static Objects objects[] = {
58
 
        MENU("DONE", done, &use),
59
 
        MENU("PRINT", to_printer, &use),
60
 
        MENU("FILE", to_file, &use),
61
 
        INFO(" Double click on point to be included/excluded ", &use),
62
 
        OTHER(pick, &use),
63
 
        {0}
64
 
    };
65
 
 
66
 
    int color;
67
 
    int tsize;
68
 
    int cury;
69
 
    int len;
70
 
    int line;
71
 
    int top, bottom, left, right, width, middle, nums;
72
 
 
73
 
    /* to give user a response of some sort */
74
 
    Menu_msg("Preparing analysis ...");
75
 
 
76
 
    /*
77
 
     * build a popup window at center of the screen.
78
 
     * 35% the height and wide enough to hold the report
79
 
     *
80
 
     */
81
 
 
82
 
    /* height of 1 line, based on NLINES taking up 35% vertical space */
83
 
    height = (.35 * (SCREEN_BOTTOM - SCREEN_TOP)) / NLINES + 1;
84
 
 
85
 
    /* size of text, 80% of line height */
86
 
    tsize = .8 * height;
87
 
    size = tsize - 2;           /* fudge for computing pixels width of text */
88
 
 
89
 
    /* indent for the text */
90
 
    edge = .1 * height + 1;
91
 
 
92
 
    /* determine the length, in chars, of printed line */
93
 
    FMT0(buf, 0);
94
 
    nums = strlen(buf) * size;
95
 
    FMT1(buf, 0.0, 0.0, 0.0);
96
 
    len = strlen(buf);
97
 
    middle = len * size;
98
 
    FMT2(buf, 0.0, 0.0, 0.0, 0.0, 0.0);
99
 
    len += strlen(buf);
100
 
 
101
 
    /* width is for max chars plus sidecar for more/less */
102
 
    width = len * size + nums + 2 * height;
103
 
    if ((SCREEN_RIGHT - SCREEN_LEFT) < width)
104
 
        width = SCREEN_RIGHT - SCREEN_LEFT;
105
 
 
106
 
 
107
 
    /* define the window */
108
 
    bottom = VIEW_MENU->top - 1;
109
 
    top = bottom - height * NLINES;
110
 
 
111
 
 
112
 
    left = SCREEN_LEFT;
113
 
    right = left + width;
114
 
    middle += left + nums;
115
 
    nums += left;
116
 
 
117
 
    /* save what is under this area, so it can be restored */
118
 
    R_panel_save(tempfile1, top, bottom+1, left, right+1);
119
 
 
120
 
 
121
 
    /* fill it with white */
122
 
    R_standard_color(BACKGROUND);
123
 
    R_box_abs(left, top, right, bottom);
124
 
 
125
 
    right -= 2 * height;        /* reduce it to exclude sidecar */
126
 
 
127
 
    /* print messages in message area */
128
 
    R_text_size(tsize, tsize);
129
 
 
130
 
 
131
 
    /* setup the more/less boxes in the sidecar */
132
 
    R_standard_color(BLACK);
133
 
    less.top = top;
134
 
    less.bottom = top + 2 * height;
135
 
    less.left = right;
136
 
    less.right = right + 2 * height;
137
 
    Outline_box(less.top, less.bottom, less.left, less.right);
138
 
 
139
 
    more.top = bottom - 2 * height;
140
 
    more.bottom = bottom;
141
 
    more.left = right;
142
 
    more.right = right + 2 * height;
143
 
    Outline_box(more.top, more.bottom, more.left, more.right);
144
 
 
145
 
    /*
146
 
     * top two lines are for column labels
147
 
     * last two line is for overall rms error.
148
 
     */
149
 
    nlines = NLINES - 3;
150
 
    first_point = 0;
151
 
 
152
 
    /* allocate predicted values */
153
 
    xres = (double *)G_calloc(group.control_points.count, sizeof(double));
154
 
    yres = (double *)G_calloc(group.control_points.count, sizeof(double));
155
 
    gnd = (double *)G_calloc(group.control_points.count, sizeof(double));
156
 
 
157
 
    /* compute transformation for the first time */
158
 
    compute_transformation();
159
 
 
160
 
 
161
 
    /* put head on the report */
162
 
    cury = top;
163
 
    dotext(LHEAD1, cury, cury + height, left, middle, 0, BLACK);
164
 
    dotext(RHEAD1, cury, cury + height, middle, right - 1, 0, BLACK);
165
 
    cury += height;
166
 
    dotext(LHEAD2, cury, cury + height, left, middle, 0, BLACK);
167
 
    dotext(RHEAD2, cury, cury + height, middle, right - 1, 0, BLACK);
168
 
    cury += height;
169
 
    R_move_abs(left, cury - 1);
170
 
    R_cont_abs(right, cury - 1);
171
 
 
172
 
    /* isolate the sidecar */
173
 
    R_move_abs(right, top);
174
 
    R_cont_abs(right, bottom);
175
 
 
176
 
    /* define report box */
177
 
    report.top = cury;
178
 
    report.left = left;
179
 
    report.right = right;
180
 
 
181
 
    /* lets do it */
182
 
    pager = 1;
183
 
    while (1) {
184
 
        R_text_size(tsize, tsize);
185
 
        line = 0;
186
 
        curp = first_point;
187
 
        cury = top + 2 * height;
188
 
        while (1) {
189
 
            if (line >= nlines || curp >= group.control_points.count)
190
 
                break;
191
 
            line++;
192
 
            if (group.con_equation_stat > 0 &&
193
 
                group.control_points.status[curp] > 0) {
194
 
                color = BLACK;
195
 
                FMT1(buf, xres[curp], yres[curp], gnd[curp]);
196
 
                if (curp == xmax || curp == ymax || curp == gmax)
197
 
                    color = RED;
198
 
                dotext(buf, cury, cury + height, nums, middle, 0, color);
199
 
            }
200
 
            else if (group.control_points.status[curp] > 0)
201
 
                dotext("?", cury, cury + height, nums, middle, 1, BLACK);
202
 
            else
203
 
                dotext("not used", cury, cury + height, nums, middle, 1,
204
 
                       BLACK);
205
 
            if (pager) {
206
 
                FMT0(buf, curp + 1);
207
 
                dotext(buf, cury, cury + height, left, nums, 0, BLACK);
208
 
                FMT2(buf,
209
 
                     group.control_points.e1[curp],
210
 
                     group.control_points.n1[curp],
211
 
                     group.control_points.e2[curp],
212
 
                     group.control_points.n2[curp],
213
 
                     group.control_points.z2[curp]);
214
 
                dotext(buf, cury, cury + height, middle, right - 1, 0, BLACK);
215
 
            }
216
 
            cury += height;
217
 
            curp++;
218
 
        }
219
 
        report.bottom = cury;
220
 
        downarrow(&more, curp < group.control_points.count ? BLACK :
221
 
                  BACKGROUND);
222
 
        uparrow(&less, first_point > 0 ? BLACK : BACKGROUND);
223
 
        R_standard_color(BACKGROUND);
224
 
        R_box_abs(left, cury, right - 1, bottom);
225
 
        if (group.con_equation_stat < 0) {
226
 
            color = RED;
227
 
            strcpy(buf, "Poorly placed control points");
228
 
        }
229
 
        else if (group.con_equation_stat == 0) {
230
 
            color = RED;
231
 
            strcpy(buf, "No active control points");
232
 
        }
233
 
        else {
234
 
            color = BLACK;
235
 
            sprintf(buf, "Overall rms error: %.2f", rms);
236
 
        }
237
 
        dotext(buf, bottom - height, bottom, left, right - 1, 0, color);
238
 
        R_standard_color(BLACK);
239
 
        R_move_abs(left, bottom - height);
240
 
        R_cont_abs(right - 1, bottom - height);
241
 
 
242
 
        pager = 0;
243
 
        which = -1;
244
 
        if (Input_pointer(objects) < 0)
245
 
            break;
246
 
    }
247
 
 
248
 
    /* all done. restore what was under the window */
249
 
    right += 2 * height;        /* move it back over the sidecar */
250
 
    R_standard_color(BACKGROUND);
251
 
    R_box_abs(left, top, right, bottom);
252
 
    R_panel_restore(tempfile1);
253
 
    R_panel_delete(tempfile1);
254
 
    R_flush();
255
 
 
256
 
    G_free(xres);
257
 
    G_free(yres);
258
 
    G_free(gnd);
259
 
    for (i = 0; i < group.control_points.count; i++)
260
 
        group.control_points.status[i] = group.control_points.status[i];
261
 
    I_put_con_points(group.name, &group.control_points);
262
 
    display_conz_points(1);
263
 
    return 0;                   /* return but don't QUIT */
264
 
}
265
 
 
266
 
static int uparrow(struct box *box, int color)
267
 
{
268
 
    R_standard_color(color);
269
 
    Uparrow(box->top + edge, box->bottom - edge, box->left + edge,
270
 
            box->right - edge);
271
 
 
272
 
    return 0;
273
 
}
274
 
 
275
 
static int downarrow(struct box *box, int color)
276
 
{
277
 
    R_standard_color(color);
278
 
    Downarrow(box->top + edge, box->bottom - edge, box->left + edge,
279
 
              box->right - edge);
280
 
 
281
 
    return 0;
282
 
}
283
 
 
284
 
static int pick(int x, int y)
285
 
{
286
 
    int n;
287
 
    int cur;
288
 
 
289
 
    cur = which;
290
 
    cancel_which();
291
 
    if (inbox(&more, x, y)) {
292
 
        if (curp >= group.control_points.count)
293
 
            return 0;
294
 
        first_point = curp;
295
 
        pager = 1;
296
 
        return 1;
297
 
    }
298
 
    if (inbox(&less, x, y)) {
299
 
        if (first_point == 0)
300
 
            return 0;
301
 
        first_point -= nlines;
302
 
        if (first_point < 0)
303
 
            first_point = 0;
304
 
        pager = 1;
305
 
        return 1;
306
 
    }
307
 
    if (!inbox(&report, x, y)) {
308
 
        return 0;
309
 
    }
310
 
 
311
 
    n = (y - report.top) / height;
312
 
    if (n == cur) {             /* second click! */
313
 
        group.control_points.status[first_point + n] =
314
 
            !group.control_points.status[first_point + n];
315
 
        Menu_msg("Computing equations...");
316
 
        compute_transformation();
317
 
        show_point(first_point + n, 1);
318
 
        return 1;
319
 
    }
320
 
 
321
 
    /* first click */
322
 
    which = n;
323
 
    show_point(first_point + n, 0);
324
 
    R_standard_color(RED);
325
 
    Outline_box((report.top + n * height)+1, report.top + (n + 1) * height,
326
 
                report.left, report.right - 1);
327
 
    R_flush();
328
 
    return 0;                   /* ignore first click */
329
 
 
330
 
}
331
 
 
332
 
static int done(void)
333
 
{
334
 
    cancel_which();
335
 
    return -1;
336
 
}
337
 
 
338
 
static int cancel_which(void)
339
 
{
340
 
    if (which >= 0) {
341
 
        R_standard_color(BACKGROUND);
342
 
        Outline_box((report.top + which * height)+1,
343
 
                    report.top + (which + 1) * height, report.left,
344
 
                    report.right - 1);
345
 
        show_point(first_point + which, 1);
346
 
    }
347
 
    which = -1;
348
 
 
349
 
    return 0;
350
 
}
351
 
 
352
 
static int inbox(struct box *box, int x, int y)
353
 
{
354
 
    return (x > box->left && x < box->right && y > box->top &&
355
 
            y < box->bottom);
356
 
}
357
 
 
358
 
static int dotext(char *text, int top, int bottom, int left, int right,
359
 
                  int centered, int color)
360
 
{
361
 
    R_standard_color(BACKGROUND);
362
 
    R_box_abs(left, top, right, bottom);
363
 
    R_standard_color(color);
364
 
    R_move_abs(left + 1 + edge, bottom - 1 - edge);
365
 
    if (centered)
366
 
        R_move_rel((right - left - strlen(text) * size) / 2, 0);
367
 
    R_set_window(top, bottom, left, right);     /* for text clipping */
368
 
    R_text(text);
369
 
    R_set_window(SCREEN_TOP, SCREEN_BOTTOM, SCREEN_LEFT, SCREEN_RIGHT);
370
 
 
371
 
    return 0;
372
 
}
373
 
 
374
 
static int compute_transformation(void)
375
 
{
376
 
    int n, count;
377
 
    double d, d1, d2, sum;
378
 
    double e1, n1, z1;
379
 
    double xval, yval, gval;
380
 
 
381
 
    G_debug(2, "INVERSE ORTHO:\n");
382
 
 
383
 
    xmax = ymax = gmax = 0;
384
 
    xval = yval = gval = 0.0;
385
 
 
386
 
    Compute_ortho_equation();
387
 
    if (group.con_equation_stat <= 0)
388
 
        return 1;
389
 
 
390
 
    /* compute the row,col error plus ground error  keep track of largest 
391
 
       and second largest error */
392
 
    sum = 0.0;
393
 
    rms = 0.0;
394
 
    count = 0;
395
 
    for (n = 0; n < group.control_points.count; n++) {
396
 
        if (group.control_points.status[n] <= 0)
397
 
            continue;
398
 
        count++;
399
 
 
400
 
        G_debug(2, "Photo coordinates \te1 = %f \tn1 = %f \tz2 = %f",
401
 
                group.control_points.e1[n], group.control_points.n1[n],
402
 
                group.control_points.z2[n]);
403
 
        G_debug(2, "Camera \txc = %f \tyc = %f \tzc = %f",
404
 
                group.XC, group.YC, group.ZC);
405
 
        G_debug(2, "       \tomega = %f \tphi = %f \tkappa = %f",
406
 
                group.omega, group.phi, group.kappa);
407
 
 
408
 
        I_inverse_ortho_ref(temp_points.e1[n],
409
 
                            temp_points.n1[n],
410
 
                            temp_points.z2[n],
411
 
                            &e1, &n1, &z1,
412
 
                            &group.camera_ref,
413
 
                            group.XC, group.YC, group.ZC, group.MI);
414
 
 
415
 
/*****
416
 
        I_inverse_ortho_ref    (group.control_points.e1[n], 
417
 
                                group.control_points.n1[n], 
418
 
                                group.control_points.z2[n], 
419
 
                                &e1, &n1, &z1, 
420
 
                                &group.camera_ref, 
421
 
                                group.XC, group.YC, group.ZC, 
422
 
                                group.omega, group.phi, group.kappa);
423
 
*****/
424
 
 
425
 
        G_debug(2, "Ground coordinates \tx = %f \ty = %f", e1, n1);
426
 
 
427
 
        d = e1 - group.control_points.e2[n];
428
 
        xres[n] = d;
429
 
        if (d < 0)
430
 
            d = -d;
431
 
        if (d > xval) {
432
 
            xmax = n;
433
 
            xval = d;
434
 
        }
435
 
 
436
 
        d = n1 - group.control_points.n2[n];
437
 
        yres[n] = d;
438
 
        if (d < 0)
439
 
            d = -d;
440
 
        if (d > yval) {
441
 
            ymax = n;
442
 
            yval = d;
443
 
        }
444
 
 
445
 
        /* compute target error (ie along diagonal) */
446
 
        d1 = e1 - group.control_points.e2[n];
447
 
        d2 = n1 - group.control_points.n2[n];
448
 
        d = d1 * d1 + d2 * d2;
449
 
        sum += d;               /* add it to rms sum, before taking sqrt */
450
 
        d = sqrt(d);
451
 
        gnd[n] = d;
452
 
        if (d > gval) {         /* is this one the max? */
453
 
            gmax = n;
454
 
            gval = d;
455
 
        }
456
 
    }
457
 
 
458
 
    /* compute overall rms error */
459
 
    if (count)
460
 
        rms = sqrt(sum / count);
461
 
 
462
 
    return 0;
463
 
}
464
 
 
465
 
static int debug(char *msg)
466
 
{
467
 
    R_stabilize();
468
 
    Curses_write_window(PROMPT_WINDOW, 1, 1, msg);
469
 
    Curses_getch(0);
470
 
 
471
 
    return 0;
472
 
}
473
 
 
474
 
static int to_file(void)
475
 
{
476
 
    FILE *fd;
477
 
    char msg[1024];
478
 
 
479
 
    cancel_which();
480
 
    if (Input_other(askfile, "Keyboard") < 0) {
481
 
        return 0;
482
 
    }
483
 
 
484
 
    fd = fopen(buf, "w");
485
 
    if (fd == NULL) {
486
 
        sprintf(msg, "** Unable to create file %s\n", buf);
487
 
        Beep();
488
 
        Curses_write_window(PROMPT_WINDOW, 2, 1, msg);
489
 
    }
490
 
    else {
491
 
        do_report(fd);
492
 
        fclose(fd);
493
 
        sprintf(msg, "Report saved in file %s\n", buf);
494
 
        Curses_write_window(PROMPT_WINDOW, 2, 1, msg);
495
 
    }
496
 
    return 0;
497
 
}
498
 
 
499
 
static int askfile(void)
500
 
{
501
 
    char file[100];
502
 
 
503
 
    while (1) {
504
 
        Curses_prompt_gets("Enter file to hold report: ", file);
505
 
        G_strip(file);
506
 
        if (*file == 0)
507
 
            return -1;
508
 
        if (G_index(file, '/'))
509
 
            strcpy(buf, file);
510
 
        else
511
 
            sprintf(buf, "%s/%s", G_home(), file);
512
 
        if (access(buf, 0) != 0)
513
 
            return 1;
514
 
        sprintf(buf, "** %s already exists. choose another file", file);
515
 
        Beep();
516
 
        Curses_write_window(PROMPT_WINDOW, 2, 1, buf);
517
 
    }
518
 
 
519
 
    return 0;
520
 
}
521
 
 
522
 
static int to_printer(void)
523
 
{
524
 
    FILE *fd;
525
 
 
526
 
    cancel_which();
527
 
    Menu_msg("sending report to printer ...");
528
 
 
529
 
    fd = popen("lpr", "w");
530
 
    do_report(fd);
531
 
    pclose(fd);
532
 
    return 0;
533
 
}
534
 
 
535
 
static int do_report(FILE * fd)
536
 
{
537
 
    char buf[100];
538
 
    int n;
539
 
    int width;
540
 
 
541
 
    fprintf(fd, "LOCATION: %-20s GROUP: %-20s MAPSET: %s\n\n",
542
 
            G_location(), group.name, G_mapset());
543
 
    fprintf(fd, "%15sAnalysis of control point registration\n\n", "");
544
 
    fprintf(fd, "%s   %s\n", LHEAD1, RHEAD1);
545
 
    fprintf(fd, "%s   %s\n", LHEAD2, RHEAD2);
546
 
 
547
 
    FMT1(buf, 0.0, 0.0, 0.0);
548
 
    width = strlen(buf);
549
 
 
550
 
    for (n = 0; n < group.control_points.count; n++) {
551
 
        FMT0(buf, n + 1);
552
 
        fprintf(fd, "%s", buf);
553
 
        if (group.con_equation_stat > 0 && group.control_points.status[n] > 0) {
554
 
            FMT1(buf, xres[n], yres[n], gnd[n]);
555
 
            fprintf(fd, "%s", buf);
556
 
        }
557
 
        else if (group.control_points.status[n] > 0)
558
 
            printcentered(fd, "?", width);
559
 
        else
560
 
            printcentered(fd, "not used", width);
561
 
        FMT2(buf,
562
 
             group.control_points.e1[n],
563
 
             group.control_points.n1[n],
564
 
             group.control_points.e2[n],
565
 
             group.control_points.n2[n], group.control_points.z2[n]);
566
 
        fprintf(fd, "   %s\n", buf);
567
 
    }
568
 
    fprintf(fd, "\n");
569
 
    if (group.con_equation_stat < 0)
570
 
        fprintf(fd, "Poorly place control points\n");
571
 
    else if (group.con_equation_stat == 0)
572
 
        fprintf(fd, "No active control points\n");
573
 
    else
574
 
        fprintf(fd, "Overall rms error: %.2f\n", rms);
575
 
 
576
 
    return 0;
577
 
}
578
 
 
579
 
static int printcentered(FILE * fd, char *buf, int width)
580
 
{
581
 
    int len;
582
 
    int n;
583
 
    int i;
584
 
 
585
 
    len = strlen(buf);
586
 
    n = (width - len) / 2;
587
 
 
588
 
    for (i = 0; i < n; i++)
589
 
        fprintf(fd, " ");
590
 
    fprintf(fd, "%s", buf);
591
 
    i += len;
592
 
    while (i++ < width)
593
 
        fprintf(fd, " ");
594
 
 
595
 
    return 0;
596
 
}
597
 
 
598
 
static int show_point(int n, int true_color)
599
 
{
600
 
    if (!true_color)
601
 
        R_standard_color(ORANGE);
602
 
    else if (group.control_points.status[n])
603
 
        R_standard_color(GREEN);
604
 
    else
605
 
        R_standard_color(RED);
606
 
    display_one_point(VIEW_MAP1, group.control_points.e1[n],
607
 
                      group.control_points.n1[n]);
608
 
 
609
 
    return 0;
610
 
}