units-representation.ts 6.2 KB

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