Bladeren bron

Sphere3D.addVec3

Alexander Rose 6 jaren geleden
bovenliggende
commit
88dc9b9976
2 gewijzigde bestanden met toevoegingen van 17 en 4 verwijderingen
  1. 2 2
      src/mol-geo/geometry/lines/lines.ts
  2. 15 2
      src/mol-math/geometry/primitives/sphere3d.ts

+ 2 - 2
src/mol-geo/geometry/lines/lines.ts

@@ -180,7 +180,7 @@ function getBoundingSphere(lineStart: Float32Array, lineEnd: Float32Array, lineC
     const start = calculateBoundingSphere(lineStart, lineCount * 4, transform, transformCount)
     const end = calculateBoundingSphere(lineEnd, lineCount * 4, transform, transformCount)
     return {
-        boundingSphere: Sphere3D.addSphere(start.boundingSphere, end.boundingSphere),
-        invariantBoundingSphere: Sphere3D.addSphere(start.invariantBoundingSphere, end.invariantBoundingSphere)
+        boundingSphere: Sphere3D.expandBySphere(start.boundingSphere, end.boundingSphere),
+        invariantBoundingSphere: Sphere3D.expandBySphere(start.invariantBoundingSphere, end.invariantBoundingSphere)
     }
 }

+ 15 - 2
src/mol-math/geometry/primitives/sphere3d.ts

@@ -83,12 +83,25 @@ namespace Sphere3D {
         return out
     }
 
-    export function addSphere(out: Sphere3D, sphere: Sphere3D) {
+    const tmpAddVec3 = Vec3.zero()
+    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)
+        Vec3.sub(tmpAddVec3, s.center, v)
+        Vec3.sub(tmpAddVec3, s.center, tmpAddVec3)
+        Vec3.setMagnitude(tmpAddVec3, tmpAddVec3, s.radius)
+        Vec3.scale(out.center, Vec3.add(tmpAddVec3, tmpAddVec3, v), 0.5)
+        out.radius = Vec3.distance(out.center, v)
+        return out
+    }
+
+    /** Expand sphere radius by another sphere */
+    export function expandBySphere(out: Sphere3D, sphere: Sphere3D) {
         out.radius = Math.max(out.radius, Vec3.distance(out.center, sphere.center) + sphere.radius)
         return out
     }
 
-    /** Expand sphere by delta */
+    /** Expand sphere radius by delta */
     export function expand(out: Sphere3D, sphere: Sphere3D, delta: number): Sphere3D {
         out.radius = sphere.radius + delta
         return out