UNPKG

molstar

Version:

A comprehensive macromolecular library.

222 lines (221 loc) 16 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.DirectVolume = void 0; var tslib_1 = require("tslib"); var util_1 = require("../../../mol-data/util"); var location_iterator_1 = require("../../../mol-geo/util/location-iterator"); var util_2 = require("../../../mol-gl/renderable/util"); var texture_1 = require("../../../mol-gl/webgl/texture"); var geometry_1 = require("../../../mol-math/geometry"); var linear_algebra_1 = require("../../../mol-math/linear-algebra"); var mol_util_1 = require("../../../mol-util"); var param_definition_1 = require("../../../mol-util/param-definition"); var box_1 = require("../../primitive/box"); var base_1 = require("../base"); var color_data_1 = require("../color-data"); var marker_data_1 = require("../marker-data"); var overpaint_data_1 = require("../overpaint-data"); var transparency_data_1 = require("../transparency-data"); var transfer_function_1 = require("./transfer-function"); var clipping_data_1 = require("../clipping-data"); var volume_1 = require("../../../mol-model/volume"); var substance_data_1 = require("../substance-data"); var VolumeBox = (0, box_1.Box)(); var DirectVolume; (function (DirectVolume) { function create(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder, directVolume) { return directVolume ? update(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder, directVolume) : fromData(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder); } DirectVolume.create = create; function hashCode(directVolume) { return (0, util_1.hashFnv32a)([ directVolume.bboxSize.ref.version, directVolume.gridDimension.ref.version, directVolume.gridTexture.ref.version, directVolume.transform.ref.version, directVolume.gridStats.ref.version ]); } function fromData(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder) { var boundingSphere = (0, geometry_1.Sphere3D)(); var currentHash = -1; var width = texture.getWidth(); var height = texture.getHeight(); var depth = texture.getDepth(); var directVolume = { kind: 'direct-volume', gridDimension: mol_util_1.ValueCell.create(gridDimension), gridTexture: mol_util_1.ValueCell.create(texture), gridTextureDim: mol_util_1.ValueCell.create(linear_algebra_1.Vec3.create(width, height, depth)), gridStats: mol_util_1.ValueCell.create(linear_algebra_1.Vec4.create(stats.min, stats.max, stats.mean, stats.sigma)), bboxMin: mol_util_1.ValueCell.create(bbox.min), bboxMax: mol_util_1.ValueCell.create(bbox.max), bboxSize: mol_util_1.ValueCell.create(linear_algebra_1.Vec3.sub((0, linear_algebra_1.Vec3)(), bbox.max, bbox.min)), transform: mol_util_1.ValueCell.create(transform), cellDim: mol_util_1.ValueCell.create(cellDim), unitToCartn: mol_util_1.ValueCell.create(unitToCartn), cartnToUnit: mol_util_1.ValueCell.create(linear_algebra_1.Mat4.invert((0, linear_algebra_1.Mat4)(), unitToCartn)), get boundingSphere() { var newHash = hashCode(directVolume); if (newHash !== currentHash) { var b = getBoundingSphere(directVolume.gridDimension.ref.value, directVolume.transform.ref.value); geometry_1.Sphere3D.copy(boundingSphere, b); currentHash = newHash; } return boundingSphere; }, packedGroup: mol_util_1.ValueCell.create(packedGroup), axisOrder: mol_util_1.ValueCell.create(axisOrder), setBoundingSphere: function (sphere) { geometry_1.Sphere3D.copy(boundingSphere, sphere); currentHash = hashCode(directVolume); } }; return directVolume; } function update(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder, directVolume) { var width = texture.getWidth(); var height = texture.getHeight(); var depth = texture.getDepth(); mol_util_1.ValueCell.update(directVolume.gridDimension, gridDimension); mol_util_1.ValueCell.update(directVolume.gridTexture, texture); mol_util_1.ValueCell.update(directVolume.gridTextureDim, linear_algebra_1.Vec3.set(directVolume.gridTextureDim.ref.value, width, height, depth)); mol_util_1.ValueCell.update(directVolume.gridStats, linear_algebra_1.Vec4.set(directVolume.gridStats.ref.value, stats.min, stats.max, stats.mean, stats.sigma)); mol_util_1.ValueCell.update(directVolume.bboxMin, bbox.min); mol_util_1.ValueCell.update(directVolume.bboxMax, bbox.max); mol_util_1.ValueCell.update(directVolume.bboxSize, linear_algebra_1.Vec3.sub(directVolume.bboxSize.ref.value, bbox.max, bbox.min)); mol_util_1.ValueCell.update(directVolume.transform, transform); mol_util_1.ValueCell.update(directVolume.cellDim, cellDim); mol_util_1.ValueCell.update(directVolume.unitToCartn, unitToCartn); mol_util_1.ValueCell.update(directVolume.cartnToUnit, linear_algebra_1.Mat4.invert((0, linear_algebra_1.Mat4)(), unitToCartn)); mol_util_1.ValueCell.updateIfChanged(directVolume.packedGroup, packedGroup); mol_util_1.ValueCell.updateIfChanged(directVolume.axisOrder, linear_algebra_1.Vec3.fromArray(directVolume.axisOrder.ref.value, axisOrder, 0)); return directVolume; } function createEmpty(directVolume) { var bbox = (0, geometry_1.Box3D)(); var gridDimension = (0, linear_algebra_1.Vec3)(); var transform = linear_algebra_1.Mat4.identity(); var unitToCartn = linear_algebra_1.Mat4.identity(); var cellDim = (0, linear_algebra_1.Vec3)(); var texture = (0, texture_1.createNullTexture)(); var stats = volume_1.Grid.One.stats; var packedGroup = false; var axisOrder = linear_algebra_1.Vec3.create(0, 1, 2); return create(bbox, gridDimension, transform, unitToCartn, cellDim, texture, stats, packedGroup, axisOrder, directVolume); } DirectVolume.createEmpty = createEmpty; DirectVolume.Params = tslib_1.__assign(tslib_1.__assign({}, base_1.BaseGeometry.Params), { ignoreLight: param_definition_1.ParamDefinition.Boolean(false, base_1.BaseGeometry.ShadingCategory), xrayShaded: param_definition_1.ParamDefinition.Boolean(false, base_1.BaseGeometry.ShadingCategory), controlPoints: param_definition_1.ParamDefinition.LineGraph([ linear_algebra_1.Vec2.create(0.19, 0.0), linear_algebra_1.Vec2.create(0.2, 0.05), linear_algebra_1.Vec2.create(0.25, 0.05), linear_algebra_1.Vec2.create(0.26, 0.0), linear_algebra_1.Vec2.create(0.79, 0.0), linear_algebra_1.Vec2.create(0.8, 0.05), linear_algebra_1.Vec2.create(0.85, 0.05), linear_algebra_1.Vec2.create(0.86, 0.0), ], { isEssential: true }), stepsPerCell: param_definition_1.ParamDefinition.Numeric(3, { min: 1, max: 10, step: 1 }), jumpLength: param_definition_1.ParamDefinition.Numeric(0, { min: 0, max: 20, step: 0.1 }) }); DirectVolume.Utils = { Params: DirectVolume.Params, createEmpty: createEmpty, createValues: createValues, createValuesSimple: createValuesSimple, updateValues: updateValues, updateBoundingSphere: updateBoundingSphere, createRenderableState: createRenderableState, updateRenderableState: updateRenderableState, createPositionIterator: createPositionIterator }; function createPositionIterator(directVolume, transform) { var t = directVolume.transform.ref.value; var _a = directVolume.gridDimension.ref.value, x = _a[0], y = _a[1], z = _a[2]; var groupCount = x * y * z; var instanceCount = transform.instanceCount.ref.value; var location = (0, location_iterator_1.PositionLocation)(); var p = location.position; var m = transform.aTransform.ref.value; var getLocation = function (groupIndex, instanceIndex) { var k = Math.floor(groupIndex / z); p[0] = Math.floor(k / y); p[1] = k % y; p[2] = groupIndex % z; linear_algebra_1.Vec3.transformMat4(p, p, t); if (instanceIndex >= 0) { linear_algebra_1.Vec3.transformMat4Offset(p, p, m, 0, 0, instanceIndex * 16); } return location; }; return (0, location_iterator_1.LocationIterator)(groupCount, instanceCount, 1, getLocation); } function getMaxSteps(gridDim, stepsPerCell) { return Math.ceil(linear_algebra_1.Vec3.magnitude(gridDim) * stepsPerCell); } function getStepScale(cellDim, stepsPerCell) { return Math.min.apply(Math, cellDim) * (1 / stepsPerCell); } function getTransferScale(stepsPerCell) { return (1 / stepsPerCell); } function createValues(directVolume, transform, locationIt, theme, props) { var gridTexture = directVolume.gridTexture, gridTextureDim = directVolume.gridTextureDim, gridStats = directVolume.gridStats; var bboxSize = directVolume.bboxSize, bboxMin = directVolume.bboxMin, bboxMax = directVolume.bboxMax, gridDimension = directVolume.gridDimension, gridTransform = directVolume.transform; var instanceCount = locationIt.instanceCount, groupCount = locationIt.groupCount; var positionIt = DirectVolume.Utils.createPositionIterator(directVolume, transform); var color = (0, color_data_1.createColors)(locationIt, positionIt, theme.color); var marker = props.instanceGranularity ? (0, marker_data_1.createMarkers)(instanceCount, 'instance') : (0, marker_data_1.createMarkers)(instanceCount * groupCount, 'groupInstance'); var overpaint = (0, overpaint_data_1.createEmptyOverpaint)(); var transparency = (0, transparency_data_1.createEmptyTransparency)(); var material = (0, substance_data_1.createEmptySubstance)(); var clipping = (0, clipping_data_1.createEmptyClipping)(); var _a = gridDimension.ref.value, x = _a[0], y = _a[1], z = _a[2]; var counts = { drawCount: VolumeBox.indices.length, vertexCount: x * y * z, groupCount: groupCount, instanceCount: instanceCount }; var invariantBoundingSphere = geometry_1.Sphere3D.clone(directVolume.boundingSphere); var boundingSphere = (0, util_2.calculateTransformBoundingSphere)(invariantBoundingSphere, transform.aTransform.ref.value, instanceCount); var controlPoints = (0, transfer_function_1.getControlPointsFromVec2Array)(props.controlPoints); var transferTex = (0, transfer_function_1.createTransferFunctionTexture)(controlPoints); return tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign(tslib_1.__assign({ dGeometryType: mol_util_1.ValueCell.create('directVolume') }, color), marker), overpaint), transparency), material), clipping), transform), base_1.BaseGeometry.createValues(props, counts)), { aPosition: mol_util_1.ValueCell.create(VolumeBox.vertices), elements: mol_util_1.ValueCell.create(VolumeBox.indices), boundingSphere: mol_util_1.ValueCell.create(boundingSphere), invariantBoundingSphere: mol_util_1.ValueCell.create(invariantBoundingSphere), uInvariantBoundingSphere: mol_util_1.ValueCell.create(linear_algebra_1.Vec4.ofSphere(invariantBoundingSphere)), uBboxMin: bboxMin, uBboxMax: bboxMax, uBboxSize: bboxSize, uMaxSteps: mol_util_1.ValueCell.create(getMaxSteps(gridDimension.ref.value, props.stepsPerCell)), uStepScale: mol_util_1.ValueCell.create(getStepScale(directVolume.cellDim.ref.value, props.stepsPerCell)), uJumpLength: mol_util_1.ValueCell.create(props.jumpLength), uTransform: gridTransform, uGridDim: gridDimension, tTransferTex: transferTex, uTransferScale: mol_util_1.ValueCell.create(getTransferScale(props.stepsPerCell)), dGridTexType: mol_util_1.ValueCell.create(gridTexture.ref.value.getDepth() > 0 ? '3d' : '2d'), uGridTexDim: gridTextureDim, tGridTex: gridTexture, uGridStats: gridStats, uCellDim: directVolume.cellDim, uCartnToUnit: directVolume.cartnToUnit, uUnitToCartn: directVolume.unitToCartn, dPackedGroup: directVolume.packedGroup, dAxisOrder: mol_util_1.ValueCell.create(directVolume.axisOrder.ref.value.join('')), dIgnoreLight: mol_util_1.ValueCell.create(props.ignoreLight), dXrayShaded: mol_util_1.ValueCell.create(props.xrayShaded) }); } function createValuesSimple(directVolume, props, colorValue, sizeValue, transform) { var s = base_1.BaseGeometry.createSimple(colorValue, sizeValue, transform); var p = tslib_1.__assign(tslib_1.__assign({}, param_definition_1.ParamDefinition.getDefaultValues(DirectVolume.Params)), props); return createValues(directVolume, s.transform, s.locationIterator, s.theme, p); } function updateValues(values, props) { base_1.BaseGeometry.updateValues(values, props); mol_util_1.ValueCell.updateIfChanged(values.dIgnoreLight, props.ignoreLight); mol_util_1.ValueCell.updateIfChanged(values.dXrayShaded, props.xrayShaded); var controlPoints = (0, transfer_function_1.getControlPointsFromVec2Array)(props.controlPoints); (0, transfer_function_1.createTransferFunctionTexture)(controlPoints, values.tTransferTex); mol_util_1.ValueCell.updateIfChanged(values.uMaxSteps, getMaxSteps(values.uGridDim.ref.value, props.stepsPerCell)); mol_util_1.ValueCell.updateIfChanged(values.uStepScale, getStepScale(values.uCellDim.ref.value, props.stepsPerCell)); mol_util_1.ValueCell.updateIfChanged(values.uTransferScale, getTransferScale(props.stepsPerCell)); mol_util_1.ValueCell.updateIfChanged(values.uJumpLength, props.jumpLength); } function updateBoundingSphere(values, directVolume) { var invariantBoundingSphere = geometry_1.Sphere3D.clone(directVolume.boundingSphere); var boundingSphere = (0, util_2.calculateTransformBoundingSphere)(invariantBoundingSphere, values.aTransform.ref.value, values.instanceCount.ref.value); if (!geometry_1.Sphere3D.equals(boundingSphere, values.boundingSphere.ref.value)) { mol_util_1.ValueCell.update(values.boundingSphere, boundingSphere); } if (!geometry_1.Sphere3D.equals(invariantBoundingSphere, values.invariantBoundingSphere.ref.value)) { mol_util_1.ValueCell.update(values.invariantBoundingSphere, invariantBoundingSphere); mol_util_1.ValueCell.update(values.uInvariantBoundingSphere, linear_algebra_1.Vec4.fromSphere(values.uInvariantBoundingSphere.ref.value, invariantBoundingSphere)); } } function createRenderableState(props) { var state = base_1.BaseGeometry.createRenderableState(props); state.opaque = false; state.writeDepth = false; return state; } function updateRenderableState(state, props) { base_1.BaseGeometry.updateRenderableState(state, props); state.opaque = false; state.writeDepth = false; } })(DirectVolume = exports.DirectVolume || (exports.DirectVolume = {})); // function getBoundingSphere(gridDimension, gridTransform) { return geometry_1.Sphere3D.fromDimensionsAndTransform((0, geometry_1.Sphere3D)(), gridDimension, gridTransform); }