complex-visual.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (c) 2018 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Structure } from 'mol-model/structure';
  7. import { Visual, VisualContext } from '..';
  8. import { MeshRenderObject, LinesRenderObject, PointsRenderObject, DirectVolumeRenderObject } from 'mol-gl/render-object';
  9. import { createComplexMeshRenderObject, UnitKind, UnitKindOptions } from './visual/util/common';
  10. import { StructureProps, StructureMeshParams, StructureParams } from './index';
  11. import { deepEqual, ValueCell } from 'mol-util';
  12. import { Loci, isEveryLoci, EmptyLoci } from 'mol-model/loci';
  13. import { Interval } from 'mol-data/int';
  14. import { MultiSelectParam, paramDefaultValues } from 'mol-util/parameter';
  15. import { RenderableValues } from 'mol-gl/renderable/schema';
  16. import { createSizes } from 'mol-geo/geometry/size-data';
  17. import { Geometry, updateRenderableState } from 'mol-geo/geometry/geometry';
  18. import { LocationIterator } from 'mol-geo/util/location-iterator';
  19. import { PickingId } from 'mol-geo/geometry/picking';
  20. import { createColors } from 'mol-geo/geometry/color-data';
  21. import { MarkerAction, applyMarkerAction } from 'mol-geo/geometry/marker-data';
  22. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  23. import { VisualUpdateState, colorChanged, sizeChanged } from 'mol-repr/util';
  24. export interface ComplexVisual<P extends StructureProps> extends Visual<Structure, P> { }
  25. const ComplexParams = {
  26. ...StructureParams,
  27. unitKinds: MultiSelectParam<UnitKind>('Unit Kind', '', ['atomic', 'spheres'], UnitKindOptions),
  28. }
  29. const DefaultComplexProps = paramDefaultValues(ComplexParams)
  30. type ComplexProps = typeof DefaultComplexProps
  31. type ComplexRenderObject = MeshRenderObject | LinesRenderObject | PointsRenderObject | DirectVolumeRenderObject
  32. interface ComplexVisualBuilder<P extends ComplexProps, G extends Geometry> {
  33. defaultProps: P
  34. createGeometry(ctx: VisualContext, structure: Structure, props: P, geometry?: G): Promise<G>
  35. createLocationIterator(structure: Structure): LocationIterator
  36. getLoci(pickingId: PickingId, structure: Structure, id: number): Loci
  37. mark(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean): boolean,
  38. setUpdateState(state: VisualUpdateState, newProps: P, currentProps: P): void
  39. }
  40. interface ComplexVisualGeometryBuilder<P extends ComplexProps, G extends Geometry> extends ComplexVisualBuilder<P, G> {
  41. createEmptyGeometry(geometry?: G): G
  42. createRenderObject(ctx: VisualContext, structure: Structure, geometry: Geometry, locationIt: LocationIterator, currentProps: P): Promise<ComplexRenderObject>
  43. updateValues(values: RenderableValues, newProps: P): void
  44. }
  45. export function ComplexVisual<P extends ComplexMeshProps>(builder: ComplexVisualGeometryBuilder<P, Geometry>): ComplexVisual<P> {
  46. const { defaultProps, createGeometry, createLocationIterator, getLoci, mark, setUpdateState } = builder
  47. const { createRenderObject, updateValues } = builder
  48. const updateState = VisualUpdateState.create()
  49. let renderObject: ComplexRenderObject | undefined
  50. let currentProps: P
  51. let geometry: Geometry
  52. let currentStructure: Structure
  53. let locationIt: LocationIterator
  54. let conformationHash: number
  55. async function create(ctx: VisualContext, structure: Structure, props: Partial<P> = {}) {
  56. currentProps = Object.assign({}, defaultProps, props, { structure })
  57. currentStructure = structure
  58. conformationHash = Structure.conformationHash(currentStructure)
  59. geometry = await createGeometry(ctx, currentStructure, currentProps, geometry)
  60. locationIt = createLocationIterator(structure)
  61. renderObject = await createRenderObject(ctx, structure, geometry, locationIt, currentProps)
  62. }
  63. async function update(ctx: VisualContext, props: Partial<P>) {
  64. const newProps = Object.assign({}, currentProps, props, { structure: currentStructure })
  65. if (!renderObject) return false
  66. locationIt.reset()
  67. VisualUpdateState.reset(updateState)
  68. setUpdateState(updateState, newProps, currentProps)
  69. const newConformationHash = Structure.conformationHash(currentStructure)
  70. if (newConformationHash !== conformationHash) {
  71. conformationHash = newConformationHash
  72. updateState.createGeometry = true
  73. }
  74. if (colorChanged(currentProps, newProps)) updateState.updateColor = true
  75. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createGeometry = true
  76. //
  77. if (updateState.createGeometry) {
  78. geometry = await createGeometry(ctx, currentStructure, newProps, geometry)
  79. ValueCell.update(renderObject.values.drawCount, Geometry.getDrawCount(geometry))
  80. updateState.updateColor = true
  81. }
  82. if (updateState.updateSize) {
  83. // not all geometries have size data, so check here
  84. if ('uSize' in renderObject.values) {
  85. await createSizes(ctx.runtime, locationIt, newProps, renderObject.values)
  86. }
  87. }
  88. if (updateState.updateColor) {
  89. await createColors(ctx.runtime, locationIt, newProps, renderObject.values)
  90. }
  91. updateValues(renderObject.values, newProps)
  92. updateRenderableState(renderObject.state, newProps)
  93. currentProps = newProps
  94. return true
  95. }
  96. return {
  97. get renderObject () { return renderObject },
  98. async createOrUpdate(ctx: VisualContext, props: Partial<P> = {}, structure?: Structure) {
  99. if (!structure && !currentStructure) {
  100. throw new Error('missing structure')
  101. } else if (structure && (!currentStructure || !renderObject)) {
  102. await create(ctx, structure, props)
  103. } else if (structure && structure.hashCode !== currentStructure.hashCode) {
  104. await create(ctx, structure, props)
  105. } else {
  106. if (structure && Structure.conformationHash(structure) !== Structure.conformationHash(currentStructure)) {
  107. currentStructure = structure
  108. }
  109. await update(ctx, props)
  110. }
  111. },
  112. getLoci(pickingId: PickingId) {
  113. return renderObject ? getLoci(pickingId, currentStructure, renderObject.id) : EmptyLoci
  114. },
  115. mark(loci: Loci, action: MarkerAction) {
  116. if (!renderObject) return false
  117. const { tMarker } = renderObject.values
  118. const { groupCount, instanceCount } = locationIt
  119. function apply(interval: Interval) {
  120. const start = Interval.start(interval)
  121. const end = Interval.end(interval)
  122. return applyMarkerAction(tMarker.ref.value.array, start, end, action)
  123. }
  124. let changed = false
  125. if (isEveryLoci(loci)) {
  126. changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
  127. } else {
  128. changed = mark(loci, currentStructure, apply)
  129. }
  130. if (changed) {
  131. ValueCell.update(tMarker, tMarker.ref.value)
  132. }
  133. return changed
  134. },
  135. destroy() {
  136. // TODO
  137. renderObject = undefined
  138. }
  139. }
  140. }
  141. // mesh
  142. export const ComplexMeshParams = {
  143. ...StructureMeshParams,
  144. unitKinds: MultiSelectParam<UnitKind>('Unit Kind', '', [ 'atomic', 'spheres' ], UnitKindOptions),
  145. }
  146. export const DefaultComplexMeshProps = paramDefaultValues(ComplexMeshParams)
  147. export type ComplexMeshProps = typeof DefaultComplexMeshProps
  148. export interface ComplexMeshVisualBuilder<P extends ComplexMeshProps> extends ComplexVisualBuilder<P, Mesh> { }
  149. export function ComplexMeshVisual<P extends ComplexMeshProps>(builder: ComplexMeshVisualBuilder<P>): ComplexVisual<P> {
  150. return ComplexVisual({
  151. ...builder,
  152. setUpdateState: (state: VisualUpdateState, newProps: P, currentProps: P) => {
  153. builder.setUpdateState(state, newProps, currentProps)
  154. if (sizeChanged(currentProps, newProps)) state.createGeometry = true
  155. },
  156. createEmptyGeometry: Mesh.createEmpty,
  157. createRenderObject: createComplexMeshRenderObject,
  158. updateValues: Mesh.updateValues
  159. })
  160. }