Polygon.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <grass/gis.h>
  5. #include "driverlib.h"
  6. #include "htmlmap.h"
  7. #define RAD_DEG 57.29578
  8. static void delete_point(int *x, int *y, int count)
  9. {
  10. int i;
  11. for (i = 0; i < count; i++) {
  12. x[i] = x[i + 1];
  13. y[i] = y[i + 1];
  14. }
  15. }
  16. static double find_azimuth(double x1, double y1, double x2, double y2)
  17. {
  18. double xx, yy;
  19. xx = x1 - x2;
  20. yy = y1 - y2;
  21. if (x1 == x2) {
  22. return (y2 > y1) ? 90.0 : 270.0;
  23. }
  24. else {
  25. if (y2 < y1) {
  26. if (x2 > x1) {
  27. return 360.0 + (RAD_DEG * atan(yy / xx));
  28. }
  29. else {
  30. return 180.0 + (RAD_DEG * atan(yy / xx));
  31. }
  32. }
  33. else {
  34. if (x2 > x1) {
  35. return (RAD_DEG * atan(yy / xx));
  36. }
  37. else {
  38. return 180.0 + (RAD_DEG * atan(yy / xx));
  39. }
  40. }
  41. }
  42. }
  43. void html_polygon(const double *px, const double *py, int n)
  44. {
  45. struct MapPoly *new;
  46. int i;
  47. int delta_x, delta_y;
  48. int min_x, max_x, min_y, max_y;
  49. double min_azimuth = 1.0;
  50. double azimuth1, azimuth2, diff1, diff2;
  51. int *x = G_malloc(n * sizeof(int));
  52. int *y = G_malloc(n * sizeof(int));
  53. for (i = 0; i < n; i++) {
  54. x[i] = (int) floor(px[i] + 0.5);
  55. y[i] = (int) floor(py[i] + 0.5);
  56. }
  57. /*
  58. * remove points that have adjacent duplicates or have differences of
  59. * less the the minimum allowed. remove end points that are same as
  60. * the begin point (ending point = starting point is added
  61. * during Graph_Clse)
  62. */
  63. i = 0;
  64. while (i < (n - 1)) {
  65. delta_x = x[i] - x[i + 1];
  66. if (delta_x < 0)
  67. delta_x = -delta_x;
  68. delta_y = y[i] - y[i + 1];
  69. if (delta_y < 0)
  70. delta_y = -delta_y;
  71. if ((x[i] == x[i + 1] && y[i] == y[i + 1]) ||
  72. (delta_x <= html.MINIMUM_DIST && delta_y <= html.MINIMUM_DIST)) {
  73. delete_point(&x[i + 1], &y[i + 1], n - i - 1);
  74. --n;
  75. }
  76. else {
  77. ++i;
  78. }
  79. }
  80. /* perform same checks for last point & first point */
  81. while (1) {
  82. delta_x = x[0] - x[n - 1];
  83. if (delta_x < 0)
  84. delta_x = -delta_x;
  85. delta_y = y[0] - y[n - 1];
  86. if (delta_y < 0)
  87. delta_y = -delta_y;
  88. if ((x[0] == x[n - 1] && y[0] == y[n - 1]) ||
  89. (delta_x <= html.MINIMUM_DIST && delta_y <= html.MINIMUM_DIST)) {
  90. --n;
  91. }
  92. else {
  93. break;
  94. }
  95. }
  96. /*
  97. * if a polygon has either x or y extents less than the bounding box
  98. * minimum, ignore it
  99. *
  100. */
  101. min_x = max_x = x[0];
  102. min_y = max_y = y[0];
  103. for (i = 0; i < n; i++) {
  104. if (x[i] < min_x)
  105. min_x = x[i];
  106. if (x[i] > max_x)
  107. max_x = x[i];
  108. if (y[i] < min_y)
  109. min_y = y[i];
  110. if (y[i] > max_y)
  111. max_y = y[i];
  112. }
  113. delta_x = max_x - min_x;
  114. delta_y = max_y - min_y;
  115. if (delta_x < html.BBOX_MINIMUM || delta_y < html.BBOX_MINIMUM) {
  116. n = 0;
  117. }
  118. /*
  119. * remove points in excess of MAX_POINTS vertices
  120. */
  121. while (n > html.MAX_POINTS) {
  122. for (i = 0; i < (n - 2); i++) {
  123. /*
  124. * see if middle point can be removed, by checking if the
  125. * relative bearing to the middle is less than our current tolerance
  126. */
  127. azimuth1 = find_azimuth((double)x[i], (double)y[i],
  128. (double)x[i + 1], (double)y[i + 1]);
  129. azimuth2 = find_azimuth((double)x[i], (double)y[i],
  130. (double)x[i + 2], (double)y[i + 2]);
  131. diff1 = fmod(fabs((azimuth2 + 360.0) - azimuth1), 360.0);
  132. diff2 = fmod(fabs((azimuth1 + 360.0) - azimuth2), 360.0);
  133. if (diff1 <= min_azimuth || diff2 <= min_azimuth) {
  134. delete_point(&x[i + 1], &y[i + 1], n - i - 1);
  135. --n;
  136. ++i;
  137. /* either stop deleting points because we're less than 100,
  138. or keep deleting points with the same difference as this
  139. one (which might make a smaller polygon yet).
  140. if (n <= 100) {
  141. break;
  142. }
  143. */
  144. }
  145. }
  146. /* increase minimum azimuth difference for next round */
  147. min_azimuth += 1.0;
  148. }
  149. /*
  150. * copy remaining points into a new MapPoly
  151. */
  152. if (n >= 3) {
  153. new = (struct MapPoly *)G_malloc(sizeof(struct MapPoly));
  154. /* grab the last text string written as url */
  155. new->url = G_store(html.last_text);
  156. /* hook up new MapPoly into list */
  157. new->next_poly = NULL;
  158. *html.tail = new;
  159. html.tail = &(new->next_poly);
  160. new->num_pts = n;
  161. new->x_pts = x;
  162. new->y_pts = y;
  163. }
  164. else {
  165. G_free(x);
  166. G_free(y);
  167. }
  168. }