dgraph.c 13 KB

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