units-visual.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 { Unit, Structure } from 'mol-model/structure';
  7. import { RepresentationProps, Visual, VisualContext } from '../representation';
  8. import { StructureMeshParams, StructurePointsParams, StructureLinesParams, StructureDirectVolumeParams } from './representation';
  9. import { Loci, isEveryLoci, EmptyLoci } from 'mol-model/loci';
  10. import { MeshRenderObject, PointsRenderObject, LinesRenderObject, DirectVolumeRenderObject } from 'mol-gl/render-object';
  11. import { createUnitsMeshRenderObject, createUnitsPointsRenderObject, createUnitsTransform, createUnitsLinesRenderObject, createUnitsDirectVolumeRenderObject, includesUnitKind } from './visual/util/common';
  12. import { deepEqual, ValueCell, UUID } from 'mol-util';
  13. import { Interval } from 'mol-data/int';
  14. import { ParamDefinition as PD } from 'mol-util/param-definition';
  15. import { RenderableValues } from 'mol-gl/renderable/schema';
  16. import { Geometry, updateRenderableState } from 'mol-geo/geometry/geometry';
  17. import { LocationIterator } from 'mol-geo/util/location-iterator';
  18. import { PickingId } from 'mol-geo/geometry/picking';
  19. import { createMarkers, MarkerAction, applyMarkerAction } from 'mol-geo/geometry/marker-data';
  20. import { createSizes } from 'mol-geo/geometry/size-data';
  21. import { createColors } from 'mol-geo/geometry/color-data';
  22. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  23. import { Points } from 'mol-geo/geometry/points/points';
  24. import { Lines } from 'mol-geo/geometry/lines/lines';
  25. import { DirectVolume } from 'mol-geo/geometry/direct-volume/direct-volume';
  26. import { VisualUpdateState } from 'mol-repr/util';
  27. import { Theme } from 'mol-theme/theme';
  28. import { ColorTheme } from 'mol-theme/color';
  29. import { SizeTheme } from 'mol-theme/size';
  30. import { UnitsParams } from './units-representation';
  31. export type StructureGroup = { structure: Structure, group: Unit.SymmetryGroup }
  32. export interface UnitsVisual<P extends RepresentationProps = {}> extends Visual<StructureGroup, P> { }
  33. type UnitsRenderObject = MeshRenderObject | LinesRenderObject | PointsRenderObject | DirectVolumeRenderObject
  34. interface UnitsVisualBuilder<P extends UnitsParams, G extends Geometry> {
  35. defaultProps: PD.Values<P>
  36. createGeometry(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<P>, geometry?: G): Promise<G>
  37. createLocationIterator(group: Unit.SymmetryGroup): LocationIterator
  38. getLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number): Loci
  39. mark(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean): boolean
  40. setUpdateState(state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme): void
  41. }
  42. interface UnitsVisualGeometryBuilder<P extends UnitsParams, G extends Geometry> extends UnitsVisualBuilder<P, G> {
  43. createEmptyGeometry(geometry?: G): G
  44. createRenderObject(ctx: VisualContext, group: Unit.SymmetryGroup, geometry: Geometry, locationIt: LocationIterator, theme: Theme, currentProps: PD.Values<P>): Promise<UnitsRenderObject>
  45. updateValues(values: RenderableValues, newProps: PD.Values<P>): void
  46. updateBoundingSphere(values: RenderableValues, geometry: Geometry): void
  47. }
  48. export function UnitsVisual<P extends UnitsParams>(builder: UnitsVisualGeometryBuilder<P, Geometry>): UnitsVisual<P> {
  49. const { defaultProps, createGeometry, createLocationIterator, getLoci, mark, setUpdateState } = builder
  50. const { createEmptyGeometry, createRenderObject, updateValues, updateBoundingSphere } = builder
  51. const updateState = VisualUpdateState.create()
  52. let renderObject: UnitsRenderObject | undefined
  53. let currentProps: PD.Values<P>
  54. let currentTheme: Theme
  55. let geometry: Geometry
  56. let currentGroup: Unit.SymmetryGroup
  57. let currentStructure: Structure
  58. let locationIt: LocationIterator
  59. let currentConformationId: UUID
  60. async function create(ctx: VisualContext, group: Unit.SymmetryGroup, theme: Theme, props: Partial<PD.Values<P>> = {}) {
  61. currentProps = Object.assign({}, defaultProps, props, { structure: currentStructure })
  62. currentTheme = theme
  63. currentGroup = group
  64. const unit = group.units[0]
  65. currentConformationId = Unit.conformationId(unit)
  66. geometry = includesUnitKind(currentProps.unitKinds, unit)
  67. ? await createGeometry(ctx, unit, currentStructure, theme, currentProps, geometry)
  68. : createEmptyGeometry(geometry)
  69. // TODO create empty location iterator when not in unitKinds
  70. locationIt = createLocationIterator(group)
  71. renderObject = await createRenderObject(ctx, group, geometry, locationIt, theme, currentProps)
  72. }
  73. async function update(ctx: VisualContext, group: Unit.SymmetryGroup, theme: Theme, props: Partial<PD.Values<P>> = {}) {
  74. if (!renderObject) return
  75. const newProps = Object.assign({}, currentProps, props, { structure: currentStructure })
  76. const unit = group.units[0]
  77. locationIt.reset()
  78. VisualUpdateState.reset(updateState)
  79. setUpdateState(updateState, newProps, currentProps, theme, currentTheme)
  80. if (!ColorTheme.areEqual(theme.color, currentTheme.color)) {
  81. // console.log('new colorTheme')
  82. updateState.updateColor = true
  83. }
  84. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) {
  85. // console.log('new unitKinds')
  86. updateState.createGeometry = true
  87. }
  88. if (group.transformHash !== currentGroup.transformHash) {
  89. // console.log('new transformHash')
  90. if (group.units.length !== currentGroup.units.length || updateState.updateColor) {
  91. updateState.updateTransform = true
  92. } else {
  93. updateState.updateMatrix = true
  94. }
  95. }
  96. // check if the conformation of unit.model has changed
  97. const newConformationId = Unit.conformationId(unit)
  98. if (newConformationId !== currentConformationId) {
  99. // console.log('new conformation')
  100. currentConformationId = newConformationId
  101. updateState.createGeometry = true
  102. }
  103. //
  104. if (updateState.updateTransform) {
  105. // console.log('update transform')
  106. locationIt = createLocationIterator(group)
  107. const { instanceCount, groupCount } = locationIt
  108. createMarkers(instanceCount * groupCount, renderObject.values)
  109. updateState.updateColor = true
  110. updateState.updateMatrix = true
  111. }
  112. if (updateState.updateMatrix) {
  113. // console.log('update matrix')
  114. createUnitsTransform(group, renderObject.values)
  115. }
  116. if (updateState.createGeometry) {
  117. // console.log('update geometry')
  118. geometry = includesUnitKind(newProps.unitKinds, unit)
  119. ? await createGeometry(ctx, unit, currentStructure, theme, newProps, geometry)
  120. : createEmptyGeometry(geometry)
  121. ValueCell.update(renderObject.values.drawCount, Geometry.getDrawCount(geometry))
  122. updateBoundingSphere(renderObject.values, geometry)
  123. updateState.updateColor = true
  124. }
  125. if (updateState.updateSize) {
  126. // not all geometries have size data, so check here
  127. if ('uSize' in renderObject.values) {
  128. // console.log('update size')
  129. await createSizes(ctx.runtime, locationIt, theme.size, renderObject.values)
  130. }
  131. }
  132. if (updateState.updateColor) {
  133. // console.log('update color')
  134. await createColors(ctx.runtime, locationIt, theme.color, renderObject.values)
  135. }
  136. updateValues(renderObject.values, newProps)
  137. updateRenderableState(renderObject.state, newProps)
  138. currentProps = newProps
  139. currentTheme = theme
  140. currentGroup = group
  141. }
  142. return {
  143. get groupCount() { return locationIt ? locationIt.count : 0 },
  144. get renderObject () { return renderObject },
  145. async createOrUpdate(ctx: VisualContext, theme: Theme, props: Partial<PD.Values<P>> = {}, structureGroup?: StructureGroup) {
  146. if (structureGroup) currentStructure = structureGroup.structure
  147. const group = structureGroup ? structureGroup.group : undefined
  148. if (!group && !currentGroup) {
  149. throw new Error('missing group')
  150. } else if (group && (!currentGroup || !renderObject)) {
  151. // console.log('unit-visual first create')
  152. await create(ctx, group, theme, props)
  153. } else if (group && group.hashCode !== currentGroup.hashCode) {
  154. // console.log('unit-visual group.hashCode !== currentGroup.hashCode')
  155. await create(ctx, group, theme, props)
  156. } else {
  157. // console.log('unit-visual update')
  158. await update(ctx, group || currentGroup, theme, props)
  159. }
  160. },
  161. getLoci(pickingId: PickingId) {
  162. return renderObject ? getLoci(pickingId, { structure: currentStructure, group: currentGroup }, renderObject.id) : EmptyLoci
  163. },
  164. mark(loci: Loci, action: MarkerAction) {
  165. if (!renderObject) return false
  166. const { tMarker } = renderObject.values
  167. const { groupCount, instanceCount } = locationIt
  168. function apply(interval: Interval) {
  169. const start = Interval.start(interval)
  170. const end = Interval.end(interval)
  171. return applyMarkerAction(tMarker.ref.value.array, start, end, action)
  172. }
  173. let changed = false
  174. if (isEveryLoci(loci) || (Structure.isLoci(loci) && loci.structure === currentStructure)) {
  175. changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
  176. } else {
  177. changed = mark(loci, { structure: currentStructure, group: currentGroup }, apply)
  178. }
  179. if (changed) {
  180. ValueCell.update(tMarker, tMarker.ref.value)
  181. }
  182. return changed
  183. },
  184. setVisibility(value: boolean) {
  185. if (renderObject) renderObject.state.visible = value
  186. },
  187. setPickable(value: boolean) {
  188. if (renderObject) renderObject.state.pickable = value
  189. },
  190. destroy() {
  191. // TODO
  192. renderObject = undefined
  193. }
  194. }
  195. }
  196. // mesh
  197. export const UnitsMeshParams = {
  198. ...StructureMeshParams,
  199. ...UnitsParams,
  200. }
  201. export type UnitsMeshParams = typeof UnitsMeshParams
  202. export interface UnitsMeshVisualBuilder<P extends UnitsMeshParams> extends UnitsVisualBuilder<P, Mesh> { }
  203. export function UnitsMeshVisual<P extends UnitsMeshParams>(builder: UnitsMeshVisualBuilder<P>): UnitsVisual<P> {
  204. return UnitsVisual({
  205. ...builder,
  206. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme) => {
  207. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme)
  208. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true
  209. },
  210. createEmptyGeometry: Mesh.createEmpty,
  211. createRenderObject: createUnitsMeshRenderObject,
  212. updateValues: Mesh.updateValues,
  213. updateBoundingSphere: Mesh.updateBoundingSphere
  214. })
  215. }
  216. // points
  217. export const UnitsPointsParams = {
  218. ...StructurePointsParams,
  219. ...UnitsParams,
  220. }
  221. export type UnitsPointsParams = typeof UnitsPointsParams
  222. export interface UnitsPointVisualBuilder<P extends UnitsPointsParams> extends UnitsVisualBuilder<P, Points> { }
  223. export function UnitsPointsVisual<P extends UnitsPointsParams>(builder: UnitsPointVisualBuilder<P>): UnitsVisual<P> {
  224. return UnitsVisual({
  225. ...builder,
  226. createEmptyGeometry: Points.createEmpty,
  227. createRenderObject: createUnitsPointsRenderObject,
  228. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme) => {
  229. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme)
  230. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true
  231. },
  232. updateValues: Points.updateValues,
  233. updateBoundingSphere: Points.updateBoundingSphere
  234. })
  235. }
  236. // lines
  237. export const UnitsLinesParams = {
  238. ...StructureLinesParams,
  239. ...UnitsParams,
  240. }
  241. export type UnitsLinesParams = typeof UnitsLinesParams
  242. export interface UnitsLinesVisualBuilder<P extends UnitsLinesParams> extends UnitsVisualBuilder<P, Lines> { }
  243. export function UnitsLinesVisual<P extends UnitsLinesParams>(builder: UnitsLinesVisualBuilder<P>): UnitsVisual<P> {
  244. return UnitsVisual({
  245. ...builder,
  246. createEmptyGeometry: Lines.createEmpty,
  247. createRenderObject: createUnitsLinesRenderObject,
  248. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme) => {
  249. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme)
  250. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.updateSize = true
  251. },
  252. updateValues: Lines.updateValues,
  253. updateBoundingSphere: Lines.updateBoundingSphere
  254. })
  255. }
  256. // direct-volume
  257. export const UnitsDirectVolumeParams = {
  258. ...StructureDirectVolumeParams,
  259. ...UnitsParams,
  260. }
  261. export type UnitsDirectVolumeParams = typeof UnitsDirectVolumeParams
  262. export interface UnitsDirectVolumeVisualBuilder<P extends UnitsDirectVolumeParams> extends UnitsVisualBuilder<P, DirectVolume> { }
  263. export function UnitsDirectVolumeVisual<P extends UnitsDirectVolumeParams>(builder: UnitsDirectVolumeVisualBuilder<P>): UnitsVisual<P> {
  264. return UnitsVisual({
  265. ...builder,
  266. createEmptyGeometry: DirectVolume.createEmpty,
  267. createRenderObject: createUnitsDirectVolumeRenderObject,
  268. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<P>, currentProps: PD.Values<P>, newTheme: Theme, currentTheme: Theme) => {
  269. builder.setUpdateState(state, newProps, currentProps, newTheme, currentTheme)
  270. if (!SizeTheme.areEqual(newTheme.size, currentTheme.size)) state.createGeometry = true
  271. },
  272. updateValues: DirectVolume.updateValues,
  273. updateBoundingSphere: DirectVolume.updateBoundingSphere
  274. })
  275. }