File: angle.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 (217 lines) | stat: -rw-r--r-- 5,842 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 ****************************************************************************
 *
 * MODULE:       Vector library
 *
 * AUTHOR(S):    Original author CERL, probably Dave Gerdes.
 *               Update to GRASS 5.7 Radim Blazek.
 *
 * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
 *
 * 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.
 *
 *****************************************************************************/
/*
 *    functions - calc_begin_angle(), and calc_end_angle()
 *    used to calculate the angle of a line to a node.
 *    returns -  (float)angle (-PI ... +PI)
 *    returns -  (float)(-9)  if only 1 point or more points but identical
 */

#include <stdio.h>
#include <math.h>
#include <grass/vector.h>

static double d_atan2(double, double);

float dig_calc_begin_angle(const struct line_pnts *points, double thresh)
{
    double last_x;
    double last_y;
    const double *xptr;
    const double *yptr;
    int short_line;
    int i;
    int n_points;
    const double *xarray;
    const double *yarray;

    /* temporary way to set up use of struct line_pnts */
    n_points = points->n_points;
    xarray = points->x;
    yarray = points->y;

    last_x = *xarray;
    last_y = *yarray;
    xptr = xarray + 1;
    yptr = yarray + 1;

    /* check degenerate line */
    if (dig_line_degenerate(points) > 0)
        return ((float)-9.);

    short_line = 1;
    if (n_points != 2) {
        /* Search for next different coord. Note that in >= g5.7, threshold
         * is not used for build process. */
        /* 4.1 but do not use opposite node if there are other points */
        for (i = 1; i < n_points - 1; i++) {
            if ((thresh < fabs(*xptr - last_x)) ||
                (thresh < fabs(*yptr - last_y))) {
                short_line = 0;
                break;
            }
            xptr++;
            yptr++;
        }
    }

    if (short_line) {
        /* for 4.1 change this to take 1st point after node  -dpg 12/92 */
        /* return ((float) d_atan2 (yarray[n_points - 1] - last_y,
         * xarray[n_points - 1] - last_x)); */
        return ((float)d_atan2(yarray[1] - last_y, xarray[1] - last_x));
    }

    return ((float)d_atan2(*yptr - last_y, *xptr - last_x));
} /*  calc_begin_angle()  */

float dig_calc_end_angle(const struct line_pnts *points, double thresh)
{
    double last_x;
    double last_y;
    const double *xptr;
    const double *yptr;
    int short_line;
    int i;
    int n_points;
    const double *xarray;
    const double *yarray;

    short_line = 1;

    xarray = points->x;
    yarray = points->y;
    n_points = points->n_points;

    /* check degenerate line */
    if (dig_line_degenerate(points) > 0)
        return ((float)-9.);

    last_x = *(xarray + n_points - 1);
    last_y = *(yarray + n_points - 1);
    xptr = xarray + n_points - 2;
    yptr = yarray + n_points - 2;

    if (n_points != 2) {
        /* Search for next different coord. Note that in >= g5.7, threshold
         * is not used for build process. */
        /* 4.1 but do not use opposite node if there are other points */
        for (i = n_points - 2; i > 0; i--) {
            if ((thresh < fabs(*xptr - last_x)) ||
                (thresh < fabs(*yptr - last_y))) {
                short_line = 0;
                break;
            }
            xptr--;
            yptr--;
        }
    }

    if (short_line) {
        /* updated for 4.1 to take next point away from node  -dpg */
        /* return ((float) d_atan2 (yarray[0] - last_y, xarray[0] - last_x)); */
        return ((float)d_atan2(yarray[n_points - 2] - last_y,
                               xarray[n_points - 2] - last_x));
    }

    return ((float)d_atan2(*yptr - last_y, *xptr - last_x));
}

int dig_is_line_degenerate(const struct line_pnts *points, double thresh)
{
    double last_x;
    double last_y;
    const double *xptr;
    const double *yptr;
    int short_line;
    int i;
    int n_points;
    const double *xarray;
    const double *yarray;

    /* temporary way to set up use of struct line_pnts */
    n_points = points->n_points;
    xarray = points->x;
    yarray = points->y;

    last_x = *xarray;
    last_y = *yarray;
    xptr = xarray + 1;
    yptr = yarray + 1;

    short_line = 1;
    for (i = 1; i < n_points; i++) { /* Search for next different coord */
        if ((thresh < fabs(*xptr - last_x)) ||
            (thresh < fabs(*yptr - last_y))) {
            short_line = 0;
            break;
        }
        xptr++;
        yptr++;
    }

    if (short_line)
        return (1);

    return (0);
}

/* Check if line is degenerate (one point or more identical points)
 *  Returns: 0 is not degenerate (but some points may be identical)
 *           1 one point
 *           2 more identical points
 */
int dig_line_degenerate(const struct line_pnts *points)
{
    int i, ident;
    int n_points;

    G_debug(5, "dig_line_degenerate()");
    /* temporary way to set up use of struct line_pnts */
    n_points = points->n_points;

    if (n_points == 1) {
        G_debug(5, "  Line is degenerate (one points)");
        return 1;
    }

    /* check identical points (= one point) */
    ident = 1;
    for (i = 1; i < n_points; i++) {
        if (points->x[i] != points->x[i - 1] ||
            points->y[i] != points->y[i - 1]) {
            ident = 0;
            break;
        }
    }

    if (ident) {
        G_debug(5, "  Line is degenerate (more points)");
        return 2;
    }

    return 0;
}

static double d_atan2(double y, double x)
{
    if (y == 0.0 && x == 0.0)
        return (0.0);
    else
        return (atan2(y, x));
}