GRASS GIS 8 Programmer's Manual
8.5.0dev(2024)-36359e2344
vector/diglib/line_dist.c
Go to the documentation of this file.
1
/*****************************************************************************
2
*
3
* MODULE: Vector library
4
*
5
* AUTHOR(S): Original author CERL, probably Dave Gerdes.
6
* Update to GRASS 5.7 Radim Blazek.
7
*
8
* PURPOSE: Lower level functions for reading/writing/manipulating vectors.
9
*
10
* COPYRIGHT: (C) 2001 by the GRASS Development Team
11
*
12
* This program is free software under the GNU General Public
13
* License (>=v2). Read the file COPYING that comes with GRASS
14
* for details.
15
*
16
*****************************************************************************/
17
18
#include <math.h>
19
20
#define ZERO(x) ((x) < tolerance && (x) > -tolerance)
21
#define TOLERANCE 1.0e-10
22
static
double
tolerance =
TOLERANCE
;
23
24
int
dig_set_distance_to_line_tolerance
(
double
t
)
25
{
26
if
(
t
<= 0.0)
27
t
=
TOLERANCE
;
28
tolerance =
t
;
29
30
return
0;
31
}
32
33
/*
34
* dig_distance2_point_to_line ()
35
* compute square of distance of point (x,y) to line segment (x1,y1 - x2,y2)
36
* ( works correctly for x1==x2 && y1==y2 )
37
*
38
* returns: square distance
39
* sets (if not NULL): *px, *py - nearest point on segment
40
* *pdist - distance of px,py from segment start
41
* *status = 0 if ok, -1 if t < 0 and 1 if t > 1
42
* (tells if point is w/in segment space, or
43
* past ends)
44
*/
45
46
double
47
dig_distance2_point_to_line
(
double
x
,
double
y,
double
z,
/* point */
48
double
x1,
double
y1,
double
z1,
/* line segment */
49
double
x2,
double
y2,
double
z2,
50
int
with_z,
/* use z coordinate, (3D calculation) */
51
double
*px,
double
*py,
52
double
*pz,
/* point on segment */
53
double
*pdist,
/* distance of point on segment from
54
the first point of segment */
55
int
*status)
56
{
57
register
double
dx, dy, dz;
58
register
double
dpx, dpy, dpz;
59
register
double
tpx, tpy, tpz;
60
double
t
;
61
int
st
;
62
63
st
= 0;
64
65
if
(!with_z) {
66
z = 0;
67
z1 = 0;
68
z2 = 0;
69
}
70
71
dx = x2 - x1;
72
dy = y2 - y1;
73
dz = z2 - z1;
74
75
if
(
ZERO
(dx) &&
ZERO
(dy) &&
ZERO
(dz)) {
/* line is degenerate */
76
dx = x1 -
x
;
77
dy = y1 - y;
78
dz = z1 - z;
79
tpx = x1;
80
tpy = y1;
81
tpz = z1;
82
}
83
else
{
84
t
= (dx * (
x
- x1) + dy * (y - y1) + dz * (z - z1)) /
85
(dx * dx + dy * dy + dz * dz);
86
87
if
(
t
<= 0.0) {
/* go to x1,y1,z1 */
88
if
(
t
< 0.0) {
89
st
= -1;
90
}
91
tpx = x1;
92
tpy = y1;
93
tpz = z1;
94
}
95
else
if
(
t
>= 1.0) {
/* go to x2,y2,z2 */
96
if
(
t
> 1.0) {
97
st
= 1;
98
}
99
tpx = x2;
100
tpy = y2;
101
tpz = z2;
102
}
103
else
{
104
/* go t from x1,y1,z1 towards x2,y2,z2 */
105
tpx = dx *
t
+ x1;
106
tpy = dy *
t
+ y1;
107
tpz = dz *
t
+ z1;
108
}
109
dx = tpx -
x
;
110
dy = tpy - y;
111
dz = tpz - z;
112
}
113
114
if
(px)
115
*px = tpx;
116
if
(py)
117
*py = tpy;
118
if
(pz)
119
*pz = tpz;
120
if
(status)
121
*status =
st
;
122
123
if
(pdist) {
124
dpx = tpx - x1;
125
dpy = tpy - y1;
126
dpz = tpz - z1;
127
*pdist = sqrt(dpx * dpx + dpy * dpy + dpz * dpz);
128
}
129
130
return
(dx * dx + dy * dy + dz * dz);
131
}
st
struct state * st
Definition:
parser.c:104
t
double t
Definition:
r_raster.c:39
dig_set_distance_to_line_tolerance
int dig_set_distance_to_line_tolerance(double t)
Definition:
vector/diglib/line_dist.c:24
TOLERANCE
#define TOLERANCE
Definition:
vector/diglib/line_dist.c:21
ZERO
#define ZERO(x)
Definition:
vector/diglib/line_dist.c:20
dig_distance2_point_to_line
double dig_distance2_point_to_line(double x, double y, double z, double x1, double y1, double z1, double x2, double y2, double z2, int with_z, double *px, double *py, double *pz, double *pdist, int *status)
Definition:
vector/diglib/line_dist.c:47
x
#define x
lib
vector
diglib
line_dist.c
Generated on Thu Nov 21 2024 07:03:28 for GRASS GIS 8 Programmer's Manual by
1.9.1