poly.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. *
  9. * PURPOSE: Lower level functions for reading/writing/manipulating vectors.
  10. *
  11. * COPYRIGHT: (C) 2009 by the GRASS Development Team
  12. *
  13. * This program is free software under the GNU General Public
  14. * License (>=v2). Read the file COPYING that comes with GRASS
  15. * for details.
  16. *
  17. *****************************************************************************/
  18. #include <math.h>
  19. #include <grass/Vect.h>
  20. #ifndef HUGE_VAL
  21. #define HUGE_VAL 9999999999999.0
  22. #endif
  23. /*
  24. ** fills BPoints (must be inited previously) by points from input
  25. ** array LPoints. Each input LPoints[i] must have at least 2 points.
  26. **
  27. ** returns number of points or -1 on error
  28. */
  29. int dig_get_poly_points(int n_lines, struct line_pnts **LPoints, int *direction, /* line direction: > 0 or < 0 */
  30. struct line_pnts *BPoints)
  31. {
  32. register int i, j, point, start, end, inc;
  33. struct line_pnts *Points;
  34. int n_points;
  35. BPoints->n_points = 0;
  36. if (n_lines < 1) {
  37. return 0;
  38. }
  39. /* Calc required space */
  40. n_points = 0;
  41. for (i = 0; i < n_lines; i++) {
  42. Points = LPoints[i];
  43. n_points += Points->n_points - 1; /* each line from first to last - 1 */
  44. }
  45. n_points++; /* last point */
  46. if (0 > dig_alloc_points(BPoints, n_points))
  47. return (-1);
  48. point = 0;
  49. j = 0;
  50. for (i = 0; i < n_lines; i++) {
  51. Points = LPoints[i];
  52. if (direction[i] > 0) {
  53. start = 0;
  54. end = Points->n_points - 1;
  55. inc = 1;
  56. }
  57. else {
  58. start = Points->n_points - 1;
  59. end = 0;
  60. inc = -1;
  61. }
  62. for (j = start; j != end; j += inc) {
  63. BPoints->x[point] = Points->x[j];
  64. BPoints->y[point] = Points->y[j];
  65. }
  66. point++;
  67. }
  68. /* last point */
  69. BPoints->x[point] = Points->x[j];
  70. BPoints->y[point] = Points->y[j];
  71. BPoints->n_points = n_points;
  72. return (BPoints->n_points);
  73. }
  74. /*
  75. ** Calculate signed area size for polygon.
  76. **
  77. ** Total area is positive for clockwise and negative for counterclockwise
  78. ** Formula modified from
  79. ** Sunday, Daniel. 2002. Fast Polygon Area and Newell Normal Computation.
  80. ** Journal of Graphics Tools; 7(2):9-13.
  81. */
  82. int dig_find_area_poly(struct line_pnts *Points, double *totalarea)
  83. {
  84. int i, n = Points->n_points - 1;
  85. double *x, *y;
  86. double tot_area;
  87. /* prune first with Vect_line_prune(Points) for speed? */
  88. x = Points->x;
  89. y = Points->y;
  90. /* first point 0 == point n */
  91. tot_area = y[0] * (x[1] - x[n - 1]);
  92. for (i = 1; i < n; i++) {
  93. tot_area += y[i] * (x[i + 1] - x[i - 1]);
  94. }
  95. *totalarea = 0.5 * tot_area;
  96. return (0);
  97. }
  98. /*
  99. * find orientation of polygon
  100. * faster than signed area
  101. *
  102. * return value is positive for CW, negative for CCW, 0 for degenerate
  103. *
  104. * Points must be closed polygon
  105. *
  106. * this code uses bits and pieces from softSurfer and GEOS
  107. * (C) 2000 softSurfer (www.softsurfer.com)
  108. * (C) 2006 Refractions Research Inc.
  109. */
  110. double dig_find_poly_orientation(struct line_pnts *Points)
  111. {
  112. unsigned int pnext, pprev, pcur = 0;
  113. unsigned int lastpoint = Points->n_points - 1;
  114. double *x, *y;
  115. /* first find leftmost highest vertex of the polygon */
  116. /* could also be leftmost lowest, rightmost highest or rightmost lowest */
  117. x = Points->x;
  118. y = Points->y;
  119. for (pnext = 1; pnext < lastpoint; pnext++) {
  120. if (y[pnext] < y[pcur])
  121. continue;
  122. else if (y[pnext] == y[pcur]) { /* just as high */
  123. if (x[pnext] < x[pcur]) /* but to the right */
  124. continue;
  125. }
  126. pcur = pnext; /* a new leftmost highest vertex */
  127. }
  128. /* Points are not pruned, so ... */
  129. /* find next distinct point */
  130. if (pcur == lastpoint)
  131. pnext = 0;
  132. else
  133. pnext = pcur + 1;
  134. while (pnext != pcur) {
  135. if (x[pcur] != x[pnext] || y[pcur] != y[pnext])
  136. break;
  137. if (pnext < lastpoint - 1)
  138. pnext++;
  139. else
  140. pnext = 0;
  141. }
  142. /* find previous distinct point */
  143. if (pcur == 0)
  144. pcur = lastpoint;
  145. pprev = pcur - 1;
  146. while (pprev != pcur) {
  147. if (x[pcur] != x[pprev] || y[pcur] != y[pprev])
  148. break;
  149. if (pprev > 1)
  150. pprev--;
  151. else
  152. pprev = lastpoint;
  153. }
  154. /* orientation at vertex pcur == signed area for triangle pprev, pcur, pnext
  155. * rather use robust determinant of Olivier Devillers? */
  156. return (x[pnext] - x[pprev]) * (y[pcur] - y[pprev])
  157. - (x[pcur] - x[pprev]) * (y[pnext] - y[pprev]);
  158. }