Bladeren bron

math lib tweaks

Alexander Rose 6 jaren geleden
bovenliggende
commit
fb623f35a3

+ 7 - 3
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

+ 5 - 1
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)

+ 4 - 0
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.

+ 15 - 11
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

+ 5 - 5
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];

+ 4 - 0
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.

+ 26 - 20
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;
     }
 
     /**

+ 4 - 0
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.