Skip to content
Snippets Groups Projects
gaussian-surface-wireframe.ts 2.85 KiB
Newer Older
Alexander Rose's avatar
Alexander Rose committed
/**
 * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
 *
 * @author Alexander Rose <alexander.rose@weirdbyte.de>
 */

import { Unit, Structure } from 'mol-model/structure';
Alexander Rose's avatar
Alexander Rose committed
import { UnitsVisual } from '../index';
import { VisualUpdateState } from '../../util';
Alexander Rose's avatar
Alexander Rose committed
import { UnitsLinesVisual, UnitsLinesParams } from '../units-visual';
Alexander Rose's avatar
Alexander Rose committed
import { StructureElementIterator, getElementLoci, markElement } from './util/element';
Alexander Rose's avatar
Alexander Rose committed
import { GaussianDensityProps, GaussianDensityParams } from 'mol-model/structure/structure/unit/gaussian-density';
import { ParamDefinition as PD } from 'mol-util/param-definition';
import { SizeThemeName, SizeThemeOptions } from 'mol-theme/size';
import { Lines } from 'mol-geo/geometry/lines/lines';
import { computeMarchingCubesLines } from 'mol-geo/util/marching-cubes/algorithm';
import { VisualContext } from 'mol-repr';
Alexander Rose's avatar
Alexander Rose committed

async function createGaussianWireframe(ctx: VisualContext, unit: Unit, structure: Structure, props: GaussianDensityProps, lines?: Lines): Promise<Lines> {
Alexander Rose's avatar
Alexander Rose committed
    const { smoothness } = props
    const { transform, field, idField } = await unit.computeGaussianDensity(props, ctx.runtime)
Alexander Rose's avatar
Alexander Rose committed

    const params = {
Alexander Rose's avatar
Alexander Rose committed
        isoLevel: Math.exp(-smoothness),
        scalarField: field,
        idField
    }
    const wireframe = await computeMarchingCubesLines(params, lines).runAsChild(ctx.runtime)

    Lines.transformImmediate(wireframe, transform)
Alexander Rose's avatar
Alexander Rose committed

    return wireframe
Alexander Rose's avatar
Alexander Rose committed
}

Alexander Rose's avatar
Alexander Rose committed
export const GaussianWireframeParams = {
    ...UnitsLinesParams,
    ...GaussianDensityParams,
David Sehnal's avatar
David Sehnal committed
    sizeTheme: PD.Select<SizeThemeName>('Size Theme', '', 'uniform', SizeThemeOptions),
    sizeValue: PD.Numeric('Size Value', '', 2, 0, 10, 0.1),
    lineSizeAttenuation: PD.Boolean('Line Size Attenuation', '', false),
Alexander Rose's avatar
Alexander Rose committed
}
David Sehnal's avatar
David Sehnal committed
export const DefaultGaussianWireframeProps = PD.getDefaultValues(GaussianWireframeParams)
Alexander Rose's avatar
Alexander Rose committed
export type GaussianWireframeProps = typeof DefaultGaussianWireframeProps

export function GaussianWireframeVisual(): UnitsVisual<GaussianWireframeProps> {
    return UnitsLinesVisual<GaussianWireframeProps>({
        defaultProps: DefaultGaussianWireframeProps,
        createGeometry: createGaussianWireframe,
Alexander Rose's avatar
Alexander Rose committed
        createLocationIterator: StructureElementIterator.fromGroup,
        getLoci: getElementLoci,
        mark: markElement,
        setUpdateState: (state: VisualUpdateState, newProps: GaussianWireframeProps, currentProps: GaussianWireframeProps) => {
            if (newProps.resolution !== currentProps.resolution) state.createGeometry = true
Alexander Rose's avatar
Alexander Rose committed
            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
Alexander Rose's avatar
Alexander Rose committed
        }
    })
}