Resources Numerical accuracy Calculation policy

Reference picture size and position

Places a copied area of a sheet in the model at its true size.

How the precision is kept

The picture's scale is one 64-bit number, millimeters per pixel, taken from the copied area's width in millimeters divided by its width in pixels. Its size and corners are products and sums of that scale and the stored middle point.

Calculations under this policy

4 calculations in 1 source file. The code is read from the released app when this page loads. It is the calculation people run, not a copy kept for this page.

picture.js DetailCraft 0.8.1 64-bit 4 calculations Reviewed

Tests tools/detailcraft-test/test-areaclip.mjs

Scale of a pasted picture

64-bit

Works out how big one pixel of the copied area is in the model.

mm per pixel = width in mm ÷ width in pixels

One 64-bit division. The width in millimeters comes from sizeMm in areaclip.js, using the sheet's real scale when the clip has one and its paper size otherwise; which one was used is stored as size.

  • picture.js
  • lines 50 to 59
  • DetailCraft 0.8.1
/** A clip (untrusted, as read from localStorage) → the picture to add at `at` (its middle), with its PNG. */
export function pictureFromClip(raw, { at = [0, 0, 0] } = {}) {
  const clip = cleanAreaClip(raw);
  if (!clip) return null;
  const size = clip.real ? 'real' : 'paper';
  const [w] = sizeMm(clip, { use: size });
  const name = clip.label || clip.sheet || clip.source || '';
  const picture = cleanPicture({ name, px: clip.px, mmPerPx: w / clip.px[0], x: at[0], y: at[1], z: at[2], opacity: clip.overlay ? clip.overlay.opacity : 1, size, hash: srcHash(clip.src) });
  return picture ? { picture, src: clip.src, size, clip } : null;
}

Picture size in millimeters

64-bit

The width and height of the picture in the model.

w = pixelsₓ × mm per pixel; h = pixels_y × mm per pixel

Two 64-bit products with whole-number pixel counts.

  • picture.js
  • line 76
  • DetailCraft 0.8.1
export const pictureSizeMm = (p) => [p.px[0] * p.mmPerPx, p.px[1] * p.mmPerPx];

Picture corners

64-bit

The four corners of the picture's outline.

corner = middle ± size ÷ 2

Halving is exact in binary; each corner coordinate is one addition or subtraction from the stored middle point.

  • picture.js
  • lines 77 to 80
  • DetailCraft 0.8.1
export function pictureCorners(p) {
  const [w, h] = pictureSizeMm(p);
  return [[p.x - w / 2, p.y - h / 2, p.z], [p.x + w / 2, p.y - h / 2, p.z], [p.x + w / 2, p.y + h / 2, p.z], [p.x - w / 2, p.y + h / 2, p.z]];
}

Picture values read from a file

64-bit

Checks a picture's numbers when a model is opened.

Numbers are converted with Number() and kept as they are when inside the limits; a picture outside them is dropped. Opacity is clamped to 0.05…1. Nothing is rounded.

  • picture.js
  • lines 38 to 48
  • DetailCraft 0.8.1
export function cleanPicture(raw) {
  if (!raw || typeof raw !== 'object') return null;
  const px = Array.isArray(raw.px) ? raw.px.map(Number) : [];
  const k = Number(raw.mmPerPx), x = Number(raw.x), y = Number(raw.y), z = Number(raw.z), o = Number(raw.opacity);
  if (px.length !== 2 || px.some((v) => !Number.isInteger(v) || v < 1 || v > 8000)) return null;
  if (!(k > 0 && k <= 1e5) || ![x, y, z].every((v) => fin(v) && Math.abs(v) <= 1e7)) return null;
  return {
    name: line(raw.name, 120), hash: HASH.test(raw.hash) ? raw.hash : '', px, mmPerPx: k, x, y, z,
    opacity: fin(o) ? Math.max(OPACITY_MIN, Math.min(1, o)) : 1, size: raw.size === 'paper' ? 'paper' : 'real',
  };
}

In detail

Method
mm per pixel = width in mm ÷ width in pixels; size = pixels × mm per pixel; corners = middle ± half the size.
Number types
64-bit numbers; whole-number pixel counts.
Limits
The scale comes from the width only, and the height uses the same millimeters per pixel. Values read from a file must be within limits: scale up to 1e5, position within ±1e7 mm, 1 to 8000 pixels a side.

Approach: 64-bit, direct

Every value is a 64-bit number from input to result. Formulas are closed form or a direct solve, and every comparison uses a stated tolerance.

Every policy that takes this approach