volume-view.ts 2.8 KB

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