UNPKG

molstar

Version:

A comprehensive macromolecular library.

392 lines (391 loc) 24.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, __awaiter, __generator } from "tslib"; import { ParamDefinition as PD } from '../../../mol-util/param-definition'; import { UnitsMeshParams, UnitsTextureMeshParams, UnitsMeshVisual, UnitsTextureMeshVisual } from '../units-visual'; import { GaussianDensityParams, computeUnitGaussianDensity, computeUnitGaussianDensityTexture2d, computeStructureGaussianDensity, computeStructureGaussianDensityTexture2d } from './util/gaussian'; import { Mesh } from '../../../mol-geo/geometry/mesh/mesh'; import { computeMarchingCubesMesh } from '../../../mol-geo/util/marching-cubes/algorithm'; import { ElementIterator, getElementLoci, eachElement, getSerialElementLoci, eachSerialElement } from './util/element'; import { TextureMesh } from '../../../mol-geo/geometry/texture-mesh/texture-mesh'; import { extractIsosurface } from '../../../mol-gl/compute/marching-cubes/isosurface'; import { Sphere3D } from '../../../mol-math/geometry'; import { ComplexMeshParams, ComplexMeshVisual, ComplexTextureMeshVisual, ComplexTextureMeshParams } from '../complex-visual'; import { getVolumeSliceInfo } from './util/common'; import { applyMeshColorSmoothing } from '../../../mol-geo/geometry/mesh/color-smoothing'; import { applyTextureMeshColorSmoothing } from '../../../mol-geo/geometry/texture-mesh/color-smoothing'; import { ColorSmoothingParams, getColorSmoothingProps } from '../../../mol-geo/geometry/base'; import { Vec3 } from '../../../mol-math/linear-algebra'; import { isTimingMode } from '../../../mol-util/debug'; import { ValueCell } from '../../../mol-util/value-cell'; var SharedParams = __assign(__assign(__assign({}, GaussianDensityParams), ColorSmoothingParams), { ignoreHydrogens: PD.Boolean(false), tryUseGpu: PD.Boolean(true), includeParent: PD.Boolean(false, { isHidden: true }) }); export var GaussianSurfaceMeshParams = __assign(__assign(__assign({}, UnitsMeshParams), UnitsTextureMeshParams), SharedParams); export var StructureGaussianSurfaceMeshParams = __assign(__assign(__assign({}, ComplexMeshParams), ComplexTextureMeshParams), SharedParams); function gpuSupport(webgl) { return webgl.extensions.colorBufferFloat && webgl.extensions.textureFloat && webgl.extensions.blendMinMax && webgl.extensions.drawBuffers; } function suitableForGpu(structure, props, webgl) { // lower resolutions are about as fast on CPU vs integrated GPU, // very low resolutions have artifacts when calculated on GPU if (props.resolution > 1) return false; // the GPU is much more memory contraint, especially true for integrated GPUs, // being conservative here still allows for small and medium sized assemblies var d = webgl.maxTextureSize / 3; var _a = getVolumeSliceInfo(structure.boundary.box, props.resolution, d * d), areaCells = _a.areaCells, maxAreaCells = _a.maxAreaCells; return areaCells < maxAreaCells; } export function GaussianSurfaceVisual(materialId, structure, props, webgl) { if (props.tryUseGpu && webgl && gpuSupport(webgl) && suitableForGpu(structure, props, webgl)) { return GaussianSurfaceTextureMeshVisual(materialId); } return GaussianSurfaceMeshVisual(materialId); } export function StructureGaussianSurfaceVisual(materialId, structure, props, webgl) { if (props.tryUseGpu && webgl && gpuSupport(webgl) && suitableForGpu(structure, props, webgl)) { return StructureGaussianSurfaceTextureMeshVisual(materialId); } return StructureGaussianSurfaceMeshVisual(materialId); } // function createGaussianSurfaceMesh(ctx, unit, structure, theme, props, mesh) { return __awaiter(this, void 0, void 0, function () { var smoothness, _a, transform, field, idField, radiusFactor, resolution, maxRadius, params, surface, sphere; return __generator(this, function (_b) { switch (_b.label) { case 0: smoothness = props.smoothness; return [4 /*yield*/, computeUnitGaussianDensity(structure, unit, theme.size, props).runInContext(ctx.runtime)]; case 1: _a = _b.sent(), transform = _a.transform, field = _a.field, idField = _a.idField, radiusFactor = _a.radiusFactor, resolution = _a.resolution, maxRadius = _a.maxRadius; params = { isoLevel: Math.exp(-smoothness) / radiusFactor, scalarField: field, idField: idField }; return [4 /*yield*/, computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime)]; case 2: surface = _b.sent(); surface.meta.resolution = resolution; Mesh.transform(surface, transform); if (ctx.webgl && !ctx.webgl.isWebGL2) { Mesh.uniformTriangleGroup(surface); ValueCell.updateIfChanged(surface.varyingGroup, false); } else { ValueCell.updateIfChanged(surface.varyingGroup, true); } sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, maxRadius); surface.setBoundingSphere(sphere); return [2 /*return*/, surface]; } }); }); } export function GaussianSurfaceMeshVisual(materialId) { return UnitsMeshVisual({ defaultProps: PD.getDefaultValues(GaussianSurfaceMeshParams), createGeometry: createGaussianSurfaceMesh, createLocationIterator: ElementIterator.fromGroup, getLoci: getElementLoci, eachLocation: eachElement, setUpdateState: function (state, newProps, currentProps) { if (newProps.resolution !== currentProps.resolution) state.createGeometry = true; if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true; if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true; if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true; if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true; if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true; if (newProps.smoothColors.name !== currentProps.smoothColors.name) { state.updateColor = true; } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') { if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true; if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true; } }, mustRecreate: function (structureGroup, props, webgl) { return props.tryUseGpu && !!webgl && suitableForGpu(structureGroup.structure, props, webgl); }, processValues: function (values, geometry, props, theme, webgl) { var _a = geometry.meta, resolution = _a.resolution, colorTexture = _a.colorTexture; var csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution); if (csp) { applyMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture); geometry.meta.colorTexture = values.tColorGrid.ref.value; } }, dispose: function (geometry) { var _a; (_a = geometry.meta.colorTexture) === null || _a === void 0 ? void 0 : _a.destroy(); } }, materialId); } // function createStructureGaussianSurfaceMesh(ctx, structure, theme, props, mesh) { return __awaiter(this, void 0, void 0, function () { var smoothness, _a, transform, field, idField, radiusFactor, resolution, maxRadius, params, surface, sphere; return __generator(this, function (_b) { switch (_b.label) { case 0: smoothness = props.smoothness; return [4 /*yield*/, computeStructureGaussianDensity(structure, theme.size, props).runInContext(ctx.runtime)]; case 1: _a = _b.sent(), transform = _a.transform, field = _a.field, idField = _a.idField, radiusFactor = _a.radiusFactor, resolution = _a.resolution, maxRadius = _a.maxRadius; params = { isoLevel: Math.exp(-smoothness) / radiusFactor, scalarField: field, idField: idField }; return [4 /*yield*/, computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime)]; case 2: surface = _b.sent(); surface.meta.resolution = resolution; Mesh.transform(surface, transform); if (ctx.webgl && !ctx.webgl.isWebGL2) { Mesh.uniformTriangleGroup(surface); ValueCell.updateIfChanged(surface.varyingGroup, false); } else { ValueCell.updateIfChanged(surface.varyingGroup, true); } sphere = Sphere3D.expand(Sphere3D(), structure.boundary.sphere, maxRadius); surface.setBoundingSphere(sphere); return [2 /*return*/, surface]; } }); }); } export function StructureGaussianSurfaceMeshVisual(materialId) { return ComplexMeshVisual({ defaultProps: PD.getDefaultValues(StructureGaussianSurfaceMeshParams), createGeometry: createStructureGaussianSurfaceMesh, createLocationIterator: ElementIterator.fromStructure, getLoci: getSerialElementLoci, eachLocation: eachSerialElement, setUpdateState: function (state, newProps, currentProps) { if (newProps.resolution !== currentProps.resolution) state.createGeometry = true; if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true; if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true; if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true; if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true; if (newProps.smoothColors.name !== currentProps.smoothColors.name) { state.updateColor = true; } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') { if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true; if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true; } }, mustRecreate: function (structure, props, webgl) { return props.tryUseGpu && !!webgl && suitableForGpu(structure, props, webgl); }, processValues: function (values, geometry, props, theme, webgl) { var _a = geometry.meta, resolution = _a.resolution, colorTexture = _a.colorTexture; var csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution); if (csp) { applyMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture); geometry.meta.colorTexture = values.tColorGrid.ref.value; } }, dispose: function (geometry) { var _a; (_a = geometry.meta.colorTexture) === null || _a === void 0 ? void 0 : _a.destroy(); } }, materialId); } // var GaussianSurfaceName = 'gaussian-surface'; function createGaussianSurfaceTextureMesh(ctx, unit, structure, theme, props, textureMesh) { return __awaiter(this, void 0, void 0, function () { var _a, namedTextures, resources, _b, colorBufferFloat, textureFloat, colorBufferHalfFloat, textureHalfFloat, densityTextureData, isoLevel, axisOrder, buffer, gv, groupCount, boundingSphere, surface; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!ctx.webgl) throw new Error('webgl context required to create gaussian surface texture-mesh'); if (isTimingMode) ctx.webgl.timer.mark('createGaussianSurfaceTextureMesh'); _a = ctx.webgl, namedTextures = _a.namedTextures, resources = _a.resources, _b = _a.extensions, colorBufferFloat = _b.colorBufferFloat, textureFloat = _b.textureFloat, colorBufferHalfFloat = _b.colorBufferHalfFloat, textureHalfFloat = _b.textureHalfFloat; if (!namedTextures[GaussianSurfaceName]) { namedTextures[GaussianSurfaceName] = colorBufferHalfFloat && textureHalfFloat ? resources.texture('image-float16', 'rgba', 'fp16', 'linear') : colorBufferFloat && textureFloat ? resources.texture('image-float32', 'rgba', 'float', 'linear') : resources.texture('image-uint8', 'rgba', 'ubyte', 'linear'); } return [4 /*yield*/, computeUnitGaussianDensityTexture2d(structure, unit, theme.size, true, props, ctx.webgl, namedTextures[GaussianSurfaceName]).runInContext(ctx.runtime)]; case 1: densityTextureData = _c.sent(); isoLevel = Math.exp(-props.smoothness) / densityTextureData.radiusFactor; axisOrder = Vec3.create(0, 1, 2); buffer = textureMesh === null || textureMesh === void 0 ? void 0 : textureMesh.doubleBuffer.get(); gv = extractIsosurface(ctx.webgl, densityTextureData.texture, densityTextureData.gridDim, densityTextureData.gridTexDim, densityTextureData.gridTexScale, densityTextureData.transform, isoLevel, false, true, axisOrder, true, buffer === null || buffer === void 0 ? void 0 : buffer.vertex, buffer === null || buffer === void 0 ? void 0 : buffer.group, buffer === null || buffer === void 0 ? void 0 : buffer.normal); if (isTimingMode) ctx.webgl.timer.markEnd('createGaussianSurfaceTextureMesh'); groupCount = unit.elements.length; boundingSphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, densityTextureData.maxRadius); surface = TextureMesh.create(gv.vertexCount, groupCount, gv.vertexTexture, gv.groupTexture, gv.normalTexture, boundingSphere, textureMesh); surface.meta.resolution = densityTextureData.resolution; return [2 /*return*/, surface]; } }); }); } export function GaussianSurfaceTextureMeshVisual(materialId) { return UnitsTextureMeshVisual({ defaultProps: PD.getDefaultValues(GaussianSurfaceMeshParams), createGeometry: createGaussianSurfaceTextureMesh, createLocationIterator: ElementIterator.fromGroup, getLoci: getElementLoci, eachLocation: eachElement, setUpdateState: function (state, newProps, currentProps) { if (newProps.resolution !== currentProps.resolution) state.createGeometry = true; if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true; if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true; if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true; if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true; if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true; if (newProps.smoothColors.name !== currentProps.smoothColors.name) { state.updateColor = true; } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') { if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true; if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true; } }, mustRecreate: function (structureGroup, props, webgl) { return !props.tryUseGpu || !webgl || !suitableForGpu(structureGroup.structure, props, webgl); }, processValues: function (values, geometry, props, theme, webgl) { var _a = geometry.meta, resolution = _a.resolution, colorTexture = _a.colorTexture; var csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution); if (csp && webgl) { applyTextureMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture); geometry.meta.colorTexture = values.tColorGrid.ref.value; } }, dispose: function (geometry) { var _a; geometry.vertexTexture.ref.value.destroy(); geometry.groupTexture.ref.value.destroy(); geometry.normalTexture.ref.value.destroy(); geometry.doubleBuffer.destroy(); (_a = geometry.meta.colorTexture) === null || _a === void 0 ? void 0 : _a.destroy(); } }, materialId); } // function createStructureGaussianSurfaceTextureMesh(ctx, structure, theme, props, textureMesh) { return __awaiter(this, void 0, void 0, function () { var _a, namedTextures, resources, _b, colorBufferFloat, textureFloat, colorBufferHalfFloat, textureHalfFloat, densityTextureData, isoLevel, axisOrder, buffer, gv, groupCount, boundingSphere, surface; return __generator(this, function (_c) { switch (_c.label) { case 0: if (!ctx.webgl) throw new Error('webgl context required to create structure gaussian surface texture-mesh'); if (isTimingMode) ctx.webgl.timer.mark('createStructureGaussianSurfaceTextureMesh'); _a = ctx.webgl, namedTextures = _a.namedTextures, resources = _a.resources, _b = _a.extensions, colorBufferFloat = _b.colorBufferFloat, textureFloat = _b.textureFloat, colorBufferHalfFloat = _b.colorBufferHalfFloat, textureHalfFloat = _b.textureHalfFloat; if (!namedTextures[GaussianSurfaceName]) { namedTextures[GaussianSurfaceName] = colorBufferHalfFloat && textureHalfFloat ? resources.texture('image-float16', 'rgba', 'fp16', 'linear') : colorBufferFloat && textureFloat ? resources.texture('image-float32', 'rgba', 'float', 'linear') : resources.texture('image-uint8', 'rgba', 'ubyte', 'linear'); } return [4 /*yield*/, computeStructureGaussianDensityTexture2d(structure, theme.size, true, props, ctx.webgl, namedTextures[GaussianSurfaceName]).runInContext(ctx.runtime)]; case 1: densityTextureData = _c.sent(); isoLevel = Math.exp(-props.smoothness) / densityTextureData.radiusFactor; axisOrder = Vec3.create(0, 1, 2); buffer = textureMesh === null || textureMesh === void 0 ? void 0 : textureMesh.doubleBuffer.get(); gv = extractIsosurface(ctx.webgl, densityTextureData.texture, densityTextureData.gridDim, densityTextureData.gridTexDim, densityTextureData.gridTexScale, densityTextureData.transform, isoLevel, false, true, axisOrder, true, buffer === null || buffer === void 0 ? void 0 : buffer.vertex, buffer === null || buffer === void 0 ? void 0 : buffer.group, buffer === null || buffer === void 0 ? void 0 : buffer.normal); if (isTimingMode) ctx.webgl.timer.markEnd('createStructureGaussianSurfaceTextureMesh'); groupCount = structure.elementCount; boundingSphere = Sphere3D.expand(Sphere3D(), structure.boundary.sphere, densityTextureData.maxRadius); surface = TextureMesh.create(gv.vertexCount, groupCount, gv.vertexTexture, gv.groupTexture, gv.normalTexture, boundingSphere, textureMesh); surface.meta.resolution = densityTextureData.resolution; return [2 /*return*/, surface]; } }); }); } export function StructureGaussianSurfaceTextureMeshVisual(materialId) { return ComplexTextureMeshVisual({ defaultProps: PD.getDefaultValues(StructureGaussianSurfaceMeshParams), createGeometry: createStructureGaussianSurfaceTextureMesh, createLocationIterator: ElementIterator.fromStructure, getLoci: getSerialElementLoci, eachLocation: eachSerialElement, setUpdateState: function (state, newProps, currentProps) { if (newProps.resolution !== currentProps.resolution) state.createGeometry = true; if (newProps.radiusOffset !== currentProps.radiusOffset) state.createGeometry = true; if (newProps.smoothness !== currentProps.smoothness) state.createGeometry = true; if (newProps.ignoreHydrogens !== currentProps.ignoreHydrogens) state.createGeometry = true; if (newProps.traceOnly !== currentProps.traceOnly) state.createGeometry = true; if (newProps.includeParent !== currentProps.includeParent) state.createGeometry = true; if (newProps.smoothColors.name !== currentProps.smoothColors.name) { state.updateColor = true; } else if (newProps.smoothColors.name === 'on' && currentProps.smoothColors.name === 'on') { if (newProps.smoothColors.params.resolutionFactor !== currentProps.smoothColors.params.resolutionFactor) state.updateColor = true; if (newProps.smoothColors.params.sampleStride !== currentProps.smoothColors.params.sampleStride) state.updateColor = true; } }, mustRecreate: function (structure, props, webgl) { return !props.tryUseGpu || !webgl || !suitableForGpu(structure, props, webgl); }, processValues: function (values, geometry, props, theme, webgl) { var _a = geometry.meta, resolution = _a.resolution, colorTexture = _a.colorTexture; var csp = getColorSmoothingProps(props.smoothColors, theme.color.preferSmoothing, resolution); if (csp && webgl) { applyTextureMeshColorSmoothing(values, csp.resolution, csp.stride, webgl, colorTexture); geometry.meta.colorTexture = values.tColorGrid.ref.value; } }, dispose: function (geometry) { var _a; geometry.vertexTexture.ref.value.destroy(); geometry.groupTexture.ref.value.destroy(); geometry.normalTexture.ref.value.destroy(); geometry.doubleBuffer.destroy(); (_a = geometry.meta.colorTexture) === null || _a === void 0 ? void 0 : _a.destroy(); } }, materialId); }