molstar
Version:
A comprehensive macromolecular library.
288 lines (287 loc) • 15.3 kB
JavaScript
/**
* Copyright (c) 2018-2024 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Gianluca Tomasello <giagitom@gmail.com>
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.InterUnitBondCylinderParams = void 0;
exports.InterUnitBondCylinderVisual = InterUnitBondCylinderVisual;
exports.InterUnitBondCylinderImpostorVisual = InterUnitBondCylinderImpostorVisual;
exports.InterUnitBondCylinderMeshVisual = InterUnitBondCylinderMeshVisual;
const param_definition_1 = require("../../../mol-util/param-definition");
const structure_1 = require("../../../mol-model/structure");
const mesh_1 = require("../../../mol-geo/geometry/mesh/mesh");
const linear_algebra_1 = require("../../../mol-math/linear-algebra");
const mol_util_1 = require("../../../mol-util");
const link_1 = require("./util/link");
const complex_visual_1 = require("../complex-visual");
const types_1 = require("../../../mol-model/structure/model/types");
const bond_1 = require("./util/bond");
const geometry_1 = require("../../../mol-math/geometry");
const cylinders_1 = require("../../../mol-geo/geometry/cylinders/cylinders");
const sorted_array_1 = require("../../../mol-data/int/sorted-array");
const size_1 = require("../../../mol-theme/size");
const location_iterator_1 = require("../../../mol-geo/util/location-iterator");
const tmpRefPosBondIt = new structure_1.Bond.ElementBondIterator();
function setRefPosition(pos, structure, unit, index) {
tmpRefPosBondIt.setElement(structure, unit, index);
while (tmpRefPosBondIt.hasNext) {
const bA = tmpRefPosBondIt.move();
bA.otherUnit.conformation.position(bA.otherUnit.elements[bA.otherIndex], pos);
return pos;
}
return null;
}
const tmpRef = (0, linear_algebra_1.Vec3)();
function getInterUnitBondCylinderBuilderProps(structure, theme, props) {
const locE = structure_1.StructureElement.Location.create(structure);
const locB = structure_1.Bond.Location(structure, undefined, undefined, structure, undefined, undefined);
const bonds = structure.interUnitBonds;
const { edgeCount, edges } = bonds;
const { sizeFactor, sizeAspectRatio, adjustCylinderLength, aromaticBonds, multipleBonds } = props;
const mbOff = multipleBonds === 'off';
const mbSymmetric = multipleBonds === 'symmetric';
const delta = (0, linear_algebra_1.Vec3)();
let stub;
const { child } = structure;
if (props.includeParent && child) {
stub = (edgeIndex) => {
const b = edges[edgeIndex];
const childUnitA = child.unitMap.get(b.unitA);
const childUnitB = child.unitMap.get(b.unitB);
const unitA = structure.unitMap.get(b.unitA);
const eA = unitA.elements[b.indexA];
const unitB = structure.unitMap.get(b.unitB);
const eB = unitB.elements[b.indexB];
return (childUnitA && sorted_array_1.SortedArray.has(childUnitA.elements, eA) &&
(!childUnitB || !sorted_array_1.SortedArray.has(childUnitB.elements, eB)));
};
}
const radius = (edgeIndex) => {
const 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;
};
const radiusA = (edgeIndex) => {
const b = edges[edgeIndex];
locE.unit = structure.unitMap.get(b.unitA);
locE.element = locE.unit.elements[b.indexA];
return theme.size.size(locE) * sizeFactor;
};
const radiusB = (edgeIndex) => {
const 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: (edgeIndex) => {
const b = edges[edgeIndex];
let unitA, unitB;
let 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: (posA, posB, edgeIndex, adjust) => {
const b = edges[edgeIndex];
const uA = structure.unitMap.get(b.unitA);
const uB = structure.unitMap.get(b.unitB);
uA.conformation.position(uA.elements[b.indexA], posA);
uB.conformation.position(uB.elements[b.indexB], posB);
if (adjust && adjustCylinderLength) {
const rA = radiusA(edgeIndex), rB = radiusB(edgeIndex);
const r = Math.min(rA, rB) * sizeAspectRatio;
const oA = Math.sqrt(Math.max(0, rA * rA - r * r)) - 0.05;
const oB = Math.sqrt(Math.max(0, rB * rB - r * r)) - 0.05;
if (oA <= 0.01 && oB <= 0.01)
return;
linear_algebra_1.Vec3.normalize(delta, linear_algebra_1.Vec3.sub(delta, posB, posA));
linear_algebra_1.Vec3.scaleAndAdd(posA, posA, delta, oA);
linear_algebra_1.Vec3.scaleAndAdd(posB, posB, delta, -oB);
}
},
style: (edgeIndex) => {
const o = edges[edgeIndex].props.order;
const f = mol_util_1.BitFlags.create(edges[edgeIndex].props.flag);
if (types_1.BondType.is(f, 2 /* BondType.Flag.MetallicCoordination */) || types_1.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 && types_1.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: (edgeIndex) => {
return radius(edgeIndex) * sizeAspectRatio;
},
ignore: (0, bond_1.makeInterBondIgnoreTest)(structure, props),
stub
};
}
function createInterUnitBondCylinderImpostors(ctx, structure, theme, props, cylinders) {
if (!(0, bond_1.hasStructureVisibleBonds)(structure, props))
return cylinders_1.Cylinders.createEmpty(cylinders);
if (!structure.interUnitBonds.edgeCount)
return cylinders_1.Cylinders.createEmpty(cylinders);
const builderProps = getInterUnitBondCylinderBuilderProps(structure, theme, props);
const { cylinders: c, boundingSphere } = (0, link_1.createLinkCylinderImpostors)(ctx, builderProps, props, cylinders);
if (boundingSphere) {
c.setBoundingSphere(boundingSphere);
}
else if (c.cylinderCount > 0) {
const { child } = structure;
const sphere = geometry_1.Sphere3D.expand((0, geometry_1.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 (!(0, bond_1.hasStructureVisibleBonds)(structure, props))
return mesh_1.Mesh.createEmpty(mesh);
if (!structure.interUnitBonds.edgeCount)
return mesh_1.Mesh.createEmpty(mesh);
const builderProps = getInterUnitBondCylinderBuilderProps(structure, theme, props);
const { mesh: m, boundingSphere } = (0, link_1.createLinkCylinderMesh)(ctx, builderProps, props, mesh);
if (boundingSphere) {
m.setBoundingSphere(boundingSphere);
}
else if (m.triangleCount > 0) {
const { child } = structure;
const sphere = geometry_1.Sphere3D.expand((0, geometry_1.Sphere3D)(), (child !== null && child !== void 0 ? child : structure).boundary.sphere, 1 * props.sizeFactor);
m.setBoundingSphere(sphere);
}
return m;
}
exports.InterUnitBondCylinderParams = {
...complex_visual_1.ComplexMeshParams,
...complex_visual_1.ComplexCylindersParams,
...bond_1.BondCylinderParams,
sizeFactor: param_definition_1.ParamDefinition.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
sizeAspectRatio: param_definition_1.ParamDefinition.Numeric(2 / 3, { min: 0, max: 3, step: 0.01 }),
tryUseImpostor: param_definition_1.ParamDefinition.Boolean(true),
includeParent: param_definition_1.ParamDefinition.Boolean(false),
};
function InterUnitBondCylinderVisual(materialId, structure, props, webgl) {
return props.tryUseImpostor && webgl && webgl.extensions.fragDepth
? InterUnitBondCylinderImpostorVisual(materialId)
: InterUnitBondCylinderMeshVisual(materialId);
}
function InterUnitBondCylinderImpostorVisual(materialId) {
return (0, complex_visual_1.ComplexCylindersVisual)({
defaultProps: param_definition_1.ParamDefinition.getDefaultValues(exports.InterUnitBondCylinderParams),
createGeometry: createInterUnitBondCylinderImpostors,
createLocationIterator: (structure, props) => {
return !(0, bond_1.hasStructureVisibleBonds)(structure, props)
? location_iterator_1.EmptyLocationIterator
: bond_1.BondIterator.fromStructure(structure, { includeLocation2: props.colorMode === 'interpolate' });
},
getLoci: bond_1.getInterBondLoci,
eachLocation: bond_1.eachInterBond,
setUpdateState: (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) => {
state.createGeometry = (newProps.sizeFactor !== currentProps.sizeFactor ||
newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
newProps.linkScale !== currentProps.linkScale ||
newProps.linkSpacing !== currentProps.linkSpacing ||
newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
newProps.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant ||
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 ||
!(0, mol_util_1.arrayEqual)(newProps.includeTypes, currentProps.includeTypes) ||
!(0, mol_util_1.arrayEqual)(newProps.excludeTypes, currentProps.excludeTypes) ||
newProps.adjustCylinderLength !== currentProps.adjustCylinderLength ||
newProps.multipleBonds !== currentProps.multipleBonds);
if (newProps.colorMode !== currentProps.colorMode) {
state.createGeometry = true;
state.updateTransform = true;
state.updateColor = true;
}
if ((0, bond_1.hasStructureVisibleBonds)(newStructure, newProps) && newStructure.interUnitBonds !== currentStructure.interUnitBonds) {
state.createGeometry = true;
state.updateTransform = true;
state.updateColor = true;
state.updateSize = true;
}
},
mustRecreate: (structure, props, webgl) => {
return !props.tryUseImpostor || !webgl;
}
}, materialId);
}
function InterUnitBondCylinderMeshVisual(materialId) {
return (0, complex_visual_1.ComplexMeshVisual)({
defaultProps: param_definition_1.ParamDefinition.getDefaultValues(exports.InterUnitBondCylinderParams),
createGeometry: createInterUnitBondCylinderMesh,
createLocationIterator: (structure, props) => {
return !(0, bond_1.hasStructureVisibleBonds)(structure, props)
? location_iterator_1.EmptyLocationIterator
: bond_1.BondIterator.fromStructure(structure);
},
getLoci: bond_1.getInterBondLoci,
eachLocation: bond_1.eachInterBond,
setUpdateState: (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.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant ||
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 ||
!(0, mol_util_1.arrayEqual)(newProps.includeTypes, currentProps.includeTypes) ||
!(0, mol_util_1.arrayEqual)(newProps.excludeTypes, currentProps.excludeTypes) ||
newProps.adjustCylinderLength !== currentProps.adjustCylinderLength ||
newProps.multipleBonds !== currentProps.multipleBonds ||
newProps.adjustCylinderLength && !size_1.SizeTheme.areEqual(newTheme.size, currentTheme.size));
if ((0, bond_1.hasStructureVisibleBonds)(newStructure, newProps) && newStructure.interUnitBonds !== currentStructure.interUnitBonds) {
state.createGeometry = true;
state.updateTransform = true;
state.updateColor = true;
state.updateSize = true;
}
},
mustRecreate: (structure, props, webgl) => {
return props.tryUseImpostor && !!webgl;
}
}, materialId);
}
;