complex-representation.ts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * Copyright (c) 2018-2022 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 { ComplexVisual, StructureRepresentation, StructureRepresentationStateBuilder, StructureRepresentationState } from './representation';
  9. import { Representation, RepresentationContext, RepresentationParamsGetter } from '../representation';
  10. import { Structure, StructureElement, Bond } from '../../mol-model/structure';
  11. import { Subject } from 'rxjs';
  12. import { getNextMaterialId, GraphicsRenderObject } from '../../mol-gl/render-object';
  13. import { Theme } from '../../mol-theme/theme';
  14. import { Task } from '../../mol-task';
  15. import { PickingId } from '../../mol-geo/geometry/picking';
  16. import { EmptyLoci, Loci, isEveryLoci, isDataLoci, EveryLoci } from '../../mol-model/loci';
  17. import { MarkerAction, MarkerActions } from '../../mol-util/marker-action';
  18. import { Overpaint } from '../../mol-theme/overpaint';
  19. import { StructureParams } from './params';
  20. import { Clipping } from '../../mol-theme/clipping';
  21. import { Transparency } from '../../mol-theme/transparency';
  22. import { WebGLContext } from '../../mol-gl/webgl/context';
  23. import { Substance } from '../../mol-theme/substance';
  24. export function ComplexRepresentation<P extends StructureParams>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<Structure, P>, visualCtor: (materialId: number, structure: Structure, props: PD.Values<P>, webgl?: WebGLContext) => ComplexVisual<P>): StructureRepresentation<P> {
  25. let version = 0;
  26. const { webgl } = ctx;
  27. const updated = new Subject<number>();
  28. const geometryState = new Representation.GeometryState();
  29. const materialId = getNextMaterialId();
  30. const renderObjects: GraphicsRenderObject[] = [];
  31. const _state = StructureRepresentationStateBuilder.create();
  32. let visual: ComplexVisual<P> | undefined;
  33. let _structure: Structure;
  34. let _params: P;
  35. let _props: PD.Values<P>;
  36. let _theme = Theme.createEmpty();
  37. function createOrUpdate(props: Partial<PD.Values<P>> = {}, structure?: Structure) {
  38. if (structure && structure !== _structure) {
  39. _params = getParams(ctx, structure);
  40. _structure = structure;
  41. if (!_props) _props = PD.getDefaultValues(_params);
  42. }
  43. _props = Object.assign({}, _props, props);
  44. return Task.create('Creating or updating ComplexRepresentation', async runtime => {
  45. let newVisual = false;
  46. if (!visual) {
  47. visual = visualCtor(materialId, _structure, _props, webgl);
  48. newVisual = true;
  49. } else if (visual.mustRecreate?.(_structure, _props, webgl)) {
  50. visual.destroy();
  51. visual = visualCtor(materialId, _structure, _props, webgl);
  52. newVisual = true;
  53. }
  54. const promise = visual.createOrUpdate({ webgl, runtime }, _theme, _props, structure);
  55. if (promise) await promise;
  56. if (newVisual) setState(_state); // current state for new visual
  57. // update list of renderObjects
  58. renderObjects.length = 0;
  59. if (visual && visual.renderObject) {
  60. renderObjects.push(visual.renderObject);
  61. geometryState.add(visual.renderObject.id, visual.geometryVersion);
  62. }
  63. geometryState.snapshot();
  64. // increment version
  65. version += 1;
  66. updated.next(version);
  67. });
  68. }
  69. function getLoci(pickingId: PickingId) {
  70. return visual ? visual.getLoci(pickingId) : EmptyLoci;
  71. }
  72. function getAllLoci() {
  73. return [Structure.Loci(_structure.target)];
  74. }
  75. function mark(loci: Loci, action: MarkerAction) {
  76. if (!_structure) return false;
  77. if (!MarkerActions.is(_state.markerActions, action)) return false;
  78. if (Structure.isLoci(loci) || StructureElement.Loci.is(loci) || Bond.isLoci(loci)) {
  79. if (!Structure.areRootsEquivalent(loci.structure, _structure)) return false;
  80. // Remap `loci` from equivalent structure to the current `_structure`
  81. loci = Loci.remap(loci, _structure);
  82. if (Structure.isLoci(loci) || (StructureElement.Loci.is(loci) && StructureElement.Loci.isWholeStructure(loci))) {
  83. // Change to `EveryLoci` to allow for downstream optimizations
  84. loci = EveryLoci;
  85. }
  86. } else if (!isEveryLoci(loci) && !isDataLoci(loci)) {
  87. return false;
  88. }
  89. if (Loci.isEmpty(loci)) return false;
  90. return visual ? visual.mark(loci, action) : false;
  91. }
  92. function setState(state: Partial<StructureRepresentationState>) {
  93. StructureRepresentationStateBuilder.update(_state, state);
  94. if (state.visible !== undefined && visual) {
  95. // hide visual when _unitTransforms is set and not the identity
  96. visual.setVisibility(state.visible && (_state.unitTransforms === null || _state.unitTransforms.isIdentity));
  97. }
  98. if (state.alphaFactor !== undefined && visual) visual.setAlphaFactor(state.alphaFactor);
  99. if (state.pickable !== undefined && visual) visual.setPickable(state.pickable);
  100. if (state.overpaint !== undefined && visual) {
  101. // Remap loci from equivalent structure to the current structure
  102. const remappedOverpaint = Overpaint.remap(state.overpaint, _structure);
  103. visual.setOverpaint(remappedOverpaint, webgl);
  104. }
  105. if (state.transparency !== undefined && visual) {
  106. // Remap loci from equivalent structure to the current structure
  107. const remappedTransparency = Transparency.remap(state.transparency, _structure);
  108. visual.setTransparency(remappedTransparency, webgl);
  109. }
  110. if (state.substance !== undefined && visual) {
  111. // Remap loci from equivalent structure to the current structure
  112. const remappedSubstance = Substance.remap(state.substance, _structure);
  113. visual.setSubstance(remappedSubstance, webgl);
  114. }
  115. if (state.clipping !== undefined && visual) {
  116. // Remap loci from equivalent structure to the current structure
  117. const remappedClipping = Clipping.remap(state.clipping, _structure);
  118. visual.setClipping(remappedClipping);
  119. }
  120. if (state.themeStrength !== undefined && visual) visual.setThemeStrength(state.themeStrength);
  121. if (state.transform !== undefined && visual) visual.setTransform(state.transform);
  122. if (state.unitTransforms !== undefined && visual) {
  123. // Since ComplexVisuals always renders geometries between units, the application
  124. // of `unitTransforms` does not make sense. When given here and not the identity,
  125. // it is ignored and sets the visual's visibility to `false`.
  126. visual.setVisibility(_state.visible && (state.unitTransforms === null || state.unitTransforms.isIdentity));
  127. }
  128. }
  129. function setTheme(theme: Theme) {
  130. _theme = theme;
  131. }
  132. function destroy() {
  133. if (visual) visual.destroy();
  134. }
  135. return {
  136. label,
  137. get groupCount() {
  138. return visual ? visual.groupCount : 0;
  139. },
  140. get props() { return _props; },
  141. get params() { return _params; },
  142. get state() { return _state; },
  143. get theme() { return _theme; },
  144. get geometryVersion() { return geometryState.version; },
  145. renderObjects,
  146. updated,
  147. createOrUpdate,
  148. setState,
  149. setTheme,
  150. getLoci,
  151. getAllLoci,
  152. mark,
  153. destroy
  154. };
  155. }