poly.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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 <neatogen/neato.h>
  11. #include <assert.h>
  12. #include <string.h>
  13. #include <math.h>
  14. #include <neatogen/poly.h>
  15. #include <common/geom.h>
  16. #include <neatogen/mem.h>
  17. #include <stdbool.h>
  18. #include <util/alloc.h>
  19. #include <util/streq.h>
  20. static const int BOX = 1;
  21. static const int CIRCLE = 2;
  22. static bool ISBOX(const Poly *p) { return p->kind & BOX; }
  23. static bool ISCIRCLE(const Poly *p) { return p->kind & CIRCLE; }
  24. static size_t maxcnt = 0;
  25. static Point *tp1 = NULL;
  26. static Point *tp2 = NULL;
  27. static Point *tp3 = NULL;
  28. void polyFree(void)
  29. {
  30. maxcnt = 0;
  31. free(tp1);
  32. free(tp2);
  33. free(tp3);
  34. tp1 = NULL;
  35. tp2 = NULL;
  36. tp3 = NULL;
  37. }
  38. void breakPoly(Poly * pp)
  39. {
  40. free(pp->verts);
  41. }
  42. static void bbox(Point *verts, size_t cnt, Point *o, Point *c) {
  43. double x_min, y_min, x_max, y_max;
  44. x_min = x_max = verts->x;
  45. y_min = y_max = verts->y;
  46. for (size_t i = 1; i < cnt; i++) {
  47. verts++;
  48. x_min = fmin(x_min, verts->x);
  49. y_min = fmin(y_min, verts->y);
  50. x_max = fmax(x_max, verts->x);
  51. y_max = fmax(y_max, verts->y);
  52. }
  53. o->x = x_min;
  54. o->y = y_min;
  55. c->x = x_max;
  56. c->y = y_max;
  57. }
  58. static void inflatePts(Point *verts, size_t cnt, double xmargin,
  59. double ymargin) {
  60. Point *cur;
  61. cur = &verts[0];
  62. for (size_t i = 0; i < cnt; i++) {
  63. cur->x *= xmargin;
  64. cur->y *= ymargin;
  65. cur++;
  66. }
  67. }
  68. static int isBox(Point *verts, size_t cnt) {
  69. if (cnt != 4)
  70. return 0;
  71. if (verts[0].y == verts[1].y)
  72. return ((verts[2].y == verts[3].y) &&
  73. (verts[0].x == verts[3].x) && (verts[1].x == verts[2].x));
  74. else
  75. return ((verts[0].x == verts[1].x) &&
  76. (verts[2].x == verts[3].x) &&
  77. (verts[0].y == verts[3].y) && (verts[1].y == verts[2].y));
  78. }
  79. static Point makeScaledTransPoint(double x, double y, double dx, double dy) {
  80. Point rv;
  81. rv.x = PS2INCH(x) + dx;
  82. rv.y = PS2INCH(y) + dy;
  83. return rv;
  84. }
  85. static Point makeScaledPoint(double x, double y)
  86. {
  87. Point rv;
  88. rv.x = PS2INCH(x);
  89. rv.y = PS2INCH(y);
  90. return rv;
  91. }
  92. static Point *genRound(Agnode_t *n, size_t *sidep, double xm, double ym) {
  93. size_t sides = 0;
  94. char *p = agget(n, "samplepoints");
  95. int isides = 0;
  96. if (p)
  97. isides = atoi(p);
  98. sides = isides < 3 ? DFLT_SAMPLE : (size_t)isides;
  99. Point *verts = gv_calloc(sides, sizeof(Point));
  100. for (size_t i = 0; i < sides; i++) {
  101. verts[i].x =
  102. (ND_width(n) / 2.0 + xm) * cos((double)i / (double)sides * M_PI * 2.0);
  103. verts[i].y =
  104. (ND_height(n) / 2.0 + ym) * sin((double)i / (double)sides * M_PI * 2.0);
  105. }
  106. *sidep = sides;
  107. return verts;
  108. }
  109. #define PUTPT(P,X,Y) ((P).x=(X),(P).y=(Y))
  110. int makeAddPoly(Poly *pp, Agnode_t *n, double xmargin, double ymargin) {
  111. size_t sides;
  112. Point *verts;
  113. polygon_t *poly;
  114. if (ND_clust(n)) {
  115. Point b;
  116. sides = 4;
  117. b.x = ND_width(n) / 2.0 + xmargin;
  118. b.y = ND_height(n) / 2.0 + ymargin;
  119. pp->kind = BOX;
  120. verts = gv_calloc(sides, sizeof(Point));
  121. PUTPT(verts[0], b.x, b.y);
  122. PUTPT(verts[1], -b.x, b.y);
  123. PUTPT(verts[2], -b.x, -b.y);
  124. PUTPT(verts[3], b.x, -b.y);
  125. } else
  126. switch (shapeOf(n)) {
  127. case SH_POLY:
  128. poly = ND_shape_info(n);
  129. sides = poly->sides;
  130. if (streq(ND_shape(n)->name, "box"))
  131. pp->kind = BOX;
  132. else if (streq(ND_shape(n)->name, "polygon")
  133. && isBox(poly->vertices, sides))
  134. pp->kind = BOX;
  135. else if ((poly->sides < 3) && poly->regular)
  136. pp->kind = CIRCLE;
  137. else
  138. pp->kind = 0;
  139. if (sides >= 3) { /* real polygon */
  140. verts = gv_calloc(sides, sizeof(Point));
  141. if (pp->kind == BOX) {
  142. /* To do an additive margin, we rely on knowing that
  143. * the vertices are CCW starting from the UR
  144. */
  145. verts[0].x = PS2INCH(poly->vertices[0].x) + xmargin;
  146. verts[0].y = PS2INCH(poly->vertices[0].y) + ymargin;
  147. verts[1].x = PS2INCH(poly->vertices[1].x) - xmargin;
  148. verts[1].y = PS2INCH(poly->vertices[1].y) + ymargin;
  149. verts[2].x = PS2INCH(poly->vertices[2].x) - xmargin;
  150. verts[2].y = PS2INCH(poly->vertices[2].y) - ymargin;
  151. verts[3].x = PS2INCH(poly->vertices[3].x) + xmargin;
  152. verts[3].y = PS2INCH(poly->vertices[3].y) - ymargin;
  153. }
  154. else {
  155. for (size_t i = 0; i < sides; i++) {
  156. const double h = hypot(poly->vertices[i].x, poly->vertices[i].y);
  157. verts[i].x = poly->vertices[i].x * (1.0 + xmargin/h);
  158. verts[i].y = poly->vertices[i].y * (1.0 + ymargin/h);
  159. verts[i].x = PS2INCH(verts[i].x);
  160. verts[i].y = PS2INCH(verts[i].y);
  161. }
  162. }
  163. } else
  164. verts = genRound(n, &sides, xmargin, ymargin);
  165. break;
  166. case SH_RECORD: {
  167. sides = 4;
  168. verts = gv_calloc(sides, sizeof(Point));
  169. boxf b = ((field_t*)ND_shape_info(n))->b;
  170. verts[0] = makeScaledTransPoint(b.LL.x, b.LL.y, -xmargin, -ymargin);
  171. verts[1] = makeScaledTransPoint(b.UR.x, b.LL.y, xmargin, -ymargin);
  172. verts[2] = makeScaledTransPoint(b.UR.x, b.UR.y, xmargin, ymargin);
  173. verts[3] = makeScaledTransPoint(b.LL.x, b.UR.y, -xmargin, ymargin);
  174. pp->kind = BOX;
  175. break;
  176. }
  177. case SH_POINT:
  178. pp->kind = CIRCLE;
  179. verts = genRound(n, &sides, xmargin, ymargin);
  180. break;
  181. default:
  182. agerrorf("makeAddPoly: unknown shape type %s\n",
  183. ND_shape(n)->name);
  184. return 1;
  185. }
  186. pp->verts = verts;
  187. pp->nverts = (int)sides;
  188. bbox(verts, sides, &pp->origin, &pp->corner);
  189. if (sides > maxcnt)
  190. maxcnt = sides;
  191. return 0;
  192. }
  193. int makePoly(Poly *pp, Agnode_t *n, double xmargin, double ymargin) {
  194. size_t sides;
  195. Point *verts;
  196. polygon_t *poly;
  197. if (ND_clust(n)) {
  198. Point b;
  199. sides = 4;
  200. b.x = ND_width(n) / 2.0;
  201. b.y = ND_height(n) / 2.0;
  202. pp->kind = BOX;
  203. verts = gv_calloc(sides, sizeof(Point));
  204. PUTPT(verts[0], b.x, b.y);
  205. PUTPT(verts[1], -b.x, b.y);
  206. PUTPT(verts[2], -b.x, -b.y);
  207. PUTPT(verts[3], b.x, -b.y);
  208. } else
  209. switch (shapeOf(n)) {
  210. case SH_POLY:
  211. poly = ND_shape_info(n);
  212. sides = poly->sides;
  213. if (sides >= 3) { /* real polygon */
  214. verts = gv_calloc(sides, sizeof(Point));
  215. for (size_t i = 0; i < sides; i++) {
  216. verts[i].x = PS2INCH(poly->vertices[i].x);
  217. verts[i].y = PS2INCH(poly->vertices[i].y);
  218. }
  219. } else
  220. verts = genRound(n, &sides, 0, 0);
  221. if (streq(ND_shape(n)->name, "box"))
  222. pp->kind = BOX;
  223. else if (streq(ND_shape(n)->name, "polygon")
  224. && isBox(verts, sides))
  225. pp->kind = BOX;
  226. else if ((poly->sides < 3) && poly->regular)
  227. pp->kind = CIRCLE;
  228. else
  229. pp->kind = 0;
  230. break;
  231. case SH_RECORD: {
  232. sides = 4;
  233. verts = gv_calloc(sides, sizeof(Point));
  234. boxf b = ((field_t *) ND_shape_info(n))->b;
  235. verts[0] = makeScaledPoint(b.LL.x, b.LL.y);
  236. verts[1] = makeScaledPoint(b.UR.x, b.LL.y);
  237. verts[2] = makeScaledPoint(b.UR.x, b.UR.y);
  238. verts[3] = makeScaledPoint(b.LL.x, b.UR.y);
  239. pp->kind = BOX;
  240. break;
  241. }
  242. case SH_POINT:
  243. pp->kind = CIRCLE;
  244. verts = genRound(n, &sides, 0, 0);
  245. break;
  246. default:
  247. agerrorf("makePoly: unknown shape type %s\n",
  248. ND_shape(n)->name);
  249. return 1;
  250. }
  251. if ((xmargin != 1.0) || (ymargin != 1.0))
  252. inflatePts(verts, sides, xmargin, ymargin);
  253. pp->verts = verts;
  254. pp->nverts = (int)sides;
  255. bbox(verts, sides, &pp->origin, &pp->corner);
  256. if (sides > maxcnt)
  257. maxcnt = sides;
  258. return 0;
  259. }
  260. static int
  261. pintersect(Point originp, Point cornerp, Point originq, Point cornerq)
  262. {
  263. return ((originp.x <= cornerq.x) && (originq.x <= cornerp.x) &&
  264. (originp.y <= cornerq.y) && (originq.y <= cornerp.y));
  265. }
  266. #define Pin 1
  267. #define Qin 2
  268. #define Unknown 0
  269. #define advance(A,B,N) (B++, A = (A+1)%N)
  270. static int edgesIntersect(Point * P, Point * Q, int n, int m)
  271. {
  272. int a = 0;
  273. int b = 0;
  274. int aa = 0;
  275. int ba = 0;
  276. int a1, b1;
  277. Point A, B;
  278. double cross;
  279. int bHA, aHB;
  280. Point p;
  281. int inflag = Unknown;
  282. /* int i = 0; */
  283. /* int Reset = 0; */
  284. do {
  285. a1 = (a + n - 1) % n;
  286. b1 = (b + m - 1) % m;
  287. subpt(&A, P[a], P[a1]);
  288. subpt(&B, Q[b], Q[b1]);
  289. cross = area_2((Point){0}, A, B);
  290. bHA = leftOf(P[a1], P[a], Q[b]);
  291. aHB = leftOf(Q[b1], Q[b], P[a]);
  292. /* If A & B intersect, update inflag. */
  293. if (intersection(P[a1], P[a], Q[b1], Q[b], &p))
  294. return 1;
  295. /* Advance rules. */
  296. if ((cross == 0) && !bHA && !aHB) {
  297. if (inflag == Pin)
  298. advance(b, ba, m);
  299. else
  300. advance(a, aa, n);
  301. } else if (cross >= 0)
  302. if (bHA)
  303. advance(a, aa, n);
  304. else {
  305. advance(b, ba, m);
  306. } else { /* if ( cross < 0 ) */
  307. if (aHB)
  308. advance(b, ba, m);
  309. else
  310. advance(a, aa, n);
  311. }
  312. } while (((aa < n) || (ba < m)) && (aa < 2 * n) && (ba < 2 * m));
  313. return 0;
  314. }
  315. /* inPoly:
  316. * Return 1 if q is inside polygon vertex[]
  317. * Assume points are in CCW order
  318. */
  319. static int inPoly(Point vertex[], int n, Point q)
  320. {
  321. int i, i1; /* point index; i1 = i-1 mod n */
  322. double x; /* x intersection of e with ray */
  323. double crossings = 0; /* number of edge/ray crossings */
  324. if (tp3 == NULL)
  325. tp3 = gv_calloc(maxcnt, sizeof(Point));
  326. /* Shift so that q is the origin. */
  327. for (i = 0; i < n; i++) {
  328. tp3[i].x = vertex[i].x - q.x;
  329. tp3[i].y = vertex[i].y - q.y;
  330. }
  331. /* For each edge e=(i-1,i), see if crosses ray. */
  332. for (i = 0; i < n; i++) {
  333. i1 = (i + n - 1) % n;
  334. /* if edge is horizontal, test to see if the point is on it */
  335. if (tp3[i].y == 0 && tp3[i1].y == 0) {
  336. if (tp3[i].x * tp3[i1].x < 0) {
  337. return 1;
  338. } else {
  339. continue;
  340. }
  341. }
  342. /* if e straddles the x-axis... */
  343. if ((tp3[i].y >= 0 && tp3[i1].y <= 0) ||
  344. (tp3[i1].y >= 0 && tp3[i].y <= 0)) {
  345. /* e straddles ray, so compute intersection with ray. */
  346. x = (tp3[i].x * tp3[i1].y - tp3[i1].x * tp3[i].y)
  347. / (double) (tp3[i1].y - tp3[i].y);
  348. /* if intersect at origin, we've found intersection */
  349. if (x == 0)
  350. return 1;;
  351. /* crosses ray if strictly positive intersection. */
  352. if (x > 0) {
  353. if (tp3[i].y == 0 || tp3[i1].y == 0) {
  354. crossings += .5; /* goes through vertex */
  355. } else {
  356. crossings += 1.0;
  357. }
  358. }
  359. }
  360. }
  361. /* q inside if an odd number of crossings. */
  362. if ((int)crossings % 2 == 1)
  363. return 1;
  364. else
  365. return 0;
  366. }
  367. static bool inBox(Point p, Point origin_point, Point corner) {
  368. return p.x <= corner.x && p.x >= origin_point.x && p.y <= corner.y &&
  369. p.y >= origin_point.y;
  370. }
  371. static void transCopy(Point * inp, int cnt, Point off, Point * outp)
  372. {
  373. int i;
  374. for (i = 0; i < cnt; i++) {
  375. outp->x = inp->x + off.x;
  376. outp->y = inp->y + off.y;
  377. inp++;
  378. outp++;
  379. }
  380. }
  381. int polyOverlap(Point p, Poly * pp, Point q, Poly * qp)
  382. {
  383. Point op, cp;
  384. Point oq, cq;
  385. /* translate bounding boxes */
  386. addpt(&op, p, pp->origin);
  387. addpt(&cp, p, pp->corner);
  388. addpt(&oq, q, qp->origin);
  389. addpt(&cq, q, qp->corner);
  390. /* If bounding boxes don't overlap, done */
  391. if (!pintersect(op, cp, oq, cq))
  392. return 0;
  393. if (ISBOX(pp) && ISBOX(qp))
  394. return 1;
  395. if (ISCIRCLE(pp) && ISCIRCLE(qp)) {
  396. double d =
  397. (pp->corner.x - pp->origin.x + qp->corner.x - qp->origin.x);
  398. double dx = p.x - q.x;
  399. double dy = p.y - q.y;
  400. if ((dx * dx + dy * dy) > (d * d) / 4.0)
  401. return 0;
  402. else
  403. return 1;
  404. }
  405. if (tp1 == NULL) {
  406. tp1 = gv_calloc(maxcnt, sizeof(Point));
  407. tp2 = gv_calloc(maxcnt, sizeof(Point));
  408. }
  409. transCopy(pp->verts, pp->nverts, p, tp1);
  410. transCopy(qp->verts, qp->nverts, q, tp2);
  411. return (edgesIntersect(tp1, tp2, pp->nverts, qp->nverts) ||
  412. (inBox(*tp1, oq, cq) && inPoly(tp2, qp->nverts, *tp1)) ||
  413. (inBox(*tp2, op, cp) && inPoly(tp1, pp->nverts, *tp2)));
  414. }