tin.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*!
  2. \file lib/vector/Vlib/tin.c
  3. \brief Vector library - TIN
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2001-2009 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Radim Blazek
  9. */
  10. #include <grass/vector.h>
  11. /*!
  12. \brief Calculates z coordinate for point from TIN
  13. \param Map pointer to vector map
  14. \param tx,ty point coordinates
  15. \param[out] tz z-coordinate of point
  16. \param[out] angle angle
  17. \param[out] slope slope
  18. \return 1 on success,
  19. \return 0 point is not in area,
  20. \return -1 area has not 4 points or has island
  21. */
  22. int
  23. Vect_tin_get_z(struct Map_info *Map,
  24. double tx, double ty, double *tz, double *angle, double *slope)
  25. {
  26. int i, area, n_points;
  27. struct Plus_head *Plus;
  28. struct P_area *Area;
  29. static struct line_pnts *Points;
  30. static int first_time = 1;
  31. double *x, *y, *z;
  32. double vx1, vx2, vy1, vy2, vz1, vz2;
  33. double a, b, c, d;
  34. /* TODO angle, slope */
  35. Plus = &(Map->plus);
  36. if (first_time == 1) {
  37. Points = Vect_new_line_struct();
  38. first_time = 0;
  39. }
  40. area = Vect_find_area(Map, tx, ty);
  41. G_debug(3, "TIN: area = %d", area);
  42. if (area == 0)
  43. return 0;
  44. Area = Plus->Area[area];
  45. if (Area->n_isles > 0)
  46. return -1;
  47. Vect_get_area_points(Map, area, Points);
  48. n_points = Points->n_points;
  49. if (n_points != 4)
  50. return -1;
  51. x = Points->x;
  52. y = Points->y;
  53. z = Points->z;
  54. for (i = 0; i < 3; i++) {
  55. G_debug(3, "TIN: %d %f %f %f", i, x[i], y[i], z[i]);
  56. }
  57. vx1 = x[1] - x[0];
  58. vy1 = y[1] - y[0];
  59. vz1 = z[1] - z[0];
  60. vx2 = x[2] - x[0];
  61. vy2 = y[2] - y[0];
  62. vz2 = z[2] - z[0];
  63. a = vy1 * vz2 - vy2 * vz1;
  64. b = vz1 * vx2 - vz2 * vx1;
  65. c = vx1 * vy2 - vx2 * vy1;
  66. d = -a * x[0] - b * y[0] - c * z[0];
  67. /* OK ? */
  68. *tz = -(d + a * tx + b * ty) / c;
  69. G_debug(3, "TIN: z = %f", *tz);
  70. return 1;
  71. }