@things-factory/figure-viewer 10.1.203 → 10.1.205

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import { compileFigureAsset, compileFigureGraph, figureOccupiedBoxesOf, figureWritersOf, FIGURE_INSTANCE_SIZE_INPUTS } from '@hatiolab/figure-model'
1
+ import { compileFigureGraph, figurePartsExtentOf, FIGURE_INSTANCE_SIZE_INPUTS } from '@hatiolab/figure-model'
2
2
  import type { FigureAsset } from '@hatiolab/figure-model'
3
3
 
4
4
  /* The preview's arithmetic, apart from the element so it is tested without a browser (see figure-preview.ts). */
@@ -26,11 +26,9 @@ export function authoredSizeOf(asset: FigureAsset): { x: number; y: number; z: n
26
26
  const d = asset.designInputs
27
27
  const declared = (['x', 'y', 'z'] as const).map(axis => d[FIGURE_INSTANCE_SIZE_INPUTS[axis]])
28
28
  if (declared.every(v => typeof v === 'number' && v > 0)) return { x: declared[0]!, y: declared[1]!, z: declared[2]! }
29
- /* No declared size: the room its bodies take up, so the instance is placed at the size it draws. */
30
- const boxes = figureOccupiedBoxesOf(compileFigureAsset(asset).evaluate().geometry, figureWritersOf(asset))
31
- const span = (axis: 'x' | 'y' | 'z') =>
32
- boxes.length ? Math.max(...boxes.map(b => b.centre[axis] + b.extent[axis] / 2)) - Math.min(...boxes.map(b => b.centre[axis] - b.extent[axis] / 2)) : 0
33
- return { x: Math.max(1, Math.round(span('x'))), y: Math.max(1, Math.round(span('y'))), z: Math.max(1, Math.round(span('z'))) }
29
+ /* No declared size: the size its parts are, as the board takes it for a figure that declares no space yet. */
30
+ const parts = figurePartsExtentOf(asset)
31
+ return { x: Math.max(1, Math.round(parts.x)), y: Math.max(1, Math.round(parts.y)), z: Math.max(1, Math.round(parts.z)) }
34
32
  }
35
33
 
36
34
  type Size = { x: number; y: number; z: number }
@@ -1,4 +1,4 @@
1
- import { compileFigureAsset, compileFigureGraph, figureOccupiedBoxesOf, figureWritersOf, FIGURE_INSTANCE_SIZE_INPUTS } from '@hatiolab/figure-model';
1
+ import { compileFigureGraph, figurePartsExtentOf, FIGURE_INSTANCE_SIZE_INPUTS } from '@hatiolab/figure-model';
2
2
  /* The preview's arithmetic, apart from the element so it is tested without a browser (see figure-preview.ts). */
3
3
  export const PREVIEW_FACTOR = 2;
4
4
  /** The type the preview registers the figure under: this figure's own, never a real type a board uses. */
@@ -21,10 +21,9 @@ export function authoredSizeOf(asset) {
21
21
  const declared = ['x', 'y', 'z'].map(axis => d[FIGURE_INSTANCE_SIZE_INPUTS[axis]]);
22
22
  if (declared.every(v => typeof v === 'number' && v > 0))
23
23
  return { x: declared[0], y: declared[1], z: declared[2] };
24
- /* No declared size: the room its bodies take up, so the instance is placed at the size it draws. */
25
- const boxes = figureOccupiedBoxesOf(compileFigureAsset(asset).evaluate().geometry, figureWritersOf(asset));
26
- const span = (axis) => boxes.length ? Math.max(...boxes.map(b => b.centre[axis] + b.extent[axis] / 2)) - Math.min(...boxes.map(b => b.centre[axis] - b.extent[axis] / 2)) : 0;
27
- return { x: Math.max(1, Math.round(span('x'))), y: Math.max(1, Math.round(span('y'))), z: Math.max(1, Math.round(span('z'))) };
24
+ /* No declared size: the size its parts are, as the board takes it for a figure that declares no space yet. */
25
+ const parts = figurePartsExtentOf(asset);
26
+ return { x: Math.max(1, Math.round(parts.x)), y: Math.max(1, Math.round(parts.y)), z: Math.max(1, Math.round(parts.z)) };
28
27
  }
29
28
  /**
30
29
  * The box a board instance stands in at these size inputs: the figure's declared space evaluated there, as things-scene
@@ -1 +1 @@
1
- {"version":3,"file":"preview-model.js","sourceRoot":"","sources":["../client/preview-model.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,qBAAqB,EAAE,eAAe,EAAE,2BAA2B,EAAE,MAAM,wBAAwB,CAAA;AAGpJ,iHAAiH;AAEjH,MAAM,CAAC,MAAM,cAAc,GAAG,CAAC,CAAA;AAE/B,0GAA0G;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AAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;QACtD,CAAC,CAAC;KACH,CAAC,CACH,CAAA;AACH,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,WAAW,CAAC,MAAc,EAAE,MAAc,EAAE,OAAiB;IAC3E,OAAO,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,IAAI,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,IAAI,OAAO,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;AACtF,CAAC","sourcesContent":["import { compileFigureAsset, compileFigureGraph, figureOccupiedBoxesOf, figureWritersOf, FIGURE_INSTANCE_SIZE_INPUTS } from '@hatiolab/figure-model'\nimport type { FigureAsset } from '@hatiolab/figure-model'\n\n/* The preview's arithmetic, apart from the element so it is tested without a browser (see figure-preview.ts). */\n\nexport const PREVIEW_FACTOR = 2\n\n/** The type the preview registers the figure under: this figure's own, never a real type a board uses. */\nexport function previewTypeOf(context: string): string {\n return `figure-preview-${context || 'new'}`\n}\n\nexport const PREVIEW_WAYS: readonly { name: string; axes: readonly ('x' | 'y' | 'z')[] }[] = [\n { name: 'base', axes: [] },\n { name: 'x', axes: ['x'] },\n { name: 'y', axes: ['y'] },\n { name: 'z', axes: ['z'] },\n { name: 'x·y', axes: ['x', 'y'] },\n { name: 'x·z', axes: ['x', 'z'] },\n { name: 'y·z', axes: ['y', 'z'] },\n { name: 'x·y·z', axes: ['x', 'y', 'z'] }\n]\n\n/** The size the figure was made at: its instance size when it declares one, else the room its bodies take. */\nexport function authoredSizeOf(asset: FigureAsset): { x: number; y: number; z: number } {\n const d = asset.designInputs\n const declared = (['x', 'y', 'z'] as const).map(axis => d[FIGURE_INSTANCE_SIZE_INPUTS[axis]])\n if (declared.every(v => typeof v === 'number' && v > 0)) return { x: declared[0]!, y: declared[1]!, z: declared[2]! }\n /* No declared size: the room its bodies take up, so the instance is placed at the size it draws. */\n const boxes = figureOccupiedBoxesOf(compileFigureAsset(asset).evaluate().geometry, figureWritersOf(asset))\n const span = (axis: 'x' | 'y' | 'z') =>\n boxes.length ? Math.max(...boxes.map(b => b.centre[axis] + b.extent[axis] / 2)) - Math.min(...boxes.map(b => b.centre[axis] - b.extent[axis] / 2)) : 0\n return { x: Math.max(1, Math.round(span('x'))), y: Math.max(1, Math.round(span('y'))), z: Math.max(1, Math.round(span('z'))) }\n}\n\ntype Size = { x: number; y: number; z: number }\n\n/**\n * The box a board instance stands in at these size inputs: the figure's declared space evaluated there, as things-scene\n * reads an instance's width, height and depth -- the space, never the inputs (ADR-0087, 2026-10-06). The two differ by\n * whatever the parts reach past the size: a roller row's spin swept 3660 × 1160 × 660 mm round a 3000 × 800 × 600 body,\n * and a preview that asked for the inputs drew the figure at the wrong size (first 15 minutes, re-measured 2026-10-16).\n * Undefined when the figure declares no space.\n */\nexport function occupancyAtSize(asset: FigureAsset, size: Size): Size | undefined {\n const bounds = asset.occupancy?.bounds as Record<'x' | 'y' | 'z', { min: string; max: string }> | undefined\n if (!bounds) return undefined\n /* A figure with no size inputs is scaled whole by the board from the box asked: the size is its box already. */\n const inputs = asset.document.model.inputs\n if (!(['x', 'y', 'z'] as const).every(axis => inputs.some(i => i.id === FIGURE_INSTANCE_SIZE_INPUTS[axis]))) return undefined\n let graph = graphs.get(asset)\n if (!graph) graphs.set(asset, (graph = compileFigureGraph(asset.document.model as never)))\n const given: Record<string, number> = { ...asset.designInputs, ...asset.stateDefaults }\n for (const axis of ['x', 'y', 'z'] as const) given[FIGURE_INSTANCE_SIZE_INPUTS[axis]] = size[axis]\n let values: Record<string, number>\n try {\n values = graph.evaluate(given).values as Record<string, number>\n } catch {\n return undefined\n }\n const extent = (axis: 'x' | 'y' | 'z') => Math.max((values[bounds[axis].max] ?? 0) - (values[bounds[axis].min] ?? 0), 1)\n return { x: extent('x'), y: extent('y'), z: extent('z') }\n}\nconst graphs = new WeakMap<object, ReturnType<typeof compileFigureGraph>>()\n\n/**\n * The eight instances: where each stands on the board and how large, in the scene's words. `boxOf` gives the box an\n * instance asked at a size stands in (`occupancyAtSize`); without it, the size itself.\n */\nexport function previewInstances(type: string, size: Size, boxOf: (asked: Size) => Size = asked => asked) {\n const COLUMNS = 4\n const largest = boxOf({ x: size.x * PREVIEW_FACTOR, y: size.y * PREVIEW_FACTOR, z: size.z * PREVIEW_FACTOR })\n const cell = { x: largest.x * 1.25, z: largest.z * 1.25 }\n const components = PREVIEW_WAYS.map((way, at) => {\n const grow = (axis: 'x' | 'y' | 'z') => (way.axes.includes(axis) ? PREVIEW_FACTOR : 1)\n const box = boxOf({ x: size.x * grow('x'), y: size.y * grow('y'), z: size.z * grow('z') })\n const width = Math.round(box.x)\n const height = Math.round(box.z)\n const column = at % COLUMNS\n const row = Math.floor(at / COLUMNS)\n return {\n type,\n id: `at-${way.name}`,\n left: Math.round(column * cell.x + (cell.x - width) / 2),\n top: Math.round(row * cell.z + (cell.z - height) / 2),\n width,\n height,\n /* Always written: without it the height is taken as the plan's mean and \"grow y only\" would not grow it. */\n depth: Math.round(box.y)\n }\n })\n return { components, board: { width: Math.round(cell.x * COLUMNS), height: Math.round(cell.z * Math.ceil(PREVIEW_WAYS.length / COLUMNS)) } }\n}\n\n\n/**\n * One instance in the box given -- the space it takes at the authored size (`occupancyAtSize`) -- on a board twice its\n * plan so it can grow in place: the catalogue's detail view and the modeller's preview both stand the figure this way\n * (first 15 minutes, 2026-10-16: eight variants at one scale made a 60 mm roller on a 3 m conveyor a speck nobody could\n * count). The instance's corner is placed so its centre is the board's.\n */\nexport function previewOneInstance(type: string, size: { x: number; y: number; z: number }) {\n const board = { width: Math.round(size.x * 2), height: Math.round(size.z * 2) }\n return {\n board,\n components: [{ type, id: 'one', left: Math.round(size.x / 2), top: Math.round(size.z / 2), width: size.x, height: size.z, depth: size.y }]\n }\n}\n\n/**\n * The range a board may size the figure over on an axis, or undefined when the figure does not let that axis change\n * (no declared size, or one fixed at a single value).\n */\nexport function sizeRangeOf(asset: FigureAsset, axis: 'x' | 'y' | 'z'): { min: number; max: number } | undefined {\n const input = asset.document.model.inputs.find(i => i.id === FIGURE_INSTANCE_SIZE_INPUTS[axis])\n return input && Number.isFinite(input.min) && Number.isFinite(input.max) && input.max > input.min ? { min: input.min, max: input.max } : undefined\n}\n\n/** A size asked on an axis, held to what the figure declares -- as the board clamps it. NaN and unopened axes give undefined. */\nexport function sizeWithinRange(asset: FigureAsset, axis: 'x' | 'y' | 'z', value: number): number | undefined {\n const range = sizeRangeOf(asset, axis)\n if (!range || !Number.isFinite(value)) return undefined\n return Math.min(range.max, Math.max(range.min, value))\n}\n\n/**\n * Where a variant asks a size the figure does not declare, the size it is drawn at instead -- as the board draws it\n * (things-scene clamps each axis to the declared range; chief architect's ruling 2026-09-25). Per variant, empty when\n * it is drawn as asked. A figure that follows its height only is drawn at its one width in the wider variants, and\n * the preview says so rather than drawing them as though they grew.\n */\nexport function previewClampsOf(asset: FigureAsset, size: { x: number; y: number; z: number }): Record<string, Array<{ axis: 'x' | 'y' | 'z'; asked: number; used: number }>> {\n const inputs = asset.document.model.inputs\n return Object.fromEntries(\n PREVIEW_WAYS.map(way => [\n way.name,\n (['x', 'y', 'z'] as const).flatMap(axis => {\n const input = inputs.find(i => i.id === FIGURE_INSTANCE_SIZE_INPUTS[axis])\n if (!input) return []\n const asked = Math.round(size[axis] * (way.axes.includes(axis) ? PREVIEW_FACTOR : 1))\n const used = Math.round(Math.min(Math.max(asked, input.min ?? -Infinity), input.max ?? Infinity))\n return used === asked ? [] : [{ axis, asked, used }]\n })\n ])\n )\n}\n\n/**\n * The part the preview's size help speaks of: the one picked in the help, else the one chosen in the editor, else the\n * first that has a size rule. A choice that is not among the parts (removed, or a part with no rule) is passed over.\n */\nexport function setupPartOf(picked: string, chosen: string, targets: string[]): string {\n return [picked, chosen].find(id => !!id && targets.includes(id)) ?? targets[0] ?? ''\n}\n"]}
1
+ {"version":3,"file":"preview-model.js","sourceRoot":"","s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Graph, figurePartsExtentOf, FIGURE_INSTANCE_SIZE_INPUTS } from '@hatiolab/figure-model'\nimport type { FigureAsset } from '@hatiolab/figure-model'\n\n/* The preview's arithmetic, apart from the element so it is tested without a browser (see figure-preview.ts). */\n\nexport const PREVIEW_FACTOR = 2\n\n/** The type the preview registers the figure under: this figure's own, never a real type a board uses. */\nexport function previewTypeOf(context: string): string {\n return `figure-preview-${context || 'new'}`\n}\n\nexport const PREVIEW_WAYS: readonly { name: string; axes: readonly ('x' | 'y' | 'z')[] }[] = [\n { name: 'base', axes: [] },\n { name: 'x', axes: ['x'] },\n { name: 'y', axes: ['y'] },\n { name: 'z', axes: ['z'] },\n { name: 'x·y', axes: ['x', 'y'] },\n { name: 'x·z', axes: ['x', 'z'] },\n { name: 'y·z', axes: ['y', 'z'] },\n { name: 'x·y·z', axes: ['x', 'y', 'z'] }\n]\n\n/** The size the figure was made at: its instance size when it declares one, else the room its bodies take. */\nexport function authoredSizeOf(asset: FigureAsset): { x: number; y: number; z: number } {\n const d = asset.designInputs\n const declared = (['x', 'y', 'z'] as const).map(axis => d[FIGURE_INSTANCE_SIZE_INPUTS[axis]])\n if (declared.every(v => typeof v === 'number' && v > 0)) return { x: declared[0]!, y: declared[1]!, z: declared[2]! }\n /* No declared size: the size its parts are, as the board takes it for a figure that declares no space yet. */\n const parts = figurePartsExtentOf(asset)\n return { x: Math.max(1, Math.round(parts.x)), y: Math.max(1, Math.round(parts.y)), z: Math.max(1, Math.round(parts.z)) }\n}\n\ntype Size = { x: number; y: number; z: number }\n\n/**\n * The box a board instance stands in at these size inputs: the figure's declared space evaluated there, as things-scene\n * reads an instance's width, height and depth -- the space, never the inputs (ADR-0087, 2026-10-06). The two differ by\n * whatever the parts reach past the size: a roller row's spin swept 3660 × 1160 × 660 mm round a 3000 × 800 × 600 body,\n * and a preview that asked for the inputs drew the figure at the wrong size (first 15 minutes, re-measured 2026-10-16).\n * Undefined when the figure declares no space.\n */\nexport function occupancyAtSize(asset: FigureAsset, size: Size): Size | undefined {\n const bounds = asset.occupancy?.bounds as Record<'x' | 'y' | 'z', { min: string; max: string }> | undefined\n if (!bounds) return undefined\n /* A figure with no size inputs is scaled whole by the board from the box asked: the size is its box already. */\n const inputs = asset.document.model.inputs\n if (!(['x', 'y', 'z'] as const).every(axis => inputs.some(i => i.id === FIGURE_INSTANCE_SIZE_INPUTS[axis]))) return undefined\n let graph = graphs.get(asset)\n if (!graph) graphs.set(asset, (graph = compileFigureGraph(asset.document.model as never)))\n const given: Record<string, number> = { ...asset.designInputs, ...asset.stateDefaults }\n for (const axis of ['x', 'y', 'z'] as const) given[FIGURE_INSTANCE_SIZE_INPUTS[axis]] = size[axis]\n let values: Record<string, number>\n try {\n values = graph.evaluate(given).values as Record<string, number>\n } catch {\n return undefined\n }\n const extent = (axis: 'x' | 'y' | 'z') => Math.max((values[bounds[axis].max] ?? 0) - (values[bounds[axis].min] ?? 0), 1)\n return { x: extent('x'), y: extent('y'), z: extent('z') }\n}\nconst graphs = new WeakMap<object, ReturnType<typeof compileFigureGraph>>()\n\n/**\n * The eight instances: where each stands on the board and how large, in the scene's words. `boxOf` gives the box an\n * instance asked at a size stands in (`occupancyAtSize`); without it, the size itself.\n */\nexport function previewInstances(type: string, size: Size, boxOf: (asked: Size) => Size = asked => asked) {\n const COLUMNS = 4\n const largest = boxOf({ x: size.x * PREVIEW_FACTOR, y: size.y * PREVIEW_FACTOR, z: size.z * PREVIEW_FACTOR })\n const cell = { x: largest.x * 1.25, z: largest.z * 1.25 }\n const components = PREVIEW_WAYS.map((way, at) => {\n const grow = (axis: 'x' | 'y' | 'z') => (way.axes.includes(axis) ? PREVIEW_FACTOR : 1)\n const box = boxOf({ x: size.x * grow('x'), y: size.y * grow('y'), z: size.z * grow('z') })\n const width = Math.round(box.x)\n const height = Math.round(box.z)\n const column = at % COLUMNS\n const row = Math.floor(at / COLUMNS)\n return {\n type,\n id: `at-${way.name}`,\n left: Math.round(column * cell.x + (cell.x - width) / 2),\n top: Math.round(row * cell.z + (cell.z - height) / 2),\n width,\n height,\n /* Always written: without it the height is taken as the plan's mean and \"grow y only\" would not grow it. */\n depth: Math.round(box.y)\n }\n })\n return { components, board: { width: Math.round(cell.x * COLUMNS), height: Math.round(cell.z * Math.ceil(PREVIEW_WAYS.length / COLUMNS)) } }\n}\n\n\n/**\n * One instance in the box given -- the space it takes at the authored size (`occupancyAtSize`) -- on a board twice its\n * plan so it can grow in place: the catalogue's detail view and the modeller's preview both stand the figure this way\n * (first 15 minutes, 2026-10-16: eight variants at one scale made a 60 mm roller on a 3 m conveyor a speck nobody could\n * count). The instance's corner is placed so its centre is the board's.\n */\nexport function previewOneInstance(type: string, size: { x: number; y: number; z: number }) {\n const board = { width: Math.round(size.x * 2), height: Math.round(size.z * 2) }\n return {\n board,\n components: [{ type, id: 'one', left: Math.round(size.x / 2), top: Math.round(size.z / 2), width: size.x, height: size.z, depth: size.y }]\n }\n}\n\n/**\n * The range a board may size the figure over on an axis, or undefined when the figure does not let that axis change\n * (no declared size, or one fixed at a single value).\n */\nexport function sizeRangeOf(asset: FigureAsset, axis: 'x' | 'y' | 'z'): { min: number; max: number } | undefined {\n const input = asset.document.model.inputs.find(i => i.id === FIGURE_INSTANCE_SIZE_INPUTS[axis])\n return input && Number.isFinite(input.min) && Number.isFinite(input.max) && input.max > input.min ? { min: input.min, max: input.max } : undefined\n}\n\n/** A size asked on an axis, held to what the figure declares -- as the board clamps it. NaN and unopened axes give undefined. */\nexport function sizeWithinRange(asset: FigureAsset, axis: 'x' | 'y' | 'z', value: number): number | undefined {\n const range = sizeRangeOf(asset, axis)\n if (!range || !Number.isFinite(value)) return undefined\n return Math.min(range.max, Math.max(range.min, value))\n}\n\n/**\n * Where a variant asks a size the figure does not declare, the size it is drawn at instead -- as the board draws it\n * (things-scene clamps each axis to the declared range; chief architect's ruling 2026-09-25). Per variant, empty when\n * it is drawn as asked. A figure that follows its height only is drawn at its one width in the wider variants, and\n * the preview says so rather than drawing them as though they grew.\n */\nexport function previewClampsOf(asset: FigureAsset, size: { x: number; y: number; z: number }): Record<string, Array<{ axis: 'x' | 'y' | 'z'; asked: number; used: number }>> {\n const inputs = asset.document.model.inputs\n return Object.fromEntries(\n PREVIEW_WAYS.map(way => [\n way.name,\n (['x', 'y', 'z'] as const).flatMap(axis => {\n const input = inputs.find(i => i.id === FIGURE_INSTANCE_SIZE_INPUTS[axis])\n if (!input) return []\n const asked = Math.round(size[axis] * (way.axes.includes(axis) ? PREVIEW_FACTOR : 1))\n const used = Math.round(Math.min(Math.max(asked, input.min ?? -Infinity), input.max ?? Infinity))\n return used === asked ? [] : [{ axis, asked, used }]\n })\n ])\n )\n}\n\n/**\n * The part the preview's size help speaks of: the one picked in the help, else the one chosen in the editor, else the\n * first that has a size rule. A choice that is not among the parts (removed, or a part with no rule) is passed over.\n */\nexport function setupPartOf(picked: string, chosen: string, targets: string[]): string {\n return [picked, chosen].find(id => !!id && targets.includes(id)) ?? targets[0] ?? ''\n}\n"]}