UNPKG

molstar

Version:

A comprehensive macromolecular library.

113 lines (112 loc) 5.25 kB
/** * Copyright (c) 2019 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 { UnitsMeshParams, UnitsMeshVisual } from '../../../mol-repr/structure/units-visual'; import { Unit, Structure, StructureElement } from '../../../mol-model/structure'; import { Mesh } from '../../../mol-geo/geometry/mesh/mesh'; import { MeshBuilder } from '../../../mol-geo/geometry/mesh/mesh-builder'; import { Vec3 } from '../../../mol-math/linear-algebra'; import { addEllipsoid } from '../../../mol-geo/geometry/mesh/builder/ellipsoid'; import { Axes3D, Sphere3D } from '../../../mol-math/geometry'; import { OrderedSet, Interval } from '../../../mol-data/int'; import { EmptyLoci } from '../../../mol-model/loci'; import { LocationIterator } from '../../../mol-geo/util/location-iterator'; import { BaseGeometry } from '../../../mol-geo/geometry/base'; export var OrientationEllipsoidMeshParams = __assign(__assign({}, UnitsMeshParams), { sizeFactor: PD.Numeric(1, { min: 0, max: 2, step: 0.1 }), detail: PD.Numeric(0, { min: 0, max: 3, step: 1 }, BaseGeometry.CustomQualityParamInfo) }); export function OrientationEllipsoidMeshVisual(materialId) { return UnitsMeshVisual({ defaultProps: PD.getDefaultValues(OrientationEllipsoidMeshParams), createGeometry: createOrientationEllipsoidMesh, createLocationIterator: UnitIterator, getLoci: getUnitLoci, eachLocation: eachUnit, setUpdateState: function (state, newProps, currentProps) { state.createGeometry = (newProps.sizeFactor !== currentProps.sizeFactor || newProps.detail !== currentProps.detail); } }, materialId); } function isUnitApplicable(unit) { if (Unit.Traits.is(unit.traits, 1 /* Unit.Trait.MultiChain */)) return false; if (Unit.Traits.is(unit.traits, 2 /* Unit.Trait.Partitioned */)) return false; if (Unit.isCoarse(unit)) return true; if (unit.elements.length === 0) return false; unit.model.atomicHierarchy.derived.residue.moleculeType; var rI = unit.residueIndex[unit.elements[0]]; var mt = unit.model.atomicHierarchy.derived.residue.moleculeType[rI]; if (mt === 3 /* MoleculeType.Ion */) return false; if (mt === 2 /* MoleculeType.Water */) return false; return true; } export function createOrientationEllipsoidMesh(ctx, unit, structure, theme, props, mesh) { if (!isUnitApplicable(unit)) return Mesh.createEmpty(mesh); var detail = props.detail, sizeFactor = props.sizeFactor; var vertexCount = 256; var builderState = MeshBuilder.createState(vertexCount, vertexCount / 2, mesh); var axes = unit.principalAxes.boxAxes; var origin = axes.origin, dirA = axes.dirA, dirB = axes.dirB; var size = Axes3D.size(Vec3(), axes); Vec3.scale(size, size, sizeFactor / 2); var radiusScale = Vec3.create(size[2], size[1], size[0]); builderState.currentGroup = 0; addEllipsoid(builderState, origin, dirA, dirB, radiusScale, detail + 1); var m = MeshBuilder.getMesh(builderState); var sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * props.sizeFactor); m.setBoundingSphere(sphere); return m; } // function UnitIterator(structureGroup) { var group = structureGroup.group, structure = structureGroup.structure; var groupCount = 1; var instanceCount = group.units.length; var location = StructureElement.Location.create(structure); var getLocation = function (groupIndex, instanceIndex) { var unit = group.units[instanceIndex]; location.unit = unit; location.element = unit.elements[groupIndex]; return location; }; return LocationIterator(groupCount, instanceCount, 1, getLocation); } function getUnitLoci(pickingId, structureGroup, id) { var objectId = pickingId.objectId, instanceId = pickingId.instanceId; if (id === objectId) { var structure = structureGroup.structure, group = structureGroup.group; var unit = group.units[instanceId]; var indices = OrderedSet.ofBounds(0, unit.elements.length); return StructureElement.Loci(structure, [{ unit: unit, indices: indices }]); } return EmptyLoci; } function eachUnit(loci, structureGroup, apply) { var changed = false; if (!StructureElement.Loci.is(loci)) return false; var structure = structureGroup.structure, group = structureGroup.group; if (!Structure.areEquivalent(loci.structure, structure)) return false; var elementCount = group.elements.length; for (var _i = 0, _a = loci.elements; _i < _a.length; _i++) { var e = _a[_i]; var unitIdx = group.unitIndexMap.get(e.unit.id); if (unitIdx !== undefined) { if (OrderedSet.size(e.indices) === elementCount) { if (apply(Interval.ofSingleton(unitIdx))) changed = true; } } } return changed; }