poly.c 8.3 KB

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