carbohydrate-terminal-link-cylinder.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, Link, StructureElement } from 'mol-model/structure';
  7. import { Loci, EmptyLoci } from 'mol-model/loci';
  8. import { Vec3 } from 'mol-math/linear-algebra';
  9. import { createLinkCylinderMesh, LinkCylinderParams } from './util/link';
  10. import { OrderedSet, Interval } from 'mol-data/int';
  11. import { ComplexMeshVisual, ComplexVisual } from '../complex-visual';
  12. import { LinkType } from 'mol-model/structure/model/types';
  13. import { BitFlags } from 'mol-util';
  14. import { UnitsMeshParams } from '../units-visual';
  15. import { ParamDefinition as PD } from 'mol-util/param-definition';
  16. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  17. import { LocationIterator } from 'mol-geo/util/location-iterator';
  18. import { PickingId } from 'mol-geo/geometry/picking';
  19. import { VisualUpdateState } from '../../util';
  20. import { VisualContext } from 'mol-repr/representation';
  21. import { Theme } from 'mol-theme/theme';
  22. async function createCarbohydrateTerminalLinkCylinderMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<CarbohydrateTerminalLinkParams>, mesh?: Mesh) {
  23. const { terminalLinks, elements } = structure.carbohydrates
  24. const { linkSizeFactor } = props
  25. const location = StructureElement.create()
  26. const builderProps = {
  27. linkCount: terminalLinks.length,
  28. referencePosition: (edgeIndex: number) => null,
  29. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  30. const l = terminalLinks[edgeIndex]
  31. if (l.fromCarbohydrate) {
  32. Vec3.copy(posA, elements[l.carbohydrateIndex].geometry.center)
  33. l.elementUnit.conformation.position(l.elementIndex, posB)
  34. } else {
  35. l.elementUnit.conformation.position(l.elementIndex, posA)
  36. Vec3.copy(posB, elements[l.carbohydrateIndex].geometry.center)
  37. }
  38. },
  39. order: (edgeIndex: number) => 1,
  40. flags: (edgeIndex: number) => BitFlags.create(LinkType.Flag.None),
  41. radius: (edgeIndex: number) => {
  42. const l = terminalLinks[edgeIndex]
  43. if (l.fromCarbohydrate) {
  44. location.unit = elements[l.carbohydrateIndex].unit
  45. location.element = elements[l.carbohydrateIndex].anomericCarbon
  46. } else {
  47. location.unit = l.elementUnit
  48. location.element = l.elementUnit.elements[l.elementIndex]
  49. }
  50. return theme.size.size(location) * linkSizeFactor
  51. }
  52. }
  53. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  54. }
  55. export const CarbohydrateTerminalLinkParams = {
  56. ...UnitsMeshParams,
  57. ...LinkCylinderParams,
  58. detail: PD.Numeric(0, { min: 0, max: 3, step: 1 }),
  59. linkSizeFactor: PD.Numeric(0.3, { min: 0, max: 3, step: 0.01 }),
  60. }
  61. export type CarbohydrateTerminalLinkParams = typeof CarbohydrateTerminalLinkParams
  62. export function CarbohydrateTerminalLinkVisual(): ComplexVisual<CarbohydrateTerminalLinkParams> {
  63. return ComplexMeshVisual<CarbohydrateTerminalLinkParams>({
  64. defaultProps: PD.getDefaultValues(CarbohydrateTerminalLinkParams),
  65. createGeometry: createCarbohydrateTerminalLinkCylinderMesh,
  66. createLocationIterator: CarbohydrateTerminalLinkIterator,
  67. getLoci: getTerminalLinkLoci,
  68. mark: markTerminalLink,
  69. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<CarbohydrateTerminalLinkParams>, currentProps: PD.Values<CarbohydrateTerminalLinkParams>) => {
  70. state.createGeometry = newProps.radialSegments !== currentProps.radialSegments
  71. }
  72. })
  73. }
  74. function CarbohydrateTerminalLinkIterator(structure: Structure): LocationIterator {
  75. const { elements, terminalLinks } = structure.carbohydrates
  76. const groupCount = terminalLinks.length
  77. const instanceCount = 1
  78. const location = Link.Location()
  79. const getLocation = (groupIndex: number) => {
  80. const terminalLink = terminalLinks[groupIndex]
  81. const carb = elements[terminalLink.carbohydrateIndex]
  82. const indexCarb = OrderedSet.indexOf(carb.unit.elements, carb.anomericCarbon)
  83. if (terminalLink.fromCarbohydrate) {
  84. location.aUnit = carb.unit
  85. location.aIndex = indexCarb as StructureElement.UnitIndex
  86. location.bUnit = terminalLink.elementUnit
  87. location.bIndex = terminalLink.elementIndex
  88. } else {
  89. location.aUnit = terminalLink.elementUnit
  90. location.aIndex = terminalLink.elementIndex
  91. location.bUnit = carb.unit
  92. location.bIndex = indexCarb as StructureElement.UnitIndex
  93. }
  94. return location
  95. }
  96. return LocationIterator(groupCount, instanceCount, getLocation, true)
  97. }
  98. function getTerminalLinkLoci(pickingId: PickingId, structure: Structure, id: number) {
  99. const { objectId, groupId } = pickingId
  100. if (id === objectId) {
  101. const { terminalLinks, elements } = structure.carbohydrates
  102. const l = terminalLinks[groupId]
  103. const carb = elements[l.carbohydrateIndex]
  104. const carbIndex = OrderedSet.indexOf(carb.unit.elements, carb.anomericCarbon)
  105. if (l.fromCarbohydrate) {
  106. return Link.Loci(structure, [
  107. Link.Location(
  108. carb.unit, carbIndex as StructureElement.UnitIndex,
  109. l.elementUnit, l.elementIndex
  110. )
  111. ])
  112. } else {
  113. return Link.Loci(structure, [
  114. Link.Location(
  115. l.elementUnit, l.elementIndex,
  116. carb.unit, carbIndex as StructureElement.UnitIndex
  117. )
  118. ])
  119. }
  120. }
  121. return EmptyLoci
  122. }
  123. function markTerminalLink(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean) {
  124. const { getTerminalLinkIndex } = structure.carbohydrates
  125. let changed = false
  126. if (Link.isLoci(loci)) {
  127. for (const l of loci.links) {
  128. const idx = getTerminalLinkIndex(l.aUnit, l.aUnit.elements[l.aIndex], l.bUnit, l.bUnit.elements[l.bIndex])
  129. if (idx !== undefined) {
  130. if (apply(Interval.ofSingleton(idx))) changed = true
  131. }
  132. }
  133. }
  134. return changed
  135. }