representation.ts 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 { Representation, Visual, RepresentationContext, VisualContext, RepresentationProvider, RepresentationParamsGetter } from '../representation';
  8. import { VolumeData } from 'mol-model/volume';
  9. import { Loci, EmptyLoci, isEveryLoci } from 'mol-model/loci';
  10. import { Geometry, updateRenderableState } from 'mol-geo/geometry/geometry';
  11. import { ParamDefinition as PD } from 'mol-util/param-definition';
  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. import { Theme, createTheme } from 'mol-theme/theme';
  22. import { Subject } from 'rxjs';
  23. export interface VolumeVisual<P extends VolumeParams> extends Visual<VolumeData, P> { }
  24. type VolumeRenderObject = MeshRenderObject | LinesRenderObject | PointsRenderObject | DirectVolumeRenderObject
  25. interface VolumeVisualBuilder<P extends VolumeParams, G extends Geometry> {
  26. defaultProps: PD.Values<P>
  27. createGeometry(ctx: VisualContext, volumeData: VolumeData, props: PD.Values<P>, geometry?: G): Promise<G>
  28. getLoci(pickingId: PickingId, id: number): Loci
  29. mark(loci: Loci, apply: (interval: Interval) => boolean): boolean
  30. setUpdateState(state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>): void
  31. }
  32. interface VolumeVisualGeometryBuilder<P extends VolumeParams, G extends Geometry> extends VolumeVisualBuilder<P, G> {
  33. createRenderObject(ctx: VisualContext, geometry: G, locationIt: LocationIterator, theme: Theme, currentProps: PD.Values<P>): Promise<VolumeRenderObject>
  34. updateValues(values: RenderableValues, geometry: G, newProps: PD.Values<P>): void
  35. }
  36. export function VolumeVisual<P extends VolumeParams>(builder: VolumeVisualGeometryBuilder<P, Geometry>): VolumeVisual<P> {
  37. const { defaultProps, createGeometry, getLoci, mark, setUpdateState } = builder
  38. const { createRenderObject, updateValues } = builder
  39. const updateState = VisualUpdateState.create()
  40. let currentProps: PD.Values<P>
  41. let renderObject: VolumeRenderObject | undefined
  42. let currentVolume: VolumeData
  43. let geometry: Geometry
  44. let locationIt: LocationIterator
  45. async function create(ctx: VisualContext, volume: VolumeData, theme: Theme, props: Partial<PD.Values<P>> = {}) {
  46. currentProps = Object.assign({}, defaultProps, props)
  47. geometry = await createGeometry(ctx, volume, currentProps, geometry)
  48. locationIt = LocationIterator(1, 1, () => NullLocation)
  49. renderObject = await createRenderObject(ctx, geometry, locationIt, theme, currentProps)
  50. }
  51. async function update(ctx: VisualContext, theme: Theme, props: Partial<PD.Values<P>> = {}) {
  52. if (!renderObject) return
  53. const newProps = Object.assign({}, currentProps, props)
  54. VisualUpdateState.reset(updateState)
  55. setUpdateState(updateState, newProps, currentProps)
  56. if (updateState.createGeometry) {
  57. geometry = await createGeometry(ctx, currentVolume, currentProps, geometry)
  58. ValueCell.update(renderObject.values.drawCount, Geometry.getDrawCount(geometry))
  59. }
  60. updateValues(renderObject.values, geometry, newProps)
  61. updateRenderableState(renderObject.state, newProps)
  62. currentProps = newProps
  63. }
  64. return {
  65. get groupCount() { return locationIt ? locationIt.count : 0 },
  66. get renderObject () { return renderObject },
  67. async createOrUpdate(ctx: VisualContext, theme: Theme, props: Partial<PD.Values<P>> = {}, volume?: VolumeData) {
  68. if (!volume && !currentVolume) {
  69. throw new Error('missing volume')
  70. } else if (volume && (!currentVolume || !renderObject)) {
  71. currentVolume = volume
  72. await create(ctx, volume, theme, props)
  73. } else if (volume && volume !== currentVolume) {
  74. currentVolume = volume
  75. await create(ctx, volume, theme, props)
  76. } else {
  77. await update(ctx, theme, props)
  78. }
  79. currentProps = Object.assign({}, defaultProps, props)
  80. },
  81. getLoci(pickingId: PickingId) {
  82. return renderObject ? getLoci(pickingId, renderObject.id) : EmptyLoci
  83. },
  84. mark(loci: Loci, action: MarkerAction) {
  85. if (!renderObject) return false
  86. const { tMarker } = renderObject.values
  87. const { groupCount, instanceCount } = locationIt
  88. function apply(interval: Interval) {
  89. const start = Interval.start(interval)
  90. const end = Interval.end(interval)
  91. return applyMarkerAction(tMarker.ref.value.array, start, end, action)
  92. }
  93. let changed = false
  94. if (isEveryLoci(loci)) {
  95. changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
  96. } else {
  97. changed = mark(loci, apply)
  98. }
  99. if (changed) {
  100. ValueCell.update(tMarker, tMarker.ref.value)
  101. }
  102. return changed
  103. },
  104. setVisibility(value: boolean) {
  105. if (renderObject) renderObject.state.visible = value
  106. },
  107. setPickable(value: boolean) {
  108. if (renderObject) renderObject.state.pickable = value
  109. },
  110. destroy() {
  111. // TODO
  112. renderObject = undefined
  113. }
  114. }
  115. }
  116. export interface VolumeRepresentation<P extends VolumeParams> extends Representation<VolumeData, P> { }
  117. export type VolumeRepresentationProvider<P extends VolumeParams> = RepresentationProvider<VolumeData, P>
  118. //
  119. export const VolumeParams = {
  120. ...Geometry.Params,
  121. isoValue: PD.Numeric(0.22, { min: -1, max: 1, step: 0.01 }),
  122. }
  123. export type VolumeParams = typeof VolumeParams
  124. export function VolumeRepresentation<P extends VolumeParams>(label: string, ctx: RepresentationContext, getParams: RepresentationParamsGetter<VolumeData, P>, visualCtor: (volume: VolumeData) => VolumeVisual<P>): VolumeRepresentation<P> {
  125. let version = 0
  126. const updated = new Subject<number>()
  127. const _state = Representation.createState()
  128. let visual: VolumeVisual<P>
  129. let _volume: VolumeData
  130. let _props: PD.Values<P>
  131. let _params: P
  132. let _theme: Theme
  133. let busy = false
  134. function createOrUpdate(props: Partial<PD.Values<P>> = {}, volume?: VolumeData) {
  135. if (volume && volume !== _volume) {
  136. _params = getParams(ctx, volume)
  137. _volume = volume
  138. if (!_props) _props = PD.getDefaultValues(_params)
  139. }
  140. _props = Object.assign({}, _props, props)
  141. _theme = createTheme(ctx, _props, {}, _theme)
  142. return Task.create('VolumeRepresentation.create', async runtime => {
  143. // TODO queue it somehow
  144. if (busy) return
  145. if (!visual && !volume) {
  146. throw new Error('volume data missing')
  147. } else if (volume && !visual) {
  148. busy = true
  149. visual = visualCtor(volume)
  150. await visual.createOrUpdate({ webgl: ctx.webgl, runtime }, _theme, _props, volume)
  151. busy = false
  152. } else {
  153. busy = true
  154. await visual.createOrUpdate({ webgl: ctx.webgl, runtime }, _theme, _props, volume)
  155. busy = false
  156. }
  157. updated.next(version++)
  158. });
  159. }
  160. function getLoci(pickingId: PickingId) {
  161. return visual ? visual.getLoci(pickingId) : EmptyLoci
  162. }
  163. function mark(loci: Loci, action: MarkerAction) {
  164. return visual ? visual.mark(loci, action) : false
  165. }
  166. function setState(state: Partial<Representation.State>) {
  167. if (state.visible !== undefined && visual) visual.setVisibility(state.visible)
  168. if (state.pickable !== undefined && visual) visual.setPickable(state.pickable)
  169. Representation.updateState(_state, state)
  170. }
  171. function destroy() {
  172. if (visual) visual.destroy()
  173. }
  174. return {
  175. label,
  176. get groupCount() {
  177. return visual ? visual.groupCount : 0
  178. },
  179. get renderObjects() {
  180. return visual && visual.renderObject ? [ visual.renderObject ] : []
  181. },
  182. get props () { return _props },
  183. get params() { return _params },
  184. get state() { return _state },
  185. updated,
  186. createOrUpdate,
  187. setState,
  188. getLoci,
  189. mark,
  190. destroy
  191. }
  192. }