molstar
Version:
A comprehensive macromolecular library.
95 lines (94 loc) • 4.68 kB
JavaScript
/**
* Copyright (c) 2018-2026 mol* contributors, licensed under MIT, See LICENSE file for more info.
*
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @author Gianluca Tomasello <giagitom@gmail.com>
*/
import { ParamDefinition as PD } from '../../../mol-util/param-definition.js';
import { Lines } from '../../../mol-geo/geometry/lines/lines.js';
import { computeStructureGaussianDensity, computeUnitGaussianDensity, GaussianDensityParams } from './util/gaussian.js';
import { computeMarchingCubesLines } from '../../../mol-geo/util/marching-cubes/algorithm.js';
import { UnitsLinesParams, UnitsLinesVisual } from '../units-visual.js';
import { ElementIterator, getElementLoci, eachElement, getSerialElementLoci, eachSerialElement } from './util/element.js';
import { Sphere3D } from '../../../mol-math/geometry.js';
import { ComplexLinesParams, ComplexLinesVisual } from '../complex-visual.js';
const SharedParams = {
...GaussianDensityParams,
sizeFactor: PD.Numeric(3, { min: 0, max: 10, step: 0.1 }),
lineSizeAttenuation: PD.Boolean(false),
};
export const GaussianWireframeParams = {
...UnitsLinesParams,
...SharedParams,
};
export const StructureGaussianWireframeParams = {
...ComplexLinesParams,
...SharedParams,
};
async function createGaussianWireframe(ctx, unit, structure, theme, props, lines) {
const { smoothness } = props;
const { transform, field, idField, maxRadius } = await computeUnitGaussianDensity(structure, unit, theme.size, props).runInContext(ctx.runtime);
const params = {
isoLevel: Math.exp(-smoothness),
scalarField: field,
idField
};
const wireframe = await computeMarchingCubesLines(params, lines).runAsChild(ctx.runtime);
Lines.transform(wireframe, transform);
const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, maxRadius);
wireframe.setBoundingSphere(sphere);
return wireframe;
}
export function GaussianWireframeVisual(materialId) {
return UnitsLinesVisual({
defaultProps: PD.getDefaultValues(GaussianWireframeParams),
createGeometry: createGaussianWireframe,
createLocationIterator: ElementIterator.fromGroup,
getLoci: getElementLoci,
eachLocation: eachElement,
setUpdateState: (state, newProps, currentProps) => {
state.createGeometry = (newProps.resolution !== currentProps.resolution ||
newProps.radiusOffset !== currentProps.radiusOffset ||
newProps.smoothness !== currentProps.smoothness ||
newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
newProps.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant ||
newProps.traceOnly !== currentProps.traceOnly ||
newProps.includeParent !== currentProps.includeParent ||
newProps.floodfill !== currentProps.floodfill);
}
}, materialId);
}
//
async function createStructureGaussianWireframe(ctx, structure, theme, props, lines) {
const { smoothness } = props;
const { transform, field, idField, maxRadius } = await computeStructureGaussianDensity(structure, theme.size, props).runInContext(ctx.runtime);
const params = {
isoLevel: Math.exp(-smoothness),
scalarField: field,
idField
};
const wireframe = await computeMarchingCubesLines(params, lines).runAsChild(ctx.runtime);
Lines.transform(wireframe, transform);
const sphere = Sphere3D.expand(Sphere3D(), structure.boundary.sphere, maxRadius);
wireframe.setBoundingSphere(sphere);
return wireframe;
}
export function StructureGaussianWireframeVisual(materialId) {
return ComplexLinesVisual({
defaultProps: PD.getDefaultValues(StructureGaussianWireframeParams),
createGeometry: createStructureGaussianWireframe,
createLocationIterator: ElementIterator.fromStructure,
getLoci: getSerialElementLoci,
eachLocation: eachSerialElement,
setUpdateState: (state, newProps, currentProps) => {
state.createGeometry = (newProps.resolution !== currentProps.resolution ||
newProps.radiusOffset !== currentProps.radiusOffset ||
newProps.smoothness !== currentProps.smoothness ||
newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
newProps.ignoreHydrogensVariant !== currentProps.ignoreHydrogensVariant ||
newProps.traceOnly !== currentProps.traceOnly ||
newProps.includeParent !== currentProps.includeParent ||
newProps.floodfill !== currentProps.floodfill);
}
}, materialId);
}