carbohydrate-terminal-link-cylinder.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. linkSizeFactor: PD.Numeric(0.3, { min: 0, max: 3, step: 0.01 }),
  59. }
  60. export type CarbohydrateTerminalLinkParams = typeof CarbohydrateTerminalLinkParams
  61. export function CarbohydrateTerminalLinkVisual(): ComplexVisual<CarbohydrateTerminalLinkParams> {
  62. return ComplexMeshVisual<CarbohydrateTerminalLinkParams>({
  63. defaultProps: PD.getDefaultValues(CarbohydrateTerminalLinkParams),
  64. createGeometry: createCarbohydrateTerminalLinkCylinderMesh,
  65. createLocationIterator: CarbohydrateTerminalLinkIterator,
  66. getLoci: getTerminalLinkLoci,
  67. mark: markTerminalLink,
  68. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<CarbohydrateTerminalLinkParams>, currentProps: PD.Values<CarbohydrateTerminalLinkParams>) => {
  69. state.createGeometry = (
  70. newProps.linkSizeFactor !== currentProps.linkSizeFactor ||
  71. newProps.radialSegments !== currentProps.radialSegments
  72. )
  73. }
  74. })
  75. }
  76. function CarbohydrateTerminalLinkIterator(structure: Structure): LocationIterator {
  77. const { elements, terminalLinks } = structure.carbohydrates
  78. const groupCount = terminalLinks.length
  79. const instanceCount = 1
  80. const location = Link.Location()
  81. const getLocation = (groupIndex: number) => {
  82. const terminalLink = terminalLinks[groupIndex]
  83. const carb = elements[terminalLink.carbohydrateIndex]
  84. const indexCarb = OrderedSet.indexOf(carb.unit.elements, carb.anomericCarbon)
  85. if (terminalLink.fromCarbohydrate) {
  86. location.aUnit = carb.unit
  87. location.aIndex = indexCarb as StructureElement.UnitIndex
  88. location.bUnit = terminalLink.elementUnit
  89. location.bIndex = terminalLink.elementIndex
  90. } else {
  91. location.aUnit = terminalLink.elementUnit
  92. location.aIndex = terminalLink.elementIndex
  93. location.bUnit = carb.unit
  94. location.bIndex = indexCarb as StructureElement.UnitIndex
  95. }
  96. return location
  97. }
  98. return LocationIterator(groupCount, instanceCount, getLocation, true)
  99. }
  100. function getTerminalLinkLoci(pickingId: PickingId, structure: Structure, id: number) {
  101. const { objectId, groupId } = pickingId
  102. if (id === objectId) {
  103. const { terminalLinks, elements } = structure.carbohydrates
  104. const l = terminalLinks[groupId]
  105. const carb = elements[l.carbohydrateIndex]
  106. const carbIndex = OrderedSet.indexOf(carb.unit.elements, carb.anomericCarbon)
  107. if (l.fromCarbohydrate) {
  108. return Link.Loci(structure, [
  109. Link.Location(
  110. carb.unit, carbIndex as StructureElement.UnitIndex,
  111. l.elementUnit, l.elementIndex
  112. )
  113. ])
  114. } else {
  115. return Link.Loci(structure, [
  116. Link.Location(
  117. l.elementUnit, l.elementIndex,
  118. carb.unit, carbIndex as StructureElement.UnitIndex
  119. )
  120. ])
  121. }
  122. }
  123. return EmptyLoci
  124. }
  125. function markTerminalLink(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean) {
  126. const { getTerminalLinkIndex } = structure.carbohydrates
  127. let changed = false
  128. if (Link.isLoci(loci)) {
  129. for (const l of loci.links) {
  130. const idx = getTerminalLinkIndex(l.aUnit, l.aUnit.elements[l.aIndex], l.bUnit, l.bUnit.elements[l.bIndex])
  131. if (idx !== undefined) {
  132. if (apply(Interval.ofSingleton(idx))) changed = true
  133. }
  134. }
  135. }
  136. return changed
  137. }