UNPKG

molstar

Version:

A comprehensive macromolecular library.

282 lines (281 loc) 15.4 kB
/** * Copyright (c) 2018-2022 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> * @author David Sehnal <david.sehnal@gmail.com> */ import { __assign } from "tslib"; import { ParamDefinition as PD } from '../../../mol-util/param-definition'; import { Unit, StructureElement, Bond } from '../../../mol-model/structure'; import { Mesh } from '../../../mol-geo/geometry/mesh/mesh'; import { Vec3 } from '../../../mol-math/linear-algebra'; import { arrayEqual } from '../../../mol-util'; import { createLinkCylinderImpostors, createLinkCylinderMesh } from './util/link'; import { UnitsMeshParams, UnitsMeshVisual, UnitsCylindersParams, UnitsCylindersVisual } from '../units-visual'; import { BondType } from '../../../mol-model/structure/model/types'; import { BondCylinderParams, BondIterator, eachIntraBond, getIntraBondLoci, ignoreBondType, makeIntraBondIgnoreTest } from './util/bond'; import { Sphere3D } from '../../../mol-math/geometry'; import { IntAdjacencyGraph } from '../../../mol-math/graph'; import { Cylinders } from '../../../mol-geo/geometry/cylinders/cylinders'; import { SortedArray } from '../../../mol-data/int'; import { arrayIntersectionSize } from '../../../mol-util/array'; // avoiding namespace lookup improved performance in Chrome (Aug 2020) var isBondType = BondType.is; function getIntraUnitBondCylinderBuilderProps(unit, structure, theme, props) { var elements = unit.elements; var bonds = unit.bonds; var edgeCount = bonds.edgeCount, a = bonds.a, b = bonds.b, edgeProps = bonds.edgeProps, offset = bonds.offset; var _order = edgeProps.order, _flags = edgeProps.flags; var sizeFactor = props.sizeFactor, sizeAspectRatio = props.sizeAspectRatio, adjustCylinderLength = props.adjustCylinderLength, aromaticBonds = props.aromaticBonds, includeTypes = props.includeTypes, excludeTypes = props.excludeTypes, multipleBonds = props.multipleBonds; var mbOff = multipleBonds === 'off'; var mbSymmetric = multipleBonds === 'symmetric'; var include = BondType.fromNames(includeTypes); var exclude = BondType.fromNames(excludeTypes); var ignoreComputedAromatic = ignoreBondType(include, exclude, 32 /* BondType.Flag.Computed */); var vRef = Vec3(), delta = Vec3(); var pos = unit.conformation.invariantPosition; var stub; var locE = StructureElement.Location.create(structure, unit); var locB = Bond.Location(structure, unit, undefined, structure, unit, undefined); var child = structure.child; if (props.includeParent && child) { var childUnit_1 = child.unitMap.get(unit.id); if (!childUnit_1) throw new Error('expected childUnit to exist'); stub = function (edgeIndex) { var eA = elements[a[edgeIndex]]; var eB = elements[b[edgeIndex]]; return SortedArray.has(childUnit_1.elements, eA) && !SortedArray.has(childUnit_1.elements, eB); }; } var radius = function (edgeIndex) { locB.aIndex = a[edgeIndex]; locB.bIndex = b[edgeIndex]; return theme.size.size(locB) * sizeFactor; }; var radiusA = function (edgeIndex) { locE.element = elements[a[edgeIndex]]; return theme.size.size(locE) * sizeFactor; }; var radiusB = function (edgeIndex) { locE.element = elements[b[edgeIndex]]; return theme.size.size(locE) * sizeFactor; }; var _a = unit.rings, elementRingIndices = _a.elementRingIndices, elementAromaticRingIndices = _a.elementAromaticRingIndices; var deloTriplets = aromaticBonds ? unit.resonance.delocalizedTriplets : undefined; return { linkCount: edgeCount * 2, referencePosition: function (edgeIndex) { var _a, _b; var aI = a[edgeIndex], bI = b[edgeIndex]; var rI = deloTriplets === null || deloTriplets === void 0 ? void 0 : deloTriplets.getThirdElement(aI, bI); if (rI !== undefined) return pos(elements[rI], vRef); if (aI > bI) _a = [bI, aI], aI = _a[0], bI = _a[1]; if (offset[aI + 1] - offset[aI] === 1) _b = [bI, aI], aI = _b[0], bI = _b[1]; var aR = elementAromaticRingIndices.get(aI) || elementRingIndices.get(aI); var maxSize = 0; for (var i = offset[aI], il = offset[aI + 1]; i < il; ++i) { var _bI = b[i]; if (_bI !== bI && _bI !== aI) { if (aR) { var _bR = elementAromaticRingIndices.get(_bI) || elementRingIndices.get(_bI); if (!_bR) continue; var size = arrayIntersectionSize(aR, _bR); if (size > maxSize) { maxSize = size; pos(elements[_bI], vRef); } } else { return pos(elements[_bI], vRef); } } } return maxSize > 0 ? vRef : null; }, position: function (posA, posB, edgeIndex) { pos(elements[a[edgeIndex]], posA); pos(elements[b[edgeIndex]], 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 = _order[edgeIndex]; var f = _flags[edgeIndex]; if (isBondType(f, 2 /* BondType.Flag.MetallicCoordination */) || isBondType(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) { var aI = a[edgeIndex], bI = b[edgeIndex]; var aR = elementAromaticRingIndices.get(aI); var bR = elementAromaticRingIndices.get(bI); var arCount = (aR && bR) ? arrayIntersectionSize(aR, bR) : 0; if (isBondType(f, 16 /* BondType.Flag.Aromatic */) || (arCount && !ignoreComputedAromatic)) { if (arCount === 2) { return 8 /* LinkStyle.MirroredAromatic */; } else { 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: makeIntraBondIgnoreTest(structure, unit, props), stub: stub }; } function createIntraUnitBondCylinderImpostors(ctx, unit, structure, theme, props, cylinders) { if (!Unit.isAtomic(unit)) return Cylinders.createEmpty(cylinders); if (!unit.bonds.edgeCount) return Cylinders.createEmpty(cylinders); var child = structure.child; var childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id); if (child && !childUnit) return Cylinders.createEmpty(cylinders); var builderProps = getIntraUnitBondCylinderBuilderProps(unit, 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 sphere = Sphere3D.expand(Sphere3D(), (childUnit !== null && childUnit !== void 0 ? childUnit : unit).boundary.sphere, 1 * props.sizeFactor); c.setBoundingSphere(sphere); } return c; } function createIntraUnitBondCylinderMesh(ctx, unit, structure, theme, props, mesh) { if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh); if (!unit.bonds.edgeCount) return Mesh.createEmpty(mesh); var child = structure.child; var childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id); if (child && !childUnit) return Mesh.createEmpty(mesh); var builderProps = getIntraUnitBondCylinderBuilderProps(unit, 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 sphere = Sphere3D.expand(Sphere3D(), (childUnit !== null && childUnit !== void 0 ? childUnit : unit).boundary.sphere, 1 * props.sizeFactor); m.setBoundingSphere(sphere); } return m; } export var IntraUnitBondCylinderParams = __assign(__assign(__assign(__assign({}, UnitsMeshParams), UnitsCylindersParams), 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 IntraUnitBondCylinderVisual(materialId, structure, props, webgl) { return props.tryUseImpostor && webgl && webgl.extensions.fragDepth ? IntraUnitBondCylinderImpostorVisual(materialId) : IntraUnitBondCylinderMeshVisual(materialId); } export function IntraUnitBondCylinderImpostorVisual(materialId) { return UnitsCylindersVisual({ defaultProps: PD.getDefaultValues(IntraUnitBondCylinderParams), createGeometry: createIntraUnitBondCylinderImpostors, createLocationIterator: BondIterator.fromGroup, getLoci: getIntraBondLoci, eachLocation: eachIntraBond, setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup) { 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.aromaticBonds !== currentProps.aromaticBonds || newProps.multipleBonds !== currentProps.multipleBonds); var newUnit = newStructureGroup.group.units[0]; var currentUnit = currentStructureGroup.group.units[0]; if (Unit.isAtomic(newUnit) && Unit.isAtomic(currentUnit)) { if (!IntAdjacencyGraph.areEqual(newUnit.bonds, currentUnit.bonds)) { state.createGeometry = true; state.updateTransform = true; state.updateColor = true; state.updateSize = true; } } }, mustRecreate: function (structureGroup, props, webgl) { return !props.tryUseImpostor || !webgl; } }, materialId); } export function IntraUnitBondCylinderMeshVisual(materialId) { return UnitsMeshVisual({ defaultProps: PD.getDefaultValues(IntraUnitBondCylinderParams), createGeometry: createIntraUnitBondCylinderMesh, createLocationIterator: BondIterator.fromGroup, getLoci: getIntraBondLoci, eachLocation: eachIntraBond, setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructureGroup, currentStructureGroup) { 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.aromaticBonds !== currentProps.aromaticBonds || newProps.multipleBonds !== currentProps.multipleBonds); var newUnit = newStructureGroup.group.units[0]; var currentUnit = currentStructureGroup.group.units[0]; if (Unit.isAtomic(newUnit) && Unit.isAtomic(currentUnit)) { if (!IntAdjacencyGraph.areEqual(newUnit.bonds, currentUnit.bonds)) { state.createGeometry = true; state.updateTransform = true; state.updateColor = true; state.updateSize = true; } } }, mustRecreate: function (structureGroup, props, webgl) { return props.tryUseImpostor && !!webgl; } }, materialId); }