File: poly.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 (330 lines) | stat: -rw-r--r-- 10,060 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*****************************************************************************
 *
 * MODULE:       Vector library
 *
 * AUTHOR(S):    Original author CERL, probably Dave Gerdes.
 *               Update to GRASS 5.7 Radim Blazek.
 *               Update to GRASS 7.0 Markus Metz
 *
 * PURPOSE:      Lower level functions for reading/writing/manipulating vectors.
 *
 * COPYRIGHT:    (C) 2009 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 <math.h>
#include <grass/vector.h>

#ifndef HUGE_VAL
#define HUGE_VAL 9999999999999.0
#endif

/*
 * fills BPoints (must be inited previously) by points from input
 * array LPoints
 *
 * each input LPoints[i] must have at least 2 points
 *
 * returns number of points or -1 on error
 */

int dig_get_poly_points(int n_lines, struct line_pnts **LPoints,
                        int *direction, /* line direction: > 0 or < 0 */
                        struct line_pnts *BPoints)
{
    register int i, j, point, start, end, inc;
    struct line_pnts *Points;
    int n_points;

    BPoints->n_points = 0;

    if (n_lines < 1) {
        return 0;
    }

    /* Calc required space */
    n_points = 0;
    for (i = 0; i < n_lines; i++) {
        Points = LPoints[i];
        n_points += Points->n_points - 1; /* each line from first to last - 1 */
    }
    n_points++; /* last point */

    if (0 > dig_alloc_points(BPoints, n_points))
        return (-1);

    point = 0;
    j = 0;
    for (i = 0; i < n_lines; i++) {
        Points = LPoints[i];
        if (direction[i] > 0) {
            start = 0;
            end = Points->n_points - 1;
            inc = 1;
        }
        else {
            start = Points->n_points - 1;
            end = 0;
            inc = -1;
        }

        for (j = start; j != end; j += inc) {
            BPoints->x[point] = Points->x[j];
            BPoints->y[point] = Points->y[j];
            point++;
        }
    }
    /* last point */
    BPoints->x[point] = Points->x[j];
    BPoints->y[point] = Points->y[j];

    BPoints->n_points = n_points;

    return (BPoints->n_points);
}

/*
 * calculate signed area size for polygon
 *
 * points must be closed polygon with first point = last point
 *
 * returns signed area, positive for clockwise, negative for
 * counterclockwise, 0 for degenerate
 */
int dig_find_area_poly(struct line_pnts *Points, double *totalarea)
{
    int i;
    double *x, *y;
    double tot_area;

    x = Points->x;
    y = Points->y;

    /* line integral: *Points do not need to be pruned */
    /* surveyor's formula is more common, but more prone to
     * fp precision limit errors, and *Points would need to be pruned */
    tot_area = 0.0;
    for (i = 1; i < Points->n_points; i++) {
        tot_area += (x[i] - x[i - 1]) * (y[i] + y[i - 1]);
    }
    *totalarea = 0.5 * tot_area;

    return (0);
}

/*
 * find orientation of polygon (clockwise or counterclockwise)
 * in theory faster than signed area for > 4 vertices, but is not robust
 * against special cases
 * use dig_find_area_poly instead
 *
 * points must be closed polygon with first point = last point
 *
 * this code uses bits and pieces from softSurfer and GEOS
 * (C) 2000 softSurfer (www.softsurfer.com)
 * (C) 2006 Refractions Research Inc.
 *
 * copes with partially collapsed boundaries and 8-shaped isles
 * the code is long and not much faster than dig_find_area_poly
 * it can be written much shorter, but that comes with speed penalty
 *
 * returns orientation, positive for CW, negative for CCW, 0 for degenerate
 */
double dig_find_poly_orientation(struct line_pnts *Points)
{
    unsigned int pnext, pprev, pcur = 0;
    unsigned int lastpoint = Points->n_points - 1;
    double *x, *y, orientation;

    x = Points->x;
    y = Points->y;

    /* first find leftmost highest vertex of the polygon */
    for (pnext = 1; pnext < lastpoint; pnext++) {
        if (y[pnext] < y[pcur])
            continue;
        else if (y[pnext] == y[pcur]) { /* just as high */
            if (x[pnext] > x[pcur])     /* but to the right */
                continue;
            if (x[pnext] ==
                x[pcur]) { /* duplicate point, self-intersecting polygon ? */
                pprev = (pcur == 0 ? lastpoint - 1 : pcur - 1);
                if (y[pnext - 1] < y[pprev])
                    continue;
            }
        }
        pcur = pnext; /* a new leftmost highest vertex */
    }

    /* Points are not pruned, so ... */
    pnext = pcur;
    pprev = pcur;

    /* find next distinct point */
    do {
        if (pnext < lastpoint - 1)
            pnext++;
        else
            pnext = 0;
    } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);

    /* find previous distinct point */
    do {
        if (pprev > 0)
            pprev--;
        else
            pprev = lastpoint - 1;
    } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);

    /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
     * rather use robust determinant of Olivier Devillers? */
    orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev]) -
                  (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);

    if (orientation)
        return orientation;

    /* orientation is 0, can happen with dirty boundaries, next check */
    /* find rightmost highest vertex of the polygon */
    pcur = 0;
    for (pnext = 1; pnext < lastpoint; pnext++) {
        if (y[pnext] < y[pcur])
            continue;
        else if (y[pnext] == y[pcur]) { /* just as high */
            if (x[pnext] < x[pcur])     /* but to the left */
                continue;
            if (x[pnext] ==
                x[pcur]) { /* duplicate point, self-intersecting polygon ? */
                pprev = (pcur == 0 ? lastpoint - 1 : pcur - 1);
                if (y[pnext - 1] < y[pprev])
                    continue;
            }
        }
        pcur = pnext; /* a new rightmost highest vertex */
    }

    /* Points are not pruned, so ... */
    pnext = pcur;
    pprev = pcur;

    /* find next distinct point */
    do {
        if (pnext < lastpoint - 1)
            pnext++;
        else
            pnext = 0;
    } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);

    /* find previous distinct point */
    do {
        if (pprev > 0)
            pprev--;
        else
            pprev = lastpoint - 1;
    } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);

    /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
     * rather use robust determinant of Olivier Devillers? */
    orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev]) -
                  (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);

    if (orientation)
        return orientation;

    /* orientation is 0, next check */
    /* find leftmost lowest vertex of the polygon */
    pcur = 0;
    for (pnext = 1; pnext < lastpoint; pnext++) {
        if (y[pnext] > y[pcur])
            continue;
        else if (y[pnext] == y[pcur]) { /* just as low */
            if (x[pnext] > x[pcur])     /* but to the right */
                continue;
            if (x[pnext] ==
                x[pcur]) { /* duplicate point, self-intersecting polygon ? */
                pprev = (pcur == 0 ? lastpoint - 1 : pcur - 1);
                if (y[pnext - 1] > y[pprev])
                    continue;
            }
        }
        pcur = pnext; /* a new leftmost lowest vertex */
    }

    /* Points are not pruned, so ... */
    pnext = pcur;
    pprev = pcur;

    /* find next distinct point */
    do {
        if (pnext < lastpoint - 1)
            pnext++;
        else
            pnext = 0;
    } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);

    /* find previous distinct point */
    do {
        if (pprev > 0)
            pprev--;
        else
            pprev = lastpoint - 1;
    } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);

    /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
     * rather use robust determinant of Olivier Devillers? */
    orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev]) -
                  (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);

    if (orientation)
        return orientation;

    /* orientation is 0, last check */
    /* find rightmost lowest vertex of the polygon */
    pcur = 0;
    for (pnext = 1; pnext < lastpoint; pnext++) {
        if (y[pnext] > y[pcur])
            continue;
        else if (y[pnext] == y[pcur]) { /* just as low */
            if (x[pnext] < x[pcur])     /* but to the left */
                continue;
            if (x[pnext] ==
                x[pcur]) { /* duplicate point, self-intersecting polygon ? */
                pprev = (pcur == 0 ? lastpoint - 1 : pcur - 1);
                if (y[pnext - 1] > y[pprev])
                    continue;
            }
        }
        pcur = pnext; /* a new rightmost lowest vertex */
    }

    /* Points are not pruned, so ... */
    pnext = pcur;
    pprev = pcur;

    /* find next distinct point */
    do {
        if (pnext < lastpoint - 1)
            pnext++;
        else
            pnext = 0;
    } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);

    /* find previous distinct point */
    do {
        if (pprev > 0)
            pprev--;
        else
            pprev = lastpoint - 1;
    } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);

    /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
     * rather use robust determinant of Olivier Devillers? */
    orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev]) -
                  (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);

    return orientation; /* 0 for degenerate */
}