UNPKG

molstar

Version:

A comprehensive macromolecular library.

386 lines (385 loc) 17.7 kB
"use strict"; /** * Copyright (c) 2018-2023 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> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ElementIterator = void 0; exports.makeElementIgnoreTest = makeElementIgnoreTest; exports.createElementSphereMesh = createElementSphereMesh; exports.createElementSphereImpostor = createElementSphereImpostor; exports.eachElement = eachElement; exports.getElementLoci = getElementLoci; exports.createStructureElementSphereMesh = createStructureElementSphereMesh; exports.createStructureElementSphereImpostor = createStructureElementSphereImpostor; exports.eachSerialElement = eachSerialElement; exports.getSerialElementLoci = getSerialElementLoci; const linear_algebra_1 = require("../../../../mol-math/linear-algebra"); const structure_1 = require("../../../../mol-model/structure"); const loci_1 = require("../../../../mol-model/loci"); const int_1 = require("../../../../mol-data/int"); const mesh_1 = require("../../../../mol-geo/geometry/mesh/mesh"); const sphere_1 = require("../../../../mol-geo/primitive/sphere"); const mesh_builder_1 = require("../../../../mol-geo/geometry/mesh/mesh-builder"); const sphere_2 = require("../../../../mol-geo/geometry/mesh/builder/sphere"); const location_iterator_1 = require("../../../../mol-geo/util/location-iterator"); const spheres_1 = require("../../../../mol-geo/geometry/spheres/spheres"); const spheres_builder_1 = require("../../../../mol-geo/geometry/spheres/spheres-builder"); const common_1 = require("./common"); const geometry_1 = require("../../../../mol-math/geometry"); // avoiding namespace lookup improved performance in Chrome (Aug 2020) const v3add = linear_algebra_1.Vec3.add; function makeElementIgnoreTest(structure, unit, props) { const { ignoreHydrogens, ignoreHydrogensVariant, traceOnly } = props; const isCoarse = structure_1.Unit.isCoarse(unit); const { child } = structure; const childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id); if (child && !childUnit) throw new Error('expected childUnit to exist if child exists'); if (!child && !ignoreHydrogens && !traceOnly) return; return (element) => { return ((!!childUnit && !int_1.SortedArray.has(childUnit.elements, element)) || (!isCoarse && ignoreHydrogens && (0, common_1.isHydrogen)(structure, unit, element, ignoreHydrogensVariant)) || (traceOnly && !(0, common_1.isTrace)(unit, element))); }; } function createElementSphereMesh(ctx, unit, structure, theme, props, mesh) { const { child } = structure; const childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id); if (child && !childUnit) return mesh_1.Mesh.createEmpty(mesh); const { detail, sizeFactor, stride } = props; const { elements, conformation: c } = unit; const elementCount = elements.length; const vertexCount = elementCount * (0, sphere_1.sphereVertexCount)(detail); const builderState = mesh_builder_1.MeshBuilder.createState(vertexCount, vertexCount / 2, mesh); const v = (0, linear_algebra_1.Vec3)(); const ignore = makeElementIgnoreTest(structure, unit, props); const l = structure_1.StructureElement.Location.create(structure, unit); const themeSize = theme.size.size; const center = (0, linear_algebra_1.Vec3)(); let maxSize = 0; let count = 0; for (let i = 0; i < elementCount; i++) { if (stride && i % stride !== 0) continue; if (ignore && ignore(elements[i])) continue; c.invariantPosition(elements[i], v); v3add(center, center, v); count += 1; l.element = elements[i]; const size = themeSize(l); if (size > maxSize) maxSize = size; builderState.currentGroup = i; (0, sphere_2.addSphere)(builderState, v, size * sizeFactor, detail); } const m = mesh_builder_1.MeshBuilder.getMesh(builderState); if (count === 0) return m; // re-use boundingSphere if it has not changed much let boundingSphere; linear_algebra_1.Vec3.scale(center, center, 1 / count); const oldBoundingSphere = mesh ? geometry_1.Sphere3D.clone(mesh.boundingSphere) : undefined; if (oldBoundingSphere && linear_algebra_1.Vec3.distance(center, oldBoundingSphere.center) / oldBoundingSphere.radius < 0.1) { boundingSphere = oldBoundingSphere; } else { boundingSphere = geometry_1.Sphere3D.expand((0, geometry_1.Sphere3D)(), (childUnit !== null && childUnit !== void 0 ? childUnit : unit).boundary.sphere, maxSize * sizeFactor + 0.05); } m.setBoundingSphere(boundingSphere); return m; } function createElementSphereImpostor(ctx, unit, structure, theme, props, spheres) { const { child } = structure; const childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id); if (child && !childUnit) return spheres_1.Spheres.createEmpty(spheres); const { sizeFactor, stride } = props; const { elements, conformation: c } = unit; const elementCount = elements.length; const builder = spheres_builder_1.SpheresBuilder.create(elementCount, elementCount / 2, spheres); const v = (0, linear_algebra_1.Vec3)(); const ignore = makeElementIgnoreTest(structure, unit, props); const l = structure_1.StructureElement.Location.create(structure, unit); const themeSize = theme.size.size; const center = (0, linear_algebra_1.Vec3)(); let maxSize = 0; let count = 0; if ((stride && stride > 1) || ignore || theme.size.granularity !== 'uniform') { for (let i = 0; i < elementCount; i++) { if (stride && i % stride !== 0) continue; if (ignore && ignore(elements[i])) continue; c.invariantPosition(elements[i], v); builder.add(v[0], v[1], v[2], i); v3add(center, center, v); count += 1; l.element = elements[i]; const size = themeSize(l); if (size > maxSize) maxSize = size; } } else { for (let i = 0; i < elementCount; i++) { c.invariantPosition(elements[i], v); builder.add(v[0], v[1], v[2], i); v3add(center, center, v); } count = elementCount; maxSize = themeSize(l); } const s = builder.getSpheres(); if (count === 0) return s; // re-use boundingSphere if it has not changed much let boundingSphere; linear_algebra_1.Vec3.scale(center, center, 1 / count); const oldBoundingSphere = spheres ? geometry_1.Sphere3D.clone(spheres.boundingSphere) : undefined; if (oldBoundingSphere && linear_algebra_1.Vec3.distance(center, oldBoundingSphere.center) / oldBoundingSphere.radius < 0.1) { boundingSphere = oldBoundingSphere; } else { boundingSphere = geometry_1.Sphere3D.expand((0, geometry_1.Sphere3D)(), (childUnit !== null && childUnit !== void 0 ? childUnit : unit).boundary.sphere, maxSize * sizeFactor + 0.05); } s.setBoundingSphere(boundingSphere); return s; } function eachElement(loci, structureGroup, apply) { let changed = false; if (!structure_1.StructureElement.Loci.is(loci)) return false; const { structure, group } = structureGroup; if (!structure_1.Structure.areEquivalent(loci.structure, structure)) return false; const elementCount = group.elements.length; const { unitIndexMap } = group; for (const e of loci.elements) { const unitIdx = unitIndexMap.get(e.unit.id); if (unitIdx !== undefined) { const offset = unitIdx * elementCount; // to target unit instance if (int_1.Interval.is(e.indices)) { const start = offset + int_1.Interval.start(e.indices); const end = offset + int_1.Interval.end(e.indices); if (apply(int_1.Interval.ofBounds(start, end))) changed = true; } else { for (let i = 0, _i = e.indices.length; i < _i; i++) { const start = e.indices[i]; let endI = i + 1; while (endI < _i && e.indices[endI] === start) endI++; i = endI - 1; const end = e.indices[i]; changed = apply(int_1.Interval.ofRange(offset + start, offset + end)) || changed; } } } } return changed; } function getElementLoci(pickingId, structureGroup, id) { const { objectId, instanceId, groupId } = pickingId; if (id === objectId) { const { structure, group } = structureGroup; const unit = group.units[instanceId]; const indices = int_1.OrderedSet.ofSingleton(groupId); return structure_1.StructureElement.Loci(structure.target, [{ unit, indices }]); } return loci_1.EmptyLoci; } // function createStructureElementSphereMesh(ctx, structure, theme, props, mesh) { const { child } = structure; const { detail, sizeFactor, stride } = props; const { getSerialIndex } = structure.serialMapping; const structureElementCount = structure.elementCount; const vertexCount = structureElementCount * (0, sphere_1.sphereVertexCount)(detail); const builderState = mesh_builder_1.MeshBuilder.createState(vertexCount, vertexCount / 2, mesh); const themeSize = theme.size.size; const center = (0, linear_algebra_1.Vec3)(); let maxSize = 0; let count = 0; for (const unit of structure.units) { const childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id); if (child && !childUnit) continue; const { elements, conformation: c } = unit; const elementCount = elements.length; const v = (0, linear_algebra_1.Vec3)(); const ignore = makeElementIgnoreTest(structure, unit, props); const l = structure_1.StructureElement.Location.create(structure, unit); for (let i = 0; i < elementCount; i++) { const eI = elements[i]; if (stride && i % stride !== 0) continue; if (ignore && ignore(eI)) continue; c.position(eI, v); v3add(center, center, v); count += 1; l.element = eI; const size = themeSize(l); if (size > maxSize) maxSize = size; builderState.currentGroup = getSerialIndex(unit, eI); (0, sphere_2.addSphere)(builderState, v, size * sizeFactor, detail); } } const m = mesh_builder_1.MeshBuilder.getMesh(builderState); if (count === 0) return m; // re-use boundingSphere if it has not changed much let boundingSphere; linear_algebra_1.Vec3.scale(center, center, 1 / count); const oldBoundingSphere = mesh ? geometry_1.Sphere3D.clone(mesh.boundingSphere) : undefined; if (oldBoundingSphere && linear_algebra_1.Vec3.distance(center, oldBoundingSphere.center) / oldBoundingSphere.radius < 1.0) { boundingSphere = oldBoundingSphere; } else { boundingSphere = geometry_1.Sphere3D.expand((0, geometry_1.Sphere3D)(), (child !== null && child !== void 0 ? child : structure).boundary.sphere, maxSize * sizeFactor + 0.05); } m.setBoundingSphere(boundingSphere); return m; } function createStructureElementSphereImpostor(ctx, structure, theme, props, spheres) { const { child } = structure; const { sizeFactor, stride } = props; const { getSerialIndex } = structure.serialMapping; const structureElementCount = structure.elementCount; const builder = spheres_builder_1.SpheresBuilder.create(structureElementCount, structureElementCount / 2, spheres); const themeSize = theme.size.size; const center = (0, linear_algebra_1.Vec3)(); let maxSize = 0; let count = 0; for (const unit of structure.units) { const childUnit = child === null || child === void 0 ? void 0 : child.unitMap.get(unit.id); if (child && !childUnit) continue; const { elements, conformation: c } = unit; const elementCount = elements.length; const v = (0, linear_algebra_1.Vec3)(); const ignore = makeElementIgnoreTest(structure, unit, props); const l = structure_1.StructureElement.Location.create(structure, unit); if ((stride && stride > 1) || ignore || theme.size.granularity !== 'uniform') { for (let i = 0; i < elementCount; i++) { const eI = elements[i]; if (stride && i % stride !== 0) continue; if (ignore && ignore(eI)) continue; c.position(eI, v); builder.add(v[0], v[1], v[2], getSerialIndex(unit, eI)); v3add(center, center, v); count += 1; l.element = eI; const size = themeSize(l); if (size > maxSize) maxSize = size; } } else { for (let i = 0; i < elementCount; i++) { const eI = elements[i]; c.position(eI, v); builder.add(v[0], v[1], v[2], getSerialIndex(unit, eI)); v3add(center, center, v); } count += elementCount; maxSize = themeSize(l); } } const s = builder.getSpheres(); if (count === 0) return s; // re-use boundingSphere if it has not changed much let boundingSphere; linear_algebra_1.Vec3.scale(center, center, 1 / count); const oldBoundingSphere = spheres ? geometry_1.Sphere3D.clone(spheres.boundingSphere) : undefined; if (oldBoundingSphere && linear_algebra_1.Vec3.distance(center, oldBoundingSphere.center) / oldBoundingSphere.radius < 1.0) { boundingSphere = oldBoundingSphere; } else { boundingSphere = geometry_1.Sphere3D.expand((0, geometry_1.Sphere3D)(), (child !== null && child !== void 0 ? child : structure).boundary.sphere, maxSize * sizeFactor + 0.05); } s.setBoundingSphere(boundingSphere); return s; } function eachSerialElement(loci, structure, apply) { let changed = false; if (!structure_1.StructureElement.Loci.is(loci)) return false; if (!structure_1.Structure.areEquivalent(loci.structure, structure)) return false; const { cumulativeUnitElementCount } = structure.serialMapping; for (const e of loci.elements) { const unitIdx = structure.unitIndexMap.get(e.unit.id); if (unitIdx !== undefined) { if (int_1.Interval.is(e.indices)) { const start = cumulativeUnitElementCount[unitIdx] + int_1.Interval.start(e.indices); const end = cumulativeUnitElementCount[unitIdx] + int_1.Interval.end(e.indices); if (apply(int_1.Interval.ofBounds(start, end))) changed = true; } else { for (let i = 0, _i = e.indices.length; i < _i; i++) { const idx = cumulativeUnitElementCount[unitIdx] + e.indices[i]; if (apply(int_1.Interval.ofSingleton(idx))) changed = true; } } } } return changed; } function getSerialElementLoci(pickingId, structure, id) { const { objectId, groupId } = pickingId; if (id === objectId) { const { unitIndices, cumulativeUnitElementCount } = structure.serialMapping; const unitIdx = unitIndices[groupId]; const unit = structure.units[unitIdx]; const idx = groupId - cumulativeUnitElementCount[unitIdx]; const indices = int_1.OrderedSet.ofSingleton(idx); return structure_1.StructureElement.Loci(structure, [{ unit, indices }]); } return loci_1.EmptyLoci; } // var ElementIterator; (function (ElementIterator) { function fromGroup(structureGroup) { const { group, structure } = structureGroup; const groupCount = group.elements.length; const instanceCount = group.units.length; const location = structure_1.StructureElement.Location.create(structure); const getLocation = (groupIndex, instanceIndex) => { const unit = group.units[instanceIndex]; location.unit = unit; location.element = unit.elements[groupIndex]; return location; }; return (0, location_iterator_1.LocationIterator)(groupCount, instanceCount, 1, getLocation); } ElementIterator.fromGroup = fromGroup; function fromStructure(structure) { const { units, elementCount } = structure; const groupCount = elementCount; const instanceCount = 1; const { unitIndices, elementIndices } = structure.serialMapping; const location = structure_1.StructureElement.Location.create(structure); const getLocation = (groupIndex) => { location.unit = units[unitIndices[groupIndex]]; location.element = elementIndices[groupIndex]; return location; }; return (0, location_iterator_1.LocationIterator)(groupCount, instanceCount, 1, getLocation, true); } ElementIterator.fromStructure = fromStructure; })(ElementIterator || (exports.ElementIterator = ElementIterator = {}));