Skip to main content

Utilities

Math for the coordinate systems of ClassCAD, and timing.

Objects​

MathUtils​

const MathUtils: {
calcluateTransform: (originPos, xDir, yDir, offset, rotation, inverted) => Matrix4;
convertToCoordinateSystem: (matrix) => number[][];
convertToMatrix3: (coordinateSystem) => Matrix3;
convertToMatrix4: (coordinateSystem) => Matrix4;
round(value, digits): number;
};

Math for the coordinate systems of ClassCAD: to three.js matrices and back, and rounding.

ClassCAD gives a coordinate system (coordinateSystem of an object of the structure tree) as four rows: the origin, the x axis, the y axis and the z axis.

Type Declaration​

NameTypeDescription
calcluateTransform()(originPos, xDir, yDir, offset, rotation, inverted) => Matrix4Calculates a transformation matrix: a coordinate system from an origin and two directions, then moved and turned in it. xDir and yDir are NOT orthogonal in general case, thus the y-axis is calculated as a series of cross products. When yDir is parallel to xDir, the y (or x) axis of the world is taken instead.
convertToCoordinateSystem()(matrix) => number[][]Makes a coordinate system as ClassCAD takes it from a three.js matrix: origin, x axis, y axis and z axis.
convertToMatrix3()(coordinateSystem) => Matrix3Makes the three.js rotation matrix of a coordinate system: its axes as the columns; the origin is left out.
convertToMatrix4()(coordinateSystem) => Matrix4Makes the three.js matrix of a coordinate system: its axes as the basis, its origin as the position.
round()(value, digits) => numberRounds a number to a number of digits after the decimal point; NaN stays NaN.

Example​

import { getDrawing, MathUtils } from '@buerli.io/core'

// Place a three.js object where a work coordinate system of the tree is, in its product
const { coordinateSystem } = getDrawing(drawingId).structure.tree[workCSysId]
if (coordinateSystem) {
helper.matrix.copy(MathUtils.convertToMatrix4(coordinateSystem))
helper.matrixAutoUpdate = false
}

Timing​

const Timing: {
dump: (key, message, mode) => void;
measure: (key, mode) => number;
start: (key) => string;
stop: (key) => number;
};

Measures how long things take, by name, with performance.now().

Type Declaration​

NameTypeDescription
dump()(key, message, mode) => voidLogs what Timing.measure returns, as <message> <ms>ms (the name stands in for a missing message).
measure()(key, mode) => numberReturns the milliseconds since a measurement started, or with 'fromPrevious' since it was last measured; NaN for a name that was not started.
start()(key) => stringStarts a time measurement.
stop()(key) => numberEnds a measurement, and returns the milliseconds since it started; NaN for a name that was not started.

Example​

import { createApi } from '@buerli.io/classcad'
import { Timing } from '@buerli.io/core'

const api = createApi(drawingId)
const key = Timing.start()
await api.v1.common.recalc()
Timing.dump(key, 'first recalc') // first recalc 12.34ms
await api.v1.common.recalc()
Timing.dump(key, 'second recalc', 'fromPrevious')
console.log(Timing.stop(key)) // the milliseconds since start