UNPKG

molstar

Version:

A comprehensive macromolecular library.

95 lines (94 loc) 4.29 kB
/** * Copyright (c) 2018-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 { Axes3D } from '../../../../mol-math/geometry'; import { MeshBuilder } from '../mesh-builder'; import { addCylinder } from './cylinder'; import { addSphere } from './sphere'; import { createCage } from '../../../primitive/cage'; var tmpStart = Vec3.zero(); var tmpEnd = Vec3.zero(); var cylinderProps = {}; export function addBoundingBox(state, box, radius, detail, radialSegments) { var min = box.min, max = box.max; cylinderProps.radiusTop = radius; cylinderProps.radiusBottom = radius; cylinderProps.radialSegments = radialSegments; Vec3.set(tmpStart, max[0], max[1], max[2]); addSphere(state, tmpStart, radius, detail); Vec3.set(tmpEnd, max[0], max[1], min[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpEnd, max[0], min[1], max[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpEnd, min[0], max[1], max[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpStart, min[0], min[1], min[2]); addSphere(state, tmpStart, radius, detail); Vec3.set(tmpEnd, min[0], min[1], max[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpEnd, min[0], max[1], min[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpEnd, max[0], min[1], min[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpStart, max[0], min[1], min[2]); addSphere(state, tmpStart, radius, detail); Vec3.set(tmpEnd, max[0], min[1], max[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpEnd, max[0], max[1], min[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpStart, min[0], min[1], max[2]); addSphere(state, tmpStart, radius, detail); Vec3.set(tmpEnd, min[0], max[1], max[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpEnd, max[0], min[1], max[2]); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpStart, min[0], max[1], min[2]); addSphere(state, tmpStart, radius, detail); Vec3.set(tmpEnd, max[0], max[1], min[2]); addSphere(state, tmpEnd, radius, detail); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); Vec3.set(tmpEnd, min[0], max[1], max[2]); addSphere(state, tmpEnd, radius, detail); addCylinder(state, tmpStart, tmpEnd, 1, cylinderProps); } // var tmpBoxVecCorner = Vec3(); var tmpBoxVecA = Vec3(); var tmpBoxVecB = Vec3(); var tmpBoxVecC = Vec3(); var tmpMatrix = Mat4.identity(); var tmpVertices = new Float32Array(8 * 3); var 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, axes, radiusScale, detail, radialSegments) { var origin = axes.origin, dirA = axes.dirA, dirB = axes.dirB, dirC = axes.dirC; var negDirA = Vec3.negate(tmpBoxVecA, dirA); var negDirB = Vec3.negate(tmpBoxVecB, dirB); var negDirC = Vec3.negate(tmpBoxVecC, dirC); var offset = 0; var addCornerHelper = function (v1, v2, v3) { 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); var cage = createCage(tmpVertices, tmpEdges); var volume = Axes3D.volume(axes); var radius = (Math.cbrt(volume) / 300) * radiusScale; MeshBuilder.addCage(state, tmpMatrix, cage, radius, detail, radialSegments); }