GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
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 SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
9 SPDX-License-Identifier: GPL-2.0-or-later
10
11 \author Radim Blazek
12 */
13
14#include <grass/vector.h>
15
16/*!
17 \brief Calculates z coordinate for point from TIN
18
19 \param Map pointer to vector map
20 \param tx,ty point coordinates
21 \param[out] tz z-coordinate of point
22 \param[out] angle angle (unsupported)
23 \param[out] slope slope (unsupported)
24
25 \return 1 on success,
26 \return 0 point is not in area,
27 \return -1 area has not 4 points or has island
28 */
29int Vect_tin_get_z(struct Map_info *Map, double tx, double ty, double *tz,
30 double *angle G_UNUSED, double *slope G_UNUSED)
31{
32 int i, area, n_points;
33 struct Plus_head *Plus;
34 struct P_area *Area;
35 static struct line_pnts *Points;
36 static int first_time = 1;
37 double *x, *y, *z;
38 double vx1, vx2, vy1, vy2, vz1, vz2;
39 double a, b, c, d;
40
41 /* TODO angle, slope */
42
43 Plus = &(Map->plus);
44 if (first_time == 1) {
45 Points = Vect_new_line_struct();
46 first_time = 0;
47 }
48
49 area = Vect_find_area(Map, tx, ty);
50 G_debug(3, "TIN: area = %d", area);
51 if (area == 0)
52 return 0;
53
54 Area = Plus->Area[area];
55 if (Area->n_isles > 0)
56 return -1;
57
58 Vect_get_area_points(Map, area, Points);
59 n_points = Points->n_points;
60 if (n_points != 4)
61 return -1;
62
63 x = Points->x;
64 y = Points->y;
65 z = Points->z;
66 for (i = 0; i < 3; i++) {
67 G_debug(3, "TIN: %d %f %f %f", i, x[i], y[i], z[i]);
68 }
69
70 vx1 = x[1] - x[0];
71 vy1 = y[1] - y[0];
72 vz1 = z[1] - z[0];
73 vx2 = x[2] - x[0];
74 vy2 = y[2] - y[0];
75 vz2 = z[2] - z[0];
76
77 a = vy1 * vz2 - vy2 * vz1;
78 b = vz1 * vx2 - vz2 * vx1;
79 c = vx1 * vy2 - vx2 * vy1;
80 d = -a * x[0] - b * y[0] - c * z[0];
81
82 /* OK ? */
83 *tz = -(d + a * tx + b * ty) / c;
84 G_debug(3, "TIN: z = %f", *tz);
85
86 return 1;
87}
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:43
int Vect_find_area(struct Map_info *, double, double)
Find the nearest area.
#define G_UNUSED
A macro for an attribute, if attached to a variable, indicating that the variable is not used.
Definition gis.h:43
double b
Definition r_raster.c:37
Vector map info.
Area (topology) info.
plus_t n_isles
Number of islands inside.
Basic topology-related info.
Feature geometry info - coordinates.
double * y
Array of Y coordinates.
double * x
Array of X coordinates.
int n_points
Number of points.
double * z
Array of Z coordinates.
int Vect_tin_get_z(struct Map_info *Map, double tx, double ty, double *tz, double *angle, double *slope)
Calculates z coordinate for point from TIN.
Definition tin.c:29