UNPKG

molstar

Version:

A comprehensive macromolecular library.

118 lines (117 loc) 5.6 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 { Structure, StructureElement, Unit } from '../../../mol-model/structure'; import { EmptyLoci } from '../../../mol-model/loci'; import { Vec3 } from '../../../mol-math/linear-algebra'; import { createLinkCylinderMesh, LinkCylinderParams } from './util/link'; import { OrderedSet, Interval } from '../../../mol-data/int'; import { ComplexMeshVisual } from '../complex-visual'; import { UnitsMeshParams } from '../units-visual'; import { ParamDefinition as PD } from '../../../mol-util/param-definition'; import { LocationIterator } from '../../../mol-geo/util/location-iterator'; import { getAltResidueLociFromId } from './util/common'; import { Sphere3D } from '../../../mol-math/geometry'; function createCarbohydrateLinkCylinderMesh(ctx, structure, theme, props, mesh) { var _a = structure.carbohydrates, links = _a.links, elements = _a.elements; var linkSizeFactor = props.linkSizeFactor; var location = StructureElement.Location.create(structure); var builderProps = { linkCount: links.length, position: function (posA, posB, edgeIndex) { var l = links[edgeIndex]; Vec3.copy(posA, elements[l.carbohydrateIndexA].geometry.center); Vec3.copy(posB, elements[l.carbohydrateIndexB].geometry.center); }, radius: function (edgeIndex) { var l = links[edgeIndex]; var carbA = elements[l.carbohydrateIndexA]; var ringA = carbA.unit.rings.all[carbA.ringIndex]; location.unit = carbA.unit; location.element = carbA.unit.elements[ringA[0]]; return theme.size.size(location) * linkSizeFactor; }, }; var _b = createLinkCylinderMesh(ctx, builderProps, props, mesh), m = _b.mesh, boundingSphere = _b.boundingSphere; if (boundingSphere) { m.setBoundingSphere(boundingSphere); } else if (m.triangleCount > 0) { var sphere = Sphere3D.expand(Sphere3D(), structure.boundary.sphere, 1 * linkSizeFactor); m.setBoundingSphere(sphere); } return m; } export var CarbohydrateLinkParams = __assign(__assign(__assign({}, UnitsMeshParams), LinkCylinderParams), { linkSizeFactor: PD.Numeric(0.3, { min: 0, max: 3, step: 0.01 }) }); export function CarbohydrateLinkVisual(materialId) { return ComplexMeshVisual({ defaultProps: PD.getDefaultValues(CarbohydrateLinkParams), createGeometry: createCarbohydrateLinkCylinderMesh, createLocationIterator: CarbohydrateLinkIterator, getLoci: getLinkLoci, eachLocation: eachCarbohydrateLink, setUpdateState: function (state, newProps, currentProps) { state.createGeometry = (newProps.linkSizeFactor !== currentProps.linkSizeFactor || newProps.radialSegments !== currentProps.radialSegments || newProps.linkCap !== currentProps.linkCap); } }, materialId); } function CarbohydrateLinkIterator(structure) { var _a = structure.carbohydrates, elements = _a.elements, links = _a.links; var groupCount = links.length; var instanceCount = 1; var location = StructureElement.Location.create(structure); var getLocation = function (groupIndex) { var link = links[groupIndex]; var carbA = elements[link.carbohydrateIndexA]; var ringA = carbA.unit.rings.all[carbA.ringIndex]; location.unit = carbA.unit; location.element = carbA.unit.elements[ringA[0]]; return location; }; return LocationIterator(groupCount, instanceCount, 1, getLocation, true); } function getLinkLoci(pickingId, structure, id) { var objectId = pickingId.objectId, groupId = pickingId.groupId; if (id === objectId) { var _a = structure.carbohydrates, links = _a.links, elements = _a.elements; var l = links[groupId]; var carbA = elements[l.carbohydrateIndexA]; var carbB = elements[l.carbohydrateIndexB]; return StructureElement.Loci.union(getAltResidueLociFromId(structure, carbA.unit, carbA.residueIndex, carbA.altId), getAltResidueLociFromId(structure, carbB.unit, carbB.residueIndex, carbB.altId)); } return EmptyLoci; } var __linkIndicesSet = new Set(); function eachCarbohydrateLink(loci, structure, apply) { var changed = false; if (!StructureElement.Loci.is(loci)) return false; if (!Structure.areEquivalent(loci.structure, structure)) return false; var getLinkIndices = structure.carbohydrates.getLinkIndices; var _loop_1 = function (unit, indices) { if (!Unit.isAtomic(unit)) return "continue"; __linkIndicesSet.clear(); OrderedSet.forEach(indices, function (v) { var linkIndices = getLinkIndices(unit, unit.elements[v]); for (var i = 0, il = linkIndices.length; i < il; ++i) { if (!__linkIndicesSet.has(linkIndices[i])) { __linkIndicesSet.add(linkIndices[i]); if (apply(Interval.ofSingleton(linkIndices[i]))) changed = true; } } }); }; for (var _i = 0, _a = loci.elements; _i < _a.length; _i++) { var _b = _a[_i], unit = _b.unit, indices = _b.indices; _loop_1(unit, indices); } return changed; }