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

« back to all changes in this revision

Viewing changes to display/d.profile/PlotProfile.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
 
/* PlotProfile.c
2
 
 *
3
 
 * function defined:
4
 
 *
5
 
 * PlotProfile(profile,letter,min,max)
6
 
 *
7
 
 * struct Profile profile;      - profile structure 
8
 
 * int min,                     - min cell-file value
9
 
 *     max;                     - max cell-file calue
10
 
 * 
11
 
 * PURPOSE: To plot a profile in the currently chosen on-screen window.
12
 
 * The profile's length is scaled to fit along the x-axis.  The profile
13
 
 * is scaled to fit the maximum and minimum cell-file values (instead of
14
 
 * the maximum and minimum profile values) on the y-axis. 
15
 
 *
16
 
 * NOTES: 
17
 
 *
18
 
 * 1) assumes that R_open_driver has already been called.
19
 
 *
20
 
 * 2) assumes that the profile structure has been both initialized by
21
 
 * a call to InitProfile, and filled with data by a call to ExtractProfile.
22
 
 *
23
 
 * Dave Johnson
24
 
 * DBA Systems, Inc.
25
 
 * 10560 Arrowhead Drive
26
 
 * Fairfax, Virginia 22030
27
 
 *
28
 
 */
29
 
 
30
 
#include <limits.h>
31
 
#include <grass/raster.h>
32
 
#include <grass/display.h>
33
 
#include "profile.h"
34
 
 
35
 
#define ORIGIN_X        0.13
36
 
#define ORIGIN_Y        0.07
37
 
#define YAXIS_END       0.77
38
 
#define XAXIS_END       0.95
39
 
#define TEXT_HEIGHT     0.11
40
 
#define TEXT_COLUMN     0.07
41
 
 
42
 
double _get_cat(UCAT *, int);
43
 
 
44
 
int PlotProfile(struct Profile profile, char *letter, int min, int max)
45
 
{
46
 
    struct ProfileNode *ptr;
47
 
    char txt_buf[512];
48
 
    int done;
49
 
    int text_width,
50
 
        text_height,
51
 
        i, t, b, l, r, tt, tb, tl, tr, height, width, x_line[3], y_line[3];
52
 
    double yoffset, xoffset, xscale, yscale;
53
 
 
54
 
    /* get current graphics window coordinates */
55
 
    D_get_screen_window(&t, &b, &l, &r);
56
 
    R_set_window(t, b, l, r);
57
 
 
58
 
    /* erase current graphics window to black */
59
 
    R_standard_color(D_translate_color(DEFAULT_BG_COLOR));
60
 
    D_erase_window();
61
 
 
62
 
    /* create axis lines */
63
 
    height = b - t;
64
 
    width = r - l;
65
 
    x_line[0] = x_line[1] = l + (int)(ORIGIN_X * width);
66
 
    x_line[2] = l + (int)(XAXIS_END * width);
67
 
    y_line[0] = b - (int)(YAXIS_END * height);
68
 
    y_line[1] = y_line[2] = b - (int)(ORIGIN_Y * height);
69
 
    R_standard_color(D_translate_color(DEFAULT_FG_COLOR));
70
 
 
71
 
    /* figure scaling factors and offsets for profile line */
72
 
    xscale = ((double)(x_line[2] - x_line[1]) / ((double)profile.count));
73
 
    yscale = ((double)(y_line[1] - y_line[0])) / ((double)(max - min));
74
 
    yoffset = (double)(y_line[1]);
75
 
    xoffset = (double)x_line[1];
76
 
 
77
 
    /* plot profile */
78
 
    ptr = profile.ptr;
79
 
    R_move_abs((int)xoffset, (int)yoffset);
80
 
    for (i = 0; i <= profile.count; i++) {
81
 
        if (ptr == NULL)
82
 
            break;
83
 
        if (xscale > 1) {
84
 
            R_cont_abs((int)(xoffset + xscale * i),
85
 
                       (int)(yoffset - yscale * _get_cat(&ptr->cat, min)));
86
 
            R_cont_abs((int)(xoffset + xscale * (i + 1.0)),
87
 
                       (int)(yoffset - yscale * _get_cat(&ptr->cat, min)));
88
 
        }
89
 
        else
90
 
            R_cont_abs((int)(xoffset + xscale * i),
91
 
                       (int)(yoffset - yscale * _get_cat(&ptr->cat, min)));
92
 
        ptr = ptr->next;
93
 
    }
94
 
    R_standard_color(D_translate_color("red"));
95
 
    R_polyline_abs(x_line, y_line, 3);
96
 
 
97
 
    /* loop until coordinate text is sized correctly to fit in window */
98
 
    text_height = TEXT_HEIGHT * (b - t);
99
 
    text_width = text_height * 0.8;
100
 
    R_standard_color(D_translate_color(DEFAULT_FG_COLOR));
101
 
 
102
 
    if (G_projection() == PROJECTION_LL)
103
 
        sprintf(txt_buf, "%s: From (%10.6f,%10.6f) to (%10.6f,%10.6f)",
104
 
            letter, profile.e1, profile.n1, profile.e2, profile.n2);
105
 
    else
106
 
        sprintf(txt_buf, "%s: From (%10.2f,%10.2f) to (%10.2f,%10.2f)",
107
 
            letter, profile.e1, profile.n1, profile.e2, profile.n2);
108
 
 
109
 
    done = 0;
110
 
    do {
111
 
        R_get_text_box(txt_buf, &tt, &tb, &tl, &tr);
112
 
        if ((tr - tl) >= (r - l)) {
113
 
            text_height *= 0.95;
114
 
            text_width *= 0.95;
115
 
            R_text_size(text_width, text_height);
116
 
        }
117
 
        else
118
 
            done = 1;
119
 
    }
120
 
    while (!done);
121
 
    R_move_abs((int)(l + 0.5 * (r - l) - .5 * (tr - tl)),
122
 
               (int)(t + .12 * (b - t)));
123
 
    R_text(txt_buf);
124
 
 
125
 
    /* set text size for y-axis labels */
126
 
    text_height = TEXT_HEIGHT * (b - t);
127
 
    text_width = text_height * 0.8;
128
 
    R_text_size(text_width, text_height);
129
 
 
130
 
    /* plot y-axis label (bottom) */
131
 
    sprintf(txt_buf, "%d", min);
132
 
    R_get_text_box(txt_buf, &tt, &tb, &tl, &tr);
133
 
    R_move_abs((int)(l + TEXT_COLUMN * (r - l) - .5 * (tr - tl)),
134
 
               (int)(yoffset + .5 * (tb - tt)));
135
 
    R_text(txt_buf);
136
 
 
137
 
    /* plot y-axis label (top) */
138
 
    sprintf(txt_buf, "%d", max);
139
 
    R_get_text_box(txt_buf, &tt, &tb, &tl, &tr);
140
 
    R_move_abs((int)(l + TEXT_COLUMN * (r - l) - .5 * (tr - tl)),
141
 
               (int)(y_line[0] + .5 * (tb - tt)));
142
 
    R_text(txt_buf);
143
 
    R_stabilize();
144
 
 
145
 
    return 0;
146
 
}
147
 
 
148
 
 
149
 
double _get_cat(UCAT * theCat, int min)
150
 
{
151
 
    switch (theCat->type) {
152
 
    case CELL_TYPE:
153
 
        if (theCat->val.c >= min)
154
 
            return (double)(theCat->val.c - (double)min);
155
 
        else
156
 
            return (double)0.0;
157
 
    case FCELL_TYPE:
158
 
        if (theCat->val.f >= min)
159
 
            return (double)(theCat->val.f - (double)min);
160
 
        else
161
 
            return (double)0.0;
162
 
    case DCELL_TYPE:
163
 
        if (theCat->val.d >= min)
164
 
            return (theCat->val.d - (double)min);
165
 
        else
166
 
            return (double)0.0;
167
 
    default:                    /* Shouldn't happen */
168
 
        return (double)0.0;
169
 
    }
170
 
}
171
 
 
172
 
/* vim: set softtabstop=4 shiftwidth=4 expandtab: */