inter-unit-link-cylinder.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 { ValueCell } from 'mol-util/value-cell'
  7. import { createMeshRenderObject, MeshRenderObject } from 'mol-gl/render-object'
  8. import { Link, Structure } from 'mol-model/structure';
  9. import { DefaultStructureProps, StructureVisual } from '../index';
  10. import { RuntimeContext } from 'mol-task'
  11. import { LinkCylinderProps, DefaultLinkCylinderProps, createLinkCylinderMesh } from './util/link';
  12. import { MeshValues } from 'mol-gl/renderable';
  13. import { getMeshData } from '../../../util/mesh-data';
  14. import { Mesh } from '../../../shape/mesh';
  15. import { PickingId } from '../../../util/picking';
  16. import { Vec3 } from 'mol-math/linear-algebra';
  17. import { createUniformColor } from '../../../util/color-data';
  18. import { Loci, isEveryLoci, EmptyLoci } from 'mol-model/loci';
  19. import { MarkerAction, applyMarkerAction, createMarkers, MarkerData } from '../../../util/marker-data';
  20. import { SizeTheme } from '../../../theme';
  21. import { createIdentityTransform } from './util/common';
  22. import { updateMeshValues, updateRenderableState, createMeshValues, createRenderableState } from '../../util';
  23. // import { chainIdLinkColorData } from '../../../theme/structure/color/chain-id';
  24. async function createInterUnitLinkCylinderMesh(ctx: RuntimeContext, structure: Structure, props: LinkCylinderProps, mesh?: Mesh) {
  25. const links = structure.links
  26. const { bondCount, bonds } = links
  27. if (!bondCount) return Mesh.createEmpty(mesh)
  28. const builderProps = {
  29. linkCount: bondCount,
  30. referencePosition: (edgeIndex: number) => null, // TODO
  31. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  32. const b = bonds[edgeIndex]
  33. const uA = b.unitA, uB = b.unitB
  34. uA.conformation.position(uA.elements[b.indexA], posA)
  35. uB.conformation.position(uB.elements[b.indexB], posB)
  36. },
  37. order: (edgeIndex: number) => bonds[edgeIndex].order,
  38. flags: (edgeIndex: number) => bonds[edgeIndex].flag
  39. }
  40. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  41. }
  42. export const DefaultInterUnitLinkProps = {
  43. ...DefaultStructureProps,
  44. ...DefaultLinkCylinderProps,
  45. sizeTheme: { name: 'physical', factor: 0.3 } as SizeTheme,
  46. }
  47. export type InterUnitLinkProps = Partial<typeof DefaultInterUnitLinkProps>
  48. export function InterUnitLinkVisual(): StructureVisual<InterUnitLinkProps> {
  49. let renderObject: MeshRenderObject
  50. let currentProps: typeof DefaultInterUnitLinkProps
  51. let mesh: Mesh
  52. let currentStructure: Structure
  53. return {
  54. get renderObject () { return renderObject },
  55. async create(ctx: RuntimeContext, structure: Structure, props: InterUnitLinkProps = {}) {
  56. currentProps = Object.assign({}, DefaultInterUnitLinkProps, props)
  57. currentStructure = structure
  58. const elementCount = structure.links.bondCount
  59. const instanceCount = 1
  60. mesh = await createInterUnitLinkCylinderMesh(ctx, structure, currentProps)
  61. const transforms = createIdentityTransform()
  62. const color = createUniformColor({ value: 0x999911 }) // TODO
  63. const marker = createMarkers(instanceCount * elementCount)
  64. const counts = { drawCount: mesh.triangleCount * 3, elementCount, instanceCount }
  65. const values: MeshValues = {
  66. ...getMeshData(mesh),
  67. ...color,
  68. ...marker,
  69. aTransform: transforms,
  70. elements: mesh.indexBuffer,
  71. ...createMeshValues(currentProps, counts),
  72. }
  73. const state = createRenderableState(currentProps)
  74. renderObject = createMeshRenderObject(values, state)
  75. },
  76. async update(ctx: RuntimeContext, props: InterUnitLinkProps) {
  77. const newProps = Object.assign({}, currentProps, props)
  78. if (!renderObject) return false
  79. // TODO create in-place
  80. if (currentProps.radialSegments !== newProps.radialSegments) return false
  81. updateMeshValues(renderObject.values, newProps)
  82. updateRenderableState(renderObject.state, newProps)
  83. return false
  84. },
  85. getLoci(pickingId: PickingId) {
  86. return getLinkLoci(pickingId, currentStructure, renderObject.id)
  87. },
  88. mark(loci: Loci, action: MarkerAction) {
  89. markLink(loci, action, currentStructure, renderObject.values)
  90. },
  91. destroy() {
  92. // TODO
  93. }
  94. }
  95. }
  96. function getLinkLoci(pickingId: PickingId, structure: Structure, id: number) {
  97. const { objectId, elementId } = pickingId
  98. if (id === objectId) {
  99. const bond = structure.links.bonds[elementId]
  100. return Link.Loci([{
  101. aUnit: bond.unitA,
  102. aIndex: bond.indexA,
  103. bUnit: bond.unitB,
  104. bIndex: bond.indexB
  105. }])
  106. }
  107. return EmptyLoci
  108. }
  109. function markLink(loci: Loci, action: MarkerAction, structure: Structure, values: MarkerData) {
  110. const tMarker = values.tMarker
  111. const links = structure.links
  112. const elementCount = links.bondCount
  113. const instanceCount = 1
  114. let changed = false
  115. const array = tMarker.ref.value.array
  116. if (isEveryLoci(loci)) {
  117. applyMarkerAction(array, 0, elementCount * instanceCount, action)
  118. changed = true
  119. } else if (Link.isLoci(loci)) {
  120. for (const b of loci.links) {
  121. const _idx = structure.links.getBondIndex(b.aIndex, b.aUnit, b.bIndex, b.bUnit)
  122. if (_idx !== -1) {
  123. const idx = _idx
  124. if (applyMarkerAction(array, idx, idx + 1, action) && !changed) {
  125. changed = true
  126. }
  127. }
  128. }
  129. } else {
  130. return
  131. }
  132. if (changed) {
  133. ValueCell.update(tMarker, tMarker.ref.value)
  134. }
  135. }