units-representation.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { Structure, Unit } from 'mol-model/structure';
  8. import { Task } from 'mol-task'
  9. import { RenderObject } from 'mol-gl/render-object';
  10. import { Representation, RepresentationProps, Visual } from '..';
  11. import { PickingId } from '../../util/picking';
  12. import { Loci, EmptyLoci, isEmptyLoci } from 'mol-model/loci';
  13. import { MarkerAction } from '../../util/marker-data';
  14. import { getQualityProps } from '../util';
  15. import { StructureProps } from '.';
  16. export interface UnitsVisual<P extends RepresentationProps = {}> extends Visual<Unit.SymmetryGroup, P> { }
  17. export interface StructureVisual<P extends RepresentationProps = {}> extends Visual<Structure, P> { }
  18. export interface StructureRepresentation<P extends RepresentationProps = {}> extends Representation<Structure, P> { }
  19. export function UnitsRepresentation<P extends StructureProps>(label: string, visualCtor: () => UnitsVisual<P>): StructureRepresentation<P> {
  20. let visuals = new Map<number, { group: Unit.SymmetryGroup, visual: UnitsVisual<P> }>()
  21. let _props: P
  22. let _structure: Structure
  23. let _groups: ReadonlyArray<Unit.SymmetryGroup>
  24. function createOrUpdate(props: Partial<P> = {}, structure?: Structure) {
  25. _props = Object.assign({}, _props, props, getQualityProps(props, structure))
  26. _props.colorTheme.structure = structure
  27. return Task.create('Creating or updating StructureRepresentation', async ctx => {
  28. if (!_structure && !structure) {
  29. throw new Error('missing structure')
  30. } else if (structure && !_structure) {
  31. // First call with a structure, create visuals for each group.
  32. _groups = structure.unitSymmetryGroups;
  33. for (let i = 0; i < _groups.length; i++) {
  34. const group = _groups[i];
  35. const visual = visualCtor()
  36. await visual.createOrUpdate(ctx, _props, group)
  37. visuals.set(group.hashCode, { visual, group })
  38. }
  39. } else if (structure && _structure.hashCode !== structure.hashCode) {
  40. // Tries to re-use existing visuals for the groups of the new structure.
  41. // Creates additional visuals if needed, destroys left-over visuals.
  42. _groups = structure.unitSymmetryGroups;
  43. // const newGroups: Unit.SymmetryGroup[] = []
  44. const oldVisuals = visuals
  45. visuals = new Map()
  46. for (let i = 0; i < _groups.length; i++) {
  47. const group = _groups[i];
  48. const visualGroup = oldVisuals.get(group.hashCode)
  49. if (visualGroup) {
  50. const { visual } = visualGroup
  51. await visual.createOrUpdate(ctx, _props, group)
  52. visuals.set(group.hashCode, { visual, group })
  53. oldVisuals.delete(group.hashCode)
  54. } else {
  55. // newGroups.push(group)
  56. const visual = visualCtor()
  57. await visual.createOrUpdate(ctx, _props, group)
  58. visuals.set(group.hashCode, { visual, group })
  59. }
  60. }
  61. oldVisuals.forEach(({ visual }) => visual.destroy())
  62. // For new groups, re-use left-over visuals
  63. // const unusedVisuals: UnitsVisual<P>[] = []
  64. // oldVisuals.forEach(({ visual }) => unusedVisuals.push(visual))
  65. // newGroups.forEach(async group => {
  66. // const visual = unusedVisuals.pop() || visualCtor()
  67. // await visual.createOrUpdate(ctx, _props, group)
  68. // visuals.set(group.hashCode, { visual, group })
  69. // })
  70. // unusedVisuals.forEach(visual => visual.destroy())
  71. } else if (structure && _structure.hashCode === structure.hashCode) {
  72. // Expects that for structures with the same hashCode,
  73. // the unitSymmetryGroups are the same as well.
  74. // Re-uses existing visuals for the groups of the new structure.
  75. _groups = structure.unitSymmetryGroups;
  76. for (let i = 0; i < _groups.length; i++) {
  77. const group = _groups[i];
  78. const visualGroup = visuals.get(group.hashCode)
  79. if (visualGroup) {
  80. await visualGroup.visual.createOrUpdate(ctx, _props, group)
  81. visualGroup.group = group
  82. } else {
  83. throw new Error(`expected to find visual for hashCode ${group.hashCode}`)
  84. }
  85. }
  86. } else {
  87. // No new structure given, just update all visuals with new props.
  88. visuals.forEach(async ({ visual, group }) => {
  89. await visual.createOrUpdate(ctx, _props, group)
  90. })
  91. }
  92. if (structure) _structure = structure
  93. });
  94. }
  95. function getLoci(pickingId: PickingId) {
  96. let loci: Loci = EmptyLoci
  97. visuals.forEach(({ visual }) => {
  98. const _loci = visual.getLoci(pickingId)
  99. if (!isEmptyLoci(_loci)) loci = _loci
  100. })
  101. return loci
  102. }
  103. function mark(loci: Loci, action: MarkerAction) {
  104. visuals.forEach(({ visual }) => visual.mark(loci, action))
  105. }
  106. function destroy() {
  107. visuals.forEach(({ visual }) => visual.destroy())
  108. visuals.clear()
  109. }
  110. return {
  111. label,
  112. get renderObjects() {
  113. const renderObjects: RenderObject[] = []
  114. visuals.forEach(({ visual }) => {
  115. if (visual.renderObject) renderObjects.push(visual.renderObject)
  116. })
  117. return renderObjects
  118. },
  119. get props() {
  120. return _props
  121. },
  122. createOrUpdate,
  123. getLoci,
  124. mark,
  125. destroy
  126. }
  127. }