units-visual.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 } from '../';
  8. import { DefaultStructureMeshProps, VisualUpdateState, DefaultStructurePointProps } from '.';
  9. import { RuntimeContext } from 'mol-task';
  10. import { PickingId } from '../../geometry/picking';
  11. import { LocationIterator } from '../../util/location-iterator';
  12. import { Mesh } from '../../geometry/mesh/mesh';
  13. import { MarkerAction, applyMarkerAction, createMarkers } from '../../geometry/marker-data';
  14. import { Loci, isEveryLoci, EmptyLoci } from 'mol-model/loci';
  15. import { MeshRenderObject, PointRenderObject } from 'mol-gl/render-object';
  16. import { createUnitsMeshRenderObject, createUnitsPointRenderObject, createUnitsTransform } from './visual/util/common';
  17. import { deepEqual, ValueCell, UUID } from 'mol-util';
  18. import { Interval } from 'mol-data/int';
  19. import { Point } from '../../geometry/point/point';
  20. import { updateRenderableState } from '../../geometry/geometry';
  21. import { createColors } from '../../geometry/color-data';
  22. export type StructureGroup = { structure: Structure, group: Unit.SymmetryGroup }
  23. export interface UnitsVisual<P extends RepresentationProps = {}> extends Visual<StructureGroup, P> { }
  24. export const DefaultUnitsMeshProps = {
  25. ...DefaultStructureMeshProps,
  26. unitKinds: [ Unit.Kind.Atomic, Unit.Kind.Spheres ] as Unit.Kind[]
  27. }
  28. export type UnitsMeshProps = typeof DefaultUnitsMeshProps
  29. export interface UnitsMeshVisualBuilder<P extends UnitsMeshProps> {
  30. defaultProps: P
  31. createMesh(ctx: RuntimeContext, unit: Unit, structure: Structure, props: P, mesh?: Mesh): Promise<Mesh>
  32. createLocationIterator(group: Unit.SymmetryGroup): LocationIterator
  33. getLoci(pickingId: PickingId, group: Unit.SymmetryGroup, id: number): Loci
  34. mark(loci: Loci, group: Unit.SymmetryGroup, apply: (interval: Interval) => boolean): boolean
  35. setUpdateState(state: VisualUpdateState, newProps: P, currentProps: P): void
  36. }
  37. export function UnitsMeshVisual<P extends UnitsMeshProps>(builder: UnitsMeshVisualBuilder<P>): UnitsVisual<P> {
  38. const { defaultProps, createMesh, createLocationIterator, getLoci, mark, setUpdateState } = builder
  39. const updateState = VisualUpdateState.create()
  40. let renderObject: MeshRenderObject | undefined
  41. let currentProps: P
  42. let mesh: Mesh
  43. let currentGroup: Unit.SymmetryGroup
  44. let currentStructure: Structure
  45. let locationIt: LocationIterator
  46. let currentConformationId: UUID
  47. async function create(ctx: RuntimeContext, group: Unit.SymmetryGroup, props: Partial<P> = {}) {
  48. currentProps = Object.assign({}, defaultProps, props)
  49. currentProps.colorTheme.structure = currentStructure
  50. currentGroup = group
  51. const unit = group.units[0]
  52. currentConformationId = Unit.conformationId(unit)
  53. mesh = currentProps.unitKinds.includes(unit.kind)
  54. ? await createMesh(ctx, unit, currentStructure, currentProps, mesh)
  55. : Mesh.createEmpty(mesh)
  56. // TODO create empty location iterator when not in unitKinds
  57. locationIt = createLocationIterator(group)
  58. renderObject = await createUnitsMeshRenderObject(ctx, group, mesh, locationIt, currentProps)
  59. }
  60. async function update(ctx: RuntimeContext, props: Partial<P> = {}) {
  61. if (!renderObject) return
  62. const newProps = Object.assign({}, currentProps, props)
  63. newProps.colorTheme.structure = currentStructure
  64. const unit = currentGroup.units[0]
  65. locationIt.reset()
  66. VisualUpdateState.reset(updateState)
  67. setUpdateState(updateState, newProps, currentProps)
  68. const newConformationId = Unit.conformationId(unit)
  69. if (newConformationId !== currentConformationId) {
  70. currentConformationId = newConformationId
  71. updateState.createGeometry = true
  72. }
  73. if (currentGroup.units.length !== locationIt.instanceCount) updateState.updateTransform = true
  74. if (!deepEqual(newProps.sizeTheme, currentProps.sizeTheme)) updateState.createGeometry = true
  75. if (!deepEqual(newProps.colorTheme, currentProps.colorTheme)) updateState.updateColor = true
  76. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createGeometry = true
  77. //
  78. if (updateState.updateTransform) {
  79. locationIt = createLocationIterator(currentGroup)
  80. const { instanceCount, groupCount } = locationIt
  81. createUnitsTransform(currentGroup, renderObject.values)
  82. createMarkers(instanceCount * groupCount, renderObject.values)
  83. updateState.updateColor = true
  84. }
  85. if (updateState.createGeometry) {
  86. mesh = newProps.unitKinds.includes(unit.kind)
  87. ? await createMesh(ctx, unit, currentStructure, newProps, mesh)
  88. : Mesh.createEmpty(mesh)
  89. ValueCell.update(renderObject.values.drawCount, mesh.triangleCount * 3)
  90. updateState.updateColor = true
  91. }
  92. if (updateState.updateColor) {
  93. await createColors(ctx, locationIt, newProps.colorTheme, renderObject.values)
  94. }
  95. // TODO why do I need to cast here?
  96. Mesh.updateValues(renderObject.values, newProps as UnitsMeshProps)
  97. updateRenderableState(renderObject.state, newProps as UnitsMeshProps)
  98. currentProps = newProps
  99. }
  100. return {
  101. get renderObject () { return renderObject },
  102. async createOrUpdate(ctx: RuntimeContext, props: Partial<P> = {}, structureGroup?: StructureGroup) {
  103. if (structureGroup) currentStructure = structureGroup.structure
  104. const group = structureGroup ? structureGroup.group : undefined
  105. if (!group && !currentGroup) {
  106. throw new Error('missing group')
  107. } else if (group && (!currentGroup || !renderObject)) {
  108. // console.log('unit-visual first create')
  109. await create(ctx, group, props)
  110. } else if (group && group.hashCode !== currentGroup.hashCode) {
  111. // console.log('unit-visual group.hashCode !== currentGroup.hashCode')
  112. await create(ctx, group, props)
  113. } else {
  114. // console.log('unit-visual update')
  115. if (group && !sameGroupConformation(group, currentGroup)) {
  116. // console.log('unit-visual new conformation')
  117. currentGroup = group
  118. }
  119. await update(ctx, props)
  120. }
  121. },
  122. getLoci(pickingId: PickingId) {
  123. return renderObject ? getLoci(pickingId, currentGroup, renderObject.id) : EmptyLoci
  124. },
  125. mark(loci: Loci, action: MarkerAction) {
  126. if (!renderObject) return false
  127. const { tMarker } = renderObject.values
  128. const { groupCount, instanceCount } = locationIt
  129. function apply(interval: Interval) {
  130. const start = Interval.start(interval)
  131. const end = Interval.end(interval)
  132. return applyMarkerAction(tMarker.ref.value.array, start, end, action)
  133. }
  134. let changed = false
  135. if (isEveryLoci(loci)) {
  136. changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
  137. } else {
  138. changed = mark(loci, currentGroup, apply)
  139. }
  140. if (changed) {
  141. ValueCell.update(tMarker, tMarker.ref.value)
  142. }
  143. return changed
  144. },
  145. destroy() {
  146. // TODO
  147. renderObject = undefined
  148. }
  149. }
  150. }
  151. function sameGroupConformation(groupA: Unit.SymmetryGroup, groupB: Unit.SymmetryGroup) {
  152. return (
  153. groupA.units.length === groupB.units.length &&
  154. Unit.conformationId(groupA.units[0]) === Unit.conformationId(groupB.units[0])
  155. )
  156. }
  157. //
  158. export const DefaultUnitsPointProps = {
  159. ...DefaultStructurePointProps,
  160. unitKinds: [ Unit.Kind.Atomic, Unit.Kind.Spheres ] as Unit.Kind[]
  161. }
  162. export type UnitsPointProps = typeof DefaultUnitsPointProps
  163. export interface UnitsPointVisualBuilder<P extends UnitsPointProps> {
  164. defaultProps: P
  165. createPoint(ctx: RuntimeContext, unit: Unit, structure: Structure, props: P, point?: Point): Promise<Point>
  166. createLocationIterator(group: Unit.SymmetryGroup): LocationIterator
  167. getLoci(pickingId: PickingId, group: Unit.SymmetryGroup, id: number): Loci
  168. mark(loci: Loci, group: Unit.SymmetryGroup, apply: (interval: Interval) => boolean): boolean
  169. setUpdateState(state: VisualUpdateState, newProps: P, currentProps: P): void
  170. }
  171. export function UnitsPointVisual<P extends UnitsPointProps>(builder: UnitsPointVisualBuilder<P>): UnitsVisual<P> {
  172. const { defaultProps, createPoint, createLocationIterator, getLoci, mark, setUpdateState } = builder
  173. const updateState = VisualUpdateState.create()
  174. let renderObject: PointRenderObject | undefined
  175. let currentProps: P
  176. let point: Point
  177. let currentGroup: Unit.SymmetryGroup
  178. let currentStructure: Structure
  179. let locationIt: LocationIterator
  180. let currentConformationId: UUID
  181. async function create(ctx: RuntimeContext, group: Unit.SymmetryGroup, props: Partial<P> = {}) {
  182. currentProps = Object.assign({}, defaultProps, props)
  183. currentProps.colorTheme.structure = currentStructure
  184. currentGroup = group
  185. const unit = group.units[0]
  186. currentConformationId = Unit.conformationId(unit)
  187. point = currentProps.unitKinds.includes(unit.kind)
  188. ? await createPoint(ctx, unit, currentStructure, currentProps, point)
  189. : Point.createEmpty(point)
  190. // TODO create empty location iterator when not in unitKinds
  191. locationIt = createLocationIterator(group)
  192. renderObject = await createUnitsPointRenderObject(ctx, group, point, locationIt, currentProps)
  193. }
  194. async function update(ctx: RuntimeContext, props: Partial<P> = {}) {
  195. if (!renderObject) return
  196. const newProps = Object.assign({}, currentProps, props)
  197. newProps.colorTheme.structure = currentStructure
  198. const unit = currentGroup.units[0]
  199. locationIt.reset()
  200. VisualUpdateState.reset(updateState)
  201. setUpdateState(updateState, newProps, currentProps)
  202. const newConformationId = Unit.conformationId(unit)
  203. if (newConformationId !== currentConformationId) {
  204. currentConformationId = newConformationId
  205. updateState.createGeometry = true
  206. }
  207. if (currentGroup.units.length !== locationIt.instanceCount) updateState.updateTransform = true
  208. if (!deepEqual(newProps.sizeTheme, currentProps.sizeTheme)) updateState.createGeometry = true
  209. if (!deepEqual(newProps.colorTheme, currentProps.colorTheme)) updateState.updateColor = true
  210. if (!deepEqual(newProps.unitKinds, currentProps.unitKinds)) updateState.createGeometry = true
  211. //
  212. if (updateState.updateTransform) {
  213. locationIt = createLocationIterator(currentGroup)
  214. const { instanceCount, groupCount } = locationIt
  215. createUnitsTransform(currentGroup, renderObject.values)
  216. createMarkers(instanceCount * groupCount, renderObject.values)
  217. updateState.updateColor = true
  218. }
  219. if (updateState.createGeometry) {
  220. point = newProps.unitKinds.includes(unit.kind)
  221. ? await createPoint(ctx, unit, currentStructure, newProps, point)
  222. : Point.createEmpty(point)
  223. ValueCell.update(renderObject.values.drawCount, point.vertexCount)
  224. updateState.updateColor = true
  225. }
  226. if (updateState.updateColor) {
  227. await createColors(ctx, locationIt, newProps.colorTheme, renderObject.values)
  228. }
  229. // TODO why do I need to cast here?
  230. Point.updateValues(renderObject.values, newProps as UnitsPointProps)
  231. updateRenderableState(renderObject.state, newProps as UnitsPointProps)
  232. currentProps = newProps
  233. }
  234. return {
  235. get renderObject () { return renderObject },
  236. async createOrUpdate(ctx: RuntimeContext, props: Partial<P> = {}, structureGroup?: StructureGroup) {
  237. if (structureGroup) currentStructure = structureGroup.structure
  238. const group = structureGroup ? structureGroup.group : undefined
  239. if (!group && !currentGroup) {
  240. throw new Error('missing group')
  241. } else if (group && (!currentGroup || !renderObject)) {
  242. // console.log('unit-visual first create')
  243. await create(ctx, group, props)
  244. } else if (group && group.hashCode !== currentGroup.hashCode) {
  245. // console.log('unit-visual group.hashCode !== currentGroup.hashCode')
  246. await create(ctx, group, props)
  247. } else {
  248. // console.log('unit-visual update')
  249. if (group && !sameGroupConformation(group, currentGroup)) {
  250. // console.log('unit-visual new conformation')
  251. currentGroup = group
  252. }
  253. await update(ctx, props)
  254. }
  255. },
  256. getLoci(pickingId: PickingId) {
  257. return renderObject ? getLoci(pickingId, currentGroup, renderObject.id) : EmptyLoci
  258. },
  259. mark(loci: Loci, action: MarkerAction) {
  260. if (!renderObject) return false
  261. const { tMarker } = renderObject.values
  262. const { groupCount, instanceCount } = locationIt
  263. function apply(interval: Interval) {
  264. const start = Interval.start(interval)
  265. const end = Interval.end(interval)
  266. return applyMarkerAction(tMarker.ref.value.array, start, end, action)
  267. }
  268. let changed = false
  269. if (isEveryLoci(loci)) {
  270. changed = apply(Interval.ofBounds(0, groupCount * instanceCount))
  271. } else {
  272. changed = mark(loci, currentGroup, apply)
  273. }
  274. if (changed) {
  275. ValueCell.update(tMarker, tMarker.ref.value)
  276. }
  277. return changed
  278. },
  279. destroy() {
  280. // TODO
  281. renderObject = undefined
  282. }
  283. }
  284. }