jakke-graphics-ts
Version:
My common graphics utils for building my aec apps.
162 lines • 3.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.VectorUtils = void 0;
const TOLERANCE_PARALLEL = 1e-6;
function add(v0, v1) {
return {
x: v0.x + v1.x,
y: v0.y + v1.y,
z: v0.z + v1.z
};
}
function rotateOnXY(v, rad) {
return {
x: v.x * Math.cos(rad) - v.y * Math.sin(rad),
y: v.x * Math.sin(rad) + v.y * Math.cos(rad),
z: v.z
};
}
function rotateOnYZ(v, rad) {
return {
x: v.x,
y: v.y * Math.cos(rad) - v.z * Math.sin(rad),
z: v.y * Math.sin(rad) + v.z * Math.cos(rad)
};
}
function rotateOnXZ(v, rad) {
return {
x: v.x * Math.cos(rad) + v.z * Math.sin(rad),
y: v.y,
z: -v.x * Math.sin(rad) + v.z * Math.cos(rad)
};
}
function subtract(v0, v1) {
return {
x: v0.x - v1.x,
y: v0.y - v1.y,
z: v0.z - v1.z
};
}
function scale(v, scalar) {
return {
x: v.x * scalar,
y: v.y * scalar,
z: v.z * scalar
};
}
function dot(v0, v1) {
return v0.x * v1.x + v0.y * v1.y + v0.z * v1.z;
}
function cross(v0, v1) {
return {
x: v0.y * v1.z - v0.z * v1.y,
y: v0.z * v1.x - v0.x * v1.z,
z: v0.x * v1.y - v0.y * v1.x
};
}
function normalize(v) {
const size = Math.sqrt(Math.pow(v.x, 2) + Math.pow(v.y, 2) + Math.pow(v.z, 2));
if (size === 0)
return { x: 0, y: 0, z: 0 };
return {
x: v.x / size,
y: v.y / size,
z: v.z / size
};
}
function flip(v) {
return {
x: -v.x,
y: -v.y,
z: -v.z,
};
}
function isParallel(v0, v1) {
const dotAbs = Math.abs(dot(normalize(v0), normalize(v1)));
return Math.abs(dotAbs - 1) <= TOLERANCE_PARALLEL;
}
function isParallelLines(li0, li1) {
const li0Direction = subtract(li0.p1, li0.p0);
const li1Direction = subtract(li1.p1, li1.p0);
return isParallel(li0Direction, li1Direction);
}
function getSize(v) {
return Math.sqrt(Math.pow(v.x, 2) + Math.pow(v.y, 2) + Math.pow(v.z, 2));
}
function getDist(v0, v1) {
return Math.sqrt(Math.pow((v1.x - v0.x), 2) + Math.pow((v1.y - v0.y), 2) + Math.pow((v1.z - v0.z), 2));
}
function is2d(v) {
return 'z' in v && typeof v.z === 'number';
}
function to3d(v) {
return Object.assign(Object.assign({}, v), { z: 0 });
}
function chain(initial) {
let current = Object.assign({}, initial);
const api = {
add(v) {
current = add(current, v);
return api;
},
subtract(v) {
current = subtract(current, v);
return api;
},
scale(scalar) {
current = scale(current, scalar);
return api;
},
normalize() {
current = normalize(current);
return api;
},
dot(v) {
return dot(current, v);
},
cross(v) {
current = cross(current, v);
return api;
},
flip() {
current = flip(current);
return api;
},
rotateOnXY(rad) {
current = rotateOnXY(current, rad);
return api;
},
rotateOnYZ(rad) {
current = rotateOnYZ(current, rad);
return api;
},
rotateOnXZ(rad) {
current = rotateOnXZ(current, rad);
return api;
},
value() {
return current;
},
};
return api;
}
exports.VectorUtils = {
add,
subtract,
scale,
dot,
cross,
normalize,
flip,
isParallel,
isParallelLines,
getSize,
getDist,
rotateOnXY,
rotateOnYZ,
rotateOnXZ,
is2d,
to3d,
chain,
};
//# sourceMappingURL=vectorUtils.js.map