molstar
Version:
A comprehensive macromolecular library.
101 lines (100 loc) • 5.7 kB
JavaScript
/**
* Copyright (c) 2019-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, UnitsMeshVisual } from '../units-visual';
import { MolecularSurfaceCalculationParams } from '../../../mol-math/geometry/molecular-surface';
import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
import { computeUnitMolecularSurface } from './util/molecular-surface';
import { computeMarchingCubesMesh } from '../../../mol-geo/util/marching-cubes/algorithm';
import { ElementIterator, getElementLoci, eachElement } from './util/element';
import { CommonSurfaceParams } from './util/common';
import { Sphere3D } from '../../../mol-math/geometry';
import { applyMeshColorSmoothing } from '../../../mol-geo/geometry/mesh/color-smoothing';
import { ColorSmoothingParams, getColorSmoothingProps } from '../../../mol-geo/geometry/base';
import { ValueCell } from '../../../mol-util';
export var MolecularSurfaceMeshParams = __assign(__assign(__assign(__assign({}, UnitsMeshParams), MolecularSurfaceCalculationParams), CommonSurfaceParams), ColorSmoothingParams);
//
function createMolecularSurfaceMesh(ctx, unit, structure, theme, props, mesh) {
return __awaiter(this, void 0, void 0, function () {
var _a, transform, field, idField, resolution, maxRadius, params, surface, iterations, sphere;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, computeUnitMolecularSurface(structure, unit, theme.size, props).runInContext(ctx.runtime)];
case 1:
_a = _b.sent(), transform = _a.transform, field = _a.field, idField = _a.idField, resolution = _a.resolution, maxRadius = _a.maxRadius;
params = {
isoLevel: props.probeRadius,
scalarField: field,
idField: idField
};
return [4 /*yield*/, computeMarchingCubesMesh(params, mesh).runAsChild(ctx.runtime)];
case 2:
surface = _b.sent();
if (props.includeParent) {
iterations = Math.ceil(2 / props.resolution);
Mesh.smoothEdges(surface, { iterations: iterations, maxNewEdgeLength: Math.sqrt(2) });
}
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);
surface.meta.resolution = resolution;
return [2 /*return*/, surface];
}
});
});
}
export function MolecularSurfaceMeshVisual(materialId) {
return UnitsMeshVisual({
defaultProps: PD.getDefaultValues(MolecularSurfaceMeshParams),
createGeometry: createMolecularSurfaceMesh,
createLocationIterator: ElementIterator.fromGroup,
getLoci: getElementLoci,
eachLocation: eachElement,
setUpdateState: function (state, newProps, currentProps) {
if (newProps.resolution !== currentProps.resolution)
state.createGeometry = true;
if (newProps.probeRadius !== currentProps.probeRadius)
state.createGeometry = true;
if (newProps.probePositions !== currentProps.probePositions)
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;
}
},
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);
}