All files / src/core/solver solver.ts

78.54% Statements 465/592
75.33% Branches 113/150
97.05% Functions 33/34
78.54% Lines 465/592

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749                          1x 1x 1x 1x 1x 1x 1x       6326x 6326x 6326x 1347538x 1347538x 1347538x 675341x 675341x 675341x 669053x 669053x 669053x   220385x 220385x 220385x 661800x 661800x 661800x 220385x 220385x     662765x 662765x 662765x 662765x 662765x     965x 965x 965x 965x 965x 965x   6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x 6288x   1572x 1572x 1572x 1572x 1572x 1572x 1572x   1572x 1572x 1572x 1572x   1572x 1572x 1572x 1572x 1572x 1572x     1572x   1572x 1572x 1572x 1572x 1572x 1572x 1572x 1572x 1572x       661882x 661882x 661882x 41x 41x 41x 41x 661800x 661800x 661800x 661800x 661800x 661800x                               220385x 220385x 220385x 220385x 220385x 220385x 221030x 221030x 221030x 220385x 220385x   19x 19x 19x 19x 19x 19x 33x 33x 33x 33x 19x 19x   19x 19x     19x 19x     19x 19x 19x 19x 19x     19x       19x 19x 19x 19x   9x 9x 9x 9x 41x 41x 41x 9x 9x   9x 9x 9x 60x 60x 60x 60x 60x 60x 9x   10x 10x 30x 30x 10x 10x 10x                   220385x 220385x 220385x 220385x 220385x 220385x 220385x 220385x     220385x 662727x 661800x 661800x 662727x 927x 927x 927x 927x 927x 927x                                         662727x     220385x     220385x 220385x 220385x     220385x 220385x 661800x 661800x 661800x 661800x   661800x 661800x 661756x 661800x 44x 44x       661800x     220385x 661800x 661800x   661800x 663372x     663372x 663372x 221957x   1572x 1572x   1572x 1572x 1572x 661800x   220385x 220385x   220385x 220385x       44041x 44041x 44041x 44041x 44041x 308702x 308702x 928650x 928650x 1865940x 1865940x 928650x 308702x 44041x 132265x 132265x 132265x 44041x 44041x   44041x 44041x 44041x 44041x 44041x 308702x 308702x 308702x 308702x 44041x 44041x     44041x 44041x 44041x 44041x   44041x 132265x 132265x 132265x 132740x 132740x 10x 10x 10x 132740x 132265x 132265x 10x 10x 10x   132265x 132265x 132740x 132740x 132578x 132740x 132578x 132265x   44041x 44041x 132265x 132265x 132265x 132265x 44041x 44041x   44060x 44060x 44060x 44060x 44060x 44060x 44060x 44060x 44060x 44060x 88120x 88120x 88120x 88120x 88120x   44041x 44041x 44041x 44041x 44041x 44041x 44041x 44041x   44041x 132265x 132265x 132265x 132265x 132265x   132265x 928650x 928650x 132265x 44041x 44041x       8x 8x 8x 8x   8x 38x 38x 8x 38x 38x 38x 38x 38x   8x 8x       19x 19x 19x 19x 19x 19x               19x 19x 19x   19x 176325x 176325x 176325x 176325x   19x 19x   19x 44049x 44049x   44049x 44049x 44049x   44049x 8x 8x   44041x 44041x 44041x   44041x 44049x   44041x 44041x 44041x   44043x 425x 425x 425x 425x 44049x 43616x 43616x   44049x               44049x   44041x 44041x 44041x 44041x 44041x 44041x 44041x 44041x 44041x 44041x 44041x           1x 9x 9x 9x 9x 9x 9x         9x   9x           9x 41x 41x             41x   9x         9x 9x 9x 9x   9x 61x   61x 41x   41x 41x 41x 41x 41x 41x 41x 41x 41x         41x     41x         41x 41x     41x 41x   20x 20x 20x     20x       20x 20x     20x     20x     20x                                                                                                           61x     9x       9x 9x 9x 9x 9x     9x 9x 9x               9x 9x 9x   9x 9x   9x   9x 9x 19x 19x   19x 19x 19x 19x 19x 19x   19x   11x 11x 11x 11x 10x 7x 11x 4x 4x 11x 19x   1x 1x 1x 3x 9x 9x  
/* solver.ts */
 
import type {
  PolygonConstraint,
  VertexKind,
  SimplePolygonSolveOptions,
  PolygonData,
  SolverError,
  SegmentRef,
} from "./solver-types";
 
type Pt = { x: number; y: number };
 
function err(
  kind: SolverError["kind"],
  message: string,
  details?: Record<string, unknown>
): SolverError {
  return { kind, message, details };
}
 
// ---------- basic geometry ----------
 
function dist(a: Pt, b: Pt): number {
  return Math.hypot(a.x - b.x, a.y - b.y);
}
function sub(a: Pt, b: Pt): Pt {
  return { x: a.x - b.x, y: a.y - b.y };
}
function dot(a: Pt, b: Pt): number {
  return a.x * b.x + a.y * b.y;
}
function cross(a: Pt, b: Pt): number {
  return a.x * b.y - a.y * b.x;
}
 
function signedArea(poly: Pt[]): number {
  let a = 0;
  for (let i = 0; i < poly.length; i++) {
    const j = (i + 1) % poly.length;
    a += poly[i].x * poly[j].y - poly[j].x * poly[i].y;
  }
  return 0.5 * a;
}
 
// Signed turn angle in (-π, π], using prev->cur and cur->next
function signedTurn(prev: Pt, cur: Pt, next: Pt): number {
  const e1 = sub(cur, prev);
  const e2 = sub(next, cur);
  return Math.atan2(cross(e1, e2), dot(e1, e2));
}
 
// Interior angle in (0, 2π): interior = π - turn (for CCW simple polygons)
function interiorAngle(prev: Pt, cur: Pt, next: Pt): number {
  const turn = signedTurn(prev, cur, next);
  const ang = Math.PI - turn;
  const twoPi = 2 * Math.PI;
  return ((ang % twoPi) + twoPi) % twoPi;
}
 
function pointSegmentDistance(p: Pt, a: Pt, b: Pt): number {
  const ab = sub(b, a);
  const ap = sub(p, a);
  const ab2 = dot(ab, ab);
  if (ab2 === 0) return dist(p, a);
  let t = dot(ap, ab) / ab2;
  t = Math.max(0, Math.min(1, t));
  const proj = { x: a.x + t * ab.x, y: a.y + t * ab.y };
  return dist(p, proj);
}
 
function segmentsProperlyIntersect(a: Pt, b: Pt, c: Pt, d: Pt): boolean {
  const ab = sub(b, a);
  const ac = sub(c, a);
  const ad = sub(d, a);
  const cd = sub(d, c);
  const ca = sub(a, c);
  const cb = sub(b, c);
 
  const o1 = Math.sign(cross(ab, ac));
  const o2 = Math.sign(cross(ab, ad));
  const o3 = Math.sign(cross(cd, ca));
  const o4 = Math.sign(cross(cd, cb));
 
  return (
    o1 !== 0 &&
    o2 !== 0 &&
    o3 !== 0 &&
    o4 !== 0 &&
    o1 !== o2 &&
    o3 !== o4
  );
}
 
function segmentSegmentDistance(a: Pt, b: Pt, c: Pt, d: Pt): number {
  if (segmentsProperlyIntersect(a, b, c, d)) return 0;
  return Math.min(
    pointSegmentDistance(a, c, d),
    pointSegmentDistance(b, c, d),
    pointSegmentDistance(c, a, b),
    pointSegmentDistance(d, a, b)
  );
}
 
// ---------- segment helpers (edges + diagonals) ----------
 
function normalizeSegment(seg: SegmentRef): SegmentRef {
  return seg.i < seg.j ? seg : { i: seg.j, j: seg.i };
}
function segmentKey(seg: SegmentRef): string {
  const s = normalizeSegment(seg);
  return `${s.i}-${s.j}`;
}
function segmentVec(pts: Pt[], seg: SegmentRef): Pt {
  return { x: pts[seg.j].x - pts[seg.i].x, y: pts[seg.j].y - pts[seg.i].y };
}
function vecLen(v: Pt): number {
  return Math.hypot(v.x, v.y);
}
// Undirected angle between vectors in [0, π]
function undirectedAngleBetween(u: Pt, v: Pt): number {
  return Math.atan2(Math.abs(cross(u, v)), dot(u, v));
}
 
// ---------- variable packing/unpacking (gauge fixing) ----------
//
// Fix:
//   p0 = (0,0)
//   p1.y = 0
//
// Variables x length = 2N-3:
//   x[0] = p1.x
//   then for i=2..N-1: (xi, yi)
 
function unpackVars(N: number, x: number[]): Pt[] {
  const pts: Pt[] = new Array(N);
  pts[0] = { x: 0, y: 0 };
  pts[1] = { x: x[0], y: 0 };
  let k = 1;
  for (let i = 2; i < N; i++) {
    pts[i] = { x: x[k], y: x[k + 1] };
    k += 2;
  }
  return pts;
}
 
function packVars(pts: Pt[]): number[] {
  const N = pts.length;
  const x: number[] = new Array(2 * N - 3);
  x[0] = pts[1].x;
  let k = 1;
  for (let i = 2; i < N; i++) {
    x[k] = pts[i].x;
    x[k + 1] = pts[i].y;
    k += 2;
  }
  return x;
}
 
function gaugeFix(pts: Pt[]): Pt[] {
  const N = pts.length;
 
  // translate so p0 -> origin
  const t = { x: pts[0].x, y: pts[0].y };
  const p = pts.map((q) => ({ x: q.x - t.x, y: q.y - t.y }));
 
  // rotate so p1 lies on +x axis
  const p1 = p[1];
  const ang = Math.atan2(p1.y, p1.x);
  const c = Math.cos(-ang);
  const s = Math.sin(-ang);
  const r = p.map((q) => ({ x: c * q.x - s * q.y, y: s * q.x + c * q.y }));
 
  // ensure p1.x positive
  if (r[1].x < 0) {
    for (let i = 0; i < N; i++) r[i] = { x: -r[i].x, y: -r[i].y };
  }
 
  r[1].y = 0;
  r[0] = { x: 0, y: 0 };
  return r;
}
 
function regularNgonInit(N: number): Pt[] {
  const R = 1;
  const pts: Pt[] = [];
  for (let i = 0; i < N; i++) {
    const t = (2 * Math.PI * i) / N;
    pts.push({ x: R * Math.cos(t), y: R * Math.sin(t) });
  }
  return gaugeFix(pts);
}
 
function mulberry32(seed: number): () => number {
  let a = seed >>> 0;
  return function () {
    a |= 0;
    a = (a + 0x6d2b79f5) | 0;
    let t = Math.imul(a ^ (a >>> 15), 1 | a);
    t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}
 
function jitter(pts: Pt[], rng: () => number, scale = 0.05): Pt[] {
  const out = pts.map((p) => ({
    x: p.x + (rng() * 2 - 1) * scale,
    y: p.y + (rng() * 2 - 1) * scale,
  }));
  return gaugeFix(out);
}
 
// ---------- residual building ----------
 
type ResidualBuild = {
  residuals: number[]; // hard + weighted penalties
  hardResiduals: number[]; // only numeric constraints
  penaltyResiduals: number[]; // only penalties before weighting
};
 
function buildResiduals(
  pts: Pt[],
  vertexKinds: VertexKind[],
  constraints: PolygonConstraint[],
  simplicityEps: number
): ResidualBuild {
  const N = pts.length;
  const hard: number[] = [];
 
  // hard numeric constraints
  for (const c of constraints) {
    if (c.type === "length") {
      const seg = normalizeSegment(c.seg);
      hard.push(vecLen(segmentVec(pts, seg)) - c.length);
    } else if (c.type === "interiorAngle") {
      const i = c.i;
      const prev = pts[(i + N - 1) % N];
      const cur = pts[i];
      const next = pts[(i + 1) % N];
      hard.push(interiorAngle(prev, cur, next) - c.angleRad);
    } else if (c.type === "segmentLengthRatio") {
      const sx = normalizeSegment(c.segX);
      const sy = normalizeSegment(c.segY);
      const lx = vecLen(segmentVec(pts, sx));
      const ly = vecLen(segmentVec(pts, sy));
      hard.push(lx - c.factor * ly);
    } else {
      // segmentRelativeAngle
      const sa = normalizeSegment(c.segA);
      const sb = normalizeSegment(c.segB);
      const ua = segmentVec(pts, sa);
      const ub = segmentVec(pts, sb);
      const la = vecLen(ua);
      const lb = vecLen(ub);
      const minLen = 1e-12;
      if (la < minLen || lb < minLen) {
        hard.push(1e3);
      } else {
        hard.push(undirectedAngleBetween(ua, ub) - c.angleRad);
      }
    }
  }
 
  // penalties (inequalities / geometry validity)
  const pen: number[] = [];
 
  // CCW area margin (this solver is still "SimpleNgon" but enforces CCW)
  const area = signedArea(pts);
  const areaMin = 1e-8;
  pen.push(area < areaMin ? areaMin - area : 0);
 
  // vertexKinds enforcement
  const turnEps = 1e-6;
  for (let i = 0; i < N; i++) {
    const prev = pts[(i + N - 1) % N];
    const cur = pts[i];
    const next = pts[(i + 1) % N];
    const t = signedTurn(prev, cur, next);
 
    const kind = vertexKinds[i];
    if (kind === "convex") {
      pen.push(Math.max(0, turnEps - t));
    } else if (kind === "reflex") {
      pen.push(Math.max(0, t + turnEps));
    } else {
      // straight: |turn| <= eps
      pen.push(Math.max(0, Math.abs(t) - turnEps));
    }
  }
 
  // simplicity: penalize non-adjacent edge pairs that are too close/intersect
  for (let i = 0; i < N; i++) {
    const a = pts[i];
    const b = pts[(i + 1) % N];
 
    for (let j = i + 1; j < N; j++) {
      const k = (j + 1) % N;
 
      // skip same/adjacent edges (sharing a vertex)
      if (j === i) continue;
      if (j === (i + 1) % N) continue;
      if (i === (j + 1) % N) continue;
 
      const c = pts[j];
      const d = pts[k];
 
      const dmin = segmentSegmentDistance(a, b, c, d);
      pen.push(Math.max(0, simplicityEps - dmin));
    }
  }
 
  const penaltyWeight = 100;
  const residuals = hard.concat(pen.map((v) => penaltyWeight * v));
 
  return { residuals, hardResiduals: hard, penaltyResiduals: pen };
}
 
// ---------- numeric linear algebra ----------
 
function matMulAtA(J: number[][], lambda: number): number[][] {
  const m = J.length;
  const n = J[0].length;
  const A: number[][] = Array.from({ length: n }, () => new Array(n).fill(0));
  for (let i = 0; i < m; i++) {
    const row = J[i];
    for (let a = 0; a < n; a++) {
      const ra = row[a];
      for (let b = 0; b <= a; b++) {
        A[a][b] += ra * row[b];
      }
    }
  }
  for (let a = 0; a < n; a++) {
    for (let b = 0; b < a; b++) A[b][a] = A[a][b];
    A[a][a] += lambda;
  }
  return A;
}
 
function matMulAtR(J: number[][], r: number[]): number[] {
  const m = J.length;
  const n = J[0].length;
  const g = new Array(n).fill(0);
  for (let i = 0; i < m; i++) {
    const row = J[i];
    const ri = r[i];
    for (let a = 0; a < n; a++) g[a] += row[a] * ri;
  }
  return g;
}
 
// Solve A x = b via Gaussian elimination with partial pivoting
function solveLinearSystem(Ain: number[][], bin: number[]): number[] | null {
  const n = bin.length;
  const A = Ain.map((row) => row.slice());
  const b = bin.slice();
 
  for (let i = 0; i < n; i++) {
    let piv = i;
    let best = Math.abs(A[i][i]);
    for (let r = i + 1; r < n; r++) {
      const v = Math.abs(A[r][i]);
      if (v > best) {
        best = v;
        piv = r;
      }
    }
    if (best < 1e-14) return null;
    if (piv !== i) {
      [A[i], A[piv]] = [A[piv], A[i]];
      [b[i], b[piv]] = [b[piv], b[i]];
    }
 
    const diag = A[i][i];
    for (let r = i + 1; r < n; r++) {
      const f = A[r][i] / diag;
      if (f === 0) continue;
      b[r] -= f * b[i];
      for (let c = i; c < n; c++) A[r][c] -= f * A[i][c];
    }
  }
 
  const x = new Array(n).fill(0);
  for (let i = n - 1; i >= 0; i--) {
    let s = b[i];
    for (let c = i + 1; c < n; c++) s -= A[i][c] * x[c];
    x[i] = s / A[i][i];
  }
  return x;
}
 
function dotVec(a: number[], b: number[]): number {
  let s = 0;
  for (let i = 0; i < a.length; i++) s += a[i] * b[i];
  return s;
}
function norm2(v: number[]): number {
  let s = 0;
  for (const x of v) s += x * x;
  return Math.sqrt(s);
}
function maxAbs(v: number[]): number {
  let m = 0;
  for (const x of v) m = Math.max(m, Math.abs(x));
  return m;
}
 
function finiteDiffJacobian(
  f: (x: number[]) => number[],
  x: number[],
  fx: number[]
): number[][] {
  const n = x.length;
  const m = fx.length;
  const J: number[][] = Array.from({ length: m }, () => new Array(n).fill(0));
 
  for (let j = 0; j < n; j++) {
    const xj = x[j];
    const h = 1e-6 * (1 + Math.abs(xj));
    x[j] = xj + h;
    const f2 = f(x);
    x[j] = xj;
 
    for (let i = 0; i < m; i++) {
      J[i][j] = (f2[i] - fx[i]) / h;
    }
  }
  return J;
}
 
// ---------- output ----------
 
function buildPolygonData(pts: Pt[]): PolygonData {
  const N = pts.length;
  const sideLengths: number[] = new Array(N);
  const interiorAngles: number[] = new Array(N);
 
  for (let i = 0; i < N; i++) {
    sideLengths[i] = dist(pts[i], pts[(i + 1) % N]);
  }
  for (let i = 0; i < N; i++) {
    const prev = pts[(i + N - 1) % N];
    const cur = pts[i];
    const next = pts[(i + 1) % N];
    interiorAngles[i] = interiorAngle(prev, cur, next);
  }
 
  return { sides: N, sideLengths, interiorAngles };
}
 
// ---------- LM run ----------
 
function runLM(
  N: number,
  vertexKinds: VertexKind[],
  constraints: PolygonConstraint[],
  x0: number[],
  cfg: {
    maxIterations: number;
    tol: number;
    simplicityEps: number;
    diagnostics: "none" | "basic" | "verbose";
  }
):
  | { ok: true; value: PolygonData }
  | { ok: false; attempt?: { hardMax: number; penMax: number; hardNorm: number } } {
  let x = x0.slice();
  let lambda = 1e-3;
 
  const f = (xx: number[]) => {
    const pts = unpackVars(N, xx);
    return buildResiduals(pts, vertexKinds, constraints, cfg.simplicityEps)
      .residuals;
  };
 
  let r = f(x);
  let cost = 0.5 * dotVec(r, r);
 
  for (let iter = 0; iter < cfg.maxIterations; iter++) {
    const pts = unpackVars(N, x);
    const built = buildResiduals(pts, vertexKinds, constraints, cfg.simplicityEps);
 
    const hardMax = maxAbs(built.hardResiduals);
    const penMax = maxAbs(built.penaltyResiduals);
    const hardNorm = norm2(built.hardResiduals);
 
    if (hardMax <= cfg.tol && penMax <= 1e-9) {
      return { ok: true, value: buildPolygonData(pts) };
    }
 
    const J = finiteDiffJacobian(f, x, r);
    const A = matMulAtA(J, lambda);
    const g = matMulAtR(J, r);
 
    const dx = solveLinearSystem(A, g.map((v) => -v));
    if (!dx) return { ok: false, attempt: { hardMax, penMax, hardNorm } };
 
    const xNew = x.map((v, i) => v + dx[i]);
    const rNew = f(xNew);
    const costNew = 0.5 * dotVec(rNew, rNew);
 
    if (costNew < cost) {
      x = xNew;
      r = rNew;
      cost = costNew;
      lambda = Math.max(1e-12, lambda * 0.33);
    } else {
      lambda = Math.min(1e12, lambda * 3.0);
    }
 
    if (cfg.diagnostics === "verbose" && iter % 25 === 0) {
      // eslint-disable-next-line no-console
      console.log(
        `[iter ${iter}] cost=${cost.toExponential(3)} hardMax=${hardMax.toExponential(
          3
        )} penMax=${penMax.toExponential(3)} lambda=${lambda.toExponential(2)}`
      );
    }
  }
 
  const pts = unpackVars(N, x);
  const built = buildResiduals(pts, vertexKinds, constraints, cfg.simplicityEps);
  return {
    ok: false,
    attempt: {
      hardMax: maxAbs(built.hardResiduals),
      penMax: maxAbs(built.penaltyResiduals),
      hardNorm: norm2(built.hardResiduals),
    },
  };
}
 
// ---------- public solver ----------
//
// NOTE: Despite the name, this enforces CCW via a penalty. If you later want
// to allow both orientations, remove the area penalty and add an option.
export function solveSimpleNgon(
  vertexCount: number,
  vertexKinds: VertexKind[],
  constraints: PolygonConstraint[],
  options?: SimplePolygonSolveOptions
): PolygonData | SolverError {
  if (!Number.isInteger(vertexCount) || vertexCount < 3) {
    return err("InvalidInput", "vertexCount must be an integer >= 3", {
      vertexCount,
    });
  }
  const N = vertexCount;
 
  if (vertexKinds.length !== N) {
    return err("InvalidInput", "vertexKinds must have length N", {
      expected: N,
      got: vertexKinds.length,
    });
  }
  for (let i = 0; i < N; i++) {
    const k = vertexKinds[i];
    if (k !== "convex" && k !== "reflex" && k !== "straight") {
      return err(
        "InvalidInput",
        `vertexKinds[${i}] must be "convex", "reflex", or "straight"`,
        { value: k }
      );
    }
  }
 
  if (constraints.length === 0) {
    return err("InvalidInput", "At least one constraint is required");
  }
 
  // Validate constraints
  const absoluteLengthSegs = new Set<string>();
  const ratioKeys = new Set<string>();
  const relAngleKeys = new Set<string>();
  let hasAbsoluteLength = false;
 
  for (let idx = 0; idx < constraints.length; idx++) {
    const c = constraints[idx];
 
    if (c.type === "length") {
      hasAbsoluteLength = true;
 
      const seg = normalizeSegment(c.seg);
      if (
        !Number.isInteger(seg.i) ||
        seg.i < 0 ||
        seg.i >= N ||
        !Number.isInteger(seg.j) ||
        seg.j < 0 ||
        seg.j >= N
      ) {
        return err("InvalidInput", `Invalid vertex index in length at ${idx}`, {
          c,
        });
      }
      if (seg.i === seg.j) {
        return err("InvalidInput", `length.seg at ${idx} has i === j`, { c });
      }
      if (!(c.length > 0) || !Number.isFinite(c.length)) {
        return err("InvalidInput", `length.length at ${idx} must be > 0`, {
          c,
        });
      }
      const key = segmentKey(seg);
      if (absoluteLengthSegs.has(key)) {
        return err("InvalidInput", `duplicate length constraint for segment ${key}`, { c });
      }
      absoluteLengthSegs.add(key);
    }
 
    else if (c.type === "interiorAngle") {
      const { i, angleRad } = c;
      if (!Number.isInteger(i) || i < 0 || i >= N) {
        return err("InvalidInput", `Invalid vertex index in interiorAngle at ${idx}`, { c });
      }
      if (!Number.isFinite(angleRad) || !(angleRad > 0) || !(angleRad < 2 * Math.PI)) {
        return err("InvalidInput", `interiorAngle.angleRad at ${idx} must satisfy 0 < angleRad < 2π (π allowed)`, { c });
      }
 
      const kind = vertexKinds[i];
      if (kind === "convex" && !(angleRad < Math.PI)) {
        return err("InvalidInput", `interiorAngle at ${idx} conflicts with vertexKinds[${i}] === "convex"`, { c });
      }
      if (kind === "reflex" && !(angleRad > Math.PI)) {
        return err("InvalidInput", `interiorAngle at ${idx} conflicts with vertexKinds[${i}] === "reflex"`, { c });
      }
      if (kind === "straight" && angleRad !== Math.PI) {
        return err("InvalidInput", `interiorAngle at ${idx} conflicts with vertexKinds[${i}] === "straight" (must be π)`, { c });
      }
    }
 
    else if (c.type === "segmentLengthRatio") {
      const sx = normalizeSegment(c.segX);
      const sy = normalizeSegment(c.segY);
 
      for (const [name, s] of [["segX", sx], ["segY", sy]] as const) {
        if (
          !Number.isInteger(s.i) ||
          s.i < 0 ||
          s.i >= N ||
          !Number.isInteger(s.j) ||
          s.j < 0 ||
          s.j >= N
        ) {
          return err("InvalidInput", `Invalid vertex index in segmentLengthRatio.${name} at ${idx}`, { c });
        }
        if (s.i === s.j) return err("InvalidInput", `segmentLengthRatio.${name} at ${idx} has i === j`, { c });
      }
      if (!Number.isFinite(c.factor) || !(c.factor > 0)) {
        return err("InvalidInput", `segmentLengthRatio.factor at ${idx} must be > 0`, { c });
      }
 
      const key = `${segmentKey(sx)}=${c.factor}*${segmentKey(sy)}`;
      if (ratioKeys.has(key)) return err("InvalidInput", `Duplicate segmentLengthRatio constraint`, { c });
      ratioKeys.add(key);
    }
 
    else {
      // segmentRelativeAngle
      const sa = normalizeSegment(c.segA);
      const sb = normalizeSegment(c.segB);
 
      for (const [name, s] of [["segA", sa], ["segB", sb]] as const) {
        if (
          !Number.isInteger(s.i) ||
          s.i < 0 ||
          s.i >= N ||
          !Number.isInteger(s.j) ||
          s.j < 0 ||
          s.j >= N
        ) {
          return err("InvalidInput", `Invalid vertex index in segmentRelativeAngle.${name} at ${idx}`, { c });
        }
        if (s.i === s.j) return err("InvalidInput", `segmentRelativeAngle.${name} at ${idx} has i === j`, { c });
      }
      if (!Number.isFinite(c.angleRad) || c.angleRad < 0 || c.angleRad > Math.PI) {
        return err("InvalidInput", `segmentRelativeAngle.angleRad at ${idx} must satisfy 0 <= angleRad <= π`, { c });
      }
 
      const key = `angle(${segmentKey(sa)},${segmentKey(sb)})=${c.angleRad}`;
      if (relAngleKeys.has(key)) return err("InvalidInput", `Duplicate segmentRelativeAngle constraint`, { c });
      relAngleKeys.add(key);
    }
  }
 
  // Fix global scale: need at least one absolute length.
  if (!hasAbsoluteLength) {
    return err("InvalidInput", "At least one absolute 'length' constraint is required to fix global scale");
  }
 
  const maxIterations = options?.maxIterations ?? 3000;
  const tol = options?.tolerance ?? 1e-10;
  const simplicityEps = options?.simplicityEpsilon ?? 1e-6;
  const restarts = options?.restarts ?? 0;
  const diag = options?.diagnostics ?? "none";
 
  // init
  let initPts: Pt[];
  const init = options?.initialization ?? ({ kind: "regularNgon" } as const);
  if (init.kind === "custom") {
    if (init.initialVertices.length !== N) {
      return err("InvalidInput", "custom initialization must provide N vertices", {
        expected: N,
        got: init.initialVertices.length,
      });
    }
    initPts = gaugeFix(init.initialVertices.map((p) => ({ x: p.x, y: p.y })));
  } else {
    initPts = regularNgonInit(N);
  }
 
  const seed = (options?.randomSeed ?? 123456789) | 0;
  const rng = mulberry32(seed);
 
  let best: { hardMax: number; penMax: number; hardNorm: number } | null = null;
 
  const trials = 1 + Math.max(0, restarts);
  for (let trial = 0; trial < trials; trial++) {
    const startPts = trial === 0 ? initPts : jitter(initPts, rng, 0.15);
    const startX = packVars(startPts);
 
    const res = runLM(N, vertexKinds, constraints, startX, {
      maxIterations,
      tol,
      simplicityEps,
      diagnostics: diag,
    });
 
    if (res.ok) return res.value;
 
    if (res.attempt) {
      const a = res.attempt;
      if (
        !best ||
        a.hardMax < best.hardMax ||
        (a.hardMax === best.hardMax && a.penMax < best.penMax)
      ) {
        best = a;
      }
    }
  }
 
  return err(
    "Infeasible",
    "Could not satisfy constraints and geometric requirements within tolerance",
    best ?? undefined
  );
}