Resources Numerical accuracy Calculation policy

Control program host: placement position and turn

Lets a control program read and set the position, the turn about the vertical axis and the visibility of top-level placements.

How the precision is kept

Positions are read from and written to the translation entries of the 4×4 matrix unchanged. The turn is read with atan2 and written by rebuilding the first two columns' x and y entries from their lengths and the new angle. Values a program writes are held as 64-bit numbers in an overlay until the run is kept.

Calculations under this policy

2 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.

plc-scene-mc.js ModelCraft 0.8.1 64-bit 2 calculations Reviewed

Tests None found by the review

Reading position and turn

64-bit

The x, y, z and turn a control program reads for a placement.

x, y, z = m[12], m[13], m[14]; rotZ = atan2(m[1], m[0]) × 180 ÷ π

Position entries are returned as stored. The turn is one atan2 of the first column's y and x parts, converted to degrees with the 64-bit π.

  • plc-scene-mc.js
  • lines 23 to 24
  • ModelCraft 0.8.1
const rotZOf = (m) => Math.atan2(m[1], m[0]) * 180 / Math.PI;
const baseValue = (inst, prop) => (prop === 'x' ? inst.m[12] : prop === 'y' ? inst.m[13] : prop === 'z' ? inst.m[14] : prop === 'rotZ' ? rotZOf(inst.m) : prop === 'hidden' ? !!inst.hidden : undefined);

Writing position and turn

64-bit

Applies the values a control program set to the placement's matrix when the run is kept.

sx = √(m₀² + m₁²); sy = √(m₄² + m₅²); a = rotZ × π ÷ 180; m₀ = sx·cos a, m₁ = sx·sin a, m₄ = −sy·sin a, m₅ = sy·cos a

The scales are the plan lengths of the first two columns, with a zero length replaced by 1. Only four entries are rewritten, so a mirror or a shear that was in those columns is not kept. Reading the turn back after writing it goes through cos, sin and atan2, so it can differ from the written number in the last digit. Positions are written to the translation entry unchanged.

  • plc-scene-mc.js
  • lines 55 to 59
  • ModelCraft 0.8.1
else if (prop === 'rotZ') {
  const m = inst.m.slice(), sx = Math.hypot(m[0], m[1]) || 1, sy = Math.hypot(m[4], m[5]) || 1, a = v * Math.PI / 180;
  m[0] = sx * Math.cos(a); m[1] = sx * Math.sin(a); m[4] = -sy * Math.sin(a); m[5] = sy * Math.cos(a);
  inst.m = m;
} else { const m = inst.m.slice(); m[{ x: 12, y: 13, z: 14 }[prop]] = v; inst.m = m; }

In detail

Method
rotZ = atan2(m[1], m[0]) in degrees; writing sets the x and y parts of the first two columns to scale × (cos, sin) and scale × (−sin, cos).
Number types
64-bit numbers; Number(value) on every written value.
Limits
Writing a turn keeps only the plan lengths of the first two columns: a mirror or shear in those columns is replaced by a plain rotation, and a zero length is replaced by 1.

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