poly.c 9.3 KB

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