visibility.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include <assert.h>
  11. #include <pathplan/vis.h>
  12. #include <stdbool.h>
  13. #include <stdlib.h>
  14. #include <util/alloc.h>
  15. /* allocArray:
  16. * Allocate a VxV array of COORD values.
  17. * (array2 is a pointer to an array of pointers; the array is
  18. * accessed in row-major order.)
  19. * The values in the array are initialized to 0.
  20. * Add extra rows.
  21. */
  22. static array2 allocArray(int V, int extra)
  23. {
  24. int i;
  25. assert(V >= 0);
  26. array2 arr = gv_calloc(V + extra, sizeof(COORD*));
  27. COORD *p = gv_calloc((size_t)V * (size_t)V, sizeof(COORD));
  28. for (i = 0; i < V; i++) {
  29. arr[i] = p;
  30. p += V;
  31. }
  32. for (i = V; i < V + extra; i++)
  33. arr[i] = NULL;
  34. return arr;
  35. }
  36. /* area2:
  37. * Returns twice the area of triangle abc.
  38. */
  39. COORD area2(Ppoint_t a, Ppoint_t b, Ppoint_t c)
  40. {
  41. return (a.y - b.y) * (c.x - b.x) - (c.y - b.y) * (a.x - b.x);
  42. }
  43. /* wind:
  44. * Returns 1, 0, -1 if the points abc are counterclockwise,
  45. * collinear, or clockwise.
  46. */
  47. int wind(Ppoint_t a, Ppoint_t b, Ppoint_t c)
  48. {
  49. COORD w;
  50. w = (a.y - b.y) * (c.x - b.x) - (c.y - b.y) * (a.x - b.x);
  51. /* need to allow for small math errors. seen with "gcc -O2 -mcpu=i686 -ffast-math" */
  52. return w > .0001 ? 1 : (w < -.0001 ? -1 : 0);
  53. }
  54. /* inBetween:
  55. * Return true if c is in (a,b), assuming a,b,c are collinear.
  56. */
  57. static bool inBetween(Ppoint_t a, Ppoint_t b, Ppoint_t c)
  58. {
  59. if (a.x != b.x) /* not vertical */
  60. return (a.x < c.x && c.x < b.x) || (b.x < c.x && c.x < a.x);
  61. else
  62. return (a.y < c.y && c.y < b.y) || (b.y < c.y && c.y < a.y);
  63. }
  64. /* intersect:
  65. * Returns true if the segment [c,d] blocks a and b from seeing each other.
  66. * More specifically, returns true iff c or d lies on (a,b) or the two
  67. * segments intersect as open sets.
  68. */
  69. static bool intersect(Ppoint_t a, Ppoint_t b, Ppoint_t c, Ppoint_t d)
  70. {
  71. int a_abc;
  72. int a_abd;
  73. int a_cda;
  74. int a_cdb;
  75. a_abc = wind(a, b, c);
  76. if (a_abc == 0 && inBetween(a, b, c)) {
  77. return true;
  78. }
  79. a_abd = wind(a, b, d);
  80. if (a_abd == 0 && inBetween(a, b, d)) {
  81. return true;
  82. }
  83. a_cda = wind(c, d, a);
  84. a_cdb = wind(c, d, b);
  85. /* True if c and d are on opposite sides of ab,
  86. * and a and b are on opposite sides of cd.
  87. */
  88. return a_abc * a_abd < 0 && a_cda * a_cdb < 0;
  89. }
  90. /* in_cone:
  91. * Returns true iff point b is in the cone a0,a1,a2
  92. * NB: the cone is considered a closed set
  93. */
  94. static bool in_cone(Ppoint_t a0, Ppoint_t a1, Ppoint_t a2, Ppoint_t b)
  95. {
  96. int m = wind(b, a0, a1);
  97. int p = wind(b, a1, a2);
  98. if (wind(a0, a1, a2) > 0)
  99. return m >= 0 && p >= 0; /* convex at a */
  100. else
  101. return m >= 0 || p >= 0; /* reflex at a */
  102. }
  103. /* dist2:
  104. * Returns the square of the distance between points a and b.
  105. */
  106. COORD dist2(Ppoint_t a, Ppoint_t b)
  107. {
  108. COORD delx = a.x - b.x;
  109. COORD dely = a.y - b.y;
  110. return delx * delx + dely * dely;
  111. }
  112. /* dist:
  113. * Returns the distance between points a and b.
  114. */
  115. static COORD dist(Ppoint_t a, Ppoint_t b)
  116. {
  117. return sqrt(dist2(a, b));
  118. }
  119. static bool inCone(int i, int j, Ppoint_t pts[], int nextPt[], int prevPt[])
  120. {
  121. return in_cone(pts[prevPt[i]], pts[i], pts[nextPt[i]], pts[j]);
  122. }
  123. /* clear:
  124. * Return true if no polygon line segment non-trivially intersects
  125. * the segment [pti,ptj], ignoring segments in [start,end).
  126. */
  127. static bool clear(Ppoint_t pti, Ppoint_t ptj,
  128. int start, int end,
  129. int V, Ppoint_t pts[], int nextPt[])
  130. {
  131. int k;
  132. for (k = 0; k < start; k++) {
  133. if (intersect(pti, ptj, pts[k], pts[nextPt[k]]))
  134. return false;
  135. }
  136. for (k = end; k < V; k++) {
  137. if (intersect(pti, ptj, pts[k], pts[nextPt[k]]))
  138. return false;
  139. }
  140. return true;
  141. }
  142. /* compVis:
  143. * Compute visibility graph of vertices of polygons.
  144. * Only do polygons from index startp to end.
  145. * If two nodes cannot see each other, the matrix entry is 0.
  146. * If two nodes can see each other, the matrix entry is the distance
  147. * between them.
  148. */
  149. static void compVis(vconfig_t * conf) {
  150. int V = conf->N;
  151. Ppoint_t *pts = conf->P;
  152. int *nextPt = conf->next;
  153. int *prevPt = conf->prev;
  154. array2 wadj = conf->vis;
  155. int j, i, previ;
  156. COORD d;
  157. for (i = 0; i < V; i++) {
  158. /* add edge between i and previ.
  159. * Note that this works for the cases of polygons of 1 and 2
  160. * vertices, though needless work is done.
  161. */
  162. previ = prevPt[i];
  163. d = dist(pts[i], pts[previ]);
  164. wadj[i][previ] = d;
  165. wadj[previ][i] = d;
  166. /* Check remaining, earlier vertices */
  167. if (previ == i - 1)
  168. j = i - 2;
  169. else
  170. j = i - 1;
  171. for (; j >= 0; j--) {
  172. if (inCone(i, j, pts, nextPt, prevPt) &&
  173. inCone(j, i, pts, nextPt, prevPt) &&
  174. clear(pts[i], pts[j], V, V, V, pts, nextPt)) {
  175. /* if i and j see each other, add edge */
  176. d = dist(pts[i], pts[j]);
  177. wadj[i][j] = d;
  178. wadj[j][i] = d;
  179. }
  180. }
  181. }
  182. }
  183. /* visibility:
  184. * Given a vconfig_t conf, representing polygonal barriers,
  185. * compute the visibility graph of the vertices of conf.
  186. * The graph is stored in conf->vis.
  187. */
  188. void visibility(vconfig_t * conf)
  189. {
  190. conf->vis = allocArray(conf->N, 2);
  191. compVis(conf);
  192. }
  193. /* polyhit:
  194. * Given a vconfig_t conf, as above, and a point,
  195. * return the index of the polygon that contains
  196. * the point, or else POLYID_NONE.
  197. */
  198. static int polyhit(vconfig_t * conf, Ppoint_t p)
  199. {
  200. int i;
  201. Ppoly_t poly;
  202. for (i = 0; i < conf->Npoly; i++) {
  203. poly.ps = &(conf->P[conf->start[i]]);
  204. poly.pn = (size_t)(conf->start[i + 1] - conf->start[i]);
  205. if (in_poly(poly, p))
  206. return i;
  207. }
  208. return POLYID_NONE;
  209. }
  210. /* ptVis:
  211. * Given a vconfig_t conf, representing polygonal barriers,
  212. * and a point within one of the polygons, compute the point's
  213. * visibility vector relative to the vertices of the remaining
  214. * polygons, i.e., pretend the argument polygon is invisible.
  215. *
  216. * If pp is NIL, ptVis computes the visibility vector for p
  217. * relative to all barrier vertices.
  218. */
  219. COORD *ptVis(vconfig_t * conf, int pp, Ppoint_t p)
  220. {
  221. const int V = conf->N;
  222. Ppoint_t *pts = conf->P;
  223. int *nextPt = conf->next;
  224. int *prevPt = conf->prev;
  225. int k;
  226. int start, end;
  227. Ppoint_t pk;
  228. COORD d;
  229. COORD *vadj = gv_calloc(V + 2, sizeof(COORD));
  230. if (pp == POLYID_UNKNOWN)
  231. pp = polyhit(conf, p);
  232. if (pp >= 0) {
  233. start = conf->start[pp];
  234. end = conf->start[pp + 1];
  235. } else {
  236. start = V;
  237. end = V;
  238. }
  239. for (k = 0; k < start; k++) {
  240. pk = pts[k];
  241. if (in_cone(pts[prevPt[k]], pk, pts[nextPt[k]], p) &&
  242. clear(p, pk, start, end, V, pts, nextPt)) {
  243. /* if p and pk see each other, add edge */
  244. d = dist(p, pk);
  245. vadj[k] = d;
  246. } else
  247. vadj[k] = 0;
  248. }
  249. for (k = start; k < end; k++)
  250. vadj[k] = 0;
  251. for (k = end; k < V; k++) {
  252. pk = pts[k];
  253. if (in_cone(pts[prevPt[k]], pk, pts[nextPt[k]], p) &&
  254. clear(p, pk, start, end, V, pts, nextPt)) {
  255. /* if p and pk see each other, add edge */
  256. d = dist(p, pk);
  257. vadj[k] = d;
  258. } else
  259. vadj[k] = 0;
  260. }
  261. vadj[V] = 0;
  262. vadj[V + 1] = 0;
  263. return vadj;
  264. }
  265. /* directVis:
  266. * Given two points, return true if the points can directly see each other.
  267. * If a point is associated with a polygon, the edges of the polygon
  268. * are ignored when checking visibility.
  269. */
  270. bool directVis(Ppoint_t p, int pp, Ppoint_t q, int qp, vconfig_t * conf)
  271. {
  272. int V = conf->N;
  273. Ppoint_t *pts = conf->P;
  274. int *nextPt = conf->next;
  275. int k;
  276. int s1, e1;
  277. int s2, e2;
  278. if (pp < 0) {
  279. s1 = 0;
  280. e1 = 0;
  281. if (qp < 0) {
  282. s2 = 0;
  283. e2 = 0;
  284. } else {
  285. s2 = conf->start[qp];
  286. e2 = conf->start[qp + 1];
  287. }
  288. } else if (qp < 0) {
  289. s1 = 0;
  290. e1 = 0;
  291. s2 = conf->start[pp];
  292. e2 = conf->start[pp + 1];
  293. } else if (pp <= qp) {
  294. s1 = conf->start[pp];
  295. e1 = conf->start[pp + 1];
  296. s2 = conf->start[qp];
  297. e2 = conf->start[qp + 1];
  298. } else {
  299. s1 = conf->start[qp];
  300. e1 = conf->start[qp + 1];
  301. s2 = conf->start[pp];
  302. e2 = conf->start[pp + 1];
  303. }
  304. for (k = 0; k < s1; k++) {
  305. if (intersect(p, q, pts[k], pts[nextPt[k]]))
  306. return false;
  307. }
  308. for (k = e1; k < s2; k++) {
  309. if (intersect(p, q, pts[k], pts[nextPt[k]]))
  310. return false;
  311. }
  312. for (k = e2; k < V; k++) {
  313. if (intersect(p, q, pts[k], pts[nextPt[k]]))
  314. return false;
  315. }
  316. return true;
  317. }