poly.c 9.0 KB

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