dgraph.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*!
  2. \file 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 <grass/config.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include <grass/vector.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. #include "dgraph.h"
  18. #include "e_intersect.h"
  19. #define LENGTH(DX, DY) (sqrt((DX*DX)+(DY*DY)))
  20. #ifndef MIN
  21. #define MIN(X,Y) ((X<Y)?X:Y)
  22. #endif
  23. #ifndef MAX
  24. #define MAX(X,Y) ((X>Y)?X:Y)
  25. #endif
  26. #define PI M_PI
  27. struct intersection_point
  28. {
  29. double x;
  30. double y;
  31. int group; /* IPs with very similar dist will be in the same group */
  32. };
  33. struct seg_intersection
  34. {
  35. int with; /* second segment */
  36. int ip; /* index of the IP */
  37. double dist; /* distance from first point of first segment to intersection point (IP) */
  38. };
  39. struct seg_intersection_list
  40. {
  41. int count;
  42. int allocated;
  43. struct seg_intersection *a;
  44. };
  45. struct seg_intersections
  46. {
  47. int ipcount;
  48. int ipallocated;
  49. struct intersection_point *ip;
  50. int ilcount;
  51. struct seg_intersection_list *il;
  52. int group_count;
  53. };
  54. struct seg_intersections *create_si_struct(int segments_count)
  55. {
  56. struct seg_intersections *si;
  57. int i;
  58. si = G_malloc(sizeof(struct seg_intersections));
  59. si->ipcount = 0;
  60. si->ipallocated = segments_count + 16;
  61. si->ip = G_malloc((si->ipallocated) * sizeof(struct intersection_point));
  62. si->ilcount = segments_count;
  63. si->il = G_malloc(segments_count * sizeof(struct seg_intersection_list));
  64. for (i = 0; i < segments_count; i++) {
  65. si->il[i].count = 0;
  66. si->il[i].allocated = 0;
  67. si->il[i].a = NULL;
  68. }
  69. return si;
  70. }
  71. void destroy_si_struct(struct seg_intersections *si)
  72. {
  73. int i;
  74. for (i = 0; i < si->ilcount; i++)
  75. G_free(si->il[i].a);
  76. G_free(si->il);
  77. G_free(si->ip);
  78. G_free(si);
  79. return;
  80. }
  81. /* internal use */
  82. void add_ipoint1(struct seg_intersection_list *il, int with, double dist,
  83. int ip)
  84. {
  85. struct seg_intersection *s;
  86. if (il->count == il->allocated) {
  87. il->allocated += 4;
  88. il->a =
  89. G_realloc(il->a,
  90. (il->allocated) * sizeof(struct seg_intersection));
  91. }
  92. s = &(il->a[il->count]);
  93. s->with = with;
  94. s->ip = ip;
  95. s->dist = dist;
  96. il->count++;
  97. return;
  98. }
  99. /* adds intersection point to the structure */
  100. void add_ipoint(const struct line_pnts *Points, int first_seg, int second_seg,
  101. double x, double y, struct seg_intersections *si)
  102. {
  103. struct intersection_point *t;
  104. int ip;
  105. G_debug(4, "add_ipoint()");
  106. if (si->ipcount == si->ipallocated) {
  107. si->ipallocated += 16;
  108. si->ip =
  109. G_realloc(si->ip,
  110. (si->ipallocated) * sizeof(struct intersection_point));
  111. }
  112. ip = si->ipcount;
  113. t = &(si->ip[ip]);
  114. t->x = x;
  115. t->y = y;
  116. t->group = -1;
  117. si->ipcount++;
  118. add_ipoint1(&(si->il[first_seg]), second_seg,
  119. LENGTH((Points->x[first_seg] - x),
  120. (Points->y[first_seg] - y)), ip);
  121. if (second_seg >= 0)
  122. add_ipoint1(&(si->il[second_seg]), first_seg,
  123. LENGTH((Points->x[second_seg] - x),
  124. (Points->y[second_seg] - y)), ip);
  125. }
  126. void sort_intersection_list(struct seg_intersection_list *il)
  127. {
  128. int n, i, j, min;
  129. struct seg_intersection t;
  130. G_debug(4, "sort_intersection_list()");
  131. n = il->count;
  132. G_debug(4, " n=%d", n);
  133. for (i = 0; i < n - 1; i++) {
  134. min = i;
  135. for (j = i + 1; j < n; j++) {
  136. if (il->a[j].dist < il->a[min].dist) {
  137. min = j;
  138. }
  139. }
  140. if (min != i) {
  141. t = il->a[i];
  142. il->a[i] = il->a[min];
  143. il->a[min] = t;
  144. }
  145. }
  146. return;
  147. }
  148. int compare(const void *a, const void *b)
  149. {
  150. struct intersection_point *aa, *bb;
  151. aa = *((struct intersection_point **)a);
  152. bb = *((struct intersection_point **)b);
  153. if (aa->x < bb->x)
  154. return -1;
  155. else if (aa->x > bb->x)
  156. return 1;
  157. else
  158. return (aa->y < bb->y) ? -1 : ((aa->y > bb->y) ? 1 : 0);
  159. }
  160. /* O(Points->n_points) time */
  161. double get_epsilon(struct line_pnts *Points)
  162. {
  163. int i, np;
  164. double min, t;
  165. double *x, *y;
  166. np = Points->n_points;
  167. x = Points->x;
  168. y = Points->y;
  169. min = MAX(fabs(x[1] - x[0]), fabs(y[1] - y[0]));
  170. for (i = 1; i <= np - 2; i++) {
  171. t = MAX(fabs(x[i + 1] - x[i]), fabs(y[i + 1] - y[i]));
  172. if ((t > 0) && (t < min)) {
  173. min = t;
  174. }
  175. }
  176. /* ??? is 0.001 ok ??? */
  177. return min * 0.000001;
  178. }
  179. /* currently O(n*n); future implementation O(nlogn) */
  180. struct seg_intersections *find_all_intersections(const struct line_pnts *Points)
  181. {
  182. int i, j, np;
  183. int group, t;
  184. int looped;
  185. double EPSILON = 0.00000001;
  186. double *x, *y;
  187. double x1, y1, x2, y2;
  188. int res;
  189. /*int res2
  190. double x1_, y1_, x2_, y2_, z1_, z2_; */
  191. struct seg_intersections *si;
  192. struct seg_intersection_list *il;
  193. struct intersection_point **sorted;
  194. G_debug(3, "find_all_intersections()");
  195. np = Points->n_points;
  196. x = Points->x;
  197. y = Points->y;
  198. si = create_si_struct(np - 1);
  199. looped = ((x[0] == x[np - 1]) && (y[0] == y[np - 1]));
  200. G_debug(3, " looped=%d", looped);
  201. G_debug(3, " finding intersections...");
  202. for (i = 0; i < np - 1; i++) {
  203. for (j = i + 1; j < np - 1; j++) {
  204. G_debug(4, " checking %d-%d %d-%d", i, i + 1, j, j + 1);
  205. /*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); */
  206. res =
  207. segment_intersection_2d(x[i], y[i], x[i + 1], y[i + 1], x[j],
  208. y[j], x[j + 1], y[j + 1], &x1, &y1,
  209. &x2, &y2);
  210. /* 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_);
  211. if ((res != res2) || ((res != 0) && (x1!=x1_ || y1!=y1_)) ) {
  212. G_debug(0, "exact=%d orig=%d", res, res2);
  213. 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);
  214. }
  215. */
  216. G_debug(4, " intersection type = %d", res);
  217. if (res == 1) {
  218. add_ipoint(Points, i, j, x1, y1, si);
  219. }
  220. else if ((res >= 2) && (res <= 5)) {
  221. add_ipoint(Points, i, j, x1, y1, si);
  222. add_ipoint(Points, i, j, x2, y2, si);
  223. }
  224. }
  225. }
  226. if (!looped) {
  227. /* these are not really intersection points */
  228. add_ipoint(Points, 0, -1, Points->x[0], Points->y[0], si);
  229. add_ipoint(Points, np - 2, -1, Points->x[np - 1], Points->y[np - 1],
  230. si);
  231. }
  232. G_debug(3, " finding intersections...done");
  233. G_debug(3, " postprocessing...");
  234. if (si->ipallocated > si->ipcount) {
  235. si->ipallocated = si->ipcount;
  236. si->ip =
  237. G_realloc(si->ip,
  238. (si->ipcount) * sizeof(struct intersection_point));
  239. }
  240. for (i = 0; i < si->ilcount; i++) {
  241. il = &(si->il[i]);
  242. if (il->allocated > il->count) {
  243. il->allocated = il->count;
  244. il->a =
  245. G_realloc(il->a,
  246. (il->count) * sizeof(struct seg_intersection));
  247. }
  248. if (il->count > 0) {
  249. sort_intersection_list(il);
  250. /* is it ok to use qsort here ? */
  251. }
  252. }
  253. /* si->ip will not be reallocated any more so we can use pointers */
  254. sorted = G_malloc((si->ipcount) * sizeof(struct intersection_point *));
  255. for (i = 0; i < si->ipcount; i++)
  256. sorted[i] = &(si->ip[i]);
  257. qsort(sorted, si->ipcount, sizeof(struct intersection_point *), compare);
  258. /* assign groups */
  259. group = 0; /* next available group number */
  260. for (i = 0; i < si->ipcount; i++) {
  261. t = group;
  262. for (j = i - 1; j >= 0; j--) {
  263. if (!FEQUAL(sorted[j]->x, sorted[i]->x, EPSILON))
  264. /* if (!almost_equal(sorted[j]->x, sorted[i]->x, 16)) */
  265. break;
  266. if (FEQUAL(sorted[j]->y, sorted[i]->y, EPSILON)) {
  267. /* if (almost_equal(sorted[j]->y, sorted[i]->y, 16)) { */
  268. t = sorted[j]->group;
  269. break;
  270. }
  271. }
  272. G_debug(4, " group=%d, ip=%d", t,
  273. (int)(sorted[i] - &(si->ip[0])));
  274. sorted[i]->group = t;
  275. if (t == group)
  276. group++;
  277. }
  278. si->group_count = group;
  279. G_debug(3, " postprocessing...done");
  280. /* output contents of si */
  281. for (i = 0; i < si->ilcount; i++) {
  282. G_debug(4, "%d-%d :", i, i + 1);
  283. for (j = 0; j < si->il[i].count; j++) {
  284. G_debug(4, " %d-%d, group=%d", si->il[i].a[j].with,
  285. si->il[i].a[j].with + 1, si->ip[si->il[i].a[j].ip].group);
  286. G_debug(4, " dist=%.18f", si->il[i].a[j].dist);
  287. G_debug(4, " x=%.18f, y=%.18f",
  288. si->ip[si->il[i].a[j].ip].x, si->ip[si->il[i].a[j].ip].y);
  289. }
  290. }
  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. }