Bläddra i källkod

Add check to avoid non-finite origin vector

giagitom 1 år sedan
förälder
incheckning
beb4351dc9

+ 5 - 1
src/mol-math/linear-algebra/3d/vec3.ts

@@ -48,6 +48,10 @@ namespace Vec3 {
         return out;
     }
 
+    export function hasUndetermined(a: Vec3): boolean {
+        return isFinite(a[0]) && isFinite(a[1]) && isFinite(a[2]);
+    }
+
     export function hasNaN(a: Vec3) {
         return isNaN(a[0]) || isNaN(a[1]) || isNaN(a[2]);
     }
@@ -636,4 +640,4 @@ namespace Vec3 {
     export const negUnitZ: ReadonlyVec3 = create(0, 0, -1);
 }
 
-export { Vec3 };
+export { Vec3 };

+ 9 - 3
src/mol-math/linear-algebra/matrix/principal-axes.ts

@@ -2,6 +2,7 @@
  * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ * @author Gianluca Tomasello <giagitom@gmail.com>
  */
 
 import { Matrix } from './matrix';
@@ -123,12 +124,17 @@ namespace PrincipalAxes {
         const dirB = Vec3.setMagnitude(Vec3(), a.dirB, (d2a + d2b) / 2);
         const dirC = Vec3.setMagnitude(Vec3(), a.dirC, (d3a + d3b) / 2);
 
+        const okDirA = Vec3.hasUndetermined(dirA);
+        const okDirB = Vec3.hasUndetermined(dirB);
+        const okDirC = Vec3.hasUndetermined(dirC);
+
         const origin = Vec3();
         const addCornerHelper = function (d1: number, d2: number, d3: number) {
             Vec3.copy(tmpBoxVec, center);
-            Vec3.scaleAndAdd(tmpBoxVec, tmpBoxVec, a.dirA, d1);
-            Vec3.scaleAndAdd(tmpBoxVec, tmpBoxVec, a.dirB, d2);
-            Vec3.scaleAndAdd(tmpBoxVec, tmpBoxVec, a.dirC, d3);
+
+            if (okDirA) Vec3.scaleAndAdd(tmpBoxVec, tmpBoxVec, a.dirA, d1);
+            if (okDirB) Vec3.scaleAndAdd(tmpBoxVec, tmpBoxVec, a.dirB, d2);
+            if (okDirC) Vec3.scaleAndAdd(tmpBoxVec, tmpBoxVec, a.dirC, d3);
             Vec3.add(origin, origin, tmpBoxVec);
         };
         addCornerHelper(d1a, d2a, d3a);