carbohydrate-link-cylinder.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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, LinkCylinderProps, LinkCylinderParams } from './util/link';
  10. import { OrderedSet, Interval } from 'mol-data/int';
  11. import { ComplexMeshVisual, ComplexVisual } from '../complex-visual';
  12. import { SizeThemeName, SizeThemeOptions } from 'mol-theme/size';
  13. import { LinkType } from 'mol-model/structure/model/types';
  14. import { BitFlags } from 'mol-util';
  15. import { UnitsMeshParams } from '../units-visual';
  16. import { ParamDefinition as PD } from 'mol-util/param-definition';
  17. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  18. import { LocationIterator } from 'mol-geo/util/location-iterator';
  19. import { PickingId } from 'mol-geo/geometry/picking';
  20. import { VisualUpdateState } from '../../util';
  21. import { VisualContext } from 'mol-repr';
  22. import { Theme } from 'mol-geo/geometry/geometry';
  23. // TODO create seperate visual
  24. // for (let i = 0, il = carbohydrates.terminalLinks.length; i < il; ++i) {
  25. // const tl = carbohydrates.terminalLinks[i]
  26. // const center = carbohydrates.elements[tl.carbohydrateIndex].geometry.center
  27. // tl.elementUnit.conformation.position(tl.elementUnit.elements[tl.elementIndex], p)
  28. // if (tl.fromCarbohydrate) {
  29. // builder.addCylinder(center, p, 0.5, linkParams)
  30. // } else {
  31. // builder.addCylinder(p, center, 0.5, linkParams)
  32. // }
  33. // }
  34. const radiusFactor = 0.3
  35. async function createCarbohydrateLinkCylinderMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: LinkCylinderProps, mesh?: Mesh) {
  36. const { links, elements } = structure.carbohydrates
  37. const location = StructureElement.create()
  38. const builderProps = {
  39. linkCount: links.length,
  40. referencePosition: (edgeIndex: number) => null,
  41. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  42. const l = links[edgeIndex]
  43. Vec3.copy(posA, elements[l.carbohydrateIndexA].geometry.center)
  44. Vec3.copy(posB, elements[l.carbohydrateIndexB].geometry.center)
  45. },
  46. order: (edgeIndex: number) => 1,
  47. flags: (edgeIndex: number) => BitFlags.create(LinkType.Flag.None),
  48. radius: (edgeIndex: number) => {
  49. const l = links[edgeIndex]
  50. location.unit = elements[l.carbohydrateIndexA].unit
  51. location.element = elements[l.carbohydrateIndexA].anomericCarbon
  52. return theme.size.size(location) * radiusFactor
  53. }
  54. }
  55. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  56. }
  57. export const CarbohydrateLinkParams = {
  58. ...UnitsMeshParams,
  59. ...LinkCylinderParams,
  60. sizeTheme: PD.Select<SizeThemeName>('Size Theme', '', 'physical', SizeThemeOptions),
  61. sizeValue: PD.Numeric('Size Value', '', 1, 0, 20, 0.1),
  62. detail: PD.Numeric('Sphere Detail', '', 0, 0, 3, 1),
  63. }
  64. export const DefaultCarbohydrateLinkProps = PD.getDefaultValues(CarbohydrateLinkParams)
  65. export type CarbohydrateLinkProps = typeof DefaultCarbohydrateLinkProps
  66. export function CarbohydrateLinkVisual(): ComplexVisual<CarbohydrateLinkProps> {
  67. return ComplexMeshVisual<CarbohydrateLinkProps>({
  68. defaultProps: DefaultCarbohydrateLinkProps,
  69. createGeometry: createCarbohydrateLinkCylinderMesh,
  70. createLocationIterator: CarbohydrateLinkIterator,
  71. getLoci: getLinkLoci,
  72. mark: markLink,
  73. setUpdateState: (state: VisualUpdateState, newProps: CarbohydrateLinkProps, currentProps: CarbohydrateLinkProps) => {
  74. state.createGeometry = newProps.radialSegments !== currentProps.radialSegments
  75. }
  76. })
  77. }
  78. function CarbohydrateLinkIterator(structure: Structure): LocationIterator {
  79. const { elements, links } = structure.carbohydrates
  80. const groupCount = links.length
  81. const instanceCount = 1
  82. const location = Link.Location()
  83. const getLocation = (groupIndex: number) => {
  84. const link = links[groupIndex]
  85. const carbA = elements[link.carbohydrateIndexA]
  86. const carbB = elements[link.carbohydrateIndexB]
  87. const indexA = OrderedSet.indexOf(carbA.unit.elements, carbA.anomericCarbon)
  88. const indexB = OrderedSet.indexOf(carbB.unit.elements, carbB.anomericCarbon)
  89. location.aUnit = carbA.unit
  90. location.aIndex = indexA as StructureElement.UnitIndex
  91. location.bUnit = carbB.unit
  92. location.bIndex = indexB as StructureElement.UnitIndex
  93. return location
  94. }
  95. return LocationIterator(groupCount, instanceCount, getLocation, true)
  96. }
  97. function getLinkLoci(pickingId: PickingId, structure: Structure, id: number) {
  98. const { objectId, groupId } = pickingId
  99. if (id === objectId) {
  100. const { links, elements } = structure.carbohydrates
  101. const l = links[groupId]
  102. const carbA = elements[l.carbohydrateIndexA]
  103. const carbB = elements[l.carbohydrateIndexB]
  104. const indexA = OrderedSet.indexOf(carbA.unit.elements, carbA.anomericCarbon)
  105. const indexB = OrderedSet.indexOf(carbB.unit.elements, carbB.anomericCarbon)
  106. if (indexA !== -1 && indexB !== -1) {
  107. return Link.Loci([
  108. Link.Location(
  109. carbA.unit, indexA as StructureElement.UnitIndex,
  110. carbB.unit, indexB as StructureElement.UnitIndex
  111. )
  112. ])
  113. }
  114. }
  115. return EmptyLoci
  116. }
  117. function markLink(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean) {
  118. const { getLinkIndex } = structure.carbohydrates
  119. let changed = false
  120. if (Link.isLoci(loci)) {
  121. for (const l of loci.links) {
  122. const idx = getLinkIndex(l.aUnit, l.aUnit.elements[l.aIndex], l.bUnit, l.bUnit.elements[l.bIndex])
  123. if (idx !== undefined) {
  124. if (apply(Interval.ofSingleton(idx))) changed = true
  125. }
  126. }
  127. }
  128. return changed
  129. }