GRASS GIS 8 Programmer's Manual  8.2.2dev(2023)-3d2c704037
vector/vedit/distance.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/vedit/distance.c
3 
4  \brief Vedit library - distance calculation
5 
6  (C) 2007-2008 by the GRASS Development Team
7 
8  This program is free software under the GNU General Public License
9  (>=v2). Read the file COPYING that comes with GRASS for details.
10 
11  \author Martin Landa <landa.martin gmail.com>
12 */
13 
14 #include <grass/vedit.h>
15 
16 /*!
17  \brief Calculate distances between two lines
18 
19  \todo LL projection
20 
21  \param Points1 first line geometry
22  \param Points2 second line geometry
23  \param with_z WITH_Z for 3D data
24  \param[out] mindistidx index of minimal distance
25 
26  \return minimal distance between two lines (their nodes)
27 */
28 double Vedit_get_min_distance(struct line_pnts *Points1,
29  struct line_pnts *Points2, int with_z,
30  int *mindistidx)
31 {
32  unsigned int i;
33  double distances[4];
34 
35  /*
36  distances[0] = first-first
37  distances[1] = first-last
38  distances[2] = last-first
39  distances[3] = last-last
40  */
41 
42  distances[0] =
43  Vect_points_distance(Points1->x[0], Points1->y[0], Points1->z[0],
44  Points2->x[0], Points2->y[0], Points2->z[0],
45  with_z);
46 
47  distances[1] =
48  Vect_points_distance(Points1->x[0], Points1->y[0], Points1->z[0],
49  Points2->x[Points2->n_points - 1],
50  Points2->y[Points2->n_points - 1],
51  Points2->z[Points2->n_points - 1], with_z);
52 
53  distances[2] = Vect_points_distance(Points1->x[Points1->n_points - 1],
54  Points1->y[Points1->n_points - 1],
55  Points1->z[Points1->n_points - 1],
56  Points2->x[0], Points2->y[0],
57  Points2->z[0], with_z);
58 
59  distances[3] = Vect_points_distance(Points1->x[Points1->n_points - 1],
60  Points1->y[Points1->n_points - 1],
61  Points1->z[Points1->n_points - 1],
62  Points2->x[Points2->n_points - 1],
63  Points2->y[Points2->n_points - 1],
64  Points2->z[Points2->n_points - 1],
65  with_z);
66 
67  /* find the minimal distance between first or last point of both lines */
68  *mindistidx = 0;
69  for (i = 0; i < sizeof(distances) / sizeof(double); i++) {
70  if (distances[i] >= 0.0 && distances[i] < distances[*mindistidx])
71  *mindistidx = i;
72  }
73 
74  G_debug(3, "Vedit_get_min_distance(): dists=%f,%f,%f,%f",
75  distances[0], distances[1], distances[2], distances[3]);
76 
77  return distances[*mindistidx];
78 }
int n_points
Number of points.
Definition: dig_structs.h:1692
double Vect_points_distance(double, double, double, double, double, double, int)
Calculate distance of 2 points.
Definition: line.c:898
double * x
Array of X coordinates.
Definition: dig_structs.h:1680
Feature geometry info - coordinates.
Definition: dig_structs.h:1675
double * y
Array of Y coordinates.
Definition: dig_structs.h:1684
double Vedit_get_min_distance(struct line_pnts *Points1, struct line_pnts *Points2, int with_z, int *mindistidx)
Calculate distances between two lines.
double * z
Array of Z coordinates.
Definition: dig_structs.h:1688
int G_debug(int, const char *,...) __attribute__((format(printf