complex-visual.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 { Structure } from 'mol-model/structure';
  7. import { Visual, VisualContext } from '../visual';
  8. import { createRenderObject, GraphicsRenderObject } from 'mol-gl/render-object';
  9. import { UnitKind, UnitKindOptions } from './visual/util/common';
  10. import { StructureMeshParams, StructureParams, StructureDirectVolumeParams } from './representation';
  11. import { deepEqual, ValueCell } from 'mol-util';
  12. import { Loci, isEveryLoci, EmptyLoci } from 'mol-model/loci';
  13. import { Interval } from 'mol-data/int';
  14. import { ParamDefinition as PD } from 'mol-util/param-definition';
  15. import { createSizes } from 'mol-geo/geometry/size-data';
  16. import { Geometry, GeometryUtils } from 'mol-geo/geometry/geometry';
  17. import { LocationIterator } from 'mol-geo/util/location-iterator';
  18. import { PickingId } from 'mol-geo/geometry/picking';
  19. import { createColors } from 'mol-geo/geometry/color-data';
  20. import { MarkerAction, applyMarkerAction } from 'mol-geo/geometry/marker-data';
  21. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  22. import { VisualUpdateState } from 'mol-repr/util';
  23. import { Theme, createEmptyTheme } from 'mol-theme/theme';
  24. import { ColorTheme } from 'mol-theme/color';
  25. import { SizeTheme } from 'mol-theme/size';
  26. import { UnitsParams } from './units-representation';
  27. import { DirectVolume } from 'mol-geo/geometry/direct-volume/direct-volume';
  28. import { Mat4 } from 'mol-math/linear-algebra';
  29. import { createIdentityTransform } from 'mol-geo/geometry/transform-data';
  30. import { Overpaint } from 'mol-theme/overpaint';
  31. import { applyOverpaintColor, createOverpaint, clearOverpaint } from 'mol-geo/geometry/overpaint-data';
  32. export interface ComplexVisual<P extends StructureParams> extends Visual<Structure, P> { }
  33. function createComplexRenderObject<G extends Geometry>(structure: Structure, geometry: G, locationIt: LocationIterator, theme: Theme, props: PD.Values<Geometry.Params<G>>) {
  34. const { createValues, createRenderableState } = Geometry.getUtils(geometry)
  35. const transform = createIdentityTransform()
  36. const values = createValues(geometry, transform, locationIt, theme, props)
  37. const state = createRenderableState(props)
  38. return createRenderObject(geometry.kind, values, state)
  39. }
  40. const ComplexParams = {
  41. ...StructureParams,
  42. unitKinds: PD.MultiSelect<UnitKind>(['atomic', 'spheres'], UnitKindOptions),
  43. }
  44. type ComplexParams = typeof ComplexParams
  45. interface ComplexVisualBuilder<P extends ComplexParams, G extends Geometry> {
  46. defaultProps: PD.Values<P>
  47. createGeometry(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<P>, geometry?: G): Promise<G> | G
  48. createLocationIterator(structure: Structure): LocationIterator
  49. getLoci(pickingId: PickingId, structure: Structure, id: number): Loci
  50. eachLocation(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean): boolean,
  51. setUpdateState(state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme): void
  52. }
  53. interface ComplexVisualGeometryBuilder<P extends UnitsParams, G extends Geometry> extends ComplexVisualBuilder<P, G> {
  54. geometryUtils: GeometryUtils<G>
  55. }
  56. export function ComplexVisual<G extends Geometry, P extends ComplexParams & Geometry.Params<G>>(builder: ComplexVisualGeometryBuilder<P, G>): ComplexVisual<P> {
  57. const { defaultProps, createGeometry, createLocationIterator, getLoci, eachLocation, setUpdateState } = builder
  58. const { updateValues, updateBoundingSphere, updateRenderableState } = builder.geometryUtils
  59. const updateState = VisualUpdateState.create()
  60. let renderObject: GraphicsRenderObject | undefined
  61. let newProps: PD.Values<P>
  62. let newTheme: Theme
  63. let newStructure: Structure
  64. let currentProps: PD.Values<P> = Object.assign({}, defaultProps)
  65. let currentTheme: Theme = createEmptyTheme()
  66. let currentStructure: Structure
  67. let geometry: G
  68. let locationIt: LocationIterator
  69. function prepareUpdate(theme: Theme, props: Partial<PD.Values<P>>, structure: Structure) {
  70. if (!structure && !currentStructure) {
  71. throw new Error('missing structure')
  72. }
  73. newProps = Object.assign({}, currentProps, props)
  74. newTheme = theme
  75. newStructure = structure
  76. VisualUpdateState.reset(updateState)
  77. if (!renderObject) {
  78. updateState.createNew = true
  79. } else if (!currentStructure || !Structure.areEquivalent(newStructure, currentStructure)) {
  80. updateState.createNew = true
  81. }
  82. if (updateState.createNew) {
  83. updateState.createGeometry = true
  84. return
  85. }
  86. setUpdateState(updateState, newProps, currentProps, newTheme, currentTheme)
  87. if (Structure.conformationHash(newStructure) !== Structure.conformationHash(currentStructure)) {
  88. updateState.createGeometry = true
  89. }
  90. if (!ColorTheme.areEqual(theme.color, currentTheme.color)) updateState.updateColor = true
  91. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createGeometry = true
  92. if (updateState.createGeometry) {
  93. updateState.updateColor = true
  94. }
  95. }
  96. function update(newGeometry?: G) {
  97. if (updateState.createNew) {
  98. locationIt = createLocationIterator(newStructure)
  99. if (newGeometry) {
  100. renderObject = createComplexRenderObject(newStructure, newGeometry, locationIt, newTheme, newProps)
  101. } else {
  102. throw new Error('expected geometry to be given')
  103. }
  104. } else {
  105. if (!renderObject) {
  106. throw new Error('expected renderObject to be available')
  107. }
  108. locationIt.reset()
  109. if (updateState.createGeometry) {
  110. if (newGeometry) {
  111. ValueCell.update(renderObject.values.drawCount, Geometry.getDrawCount(newGeometry))
  112. updateBoundingSphere(renderObject.values, newGeometry)
  113. } else {
  114. throw new Error('expected geometry to be given')
  115. }
  116. }
  117. if (updateState.updateSize) {
  118. // not all geometries have size data, so check here
  119. if ('uSize' in renderObject.values) {
  120. createSizes(locationIt, newTheme.size, renderObject.values)
  121. }
  122. }
  123. if (updateState.updateColor) {
  124. createColors(locationIt, newTheme.color, renderObject.values)
  125. }
  126. updateValues(renderObject.values, newProps)
  127. updateRenderableState(renderObject.state, newProps)
  128. }
  129. currentProps = newProps
  130. currentTheme = newTheme
  131. currentStructure = newStructure
  132. if (newGeometry) geometry = newGeometry
  133. }
  134. return {
  135. get groupCount() { return locationIt ? locationIt.count : 0 },
  136. get renderObject () { return locationIt && locationIt.count ? renderObject : undefined },
  137. createOrUpdate(ctx: VisualContext, theme: Theme, props: Partial<PD.Values<P>> = {}, structure?: Structure) {
  138. prepareUpdate(theme, props, structure || currentStructure)
  139. if (updateState.createGeometry) {
  140. const newGeometry = createGeometry(ctx, newStructure, newTheme, newProps, geometry)
  141. return newGeometry instanceof Promise ? newGeometry.then(update) : update(newGeometry)
  142. } else {
  143. update()
  144. }
  145. },
  146. getLoci(pickingId: PickingId) {
  147. return renderObject ? getLoci(pickingId, currentStructure, renderObject.id) : EmptyLoci
  148. },
  149. mark(loci: Loci, action: MarkerAction) {
  150. if (!renderObject) return false
  151. const { tMarker } = renderObject.values
  152. const { groupCount, instanceCount } = locationIt
  153. function apply(interval: Interval) {
  154. const start = Interval.start(interval)
  155. const end = Interval.end(interval)
  156. return applyMarkerAction(tMarker.ref.value.array, start, end, action)
  157. }
  158. let changed = false
  159. if (isEveryLoci(loci) || (Structure.isLoci(loci) && Structure.areEquivalent(loci.structure, currentStructure))) {
  160. changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
  161. } else {
  162. changed = eachLocation(loci, currentStructure, apply)
  163. }
  164. if (changed) {
  165. ValueCell.update(tMarker, tMarker.ref.value)
  166. }
  167. return changed
  168. },
  169. setVisibility(visible: boolean) {
  170. Visual.setVisibility(renderObject, visible)
  171. },
  172. setAlphaFactor(alphaFactor: number) {
  173. Visual.setAlphaFactor(renderObject, alphaFactor)
  174. },
  175. setPickable(pickable: boolean) {
  176. Visual.setPickable(renderObject, pickable)
  177. },
  178. setTransform(matrix?: Mat4, instanceMatrices?: Float32Array | null) {
  179. Visual.setTransform(renderObject, matrix, instanceMatrices)
  180. },
  181. setOverpaint(layers: Overpaint.Layers, clear = false) {
  182. if (!renderObject) return false
  183. const { tOverpaint } = renderObject.values
  184. const count = locationIt.groupCount * locationIt.instanceCount
  185. // ensure texture has right size
  186. createOverpaint(layers.list.length ? count : 0, renderObject.values)
  187. // clear if requested
  188. if (clear) clearOverpaint(tOverpaint.ref.value.array, 0, count)
  189. for (let i = 0, il = layers.list.length; i < il; ++i) {
  190. const { loci, color } = layers.list[i]
  191. const apply = (interval: Interval) => {
  192. const start = Interval.start(interval)
  193. const end = Interval.end(interval)
  194. return applyOverpaintColor(tOverpaint.ref.value.array, start, end, color, layers.alpha)
  195. }
  196. if (isEveryLoci(loci) || (Structure.isLoci(loci) && Structure.areEquivalent(loci.structure, currentStructure))) {
  197. apply(Interval.ofBounds(0, count))
  198. } else {
  199. eachLocation(loci, currentStructure, apply)
  200. }
  201. }
  202. ValueCell.update(tOverpaint, tOverpaint.ref.value)
  203. },
  204. destroy() {
  205. // TODO
  206. renderObject = undefined
  207. }
  208. }
  209. }
  210. // mesh
  211. export const ComplexMeshParams = {
  212. ...StructureMeshParams,
  213. unitKinds: PD.MultiSelect<UnitKind>([ 'atomic', 'spheres' ], UnitKindOptions),
  214. }
  215. export type ComplexMeshParams = typeof ComplexMeshParams
  216. export interface ComplexMeshVisualBuilder<P extends ComplexMeshParams> extends ComplexVisualBuilder<P, Mesh> { }
  217. export function ComplexMeshVisual<P extends ComplexMeshParams>(builder: ComplexMeshVisualBuilder<P>): ComplexVisual<P> {
  218. return ComplexVisual<Mesh, StructureMeshParams & UnitsParams>({
  219. ...builder,
  220. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme) => {
  221. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme)
  222. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true
  223. },
  224. geometryUtils: Mesh.Utils
  225. })
  226. }
  227. // direct-volume
  228. export const ComplexDirectVolumeParams = {
  229. ...StructureDirectVolumeParams,
  230. unitKinds: PD.MultiSelect<UnitKind>(['atomic', 'spheres', 'gaussians'], UnitKindOptions),
  231. }
  232. export type ComplexDirectVolumeParams = typeof ComplexDirectVolumeParams
  233. export interface ComplexDirectVolumeVisualBuilder<P extends ComplexDirectVolumeParams> extends ComplexVisualBuilder<P, DirectVolume> { }
  234. export function ComplexDirectVolumeVisual<P extends ComplexDirectVolumeParams>(builder: ComplexDirectVolumeVisualBuilder<P>): ComplexVisual<P> {
  235. return ComplexVisual<DirectVolume, StructureDirectVolumeParams & UnitsParams>({
  236. ...builder,
  237. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme) => {
  238. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme)
  239. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true
  240. },
  241. geometryUtils: DirectVolume.Utils
  242. })
  243. }