tin.c 2.0 KB

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