UNPKG

rabbit-ear

Version:
105 lines (101 loc) 4.95 kB
/* Rabbit Ear 0.9.4 alpha 2024-04-20 (c) Kraft, GNU GPLv3 License */ import { subtract, scale as scale$1, add2, resize3, add3, add, scale2 as scale2$1, subtract2, scale3 as scale3$1, subtract3, scaleNonUniform2, scaleNonUniform3, scaleNonUniform, resize2 } from '../math/vector.js'; import { multiplyMatrix3Vector3, makeMatrix3Rotate, makeMatrix3RotateX, makeMatrix3RotateY, makeMatrix3RotateZ } from '../math/matrix3.js'; import { boundingBox } from '../math/polygon.js'; import { getDimensionQuick } from '../fold/spec.js'; const unitize = (graph) => { if (!graph.vertices_coords) { return graph; } const box = boundingBox(graph.vertices_coords); const longest = Math.max(...box.span); const scaleAmount = longest === 0 ? 1 : (1 / longest); const origin = box.min; const vertices_coords = graph.vertices_coords .map(coord => subtract(coord, origin)) .map(coord => scale$1(coord, scaleAmount)); return Object.assign(graph, { vertices_coords }); }; const translate2 = (graph, translation) => { if (!graph.vertices_coords) { return graph; } const vertices_coords = graph.vertices_coords .map(coord => add2(coord, translation)); return Object.assign(graph, { vertices_coords }); }; const translate3 = (graph, translation) => { if (!graph.vertices_coords) { return graph; } const tr3 = resize3(translation); const vertices_coords = graph.vertices_coords .map(resize3) .map(coord => add3(coord, tr3)); return Object.assign(graph, { vertices_coords }); }; const translate = (graph, translation) => { if (!graph.vertices_coords) { return graph; } const vertices_coords = graph.vertices_coords .map(coord => add(coord, translation)); return Object.assign(graph, { vertices_coords }); }; const scaleUniform2 = (graph, scaleAmount = 1, origin = [0, 0]) => { if (!graph.vertices_coords) { return graph; } const vertices_coords = graph.vertices_coords .map(coord => add2(scale2$1(subtract2(coord, origin), scaleAmount), origin)); return Object.assign(graph, { vertices_coords }); }; const scaleUniform3 = (graph, scaleAmount = 1, origin = [0, 0, 0]) => { if (!graph.vertices_coords) { return graph; } const origin3 = resize3(origin); const vertices_coords = graph.vertices_coords .map(resize3) .map(coord => add3(scale3$1(subtract3(coord, origin3), scaleAmount), origin3)); return Object.assign(graph, { vertices_coords }); }; const scaleUniform = (graph, scaleAmount = 1, origin = [0, 0, 0]) => { if (!graph.vertices_coords) { return graph; } const origin3 = resize3(origin); const vertices_coords = graph.vertices_coords .map(coord => add(scale$1(subtract(coord, origin3), scaleAmount), origin3)); return Object.assign(graph, { vertices_coords }); }; const scale2 = (graph, scaleAmounts = [1, 1], origin = [0, 0]) => { const vertices_coords = graph.vertices_coords .map(coord => add2(scaleNonUniform2(subtract2(coord, origin), scaleAmounts), origin)); return Object.assign(graph, { vertices_coords }); }; const scale3 = (graph, scaleAmounts = [1, 1, 1], origin = [0, 0, 0]) => { const sc3 = [scaleAmounts[0] || 1, scaleAmounts[1] || 1, scaleAmounts[2] || 1]; const origin3 = resize3(origin); const vertices_coords = graph.vertices_coords .map(resize3) .map(coord => add3(scaleNonUniform3(subtract3(coord, origin3), sc3), origin3)); return Object.assign(graph, { vertices_coords }); }; const scale = (graph, scaleAmounts = [1, 1, 1], origin = [0, 0, 0]) => { const origin3 = resize3(origin); const vertices_coords = graph.vertices_coords .map(coord => add(scaleNonUniform(subtract(coord, origin3), scaleAmounts), origin3)); return Object.assign(graph, { vertices_coords }); }; const transform = ({ vertices_coords }, matrix) => vertices_coords .map(resize3) .map(v => multiplyMatrix3Vector3(matrix, v)); const rotate = (graph, angle, vector = [0, 0, 1], origin = [0, 0, 0]) => { const matrix = makeMatrix3Rotate(angle, resize3(vector), resize3(origin)); const vertices_coords = transform(graph, matrix); return Object.assign(graph, { vertices_coords }); }; const rotateX = (graph, angle, origin = [0, 0, 0]) => { const matrix = makeMatrix3RotateX(angle, resize3(origin)); const vertices_coords = transform(graph, matrix); return Object.assign(graph, { vertices_coords }); }; const rotateY = (graph, angle, origin = [0, 0, 0]) => { const matrix = makeMatrix3RotateY(angle, resize3(origin)); const vertices_coords = transform(graph, matrix); return Object.assign(graph, { vertices_coords }); }; const rotateZ = (graph, angle, origin = [0, 0, 0]) => { const resizeFn = getDimensionQuick(graph) === 2 ? resize2 : resize3; const matrix = makeMatrix3RotateZ(angle, resize3(origin)); const vertices_coords = transform(graph, matrix).map(coord => resizeFn(coord)); return Object.assign(graph, { vertices_coords }); }; export { rotate, rotateX, rotateY, rotateZ, scale, scale2, scale3, scaleUniform, scaleUniform2, scaleUniform3, transform, translate, translate2, translate3, unitize };