GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-d6dec75dd4
tin.c
Go to the documentation of this file.
1 /*!
2  \file lib/vector/Vlib/tin.c
3 
4  \brief Vector library - TIN
5 
6  Higher level functions for reading/writing/manipulating vectors.
7 
8  (C) 2001-2009 by the GRASS Development Team
9 
10  This program is free software under the GNU General Public License
11  (>=v2). Read the file COPYING that comes with GRASS for details.
12 
13  \author Radim Blazek
14  */
15 
16 #include <grass/vector.h>
17 
18 /*!
19  \brief Calculates z coordinate for point from TIN
20 
21  \param Map pointer to vector map
22  \param tx,ty point coordinates
23  \param[out] tz z-coordinate of point
24  \param[out] angle angle (unsupported)
25  \param[out] slope slope (unsupported)
26 
27  \return 1 on success,
28  \return 0 point is not in area,
29  \return -1 area has not 4 points or has island
30  */
31 int Vect_tin_get_z(struct Map_info *Map, double tx, double ty, double *tz,
32  double *angle UNUSED, double *slope UNUSED)
33 {
34  int i, area, n_points;
35  struct Plus_head *Plus;
36  struct P_area *Area;
37  static struct line_pnts *Points;
38  static int first_time = 1;
39  double *x, *y, *z;
40  double vx1, vx2, vy1, vy2, vz1, vz2;
41  double a, b, c, d;
42 
43  /* TODO angle, slope */
44 
45  Plus = &(Map->plus);
46  if (first_time == 1) {
47  Points = Vect_new_line_struct();
48  first_time = 0;
49  }
50 
51  area = Vect_find_area(Map, tx, ty);
52  G_debug(3, "TIN: area = %d", area);
53  if (area == 0)
54  return 0;
55 
56  Area = Plus->Area[area];
57  if (Area->n_isles > 0)
58  return -1;
59 
60  Vect_get_area_points(Map, area, Points);
61  n_points = Points->n_points;
62  if (n_points != 4)
63  return -1;
64 
65  x = Points->x;
66  y = Points->y;
67  z = Points->z;
68  for (i = 0; i < 3; i++) {
69  G_debug(3, "TIN: %d %f %f %f", i, x[i], y[i], z[i]);
70  }
71 
72  vx1 = x[1] - x[0];
73  vy1 = y[1] - y[0];
74  vz1 = z[1] - z[0];
75  vx2 = x[2] - x[0];
76  vy2 = y[2] - y[0];
77  vz2 = z[2] - z[0];
78 
79  a = vy1 * vz2 - vy2 * vz1;
80  b = vz1 * vx2 - vz2 * vx1;
81  c = vx1 * vy2 - vx2 * vy1;
82  d = -a * x[0] - b * y[0] - c * z[0];
83 
84  /* OK ? */
85  *tz = -(d + a * tx + b * ty) / c;
86  G_debug(3, "TIN: z = %f", *tz);
87 
88  return 1;
89 }
int G_debug(int, const char *,...) __attribute__((format(printf
int Vect_get_area_points(struct Map_info *, int, struct line_pnts *)
Returns polygon array of points (outer ring) of given area.
struct line_pnts * Vect_new_line_struct(void)
Creates and initializes a line_pnts structure.
Definition: line.c:45
int Vect_find_area(struct Map_info *, double, double)
Find the nearest area.
#define UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition: gis.h:47
double b
Definition: r_raster.c:39
Vector map info.
Definition: dig_structs.h:1243
struct Plus_head plus
Plus info (topology, version, ...)
Definition: dig_structs.h:1270
Area (topology) info.
Definition: dig_structs.h:1583
plus_t n_isles
Number of islands inside.
Definition: dig_structs.h:1609
Basic topology-related info.
Definition: dig_structs.h:769
struct P_area ** Area
Array of areas.
Definition: dig_structs.h:875
Feature geometry info - coordinates.
Definition: dig_structs.h:1651
double * y
Array of Y coordinates.
Definition: dig_structs.h:1659
double * x
Array of X coordinates.
Definition: dig_structs.h:1655
int n_points
Number of points.
Definition: dig_structs.h:1667
double * z
Array of Z coordinates.
Definition: dig_structs.h:1663
int Vect_tin_get_z(struct Map_info *Map, double tx, double ty, double *tz, double *angle UNUSED, double *slope UNUSED)
Calculates z coordinate for point from TIN.
Definition: tin.c:31
#define x