volume-view.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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-canvas3d/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 { IsosurfaceRepresentation } from 'mol-geo/representation/volume/isosurface-mesh';
  13. import { DirectVolumeRepresentation } from 'mol-geo/representation/volume/direct-volume';
  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 VolumeViewProps {
  26. }
  27. export async function VolumeView(app: App, viewer: Viewer, volume: VolumeData, props: VolumeViewProps = {}): Promise<VolumeView> {
  28. const active: { [k: string]: boolean } = {
  29. isosurface: true,
  30. directVolume: false,
  31. }
  32. const volumeRepresentations: { [k: string]: VolumeRepresentation<any> } = {
  33. isosurface: IsosurfaceRepresentation(),
  34. directVolume: DirectVolumeRepresentation(),
  35. }
  36. const updated: BehaviorSubject<null> = new BehaviorSubject<null>(null)
  37. let label: string = 'Volume'
  38. async function setVolumeRepresentation(k: string, value: boolean) {
  39. active[k] = value
  40. await createVolumeRepr()
  41. }
  42. async function createVolumeRepr() {
  43. for (const k in volumeRepresentations) {
  44. if (active[k]) {
  45. const p = { webgl: viewer.webgl }
  46. await app.runTask(volumeRepresentations[k].createOrUpdate(p, 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. }