units-visual.ts 17 KB

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