浏览代码

bounding-sphere fixes

- fixed renderable.spec
- simplify extrema for calculateInvariantBoundingSphere when possible
- fixed Sphere3D.expand
Alexander Rose 5 年之前
父节点
当前提交
5c77eec184
共有 3 个文件被更改,包括 43 次插入32 次删除
  1. 30 28
      src/mol-gl/_spec/renderable.spec.ts
  2. 11 1
      src/mol-gl/renderable/util.ts
  3. 2 3
      src/mol-math/geometry/primitives/sphere3d.ts

+ 30 - 28
src/mol-gl/_spec/renderable.spec.ts

@@ -1,42 +1,44 @@
 /**
- * Copyright (c) 2018 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 Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-// import { calculateBoundingSphere } from '../renderable/util';
-// import { Vec3 } from '../../mol-math/linear-algebra';
+import { calculateBoundingSphere } from '../renderable/util';
 
 describe('renderable', () => {
     it('calculateBoundingSphere', () => {
-        // const position = new Float32Array([
-        //     0, 0, 0,
-        //     1, 0, 0
-        // ])
-        // const transform = new Float32Array([
-        //     1, 0, 0, 0,
-        //     0, 1, 0, 0,
-        //     0, 0, 1, 0,
-        //     0, 0, 0, 0,
+        const position = new Float32Array([
+            0, 0, 0,
+            1, 0, 0,
+            -1, 0, 0,
+        ])
+        const transform = new Float32Array([
+            1, 0, 0, 0,
+            0, 1, 0, 0,
+            0, 0, 1, 0,
+            0, 0, 0, 0,
 
-        //     1, 0, 0, 0,
-        //     0, 1, 0, 0,
-        //     0, 0, 1, 0,
-        //     1, 0, 0, 0,
+            1, 0, 0, 0,
+            0, 1, 0, 0,
+            0, 0, 1, 0,
+            1, 0, 0, 0,
 
-        //     1, 0, 0, 0,
-        //     0, 1, 0, 0,
-        //     0, 0, 1, 0,
-        //     2, 0, 0, 0
-        // ])
+            1, 0, 0, 0,
+            0, 1, 0, 0,
+            0, 0, 1, 0,
+            -1, 0, 0, 0
+        ])
 
-        // const { boundingSphere } = calculateBoundingSphere(
-        //     position, position.length / 3,
-        //     transform, transform.length / 16
-        // )
+        const { boundingSphere, invariantBoundingSphere } = calculateBoundingSphere(
+            position, position.length / 3,
+            transform, transform.length / 16
+        )
 
-        // TODO:
-        // expect(boundingSphere.radius).toBeCloseTo(1.58, 2)
-        // expect(Vec3.equals(boundingSphere.center, Vec3.create(1.418367, 0, 0))).toBe(true)
+        expect(invariantBoundingSphere.extrema).toEqual([[0, 0, 0], [1, 0, 0], [-1, 0, 0]])
+        expect(invariantBoundingSphere.radius).toBe(1)
+        expect(invariantBoundingSphere.center).toEqual([0, 0, 0])
+        expect(boundingSphere.radius).toBe(2)
+        expect(boundingSphere.center).toEqual([0, 0, 0])
     })
 })

+ 11 - 1
src/mol-gl/renderable/util.ts

@@ -103,7 +103,17 @@ export function calculateInvariantBoundingSphere(position: Float32Array, positio
         boundaryHelper.radiusStep(v)
     }
 
-    return boundaryHelper.getSphere()
+    const sphere = boundaryHelper.getSphere()
+
+    if (positionCount <= 98) {
+        const extrema: Vec3[] = []
+        for (let i = 0, _i = positionCount * 3; i < _i; i += step) {
+            extrema.push(Vec3.fromArray(Vec3(), position, i));
+        }
+        Sphere3D.setExtrema(sphere, extrema)
+    }
+
+    return sphere
 }
 
 export function calculateTransformBoundingSphere(invariantBoundingSphere: Sphere3D, transform: Float32Array, transformCount: number): Sphere3D {

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

@@ -138,12 +138,11 @@ namespace Sphere3D {
         out.radius = sphere.radius + delta
         if (hasExtrema(sphere)) {
             setExtrema(out, sphere.extrema.map(e => {
-                Vec3.sub(tmpDir, sphere.center, e)
+                Vec3.sub(tmpDir, e, sphere.center)
                 const dist = Vec3.distance(sphere.center, e)
                 Vec3.normalize(tmpDir, tmpDir)
-                return Vec3.scaleAndAdd(Vec3(), e, tmpDir, dist + delta)
+                return Vec3.scaleAndAdd(Vec3(), sphere.center, tmpDir, dist + delta)
             }))
-            setExtrema(out, sphere.extrema)
         }
         return out
     }