File: stroke.c

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (185 lines) | stat: -rw-r--r-- 5,231 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/****************************************************************************
 *
 * MODULE:       Symbol library
 *
 * AUTHOR(S):    Radim Blazek
 *
 * PURPOSE:      Stroke symbol
 *
 * COPYRIGHT:    (C) 2001 by the GRASS Development Team
 *
 *               This program is free software under the GNU General Public
 *               License (>=v2). Read the file COPYING that comes with
 *               GRASS for details.
 *
 *****************************************************************************/

#include <stdlib.h>
#include <math.h>
#include <grass/gis.h>
#include <grass/symbol.h>

#define PI M_PI

void add_coor(SYMBCHAIN *chain, double x, double y)
{
    G_debug(5, "    add_coor %f, %f", x, y);
    if (chain->scount == chain->salloc) {
        chain->salloc += 10;
        chain->sx =
            (double *)G_realloc(chain->sx, chain->salloc * sizeof(double));
        chain->sy =
            (double *)G_realloc(chain->sy, chain->salloc * sizeof(double));
    }
    chain->sx[chain->scount] = x;
    chain->sy[chain->scount] = y;
    chain->scount++;
}

/* draw chain
 *   s - scale
 *   ch - chain number
 *   rotation - degrees CCW from East
 */
int stroke_chain(SYMBPART *part, int ch, double s, double rotation)
{
    int k, l, first;
    SYMBEL *elem;
    SYMBCHAIN *chain;
    double r;
    double a1, a2, da;
    double x, y, x0, y0;

    G_debug(5, "  stroke_chain(): ch = %d", ch);
    chain = part->chain[ch];

    G_debug(5, "    element count = %d", chain->count);
    first = 1;
    for (k = 0; k < chain->count; k++) {
        elem = chain->elem[k];
        switch (elem->type) {
        case S_LINE:
            G_debug(5, "    LINE count = %d", elem->coor.line.count);
            for (l = 0; l < elem->coor.line.count; l++) {
                x = s * elem->coor.line.x[l];
                y = s * elem->coor.line.y[l];

                if (rotation != 0.0)
                    G_rotate_around_point(0, 0, &x, &y, rotation);

                add_coor(chain, x, y);
                if (first) {
                    x0 = x;
                    y0 = y;
                    first = 0;
                }
            }
            break;
        case S_ARC:
            if (s >= 50)
                da = 1 * PI / 180; /* later calc from size and tolerance */
            else
                da = 10 * PI / 180;

            r = elem->coor.arc.r;
            G_debug(5, "    ARC da = %f r = %f", da, r);

            /* convert to positive angles */
            a1 = PI * elem->coor.arc.a1 / 180;
            if (a1 < 0)
                a1 += 2 * PI;
            a2 = PI * elem->coor.arc.a2 / 180;
            if (a2 < 0)
                a2 += 2 * PI;

            if (elem->coor.arc.clock) { /* clockwise */
                while (1) {
                    x = s * elem->coor.arc.x + s * r * cos(a1);
                    y = s * elem->coor.arc.y + s * r * sin(a1);

                    if (rotation != 0.0)
                        G_rotate_around_point(0, 0, &x, &y, rotation);

                    add_coor(chain, x, y);
                    if (first) {
                        x0 = x;
                        y0 = y;
                        first = 0;
                    }
                    if (a1 == a2)
                        break;
                    a1 -= da;
                    if (a1 < a2)
                        a1 = a2;
                }
            }
            else {
                while (1) {
                    x = s * elem->coor.arc.x + s * r * cos(a1);
                    y = s * elem->coor.arc.y + s * r * sin(a1);

                    if (rotation != 0.0)
                        G_rotate_around_point(0, 0, &x, &y, rotation);

                    add_coor(chain, x, y);
                    if (first) {
                        x0 = x;
                        y0 = y;
                        first = 0;
                    }
                    if (a1 == a2)
                        break;
                    a1 += da;
                    if (a1 > a2)
                        a1 = a2;
                }
            }
            break;
        }
    }
    if (part->type == S_POLYGON) {
        add_coor(chain, x0, y0); /* Close ring */
    }

    return 0;
}

/*!
 * \brief Stroke symbol to form used for Xdriver.
 *
 *  tolerance currently not supported
 *
 * \param Symb  pointer to
 * \param size  symbol size
 * \param rotation  symbol rotation, degrees CCW from East
 * \param tolerance  currently not supported
 *
 */
void S_stroke(SYMBOL *Symb, double size, double rotation, int tolerance)
{
    int i, j;
    double s;
    SYMBPART *part;

    G_debug(3, "S_stroke(): size = %.2f, rotation = %.2f, tolerance = %d", size,
            rotation, tolerance);

    /* TODO: support for tolerance */

    s = size * Symb->scale;

    for (i = 0; i < Symb->count; i++) {
        G_debug(4, "  part %d", i);
        part = Symb->part[i];
        switch (part->type) {
        case S_POLYGON:
            for (j = 0; j < part->count; j++) { /* RINGS */
                stroke_chain(part, j, s, rotation);
            }
            break;
        case S_STRING: /* string has 1 chain */
            stroke_chain(part, 0, s, rotation);
            break;
        }
    }
}