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