File: point.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 (188 lines) | stat: -rw-r--r-- 3,967 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
/****************************************************************
 *
 * MODULE:     v.generalize
 *
 * AUTHOR(S):  Daniel Bundala
 *
 * PURPOSE:    Definition of a point in 3D and basic operations
 *             with points
 *
 * COPYRIGHT:  (C) 2002-2005 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/gis.h>
#include <grass/vector.h>
#include <grass/glocale.h>
#include "point.h"

inline void point_subtract(POINT a, POINT b, POINT *res)
{
    res->x = a.x - b.x;
    res->y = a.y - b.y;
    res->z = a.z - b.z;
    return;
}

inline void point_add(POINT a, POINT b, POINT *res)
{
    res->x = a.x + b.x;
    res->y = a.y + b.y;
    res->z = a.z + b.z;
    return;
}

double point_dot(POINT a, POINT b)
{
    return a.x * b.x + a.y * b.y + a.z * b.z;
}

inline double point_dist2(POINT a)
{
    return a.x * a.x + a.y * a.y + a.z * a.z;
}

inline void point_assign(struct line_pnts *Points, int index, int with_z,
                         POINT *res, int is_loop)
{
    if (is_loop) {
        while (index >= Points->n_points - 1)
            index -= Points->n_points - 1;
    }
    res->x = Points->x[index];
    res->y = Points->y[index];
    if (with_z) {
        res->z = Points->z[index];
    }
    else {
        res->z = 0;
    }
    return;
}

inline void point_scalar(POINT a, double k, POINT *res)
{
    res->x = a.x * k;
    res->y = a.y * k;
    res->z = a.z * k;
    return;
}

inline void points_copy_last(struct line_pnts *Points, int pos)
{
    int n = Points->n_points - 1;

    Points->x[pos] = Points->x[n];
    Points->y[pos] = Points->y[n];
    Points->z[pos] = Points->z[n];
    Points->n_points = pos + 1;
    return;
}

inline double point_dist(POINT a, POINT b)
{
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) +
                (a.z - b.z) * (a.z - b.z));
}

inline double point_dist_square(POINT a, POINT b)
{
    return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) +
           (a.z - b.z) * (a.z - b.z);
}

inline double point_angle_between(POINT a, POINT b, POINT c)
{
    point_subtract(b, a, &a);
    point_subtract(c, b, &b);
    return acos(point_dot(a, b) / sqrt(point_dist2(a) * point_dist2(b)));
}

inline double point_dist_segment_square(POINT a, POINT b, POINT c, int with_z)
{
    double px, py, pz, pdist;
    int status;

    return dig_distance2_point_to_line(a.x, a.y, a.z, b.x, b.y, b.z, c.x, c.y,
                                       c.z, with_z, &px, &py, &pz, &pdist,
                                       &status);
}

POINT_LIST *point_list_new(POINT p)
{
    POINT_LIST *pl;

    pl = G_malloc(sizeof(POINT_LIST));

    pl->next = NULL;
    pl->p = p;
    return pl;
}

void point_list_add(POINT_LIST *l, POINT p)
{
    POINT_LIST *n;

    n = point_list_new(p);
    n->next = l->next;
    l->next = n;
    return;
}

int point_list_copy_to_line_pnts(POINT_LIST l, struct line_pnts *Points)
{

    int length, i;
    POINT_LIST *cur;

    cur = l.next;
    length = 0;

    while (cur != NULL) {
        length++;
        cur = cur->next;
    }

    if (length != Points->n_points)
        if (0 > dig_alloc_points(Points, length))
            return (-1);

    Points->n_points = length;

    cur = l.next;
    for (i = 0; i < length; i++) {
        Points->x[i] = cur->p.x;
        Points->y[i] = cur->p.y;
        Points->z[i] = cur->p.z;
        cur = cur->next;
    }

    return 0;
}

void point_list_free(POINT_LIST l)
{
    POINT_LIST *p, *n;

    p = l.next;
    while (p != NULL) {
        n = p->next;
        G_free(p);
        p = n;
    }
}

extern void point_list_delete_next(POINT_LIST *p)
{
    POINT_LIST *t = p->next;

    p->next = p->next->next;
    G_free(t);
    return;
}