Resources Numerical accuracy Calculation policy

Constraints kept through mirror and trim

Works out again the sense of an angle constraint on mirrored geometry and decides which piece of a trimmed object keeps each constraint.

How the precision is kept

The sense of an angle constraint is the sign of atan2(cross, dot) of the two lines' direction vectors, each multiplied by its stored leg direction of ±1. A trimmed piece holds the original's start or end when its parameter is within 1e-9 of 0 or 1. A dimensional constraint keeps its value through a mirror.

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.

ckeep.js CadCraft 0.8.2 64-bit 2 calculations Reviewed

Tests bin/test-cadcraft-constraints.mjs

Turn sense of an angle constraint

64-bit

Whether the angle between two constrained lines is measured counter-clockwise or clockwise after a mirror or a trim.

sign = +1 if atan2(u × v, u · v) ≥ 0, else −1

u and v are the two lines' end-minus-start vectors times their stored leg directions. The cross and dot products are ordinary 64-bit sums of two products each, and the sign is read with no tolerance.

  • ckeep.js
  • lines 35 to 36
  • CadCraft 0.8.2
const u = [(l1.x2 - l1.x1) * c.dirs[0], (l1.y2 - l1.y1) * c.dirs[0]], v = [(l2.x2 - l2.x1) * c.dirs[1], (l2.y2 - l2.y1) * c.dirs[1]];
out.sign = Math.atan2(u[0] * v[1] - u[1] * v[0], u[0] * v[0] + u[1] * v[1]) >= 0 ? 1 : -1;

Which piece holds the original's ends

64-bit

The piece of a trimmed line or arc that still has the original's start, and the one that still has its end.

start piece: t₀ ≤ 10⁻⁹; end piece: t₁ ≥ 1 − 10⁻⁹

The parameters run 0…1 along the original object (they come from commands.js pieceRange), so the 1e-9 is a fraction of the object's length and not a fixed distance.

  • ckeep.js
  • line 83
  • CadCraft 0.8.2
const withStart = pieces.find((p) => p.t0 <= 1e-9), withEnd = pieces.find((p) => p.t1 >= 1 - 1e-9);

In detail

Method
Signs and sides are taken again from the geometry by gcons.js makeConstraint; the angle's turn sense by atan2 of a cross and a dot product.
Number types
64-bit numbers.
Tolerances
1e-9 on the piece parameter, which runs 0…1 along the original line or arc, so it is a fraction of the object's length.
Limits
The turn sense uses no tolerance: two legs within rounding of parallel can take either sign.

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