complex-representation.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { ParamDefinition as PD } from '../../mol-util/param-definition';
  8. import { StructureParams, ComplexVisual, StructureRepresentation, StructureRepresentationStateBuilder, StructureRepresentationState } from './representation';
  9. import { RepresentationContext, RepresentationParamsGetter } from '../representation';
  10. import { Structure, StructureElement, Link } from '../../mol-model/structure';
  11. import { Subject } from 'rxjs';
  12. import { getNextMaterialId, GraphicsRenderObject } from '../../mol-gl/render-object';
  13. import { createEmptyTheme, Theme } from '../../mol-theme/theme';
  14. import { Task } from '../../mol-task';
  15. import { PickingId } from '../../mol-geo/geometry/picking';
  16. import { EmptyLoci, Loci, isEveryLoci } from '../../mol-model/loci';
  17. import { MarkerAction } from '../../mol-util/marker-action';
  18. import { Overpaint } from '../../mol-theme/overpaint';
  19. export function ComplexRepresentation<P extends StructureParams>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, P>, visualCtor: (materialId: number) => ComplexVisual<P>): StructureRepresentation<P> {
  20. let version = 0
  21. const updated = new Subject<number>()
  22. const materialId = getNextMaterialId()
  23. const renderObjects: GraphicsRenderObject[] = []
  24. const _state = StructureRepresentationStateBuilder.create()
  25. let visual: ComplexVisual<P> | undefined
  26. let _structure: Structure
  27. let _params: P
  28. let _props: PD.Values<P>
  29. let _theme = createEmptyTheme()
  30. function createOrUpdate(props: Partial<PD.Values<P>> = {}, structure?: Structure) {
  31. if (structure && structure !== _structure) {
  32. _params = getParams(ctx, structure)
  33. _structure = structure
  34. if (!_props) _props = PD.getDefaultValues(_params)
  35. }
  36. _props = Object.assign({}, _props, props)
  37. return Task.create('Creating or updating ComplexRepresentation', async runtime => {
  38. if (!visual) visual = visualCtor(materialId)
  39. const promise = visual.createOrUpdate({ webgl: ctx.webgl, runtime }, _theme, _props, structure)
  40. if (promise) await promise
  41. // update list of renderObjects
  42. renderObjects.length = 0
  43. if (visual && visual.renderObject) renderObjects.push(visual.renderObject)
  44. // increment version
  45. updated.next(version++)
  46. });
  47. }
  48. function getLoci(pickingId: PickingId) {
  49. return visual ? visual.getLoci(pickingId) : EmptyLoci
  50. }
  51. function mark(loci: Loci, action: MarkerAction) {
  52. if (!_structure) return false
  53. if (StructureElement.Loci.is(loci) || Link.isLoci(loci)) {
  54. if (!Structure.areRootsEquivalent(loci.structure, _structure)) return false
  55. // Remap `loci` from equivalent structure to the current `_structure`
  56. loci = Loci.remap(loci, _structure)
  57. if (Loci.isEmpty(loci)) return false
  58. } else if (isEveryLoci(loci)) {
  59. // pass through
  60. } else {
  61. return false
  62. }
  63. return visual ? visual.mark(loci, action) : false
  64. }
  65. function setState(state: Partial<StructureRepresentationState>) {
  66. StructureRepresentationStateBuilder.update(_state, state)
  67. if (state.visible !== undefined && visual) {
  68. // hide visual when _unitTransforms is set
  69. visual.setVisibility(state.visible && _state.unitTransforms === null)
  70. }
  71. if (state.alphaFactor !== undefined && visual) visual.setAlphaFactor(state.alphaFactor)
  72. if (state.pickable !== undefined && visual) visual.setPickable(state.pickable)
  73. if (state.overpaint !== undefined && visual) {
  74. // Remap loci from equivalent structure to the current structure
  75. const remappedOverpaint = Overpaint.remap(state.overpaint, _structure)
  76. visual.setOverpaint(remappedOverpaint)
  77. }
  78. if (state.transparency !== undefined && visual) visual.setTransparency(state.transparency)
  79. if (state.transform !== undefined && visual) visual.setTransform(state.transform)
  80. if (state.unitTransforms !== undefined && visual) {
  81. // Since ComplexVisuals always renders geometries between units the application of `unitTransforms`
  82. // does not make sense. When given it is ignored here and sets the visual's visibility to `false`.
  83. visual.setVisibility(_state.visible && state.unitTransforms === null)
  84. }
  85. }
  86. function setTheme(theme: Theme) {
  87. _theme = theme
  88. }
  89. function destroy() {
  90. if (visual) visual.destroy()
  91. }
  92. return {
  93. label,
  94. get groupCount() {
  95. return visual ? visual.groupCount : 0
  96. },
  97. get props() { return _props },
  98. get params() { return _params },
  99. get state() { return _state },
  100. get theme() { return _theme },
  101. renderObjects,
  102. updated,
  103. createOrUpdate,
  104. setState,
  105. setTheme,
  106. getLoci,
  107. mark,
  108. destroy
  109. }
  110. }