dgraph.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /*!
  2. \file lib/vector/Vlib/dgraph.c
  3. \brief Vector library - intersection (lower level functions)
  4. Higher level functions for reading/writing/manipulating vectors.
  5. (C) 2008-2009 by the GRASS Development Team
  6. This program is free software under the GNU General Public License
  7. (>=v2). Read the file COPYING that comes with GRASS for details.
  8. \author Rewritten by Rosen Matev (Google Summer of Code 2008)
  9. */
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <math.h>
  13. #include <grass/vector.h>
  14. #include <grass/glocale.h>
  15. #include "dgraph.h"
  16. #include "e_intersect.h"
  17. #define LENGTH(DX, DY) (sqrt((DX*DX)+(DY*DY)))
  18. #ifndef MIN
  19. #define MIN(X,Y) ((X<Y)?X:Y)
  20. #endif
  21. #ifndef MAX
  22. #define MAX(X,Y) ((X>Y)?X:Y)
  23. #endif
  24. #define PI M_PI
  25. struct intersection_point
  26. {
  27. double x;
  28. double y;
  29. int group; /* IPs with very similar dist will be in the same group */
  30. };
  31. struct seg_intersection
  32. {
  33. int with; /* second segment */
  34. int ip; /* index of the IP */
  35. double dist; /* distance from first point of first segment to intersection point (IP) */
  36. };
  37. struct seg_intersection_list
  38. {
  39. int count;
  40. int allocated;
  41. struct seg_intersection *a;
  42. };
  43. struct seg_intersections
  44. {
  45. int ipcount;
  46. int ipallocated;
  47. struct intersection_point *ip;
  48. int ilcount;
  49. struct seg_intersection_list *il;
  50. int group_count;
  51. };
  52. struct seg_intersections *create_si_struct(int segments_count)
  53. {
  54. struct seg_intersections *si;
  55. int i;
  56. si = G_malloc(sizeof(struct seg_intersections));
  57. si->ipcount = 0;
  58. si->ipallocated = segments_count + 16;
  59. si->ip = G_malloc((si->ipallocated) * sizeof(struct intersection_point));
  60. si->ilcount = segments_count;
  61. si->il = G_malloc(segments_count * sizeof(struct seg_intersection_list));
  62. for (i = 0; i < segments_count; i++) {
  63. si->il[i].count = 0;
  64. si->il[i].allocated = 0;
  65. si->il[i].a = NULL;
  66. }
  67. return si;
  68. }
  69. void destroy_si_struct(struct seg_intersections *si)
  70. {
  71. int i;
  72. for (i = 0; i < si->ilcount; i++)
  73. G_free(si->il[i].a);
  74. G_free(si->il);
  75. G_free(si->ip);
  76. G_free(si);
  77. return;
  78. }
  79. /* internal use */
  80. void add_ipoint1(struct seg_intersection_list *il, int with, double dist,
  81. int ip)
  82. {
  83. struct seg_intersection *s;
  84. if (il->count == il->allocated) {
  85. il->allocated += 4;
  86. il->a =
  87. G_realloc(il->a,
  88. (il->allocated) * sizeof(struct seg_intersection));
  89. }
  90. s = &(il->a[il->count]);
  91. s->with = with;
  92. s->ip = ip;
  93. s->dist = dist;
  94. il->count++;
  95. return;
  96. }
  97. /* adds intersection point to the structure */
  98. void add_ipoint(const struct line_pnts *Points, int first_seg, int second_seg,
  99. double x, double y, struct seg_intersections *si)
  100. {
  101. struct intersection_point *t;
  102. int ip;
  103. G_debug(4, "add_ipoint()");
  104. if (si->ipcount == si->ipallocated) {
  105. si->ipallocated += 16;
  106. si->ip =
  107. G_realloc(si->ip,
  108. (si->ipallocated) * sizeof(struct intersection_point));
  109. }
  110. ip = si->ipcount;
  111. t = &(si->ip[ip]);
  112. t->x = x;
  113. t->y = y;
  114. t->group = -1;
  115. si->ipcount++;
  116. add_ipoint1(&(si->il[first_seg]), second_seg,
  117. LENGTH((Points->x[first_seg] - x),
  118. (Points->y[first_seg] - y)), ip);
  119. if (second_seg >= 0)
  120. add_ipoint1(&(si->il[second_seg]), first_seg,
  121. LENGTH((Points->x[second_seg] - x),
  122. (Points->y[second_seg] - y)), ip);
  123. }
  124. void sort_intersection_list(struct seg_intersection_list *il)
  125. {
  126. int n, i, j, min;
  127. struct seg_intersection t;
  128. G_debug(4, "sort_intersection_list()");
  129. n = il->count;
  130. G_debug(4, " n=%d", n);
  131. for (i = 0; i < n - 1; i++) {
  132. min = i;
  133. for (j = i + 1; j < n; j++) {
  134. if (il->a[j].dist < il->a[min].dist) {
  135. min = j;
  136. }
  137. }
  138. if (min != i) {
  139. t = il->a[i];
  140. il->a[i] = il->a[min];
  141. il->a[min] = t;
  142. }
  143. }
  144. return;
  145. }
  146. int compare(const void *a, const void *b)
  147. {
  148. struct intersection_point *aa, *bb;
  149. aa = *((struct intersection_point **)a);
  150. bb = *((struct intersection_point **)b);
  151. if (aa->x < bb->x)
  152. return -1;
  153. else if (aa->x > bb->x)
  154. return 1;
  155. else
  156. return (aa->y < bb->y) ? -1 : ((aa->y > bb->y) ? 1 : 0);
  157. }
  158. /* O(Points->n_points) time */
  159. double get_epsilon(struct line_pnts *Points)
  160. {
  161. int i, np;
  162. double min, t;
  163. double *x, *y;
  164. np = Points->n_points;
  165. x = Points->x;
  166. y = Points->y;
  167. min = MAX(fabs(x[1] - x[0]), fabs(y[1] - y[0]));
  168. for (i = 1; i <= np - 2; i++) {
  169. t = MAX(fabs(x[i + 1] - x[i]), fabs(y[i + 1] - y[i]));
  170. if ((t > 0) && (t < min)) {
  171. min = t;
  172. }
  173. }
  174. /* ??? is 0.001 ok ??? */
  175. return min * 0.000001;
  176. }
  177. /* currently O(n*n); future implementation O(nlogn) */
  178. struct seg_intersections *find_all_intersections(const struct line_pnts *Points)
  179. {
  180. int i, j, np;
  181. int group, t;
  182. int looped;
  183. /* double EPSILON = 0.00000001; */
  184. double EPSILON = GRASS_EPSILON;
  185. double *x, *y;
  186. double x1, y1, x2, y2;
  187. int res;
  188. /*int res2
  189. double x1_, y1_, x2_, y2_, z1_, z2_; */
  190. struct seg_intersections *si;
  191. struct seg_intersection_list *il;
  192. struct intersection_point **sorted;
  193. G_debug(3, "find_all_intersections()");
  194. np = Points->n_points;
  195. x = Points->x;
  196. y = Points->y;
  197. si = create_si_struct(np - 1);
  198. looped = ((x[0] == x[np - 1]) && (y[0] == y[np - 1]));
  199. G_debug(3, " looped=%d", looped);
  200. G_debug(3, " finding intersections...");
  201. for (i = 0; i < np - 1; i++) {
  202. for (j = i + 1; j < np - 1; j++) {
  203. G_debug(4, " checking %d-%d %d-%d", i, i + 1, j, j + 1);
  204. /*res = segment_intersection_2d_e(x[i], y[i], x[i+1], y[i+1], x[j], y[j], x[j+1], y[j+1], &x1, &y1, &x2, &y2); */
  205. res =
  206. segment_intersection_2d(x[i], y[i], x[i + 1], y[i + 1], x[j],
  207. y[j], x[j + 1], y[j + 1], &x1, &y1,
  208. &x2, &y2);
  209. /* res2 = segment_intersection_2d_e(x[i], y[i], x[i+1], y[i+1], x[j], y[j], x[j+1], y[j+1], &x1_, &y1_, &x2_, &y2_);
  210. if ((res != res2) || ((res != 0) && (x1!=x1_ || y1!=y1_)) ) {
  211. G_debug(1, "exact=%d orig=%d", res, res2);
  212. segment_intersection_2d_test(x[i], y[i], x[i+1], y[i+1], x[j], y[j], x[j+1], y[j+1], &x1, &y1, &x2, &y2);
  213. }
  214. */
  215. G_debug(4, " intersection type = %d", res);
  216. if (res == 1) {
  217. add_ipoint(Points, i, j, x1, y1, si);
  218. }
  219. else if ((res >= 2) && (res <= 5)) {
  220. add_ipoint(Points, i, j, x1, y1, si);
  221. add_ipoint(Points, i, j, x2, y2, si);
  222. }
  223. }
  224. }
  225. if (!looped) {
  226. /* these are not really intersection points */
  227. add_ipoint(Points, 0, -1, Points->x[0], Points->y[0], si);
  228. add_ipoint(Points, np - 2, -1, Points->x[np - 1], Points->y[np - 1],
  229. si);
  230. }
  231. G_debug(3, " finding intersections...done");
  232. G_debug(3, " postprocessing...");
  233. if (si->ipallocated > si->ipcount) {
  234. si->ipallocated = si->ipcount;
  235. si->ip =
  236. G_realloc(si->ip,
  237. (si->ipcount) * sizeof(struct intersection_point));
  238. }
  239. for (i = 0; i < si->ilcount; i++) {
  240. il = &(si->il[i]);
  241. if (il->allocated > il->count) {
  242. il->allocated = il->count;
  243. il->a =
  244. G_realloc(il->a,
  245. (il->count) * sizeof(struct seg_intersection));
  246. }
  247. if (il->count > 0) {
  248. sort_intersection_list(il);
  249. /* is it ok to use qsort here ? */
  250. }
  251. }
  252. /* si->ip will not be reallocated any more so we can use pointers */
  253. sorted = G_malloc((si->ipcount) * sizeof(struct intersection_point *));
  254. for (i = 0; i < si->ipcount; i++)
  255. sorted[i] = &(si->ip[i]);
  256. qsort(sorted, si->ipcount, sizeof(struct intersection_point *), compare);
  257. /* assign groups */
  258. group = 0; /* next available group number */
  259. for (i = 0; i < si->ipcount; i++) {
  260. t = group;
  261. for (j = i - 1; j >= 0; j--) {
  262. if (!FEQUAL(sorted[j]->x, sorted[i]->x, EPSILON))
  263. /* if (!almost_equal(sorted[j]->x, sorted[i]->x, 16)) */
  264. break;
  265. if (FEQUAL(sorted[j]->y, sorted[i]->y, EPSILON)) {
  266. /* if (almost_equal(sorted[j]->y, sorted[i]->y, 16)) { */
  267. t = sorted[j]->group;
  268. break;
  269. }
  270. }
  271. G_debug(4, " group=%d, ip=%d", t,
  272. (int)(sorted[i] - &(si->ip[0])));
  273. sorted[i]->group = t;
  274. if (t == group)
  275. group++;
  276. }
  277. si->group_count = group;
  278. G_debug(3, " postprocessing...done");
  279. /* output contents of si */
  280. for (i = 0; i < si->ilcount; i++) {
  281. G_debug(4, "%d-%d :", i, i + 1);
  282. for (j = 0; j < si->il[i].count; j++) {
  283. G_debug(4, " %d-%d, group=%d", si->il[i].a[j].with,
  284. si->il[i].a[j].with + 1, si->ip[si->il[i].a[j].ip].group);
  285. G_debug(4, " dist=%.18f", si->il[i].a[j].dist);
  286. G_debug(4, " x=%.18f, y=%.18f",
  287. si->ip[si->il[i].a[j].ip].x, si->ip[si->il[i].a[j].ip].y);
  288. }
  289. }
  290. G_free(sorted);
  291. return si;
  292. }
  293. /* create's graph with n vertices and allocates memory for e edges */
  294. /* trying to add more than e edges, produces fatal error */
  295. struct planar_graph *pg_create_struct(int n, int e)
  296. {
  297. struct planar_graph *pg;
  298. pg = G_malloc(sizeof(struct planar_graph));
  299. pg->vcount = n;
  300. pg->v = G_malloc(n * sizeof(struct pg_vertex));
  301. memset(pg->v, 0, n * sizeof(struct pg_vertex));
  302. pg->ecount = 0;
  303. pg->eallocated = MAX(e, 0);
  304. pg->e = NULL;
  305. pg->e = G_malloc(e * sizeof(struct pg_edge));
  306. return pg;
  307. }
  308. void pg_destroy_struct(struct planar_graph *pg)
  309. {
  310. int i;
  311. for (i = 0; i < pg->vcount; i++) {
  312. G_free(pg->v[i].edges);
  313. G_free(pg->v[i].angles);
  314. }
  315. G_free(pg->v);
  316. G_free(pg->e);
  317. G_free(pg);
  318. }
  319. /* v1 and v2 must be valid */
  320. int pg_existsedge(struct planar_graph *pg, int v1, int v2)
  321. {
  322. struct pg_vertex *v;
  323. struct pg_edge *e;
  324. int i, ecount;
  325. if (pg->v[v1].ecount <= pg->v[v2].ecount)
  326. v = &(pg->v[v1]);
  327. else
  328. v = &(pg->v[v2]);
  329. ecount = v->ecount;
  330. for (i = 0; i < ecount; i++) {
  331. e = v->edges[i];
  332. if (((e->v1 == v1) && (e->v2 == v2)) ||
  333. ((e->v1 == v2) && (e->v2 == v1)))
  334. return 1;
  335. }
  336. return 0;
  337. }
  338. /* for internal use */
  339. void pg_addedge1(struct pg_vertex *v, struct pg_edge *e)
  340. {
  341. if (v->ecount == v->eallocated) {
  342. v->eallocated += 4;
  343. v->edges =
  344. G_realloc(v->edges, (v->eallocated) * sizeof(struct pg_edge *));
  345. }
  346. v->edges[v->ecount] = e;
  347. v->ecount++;
  348. }
  349. void pg_addedge(struct planar_graph *pg, int v1, int v2)
  350. {
  351. struct pg_edge *e;
  352. G_debug(4, "pg_addedge(), v1=%d, v2=%d", v1, v2);
  353. if ((v1 == v2) || (v1 < 0) || (v1 >= pg->vcount) || (v2 < 0) ||
  354. (v2 >= pg->vcount)) {
  355. G_fatal_error(" pg_addedge(), v1 and/or v2 is invalid");
  356. return;
  357. }
  358. if (pg_existsedge(pg, v1, v2))
  359. return;
  360. if (pg->ecount == pg->eallocated) {
  361. G_fatal_error(_("Trying to add more edges to the planar_graph "
  362. "than the initial allocation size allows"));
  363. }
  364. e = &(pg->e[pg->ecount]);
  365. e->v1 = v1;
  366. e->v2 = v2;
  367. e->winding_left = 0; /* winding is undefined if the corresponding side is not visited */
  368. e->winding_right = 0;
  369. e->visited_left = 0;
  370. e->visited_right = 0;
  371. pg->ecount++;
  372. pg_addedge1(&(pg->v[v1]), e);
  373. pg_addedge1(&(pg->v[v2]), e);
  374. return;
  375. }
  376. struct planar_graph *pg_create(const struct line_pnts *Points)
  377. {
  378. struct seg_intersections *si;
  379. struct planar_graph *pg;
  380. struct intersection_point *ip;
  381. struct pg_vertex *vert;
  382. struct pg_edge *edge;
  383. int i, j, t, v;
  384. G_debug(3, "pg_create()");
  385. si = find_all_intersections(Points);
  386. pg = pg_create_struct(si->group_count, 2 * (si->ipcount));
  387. /* set vertices info */
  388. for (i = 0; i < si->ipcount; i++) {
  389. ip = &(si->ip[i]);
  390. t = ip->group;
  391. pg->v[t].x = ip->x;
  392. pg->v[t].y = ip->y;
  393. }
  394. /* add all edges */
  395. for (i = 0; i < si->ilcount; i++) {
  396. v = si->ip[si->il[i].a[0].ip].group;
  397. for (j = 1; j < si->il[i].count; j++) {
  398. t = si->ip[si->il[i].a[j].ip].group;
  399. if (t != v) {
  400. pg_addedge(pg, v, t); /* edge direction is v ---> t */
  401. v = t;
  402. }
  403. }
  404. }
  405. /* precalculate angles with 0x */
  406. for (i = 0; i < pg->vcount; i++) {
  407. vert = &(pg->v[i]);
  408. vert->angles = G_malloc((vert->ecount) * sizeof(double));
  409. for (j = 0; j < vert->ecount; j++) {
  410. edge = vert->edges[j];
  411. t = (edge->v1 != i) ? (edge->v1) : (edge->v2);
  412. vert->angles[j] =
  413. atan2(pg->v[t].y - vert->y, pg->v[t].x - vert->x);
  414. }
  415. }
  416. destroy_si_struct(si);
  417. /*
  418. I'm not sure if shrinking of the allocated memory always preserves it's physical place.
  419. That's why I don't want to do this:
  420. if (pg->ecount < pg->eallocated) {
  421. pg->eallocated = pg->ecount;
  422. pg->e = G_realloc(pg->e, (pg->ecount)*sizeof(struct pg_edge));
  423. }
  424. */
  425. /* very time consuming */
  426. /*
  427. for (i = 0; i < pg->vcount; i++) {
  428. if (pg->v[i].ecount < pg->v[i].eallocated) {
  429. pg->v[i].eallocated = pg->v[i].ecount;
  430. pg->v[i].edges = G_realloc(pg->v[i].edges, (pg->v[i].ecount)*sizeof(struct pg_edges));
  431. }
  432. }
  433. */
  434. /* output pg */
  435. for (i = 0; i < pg->vcount; i++) {
  436. G_debug(4, " vertex %d (%g, %g)", i, pg->v[i].x, pg->v[i].y);
  437. for (j = 0; j < pg->v[i].ecount; j++) {
  438. G_debug(4, " edge %d-%d", pg->v[i].edges[j]->v1,
  439. pg->v[i].edges[j]->v2);
  440. }
  441. }
  442. return pg;
  443. }