Browse Source

mesh-builder: addAxes, addOrientedBox

Alexander Rose 5 years ago
parent
commit
28443de11f

+ 34 - 0
src/mol-geo/geometry/mesh/builder/axes.ts

@@ -0,0 +1,34 @@
+/**
+ * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ *
+ * @author Alexander Rose <alexander.rose@weirdbyte.de>
+ */
+
+import { Vec3, Mat4 } from '../../../../mol-math/linear-algebra';
+import { MeshBuilder } from '../mesh-builder';
+import { Axes3D } from '../../../../mol-math/geometry';
+import { createCage } from '../../../primitive/cage';
+
+const tmpVec = Vec3()
+const tmpMatrix = Mat4.identity()
+
+const tmpVertices = new Float32Array(6 * 3)
+const tmpEdges = new Uint8Array([0, 1, 2, 3, 4, 5])
+
+export function addAxes(state: MeshBuilder.State, axes: Axes3D, radiusScale: number, detail: number, radialSegments: number) {
+    const { origin, dirA, dirB, dirC } = axes
+
+    Vec3.add(tmpVec, origin, dirA)
+    Vec3.toArray(Vec3.add(tmpVec, origin, dirA), tmpVertices, 0)
+    Vec3.toArray(Vec3.sub(tmpVec, origin, dirA), tmpVertices, 3)
+    Vec3.toArray(Vec3.add(tmpVec, origin, dirB), tmpVertices, 6)
+    Vec3.toArray(Vec3.sub(tmpVec, origin, dirB), tmpVertices, 9)
+    Vec3.toArray(Vec3.add(tmpVec, origin, dirC), tmpVertices, 12)
+    Vec3.toArray(Vec3.sub(tmpVec, origin, dirC), tmpVertices, 15)
+
+    const cage = createCage(tmpVertices, tmpEdges)
+    const volume = Axes3D.volume(axes)
+    const radius = (Math.cbrt(volume) / 300) * radiusScale
+
+    MeshBuilder.addCage(state, tmpMatrix, cage, radius, detail, radialSegments)
+}

+ 49 - 3
src/mol-geo/geometry/mesh/builder/bounding-box.ts → src/mol-geo/geometry/mesh/builder/box.ts

@@ -1,15 +1,16 @@
 /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
 
-import { Vec3 } from '../../../../mol-math/linear-algebra';
-import { Box3D } from '../../../../mol-math/geometry';
+import { Vec3, Mat4 } from '../../../../mol-math/linear-algebra';
+import { Box3D, Axes3D } from '../../../../mol-math/geometry';
 import { MeshBuilder } from '../mesh-builder';
 import { CylinderProps } from '../../../primitive/cylinder';
 import { addCylinder } from './cylinder';
 import { addSphere } from './sphere';
+import { createCage } from '../../../primitive/cage';
 
 const tmpStart = Vec3.zero()
 const tmpEnd = Vec3.zero()
@@ -62,4 +63,49 @@ export function addBoundingBox(state: MeshBuilder.State, box: Box3D, radius: num
     Vec3.set(tmpEnd, min[0], max[1], max[2])
     addSphere(state, tmpEnd, radius, detail)
     addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps)
+}
+
+//
+
+const tmpBoxVecCorner = Vec3()
+const tmpBoxVecA = Vec3()
+const tmpBoxVecB = Vec3()
+const tmpBoxVecC = Vec3()
+const tmpMatrix = Mat4.identity()
+
+const tmpVertices = new Float32Array(8 * 3)
+const tmpEdges = new Uint8Array([
+    0, 1, 0, 3, 0, 6, 1, 2, 1, 7, 2, 3,
+    2, 4, 3, 5, 4, 5, 4, 7, 5, 6, 6, 7
+])
+
+export function addOrientedBox(state: MeshBuilder.State, axes: Axes3D, radiusScale: number, detail: number, radialSegments: number) {
+    const { origin, dirA, dirB, dirC } = axes
+    const negDirA = Vec3.negate(tmpBoxVecA, dirA)
+    const negDirB = Vec3.negate(tmpBoxVecB, dirB)
+    const negDirC = Vec3.negate(tmpBoxVecC, dirC)
+
+    let offset = 0
+    const addCornerHelper = function (v1: Vec3, v2: Vec3, v3: Vec3) {
+        Vec3.copy(tmpBoxVecCorner, origin)
+        Vec3.add(tmpBoxVecCorner, tmpBoxVecCorner, v1)
+        Vec3.add(tmpBoxVecCorner, tmpBoxVecCorner, v2)
+        Vec3.add(tmpBoxVecCorner, tmpBoxVecCorner, v3)
+        Vec3.toArray(tmpBoxVecCorner, tmpVertices, offset)
+        offset += 3
+    }
+    addCornerHelper(dirA, dirB, dirC)
+    addCornerHelper(dirA, dirB, negDirC)
+    addCornerHelper(dirA, negDirB, negDirC)
+    addCornerHelper(dirA, negDirB, dirC)
+    addCornerHelper(negDirA, negDirB, negDirC)
+    addCornerHelper(negDirA, negDirB, dirC)
+    addCornerHelper(negDirA, dirB, dirC)
+    addCornerHelper(negDirA, dirB, negDirC)
+
+    const cage = createCage(tmpVertices, tmpEdges)
+    const volume = Axes3D.volume(axes)
+    const radius = (Math.cbrt(volume) / 300) * radiusScale
+
+    MeshBuilder.addCage(state, tmpMatrix, cage, radius, detail, radialSegments)
 }

+ 4 - 67
src/mol-repr/shape/loci/orientation.ts

@@ -12,13 +12,12 @@ import { ColorNames } from '../../../mol-util/color/names';
 import { ShapeRepresentation } from '../representation';
 import { Representation, RepresentationParamsGetter, RepresentationContext } from '../../representation';
 import { Shape } from '../../../mol-model/shape';
-import { Vec3, Mat4 } from '../../../mol-math/linear-algebra';
 import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
-import { createCage } from '../../../mol-geo/primitive/cage';
-import { Axes3D } from '../../../mol-math/geometry';
 import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder';
 import { PrincipalAxes } from '../../../mol-math/linear-algebra/matrix/principal-axes';
 import { lociLabel } from '../../../mol-theme/label';
+import { addAxes } from '../../../mol-geo/geometry/mesh/builder/axes';
+import { addOrientedBox } from '../../../mol-geo/geometry/mesh/builder/box';
 
 export interface OrientationData {
     loci: Loci
@@ -60,31 +59,10 @@ export type OrientationProps = PD.Values<OrientationParams>
 
 //
 
-const tmpAxesVec = Vec3()
-
 function buildAxesMesh(principalAxes: PrincipalAxes, props: OrientationProps, mesh?: Mesh): Mesh {
     const state = MeshBuilder.createState(256, 128, mesh)
-
-    const { origin, dirA, dirB, dirC } = principalAxes.momentsAxes
-
-    const vertices = new Float32Array(6 * 3)
-    const edges = new Uint8Array([0, 1, 2, 3, 4, 5])
-    Vec3.add(tmpAxesVec, origin, dirA)
-    Vec3.toArray(Vec3.add(tmpAxesVec, origin, dirA), vertices, 0)
-    Vec3.toArray(Vec3.sub(tmpAxesVec, origin, dirA), vertices, 3)
-    Vec3.toArray(Vec3.add(tmpAxesVec, origin, dirB), vertices, 6)
-    Vec3.toArray(Vec3.sub(tmpAxesVec, origin, dirB), vertices, 9)
-    Vec3.toArray(Vec3.add(tmpAxesVec, origin, dirC), vertices, 12)
-    Vec3.toArray(Vec3.sub(tmpAxesVec, origin, dirC), vertices, 15)
-
-    const matrix = Mat4.identity()
-
-    const cage = createCage(vertices, edges)
-    const volume = Axes3D.volume(principalAxes.boxAxes)
-    const radius = (Math.cbrt(volume) / 300) * props.scale
     state.currentGroup = 1
-    MeshBuilder.addCage(state, matrix, cage, radius, 2, 20)
-
+    addAxes(state, principalAxes.momentsAxes, props.scale, 2, 20)
     return MeshBuilder.getMesh(state)
 }
 
@@ -100,51 +78,10 @@ function getAxesShape(ctx: RuntimeContext, data: OrientationData, props: Orienta
 
 //
 
-const tmpBoxVecCorner = Vec3()
-const tmpBoxVecA = Vec3()
-const tmpBoxVecB = Vec3()
-const tmpBoxVecC = Vec3()
-
 function buildBoxMesh(principalAxes: PrincipalAxes, props: OrientationProps, mesh?: Mesh): Mesh {
     const state = MeshBuilder.createState(256, 128, mesh)
-
-    const { origin, dirA, dirB, dirC } = principalAxes.boxAxes
-    const negDirA = Vec3.negate(tmpBoxVecA, dirA)
-    const negDirB = Vec3.negate(tmpBoxVecB, dirB)
-    const negDirC = Vec3.negate(tmpBoxVecC, dirC)
-
-    const vertices = new Float32Array(8 * 3)
-    const edges = new Uint8Array([
-        0, 1, 0, 3, 0, 6, 1, 2, 1, 7, 2, 3,
-        2, 4, 3, 5, 4, 5, 4, 7, 5, 6, 6, 7
-    ])
-
-    let offset = 0
-    const addCornerHelper = function (v1: Vec3, v2: Vec3, v3: Vec3) {
-        Vec3.copy(tmpBoxVecCorner, origin)
-        Vec3.add(tmpBoxVecCorner, tmpBoxVecCorner, v1)
-        Vec3.add(tmpBoxVecCorner, tmpBoxVecCorner, v2)
-        Vec3.add(tmpBoxVecCorner, tmpBoxVecCorner, v3)
-        Vec3.toArray(tmpBoxVecCorner, vertices, offset)
-        offset += 3
-    }
-    addCornerHelper(dirA, dirB, dirC)
-    addCornerHelper(dirA, dirB, negDirC)
-    addCornerHelper(dirA, negDirB, negDirC)
-    addCornerHelper(dirA, negDirB, dirC)
-    addCornerHelper(negDirA, negDirB, negDirC)
-    addCornerHelper(negDirA, negDirB, dirC)
-    addCornerHelper(negDirA, dirB, dirC)
-    addCornerHelper(negDirA, dirB, negDirC)
-
-    const matrix = Mat4.identity()
-
-    const cage = createCage(vertices, edges)
-    const volume = Axes3D.volume(principalAxes.boxAxes)
-    const radius = (Math.cbrt(volume) / 300) * props.scale
     state.currentGroup = 1
-    MeshBuilder.addCage(state, matrix, cage, radius, 2, 20)
-
+    addOrientedBox(state, principalAxes.boxAxes, props.scale, 2, 20)
     return MeshBuilder.getMesh(state)
 }