UNPKG

molstar

Version:

A comprehensive macromolecular library.

251 lines (250 loc) 13.3 kB
/** * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ import { __assign } from "tslib"; import { ParamDefinition as PD } from '../../../mol-util/param-definition'; import { StructureElement, Bond } from '../../../mol-model/structure'; import { Mesh } from '../../../mol-geo/geometry/mesh/mesh'; import { Vec3 } from '../../../mol-math/linear-algebra'; import { BitFlags, arrayEqual } from '../../../mol-util'; import { createLinkCylinderImpostors, createLinkCylinderMesh } from './util/link'; import { ComplexMeshParams, ComplexMeshVisual, ComplexCylindersParams, ComplexCylindersVisual } from '../complex-visual'; import { BondType } from '../../../mol-model/structure/model/types'; import { BondCylinderParams, BondIterator, getInterBondLoci, eachInterBond, makeInterBondIgnoreTest } from './util/bond'; import { Sphere3D } from '../../../mol-math/geometry'; import { Cylinders } from '../../../mol-geo/geometry/cylinders/cylinders'; import { SortedArray } from '../../../mol-data/int/sorted-array'; var tmpRefPosBondIt = new Bond.ElementBondIterator(); function setRefPosition(pos, structure, unit, index) { tmpRefPosBondIt.setElement(structure, unit, index); while (tmpRefPosBondIt.hasNext) { var bA = tmpRefPosBondIt.move(); bA.otherUnit.conformation.position(bA.otherUnit.elements[bA.otherIndex], pos); return pos; } return null; } var tmpRef = Vec3(); function getInterUnitBondCylinderBuilderProps(structure, theme, props) { var locE = StructureElement.Location.create(structure); var locB = Bond.Location(structure, undefined, undefined, structure, undefined, undefined); var bonds = structure.interUnitBonds; var edgeCount = bonds.edgeCount, edges = bonds.edges; var sizeFactor = props.sizeFactor, sizeAspectRatio = props.sizeAspectRatio, adjustCylinderLength = props.adjustCylinderLength, aromaticBonds = props.aromaticBonds, multipleBonds = props.multipleBonds; var mbOff = multipleBonds === 'off'; var mbSymmetric = multipleBonds === 'symmetric'; var delta = Vec3(); var stub; var child = structure.child; if (props.includeParent && child) { stub = function (edgeIndex) { var b = edges[edgeIndex]; var childUnitA = child.unitMap.get(b.unitA); var childUnitB = child.unitMap.get(b.unitB); var unitA = structure.unitMap.get(b.unitA); var eA = unitA.elements[b.indexA]; var unitB = structure.unitMap.get(b.unitB); var eB = unitB.elements[b.indexB]; return (childUnitA && SortedArray.has(childUnitA.elements, eA) && (!childUnitB || !SortedArray.has(childUnitB.elements, eB))); }; } var radius = function (edgeIndex) { var b = edges[edgeIndex]; locB.aUnit = structure.unitMap.get(b.unitA); locB.aIndex = b.indexA; locB.bUnit = structure.unitMap.get(b.unitB); locB.bIndex = b.indexB; return theme.size.size(locB) * sizeFactor; }; var radiusA = function (edgeIndex) { var b = edges[edgeIndex]; locE.unit = structure.unitMap.get(b.unitA); locE.element = locE.unit.elements[b.indexA]; return theme.size.size(locE) * sizeFactor; }; var radiusB = function (edgeIndex) { var b = edges[edgeIndex]; locE.unit = structure.unitMap.get(b.unitB); locE.element = locE.unit.elements[b.indexB]; return theme.size.size(locE) * sizeFactor; }; return { linkCount: edgeCount, referencePosition: function (edgeIndex) { var b = edges[edgeIndex]; var unitA, unitB; var indexA, indexB; if (b.unitA < b.unitB) { unitA = structure.unitMap.get(b.unitA); unitB = structure.unitMap.get(b.unitB); indexA = b.indexA; indexB = b.indexB; } else if (b.unitA > b.unitB) { unitA = structure.unitMap.get(b.unitB); unitB = structure.unitMap.get(b.unitA); indexA = b.indexB; indexB = b.indexA; } else { throw new Error('same units in createInterUnitBondCylinderMesh'); } return setRefPosition(tmpRef, structure, unitA, indexA) || setRefPosition(tmpRef, structure, unitB, indexB); }, position: function (posA, posB, edgeIndex) { var b = edges[edgeIndex]; var uA = structure.unitMap.get(b.unitA); var uB = structure.unitMap.get(b.unitB); uA.conformation.position(uA.elements[b.indexA], posA); uB.conformation.position(uB.elements[b.indexB], posB); if (adjustCylinderLength) { var rA = radiusA(edgeIndex), rB = radiusB(edgeIndex); var r = Math.min(rA, rB) * sizeAspectRatio; var oA = Math.sqrt(Math.max(0, rA * rA - r * r)) - 0.05; var oB = Math.sqrt(Math.max(0, rB * rB - r * r)) - 0.05; if (oA <= 0.01 && oB <= 0.01) return; Vec3.normalize(delta, Vec3.sub(delta, posB, posA)); Vec3.scaleAndAdd(posA, posA, delta, oA); Vec3.scaleAndAdd(posB, posB, delta, -oB); } }, style: function (edgeIndex) { var o = edges[edgeIndex].props.order; var f = BitFlags.create(edges[edgeIndex].props.flag); if (BondType.is(f, 2 /* BondType.Flag.MetallicCoordination */) || BondType.is(f, 4 /* BondType.Flag.HydrogenBond */)) { // show metallic coordinations and hydrogen bonds with dashed cylinders return 1 /* LinkStyle.Dashed */; } else if (o === 3) { return mbOff ? 0 /* LinkStyle.Solid */ : mbSymmetric ? 4 /* LinkStyle.Triple */ : 5 /* LinkStyle.OffsetTriple */; } else if (aromaticBonds && BondType.is(f, 16 /* BondType.Flag.Aromatic */)) { return 7 /* LinkStyle.Aromatic */; } return (o !== 2 || mbOff) ? 0 /* LinkStyle.Solid */ : mbSymmetric ? 2 /* LinkStyle.Double */ : 3 /* LinkStyle.OffsetDouble */; }, radius: function (edgeIndex) { return radius(edgeIndex) * sizeAspectRatio; }, ignore: makeInterBondIgnoreTest(structure, props), stub: stub }; } function createInterUnitBondCylinderImpostors(ctx, structure, theme, props, cylinders) { if (!structure.interUnitBonds.edgeCount) return Cylinders.createEmpty(cylinders); var builderProps = getInterUnitBondCylinderBuilderProps(structure, theme, props); var _a = createLinkCylinderImpostors(ctx, builderProps, props, cylinders), c = _a.cylinders, boundingSphere = _a.boundingSphere; if (boundingSphere) { c.setBoundingSphere(boundingSphere); } else if (c.cylinderCount > 0) { var child = structure.child; var sphere = Sphere3D.expand(Sphere3D(), (child !== null && child !== void 0 ? child : structure).boundary.sphere, 1 * props.sizeFactor); c.setBoundingSphere(sphere); } return c; } function createInterUnitBondCylinderMesh(ctx, structure, theme, props, mesh) { if (!structure.interUnitBonds.edgeCount) return Mesh.createEmpty(mesh); var builderProps = getInterUnitBondCylinderBuilderProps(structure, theme, props); var _a = createLinkCylinderMesh(ctx, builderProps, props, mesh), m = _a.mesh, boundingSphere = _a.boundingSphere; if (boundingSphere) { m.setBoundingSphere(boundingSphere); } else if (m.triangleCount > 0) { var child = structure.child; var sphere = Sphere3D.expand(Sphere3D(), (child !== null && child !== void 0 ? child : structure).boundary.sphere, 1 * props.sizeFactor); m.setBoundingSphere(sphere); } return m; } export var InterUnitBondCylinderParams = __assign(__assign(__assign(__assign({}, ComplexMeshParams), ComplexCylindersParams), BondCylinderParams), { sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }), sizeAspectRatio: PD.Numeric(2 / 3, { min: 0, max: 3, step: 0.01 }), tryUseImpostor: PD.Boolean(true), includeParent: PD.Boolean(false) }); export function InterUnitBondCylinderVisual(materialId, structure, props, webgl) { return props.tryUseImpostor && webgl && webgl.extensions.fragDepth ? InterUnitBondCylinderImpostorVisual(materialId) : InterUnitBondCylinderMeshVisual(materialId); } export function InterUnitBondCylinderImpostorVisual(materialId) { return ComplexCylindersVisual({ defaultProps: PD.getDefaultValues(InterUnitBondCylinderParams), createGeometry: createInterUnitBondCylinderImpostors, createLocationIterator: BondIterator.fromStructure, getLoci: getInterBondLoci, eachLocation: eachInterBond, setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) { state.createGeometry = (newProps.sizeAspectRatio !== currentProps.sizeAspectRatio || newProps.linkScale !== currentProps.linkScale || newProps.linkSpacing !== currentProps.linkSpacing || newProps.ignoreHydrogens !== currentProps.ignoreHydrogens || newProps.linkCap !== currentProps.linkCap || newProps.aromaticScale !== currentProps.aromaticScale || newProps.aromaticSpacing !== currentProps.aromaticSpacing || newProps.aromaticDashCount !== currentProps.aromaticDashCount || newProps.dashCount !== currentProps.dashCount || newProps.dashScale !== currentProps.dashScale || newProps.dashCap !== currentProps.dashCap || newProps.stubCap !== currentProps.stubCap || !arrayEqual(newProps.includeTypes, currentProps.includeTypes) || !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) || newProps.adjustCylinderLength !== currentProps.adjustCylinderLength || newProps.multipleBonds !== currentProps.multipleBonds); if (newStructure.interUnitBonds !== currentStructure.interUnitBonds) { state.createGeometry = true; state.updateTransform = true; state.updateColor = true; state.updateSize = true; } }, mustRecreate: function (structure, props, webgl) { return !props.tryUseImpostor || !webgl; } }, materialId); } export function InterUnitBondCylinderMeshVisual(materialId) { return ComplexMeshVisual({ defaultProps: PD.getDefaultValues(InterUnitBondCylinderParams), createGeometry: createInterUnitBondCylinderMesh, createLocationIterator: BondIterator.fromStructure, getLoci: getInterBondLoci, eachLocation: eachInterBond, setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) { state.createGeometry = (newProps.sizeFactor !== currentProps.sizeFactor || newProps.sizeAspectRatio !== currentProps.sizeAspectRatio || newProps.radialSegments !== currentProps.radialSegments || newProps.linkScale !== currentProps.linkScale || newProps.linkSpacing !== currentProps.linkSpacing || newProps.ignoreHydrogens !== currentProps.ignoreHydrogens || newProps.linkCap !== currentProps.linkCap || newProps.aromaticScale !== currentProps.aromaticScale || newProps.aromaticSpacing !== currentProps.aromaticSpacing || newProps.aromaticDashCount !== currentProps.aromaticDashCount || newProps.dashCount !== currentProps.dashCount || newProps.dashScale !== currentProps.dashScale || newProps.dashCap !== currentProps.dashCap || newProps.stubCap !== currentProps.stubCap || !arrayEqual(newProps.includeTypes, currentProps.includeTypes) || !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes) || newProps.adjustCylinderLength !== currentProps.adjustCylinderLength || newProps.multipleBonds !== currentProps.multipleBonds); if (newStructure.interUnitBonds !== currentStructure.interUnitBonds) { state.createGeometry = true; state.updateTransform = true; state.updateColor = true; state.updateSize = true; } }, mustRecreate: function (structure, props, webgl) { return props.tryUseImpostor && !!webgl; } }, materialId); }