24 lines
966 B
TypeScript
24 lines
966 B
TypeScript
export const ROTATION_AXES = ["x", "y", "z", "-x", "-y", "-z"] as const;
|
|
export type RotationAxis = (typeof ROTATION_AXES)[number];
|
|
|
|
/** Axes are expressed in the mounting interface's local coordinate frame. */
|
|
export function parseRotationAxis(value: unknown): RotationAxis | null {
|
|
if (value == null || value === "") return null;
|
|
if (typeof value !== "string")
|
|
throw new Error("旋转轴须为 x、y、z、-x、-y 或 -z");
|
|
const axis = value.trim().toLowerCase().replace(/^\+/, "");
|
|
if (!axis) return null;
|
|
if (!ROTATION_AXES.includes(axis as RotationAxis))
|
|
throw new Error("旋转轴须为 x、y、z、-x、-y 或 -z");
|
|
return axis as RotationAxis;
|
|
}
|
|
|
|
export function rotationAxisVector(
|
|
axis: RotationAxis,
|
|
): [number, number, number] {
|
|
const index = { x: 0, y: 1, z: 2 }[axis.replace("-", "") as "x" | "y" | "z"];
|
|
const vector: [number, number, number] = [0, 0, 0];
|
|
vector[index] = axis.startsWith("-") ? -1 : 1;
|
|
return vector;
|
|
}
|