index.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 { Task } from 'mol-task'
  7. import { RepresentationProps, Representation, Visual, RepresentationContext, VisualContext } from '..';
  8. import { VolumeData, VolumeIsoValue } from 'mol-model/volume';
  9. import { Loci, EmptyLoci, isEveryLoci } from 'mol-model/loci';
  10. import { paramDefaultValues, RangeParam } from 'mol-util/parameter';
  11. import { Geometry, updateRenderableState } from 'mol-geo/geometry/geometry';
  12. import { PickingId } from 'mol-geo/geometry/picking';
  13. import { MarkerAction, applyMarkerAction } from 'mol-geo/geometry/marker-data';
  14. import { DirectVolumeRenderObject, PointsRenderObject, LinesRenderObject, MeshRenderObject } from 'mol-gl/render-object';
  15. import { Interval } from 'mol-data/int';
  16. import { RenderableValues } from 'mol-gl/renderable/schema';
  17. import { LocationIterator } from 'mol-geo/util/location-iterator';
  18. import { NullLocation } from 'mol-model/location';
  19. import { VisualUpdateState } from 'mol-repr/util';
  20. import { ValueCell } from 'mol-util';
  21. export interface VolumeVisual<P extends RepresentationProps = {}> extends Visual<VolumeData, P> { }
  22. type VolumeRenderObject = MeshRenderObject | LinesRenderObject | PointsRenderObject | DirectVolumeRenderObject
  23. interface VolumeVisualBuilder<P extends VolumeProps, G extends Geometry> {
  24. defaultProps: P
  25. createGeometry(ctx: VisualContext, volumeData: VolumeData, props: P, geometry?: G): Promise<G>
  26. getLoci(pickingId: PickingId, id: number): Loci
  27. mark(loci: Loci, apply: (interval: Interval) => boolean): boolean
  28. setUpdateState(state: VisualUpdateState, newProps: P, currentProps: P): void
  29. }
  30. interface VolumeVisualGeometryBuilder<P extends VolumeProps, G extends Geometry> extends VolumeVisualBuilder<P, G> {
  31. createRenderObject(ctx: VisualContext, geometry: G, locationIt: LocationIterator, currentProps: P): Promise<VolumeRenderObject>
  32. updateValues(values: RenderableValues, newProps: P): void
  33. }
  34. export function VolumeVisual<P extends VolumeProps>(builder: VolumeVisualGeometryBuilder<P, Geometry>) {
  35. const { defaultProps, createGeometry, getLoci, mark, setUpdateState } = builder
  36. const { createRenderObject, updateValues } = builder
  37. const updateState = VisualUpdateState.create()
  38. let currentProps: P
  39. let renderObject: VolumeRenderObject | undefined
  40. let currentVolume: VolumeData
  41. let geometry: Geometry
  42. let locationIt: LocationIterator
  43. async function create(ctx: VisualContext, volume: VolumeData, props: Partial<VolumeProps> = {}) {
  44. currentProps = Object.assign({}, defaultProps, props)
  45. if (props.isoValueRelative) {
  46. currentProps.isoValueAbsolute = VolumeIsoValue.calcAbsolute(currentVolume.dataStats, props.isoValueRelative)
  47. console.log('create props.isoValueRelative', props.isoValueRelative, currentProps.isoValueAbsolute, currentVolume.dataStats)
  48. }
  49. geometry = await createGeometry(ctx, volume, currentProps, geometry)
  50. locationIt = LocationIterator(1, 1, () => NullLocation)
  51. renderObject = await createRenderObject(ctx, geometry, locationIt, currentProps)
  52. }
  53. async function update(ctx: VisualContext, props: Partial<VolumeProps> = {}) {
  54. if (!renderObject) return
  55. const newProps = Object.assign({}, currentProps, props)
  56. if (props.isoValueRelative) {
  57. newProps.isoValueAbsolute = VolumeIsoValue.calcAbsolute(currentVolume.dataStats, props.isoValueRelative)
  58. console.log('update props.isoValueRelative', props.isoValueRelative, newProps.isoValueAbsolute, currentVolume.dataStats)
  59. }
  60. VisualUpdateState.reset(updateState)
  61. setUpdateState(updateState, newProps, currentProps)
  62. if (updateState.createGeometry) {
  63. geometry = await createGeometry(ctx, currentVolume, currentProps, geometry)
  64. ValueCell.update(renderObject.values.drawCount, Geometry.getDrawCount(geometry))
  65. }
  66. updateValues(renderObject.values, newProps)
  67. updateRenderableState(renderObject.state, newProps)
  68. currentProps = newProps
  69. }
  70. return {
  71. get renderObject () { return renderObject },
  72. async createOrUpdate(ctx: VisualContext, props: Partial<VolumeProps> = {}, volume?: VolumeData) {
  73. if (!volume && !currentVolume) {
  74. throw new Error('missing volume')
  75. } else if (volume && (!currentVolume || !renderObject)) {
  76. currentVolume = volume
  77. await create(ctx, volume, props)
  78. } else if (volume && volume !== currentVolume) {
  79. currentVolume = volume
  80. await create(ctx, volume, props)
  81. } else {
  82. await update(ctx, props)
  83. }
  84. currentProps = Object.assign({}, defaultProps, props)
  85. },
  86. getLoci(pickingId: PickingId) {
  87. return renderObject ? getLoci(pickingId, renderObject.id) : EmptyLoci
  88. },
  89. mark(loci: Loci, action: MarkerAction) {
  90. if (!renderObject) return false
  91. const { tMarker } = renderObject.values
  92. const { groupCount, instanceCount } = locationIt
  93. function apply(interval: Interval) {
  94. const start = Interval.start(interval)
  95. const end = Interval.end(interval)
  96. return applyMarkerAction(tMarker.ref.value.array, start, end, action)
  97. }
  98. let changed = false
  99. if (isEveryLoci(loci)) {
  100. changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
  101. } else {
  102. changed = mark(loci, apply)
  103. }
  104. if (changed) {
  105. ValueCell.update(tMarker, tMarker.ref.value)
  106. }
  107. return changed
  108. },
  109. destroy() {
  110. // TODO
  111. renderObject = undefined
  112. }
  113. }
  114. }
  115. export interface VolumeRepresentation<P extends RepresentationProps = {}> extends Representation<VolumeData, P> { }
  116. export const VolumeParams = {
  117. ...Geometry.Params,
  118. isoValueAbsolute: RangeParam('Iso Value Absolute', '', 0.22, -1, 1, 0.01),
  119. isoValueRelative: RangeParam('Iso Value Relative', '', 2, -10, 10, 0.1),
  120. }
  121. export const DefaultVolumeProps = paramDefaultValues(VolumeParams)
  122. export type VolumeProps = typeof DefaultVolumeProps
  123. export function VolumeRepresentation<P extends VolumeProps>(visualCtor: (volumeData: VolumeData) => VolumeVisual<P>): VolumeRepresentation<P> {
  124. let visual: VolumeVisual<any>
  125. let _props: P
  126. let busy = false
  127. function createOrUpdate(ctx: RepresentationContext, props: Partial<P> = {}, volumeData?: VolumeData) {
  128. _props = Object.assign({}, DefaultVolumeProps, _props, props)
  129. return Task.create('VolumeRepresentation.create', async runtime => {
  130. // TODO queue it somehow
  131. if (busy) return
  132. if (!visual && !volumeData) {
  133. throw new Error('volumeData missing')
  134. } else if (volumeData && !visual) {
  135. busy = true
  136. visual = visualCtor(volumeData)
  137. await visual.createOrUpdate({ ...ctx, runtime } , props, volumeData)
  138. busy = false
  139. } else {
  140. busy = true
  141. await visual.createOrUpdate({ ...ctx, runtime }, props, volumeData)
  142. busy = false
  143. }
  144. });
  145. }
  146. return {
  147. label: 'Volume',
  148. params: VolumeParams,
  149. get renderObjects() {
  150. return visual && visual.renderObject ? [ visual.renderObject ] : []
  151. },
  152. get props () { return _props },
  153. createOrUpdate,
  154. getLoci(pickingId: PickingId) {
  155. // TODO
  156. return EmptyLoci
  157. },
  158. mark(loci: Loci, action: MarkerAction) {
  159. // TODO
  160. return false
  161. },
  162. destroy() {
  163. // TODO
  164. }
  165. }
  166. }