poly.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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/vector.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. /* tot_area = 0.0;
  105. for (i = 1; i < Points->n_points; i++) {
  106. tot_area += (x[i] - x[i - 1]) * (y[i] + y[i - 1]);
  107. } */
  108. *totalarea = 0.5 * tot_area;
  109. return (0);
  110. }
  111. /*
  112. * find orientation of polygon (clockwise or counterclockwise)
  113. * in theory faster than signed area for > 4 vertices, but is not robust
  114. * against special cases
  115. * use dig_find_area_poly instead
  116. *
  117. * points must be closed polygon with first point = last point
  118. *
  119. * this code uses bits and pieces from softSurfer and GEOS
  120. * (C) 2000 softSurfer (www.softsurfer.com)
  121. * (C) 2006 Refractions Research Inc.
  122. *
  123. * copes with partially collapsed boundaries and 8-shaped isles
  124. * the code is long and not much faster than dig_find_area_poly
  125. * it can be written much shorter, but that comes with speed penalty
  126. *
  127. * returns orientation, positive for CW, negative for CCW, 0 for degenerate
  128. */
  129. double dig_find_poly_orientation(struct line_pnts *Points)
  130. {
  131. unsigned int pnext, pprev, pcur = 0;
  132. unsigned int lastpoint = Points->n_points - 1;
  133. double *x, *y, orientation;
  134. x = Points->x;
  135. y = Points->y;
  136. /* first find leftmost highest vertex of the polygon */
  137. for (pnext = 1; pnext < lastpoint; pnext++) {
  138. if (y[pnext] < y[pcur])
  139. continue;
  140. else if (y[pnext] == y[pcur]) { /* just as high */
  141. if (x[pnext] > x[pcur]) /* but to the right */
  142. continue;
  143. if (x[pnext] == x[pcur]) { /* duplicate point, self-intersecting polygon ? */
  144. pprev = (pcur == 0 ? lastpoint - 1 : pcur - 1);
  145. if (y[pnext - 1] < y[pprev])
  146. continue;
  147. }
  148. }
  149. pcur = pnext; /* a new leftmost highest vertex */
  150. }
  151. /* Points are not pruned, so ... */
  152. pnext = pcur;
  153. pprev = pcur;
  154. /* find next distinct point */
  155. do {
  156. if (pnext < lastpoint - 1)
  157. pnext++;
  158. else
  159. pnext = 0;
  160. } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);
  161. /* find previous distinct point */
  162. do {
  163. if (pprev > 0)
  164. pprev--;
  165. else
  166. pprev = lastpoint - 1;
  167. } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);
  168. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  169. * rather use robust determinant of Olivier Devillers? */
  170. orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  171. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  172. if (orientation)
  173. return orientation;
  174. /* orientation is 0, can happen with dirty boundaries, next check */
  175. /* find rightmost highest vertex of the polygon */
  176. pcur = 0;
  177. for (pnext = 1; pnext < lastpoint; pnext++) {
  178. if (y[pnext] < y[pcur])
  179. continue;
  180. else if (y[pnext] == y[pcur]) { /* just as high */
  181. if (x[pnext] < x[pcur]) /* but to the left */
  182. continue;
  183. if (x[pnext] == x[pcur]) { /* duplicate point, self-intersecting polygon ? */
  184. pprev = (pcur == 0 ? lastpoint - 1 : pcur - 1);
  185. if (y[pnext - 1] < y[pprev])
  186. continue;
  187. }
  188. }
  189. pcur = pnext; /* a new rightmost highest vertex */
  190. }
  191. /* Points are not pruned, so ... */
  192. pnext = pcur;
  193. pprev = pcur;
  194. /* find next distinct point */
  195. do {
  196. if (pnext < lastpoint - 1)
  197. pnext++;
  198. else
  199. pnext = 0;
  200. } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);
  201. /* find previous distinct point */
  202. do {
  203. if (pprev > 0)
  204. pprev--;
  205. else
  206. pprev = lastpoint - 1;
  207. } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);
  208. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  209. * rather use robust determinant of Olivier Devillers? */
  210. orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  211. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  212. if (orientation)
  213. return orientation;
  214. /* orientation is 0, next check */
  215. /* find leftmost lowest vertex of the polygon */
  216. pcur = 0;
  217. for (pnext = 1; pnext < lastpoint; pnext++) {
  218. if (y[pnext] > y[pcur])
  219. continue;
  220. else if (y[pnext] == y[pcur]) { /* just as low */
  221. if (x[pnext] > x[pcur]) /* but to the right */
  222. continue;
  223. if (x[pnext] == x[pcur]) { /* duplicate point, self-intersecting polygon ? */
  224. pprev = (pcur == 0 ? lastpoint - 1 : pcur - 1);
  225. if (y[pnext - 1] > y[pprev])
  226. continue;
  227. }
  228. }
  229. pcur = pnext; /* a new leftmost lowest vertex */
  230. }
  231. /* Points are not pruned, so ... */
  232. pnext = pcur;
  233. pprev = pcur;
  234. /* find next distinct point */
  235. do {
  236. if (pnext < lastpoint - 1)
  237. pnext++;
  238. else
  239. pnext = 0;
  240. } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);
  241. /* find previous distinct point */
  242. do {
  243. if (pprev > 0)
  244. pprev--;
  245. else
  246. pprev = lastpoint - 1;
  247. } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);
  248. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  249. * rather use robust determinant of Olivier Devillers? */
  250. orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  251. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  252. if (orientation)
  253. return orientation;
  254. /* orientation is 0, last check */
  255. /* find rightmost lowest vertex of the polygon */
  256. pcur = 0;
  257. for (pnext = 1; pnext < lastpoint; pnext++) {
  258. if (y[pnext] > y[pcur])
  259. continue;
  260. else if (y[pnext] == y[pcur]) { /* just as low */
  261. if (x[pnext] < x[pcur]) /* but to the left */
  262. continue;
  263. if (x[pnext] == x[pcur]) { /* duplicate point, self-intersecting polygon ? */
  264. pprev = (pcur == 0 ? lastpoint - 1 : pcur - 1);
  265. if (y[pnext - 1] > y[pprev])
  266. continue;
  267. }
  268. }
  269. pcur = pnext; /* a new rightmost lowest vertex */
  270. }
  271. /* Points are not pruned, so ... */
  272. pnext = pcur;
  273. pprev = pcur;
  274. /* find next distinct point */
  275. do {
  276. if (pnext < lastpoint - 1)
  277. pnext++;
  278. else
  279. pnext = 0;
  280. } while (pnext != pcur && x[pcur] == x[pnext] && y[pcur] == y[pnext]);
  281. /* find previous distinct point */
  282. do {
  283. if (pprev > 0)
  284. pprev--;
  285. else
  286. pprev = lastpoint - 1;
  287. } while (pprev != pcur && x[pcur] == x[pprev] && y[pcur] == y[pprev]);
  288. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  289. * rather use robust determinant of Olivier Devillers? */
  290. orientation = (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  291. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  292. return orientation; /* 0 for degenerate */
  293. }