UNPKG

molstar

Version:

A comprehensive macromolecular library.

315 lines 17.8 kB
"use strict"; /** * Copyright (c) 2018-2021 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ComplexTextureMeshVisual = exports.ComplexTextureMeshParams = exports.ComplexDirectVolumeVisual = exports.ComplexDirectVolumeParams = exports.ComplexTextVisual = exports.ComplexTextParams = exports.ComplexLinesVisual = exports.ComplexLinesParams = exports.ComplexCylindersVisual = exports.ComplexCylindersParams = exports.ComplexMeshVisual = exports.ComplexMeshParams = exports.ComplexVisual = void 0; var tslib_1 = require("tslib"); var visual_1 = require("../visual"); var structure_1 = require("../../mol-model/structure"); var geometry_1 = require("../../mol-geo/geometry/geometry"); var theme_1 = require("../../mol-theme/theme"); var transform_data_1 = require("../../mol-geo/geometry/transform-data"); var render_object_1 = require("../../mol-gl/render-object"); var loci_1 = require("../../mol-model/loci"); var int_1 = require("../../mol-data/int"); var util_1 = require("../util"); var color_1 = require("../../mol-theme/color"); var mol_util_1 = require("../../mol-util"); var size_data_1 = require("../../mol-geo/geometry/size-data"); var color_data_1 = require("../../mol-geo/geometry/color-data"); var marker_action_1 = require("../../mol-util/marker-action"); var mesh_1 = require("../../mol-geo/geometry/mesh/mesh"); var cylinders_1 = require("../../mol-geo/geometry/cylinders/cylinders"); var lines_1 = require("../../mol-geo/geometry/lines/lines"); var text_1 = require("../../mol-geo/geometry/text/text"); var size_1 = require("../../mol-theme/size"); var direct_volume_1 = require("../../mol-geo/geometry/direct-volume/direct-volume"); var marker_data_1 = require("../../mol-geo/geometry/marker-data"); var params_1 = require("./params"); var texture_mesh_1 = require("../../mol-geo/geometry/texture-mesh/texture-mesh"); var type_helpers_1 = require("../../mol-util/type-helpers"); function createComplexRenderObject(structure, geometry, locationIt, theme, props, materialId) { var _a = geometry_1.Geometry.getUtils(geometry), createValues = _a.createValues, createRenderableState = _a.createRenderableState; var transform = (0, transform_data_1.createIdentityTransform)(); var values = createValues(geometry, transform, locationIt, theme, props); var state = createRenderableState(props); return (0, render_object_1.createRenderObject)(geometry.kind, values, state, materialId); } function ComplexVisual(builder, materialId) { var defaultProps = builder.defaultProps, createGeometry = builder.createGeometry, createLocationIterator = builder.createLocationIterator, getLoci = builder.getLoci, eachLocation = builder.eachLocation, setUpdateState = builder.setUpdateState, mustRecreate = builder.mustRecreate, processValues = builder.processValues, dispose = builder.dispose; var _a = builder.geometryUtils, updateValues = _a.updateValues, updateBoundingSphere = _a.updateBoundingSphere, updateRenderableState = _a.updateRenderableState, createPositionIterator = _a.createPositionIterator; var updateState = util_1.VisualUpdateState.create(); var previousMark = { loci: loci_1.EmptyLoci, action: marker_action_1.MarkerAction.None, status: -1 }; var renderObject; var newProps; var newTheme; var newStructure; var currentProps = Object.assign({}, defaultProps); var currentTheme = theme_1.Theme.createEmpty(); var currentStructure; var geometry; var locationIt; var positionIt; function prepareUpdate(theme, props, structure) { if (!structure && !currentStructure) { throw new Error('missing structure'); } newProps = Object.assign({}, currentProps, props); newTheme = theme; newStructure = structure; util_1.VisualUpdateState.reset(updateState); if (!renderObject || !currentStructure) { updateState.createNew = true; updateState.createGeometry = true; return; } setUpdateState(updateState, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure); if (!structure_1.Structure.areEquivalent(newStructure, currentStructure)) { updateState.createGeometry = true; } if (!structure_1.Structure.areHierarchiesEqual(newStructure, currentStructure)) { updateState.updateTransform = true; updateState.createGeometry = true; } if (!color_1.ColorTheme.areEqual(theme.color, currentTheme.color)) { updateState.updateColor = true; } if (!(0, mol_util_1.deepEqual)(newProps.unitKinds, currentProps.unitKinds)) { updateState.createGeometry = true; } if (currentStructure.child !== newStructure.child) { // console.log('new child'); updateState.createGeometry = true; } if (updateState.updateSize && !('uSize' in renderObject.values)) { updateState.createGeometry = true; } if (updateState.createGeometry) { updateState.updateColor = true; updateState.updateSize = true; } } function update(newGeometry) { if (updateState.createNew) { locationIt = createLocationIterator(newStructure); if (newGeometry) { renderObject = createComplexRenderObject(newStructure, newGeometry, locationIt, newTheme, newProps, materialId); positionIt = createPositionIterator(newGeometry, renderObject.values); } else { throw new Error('expected geometry to be given'); } } else { if (!renderObject) { throw new Error('expected renderObject to be available'); } if (updateState.updateTransform) { // console.log('update transform') locationIt = createLocationIterator(newStructure); var instanceCount = locationIt.instanceCount, groupCount = locationIt.groupCount; (0, marker_data_1.createMarkers)(instanceCount * groupCount, renderObject.values); } if (updateState.createGeometry) { if (newGeometry) { mol_util_1.ValueCell.updateIfChanged(renderObject.values.drawCount, geometry_1.Geometry.getDrawCount(newGeometry)); mol_util_1.ValueCell.updateIfChanged(renderObject.values.uVertexCount, geometry_1.Geometry.getVertexCount(newGeometry)); } else { throw new Error('expected geometry to be given'); } } if (updateState.updateTransform || updateState.createGeometry) { updateBoundingSphere(renderObject.values, newGeometry || geometry); positionIt = createPositionIterator(geometry, renderObject.values); } if (updateState.updateSize) { // not all geometries have size data, so check here if ('uSize' in renderObject.values) { (0, size_data_1.createSizes)(locationIt, newTheme.size, renderObject.values); } } if (updateState.updateColor) { (0, color_data_1.createColors)(locationIt, positionIt, newTheme.color, renderObject.values); } updateValues(renderObject.values, newProps); updateRenderableState(renderObject.state, newProps); } currentProps = newProps; currentTheme = newTheme; currentStructure = newStructure; if (newGeometry) geometry = newGeometry; } function lociIsSuperset(loci) { if ((0, loci_1.isEveryLoci)(loci)) return true; if (structure_1.Structure.isLoci(loci) && structure_1.Structure.areRootsEquivalent(loci.structure, currentStructure)) return true; if (structure_1.StructureElement.Loci.is(loci) && structure_1.Structure.areRootsEquivalent(loci.structure, currentStructure)) { if (structure_1.StructureElement.Loci.isWholeStructure(loci)) return true; } return false; } function lociApply(loci, apply, isMarking) { if (lociIsSuperset(loci)) { return apply(int_1.Interval.ofBounds(0, locationIt.groupCount * locationIt.instanceCount)); } else { return eachLocation(loci, currentStructure, apply, isMarking); } } function finalize(ctx) { if (renderObject) { processValues === null || processValues === void 0 ? void 0 : processValues(renderObject.values, geometry, currentProps, currentTheme, ctx.webgl); } } return { get groupCount() { return locationIt ? locationIt.count : 0; }, get renderObject() { return locationIt && locationIt.count ? renderObject : undefined; }, createOrUpdate: function (ctx, theme, props, structure) { if (props === void 0) { props = {}; } prepareUpdate(theme, props, structure || currentStructure); if (updateState.createGeometry) { var newGeometry = createGeometry(ctx, newStructure, newTheme, newProps, geometry); if ((0, type_helpers_1.isPromiseLike)(newGeometry)) { return newGeometry.then(function (g) { update(g); finalize(ctx); }); } update(newGeometry); } else { update(); } finalize(ctx); }, getLoci: function (pickingId) { return renderObject ? getLoci(pickingId, currentStructure, renderObject.id) : loci_1.EmptyLoci; }, mark: function (loci, action) { return visual_1.Visual.mark(renderObject, loci, action, lociApply, previousMark); }, setVisibility: function (visible) { visual_1.Visual.setVisibility(renderObject, visible); }, setAlphaFactor: function (alphaFactor) { visual_1.Visual.setAlphaFactor(renderObject, alphaFactor); }, setPickable: function (pickable) { visual_1.Visual.setPickable(renderObject, pickable); }, setColorOnly: function (colorOnly) { visual_1.Visual.setColorOnly(renderObject, colorOnly); }, setTransform: function (matrix, instanceMatrices) { visual_1.Visual.setTransform(renderObject, matrix, instanceMatrices); }, setOverpaint: function (overpaint) { visual_1.Visual.setOverpaint(renderObject, overpaint, lociApply, true); }, setTransparency: function (transparency) { visual_1.Visual.setTransparency(renderObject, transparency, lociApply, true); }, setClipping: function (clipping) { visual_1.Visual.setClipping(renderObject, clipping, lociApply, true); }, destroy: function () { dispose === null || dispose === void 0 ? void 0 : dispose(geometry); if (renderObject) { renderObject.state.disposed = true; renderObject = undefined; } }, mustRecreate: mustRecreate }; } exports.ComplexVisual = ComplexVisual; // mesh exports.ComplexMeshParams = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, params_1.StructureMeshParams), params_1.StructureParams); function ComplexMeshVisual(builder, materialId) { return ComplexVisual((0, tslib_1.__assign)((0, tslib_1.__assign)({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) { builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure); if (!size_1.SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true; }, geometryUtils: mesh_1.Mesh.Utils }), materialId); } exports.ComplexMeshVisual = ComplexMeshVisual; // cylinders exports.ComplexCylindersParams = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, params_1.StructureCylindersParams), params_1.StructureParams); function ComplexCylindersVisual(builder, materialId) { return ComplexVisual((0, tslib_1.__assign)((0, tslib_1.__assign)({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) { builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure); if (!size_1.SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true; }, geometryUtils: cylinders_1.Cylinders.Utils }), materialId); } exports.ComplexCylindersVisual = ComplexCylindersVisual; // lines exports.ComplexLinesParams = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, params_1.StructureLinesParams), params_1.StructureParams); function ComplexLinesVisual(builder, materialId) { return ComplexVisual((0, tslib_1.__assign)((0, tslib_1.__assign)({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) { builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure); if (!size_1.SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true; }, geometryUtils: lines_1.Lines.Utils }), materialId); } exports.ComplexLinesVisual = ComplexLinesVisual; // text exports.ComplexTextParams = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, params_1.StructureTextParams), params_1.StructureParams); function ComplexTextVisual(builder, materialId) { return ComplexVisual((0, tslib_1.__assign)((0, tslib_1.__assign)({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) { builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure); if (!size_1.SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true; if (newProps.background !== currentProps.background) state.createGeometry = true; if (newProps.backgroundMargin !== currentProps.backgroundMargin) state.createGeometry = true; if (newProps.tether !== currentProps.tether) state.createGeometry = true; if (newProps.tetherLength !== currentProps.tetherLength) state.createGeometry = true; if (newProps.tetherBaseWidth !== currentProps.tetherBaseWidth) state.createGeometry = true; if (newProps.attachment !== currentProps.attachment) state.createGeometry = true; if (newProps.fontFamily !== currentProps.fontFamily) state.createGeometry = true; if (newProps.fontQuality !== currentProps.fontQuality) state.createGeometry = true; if (newProps.fontStyle !== currentProps.fontStyle) state.createGeometry = true; if (newProps.fontVariant !== currentProps.fontVariant) state.createGeometry = true; if (newProps.fontWeight !== currentProps.fontWeight) state.createGeometry = true; }, geometryUtils: text_1.Text.Utils }), materialId); } exports.ComplexTextVisual = ComplexTextVisual; // direct-volume exports.ComplexDirectVolumeParams = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, params_1.StructureDirectVolumeParams), params_1.StructureParams); function ComplexDirectVolumeVisual(builder, materialId) { return ComplexVisual((0, tslib_1.__assign)((0, tslib_1.__assign)({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) { builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure); if (!size_1.SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true; }, geometryUtils: direct_volume_1.DirectVolume.Utils }), materialId); } exports.ComplexDirectVolumeVisual = ComplexDirectVolumeVisual; // texture-mesh exports.ComplexTextureMeshParams = (0, tslib_1.__assign)((0, tslib_1.__assign)({}, params_1.StructureTextureMeshParams), params_1.StructureParams); function ComplexTextureMeshVisual(builder, materialId) { return ComplexVisual((0, tslib_1.__assign)((0, tslib_1.__assign)({}, builder), { setUpdateState: function (state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure) { builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme, newStructure, currentStructure); if (!size_1.SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true; }, geometryUtils: texture_mesh_1.TextureMesh.Utils }), materialId); } exports.ComplexTextureMeshVisual = ComplexTextureMeshVisual; //# sourceMappingURL=complex-visual.js.map