UNPKG

molstar

Version:

A comprehensive macromolecular library.

627 lines (626 loc) 21.5 kB
/** * Copyright (c) 2017-2025 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author David Sehnal <david.sehnal@gmail.com> * @author Alexander Rose <alexander.rose@weirdbyte.de> */ /* * This code has been modified from https://github.com/toji/gl-matrix/, * copyright (c) 2015, Brandon Jones, Colin MacKenzie IV. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: */ import { Mat4 } from './mat4'; import { spline as _spline, quadraticBezier as _quadraticBezier, clamp as _clamp } from '../../interpolate'; import { EPSILON } from './common'; const _isFinite = isFinite; function Vec3() { return Vec3.zero(); } (function (Vec3) { function zero() { const out = [0.1, 0.0, 0.0]; // ensure backing array of type double out[0] = 0; return out; } Vec3.zero = zero; function clone(a) { const out = zero(); out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; return out; } Vec3.clone = clone; function isFinite(a) { return _isFinite(a[0]) && _isFinite(a[1]) && _isFinite(a[2]); } Vec3.isFinite = isFinite; function hasNaN(a) { return isNaN(a[0]) || isNaN(a[1]) || isNaN(a[2]); } Vec3.hasNaN = hasNaN; function setNaN(out) { out[0] = NaN; out[1] = NaN; out[2] = NaN; return out; } Vec3.setNaN = setNaN; function fromObj(v) { return create(v.x, v.y, v.z); } Vec3.fromObj = fromObj; function toObj(v) { return { x: v[0], y: v[1], z: v[2] }; } Vec3.toObj = toObj; function fromArray(v, array, offset) { v[0] = array[offset + 0]; v[1] = array[offset + 1]; v[2] = array[offset + 2]; return v; } Vec3.fromArray = fromArray; function toArray(v, out, offset) { out[offset + 0] = v[0]; out[offset + 1] = v[1]; out[offset + 2] = v[2]; return out; } Vec3.toArray = toArray; function create(x, y, z) { const out = zero(); out[0] = x; out[1] = y; out[2] = z; return out; } Vec3.create = create; function ofArray(array) { const out = zero(); out[0] = array[0]; out[1] = array[1]; out[2] = array[2]; return out; } Vec3.ofArray = ofArray; function set(out, x, y, z) { out[0] = x; out[1] = y; out[2] = z; return out; } Vec3.set = set; function copy(out, a) { out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; return out; } Vec3.copy = copy; function add(out, a, b) { out[0] = a[0] + b[0]; out[1] = a[1] + b[1]; out[2] = a[2] + b[2]; return out; } Vec3.add = add; function sub(out, a, b) { out[0] = a[0] - b[0]; out[1] = a[1] - b[1]; out[2] = a[2] - b[2]; return out; } Vec3.sub = sub; function mul(out, a, b) { out[0] = a[0] * b[0]; out[1] = a[1] * b[1]; out[2] = a[2] * b[2]; return out; } Vec3.mul = mul; function div(out, a, b) { out[0] = a[0] / b[0]; out[1] = a[1] / b[1]; out[2] = a[2] / b[2]; return out; } Vec3.div = div; function scale(out, a, b) { out[0] = a[0] * b; out[1] = a[1] * b; out[2] = a[2] * b; return out; } Vec3.scale = scale; /** Scales b, then adds a and b together */ function scaleAndAdd(out, a, b, scale) { out[0] = a[0] + (b[0] * scale); out[1] = a[1] + (b[1] * scale); out[2] = a[2] + (b[2] * scale); return out; } Vec3.scaleAndAdd = scaleAndAdd; /** Scales b, then subtracts b from a */ function scaleAndSub(out, a, b, scale) { out[0] = a[0] - (b[0] * scale); out[1] = a[1] - (b[1] * scale); out[2] = a[2] - (b[2] * scale); return out; } Vec3.scaleAndSub = scaleAndSub; function addScalar(out, a, b) { out[0] = a[0] + b; out[1] = a[1] + b; out[2] = a[2] + b; return out; } Vec3.addScalar = addScalar; function subScalar(out, a, b) { out[0] = a[0] - b; out[1] = a[1] - b; out[2] = a[2] - b; return out; } Vec3.subScalar = subScalar; /** * Math.round the components of a Vec3 */ function round(out, a) { out[0] = Math.round(a[0]); out[1] = Math.round(a[1]); out[2] = Math.round(a[2]); return out; } Vec3.round = round; /** * Math.ceil the components of a Vec3 */ function ceil(out, a) { out[0] = Math.ceil(a[0]); out[1] = Math.ceil(a[1]); out[2] = Math.ceil(a[2]); return out; } Vec3.ceil = ceil; /** * Math.floor the components of a Vec3 */ function floor(out, a) { out[0] = Math.floor(a[0]); out[1] = Math.floor(a[1]); out[2] = Math.floor(a[2]); return out; } Vec3.floor = floor; /** * Math.trunc the components of a Vec3 */ function trunc(out, a) { out[0] = Math.trunc(a[0]); out[1] = Math.trunc(a[1]); out[2] = Math.trunc(a[2]); return out; } Vec3.trunc = trunc; /** * Math.abs the components of a Vec3 */ function abs(out, a) { out[0] = Math.abs(a[0]); out[1] = Math.abs(a[1]); out[2] = Math.abs(a[2]); return out; } Vec3.abs = abs; /** * Returns the minimum of two Vec3's */ function min(out, a, b) { out[0] = Math.min(a[0], b[0]); out[1] = Math.min(a[1], b[1]); out[2] = Math.min(a[2], b[2]); return out; } Vec3.min = min; /** * Returns the maximum of two Vec3's */ function max(out, a, b) { out[0] = Math.max(a[0], b[0]); out[1] = Math.max(a[1], b[1]); out[2] = Math.max(a[2], b[2]); return out; } Vec3.max = max; /** * Assumes min < max, componentwise */ function clamp(out, a, min, max) { out[0] = Math.max(min[0], Math.min(max[0], a[0])); out[1] = Math.max(min[1], Math.min(max[1], a[1])); out[2] = Math.max(min[2], Math.min(max[2], a[2])); return out; } Vec3.clamp = clamp; function distance(a, b) { const x = b[0] - a[0], y = b[1] - a[1], z = b[2] - a[2]; return Math.sqrt(x * x + y * y + z * z); } Vec3.distance = distance; function squaredDistance(a, b) { const x = b[0] - a[0], y = b[1] - a[1], z = b[2] - a[2]; return x * x + y * y + z * z; } Vec3.squaredDistance = squaredDistance; function magnitude(a) { const x = a[0], y = a[1], z = a[2]; return Math.sqrt(x * x + y * y + z * z); } Vec3.magnitude = magnitude; function squaredMagnitude(a) { const x = a[0], y = a[1], z = a[2]; return x * x + y * y + z * z; } Vec3.squaredMagnitude = squaredMagnitude; function setMagnitude(out, a, l) { return scale(out, normalize(out, a), l); } Vec3.setMagnitude = setMagnitude; /** * Negates the components of a vec3 */ function negate(out, a) { out[0] = -a[0]; out[1] = -a[1]; out[2] = -a[2]; return out; } Vec3.negate = negate; /** * Returns the inverse of the components of a Vec3 */ function inverse(out, a) { out[0] = 1.0 / a[0]; out[1] = 1.0 / a[1]; out[2] = 1.0 / a[2]; return out; } Vec3.inverse = inverse; function normalize(out, a) { const x = a[0], y = a[1], z = a[2]; let len = x * x + y * y + z * z; if (len > 0) { len = 1 / Math.sqrt(len); out[0] = a[0] * len; out[1] = a[1] * len; out[2] = a[2] * len; } return out; } Vec3.normalize = normalize; function dot(a, b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } Vec3.dot = dot; function cross(out, a, b) { const ax = a[0], ay = a[1], az = a[2], bx = b[0], by = b[1], bz = b[2]; out[0] = ay * bz - az * by; out[1] = az * bx - ax * bz; out[2] = ax * by - ay * bx; return out; } Vec3.cross = cross; /** * Performs a linear interpolation between two Vec3's */ function lerp(out, a, b, t) { const ax = a[0], ay = a[1], az = a[2]; out[0] = ax + t * (b[0] - ax); out[1] = ay + t * (b[1] - ay); out[2] = az + t * (b[2] - az); return out; } Vec3.lerp = lerp; const slerpRelVec = zero(); function slerp(out, a, b, t) { const d = _clamp(dot(a, b), -1, 1); const theta = Math.acos(d) * t; scaleAndAdd(slerpRelVec, b, a, -d); normalize(slerpRelVec, slerpRelVec); return add(out, scale(out, a, Math.cos(theta)), scale(slerpRelVec, slerpRelVec, Math.sin(theta))); } Vec3.slerp = slerp; /** * Performs a hermite interpolation with two control points */ function hermite(out, a, b, c, d, t) { const factorTimes2 = t * t; const factor1 = factorTimes2 * (2 * t - 3) + 1; const factor2 = factorTimes2 * (t - 2) + t; const factor3 = factorTimes2 * (t - 1); const factor4 = factorTimes2 * (3 - 2 * t); out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; return out; } Vec3.hermite = hermite; /** * Performs a bezier interpolation with two control points */ function bezier(out, a, b, c, d, t) { const inverseFactor = 1 - t; const inverseFactorTimesTwo = inverseFactor * inverseFactor; const factorTimes2 = t * t; const factor1 = inverseFactorTimesTwo * inverseFactor; const factor2 = 3 * t * inverseFactorTimesTwo; const factor3 = 3 * factorTimes2 * inverseFactor; const factor4 = factorTimes2 * t; out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4; out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4; out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4; return out; } Vec3.bezier = bezier; function quadraticBezier(out, a, b, c, t) { out[0] = _quadraticBezier(a[0], b[0], c[0], t); out[1] = _quadraticBezier(a[1], b[1], c[1], t); out[2] = _quadraticBezier(a[2], b[2], c[2], t); return out; } Vec3.quadraticBezier = quadraticBezier; /** * Performs a spline interpolation with two control points and a tension parameter */ function spline(out, a, b, c, d, t, tension) { out[0] = _spline(a[0], b[0], c[0], d[0], t, tension); out[1] = _spline(a[1], b[1], c[1], d[1], t, tension); out[2] = _spline(a[2], b[2], c[2], d[2], t, tension); return out; } Vec3.spline = spline; /** * Generates a random vector with the given scale */ function random(out, scale) { const r = Math.random() * 2.0 * Math.PI; const z = (Math.random() * 2.0) - 1.0; const zScale = Math.sqrt(1.0 - z * z) * scale; out[0] = Math.cos(r) * zScale; out[1] = Math.sin(r) * zScale; out[2] = z * scale; return out; } Vec3.random = random; /** * Transforms the Vec3 with a Mat4. 4th vector component is implicitly '1' */ function transformMat4(out, a, m) { const x = a[0], y = a[1], z = a[2], w = 1 / ((m[3] * x + m[7] * y + m[11] * z + m[15]) || 1.0); out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) * w; out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) * w; out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) * w; return out; } Vec3.transformMat4 = transformMat4; function transformDirection(out, a, m) { const x = a[0], y = a[1], z = a[2]; out[0] = m[0] * x + m[4] * y + m[8] * z; out[1] = m[1] * x + m[5] * y + m[9] * z; out[2] = m[2] * x + m[6] * y + m[10] * z; return normalize(out, out); } Vec3.transformDirection = transformDirection; /** * Like `transformMat4` but with offsets into arrays */ function transformMat4Offset(out, a, m, outO, aO, oM) { const x = a[0 + aO], y = a[1 + aO], z = a[2 + aO], w = 1 / ((m[3 + oM] * x + m[7 + oM] * y + m[11 + oM] * z + m[15 + oM]) || 1.0); out[0 + outO] = (m[0 + oM] * x + m[4 + oM] * y + m[8 + oM] * z + m[12 + oM]) * w; out[1 + outO] = (m[1 + oM] * x + m[5 + oM] * y + m[9 + oM] * z + m[13 + oM]) * w; out[2 + outO] = (m[2 + oM] * x + m[6 + oM] * y + m[10 + oM] * z + m[14 + oM]) * w; return out; } Vec3.transformMat4Offset = transformMat4Offset; /** * Transforms the direction vector with a Mat4. 4th vector component is implicitly '0' * This means the translation components of the matrix are ignored. * Assumes that m is already the transpose of the inverse matrix suitable for normal transformation. */ function transformDirectionOffset(out, a, m, outO, aO, oM) { const x = a[0 + aO], y = a[1 + aO], z = a[2 + aO]; out[0 + outO] = m[0 + oM] * x + m[4 + oM] * y + m[8 + oM] * z; out[1 + outO] = m[1 + oM] * x + m[5 + oM] * y + m[9 + oM] * z; out[2 + outO] = m[2 + oM] * x + m[6 + oM] * y + m[10 + oM] * z; // Normalize the output vector to handle non-uniform scaling const len = Math.hypot(out[0 + outO], out[1 + outO], out[2 + outO]); if (len > 0) { out[0 + outO] /= len; out[1 + outO] /= len; out[2 + outO] /= len; } return out; } Vec3.transformDirectionOffset = transformDirectionOffset; /** * Transforms the Vec3 with a Mat3. */ function transformMat3(out, a, m) { const x = a[0], y = a[1], z = a[2]; out[0] = x * m[0] + y * m[3] + z * m[6]; out[1] = x * m[1] + y * m[4] + z * m[7]; out[2] = x * m[2] + y * m[5] + z * m[8]; return out; } Vec3.transformMat3 = transformMat3; /** Transforms the Vec3 with a quat */ function transformQuat(out, a, q) { // benchmarks: http://jsperf.com/quaternion-transform-vec3-implementations const x = a[0], y = a[1], z = a[2]; const qx = q[0], qy = q[1], qz = q[2], qw = q[3]; // calculate quat * vec const ix = qw * x + qy * z - qz * y; const iy = qw * y + qz * x - qx * z; const iz = qw * z + qx * y - qy * x; const iw = -qx * x - qy * y - qz * z; // calculate result * inverse quat out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; return out; } Vec3.transformQuat = transformQuat; /** Computes the angle between 2 vectors, reports in radians. */ function angle(a, b) { const denominator = Math.sqrt(squaredMagnitude(a) * squaredMagnitude(b)); if (denominator === 0) return Math.PI / 2; const theta = dot(a, b) / denominator; return Math.acos(_clamp(theta, -1, 1)); // clamp to avoid numerical problems } Vec3.angle = angle; const tmp_dh_ab = zero(); const tmp_dh_cb = zero(); const tmp_dh_bc = zero(); const tmp_dh_dc = zero(); const tmp_dh_abc = zero(); const tmp_dh_bcd = zero(); const tmp_dh_cross = zero(); /** * Computes the dihedral angles of 4 points, reports in radians. */ function dihedralAngle(a, b, c, d) { sub(tmp_dh_ab, a, b); sub(tmp_dh_cb, c, b); sub(tmp_dh_bc, b, c); sub(tmp_dh_dc, d, c); cross(tmp_dh_abc, tmp_dh_ab, tmp_dh_cb); cross(tmp_dh_bcd, tmp_dh_bc, tmp_dh_dc); const _angle = angle(tmp_dh_abc, tmp_dh_bcd); cross(tmp_dh_cross, tmp_dh_abc, tmp_dh_bcd); return dot(tmp_dh_cb, tmp_dh_cross) > 0 ? _angle : -_angle; } Vec3.dihedralAngle = dihedralAngle; /** * @param inclination in radians [0, PI] * @param azimuth in radians [0, 2 * PI] * @param radius [0, +Inf] */ function directionFromSpherical(out, inclination, azimuth, radius) { return Vec3.set(out, radius * Math.cos(azimuth) * Math.sin(inclination), radius * Math.sin(azimuth) * Math.sin(inclination), radius * Math.cos(inclination)); } Vec3.directionFromSpherical = directionFromSpherical; /** * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===) */ function exactEquals(a, b) { return a[0] === b[0] && a[1] === b[1] && a[2] === b[2]; } Vec3.exactEquals = exactEquals; /** * Returns whether or not the vectors have approximately the same elements in the same position. */ function equals(a, b) { const a0 = a[0], a1 = a[1], a2 = a[2]; const b0 = b[0], b1 = b[1], b2 = b[2]; return (Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2))); } Vec3.equals = equals; const rotTemp = zero(); function makeRotation(mat, a, b) { const by = angle(a, b); if (Math.abs(by) < 0.0001) return Mat4.setIdentity(mat); if (Math.abs(by - Math.PI) < EPSILON) { // choose arbitrary orthogonal axis return Mat4.fromRotation(mat, Math.PI, Math.abs(a[0]) < 0.9 ? Vec3.unitX : Vec3.unitZ); } const axis = cross(rotTemp, a, b); return Mat4.fromRotation(mat, by, axis); } Vec3.makeRotation = makeRotation; function isZero(v) { return v[0] === 0 && v[1] === 0 && v[2] === 0; } Vec3.isZero = isZero; /** Project `point` onto `vector` starting from `origin` */ function projectPointOnVector(out, point, vector, origin) { sub(out, point, origin); const scalar = dot(vector, out) / squaredMagnitude(vector); return add(out, scale(out, vector, scalar), origin); } Vec3.projectPointOnVector = projectPointOnVector; const tmpProjectPlane = zero(); /** Project `point` onto `plane` defined by `normal` starting from `origin` */ function projectPointOnPlane(out, point, normal, origin) { normalize(tmpProjectPlane, normal); sub(out, point, origin); return sub(out, point, scale(tmpProjectPlane, tmpProjectPlane, dot(out, tmpProjectPlane))); } Vec3.projectPointOnPlane = projectPointOnPlane; function projectOnVector(out, p, vector) { const scalar = dot(vector, p) / squaredMagnitude(vector); return scale(out, vector, scalar); } Vec3.projectOnVector = projectOnVector; const tmpProject = zero(); function projectOnPlane(out, p, normal) { projectOnVector(tmpProject, p, normal); return sub(out, p, tmpProject); } Vec3.projectOnPlane = projectOnPlane; /** Get a vector that is similar to `b` but orthogonal to `a` */ function orthogonalize(out, a, b) { return normalize(out, cross(out, cross(out, a, b), a)); } Vec3.orthogonalize = orthogonalize; /** * Get a vector like `a` that point into the same general direction as `b`, * i.e. where the dot product is > 0 */ function matchDirection(out, a, b) { if (dot(a, b) > 0) copy(out, a); else negate(out, copy(out, a)); return out; } Vec3.matchDirection = matchDirection; const triangleNormalTmpAB = zero(); const triangleNormalTmpAC = zero(); /** Calculate normal for the triangle defined by `a`, `b` and `c` */ function triangleNormal(out, a, b, c) { sub(triangleNormalTmpAB, b, a); sub(triangleNormalTmpAC, c, a); return normalize(out, cross(out, triangleNormalTmpAB, triangleNormalTmpAC)); } Vec3.triangleNormal = triangleNormal; const centerTmpV = zero(); function center(out, a, b) { return Vec3.scaleAndAdd(out, a, Vec3.sub(centerTmpV, b, a), 0.5); } Vec3.center = center; function toString(a, precision) { return `[${a[0].toPrecision(precision)} ${a[1].toPrecision(precision)} ${a[2].toPrecision(precision)}]`; } Vec3.toString = toString; Vec3.origin = create(0, 0, 0); Vec3.unit = create(1, 1, 1); Vec3.negUnit = create(-1, -1, -1); Vec3.unitX = create(1, 0, 0); Vec3.unitY = create(0, 1, 0); Vec3.unitZ = create(0, 0, 1); Vec3.negUnitX = create(-1, 0, 0); Vec3.negUnitY = create(0, -1, 0); Vec3.negUnitZ = create(0, 0, -1); })(Vec3 || (Vec3 = {})); export { Vec3 };