volume-view.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 Viewer from 'mol-view/viewer';
  7. import { BehaviorSubject } from 'rxjs';
  8. import { App } from './app';
  9. import { Progress } from 'mol-task';
  10. import { VolumeData } from 'mol-model/volume';
  11. import { VolumeRepresentation } from 'mol-geo/representation/volume';
  12. import IsosurfaceVisual from 'mol-geo/representation/volume/isosurface';
  13. import { Vec3 } from 'mol-math/linear-algebra';
  14. export interface VolumeView {
  15. readonly app: App
  16. readonly viewer: Viewer
  17. readonly label: string
  18. readonly volume: VolumeData
  19. readonly active: { [k: string]: boolean }
  20. readonly volumeRepresentations: { [k: string]: VolumeRepresentation<any> }
  21. readonly updated: BehaviorSubject<null>
  22. setVolumeRepresentation(name: string, value: boolean): void
  23. destroy: () => void
  24. }
  25. interface StructureViewProps {
  26. assemblyId?: string
  27. symmetryFeatureId?: number
  28. }
  29. export async function VolumeView(app: App, viewer: Viewer, volume: VolumeData, props: StructureViewProps = {}): Promise<VolumeView> {
  30. const active: { [k: string]: boolean } = {
  31. isosurface: true,
  32. volume: false,
  33. }
  34. const volumeRepresentations: { [k: string]: VolumeRepresentation<any> } = {
  35. isosurface: VolumeRepresentation(IsosurfaceVisual),
  36. }
  37. const updated: BehaviorSubject<null> = new BehaviorSubject<null>(null)
  38. let label: string = 'Volume'
  39. async function setVolumeRepresentation(k: string, value: boolean) {
  40. active[k] = value
  41. await createVolumeRepr()
  42. }
  43. async function createVolumeRepr() {
  44. for (const k in volumeRepresentations) {
  45. if (active[k]) {
  46. await app.runTask(volumeRepresentations[k].createOrUpdate({}, volume).run(
  47. progress => console.log(Progress.format(progress))
  48. ), 'Create/update representation')
  49. viewer.add(volumeRepresentations[k])
  50. } else {
  51. viewer.remove(volumeRepresentations[k])
  52. }
  53. }
  54. // const center = Vec3.clone(volume.cell.size)
  55. // Vec3.scale(center, center, 0.5)
  56. // viewer.center(center)
  57. updated.next(null)
  58. viewer.requestDraw(true)
  59. console.log('stats', viewer.stats)
  60. }
  61. await createVolumeRepr()
  62. return {
  63. app,
  64. viewer,
  65. get label() { return label },
  66. volume,
  67. active,
  68. volumeRepresentations,
  69. setVolumeRepresentation,
  70. updated,
  71. destroy: () => {
  72. for (const k in volumeRepresentations) {
  73. viewer.remove(volumeRepresentations[k])
  74. volumeRepresentations[k].destroy()
  75. }
  76. viewer.requestDraw(true)
  77. }
  78. }
  79. }