Browse Source

added Sphere3D.fromSphere3Ds and Loci.getBundleBoundingSphere

Alexander Rose 5 years ago
parent
commit
414b20f06d
2 changed files with 16 additions and 1 deletions
  1. 11 1
      src/mol-math/geometry/primitives/sphere3d.ts
  2. 5 0
      src/mol-model/loci.ts

+ 11 - 1
src/mol-math/geometry/primitives/sphere3d.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2020 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>
@@ -11,6 +11,7 @@ import { OrderedSet } from '../../../mol-data/int';
 import { NumberArray } from '../../../mol-util/type-helpers';
 import { Box3D } from './box3d';
 import { Axes3D } from './axes3d';
+import { BoundaryHelper } from '../boundary-helper';
 
 interface Sphere3D { center: Vec3, radius: number }
 
@@ -94,6 +95,15 @@ namespace Sphere3D {
         return out
     }
 
+    const boundaryHelper = new BoundaryHelper();
+    export function fromSphere3Ds(spheres: Sphere3D[]): Sphere3D {
+        boundaryHelper.reset(0);
+        for (const s of spheres) boundaryHelper.boundaryStep(s.center, s.radius);
+        boundaryHelper.finishBoundaryStep();
+        for (const s of spheres) boundaryHelper.extendStep(s.center, s.radius);
+        return boundaryHelper.getSphere();
+    }
+
     const tmpAddVec3 = Vec3()
     export function addVec3(out: Sphere3D, s: Sphere3D, v: Vec3) {
         const d = Vec3.distance(s.center, v)

+ 5 - 0
src/mol-model/loci.ts

@@ -65,6 +65,11 @@ namespace Loci {
     interface FiniteArray<T, L extends number = number> extends ReadonlyArray<T> { length: L };
     export interface Bundle<L extends number> { loci: FiniteArray<Loci, L> }
 
+    export function getBundleBoundingSphere(bundle: Bundle<any>): Sphere3D {
+        const spheres = bundle.loci.map(l => getBoundingSphere(l)).filter(s => !!s)
+        return Sphere3D.fromSphere3Ds(spheres as Sphere3D[])
+    }
+
     export function areEqual(lociA: Loci, lociB: Loci) {
         if (isEveryLoci(lociA) && isEveryLoci(lociB)) return true
         if (isEmptyLoci(lociA) && isEmptyLoci(lociB)) return true