sw_main.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <grass/gis.h>
  5. #include <grass/vector.h>
  6. #include "sw_defs.h"
  7. #include "defs.h"
  8. int triangulate, sorted, plot, debug, mode3d;
  9. struct Site *sites;
  10. int nsites;
  11. int siteidx;
  12. int sqrt_nsites;
  13. int nvertices;
  14. struct Freelist sfl;
  15. struct Site *bottomsite;
  16. int nedges;
  17. struct Freelist efl;
  18. double xmin, xmax, ymin, ymax, deltax, deltay;
  19. struct Freelist hfl;
  20. struct Halfedge *ELleftend, *ELrightend;
  21. int ELhashsize;
  22. struct Halfedge **ELhash;
  23. int PQhashsize;
  24. struct Halfedge *PQhash;
  25. int PQcount;
  26. int PQmin;
  27. struct Cell_head Window;
  28. struct bound_box Box;
  29. struct Map_info In, Out;
  30. int Type;
  31. int All;
  32. /* sort sites on y, then x, coord */
  33. int scomp(const void *v1, const void *v2)
  34. {
  35. struct Point *s1 = (struct Point *)v1;
  36. struct Point *s2 = (struct Point *)v2;
  37. if (s1->y < s2->y)
  38. return (-1);
  39. if (s1->y > s2->y)
  40. return (1);
  41. if (s1->x < s2->x)
  42. return (-1);
  43. if (s1->x > s2->x)
  44. return (1);
  45. return (0);
  46. }
  47. /* return a single in-storage site */
  48. struct Site *nextone(void)
  49. {
  50. struct Site *s;
  51. if (siteidx < nsites) {
  52. s = &sites[siteidx];
  53. siteidx++;
  54. return (s);
  55. }
  56. else
  57. return ((struct Site *)NULL);
  58. }
  59. /* removes duplicate sites that would break the voronoi alghoritm */
  60. void removeDuplicates()
  61. {
  62. int i, j;
  63. int foundDupe;
  64. i = j = 1;
  65. foundDupe = 0;
  66. while (i < nsites)
  67. if (mode3d) {
  68. if (sites[i].coord.x == sites[i - 1].coord.x &&
  69. sites[i].coord.y == sites[i - 1].coord.y &&
  70. sites[i].coord.z == sites[i - 1].coord.z)
  71. i++;
  72. else {
  73. if (i != j)
  74. sites[j] = sites[i];
  75. i++;
  76. j++;;
  77. }
  78. }
  79. else {
  80. if (sites[i].coord.x == sites[i - 1].coord.x &&
  81. sites[i].coord.y == sites[i - 1].coord.y)
  82. i++;
  83. else {
  84. if (i != j)
  85. sites[j] = sites[i];
  86. i++;
  87. j++;;
  88. }
  89. }
  90. if (j != nsites) {
  91. nsites = j;
  92. sites = (struct Site *)G_realloc(sites, nsites * sizeof(struct Site));
  93. }
  94. }
  95. /* read all sites, sort, and compute xmin, xmax, ymin, ymax */
  96. int readsites(void)
  97. {
  98. int nlines, line;
  99. struct line_pnts *Points;
  100. Points = Vect_new_line_struct();
  101. nlines = Vect_get_num_lines(&In);
  102. nsites = 0;
  103. sites = (struct Site *)G_malloc(nlines * sizeof(struct Site));
  104. for (line = 1; line <= nlines; line++) {
  105. int type;
  106. G_percent(line, nlines, 2);
  107. type = Vect_read_line(&In, Points, NULL, line);
  108. if (!(type & GV_POINTS))
  109. continue;
  110. if (!All) {
  111. if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &Box))
  112. continue;
  113. }
  114. sites[nsites].coord.x = Points->x[0];
  115. sites[nsites].coord.y = Points->y[0];
  116. if (mode3d) {
  117. G_debug(3, "Points->z[0]: %f", Points->z[0]);
  118. sites[nsites].coord.z = Points->z[0];
  119. }
  120. else
  121. sites[nsites].coord.z = 0.0;
  122. if (nsites > 1) {
  123. if (xmin > sites[nsites].coord.x)
  124. xmin = sites[nsites].coord.x;
  125. if (xmax < sites[nsites].coord.x)
  126. xmax = sites[nsites].coord.x;
  127. if (ymin > sites[nsites].coord.y)
  128. ymin = sites[nsites].coord.y;
  129. if (ymax < sites[nsites].coord.y)
  130. ymax = sites[nsites].coord.y;
  131. }
  132. else {
  133. xmin = xmax = sites[nsites].coord.x;
  134. ymin = ymax = sites[nsites].coord.y;
  135. }
  136. nsites++;
  137. }
  138. if (nsites < nlines)
  139. sites =
  140. (struct Site *)G_realloc(sites,
  141. (nsites) * sizeof(struct Site));
  142. qsort(sites, nsites, sizeof(struct Site), scomp);
  143. removeDuplicates();
  144. return 0;
  145. }
  146. /* read one site */
  147. struct Site *readone(void)
  148. {
  149. struct Site *s;
  150. s = (struct Site *)getfree(&sfl);
  151. s->refcnt = 0;
  152. s->sitenbr = siteidx;
  153. siteidx++;
  154. if (scanf("%lf %lf", &(s->coord.x), &(s->coord.y)) == EOF)
  155. return ((struct Site *)NULL);
  156. return (s);
  157. }