sw_main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <math.h>
  5. #include <grass/gis.h>
  6. #include <grass/vector.h>
  7. #include <grass/glocale.h>
  8. #include "sw_defs.h"
  9. #include "defs.h"
  10. int sorted, plot, debug, mode3d;
  11. struct Site *sites;
  12. int nsites;
  13. int siteidx;
  14. int sqrt_nsites;
  15. int nvertices;
  16. struct Freelist sfl;
  17. struct Site *bottomsite;
  18. int nedges;
  19. struct Freelist efl;
  20. double xmin, xmax, ymin, ymax, deltax, deltay;
  21. struct Freelist hfl;
  22. struct Halfedge *ELleftend, *ELrightend;
  23. int ELhashsize;
  24. struct Halfedge **ELhash;
  25. int PQhashsize;
  26. struct Halfedge *PQhash;
  27. int PQcount;
  28. int PQmin;
  29. struct Cell_head Window;
  30. struct bound_box Box;
  31. struct Map_info In, Out;
  32. int Type;
  33. int Field;
  34. int in_area;
  35. int skeleton;
  36. double segf;
  37. int nsites_alloc;
  38. /* sort sites on y, then x, coord */
  39. int scomp(const void *v1, const void *v2)
  40. {
  41. struct Point *s1 = (struct Point *)v1;
  42. struct Point *s2 = (struct Point *)v2;
  43. if (s1->y < s2->y)
  44. return (-1);
  45. if (s1->y > s2->y)
  46. return (1);
  47. if (s1->x < s2->x)
  48. return (-1);
  49. if (s1->x > s2->x)
  50. return (1);
  51. return (0);
  52. }
  53. /* return a single in-storage site */
  54. struct Site *nextone(void)
  55. {
  56. struct Site *s;
  57. if (siteidx < nsites) {
  58. s = &sites[siteidx];
  59. siteidx++;
  60. return (s);
  61. }
  62. else
  63. return ((struct Site *)NULL);
  64. }
  65. /* removes duplicate sites that would break the voronoi alghoritm */
  66. void removeDuplicates()
  67. {
  68. int i, j;
  69. i = j = 1;
  70. while (i < nsites)
  71. if (mode3d) {
  72. if (sites[i].coord.x == sites[i - 1].coord.x &&
  73. sites[i].coord.y == sites[i - 1].coord.y &&
  74. sites[i].coord.z == sites[i - 1].coord.z)
  75. i++;
  76. else {
  77. if (i != j)
  78. sites[j] = sites[i];
  79. i++;
  80. j++;;
  81. }
  82. }
  83. else {
  84. if (sites[i].coord.x == sites[i - 1].coord.x &&
  85. sites[i].coord.y == sites[i - 1].coord.y)
  86. i++;
  87. else {
  88. if (i != j)
  89. sites[j] = sites[i];
  90. i++;
  91. j++;;
  92. }
  93. }
  94. if (j != nsites) {
  95. nsites = j;
  96. sites = (struct Site *)G_realloc(sites, nsites * sizeof(struct Site));
  97. }
  98. }
  99. int addsite(double x, double y, double z, int id)
  100. {
  101. if (nsites >= nsites_alloc) {
  102. nsites_alloc += 100;
  103. sites =
  104. (struct Site *)G_realloc(sites,
  105. (nsites_alloc) * sizeof(struct Site));
  106. }
  107. sites[nsites].coord.x = x;
  108. sites[nsites].coord.y = y;
  109. sites[nsites].coord.z = z;
  110. sites[nsites].sitenbr = id;
  111. sites[nsites].refcnt = 0;
  112. if (nsites > 0) {
  113. if (xmin > sites[nsites].coord.x)
  114. xmin = sites[nsites].coord.x;
  115. if (xmax < sites[nsites].coord.x)
  116. xmax = sites[nsites].coord.x;
  117. if (ymin > sites[nsites].coord.y)
  118. ymin = sites[nsites].coord.y;
  119. if (ymax < sites[nsites].coord.y)
  120. ymax = sites[nsites].coord.y;
  121. }
  122. else {
  123. xmin = xmax = sites[nsites].coord.x;
  124. ymin = ymax = sites[nsites].coord.y;
  125. }
  126. nsites++;
  127. return nsites;
  128. }
  129. /* read all sites, sort, and compute xmin, xmax, ymin, ymax */
  130. int readsites(void)
  131. {
  132. int nlines, ltype;
  133. struct line_pnts *Points;
  134. struct line_cats *Cats;
  135. double z = 0.;
  136. Points = Vect_new_line_struct();
  137. Cats = Vect_new_cats_struct();
  138. nlines = Vect_get_num_primitives(&In, GV_POINTS);
  139. nsites = 0;
  140. sites = (struct Site *)G_malloc(nlines * sizeof(struct Site));
  141. Vect_set_constraint_type(&In, GV_POINTS);
  142. Vect_set_constraint_field(&In, Field);
  143. while(TRUE) {
  144. ltype = Vect_read_next_line(&In, Points, Cats);
  145. if(ltype == -2)
  146. break;
  147. if (!(ltype & GV_POINTS))
  148. continue;
  149. /* G_percent(Vect_get_next_line_id(&In), nlines, 2); */
  150. if (!Vect_point_in_box(Points->x[0], Points->y[0], 0.0, &Box))
  151. continue;
  152. if (mode3d) {
  153. G_debug(3, "Points->z[0]: %f", Points->z[0]);
  154. z = Points->z[0];
  155. }
  156. addsite(Points->x[0], Points->y[0], z, nsites);
  157. }
  158. if (nsites < 2) {
  159. const char *name = Vect_get_full_name(&In);
  160. Vect_close(&In);
  161. G_fatal_error(n_("Found %d point/centroid in <%s>, but at least 2 are needed",
  162. "Found %d points/centroids in <%s>, but at least 2 are needed",
  163. nsites),
  164. nsites, name);
  165. }
  166. if (nsites < nlines)
  167. sites =
  168. (struct Site *)G_realloc(sites,
  169. (nsites) * sizeof(struct Site));
  170. qsort(sites, nsites, sizeof(struct Site), scomp);
  171. removeDuplicates();
  172. Vect_destroy_line_struct(Points);
  173. Vect_destroy_cats_struct(Cats);
  174. return 0;
  175. }
  176. /* valid boundary: one area with centroid */
  177. int n_areas(int line, int *aid)
  178. {
  179. int larea, rarea, ncentroids;
  180. ncentroids = 0;
  181. Vect_get_line_areas(&In, line, &larea, &rarea);
  182. if (larea < 0)
  183. larea = Vect_get_isle_area(&In, -larea);
  184. if (larea > 0) {
  185. if (Vect_get_area_centroid(&In, larea) > 0) {
  186. ncentroids++;
  187. *aid = larea;
  188. }
  189. }
  190. if (rarea < 0)
  191. rarea = Vect_get_isle_area(&In, -rarea);
  192. if (rarea > 0) {
  193. if (Vect_get_area_centroid(&In, rarea) > 0) {
  194. ncentroids++;
  195. *aid = rarea;
  196. }
  197. }
  198. return ncentroids;
  199. }
  200. /* read all boundaries, sort, and compute xmin, xmax, ymin, ymax */
  201. int readbounds(void)
  202. {
  203. int line, nlines, ltype, node, nnodes;
  204. struct line_pnts *Points;
  205. struct line_cats *Cats;
  206. int i;
  207. int area_id;
  208. double x, y, z, x1, y1, z1;
  209. double maxdist, sdist, l, dx, dy, dz;
  210. int nconnected;
  211. struct ilist *linelist, *arealist;
  212. Points = Vect_new_line_struct();
  213. Cats = Vect_new_cats_struct();
  214. nlines = Vect_get_num_lines(&In);
  215. nsites = 0;
  216. nsites_alloc = nlines * 2;
  217. sites = (struct Site *)G_malloc(nsites_alloc * sizeof(struct Site));
  218. Vect_set_constraint_type(&In, GV_BOUNDARY);
  219. Vect_set_constraint_field(&In, Field);
  220. l = 0;
  221. maxdist = 0;
  222. for (line = 1; line <= nlines; line++) {
  223. if (!Vect_line_alive(&In, line))
  224. continue;
  225. ltype = Vect_get_line_type(&In, line);
  226. if (!(ltype & GV_BOUNDARY))
  227. continue;
  228. area_id = 0;
  229. if (n_areas(line, &area_id) != 1)
  230. continue;
  231. Vect_read_line(&In, Points, Cats, line);
  232. Vect_line_prune(Points);
  233. l += Vect_line_length(Points);
  234. nsites += Points->n_points;
  235. }
  236. if (nsites)
  237. maxdist = segf * l / nsites;
  238. G_verbose_message("Maximum segment length: %g", maxdist);
  239. nsites = 0;
  240. z = 0.;
  241. z1 = 0;
  242. dz = 0;
  243. for (line = 1; line <= nlines; line++) {
  244. if (!Vect_line_alive(&In, line))
  245. continue;
  246. ltype = Vect_get_line_type(&In, line);
  247. if (!(ltype & GV_BOUNDARY))
  248. continue;
  249. area_id = 0;
  250. if (n_areas(line, &area_id) != 1)
  251. continue;
  252. Vect_read_line(&In, Points, Cats, line);
  253. Vect_line_prune(Points);
  254. if (nsites + Points->n_points > nsites_alloc) {
  255. nsites_alloc = nsites + Points->n_points;
  256. sites =
  257. (struct Site *)G_realloc(sites,
  258. (nsites_alloc) * sizeof(struct Site));
  259. }
  260. for (i = 0; i < Points->n_points; i++) {
  261. if (!Vect_point_in_box(Points->x[i], Points->y[i], 0.0, &Box))
  262. continue;
  263. x = Points->x[i];
  264. y = Points->y[i];
  265. if (mode3d) {
  266. G_debug(3, "Points->z[i]: %f", Points->z[i]);
  267. z = Points->z[i];
  268. }
  269. if (i > 0 && i < Points->n_points - 1)
  270. addsite(x, y, z, area_id);
  271. /* densify */
  272. if (maxdist > 0 && i < Points->n_points - 1) {
  273. dx = Points->x[i + 1] - Points->x[i];
  274. dy = Points->y[i + 1] - Points->y[i];
  275. if (mode3d)
  276. dz = Points->z[i + 1] - Points->z[i];
  277. l = sqrt(dx * dx + dy * dy);
  278. if (l > maxdist) {
  279. int n = ceil(l / maxdist) + 0.5;
  280. double step = l / n;
  281. while (--n) {
  282. sdist = (step * n) / l;
  283. x1 = x + sdist * dx;
  284. y1 = y + sdist * dy;
  285. if (mode3d)
  286. z1 = z + sdist * dz;
  287. addsite(x1, y1, z1, area_id);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. /* process nodes */
  294. nnodes = Vect_get_num_nodes(&In);
  295. linelist = Vect_new_list();
  296. arealist = Vect_new_list();
  297. for (node = 1; node <= nnodes; node++) {
  298. Vect_get_node_coor(&In, node, &x, &y, &z);
  299. if (!mode3d)
  300. z = 0.;
  301. if (!Vect_point_in_box(x, y, 0.0, &Box))
  302. continue;
  303. /* count number of connected boundaries
  304. * must be >= 2 */
  305. /* count number of valid boundaries */
  306. nlines = Vect_get_node_n_lines(&In, node);
  307. nconnected = 0;
  308. Vect_reset_list(linelist);
  309. Vect_reset_list(arealist);
  310. for (i = 0; i < nlines; i++) {
  311. line = Vect_get_node_line(&In, node, i);
  312. ltype = Vect_get_line_type(&In, abs(line));
  313. if (!(ltype & GV_BOUNDARY))
  314. continue;
  315. if (n_areas(abs(line), &area_id) == 1) {
  316. Vect_list_append(linelist, line);
  317. Vect_list_append(arealist, area_id);
  318. nconnected++;
  319. }
  320. }
  321. if (arealist->n_values == 1) {
  322. area_id = arealist->value[0];
  323. addsite(x, y, z, area_id);
  324. }
  325. else if (arealist->n_values > 1) {
  326. /* displacement */
  327. double displace;
  328. if (fabs(x) > fabs(y))
  329. displace = d_ulp(fabs(x));
  330. else
  331. displace = d_ulp(fabs(y));
  332. displace *= 2;
  333. for (i = 0; i < linelist->n_values; i++) {
  334. line = linelist->value[i];
  335. if (n_areas(abs(line), &area_id) != 1)
  336. G_fatal_error(_("All boundaries in the list should be valid"));
  337. Vect_read_line(&In, Points, Cats, abs(line));
  338. Vect_line_prune(Points);
  339. if (Points->n_points < 2)
  340. G_fatal_error(_("Boundary is degenerate"));
  341. if (line < 0) {
  342. dx = Points->x[Points->n_points - 2] - Points->x[Points->n_points - 1];
  343. dy = Points->y[Points->n_points - 2] - Points->y[Points->n_points - 1];
  344. }
  345. else {
  346. dx = Points->x[1] - Points->x[0];
  347. dy = Points->y[1] - Points->y[0];
  348. }
  349. l = sqrt(dx * dx + dy * dy);
  350. if (displace > l)
  351. displace = l;
  352. }
  353. for (i = 0; i < linelist->n_values; i++) {
  354. line = linelist->value[i];
  355. if (n_areas(abs(line), &area_id) != 1)
  356. G_fatal_error(_("All boundaries in the list should be valid"));
  357. Vect_read_line(&In, Points, Cats, abs(line));
  358. Vect_line_prune(Points);
  359. if (Points->n_points < 2)
  360. G_fatal_error(_("Boundary is degenerate"));
  361. if (line < 0) {
  362. dx = Points->x[Points->n_points - 2] - Points->x[Points->n_points - 1];
  363. dy = Points->y[Points->n_points - 2] - Points->y[Points->n_points - 1];
  364. }
  365. else {
  366. dx = Points->x[1] - Points->x[0];
  367. dy = Points->y[1] - Points->y[0];
  368. }
  369. l = sqrt(dx * dx + dy * dy);
  370. if (l > displace * 2) {
  371. sdist = displace / l;
  372. x1 = x + sdist * dx;
  373. y1 = y + sdist * dy;
  374. z1 = 0;
  375. addsite(x1, y1, z1, area_id);
  376. }
  377. }
  378. }
  379. }
  380. if (nsites < 2) {
  381. const char *name = Vect_get_full_name(&In);
  382. Vect_close(&In);
  383. G_fatal_error(n_("Found %d vertex in <%s>, but at least 2 are needed",
  384. "Found %d vertices in <%s>, but at least 2 are needed",
  385. nsites),
  386. nsites, name);
  387. }
  388. qsort(sites, nsites, sizeof(struct Site), scomp);
  389. removeDuplicates();
  390. Vect_destroy_line_struct(Points);
  391. Vect_destroy_cats_struct(Cats);
  392. Vect_destroy_list(linelist);
  393. Vect_destroy_list(arealist);
  394. return 0;
  395. }