poly.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /*
  2. ****************************************************************************
  3. *
  4. * MODULE: Vector library
  5. *
  6. * AUTHOR(S): Original author CERL, probably Dave Gerdes.
  7. * Update to GRASS 5.7 Radim Blazek.
  8. * Update to GRASS 7.0 Markus Metz
  9. *
  10. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  11. *
  12. * COPYRIGHT: (C) 2009 by the GRASS Development Team
  13. *
  14. * This program is free software under the GNU General Public
  15. * License (>=v2). Read the file COPYING that comes with GRASS
  16. * for details.
  17. *
  18. *****************************************************************************/
  19. #include <math.h>
  20. #include <grass/Vect.h>
  21. #ifndef HUGE_VAL
  22. #define HUGE_VAL 9999999999999.0
  23. #endif
  24. /*
  25. * fills BPoints (must be inited previously) by points from input
  26. * array LPoints
  27. *
  28. * each input LPoints[i] must have at least 2 points
  29. *
  30. * returns number of points or -1 on error
  31. */
  32. int dig_get_poly_points(int n_lines, struct line_pnts **LPoints,
  33. int *direction, /* line direction: > 0 or < 0 */
  34. struct line_pnts *BPoints)
  35. {
  36. register int i, j, point, start, end, inc;
  37. struct line_pnts *Points;
  38. int n_points;
  39. BPoints->n_points = 0;
  40. if (n_lines < 1) {
  41. return 0;
  42. }
  43. /* Calc required space */
  44. n_points = 0;
  45. for (i = 0; i < n_lines; i++) {
  46. Points = LPoints[i];
  47. n_points += Points->n_points - 1; /* each line from first to last - 1 */
  48. }
  49. n_points++; /* last point */
  50. if (0 > dig_alloc_points(BPoints, n_points))
  51. return (-1);
  52. point = 0;
  53. j = 0;
  54. for (i = 0; i < n_lines; i++) {
  55. Points = LPoints[i];
  56. if (direction[i] > 0) {
  57. start = 0;
  58. end = Points->n_points - 1;
  59. inc = 1;
  60. }
  61. else {
  62. start = Points->n_points - 1;
  63. end = 0;
  64. inc = -1;
  65. }
  66. for (j = start; j != end; j += inc) {
  67. BPoints->x[point] = Points->x[j];
  68. BPoints->y[point] = Points->y[j];
  69. point++;
  70. }
  71. }
  72. /* last point */
  73. BPoints->x[point] = Points->x[j];
  74. BPoints->y[point] = Points->y[j];
  75. BPoints->n_points = n_points;
  76. return (BPoints->n_points);
  77. }
  78. /*
  79. * calculate signed area size for polygon
  80. *
  81. * points must be closed polygon with first point = last point
  82. *
  83. * formula modified from:
  84. * Sunday, Daniel. 2002. Fast Polygon Area and Newell Normal Computation.
  85. * Journal of Graphics Tools; 7(2):9-13.
  86. *
  87. * returns signed area, positive for clockwise, negative for
  88. * counterclockwise, 0 for degenerate
  89. */
  90. int dig_find_area_poly(struct line_pnts *Points, double *totalarea)
  91. {
  92. int i, n = Points->n_points - 1;
  93. double *x, *y;
  94. double tot_area;
  95. /* TODO: check if results are still accurate without pruning *Points first
  96. * consecutive duplicate vertices should in theory result in wrong area size */
  97. x = Points->x;
  98. y = Points->y;
  99. /* first point 0 == point n */
  100. tot_area = y[0] * (x[1] - x[n - 1]);
  101. for (i = 1; i < n; i++) {
  102. tot_area += y[i] * (x[i + 1] - x[i - 1]);
  103. }
  104. *totalarea = 0.5 * tot_area;
  105. return (0);
  106. }
  107. /*
  108. * find orientation of polygon (clockwise or counterclockwise)
  109. * faster than signed area for > 4 vertices
  110. *
  111. * points must be closed polygon with first point = last point
  112. *
  113. * this code uses bits and pieces from softSurfer and GEOS
  114. * (C) 2000 softSurfer (www.softsurfer.com)
  115. * (C) 2006 Refractions Research Inc.
  116. *
  117. * and now copes with partially collapsed boundaries
  118. * the code is long but fast
  119. * it can be written much shorter, but that comes with speed penalty
  120. *
  121. * returns orientation, positive for CW, negative for CCW, 0 for degenerate
  122. */
  123. double dig_find_poly_orientation(struct line_pnts *Points)
  124. {
  125. unsigned int pnext, pprev, pcur = 0;
  126. unsigned int lastpoint = Points->n_points - 1;
  127. double *x, *y, orientation;
  128. x = Points->x;
  129. y = Points->y;
  130. /* first find leftmost highest vertex of the polygon */
  131. for (pnext = 1; pnext < lastpoint; pnext++) {
  132. if (y[pnext] < y[pcur])
  133. continue;
  134. else if (y[pnext] == y[pcur]) { /* just as high */
  135. if (x[pnext] > x[pcur]) /* but to the right */
  136. continue;
  137. }
  138. pcur = pnext; /* a new leftmost highest vertex */
  139. }
  140. /* Points are not pruned, so ... */
  141. pnext = pcur;
  142. pprev = pcur;
  143. /* find next distinct point */
  144. do {
  145. if (pnext < lastpoint - 1)
  146. pnext++;
  147. else
  148. pnext = 0;
  149. } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);
  150. /* find previous distinct point */
  151. do {
  152. if (pprev > 0)
  153. pprev--;
  154. else
  155. pprev = lastpoint - 1;
  156. } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);
  157. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  158. * rather use robust determinant of Olivier Devillers? */
  159. orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  160. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  161. if (orientation)
  162. return orientation;
  163. /* orientation is 0, can happen with dirty boundaries, next check */
  164. /* find rightmost highest vertex of the polygon */
  165. pcur = 0;
  166. for (pnext = 1; pnext < lastpoint; pnext++) {
  167. if (y[pnext] < y[pcur])
  168. continue;
  169. else if (y[pnext] == y[pcur]) { /* just as high */
  170. if (x[pnext] < x[pcur]) /* but to the left */
  171. continue;
  172. }
  173. pcur = pnext; /* a new rightmost highest vertex */
  174. }
  175. /* Points are not pruned, so ... */
  176. pnext = pcur;
  177. pprev = pcur;
  178. /* find next distinct point */
  179. do {
  180. if (pnext < lastpoint - 1)
  181. pnext++;
  182. else
  183. pnext = 0;
  184. } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);
  185. /* find previous distinct point */
  186. do {
  187. if (pprev > 0)
  188. pprev--;
  189. else
  190. pprev = lastpoint - 1;
  191. } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);
  192. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  193. * rather use robust determinant of Olivier Devillers? */
  194. orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  195. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  196. if (orientation)
  197. return orientation;
  198. /* orientation is 0, next check */
  199. /* find leftmost lowest vertex of the polygon */
  200. pcur = 0;
  201. for (pnext = 1; pnext < lastpoint; pnext++) {
  202. if (y[pnext] > y[pcur])
  203. continue;
  204. else if (y[pnext] == y[pcur]) { /* just as low */
  205. if (x[pnext] > x[pcur]) /* but to the right */
  206. continue;
  207. }
  208. pcur = pnext; /* a new leftmost lowest vertex */
  209. }
  210. /* Points are not pruned, so ... */
  211. pnext = pcur;
  212. pprev = pcur;
  213. /* find next distinct point */
  214. do {
  215. if (pnext < lastpoint - 1)
  216. pnext++;
  217. else
  218. pnext = 0;
  219. } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);
  220. /* find previous distinct point */
  221. do {
  222. if (pprev > 0)
  223. pprev--;
  224. else
  225. pprev = lastpoint - 1;
  226. } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);
  227. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  228. * rather use robust determinant of Olivier Devillers? */
  229. orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  230. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  231. if (orientation)
  232. return orientation;
  233. /* orientation is 0, last check */
  234. /* find rightmost lowest vertex of the polygon */
  235. pcur = 0;
  236. for (pnext = 1; pnext < lastpoint; pnext++) {
  237. if (y[pnext] > y[pcur])
  238. continue;
  239. else if (y[pnext] == y[pcur]) { /* just as low */
  240. if (x[pnext] < x[pcur]) /* but to the left */
  241. continue;
  242. }
  243. pcur = pnext; /* a new rightmost lowest vertex */
  244. }
  245. /* Points are not pruned, so ... */
  246. pnext = pcur;
  247. pprev = pcur;
  248. /* find next distinct point */
  249. do {
  250. if (pnext < lastpoint - 1)
  251. pnext++;
  252. else
  253. pnext = 0;
  254. } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);
  255. /* find previous distinct point */
  256. do {
  257. if (pprev > 0)
  258. pprev--;
  259. else
  260. pprev = lastpoint - 1;
  261. } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);
  262. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  263. * rather use robust determinant of Olivier Devillers? */
  264. orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  265. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  266. return orientation; /* 0 for degenerate */
  267. }