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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 1x 4x 4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 24x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 183x 1x 1x 1x 531x 531x 531x 531x 531x 531x 531x 531x 531x 523x 133x 133x 1x 1x 1x 1x 181x 464x 181x 181x 181x 50x 50x 1x 156x 43x 113x 37x 76x 36x 40x 1x 1x 1x 1x 1x 1x 1x 1x 8734x 8734x 8734x 8734x 8734x 8734x 1x 1x 4055x 4055x 4055x 4055x 4055x 4055x 4055x 4055x 1x 624x 1872x 1872x 624x 624x 624x 1x 156x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 516x 516x 516x 2059x 1953x 2059x 732x 732x 732x 516x 516x 516x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 16x 48x 48x 48x 48x 48x 48x 40x 40x 40x 48x 48x 16x 4x 4x 4x 80x 80x 20x 20x 20x 80x 80x 4x 32x 32x 32x 32x 4x 16x 16x 16x 4x 4x 4x 4x 4x 4x 16x 16x 16x 12x 12x 12x 12x 16x 16x 16x 48x 48x 48x 48x 48x 48x 48x 48x 16x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 12x 12x 4x 4x 4x 12x 4x 4x 12x 8x 8x 8x 8x 4x 4x 4x 1x 1x 1x 1x 1x 12x 4x 8x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // Abacus Studio — pure parametric model logic (graduated verbatim from the
// off-stack bench's main.ts; see Gitea epic #5, Phase 0 #6).
//
// SPEC: apps/web/docs/abacus-studio/master-model-spec.md — anatomy, the
// locked-vs-proportional dimension model, and the constant-clearance proof this
// module's `derived()` implements.
//
// This module is framework-free and three-free: it turns the intent-knob
// parameter surface into the `-D` defines the OpenSCAD-WASM worker renders, and
// it carries the myabacus color / AMS-filament model plus the union-find shell
// classifier used to recolor a rendered STL without re-rendering. The viewer
// component owns all three.js; this file owns all geometry math + color mapping.
//
// INTENT KNOBS ONLY: every raw coordinate (frame_d, col_pitch, end_margin,
// earth_pitch, heaven_y, throw spans) is DERIVED here and in the .scad from the
// knobs (cols/earth/web/print_gap/throw/bar/shelf). Incoherent layouts are
// unrepresentable — the frame is built around wherever the beads land. The
// `clearance` + `print_gap` gaps are ABSOLUTE (held constant while everything
// else scales with `scale_factor`), which is what keeps the printed fit constant
// across sizes — the load-bearing result Phase 0 set out to prove.
// The canonical bead-color resolver, shared with the on-screen abacus. Imported
// from the React-free `./color` subpath so this module stays framework-free.
import { BEAD_COLOR_PALETTES, beadColorActive } from '@soroban/abacus-react/color'
// ---- parameters -------------------------------------------------------------
export const defaultParams = {
// frame
frame_h: 8,
border_w: 5.25,
corner_r: 4,
// grid (everything else is derived from these)
cols: 13,
earth: 4,
web: 2.5, // wall between channels (→ col_pitch 13.0)
print_gap: 2, // printed air between beads, absolute (→ rest pitch 10)
throw: 10, // slide distance of a bead group (→ master's earth channel)
bar: 7.5, // reckoning-bar clear width (→ classic 90mm field)
shelf: 7.75, // solid margin inside the field (→ strip 13.0, tile-tight)
// bead
bead_dia: 10,
bead_len: 8,
bead_proud: 4,
// locked (the mixed-scaling spine — stay constant as you scale)
clearance: 0.25,
top_chamfer: 1,
marker_mm: 12,
// myabacus style (abaci.one AbacusDisplayConfig projection)
color_scheme: 'place-value',
color_palette: 'default',
scale_factor: 1.0,
bead_shape: 'spool',
frame_color: '#c9a26e',
// filament palette (AMS): up to 8 loaded slots; every style role is quantized
// onto them (see computeFilamentMap).
filament_count: 8,
filament_1: '#c9a26e',
filament_2: '#f5f5f5',
filament_3: '#111111',
filament_4: '#2E86AB',
filament_5: '#A23B72',
filament_6: '#F18F01',
filament_7: '#6A994E',
filament_8: '#BC4B51',
// perimeter text: 4 top-face rails (preset or custom tokens) + 4 side walls
top_preset: 'friends-of-10',
top_text: '',
bottom_preset: 'friends-of-5',
bottom_text: '',
left_preset: 'custom',
left_text: '',
right_preset: 'custom',
right_text: '',
edge_front: '',
edge_back: '',
edge_left: '',
edge_right: '',
text_mode: 'inset',
text_fill: 'rainbow',
text_color: '#f5f5f5',
text_size: 6,
edge_text_size: 5,
// adhesive feet (bottom face; absolute — real feet don't scale)
feet: true,
feet_preset: 'circle 9',
feet_shape: 'circle',
feet_w: 9,
feet_depth: 1.5,
feet_fit: 0.15,
retention: 'none',
feet_undercut: 0,
feet_span: 110, // max unsupported bottom run (mm @ scale 1) before mid feet
// toggles / quality
show_frame: true,
show_beads: true,
show_markers: true,
fn: 32,
}
export type Params = typeof defaultParams
// ---- project the app's AbacusDisplayConfig onto print params ----------------
// The toy opens showing the user's ACTUAL abacus: its column count and color
// identity carry over from their AbacusDisplayConfig (abacus_settings). This is
// a READ-ONLY projection — tweaking the toy never writes back to display
// settings. Only the abacus's IDENTITY maps (columns + colors); physical print
// size is intentionally NOT projected. The display `scaleFactor` is an on-screen
// zoom (someone who zoomed in for readability didn't ask for a giant print), so
// print size stays a deliberate fabrication choice made with the size knob.
//
// The input is a narrow structural type, not the react package's
// `AbacusDisplayConfig` — this module stays framework-free, and the caller
// passes just the four fields it reads.
export type DisplayConfigInput = {
colorScheme: string
colorPalette: string
physicalAbacusColumns: number
}
// the scad asserts a 3-column floor; the app allows 1–21, so clamp the low end.
export const clampCols = (n: number): number => Math.max(3, Math.min(21, Math.round(n)))
export function paramsFromDisplayConfig(
cfg: DisplayConfigInput,
base: Params = defaultParams
): Params {
return {
...base,
cols: clampCols(cfg.physicalAbacusColumns),
color_scheme: cfg.colorScheme,
color_palette: cfg.colorPalette,
}
}
// ---- perimeter text tokens --------------------------------------------------
export const TEXT_PRESETS: Record<string, string[]> = {
'friends-of-10': ['1+9', '2+8', '3+7', '4+6', '5+5'],
'friends-of-5': ['1+4', '2+3', '3+2', '4+1'],
}
export const PRESET_OPTS = ['custom', ...Object.keys(TEXT_PRESETS)]
// common stick-on foot sizes; sizes above ~10 need a bigger border_w (the scad
// asserts). Pick 'custom' (or touch any knob) to free-edit.
export const FEET_PRESETS: Record<
string,
{ feet_shape: string; feet_w: number; feet_depth: number }
> = {
'circle 8': { feet_shape: 'circle', feet_w: 8, feet_depth: 1.2 },
'circle 9': { feet_shape: 'circle', feet_w: 9, feet_depth: 1.5 },
'circle 10': { feet_shape: 'circle', feet_w: 10, feet_depth: 1.5 },
'square 8': { feet_shape: 'square', feet_w: 8, feet_depth: 1.2 },
}
// token → [string, fontIdx]; fontIdx 1 = Noto Emoji for emoji tokens (OpenSCAD
// has no per-glyph fallback, so mixed emoji+text inside ONE token will tofu).
export const tokenize = (s: string): [string, number][] =>
s
.trim()
.split(/\s+/)
.filter(Boolean)
.map((t) => [t, /\p{Extended_Pictographic}/u.test(t) ? 1 : 0])
export const slotTokens = (preset: string, custom: string): [string, number][] =>
preset !== 'custom' && TEXT_PRESETS[preset]
? TEXT_PRESETS[preset].map((t) => [t, 0] as [string, number])
: tokenize(custom)
// ---- derived layout — mirrors the scad's intent-knob chain EXACTLY -----------
// (same s_* math: clearance + print_gap absolute, everything else scales with S;
// shelf auto-grows so band+shelf always holds a corner ArUco tile).
export type Derived = {
sShelf: number
sCp: number
sEm: number
sEp: number
sElo: number
sEty: number
sEhi: number
sHlo: number
sHhi: number
sHy: number
sFd: number
mkI: number
chamf: number
frameW: number
outerD: number
}
export const derived = (p: Params): Derived => {
const S = p.scale_factor
const cl = p.clearance
const sBd = p.bead_dia * S
const sBl = p.bead_len * S
const sBw = p.border_w * S
const sFh = p.frame_h * S
const sCr = p.corner_r * S
const chamf = Math.min(p.top_chamfer, sFh * 0.4)
const mkI = Math.max(0, chamf, sCr <= 0 ? 0 : sCr - (sCr - chamf) / Math.SQRT2 - p.marker_mm / 9)
const sShelf = Math.max(p.shelf * S, mkI + p.marker_mm - sBw)
const sCp = sBd + 2 * cl + p.web * S
const sEm = sShelf + cl + sBd / 2
const sEp = sBl + p.print_gap
const sElo = sShelf + cl + sBl / 2
const sEty = sElo + sEp * (p.earth - 1)
const sEhi = sEty + p.throw * S
const sHlo = sEhi + sBl + 2 * cl + p.bar * S
const sHhi = sHlo + p.throw * S
const sFd = sHhi + sBl / 2 + cl + sShelf
const fieldW = 2 * sEm + (p.cols - 1) * sCp
return {
sShelf,
sCp,
sEm,
sEp,
sElo,
sEty,
sEhi,
sHlo,
sHhi,
sHy: sHhi,
sFd,
mkI,
chamf,
frameW: fieldW + 2 * sBw,
outerD: sFd + 2 * sBw,
}
}
// OUTER dims: bead field (from cols) + the flush border band on each side.
export const frameW = (p: Params): number => derived(p).frameW
export const outerD = (p: Params): number => derived(p).outerD
// ---- -D defines the worker renders ------------------------------------------
// frame_w/frame_d and all pitches are intentionally NOT here — the .scad derives
// them from the intent knobs. scale_factor rides along as geometry; the color_* /
// bead_shape / frame_color style keys are JS-only (the STL is colorless — coloring
// happens in the viewer's recolor pass).
export const DEFINE_KEYS: (keyof Params)[] = [
'frame_h',
'border_w',
'corner_r',
'cols',
'earth',
'web',
'print_gap',
'throw',
'bar',
'shelf',
'bead_dia',
'bead_len',
'bead_proud',
'clearance',
'top_chamfer',
'marker_mm',
'scale_factor',
'text_mode',
'text_fill',
'text_color',
'text_size',
'edge_text_size',
'feet',
'feet_shape',
'feet_w',
'feet_depth',
'feet_fit',
'feet_undercut',
'feet_span',
'show_frame',
'show_beads',
'show_markers',
]
export const definesFrom = (p: Params): string[] => [
// JSON serialization doubles as OpenSCAD literal syntax for numbers, booleans,
// quoted strings AND [string, fontIdx] token vectors.
...DEFINE_KEYS.map((k) => `-D${k}=${JSON.stringify(p[k])}`),
`-Dtext_top=${JSON.stringify(slotTokens(p.top_preset, p.top_text))}`,
`-Dtext_bottom=${JSON.stringify(slotTokens(p.bottom_preset, p.bottom_text))}`,
`-Dtext_left=${JSON.stringify(slotTokens(p.left_preset, p.left_text))}`,
`-Dtext_right=${JSON.stringify(slotTokens(p.right_preset, p.right_text))}`,
`-Dedge_front=${JSON.stringify(tokenize(p.edge_front))}`,
`-Dedge_back=${JSON.stringify(tokenize(p.edge_back))}`,
`-Dedge_left=${JSON.stringify(tokenize(p.edge_left))}`,
`-Dedge_right=${JSON.stringify(tokenize(p.edge_right))}`,
]
// ---- myabacus color model ---------------------------------------------------
// Bead colors come from the shared canonical resolver (beadColorActive, imported
// above); this module only adds the print-side role → filament-slot mapping on
// top of it — no copied color tables. Re-export the palette table under its
// historical name so the viewer's plugRecolor keeps importing it from here.
export const COLOR_PALETTES: Record<string, string[]> = BEAD_COLOR_PALETTES
// The place-value role count = palette length. Deriving it (instead of a
// hardcoded 5) keeps beadRoleIndex in lockstep with the resolver's
// `placeValue % colors.length` by construction, not by coincidence.
const paletteLen = (palette: string): number =>
(BEAD_COLOR_PALETTES[palette] ?? BEAD_COLOR_PALETTES.default).length
// A bead resolves role → intended hex → filament slot. Column i runs left→right;
// the rightmost column is the ones place (placeValue 0).
export const beadRoleIndex = (
i: number,
isHeaven: boolean,
scheme: string,
cols: number,
palette: string
): number => {
const pv = cols - 1 - i
if (scheme === 'monochrome') return 0
if (scheme === 'heaven-earth') return isHeaven ? 0 : 1
if (scheme === 'alternating') return pv % 2 === 0 ? 0 : 1
return pv % paletteLen(palette)
}
// Intended hex per role, sourced from the canonical resolver via a representative
// (placeValue, type) for each role — one role per palette entry for place-value,
// two for heaven-earth/alternating, one for monochrome. Feeds computeFilamentMap.
export const beadRoleColors = (scheme: string, palette: string): string[] => {
const c = (placeValue: number, type: 'heaven' | 'earth'): string =>
beadColorActive({ placeValue, type }, scheme, palette)
if (scheme === 'monochrome') return [c(0, 'earth')]
if (scheme === 'heaven-earth') return [c(0, 'heaven'), c(0, 'earth')]
if (scheme === 'alternating') return [c(0, 'earth'), c(1, 'earth')]
return Array.from({ length: paletteLen(palette) }, (_, pv) => c(pv, 'earth'))
}
export const beadRoleNames = (scheme: string): string[] =>
scheme === 'monochrome'
? ['bead']
: scheme === 'heaven-earth'
? ['heaven', 'earth']
: scheme === 'alternating'
? ['even col', 'odd col']
: ['1s', '10s', '100s', '1k', '10k']
// ---- filament mapping primitives (screen colors → AMS slots) ----------------
// The role-aware quantizer that consumes these — markers first, then frame, then
// bead roles distinct-first — moved to abacus-plan.ts (`materialize`); its legacy
// `computeFilamentMap` shape re-exports from there. This file keeps only the pure
// color primitives (distance, luminance, contrast, nearest-slot) that both the
// plan and the viewer's plug pass still share.
export const hexRGB = (h: string): [number, number, number] => {
let s = h.replace('#', '')
if (s.length === 3)
s = s
.split('')
.map((c) => c + c)
.join('')
const n = Number.parseInt(s, 16)
return [(n >> 16) & 255, (n >> 8) & 255, n & 255]
}
// redmean color distance — cheap and perceptually decent for spool picking
export const colorDist = (a: string, b: string): number => {
const [r1, g1, b1] = hexRGB(a)
const [r2, g2, b2] = hexRGB(b)
const rm = (r1 + r2) / 2
const dr = r1 - r2
const dg = g1 - g2
const db = b1 - b2
return Math.sqrt((2 + rm / 256) * dr * dr + 4 * dg * dg + (2 + (255 - rm) / 256) * db * db)
}
export const lum = (hex: string): number => {
const [r, g, b] = hexRGB(hex).map((v) => {
const s = v / 255
return s <= 0.03928 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4
})
return 0.2126 * r + 0.7152 * g + 0.0722 * b
}
export const contrastRatio = (a: string, b: string): number =>
(Math.max(lum(a), lum(b)) + 0.05) / (Math.min(lum(a), lum(b)) + 0.05)
export type FilamentMap = {
slots: string[] // the loaded spools (filament_1..filament_count)
frame: number // slot index per role ↓
markerWhite: number
markerBlack: number
beadRoles: number[] // slot index per bead role (see beadRoleIndex)
markerContrast: number // WCAG ratio of the mapped marker pair (CV wants ≥3)
}
export const nearestSlot = (slots: string[], target: string, exclude = -1): number => {
let best = 0
let bd = Number.POSITIVE_INFINITY
slots.forEach((s, idx) => {
if (idx === exclude) return
const d = colorDist(target, s)
if (d < bd) {
bd = d
best = idx
}
})
return best
}
// `computeFilamentMap` (the role → slot map) now lives in abacus-plan.ts as an
// adapter over `materialize`; import it from there.
// ---- ArUco corner marker bits (js-aruco2 'ARUCO' codeList) -------------------
// The abaci.one detector reads these; '1' → white cell, row 0 = top. IDs land on
// their matching frame corner: TL=0, TR=1, BR=2, BL=3.
export const MARKER_BITS = [
'1000010000100001000010000', // id 0 → TL
'1000010000100001000010111', // id 1 → TR
'1000010000100001000001001', // id 2 → BR
'1000010000100001000001110', // id 3 → BL
]
// ---- shell classifier -------------------------------------------------------
// Per-triangle shell membership + each shell's semantics, so a scheme/palette
// change recolors the existing geometry instantly (no re-render).
export type ShellInfo = { isFrame: boolean; i: number; isHeaven: boolean }
export type ShellAnalysis = { triShell: Int32Array; shellInfo: ShellInfo[] }
// union-find the STL's triangles into connected shells (frame + free beads), then
// map each bead shell's centroid back to its (column, heaven/earth) cell with the
// same layout the .scad uses. Frame = the one shell far wider than a column pitch.
// `positions` is the flat triangle-soup position array (9 floats per triangle).
export function analyzeShells(positions: ArrayLike<number>, p: Params): ShellAnalysis {
const pos = positions
const nTri = (pos.length / 9) | 0
const Q = 100 // weld verts to a 0.01mm grid
const vid = new Map<string, number>()
const tvi = new Int32Array(nTri * 3)
for (let t = 0; t < nTri; t++) {
for (let c = 0; c < 3; c++) {
const o = (t * 3 + c) * 3
const k = `${Math.round(pos[o] * Q)},${Math.round(pos[o + 1] * Q)},${Math.round(
pos[o + 2] * Q
)}`
let v = vid.get(k)
if (v === undefined) {
v = vid.size
vid.set(k, v)
}
tvi[t * 3 + c] = v
}
}
const parent = new Int32Array(vid.size)
for (let i = 0; i < parent.length; i++) parent[i] = i
const find = (a: number): number => {
let x = a
while (parent[x] !== x) {
parent[x] = parent[parent[x]]
x = parent[x]
}
return x
}
const uni = (a: number, b: number) => {
const ra = find(a)
const rb = find(b)
if (ra !== rb) parent[rb] = ra
}
for (let t = 0; t < nTri; t++) {
uni(tvi[t * 3], tvi[t * 3 + 1])
uni(tvi[t * 3 + 1], tvi[t * 3 + 2])
}
type Box = { xmin: number; xmax: number; ymin: number; ymax: number }
const rootIdx = new Map<number, number>()
const boxes: Box[] = []
const ts = new Int32Array(nTri)
for (let t = 0; t < nTri; t++) {
const r = find(tvi[t * 3])
let si = rootIdx.get(r)
if (si === undefined) {
si = boxes.length
rootIdx.set(r, si)
boxes.push({ xmin: 1e9, xmax: -1e9, ymin: 1e9, ymax: -1e9 })
}
ts[t] = si
const b = boxes[si]
for (let c = 0; c < 3; c++) {
const o = (t * 3 + c) * 3
const x = pos[o]
const y = pos[o + 1]
if (x < b.xmin) b.xmin = x
if (x > b.xmax) b.xmax = x
if (y < b.ymin) b.ymin = y
if (y > b.ymax) b.ymax = y
}
}
// border offset: the bead field sits at (border_w, border_w) inside the outer rect
const d = derived(p)
const s_em = p.border_w * p.scale_factor + d.sEm
const s_cp = d.sCp
const s_hy = p.border_w * p.scale_factor + d.sHy
const s_ep = d.sEp
let frameSi = -1
let frameSpan = 2 * s_cp // a shell must beat >1 column pitch to be the frame
for (let si = 0; si < boxes.length; si++) {
const sp = boxes[si].xmax - boxes[si].xmin
if (sp > frameSpan) {
frameSpan = sp
frameSi = si
}
}
const shellInfo = boxes.map((b, si): ShellInfo => {
if (si === frameSi) return { isFrame: true, i: 0, isHeaven: false }
const cx = (b.xmin + b.xmax) / 2
const cy = (b.ymin + b.ymax) / 2
const i = Math.max(0, Math.min(p.cols - 1, Math.round((cx - s_em) / s_cp)))
return { isFrame: false, i, isHeaven: Math.abs(cy - s_hy) < s_ep * 0.5 }
})
return { triShell: ts, shellInfo }
}
// The filament slot a shell rides under the current scheme/palette + filament
// map. Frame rides its frame slot; each bead rides its role slot. Shared by the
// viewer's recolor pass (via shellHex) and the 3MF per-spool body split.
export const shellSlotIndex = (info: ShellInfo, p: Params, fm: FilamentMap): number =>
info.isFrame
? fm.frame
: fm.beadRoles[beadRoleIndex(info.i, info.isHeaven, p.color_scheme, p.cols, p.color_palette)]
// The intended hex for a shell under the current scheme/palette + filament map —
// the viewer converts this to a linear rgb triple via THREE.Color.
export const shellHex = (info: ShellInfo, p: Params, fm: FilamentMap): string =>
fm.slots[shellSlotIndex(info, p, fm)]
// ---- inset text-plug layout (QA for inlay fill colors) ----------------------
// mirror of the scad rails()/walls() layout: token k of a slot sits at
// A + (B−A)·(k+0.5)/n, on the top face (z≈s_fh) or a wall (z=z_edge).
export type TokenCenter = { x: number; y: number; z: number; k: number }
export function tokenCenters(p: Params): TokenCenter[] {
const S = p.scale_factor
const d = derived(p)
const W = d.frameW
const D = d.outerD
const r = p.corner_r * S
const mkEnd = d.mkI + p.marker_mm + 2
const stripX = p.border_w * S + d.sShelf
const stripY = stripX
const zTop = p.frame_h * S
const zEdge = (p.frame_h * S) / 2
const rails: [ReturnType<typeof slotTokens>, number, number, number, number, number][] = [
[slotTokens(p.top_preset, p.top_text), mkEnd, D - stripY / 2, W - mkEnd, D - stripY / 2, zTop],
[slotTokens(p.bottom_preset, p.bottom_text), mkEnd, stripY / 2, W - mkEnd, stripY / 2, zTop],
[slotTokens(p.left_preset, p.left_text), stripX / 2, mkEnd, stripX / 2, D - mkEnd, zTop],
[
slotTokens(p.right_preset, p.right_text),
W - stripX / 2,
D - mkEnd,
W - stripX / 2,
mkEnd,
zTop,
],
[tokenize(p.edge_front), r + 2, 0, W - r - 2, 0, zEdge],
[tokenize(p.edge_back), W - r - 2, D, r + 2, D, zEdge],
[tokenize(p.edge_left), 0, D - r - 2, 0, r + 2, zEdge],
[tokenize(p.edge_right), W, r + 2, W, D - r - 2, zEdge],
]
const centers: TokenCenter[] = []
for (const [toks, ax, ay, bx, by, z] of rails)
toks.forEach((_, k) => {
const f = (k + 0.5) / toks.length
centers.push({ x: ax + (bx - ax) * f, y: ay + (by - ay) * f, z, k })
})
return centers
}
export const anyTokens = (p: Params): boolean =>
[
slotTokens(p.top_preset, p.top_text),
slotTokens(p.bottom_preset, p.bottom_text),
slotTokens(p.left_preset, p.left_text),
slotTokens(p.right_preset, p.right_text),
tokenize(p.edge_front),
tokenize(p.edge_back),
tokenize(p.edge_left),
tokenize(p.edge_right),
].some((t) => t.length > 0)
|