|
@@ -5,20 +5,21 @@
|
|
|
*/
|
|
|
|
|
|
import { Unit, Structure } from 'mol-model/structure';
|
|
|
-import { RepresentationProps, Visual } from '..';
|
|
|
-import { DefaultStructureMeshProps, MeshUpdateState } from '.';
|
|
|
+import { RepresentationProps, Visual } from '../';
|
|
|
+import { DefaultStructureMeshProps, VisualUpdateState, DefaultStructurePointProps } from '.';
|
|
|
import { RuntimeContext } from 'mol-task';
|
|
|
import { PickingId } from '../../util/picking';
|
|
|
import { LocationIterator } from '../../util/location-iterator';
|
|
|
-import { Mesh } from '../../mesh/mesh';
|
|
|
+import { Mesh } from '../../geometry/mesh/mesh';
|
|
|
import { MarkerAction, applyMarkerAction, createMarkers } from '../../util/marker-data';
|
|
|
import { Loci, isEveryLoci, EmptyLoci } from 'mol-model/loci';
|
|
|
-import { MeshRenderObject } from 'mol-gl/render-object';
|
|
|
-import { createUnitsMeshRenderObject, createColors } from './visual/util/common';
|
|
|
+import { MeshRenderObject, PointRenderObject } from 'mol-gl/render-object';
|
|
|
+import { createUnitsMeshRenderObject, createColors, createUnitsPointRenderObject } from './visual/util/common';
|
|
|
import { deepEqual, ValueCell, UUID } from 'mol-util';
|
|
|
-import { updateMeshValues, updateRenderableState } from '../util';
|
|
|
import { Interval } from 'mol-data/int';
|
|
|
import { createTransforms } from '../../util/transform-data';
|
|
|
+import { updateMeshValues, updateRenderableState, updatePointValues } from '../../geometry/geometry';
|
|
|
+import { Point } from '../../geometry/point/point';
|
|
|
|
|
|
export type StructureGroup = { structure: Structure, group: Unit.SymmetryGroup }
|
|
|
|
|
@@ -36,12 +37,12 @@ export interface UnitsMeshVisualBuilder<P extends UnitsMeshProps> {
|
|
|
createLocationIterator(group: Unit.SymmetryGroup): LocationIterator
|
|
|
getLoci(pickingId: PickingId, group: Unit.SymmetryGroup, id: number): Loci
|
|
|
mark(loci: Loci, group: Unit.SymmetryGroup, apply: (interval: Interval) => boolean): boolean
|
|
|
- setUpdateState(state: MeshUpdateState, newProps: P, currentProps: P): void
|
|
|
+ setUpdateState(state: VisualUpdateState, newProps: P, currentProps: P): void
|
|
|
}
|
|
|
|
|
|
export function UnitsMeshVisual<P extends UnitsMeshProps>(builder: UnitsMeshVisualBuilder<P>): UnitsVisual<P> {
|
|
|
const { defaultProps, createMesh, createLocationIterator, getLoci, mark, setUpdateState } = builder
|
|
|
- const updateState = MeshUpdateState.create()
|
|
|
+ const updateState = VisualUpdateState.create()
|
|
|
|
|
|
let renderObject: MeshRenderObject | undefined
|
|
|
let currentProps: P
|
|
@@ -75,20 +76,20 @@ export function UnitsMeshVisual<P extends UnitsMeshProps>(builder: UnitsMeshVisu
|
|
|
const unit = currentGroup.units[0]
|
|
|
|
|
|
locationIt.reset()
|
|
|
- MeshUpdateState.reset(updateState)
|
|
|
+ VisualUpdateState.reset(updateState)
|
|
|
setUpdateState(updateState, newProps, currentProps)
|
|
|
|
|
|
const newConformationId = Unit.conformationId(unit)
|
|
|
if (newConformationId !== currentConformationId) {
|
|
|
currentConformationId = newConformationId
|
|
|
- updateState.createMesh = true
|
|
|
+ updateState.createGeometry = true
|
|
|
}
|
|
|
|
|
|
if (currentGroup.units.length !== locationIt.instanceCount) updateState.updateTransform = true
|
|
|
|
|
|
- if (!deepEqual(newProps.sizeTheme, currentProps.sizeTheme)) updateState.createMesh = true
|
|
|
+ if (!deepEqual(newProps.sizeTheme, currentProps.sizeTheme)) updateState.createGeometry = true
|
|
|
if (!deepEqual(newProps.colorTheme, currentProps.colorTheme)) updateState.updateColor = true
|
|
|
- if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createMesh = true
|
|
|
+ if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createGeometry = true
|
|
|
|
|
|
//
|
|
|
|
|
@@ -100,7 +101,7 @@ export function UnitsMeshVisual<P extends UnitsMeshProps>(builder: UnitsMeshVisu
|
|
|
updateState.updateColor = true
|
|
|
}
|
|
|
|
|
|
- if (updateState.createMesh) {
|
|
|
+ if (updateState.createGeometry) {
|
|
|
mesh = newProps.unitKinds.includes(unit.kind)
|
|
|
? await createMesh(ctx, unit, currentStructure, newProps, mesh)
|
|
|
: Mesh.createEmpty(mesh)
|
|
@@ -112,8 +113,9 @@ export function UnitsMeshVisual<P extends UnitsMeshProps>(builder: UnitsMeshVisu
|
|
|
await createColors(ctx, locationIt, newProps.colorTheme, renderObject.values)
|
|
|
}
|
|
|
|
|
|
- updateMeshValues(renderObject.values, newProps)
|
|
|
- updateRenderableState(renderObject.state, newProps)
|
|
|
+ // TODO why do I need to cast here?
|
|
|
+ updateMeshValues(renderObject.values, newProps as UnitsMeshProps)
|
|
|
+ updateRenderableState(renderObject.state, newProps as UnitsMeshProps)
|
|
|
|
|
|
currentProps = newProps
|
|
|
}
|
|
@@ -177,4 +179,155 @@ function sameGroupConformation(groupA: Unit.SymmetryGroup, groupB: Unit.Symmetry
|
|
|
groupA.units.length === groupB.units.length &&
|
|
|
Unit.conformationId(groupA.units[0]) === Unit.conformationId(groupB.units[0])
|
|
|
)
|
|
|
+}
|
|
|
+
|
|
|
+//
|
|
|
+
|
|
|
+export const DefaultUnitsPointProps = {
|
|
|
+ ...DefaultStructurePointProps,
|
|
|
+ unitKinds: [ Unit.Kind.Atomic, Unit.Kind.Spheres ] as Unit.Kind[]
|
|
|
+}
|
|
|
+export type UnitsPointProps = typeof DefaultUnitsPointProps
|
|
|
+
|
|
|
+export interface UnitsPointVisualBuilder<P extends UnitsPointProps> {
|
|
|
+ defaultProps: P
|
|
|
+ createPoint(ctx: RuntimeContext, unit: Unit, structure: Structure, props: P, point?: Point): Promise<Point>
|
|
|
+ createLocationIterator(group: Unit.SymmetryGroup): LocationIterator
|
|
|
+ getLoci(pickingId: PickingId, group: Unit.SymmetryGroup, id: number): Loci
|
|
|
+ mark(loci: Loci, group: Unit.SymmetryGroup, apply: (interval: Interval) => boolean): boolean
|
|
|
+ setUpdateState(state: VisualUpdateState, newProps: P, currentProps: P): void
|
|
|
+}
|
|
|
+
|
|
|
+export function UnitsPointVisual<P extends UnitsPointProps>(builder: UnitsPointVisualBuilder<P>): UnitsVisual<P> {
|
|
|
+ const { defaultProps, createPoint, createLocationIterator, getLoci, mark, setUpdateState } = builder
|
|
|
+ const updateState = VisualUpdateState.create()
|
|
|
+
|
|
|
+ let renderObject: PointRenderObject | undefined
|
|
|
+ let currentProps: P
|
|
|
+ let point: Point
|
|
|
+ let currentGroup: Unit.SymmetryGroup
|
|
|
+ let currentStructure: Structure
|
|
|
+ let locationIt: LocationIterator
|
|
|
+ let currentConformationId: UUID
|
|
|
+
|
|
|
+ async function create(ctx: RuntimeContext, group: Unit.SymmetryGroup, props: Partial<P> = {}) {
|
|
|
+ currentProps = Object.assign({}, defaultProps, props)
|
|
|
+ currentProps.colorTheme.structure = currentStructure
|
|
|
+ currentGroup = group
|
|
|
+
|
|
|
+ const unit = group.units[0]
|
|
|
+ currentConformationId = Unit.conformationId(unit)
|
|
|
+ point = currentProps.unitKinds.includes(unit.kind)
|
|
|
+ ? await createPoint(ctx, unit, currentStructure, currentProps, point)
|
|
|
+ : Point.createEmpty(point)
|
|
|
+
|
|
|
+ // TODO create empty location iterator when not in unitKinds
|
|
|
+ locationIt = createLocationIterator(group)
|
|
|
+ renderObject = await createUnitsPointRenderObject(ctx, group, point, locationIt, currentProps)
|
|
|
+ }
|
|
|
+
|
|
|
+ async function update(ctx: RuntimeContext, props: Partial<P> = {}) {
|
|
|
+ if (!renderObject) return
|
|
|
+
|
|
|
+ const newProps = Object.assign({}, currentProps, props)
|
|
|
+ newProps.colorTheme.structure = currentStructure
|
|
|
+ const unit = currentGroup.units[0]
|
|
|
+
|
|
|
+ locationIt.reset()
|
|
|
+ VisualUpdateState.reset(updateState)
|
|
|
+ setUpdateState(updateState, newProps, currentProps)
|
|
|
+
|
|
|
+ const newConformationId = Unit.conformationId(unit)
|
|
|
+ if (newConformationId !== currentConformationId) {
|
|
|
+ currentConformationId = newConformationId
|
|
|
+ updateState.createGeometry = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if (currentGroup.units.length !== locationIt.instanceCount) updateState.updateTransform = true
|
|
|
+
|
|
|
+ if (!deepEqual(newProps.sizeTheme, currentProps.sizeTheme)) updateState.createGeometry = true
|
|
|
+ if (!deepEqual(newProps.colorTheme, currentProps.colorTheme)) updateState.updateColor = true
|
|
|
+ if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createGeometry = true
|
|
|
+
|
|
|
+ //
|
|
|
+
|
|
|
+ if (updateState.updateTransform) {
|
|
|
+ locationIt = createLocationIterator(currentGroup)
|
|
|
+ const { instanceCount, groupCount } = locationIt
|
|
|
+ createTransforms(currentGroup, renderObject.values)
|
|
|
+ createMarkers(instanceCount * groupCount, renderObject.values)
|
|
|
+ updateState.updateColor = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if (updateState.createGeometry) {
|
|
|
+ point = newProps.unitKinds.includes(unit.kind)
|
|
|
+ ? await createPoint(ctx, unit, currentStructure, newProps, point)
|
|
|
+ : Point.createEmpty(point)
|
|
|
+ ValueCell.update(renderObject.values.drawCount, point.vertexCount)
|
|
|
+ updateState.updateColor = true
|
|
|
+ }
|
|
|
+
|
|
|
+ if (updateState.updateColor) {
|
|
|
+ await createColors(ctx, locationIt, newProps.colorTheme, renderObject.values)
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO why do I need to cast here?
|
|
|
+ updatePointValues(renderObject.values, newProps as UnitsPointProps)
|
|
|
+ updateRenderableState(renderObject.state, newProps as UnitsPointProps)
|
|
|
+
|
|
|
+ currentProps = newProps
|
|
|
+ }
|
|
|
+
|
|
|
+ return {
|
|
|
+ get renderObject () { return renderObject },
|
|
|
+ async createOrUpdate(ctx: RuntimeContext, props: Partial<P> = {}, structureGroup?: StructureGroup) {
|
|
|
+ if (structureGroup) currentStructure = structureGroup.structure
|
|
|
+ const group = structureGroup ? structureGroup.group : undefined
|
|
|
+ if (!group && !currentGroup) {
|
|
|
+ throw new Error('missing group')
|
|
|
+ } else if (group && (!currentGroup || !renderObject)) {
|
|
|
+ // console.log('unit-visual first create')
|
|
|
+ await create(ctx, group, props)
|
|
|
+ } else if (group && group.hashCode !== currentGroup.hashCode) {
|
|
|
+ // console.log('unit-visual group.hashCode !== currentGroup.hashCode')
|
|
|
+ await create(ctx, group, props)
|
|
|
+ } else {
|
|
|
+ // console.log('unit-visual update')
|
|
|
+ if (group && !sameGroupConformation(group, currentGroup)) {
|
|
|
+ // console.log('unit-visual new conformation')
|
|
|
+ currentGroup = group
|
|
|
+ }
|
|
|
+ await update(ctx, props)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ getLoci(pickingId: PickingId) {
|
|
|
+ return renderObject ? getLoci(pickingId, currentGroup, 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, currentGroup, apply)
|
|
|
+ }
|
|
|
+ if (changed) {
|
|
|
+ ValueCell.update(tMarker, tMarker.ref.value)
|
|
|
+ }
|
|
|
+ return changed
|
|
|
+ },
|
|
|
+ destroy() {
|
|
|
+ // TODO
|
|
|
+ renderObject = undefined
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|