/** * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose */ import { Unit, Structure } from 'mol-model/structure'; import { UnitsVisual, VisualUpdateState } from '..'; import { RuntimeContext } from 'mol-task' import { Mesh } from '../../../geometry/mesh/mesh'; import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual'; import { StructureElementIterator, getElementLoci, markElement } from './util/element'; import { computeMarchingCubesMesh } from '../../../util/marching-cubes/algorithm'; import { GaussianDensityProps, GaussianDensityParams } from 'mol-model/structure/structure/unit/gaussian-density'; import { paramDefaultValues } from 'mol-util/parameter'; async function createGaussianSurfaceMesh(ctx: RuntimeContext, unit: Unit, structure: Structure, props: GaussianDensityProps, mesh?: Mesh): Promise { const { smoothness } = props const { transform, field, idField } = await unit.computeGaussianDensity(props, ctx) const params = { isoLevel: Math.exp(-smoothness), scalarField: field, idField } const surface = await computeMarchingCubesMesh(params, mesh).runAsChild(ctx) Mesh.transformImmediate(surface, transform) Mesh.computeNormalsImmediate(surface) Mesh.uniformTriangleGroup(surface) return surface; } export const GaussianSurfaceParams = { ...UnitsMeshParams, ...GaussianDensityParams, } export const DefaultGaussianSurfaceProps = paramDefaultValues(GaussianSurfaceParams) export type GaussianSurfaceProps = typeof DefaultGaussianSurfaceProps export function GaussianSurfaceVisual(): UnitsVisual { return UnitsMeshVisual({ defaultProps: DefaultGaussianSurfaceProps, createGeometry: createGaussianSurfaceMesh, createLocationIterator: StructureElementIterator.fromGroup, getLoci: getElementLoci, mark: markElement, setUpdateState: (state: VisualUpdateState, newProps: GaussianSurfaceProps, currentProps: GaussianSurfaceProps) => { 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.useGpu !== currentProps.useGpu) state.createGeometry = true if (newProps.ignoreCache !== currentProps.ignoreCache) state.createGeometry = true } }) }