123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- /**
- * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
- *
- * @author Alexander Rose <alexander.rose@weirdbyte.de>
- */
- import { Structure } from 'mol-model/structure';
- import { Visual, VisualContext } from '../representation';
- import { MeshRenderObject, LinesRenderObject, PointsRenderObject, DirectVolumeRenderObject } from 'mol-gl/render-object';
- import { createComplexMeshRenderObject, UnitKind, UnitKindOptions } from './visual/util/common';
- import { StructureProps, StructureMeshParams, StructureParams } from './representation';
- import { deepEqual, ValueCell } from 'mol-util';
- import { Loci, isEveryLoci, EmptyLoci } from 'mol-model/loci';
- import { Interval } from 'mol-data/int';
- import { ParamDefinition as PD } from 'mol-util/param-definition';
- import { RenderableValues } from 'mol-gl/renderable/schema';
- import { createSizes } from 'mol-geo/geometry/size-data';
- import { Geometry, updateRenderableState } from 'mol-geo/geometry/geometry';
- import { LocationIterator } from 'mol-geo/util/location-iterator';
- import { PickingId } from 'mol-geo/geometry/picking';
- import { createColors } from 'mol-geo/geometry/color-data';
- import { MarkerAction, applyMarkerAction } from 'mol-geo/geometry/marker-data';
- import { Mesh } from 'mol-geo/geometry/mesh/mesh';
- import { VisualUpdateState } from 'mol-repr/util';
- import { Theme } from 'mol-theme/theme';
- import { ColorTheme } from 'mol-theme/color';
- import { SizeTheme } from 'mol-theme/size';
- export interface ComplexVisual<P extends StructureProps> extends Visual<Structure, P> { }
- const ComplexParams = {
- ...StructureParams,
- unitKinds: PD.MultiSelect<UnitKind>('Unit Kind', '', ['atomic', 'spheres'], UnitKindOptions),
- }
- const DefaultComplexProps = PD.getDefaultValues(ComplexParams)
- type ComplexProps = typeof DefaultComplexProps
- type ComplexRenderObject = MeshRenderObject | LinesRenderObject | PointsRenderObject | DirectVolumeRenderObject
- interface ComplexVisualBuilder<P extends ComplexProps, G extends Geometry> {
- defaultProps: P
- createGeometry(ctx: VisualContext, structure: Structure, theme: Theme, props: P, geometry?: G): Promise<G>
- createLocationIterator(structure: Structure): LocationIterator
- getLoci(pickingId: PickingId, structure: Structure, id: number): Loci
- mark(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean): boolean,
- setUpdateState(state: VisualUpdateState, newProps: P, currentProps: P, newTheme: Theme, currentTheme: Theme): void
- }
- interface ComplexVisualGeometryBuilder<P extends ComplexProps, G extends Geometry> extends ComplexVisualBuilder<P, G> {
- createEmptyGeometry(geometry?: G): G
- createRenderObject(ctx: VisualContext, structure: Structure, geometry: Geometry, locationIt: LocationIterator, theme: Theme, currentProps: P): Promise<ComplexRenderObject>
- updateValues(values: RenderableValues, newProps: P): void
- }
- export function ComplexVisual<P extends ComplexMeshProps>(builder: ComplexVisualGeometryBuilder<P, Geometry>): ComplexVisual<P> {
- const { defaultProps, createGeometry, createLocationIterator, getLoci, mark, setUpdateState } = builder
- const { createRenderObject, updateValues } = builder
- const updateState = VisualUpdateState.create()
- let renderObject: ComplexRenderObject | undefined
- let currentProps: P
- let currentTheme: Theme
- let geometry: Geometry
- let currentStructure: Structure
- let locationIt: LocationIterator
- let conformationHash: number
- async function create(ctx: VisualContext, structure: Structure, theme: Theme, props: Partial<P> = {}) {
- currentProps = Object.assign({}, defaultProps, props)
- currentTheme = theme
- currentStructure = structure
- conformationHash = Structure.conformationHash(currentStructure)
- geometry = await createGeometry(ctx, currentStructure, theme, currentProps, geometry)
- locationIt = createLocationIterator(structure)
- renderObject = await createRenderObject(ctx, structure, geometry, locationIt, theme, currentProps)
- }
- async function update(ctx: VisualContext, theme: Theme, props: Partial<P>) {
- const newProps = Object.assign({}, currentProps, props, { structure: currentStructure })
- if (!renderObject) return false
- locationIt.reset()
- VisualUpdateState.reset(updateState)
- setUpdateState(updateState, newProps, currentProps, theme, currentTheme)
- const newConformationHash = Structure.conformationHash(currentStructure)
- if (newConformationHash !== conformationHash) {
- conformationHash = newConformationHash
- updateState.createGeometry = true
- }
- if (ColorTheme.areEqual(theme.color, currentTheme.color)) updateState.updateColor = true
- if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createGeometry = true
- //
- if (updateState.createGeometry) {
- geometry = await createGeometry(ctx, currentStructure, theme, newProps, geometry)
- ValueCell.update(renderObject.values.drawCount, Geometry.getDrawCount(geometry))
- updateState.updateColor = true
- }
- if (updateState.updateSize) {
- // not all geometries have size data, so check here
- if ('uSize' in renderObject.values) {
- await createSizes(ctx.runtime, locationIt, theme.size, renderObject.values)
- }
- }
- if (updateState.updateColor) {
- await createColors(ctx.runtime, locationIt, theme.color, renderObject.values)
- }
- updateValues(renderObject.values, newProps)
- updateRenderableState(renderObject.state, newProps)
- currentProps = newProps
- currentTheme = theme
- return true
- }
- return {
- get renderObject () { return renderObject },
- async createOrUpdate(ctx: VisualContext, theme: Theme, props: Partial<P> = {}, structure?: Structure) {
- if (!structure && !currentStructure) {
- throw new Error('missing structure')
- } else if (structure && (!currentStructure || !renderObject)) {
- await create(ctx, structure, theme, props)
- } else if (structure && structure.hashCode !== currentStructure.hashCode) {
- await create(ctx, structure, theme, props)
- } else {
- if (structure && Structure.conformationHash(structure) !== Structure.conformationHash(currentStructure)) {
- currentStructure = structure
- }
- await update(ctx, theme, props)
- }
- },
- getLoci(pickingId: PickingId) {
- return renderObject ? getLoci(pickingId, currentStructure, renderObject.id) : EmptyLoci
- },
- mark(loci: Loci, action: MarkerAction) {
- if (!renderObject) return false
- const { tMarker } = renderObject.values
- const { groupCount, instanceCount } = locationIt
- function apply(interval: Interval) {
- const start = Interval.start(interval)
- const end = Interval.end(interval)
- return applyMarkerAction(tMarker.ref.value.array, start, end, action)
- }
- let changed = false
- if (isEveryLoci(loci)) {
- changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
- } else {
- changed = mark(loci, currentStructure, apply)
- }
- if (changed) {
- ValueCell.update(tMarker, tMarker.ref.value)
- }
- return changed
- },
- destroy() {
- // TODO
- renderObject = undefined
- }
- }
- }
- // mesh
- export const ComplexMeshParams = {
- ...StructureMeshParams,
- unitKinds: PD.MultiSelect<UnitKind>('Unit Kind', '', [ 'atomic', 'spheres' ], UnitKindOptions),
- }
- export const DefaultComplexMeshProps = PD.getDefaultValues(ComplexMeshParams)
- export type ComplexMeshProps = typeof DefaultComplexMeshProps
- export interface ComplexMeshVisualBuilder<P extends ComplexMeshProps> extends ComplexVisualBuilder<P, Mesh> { }
- export function ComplexMeshVisual<P extends ComplexMeshProps>(builder: ComplexMeshVisualBuilder<P>): ComplexVisual<P> {
- return ComplexVisual({
- ...builder,
- setUpdateState: (state: VisualUpdateState, newProps: P, currentProps: P, newTheme: Theme, currentTheme: Theme) => {
- builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme)
- if (SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true
- },
- createEmptyGeometry: Mesh.createEmpty,
- createRenderObject: createComplexMeshRenderObject,
- updateValues: Mesh.updateValues
- })
- }
|