trapezoid.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959
  1. /**
  2. * @file
  3. * @brief trapezoidation
  4. *
  5. * See [Fast polygon triangulation based on Seidel's algorithm](http://gamma.cs.unc.edu/SEIDEL/)
  6. *
  7. */
  8. /*************************************************************************
  9. * Copyright (c) 2011 AT&T Intellectual Property
  10. * All rights reserved. This program and the accompanying materials
  11. * are made available under the terms of the Eclipse Public License v1.0
  12. * which accompanies this distribution, and is available at
  13. * https://www.eclipse.org/legal/epl-v10.html
  14. *
  15. * Contributors: Details at https://graphviz.org
  16. *************************************************************************/
  17. #include "config.h"
  18. #include <string.h>
  19. #include <assert.h>
  20. #include <stdbool.h>
  21. #include <stdio.h>
  22. #include <math.h>
  23. #include <common/geom.h>
  24. #include <common/types.h>
  25. #include <ortho/trap.h>
  26. #include <util/alloc.h>
  27. /* Node types */
  28. #define T_X 1
  29. #define T_Y 2
  30. #define T_SINK 3
  31. #define FIRSTPT 1 /* checking whether pt. is inserted */
  32. #define LASTPT 2
  33. #define S_LEFT 1 /* for merge-direction */
  34. #define S_RIGHT 2
  35. #define INF 1<<30
  36. #define CROSS(v0, v1, v2) (((v1).x - (v0).x)*((v2).y - (v0).y) - \
  37. ((v1).y - (v0).y)*((v2).x - (v0).x))
  38. typedef struct {
  39. int nodetype; /* Y-node or S-node */
  40. int segnum;
  41. pointf yval;
  42. int trnum;
  43. int parent; /* doubly linked DAG */
  44. int left, right; /* children */
  45. } qnode_t;
  46. /// an array of qnodes
  47. typedef struct {
  48. size_t length;
  49. qnode_t *data;
  50. } qnodes_t;
  51. /* Return a new node to be added into the query tree */
  52. static int newnode(qnodes_t *qs) {
  53. qs->data = gv_recalloc(qs->data, qs->length, qs->length + 1, sizeof(qnode_t));
  54. ++qs->length;
  55. return qs->length - 1;
  56. }
  57. /* Return a free trapezoid */
  58. static int newtrap(traps_t *tr) {
  59. tr->data = gv_recalloc(tr->data, tr->length, tr->length + 1, sizeof(trap_t));
  60. ++tr->length;
  61. return tr->length - 1;
  62. }
  63. /* Return the maximum of the two points into the yval structure */
  64. static void _max (pointf *yval, pointf *v0, pointf *v1)
  65. {
  66. if (v0->y > v1->y + C_EPS)
  67. *yval = *v0;
  68. else if (FP_EQUAL(v0->y, v1->y))
  69. {
  70. if (v0->x > v1->x + C_EPS)
  71. *yval = *v0;
  72. else
  73. *yval = *v1;
  74. }
  75. else
  76. *yval = *v1;
  77. }
  78. /* Return the minimum of the two points into the yval structure */
  79. static void _min (pointf *yval, pointf *v0, pointf *v1)
  80. {
  81. if (v0->y < v1->y - C_EPS)
  82. *yval = *v0;
  83. else if (FP_EQUAL(v0->y, v1->y))
  84. {
  85. if (v0->x < v1->x)
  86. *yval = *v0;
  87. else
  88. *yval = *v1;
  89. }
  90. else
  91. *yval = *v1;
  92. }
  93. static bool _greater_than_equal_to (pointf *v0, pointf *v1)
  94. {
  95. if (v0->y > v1->y + C_EPS)
  96. return true;
  97. else if (v0->y < v1->y - C_EPS)
  98. return false;
  99. else
  100. return v0->x >= v1->x;
  101. }
  102. static bool _less_than (pointf *v0, pointf *v1)
  103. {
  104. return !_greater_than_equal_to(v0, v1);
  105. }
  106. /* Initialize the query structure (Q) and the trapezoid table (T)
  107. * when the first segment is added to start the trapezoidation. The
  108. * query-tree starts out with 4 trapezoids, one S-node and 2 Y-nodes
  109. *
  110. * 4
  111. * -----------------------------------
  112. * \
  113. * 1 \ 2
  114. * \
  115. * -----------------------------------
  116. * 3
  117. */
  118. static int
  119. init_query_structure(int segnum, segment_t *seg, traps_t *tr, qnodes_t *qs) {
  120. int i1, root;
  121. int t1, t2, t3, t4;
  122. segment_t *s = &seg[segnum];
  123. i1 = newnode(qs);
  124. qs->data[i1].nodetype = T_Y;
  125. _max(&qs->data[i1].yval, &s->v0, &s->v1); /* root */
  126. root = i1;
  127. int i2 = newnode(qs);
  128. qs->data[i1].right = i2;
  129. qs->data[i2].nodetype = T_SINK;
  130. qs->data[i2].parent = i1;
  131. int i3 = newnode(qs);
  132. qs->data[i1].left = i3;
  133. qs->data[i3].nodetype = T_Y;
  134. _min(&qs->data[i3].yval, &s->v0, &s->v1); /* root */
  135. qs->data[i3].parent = i1;
  136. int i4 = newnode(qs);
  137. qs->data[i3].left = i4;
  138. qs->data[i4].nodetype = T_SINK;
  139. qs->data[i4].parent = i3;
  140. int i5 = newnode(qs);
  141. qs->data[i3].right = i5;
  142. qs->data[i5].nodetype = T_X;
  143. qs->data[i5].segnum = segnum;
  144. qs->data[i5].parent = i3;
  145. int i6 = newnode(qs);
  146. qs->data[i5].left = i6;
  147. qs->data[i6].nodetype = T_SINK;
  148. qs->data[i6].parent = i5;
  149. int i7 = newnode(qs);
  150. qs->data[i5].right = i7;
  151. qs->data[i7].nodetype = T_SINK;
  152. qs->data[i7].parent = i5;
  153. t1 = newtrap(tr); /* middle left */
  154. t2 = newtrap(tr); /* middle right */
  155. t3 = newtrap(tr); /* bottom-most */
  156. t4 = newtrap(tr); /* topmost */
  157. tr->data[t1].hi = qs->data[i1].yval;
  158. tr->data[t2].hi = qs->data[i1].yval;
  159. tr->data[t4].lo = qs->data[i1].yval;
  160. tr->data[t1].lo = qs->data[i3].yval;
  161. tr->data[t2].lo = qs->data[i3].yval;
  162. tr->data[t3].hi = qs->data[i3].yval;
  163. tr->data[t4].hi.y = (double)(INF);
  164. tr->data[t4].hi.x = (double)(INF);
  165. tr->data[t3].lo.y = (double)-1 * (INF);
  166. tr->data[t3].lo.x = (double)-1 * (INF);
  167. tr->data[t1].rseg = segnum;
  168. tr->data[t2].lseg = segnum;
  169. tr->data[t1].u0 = t4;
  170. tr->data[t2].u0 = t4;
  171. tr->data[t1].d0 = t3;
  172. tr->data[t2].d0 = t3;
  173. tr->data[t4].d0 = t1;
  174. tr->data[t3].u0 = t1;
  175. tr->data[t4].d1 = t2;
  176. tr->data[t3].u1 = t2;
  177. tr->data[t1].sink = i6;
  178. tr->data[t2].sink = i7;
  179. tr->data[t3].sink = i4;
  180. tr->data[t4].sink = i2;
  181. tr->data[t1].state = ST_VALID;
  182. tr->data[t2].state = ST_VALID;
  183. tr->data[t3].state = ST_VALID;
  184. tr->data[t4].state = ST_VALID;
  185. qs->data[i2].trnum = t4;
  186. qs->data[i4].trnum = t3;
  187. qs->data[i6].trnum = t1;
  188. qs->data[i7].trnum = t2;
  189. s->is_inserted = true;
  190. return root;
  191. }
  192. /* Return true if the vertex v is to the left of line segment no.
  193. * segnum. Takes care of the degenerate cases when both the vertices
  194. * have the same y--cood, etc.
  195. */
  196. static bool
  197. is_left_of (int segnum, segment_t* seg, pointf *v)
  198. {
  199. segment_t *s = &seg[segnum];
  200. double area;
  201. if (_greater_than(&s->v1, &s->v0)) /* seg. going upwards */
  202. {
  203. if (FP_EQUAL(s->v1.y, v->y))
  204. {
  205. if (v->x < s->v1.x)
  206. area = 1.0;
  207. else
  208. area = -1.0;
  209. }
  210. else if (FP_EQUAL(s->v0.y, v->y))
  211. {
  212. if (v->x < s->v0.x)
  213. area = 1.0;
  214. else
  215. area = -1.0;
  216. }
  217. else
  218. area = CROSS(s->v0, s->v1, *v);
  219. }
  220. else /* v0 > v1 */
  221. {
  222. if (FP_EQUAL(s->v1.y, v->y))
  223. {
  224. if (v->x < s->v1.x)
  225. area = 1.0;
  226. else
  227. area = -1.0;
  228. }
  229. else if (FP_EQUAL(s->v0.y, v->y))
  230. {
  231. if (v->x < s->v0.x)
  232. area = 1.0;
  233. else
  234. area = -1.0;
  235. }
  236. else
  237. area = CROSS(s->v1, s->v0, (*v));
  238. }
  239. return area > 0.0;
  240. }
  241. /* Returns true if the corresponding endpoint of the given segment is */
  242. /* already inserted into the segment tree. Use the simple test of */
  243. /* whether the segment which shares this endpoint is already inserted */
  244. static bool inserted (int segnum, segment_t* seg, int whichpt)
  245. {
  246. if (whichpt == FIRSTPT)
  247. return seg[seg[segnum].prev].is_inserted;
  248. else
  249. return seg[seg[segnum].next].is_inserted;
  250. }
  251. /* This is query routine which determines which trapezoid does the
  252. * point v lie in. The return value is the trapezoid number.
  253. */
  254. static int
  255. locate_endpoint (pointf *v, pointf *vo, int r, segment_t* seg, qnodes_t* qs)
  256. {
  257. qnode_t *rptr = &qs->data[r];
  258. switch (rptr->nodetype) {
  259. case T_SINK:
  260. return rptr->trnum;
  261. case T_Y:
  262. if (_greater_than(v, &rptr->yval)) /* above */
  263. return locate_endpoint(v, vo, rptr->right, seg, qs);
  264. else if (_equal_to(v, &rptr->yval)) /* the point is already */
  265. { /* inserted. */
  266. if (_greater_than(vo, &rptr->yval)) /* above */
  267. return locate_endpoint(v, vo, rptr->right, seg, qs);
  268. else
  269. return locate_endpoint(v, vo, rptr->left, seg, qs); /* below */
  270. }
  271. else
  272. return locate_endpoint(v, vo, rptr->left, seg, qs); /* below */
  273. case T_X:
  274. if (_equal_to(v, &seg[rptr->segnum].v0) ||
  275. _equal_to(v, &seg[rptr->segnum].v1))
  276. {
  277. if (FP_EQUAL(v->y, vo->y)) /* horizontal segment */
  278. {
  279. if (vo->x < v->x)
  280. return locate_endpoint(v, vo, rptr->left, seg, qs); /* left */
  281. else
  282. return locate_endpoint(v, vo, rptr->right, seg, qs); /* right */
  283. }
  284. else if (is_left_of(rptr->segnum, seg, vo))
  285. return locate_endpoint(v, vo, rptr->left, seg, qs); /* left */
  286. else
  287. return locate_endpoint(v, vo, rptr->right, seg, qs); /* right */
  288. }
  289. else if (is_left_of(rptr->segnum, seg, v))
  290. return locate_endpoint(v, vo, rptr->left, seg, qs); /* left */
  291. else
  292. return locate_endpoint(v, vo, rptr->right, seg, qs); /* right */
  293. default:
  294. fprintf(stderr, "unexpected case in locate_endpoint\n");
  295. assert (0);
  296. break;
  297. }
  298. return 1; /* stop warning */
  299. }
  300. /* Thread in the segment into the existing trapezoidation. The
  301. * limiting trapezoids are given by tfirst and tlast (which are the
  302. * trapezoids containing the two endpoints of the segment. Merges all
  303. * possible trapezoids which flank this segment and have been recently
  304. * divided because of its insertion
  305. */
  306. static void
  307. merge_trapezoids(int segnum, int tfirst, int tlast, int side, traps_t *tr,
  308. qnodes_t* qs)
  309. {
  310. int t;
  311. /* First merge polys on the LHS */
  312. t = tfirst;
  313. while (t > 0 && _greater_than_equal_to(&tr->data[t].lo, &tr->data[tlast].lo))
  314. {
  315. int tnext, ptnext;
  316. bool cond;
  317. if (side == S_LEFT)
  318. cond = ((tnext = tr->data[t].d0) > 0 && tr->data[tnext].rseg == segnum) ||
  319. ((tnext = tr->data[t].d1) > 0 && tr->data[tnext].rseg == segnum);
  320. else
  321. cond = ((tnext = tr->data[t].d0) > 0 && tr->data[tnext].lseg == segnum) ||
  322. ((tnext = tr->data[t].d1) > 0 && tr->data[tnext].lseg == segnum);
  323. if (cond)
  324. {
  325. if (tr->data[t].lseg == tr->data[tnext].lseg &&
  326. tr->data[t].rseg == tr->data[tnext].rseg) /* good neighbours */
  327. { /* merge them */
  328. /* Use the upper node as the new node i.e. t */
  329. ptnext = qs->data[tr->data[tnext].sink].parent;
  330. if (qs->data[ptnext].left == tr->data[tnext].sink)
  331. qs->data[ptnext].left = tr->data[t].sink;
  332. else
  333. qs->data[ptnext].right = tr->data[t].sink; /* redirect parent */
  334. /* Change the upper neighbours of the lower trapezoids */
  335. if ((tr->data[t].d0 = tr->data[tnext].d0) > 0) {
  336. if (tr->data[tr->data[t].d0].u0 == tnext)
  337. tr->data[tr->data[t].d0].u0 = t;
  338. else if (tr->data[tr->data[t].d0].u1 == tnext)
  339. tr->data[tr->data[t].d0].u1 = t;
  340. }
  341. if ((tr->data[t].d1 = tr->data[tnext].d1) > 0) {
  342. if (tr->data[tr->data[t].d1].u0 == tnext)
  343. tr->data[tr->data[t].d1].u0 = t;
  344. else if (tr->data[tr->data[t].d1].u1 == tnext)
  345. tr->data[tr->data[t].d1].u1 = t;
  346. }
  347. tr->data[t].lo = tr->data[tnext].lo;
  348. tr->data[tnext].state = ST_INVALID; /* invalidate the lower */
  349. /* trapezium */
  350. }
  351. else /* not good neighbours */
  352. t = tnext;
  353. }
  354. else /* do not satisfy the outer if */
  355. t = tnext;
  356. } /* end-while */
  357. }
  358. static void update_trapezoid(segment_t *s, segment_t *seg, traps_t *tr, int t, int tn)
  359. {
  360. if (tr->data[t].u0 > 0 && tr->data[t].u1 > 0)
  361. { /* continuation of a chain from abv. */
  362. if (tr->data[t].usave > 0) /* three upper neighbours */
  363. {
  364. if (tr->data[t].uside == S_LEFT)
  365. {
  366. tr->data[tn].u0 = tr->data[t].u1;
  367. tr->data[t].u1 = -1;
  368. tr->data[tn].u1 = tr->data[t].usave;
  369. tr->data[tr->data[t].u0].d0 = t;
  370. tr->data[tr->data[tn].u0].d0 = tn;
  371. tr->data[tr->data[tn].u1].d0 = tn;
  372. }
  373. else /* intersects in the right */
  374. {
  375. tr->data[tn].u1 = -1;
  376. tr->data[tn].u0 = tr->data[t].u1;
  377. tr->data[t].u1 = tr->data[t].u0;
  378. tr->data[t].u0 = tr->data[t].usave;
  379. tr->data[tr->data[t].u0].d0 = t;
  380. tr->data[tr->data[t].u1].d0 = t;
  381. tr->data[tr->data[tn].u0].d0 = tn;
  382. }
  383. tr->data[t].usave = 0;
  384. tr->data[tn].usave = 0;
  385. }
  386. else /* No usave.... simple case */
  387. {
  388. tr->data[tn].u0 = tr->data[t].u1;
  389. tr->data[t].u1 = -1;
  390. tr->data[tn].u1 = -1;
  391. tr->data[tr->data[tn].u0].d0 = tn;
  392. }
  393. }
  394. else
  395. { /* fresh seg. or upward cusp */
  396. int tmp_u = tr->data[t].u0;
  397. int td0, td1;
  398. if ((td0 = tr->data[tmp_u].d0) > 0 && (td1 = tr->data[tmp_u].d1) > 0)
  399. { /* upward cusp */
  400. if (tr->data[td0].rseg > 0 && !is_left_of(tr->data[td0].rseg, seg, &s->v1))
  401. {
  402. tr->data[t].u0 = -1;
  403. tr->data[t].u1 = -1;
  404. tr->data[tn].u1 = -1;
  405. tr->data[tr->data[tn].u0].d1 = tn;
  406. }
  407. else /* cusp going leftwards */
  408. {
  409. tr->data[tn].u0 = -1;
  410. tr->data[tn].u1 = -1;
  411. tr->data[t].u1 = -1;
  412. tr->data[tr->data[t].u0].d0 = t;
  413. }
  414. }
  415. else /* fresh segment */
  416. {
  417. tr->data[tr->data[t].u0].d0 = t;
  418. tr->data[tr->data[t].u0].d1 = tn;
  419. }
  420. }
  421. }
  422. /* Add in the new segment into the trapezoidation and update Q and T
  423. * structures. First locate the two endpoints of the segment in the
  424. * Q-structure. Then start from the topmost trapezoid and go down to
  425. * the lower trapezoid dividing all the trapezoids in between .
  426. */
  427. static void add_segment(int segnum, segment_t *seg, traps_t *tr, qnodes_t *qs) {
  428. segment_t s;
  429. int tu, tl, sk, tfirst, tlast;
  430. int tfirstr = 0, tlastr = 0, tfirstl = 0, tlastl = 0;
  431. int i1, i2, t, tn;
  432. pointf tpt;
  433. int tribot = 0;
  434. bool is_swapped;
  435. int tmptriseg;
  436. s = seg[segnum];
  437. if (_greater_than(&s.v1, &s.v0)) /* Get higher vertex in v0 */
  438. {
  439. int tmp;
  440. tpt = s.v0;
  441. s.v0 = s.v1;
  442. s.v1 = tpt;
  443. tmp = s.root0;
  444. s.root0 = s.root1;
  445. s.root1 = tmp;
  446. is_swapped = true;
  447. }
  448. else is_swapped = false;
  449. if (!inserted(segnum, seg, is_swapped ? LASTPT : FIRSTPT))
  450. /* insert v0 in the tree */
  451. {
  452. int tmp_d;
  453. tu = locate_endpoint(&s.v0, &s.v1, s.root0, seg, qs);
  454. tl = newtrap(tr); /* tl is the new lower trapezoid */
  455. tr->data[tl].state = ST_VALID;
  456. tr->data[tl] = tr->data[tu];
  457. tr->data[tu].lo.y = s.v0.y;
  458. tr->data[tl].hi.y = s.v0.y;
  459. tr->data[tu].lo.x = s.v0.x;
  460. tr->data[tl].hi.x = s.v0.x;
  461. tr->data[tu].d0 = tl;
  462. tr->data[tu].d1 = 0;
  463. tr->data[tl].u0 = tu;
  464. tr->data[tl].u1 = 0;
  465. if ((tmp_d = tr->data[tl].d0) > 0 && tr->data[tmp_d].u0 == tu)
  466. tr->data[tmp_d].u0 = tl;
  467. if ((tmp_d = tr->data[tl].d0) > 0 && tr->data[tmp_d].u1 == tu)
  468. tr->data[tmp_d].u1 = tl;
  469. if ((tmp_d = tr->data[tl].d1) > 0 && tr->data[tmp_d].u0 == tu)
  470. tr->data[tmp_d].u0 = tl;
  471. if ((tmp_d = tr->data[tl].d1) > 0 && tr->data[tmp_d].u1 == tu)
  472. tr->data[tmp_d].u1 = tl;
  473. /* Now update the query structure and obtain the sinks for the */
  474. /* two trapezoids */
  475. i1 = newnode(qs); /* Upper trapezoid sink */
  476. i2 = newnode(qs); /* Lower trapezoid sink */
  477. sk = tr->data[tu].sink;
  478. qs->data[sk].nodetype = T_Y;
  479. qs->data[sk].yval = s.v0;
  480. qs->data[sk].segnum = segnum; /* not really reqd ... maybe later */
  481. qs->data[sk].left = i2;
  482. qs->data[sk].right = i1;
  483. qs->data[i1].nodetype = T_SINK;
  484. qs->data[i1].trnum = tu;
  485. qs->data[i1].parent = sk;
  486. qs->data[i2].nodetype = T_SINK;
  487. qs->data[i2].trnum = tl;
  488. qs->data[i2].parent = sk;
  489. tr->data[tu].sink = i1;
  490. tr->data[tl].sink = i2;
  491. tfirst = tl;
  492. }
  493. else /* v0 already present */
  494. { /* Get the topmost intersecting trapezoid */
  495. tfirst = locate_endpoint(&s.v0, &s.v1, s.root0, seg, qs);
  496. }
  497. if (!inserted(segnum, seg, is_swapped ? FIRSTPT : LASTPT))
  498. /* insert v1 in the tree */
  499. {
  500. int tmp_d;
  501. tu = locate_endpoint(&s.v1, &s.v0, s.root1, seg, qs);
  502. tl = newtrap(tr); /* tl is the new lower trapezoid */
  503. tr->data[tl].state = ST_VALID;
  504. tr->data[tl] = tr->data[tu];
  505. tr->data[tu].lo.y = tr->data[tl].hi.y = s.v1.y;
  506. tr->data[tu].lo.x = tr->data[tl].hi.x = s.v1.x;
  507. tr->data[tu].d0 = tl;
  508. tr->data[tu].d1 = 0;
  509. tr->data[tl].u0 = tu;
  510. tr->data[tl].u1 = 0;
  511. if ((tmp_d = tr->data[tl].d0) > 0 && tr->data[tmp_d].u0 == tu)
  512. tr->data[tmp_d].u0 = tl;
  513. if ((tmp_d = tr->data[tl].d0) > 0 && tr->data[tmp_d].u1 == tu)
  514. tr->data[tmp_d].u1 = tl;
  515. if ((tmp_d = tr->data[tl].d1) > 0 && tr->data[tmp_d].u0 == tu)
  516. tr->data[tmp_d].u0 = tl;
  517. if ((tmp_d = tr->data[tl].d1) > 0 && tr->data[tmp_d].u1 == tu)
  518. tr->data[tmp_d].u1 = tl;
  519. /* Now update the query structure and obtain the sinks for the */
  520. /* two trapezoids */
  521. i1 = newnode(qs); /* Upper trapezoid sink */
  522. i2 = newnode(qs); /* Lower trapezoid sink */
  523. sk = tr->data[tu].sink;
  524. qs->data[sk].nodetype = T_Y;
  525. qs->data[sk].yval = s.v1;
  526. qs->data[sk].segnum = segnum; /* not really reqd ... maybe later */
  527. qs->data[sk].left = i2;
  528. qs->data[sk].right = i1;
  529. qs->data[i1].nodetype = T_SINK;
  530. qs->data[i1].trnum = tu;
  531. qs->data[i1].parent = sk;
  532. qs->data[i2].nodetype = T_SINK;
  533. qs->data[i2].trnum = tl;
  534. qs->data[i2].parent = sk;
  535. tr->data[tu].sink = i1;
  536. tr->data[tl].sink = i2;
  537. tlast = tu;
  538. }
  539. else /* v1 already present */
  540. { /* Get the lowermost intersecting trapezoid */
  541. tlast = locate_endpoint(&s.v1, &s.v0, s.root1, seg, qs);
  542. tribot = 1;
  543. }
  544. /* Thread the segment into the query tree creating a new X-node */
  545. /* First, split all the trapezoids which are intersected by s into */
  546. /* two */
  547. t = tfirst; /* topmost trapezoid */
  548. while (t > 0 && _greater_than_equal_to(&tr->data[t].lo, &tr->data[tlast].lo))
  549. /* traverse from top to bot */
  550. {
  551. int t_sav, tn_sav;
  552. sk = tr->data[t].sink;
  553. i1 = newnode(qs); /* left trapezoid sink */
  554. i2 = newnode(qs); /* right trapezoid sink */
  555. qs->data[sk].nodetype = T_X;
  556. qs->data[sk].segnum = segnum;
  557. qs->data[sk].left = i1;
  558. qs->data[sk].right = i2;
  559. qs->data[i1].nodetype = T_SINK; /* left trapezoid (use existing one) */
  560. qs->data[i1].trnum = t;
  561. qs->data[i1].parent = sk;
  562. qs->data[i2].nodetype = T_SINK; /* right trapezoid (allocate new) */
  563. qs->data[i2].trnum = tn = newtrap(tr);
  564. tr->data[tn].state = ST_VALID;
  565. qs->data[i2].parent = sk;
  566. if (t == tfirst)
  567. tfirstr = tn;
  568. if (_equal_to(&tr->data[t].lo, &tr->data[tlast].lo))
  569. tlastr = tn;
  570. tr->data[tn] = tr->data[t];
  571. tr->data[t].sink = i1;
  572. tr->data[tn].sink = i2;
  573. t_sav = t;
  574. tn_sav = tn;
  575. /* error */
  576. if (tr->data[t].d0 <= 0 && tr->data[t].d1 <= 0) /* case cannot arise */
  577. {
  578. fprintf(stderr, "add_segment: error\n");
  579. break;
  580. }
  581. /* only one trapezoid below. partition t into two and make the */
  582. /* two resulting trapezoids t and tn as the upper neighbours of */
  583. /* the sole lower trapezoid */
  584. else if (tr->data[t].d0 > 0 && tr->data[t].d1 <= 0)
  585. { /* Only one trapezoid below */
  586. update_trapezoid(&s, seg, tr, t, tn);
  587. if (FP_EQUAL(tr->data[t].lo.y, tr->data[tlast].lo.y) &&
  588. FP_EQUAL(tr->data[t].lo.x, tr->data[tlast].lo.x) && tribot)
  589. { /* bottom forms a triangle */
  590. if (is_swapped)
  591. tmptriseg = seg[segnum].prev;
  592. else
  593. tmptriseg = seg[segnum].next;
  594. if (tmptriseg > 0 && is_left_of(tmptriseg, seg, &s.v0))
  595. {
  596. /* L-R downward cusp */
  597. tr->data[tr->data[t].d0].u0 = t;
  598. tr->data[tn].d0 = -1;
  599. tr->data[tn].d1 = -1;
  600. }
  601. else
  602. {
  603. /* R-L downward cusp */
  604. tr->data[tr->data[tn].d0].u1 = tn;
  605. tr->data[t].d0 = -1;
  606. tr->data[t].d1 = -1;
  607. }
  608. }
  609. else
  610. {
  611. if (tr->data[tr->data[t].d0].u0 > 0 && tr->data[tr->data[t].d0].u1 > 0)
  612. {
  613. if (tr->data[tr->data[t].d0].u0 == t) /* passes through LHS */
  614. {
  615. tr->data[tr->data[t].d0].usave = tr->data[tr->data[t].d0].u1;
  616. tr->data[tr->data[t].d0].uside = S_LEFT;
  617. }
  618. else
  619. {
  620. tr->data[tr->data[t].d0].usave = tr->data[tr->data[t].d0].u0;
  621. tr->data[tr->data[t].d0].uside = S_RIGHT;
  622. }
  623. }
  624. tr->data[tr->data[t].d0].u0 = t;
  625. tr->data[tr->data[t].d0].u1 = tn;
  626. }
  627. t = tr->data[t].d0;
  628. }
  629. else if (tr->data[t].d0 <= 0 && tr->data[t].d1 > 0)
  630. { /* Only one trapezoid below */
  631. update_trapezoid(&s, seg, tr, t, tn);
  632. if (FP_EQUAL(tr->data[t].lo.y, tr->data[tlast].lo.y) &&
  633. FP_EQUAL(tr->data[t].lo.x, tr->data[tlast].lo.x) && tribot)
  634. { /* bottom forms a triangle */
  635. if (is_swapped)
  636. tmptriseg = seg[segnum].prev;
  637. else
  638. tmptriseg = seg[segnum].next;
  639. if (tmptriseg > 0 && is_left_of(tmptriseg, seg, &s.v0))
  640. {
  641. /* L-R downward cusp */
  642. tr->data[tr->data[t].d1].u0 = t;
  643. tr->data[tn].d0 = -1;
  644. tr->data[tn].d1 = -1;
  645. }
  646. else
  647. {
  648. /* R-L downward cusp */
  649. tr->data[tr->data[tn].d1].u1 = tn;
  650. tr->data[t].d0 = -1;
  651. tr->data[t].d1 = -1;
  652. }
  653. }
  654. else
  655. {
  656. if (tr->data[tr->data[t].d1].u0 > 0 && tr->data[tr->data[t].d1].u1 > 0)
  657. {
  658. if (tr->data[tr->data[t].d1].u0 == t) /* passes through LHS */
  659. {
  660. tr->data[tr->data[t].d1].usave = tr->data[tr->data[t].d1].u1;
  661. tr->data[tr->data[t].d1].uside = S_LEFT;
  662. }
  663. else
  664. {
  665. tr->data[tr->data[t].d1].usave = tr->data[tr->data[t].d1].u0;
  666. tr->data[tr->data[t].d1].uside = S_RIGHT;
  667. }
  668. }
  669. tr->data[tr->data[t].d1].u0 = t;
  670. tr->data[tr->data[t].d1].u1 = tn;
  671. }
  672. t = tr->data[t].d1;
  673. }
  674. /* two trapezoids below. Find out which one is intersected by */
  675. /* this segment and proceed down that one */
  676. else
  677. {
  678. double y0, yt;
  679. pointf tmppt;
  680. int tnext;
  681. bool i_d0, i_d1;
  682. i_d0 = i_d1 = false;
  683. if (FP_EQUAL(tr->data[t].lo.y, s.v0.y))
  684. {
  685. if (tr->data[t].lo.x > s.v0.x)
  686. i_d0 = true;
  687. else
  688. i_d1 = true;
  689. }
  690. else
  691. {
  692. tmppt.y = y0 = tr->data[t].lo.y;
  693. yt = (y0 - s.v0.y)/(s.v1.y - s.v0.y);
  694. tmppt.x = s.v0.x + yt * (s.v1.x - s.v0.x);
  695. if (_less_than(&tmppt, &tr->data[t].lo))
  696. i_d0 = true;
  697. else
  698. i_d1 = true;
  699. }
  700. /* check continuity from the top so that the lower-neighbour */
  701. /* values are properly filled for the upper trapezoid */
  702. update_trapezoid(&s, seg, tr, t, tn);
  703. if (FP_EQUAL(tr->data[t].lo.y, tr->data[tlast].lo.y) &&
  704. FP_EQUAL(tr->data[t].lo.x, tr->data[tlast].lo.x) && tribot)
  705. {
  706. /* this case arises only at the lowest trapezoid.. i.e.
  707. tlast, if the lower endpoint of the segment is
  708. already inserted in the structure */
  709. tr->data[tr->data[t].d0].u0 = t;
  710. tr->data[tr->data[t].d0].u1 = -1;
  711. tr->data[tr->data[t].d1].u0 = tn;
  712. tr->data[tr->data[t].d1].u1 = -1;
  713. tr->data[tn].d0 = tr->data[t].d1;
  714. tr->data[t].d1 = -1;
  715. tr->data[tn].d1 = -1;
  716. tnext = tr->data[t].d1;
  717. }
  718. else if (i_d0)
  719. /* intersecting d0 */
  720. {
  721. tr->data[tr->data[t].d0].u0 = t;
  722. tr->data[tr->data[t].d0].u1 = tn;
  723. tr->data[tr->data[t].d1].u0 = tn;
  724. tr->data[tr->data[t].d1].u1 = -1;
  725. /* new code to determine the bottom neighbours of the */
  726. /* newly partitioned trapezoid */
  727. tr->data[t].d1 = -1;
  728. tnext = tr->data[t].d0;
  729. }
  730. else /* intersecting d1 */
  731. {
  732. tr->data[tr->data[t].d0].u0 = t;
  733. tr->data[tr->data[t].d0].u1 = -1;
  734. tr->data[tr->data[t].d1].u0 = t;
  735. tr->data[tr->data[t].d1].u1 = tn;
  736. /* new code to determine the bottom neighbours of the */
  737. /* newly partitioned trapezoid */
  738. tr->data[tn].d0 = tr->data[t].d1;
  739. tr->data[tn].d1 = -1;
  740. tnext = tr->data[t].d1;
  741. }
  742. t = tnext;
  743. }
  744. tr->data[t_sav].rseg = segnum;
  745. tr->data[tn_sav].lseg = segnum;
  746. } /* end-while */
  747. /* Now combine those trapezoids which share common segments. We can */
  748. /* use the pointers to the parent to connect these together. This */
  749. /* works only because all these new trapezoids have been formed */
  750. /* due to splitting by the segment, and hence have only one parent */
  751. tfirstl = tfirst;
  752. tlastl = tlast;
  753. merge_trapezoids(segnum, tfirstl, tlastl, S_LEFT, tr, qs);
  754. merge_trapezoids(segnum, tfirstr, tlastr, S_RIGHT, tr, qs);
  755. seg[segnum].is_inserted = true;
  756. }
  757. /* Update the roots stored for each of the endpoints of the segment.
  758. * This is done to speed up the location-query for the endpoint when
  759. * the segment is inserted into the trapezoidation subsequently
  760. */
  761. static void
  762. find_new_roots(int segnum, segment_t *seg, traps_t *tr, qnodes_t *qs) {
  763. segment_t *s = &seg[segnum];
  764. if (s->is_inserted) return;
  765. s->root0 = locate_endpoint(&s->v0, &s->v1, s->root0, seg, qs);
  766. s->root0 = tr->data[s->root0].sink;
  767. s->root1 = locate_endpoint(&s->v1, &s->v0, s->root1, seg, qs);
  768. s->root1 = tr->data[s->root1].sink;
  769. }
  770. /* Get log*n for given n */
  771. static int math_logstar_n(int n)
  772. {
  773. int i;
  774. double v;
  775. for (i = 0, v = (double) n; v >= 1; i++)
  776. v = log2(v);
  777. return i - 1;
  778. }
  779. static int math_N(int n, int h)
  780. {
  781. int i;
  782. double v;
  783. for (i = 0, v = (double) n; i < h; i++)
  784. v = log2(v);
  785. return (int) ceil((double) 1.0*n/v);
  786. }
  787. /* Main routine to perform trapezoidation */
  788. traps_t construct_trapezoids(int nseg, segment_t *seg, int *permute) {
  789. int i;
  790. int root, h;
  791. int segi = 1;
  792. // We will append later nodes by expanding this on-demand. First node is a
  793. // sentinel.
  794. qnodes_t qs = {.length = 1, .data = gv_calloc(1, sizeof(qnode_t))};
  795. // First trapezoid is reserved as a sentinel. We will append later
  796. // trapezoids by expanding this on-demand.
  797. traps_t tr = {.length = 1, .data = gv_calloc(1, sizeof(trap_t))};
  798. /* Add the first segment and get the query structure and trapezoid */
  799. /* list initialised */
  800. root = init_query_structure(permute[segi++], seg, &tr, &qs);
  801. for (i = 1; i <= nseg; i++)
  802. seg[i].root0 = seg[i].root1 = root;
  803. for (h = 1; h <= math_logstar_n(nseg); h++) {
  804. for (i = math_N(nseg, h -1) + 1; i <= math_N(nseg, h); i++)
  805. add_segment(permute[segi++], seg, &tr, &qs);
  806. /* Find a new root for each of the segment endpoints */
  807. for (i = 1; i <= nseg; i++)
  808. find_new_roots(i, seg, &tr, &qs);
  809. }
  810. for (i = math_N(nseg, math_logstar_n(nseg)) + 1; i <= nseg; i++)
  811. add_segment(permute[segi++], seg, &tr, &qs);
  812. free(qs.data);
  813. return tr;
  814. }