GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
tin.c
Go to the documentation of this file.
1 
20 #include <grass/Vect.h>
21 
35 int
36 Vect_tin_get_z(struct Map_info *Map,
37  double tx, double ty, double *tz, double *angle, double *slope)
38 {
39  int i, area, n_points;
40  struct Plus_head *Plus;
41  P_AREA *Area;
42  static struct line_pnts *Points;
43  static int first_time = 1;
44  double *x, *y, *z;
45  double vx1, vx2, vy1, vy2, vz1, vz2;
46  double a, b, c, d;
47 
48  /* TODO angle, slope */
49 
50  Plus = &(Map->plus);
51  if (first_time == 1) {
52  Points = Vect_new_line_struct();
53  first_time = 0;
54  }
55 
56  area = Vect_find_area(Map, tx, ty);
57  G_debug(3, "TIN: area = %d", area);
58  if (area == 0)
59  return 0;
60 
61  Area = Plus->Area[area];
62  if (Area->n_isles > 0)
63  return -1;
64 
65  Vect_get_area_points(Map, area, Points);
66  n_points = Points->n_points;
67  if (n_points != 4)
68  return -1;
69 
70  x = Points->x;
71  y = Points->y;
72  z = Points->z;
73  for (i = 0; i < 3; i++) {
74  G_debug(3, "TIN: %d %f %f %f", i, x[i], y[i], z[i]);
75  }
76 
77  vx1 = x[1] - x[0];
78  vy1 = y[1] - y[0];
79  vz1 = z[1] - z[0];
80  vx2 = x[2] - x[0];
81  vy2 = y[2] - y[0];
82  vz2 = z[2] - z[0];
83 
84  a = vy1 * vz2 - vy2 * vz1;
85  b = vz1 * vx2 - vz2 * vx1;
86  c = vx1 * vy2 - vx2 * vy1;
87  d = -a * x[0] - b * y[0] - c * z[0];
88 
89  /* OK ? */
90  *tz = -(d + a * tx + b * ty) / c;
91  G_debug(3, "TIN: z = %f", *tz);
92 
93  return 1;
94 }