point.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /****************************************************************
  2. *
  3. * MODULE: v.generalize
  4. *
  5. * AUTHOR(S): Daniel Bundala
  6. *
  7. * PURPOSE: Definition of a point in 3D and basic operations
  8. * with points
  9. *
  10. * COPYRIGHT: (C) 2002-2005 by the GRASS Development Team
  11. *
  12. * This program is free software under the
  13. * GNU General Public License (>=v2).
  14. * Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. ****************************************************************/
  18. #include <math.h>
  19. #include <grass/gis.h>
  20. #include <grass/vector.h>
  21. #include <grass/glocale.h>
  22. #include "point.h"
  23. inline void point_subtract(POINT a, POINT b, POINT * res)
  24. {
  25. res->x = a.x - b.x;
  26. res->y = a.y - b.y;
  27. res->z = a.z - b.z;
  28. return;
  29. }
  30. inline void point_add(POINT a, POINT b, POINT * res)
  31. {
  32. res->x = a.x + b.x;
  33. res->y = a.y + b.y;
  34. res->z = a.z + b.z;
  35. return;
  36. }
  37. double point_dot(POINT a, POINT b)
  38. {
  39. return a.x * b.x + a.y * b.y + a.z * b.z;
  40. }
  41. inline double point_dist2(POINT a)
  42. {
  43. return a.x * a.x + a.y * a.y + a.z * a.z;
  44. }
  45. inline void point_assign(struct line_pnts *Points, int index, int with_z,
  46. POINT * res, int is_loop)
  47. {
  48. if (is_loop) {
  49. while (index >= Points->n_points - 1)
  50. index -= Points->n_points - 1;
  51. }
  52. res->x = Points->x[index];
  53. res->y = Points->y[index];
  54. if (with_z) {
  55. res->z = Points->z[index];
  56. }
  57. else {
  58. res->z = 0;
  59. }
  60. return;
  61. }
  62. inline void point_scalar(POINT a, double k, POINT * res)
  63. {
  64. res->x = a.x * k;
  65. res->y = a.y * k;
  66. res->z = a.z * k;
  67. return;
  68. }
  69. inline void points_copy_last(struct line_pnts *Points, int pos)
  70. {
  71. int n = Points->n_points - 1;
  72. Points->x[pos] = Points->x[n];
  73. Points->y[pos] = Points->y[n];
  74. Points->z[pos] = Points->z[n];
  75. Points->n_points = pos + 1;
  76. return;
  77. }
  78. inline double point_dist(POINT a, POINT b)
  79. {
  80. return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) +
  81. (a.z - b.z) * (a.z - b.z));
  82. }
  83. inline double point_dist_square(POINT a, POINT b)
  84. {
  85. return (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y) +
  86. (a.z - b.z) * (a.z - b.z);
  87. }
  88. inline double point_angle_between(POINT a, POINT b, POINT c)
  89. {
  90. point_subtract(b, a, &a);
  91. point_subtract(c, b, &b);
  92. return acos(point_dot(a, b) / sqrt(point_dist2(a) * point_dist2(b)));
  93. }
  94. inline double point_dist_segment_square(POINT a, POINT b, POINT c, int with_z)
  95. {
  96. double px, py, pz, pdist;
  97. int status;
  98. return dig_distance2_point_to_line(a.x, a.y, a.z, b.x, b.y, b.z,
  99. c.x, c.y, c.z, with_z, &px, &py, &pz,
  100. &pdist, &status);
  101. }
  102. POINT_LIST *point_list_new(POINT p)
  103. {
  104. POINT_LIST *pl;
  105. pl = G_malloc(sizeof(POINT_LIST));
  106. pl->next = NULL;
  107. pl->p = p;
  108. return pl;
  109. }
  110. void point_list_add(POINT_LIST * l, POINT p)
  111. {
  112. POINT_LIST *n;
  113. n = point_list_new(p);
  114. n->next = l->next;
  115. l->next = n;
  116. return;
  117. }
  118. int point_list_copy_to_line_pnts(POINT_LIST l, struct line_pnts *Points)
  119. {
  120. int length, i;
  121. POINT_LIST *cur;
  122. cur = l.next;
  123. length = 0;
  124. while (cur != NULL) {
  125. length++;
  126. cur = cur->next;
  127. }
  128. if (length != Points->n_points)
  129. if (0 > dig_alloc_points(Points, length))
  130. return (-1);
  131. Points->n_points = length;
  132. cur = l.next;
  133. for (i = 0; i < length; i++) {
  134. Points->x[i] = cur->p.x;
  135. Points->y[i] = cur->p.y;
  136. Points->z[i] = cur->p.z;
  137. cur = cur->next;
  138. }
  139. return 0;
  140. }
  141. void point_list_free(POINT_LIST l)
  142. {
  143. POINT_LIST *p, *n;
  144. p = l.next;
  145. while (p != NULL) {
  146. n = p->next;
  147. G_free(p);
  148. p = n;
  149. }
  150. }
  151. extern void point_list_delete_next(POINT_LIST * p)
  152. {
  153. POINT_LIST *t = p->next;
  154. p->next = p->next->next;
  155. G_free(t);
  156. return;
  157. }