diff --git a/src/mol-math/geometry/primitives/box3d.ts b/src/mol-math/geometry/primitives/box3d.ts index 83c352cb5c64a908ef2b98997b9e28946e73c9ca..e333a74e56db76a6b3afee3d34dbf44f2202293e 100644 --- a/src/mol-math/geometry/primitives/box3d.ts +++ b/src/mol-math/geometry/primitives/box3d.ts @@ -11,9 +11,13 @@ import { OrderedSet } from 'mol-data/int'; interface Box3D { min: Vec3, max: Vec3 } +function Box3D() { + return Box3D.empty(); +} + namespace Box3D { export function create(min: Vec3, max: Vec3): Box3D { return { min, max }; } - export function empty(): Box3D { return { min: Vec3.zero(), max: Vec3.zero() }; } + export function empty(): Box3D { return { min: Vec3(), max: Vec3() }; } export function clone(a: Box3D): Box3D { const out = empty(); @@ -44,7 +48,7 @@ namespace Box3D { return Vec3.sub(size, box.max, box.min); } - const tmpSizeV = Vec3.zero() + const tmpSizeV = Vec3() /** Get size of the box */ export function volume(box: Box3D): number { size(tmpSizeV, box) @@ -71,7 +75,7 @@ namespace Box3D { return out } - const tmpTransformV = Vec3.zero() + const tmpTransformV = Vec3() /** Transform box with a Mat4 */ export function transform(out: Box3D, box: Box3D, m: Mat4): Box3D { const [ minX, minY, minZ ] = box.min diff --git a/src/mol-math/geometry/primitives/sphere3d.ts b/src/mol-math/geometry/primitives/sphere3d.ts index 3bc41681d9375a53cf3725a212a9b29d8393fe33..9df2560a973962dc4aa8d74c8ec994b041102260 100644 --- a/src/mol-math/geometry/primitives/sphere3d.ts +++ b/src/mol-math/geometry/primitives/sphere3d.ts @@ -13,6 +13,10 @@ import { Box3D } from './box3d'; interface Sphere3D { center: Vec3, radius: number } +function Sphere3D() { + return Sphere3D.zero(); +} + namespace Sphere3D { export function create(center: Vec3, radius: number): Sphere3D { return { center, radius }; } export function zero(): Sphere3D { return { center: Vec3.zero(), radius: 0 }; } @@ -83,7 +87,7 @@ namespace Sphere3D { return out } - const tmpAddVec3 = Vec3.zero() + const tmpAddVec3 = Vec3() export function addVec3(out: Sphere3D, s: Sphere3D, v: Vec3) { const d = Vec3.distance(s.center, v) if (d < s.radius) return Sphere3D.copy(out, s) diff --git a/src/mol-math/linear-algebra/3d/mat3.ts b/src/mol-math/linear-algebra/3d/mat3.ts index a646193d7d6ecc7403bde52b2ec097610169e964..a7bb2cfdb84a55208b2292e2751a11d03484f297 100644 --- a/src/mol-math/linear-algebra/3d/mat3.ts +++ b/src/mol-math/linear-algebra/3d/mat3.ts @@ -22,6 +22,10 @@ import { NumberArray } from 'mol-util/type-helpers'; interface Mat3 extends Array<number> { [d: number]: number, '@type': 'mat3', length: 9 } +function Mat3() { + return Mat3.zero(); +} + namespace Mat3 { export function zero(): Mat3 { // force double backing array by 0.1. diff --git a/src/mol-math/linear-algebra/3d/mat4.ts b/src/mol-math/linear-algebra/3d/mat4.ts index c8a307e8de9a57ab893dab678ce1bff9d1203076..2bc177fab268d0ba36614a7811ef140ef8a4cdc3 100644 --- a/src/mol-math/linear-algebra/3d/mat4.ts +++ b/src/mol-math/linear-algebra/3d/mat4.ts @@ -1009,28 +1009,32 @@ namespace Mat4 { return Math.sqrt(Math.max(scaleXSq, scaleYSq, scaleZSq)) } + const xAxis = Vec3.create(1, 0, 0) + const yAxis = Vec3.create(1, 0, 0) + const zAxis = Vec3.create(1, 0, 0) + /** Rotation matrix for 90deg around x-axis */ - export const rotX90: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(90), Vec3.create(1, 0, 0)) + export const rotX90: ReadonlyMat4 = Mat4.fromRotation(Mat4(), degToRad(90), xAxis) /** Rotation matrix for 180deg around x-axis */ - export const rotX180: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(180), Vec3.create(1, 0, 0)) + export const rotX180: ReadonlyMat4 = Mat4.fromRotation(Mat4(), degToRad(180), xAxis) /** Rotation matrix for 90deg around y-axis */ - export const rotY90: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(90), Vec3.create(0, 1, 0)) + export const rotY90: ReadonlyMat4 = Mat4.fromRotation(Mat4(), degToRad(90), yAxis) /** Rotation matrix for 180deg around y-axis */ - export const rotY180: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(180), Vec3.create(0, 1, 0)) + export const rotY180: ReadonlyMat4 = Mat4.fromRotation(Mat4(), degToRad(180), yAxis) /** Rotation matrix for 90deg around z-axis */ - export const rotZ90: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(90), Vec3.create(0, 0, 1)) + export const rotZ90: ReadonlyMat4 = Mat4.fromRotation(Mat4(), degToRad(90), zAxis) /** Rotation matrix for 180deg around z-axis */ - export const rotZ180: ReadonlyMat4 = Mat4.fromRotation(Mat4.identity(), degToRad(180), Vec3.create(0, 0, 1)) + export const rotZ180: ReadonlyMat4 = Mat4.fromRotation(Mat4(), degToRad(180), zAxis) /** Rotation matrix for 90deg around first x-axis and then y-axis */ - export const rotXY90: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotX90, rotY90) + export const rotXY90: ReadonlyMat4 = Mat4.mul(Mat4(), rotX90, rotY90) /** Rotation matrix for 90deg around first z-axis and then y-axis */ - export const rotZY90: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotZ90, rotY90) + export const rotZY90: ReadonlyMat4 = Mat4.mul(Mat4(), rotZ90, rotY90) /** Rotation matrix for 90deg around first z-axis and then y-axis and then z-axis */ - export const rotZYZ90: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotZY90, rotZ90) + export const rotZYZ90: ReadonlyMat4 = Mat4.mul(Mat4(), rotZY90, rotZ90) /** Rotation matrix for 90deg around first z-axis and then 180deg around x-axis */ - export const rotZ90X180: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotZ90, rotX180) + export const rotZ90X180: ReadonlyMat4 = Mat4.mul(Mat4(), rotZ90, rotX180) /** Rotation matrix for 90deg around first y-axis and then 180deg around z-axis */ - export const rotY90Z180: ReadonlyMat4 = Mat4.mul(Mat4.identity(), rotY90, rotZ180) + export const rotY90Z180: ReadonlyMat4 = Mat4.mul(Mat4(), rotY90, rotZ180) } export default Mat4 \ No newline at end of file diff --git a/src/mol-math/linear-algebra/3d/quat.ts b/src/mol-math/linear-algebra/3d/quat.ts index 59e68c88bd5fe70b02d7e19bbc4cac22ed614956..abbe498e2a93943273156d6f2142143783553921 100644 --- a/src/mol-math/linear-algebra/3d/quat.ts +++ b/src/mol-math/linear-algebra/3d/quat.ts @@ -276,7 +276,7 @@ namespace Quat { return out; } - const fromUnitVec3Temp = Vec3.zero() + const fromUnitVec3Temp = Vec3() /** Quaternion from two normalized unit vectors. */ export function fromUnitVec3 (out: Quat, a: Vec3, b: Vec3) { // assumes a and b are normalized @@ -374,7 +374,7 @@ namespace Quat { * * Both vectors are assumed to be unit length. */ - const rotTmpVec3 = Vec3.zero(); + const rotTmpVec3 = Vec3(); const rotTmpVec3UnitX = Vec3.create(1, 0, 0); const rotTmpVec3UnitY = Vec3.create(0, 1, 0); export function rotationTo(out: Quat, a: Vec3, b: Vec3) { @@ -405,8 +405,8 @@ namespace Quat { /** * Performs a spherical linear interpolation with two control points */ - let sqlerpTemp1 = Quat.zero(); - let sqlerpTemp2 = Quat.zero(); + let sqlerpTemp1 = Quat(); + let sqlerpTemp2 = Quat(); export function sqlerp(out: Quat, a: Quat, b: Quat, c: Quat, d: Quat, t: number) { slerp(sqlerpTemp1, a, d, t); slerp(sqlerpTemp2, b, c, t); @@ -419,7 +419,7 @@ namespace Quat { * axes. Each axis is a vec3 and is expected to be unit length and * perpendicular to all other specified axes. */ - const axesTmpMat = Mat3.zero(); + const axesTmpMat = Mat3(); export function setAxes(out: Quat, view: Vec3, right: Vec3, up: Vec3) { axesTmpMat[0] = right[0]; axesTmpMat[3] = right[1]; diff --git a/src/mol-math/linear-algebra/3d/vec2.ts b/src/mol-math/linear-algebra/3d/vec2.ts index be7b7cdf93b52dac9b90fca247f42f75485fd383..65a32c3ff61149bd8a9c3e606ee67ab3c3e1b5b3 100644 --- a/src/mol-math/linear-algebra/3d/vec2.ts +++ b/src/mol-math/linear-algebra/3d/vec2.ts @@ -20,6 +20,10 @@ import { NumberArray } from 'mol-util/type-helpers'; interface Vec2 extends Array<number> { [d: number]: number, '@type': 'vec2', length: 2 } +function Vec2() { + return Vec2.zero(); +} + namespace Vec2 { export function zero(): Vec2 { // force double backing array by 0.1. diff --git a/src/mol-math/linear-algebra/3d/vec3.ts b/src/mol-math/linear-algebra/3d/vec3.ts index bb16cc3530375060824f061d2a8176b4112b2325..13b3053b7edec17ba8bfd239d6b48bcd61f16c5c 100644 --- a/src/mol-math/linear-algebra/3d/vec3.ts +++ b/src/mol-math/linear-algebra/3d/vec3.ts @@ -24,6 +24,10 @@ import { NumberArray } from 'mol-util/type-helpers'; interface Vec3 extends Array<number> { [d: number]: number, '@type': 'vec3', length: 3 } +function Vec3() { + return Vec3.zero(); +} + namespace Vec3 { export function zero(): Vec3 { const out = [0.1, 0.0, 0.0]; @@ -434,26 +438,28 @@ namespace Vec3 { } } - const tmp_dh_ab = Vec3.zero(); - const tmp_dh_cb = Vec3.zero(); - const tmp_dh_bc = Vec3.zero(); - const tmp_dh_dc = Vec3.zero(); - const tmp_dh_abc = Vec3.zero(); - const tmp_dh_bcd = Vec3.zero(); - const tmp_dh_cross = Vec3.zero(); - /** Computes the dihedral angles of 4 related atoms. phi: C, N+1, CA+1, C+1 - psi: N, CA, C, N+1 - omega: CA, C, N+1, CA+1 */ - export function dihedralAngle(a: Vec3, b: Vec3, c: Vec3, d: Vec3) { - Vec3.sub(tmp_dh_ab, a, b); - Vec3.sub(tmp_dh_cb, c, b); - Vec3.sub(tmp_dh_bc, b, c); - Vec3.sub(tmp_dh_dc, d, c); - - Vec3.cross(tmp_dh_abc, tmp_dh_ab, tmp_dh_cb); - Vec3.cross(tmp_dh_bcd, tmp_dh_bc, tmp_dh_dc); - - const angle = Vec3.angle(tmp_dh_abc, tmp_dh_bcd) * 360.0 / (2 * Math.PI); - Vec3.cross(tmp_dh_cross, tmp_dh_abc, tmp_dh_bcd); - return Vec3.dot(tmp_dh_cb, tmp_dh_cross) > 0 ? 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. + */ + export function dihedralAngle(a: Vec3, b: Vec3, c: Vec3, d: Vec3): number { + 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) * 360.0 / (2 * Math.PI); + cross(tmp_dh_cross, tmp_dh_abc, tmp_dh_bcd); + return dot(tmp_dh_cb, tmp_dh_cross) > 0 ? _angle : -_angle; } /** diff --git a/src/mol-math/linear-algebra/3d/vec4.ts b/src/mol-math/linear-algebra/3d/vec4.ts index c7c4e3380250572a684ce87ce4bdbd90ee8d19df..35346760ee95d740d18a346211f7a2d1920919d7 100644 --- a/src/mol-math/linear-algebra/3d/vec4.ts +++ b/src/mol-math/linear-algebra/3d/vec4.ts @@ -23,6 +23,10 @@ import { NumberArray } from 'mol-util/type-helpers'; interface Vec4 extends Array<number> { [d: number]: number, '@type': 'vec4', length: 4 } +function Vec4() { + return Vec4.zero(); +} + namespace Vec4 { export function zero(): Vec4 { // force double backing array by 0.1.