complex-visual.ts 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 '../representation';
  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 './representation';
  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 { ParamDefinition as PD } from 'mol-util/param-definition';
  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 } from 'mol-repr/util';
  24. import { Theme } from 'mol-theme/theme';
  25. import { ColorTheme } from 'mol-theme/color';
  26. import { SizeTheme } from 'mol-theme/size';
  27. export interface ComplexVisual<P extends StructureProps> extends Visual<Structure, P> { }
  28. const ComplexParams = {
  29. ...StructureParams,
  30. unitKinds: PD.MultiSelect<UnitKind>('Unit Kind', '', ['atomic', 'spheres'], UnitKindOptions),
  31. }
  32. const DefaultComplexProps = PD.getDefaultValues(ComplexParams)
  33. type ComplexProps = typeof DefaultComplexProps
  34. type ComplexRenderObject = MeshRenderObject | LinesRenderObject | PointsRenderObject | DirectVolumeRenderObject
  35. interface ComplexVisualBuilder<P extends ComplexProps, G extends Geometry> {
  36. defaultProps: P
  37. createGeometry(ctx: VisualContext, structure: Structure, theme: Theme, props: P, geometry?: G): Promise<G>
  38. createLocationIterator(structure: Structure): LocationIterator
  39. getLoci(pickingId: PickingId, structure: Structure, id: number): Loci
  40. mark(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean): boolean,
  41. setUpdateState(state: VisualUpdateState, newProps: P, currentProps: P, newTheme: Theme, currentTheme: Theme): void
  42. }
  43. interface ComplexVisualGeometryBuilder<P extends ComplexProps, G extends Geometry> extends ComplexVisualBuilder<P, G> {
  44. createEmptyGeometry(geometry?: G): G
  45. createRenderObject(ctx: VisualContext, structure: Structure, geometry: Geometry, locationIt: LocationIterator, theme: Theme, currentProps: P): Promise<ComplexRenderObject>
  46. updateValues(values: RenderableValues, newProps: P): void
  47. }
  48. export function ComplexVisual<P extends ComplexMeshProps>(builder: ComplexVisualGeometryBuilder<P, Geometry>): ComplexVisual<P> {
  49. const { defaultProps, createGeometry, createLocationIterator, getLoci, mark, setUpdateState } = builder
  50. const { createRenderObject, updateValues } = builder
  51. const updateState = VisualUpdateState.create()
  52. let renderObject: ComplexRenderObject | undefined
  53. let currentProps: P
  54. let currentTheme: Theme
  55. let geometry: Geometry
  56. let currentStructure: Structure
  57. let locationIt: LocationIterator
  58. let conformationHash: number
  59. async function create(ctx: VisualContext, structure: Structure, theme: Theme, props: Partial<P> = {}) {
  60. currentProps = Object.assign({}, defaultProps, props)
  61. currentTheme = theme
  62. currentStructure = structure
  63. conformationHash = Structure.conformationHash(currentStructure)
  64. geometry = await createGeometry(ctx, currentStructure, theme, currentProps, geometry)
  65. locationIt = createLocationIterator(structure)
  66. renderObject = await createRenderObject(ctx, structure, geometry, locationIt, theme, currentProps)
  67. }
  68. async function update(ctx: VisualContext, theme: Theme, props: Partial<P>) {
  69. const newProps = Object.assign({}, currentProps, props, { structure: currentStructure })
  70. if (!renderObject) return false
  71. locationIt.reset()
  72. VisualUpdateState.reset(updateState)
  73. setUpdateState(updateState, newProps, currentProps, theme, currentTheme)
  74. const newConformationHash = Structure.conformationHash(currentStructure)
  75. if (newConformationHash !== conformationHash) {
  76. conformationHash = newConformationHash
  77. updateState.createGeometry = true
  78. }
  79. if (ColorTheme.areEqual(theme.color, currentTheme.color)) updateState.updateColor = true
  80. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createGeometry = true
  81. //
  82. if (updateState.createGeometry) {
  83. geometry = await createGeometry(ctx, currentStructure, theme, newProps, geometry)
  84. ValueCell.update(renderObject.values.drawCount, Geometry.getDrawCount(geometry))
  85. updateState.updateColor = true
  86. }
  87. if (updateState.updateSize) {
  88. // not all geometries have size data, so check here
  89. if ('uSize' in renderObject.values) {
  90. await createSizes(ctx.runtime, locationIt, theme.size, renderObject.values)
  91. }
  92. }
  93. if (updateState.updateColor) {
  94. await createColors(ctx.runtime, locationIt, theme.color, renderObject.values)
  95. }
  96. updateValues(renderObject.values, newProps)
  97. updateRenderableState(renderObject.state, newProps)
  98. currentProps = newProps
  99. currentTheme = theme
  100. return true
  101. }
  102. return {
  103. get renderObject () { return renderObject },
  104. async createOrUpdate(ctx: VisualContext, theme: Theme, props: Partial<P> = {}, structure?: Structure) {
  105. if (!structure && !currentStructure) {
  106. throw new Error('missing structure')
  107. } else if (structure && (!currentStructure || !renderObject)) {
  108. await create(ctx, structure, theme, props)
  109. } else if (structure && structure.hashCode !== currentStructure.hashCode) {
  110. await create(ctx, structure, theme, props)
  111. } else {
  112. if (structure && Structure.conformationHash(structure) !== Structure.conformationHash(currentStructure)) {
  113. currentStructure = structure
  114. }
  115. await update(ctx, theme, props)
  116. }
  117. },
  118. getLoci(pickingId: PickingId) {
  119. return renderObject ? getLoci(pickingId, currentStructure, renderObject.id) : EmptyLoci
  120. },
  121. mark(loci: Loci, action: MarkerAction) {
  122. if (!renderObject) return false
  123. const { tMarker } = renderObject.values
  124. const { groupCount, instanceCount } = locationIt
  125. function apply(interval: Interval) {
  126. const start = Interval.start(interval)
  127. const end = Interval.end(interval)
  128. return applyMarkerAction(tMarker.ref.value.array, start, end, action)
  129. }
  130. let changed = false
  131. if (isEveryLoci(loci)) {
  132. changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
  133. } else {
  134. changed = mark(loci, currentStructure, apply)
  135. }
  136. if (changed) {
  137. ValueCell.update(tMarker, tMarker.ref.value)
  138. }
  139. return changed
  140. },
  141. destroy() {
  142. // TODO
  143. renderObject = undefined
  144. }
  145. }
  146. }
  147. // mesh
  148. export const ComplexMeshParams = {
  149. ...StructureMeshParams,
  150. unitKinds: PD.MultiSelect<UnitKind>('Unit Kind', '', [ 'atomic', 'spheres' ], UnitKindOptions),
  151. }
  152. export const DefaultComplexMeshProps = PD.getDefaultValues(ComplexMeshParams)
  153. export type ComplexMeshProps = typeof DefaultComplexMeshProps
  154. export interface ComplexMeshVisualBuilder<P extends ComplexMeshProps> extends ComplexVisualBuilder<P, Mesh> { }
  155. export function ComplexMeshVisual<P extends ComplexMeshProps>(builder: ComplexMeshVisualBuilder<P>): ComplexVisual<P> {
  156. return ComplexVisual({
  157. ...builder,
  158. setUpdateState: (state: VisualUpdateState, newProps: P, currentProps: P, newTheme: Theme, currentTheme: Theme) => {
  159. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme)
  160. if (SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true
  161. },
  162. createEmptyGeometry: Mesh.createEmpty,
  163. createRenderObject: createComplexMeshRenderObject,
  164. updateValues: Mesh.updateValues
  165. })
  166. }