carbohydrate-link-cylinder.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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/visual';
  21. import { Theme } from '../../../mol-theme/theme';
  22. function createCarbohydrateLinkCylinderMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<CarbohydrateLinkParams>, mesh?: Mesh) {
  23. const { links, elements } = structure.carbohydrates
  24. const { linkSizeFactor } = props
  25. const location = StructureElement.create()
  26. const builderProps = {
  27. linkCount: links.length,
  28. referencePosition: (edgeIndex: number) => null,
  29. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  30. const l = links[edgeIndex]
  31. Vec3.copy(posA, elements[l.carbohydrateIndexA].geometry.center)
  32. Vec3.copy(posB, elements[l.carbohydrateIndexB].geometry.center)
  33. },
  34. order: (edgeIndex: number) => 1,
  35. flags: (edgeIndex: number) => BitFlags.create(LinkType.Flag.None),
  36. radius: (edgeIndex: number) => {
  37. const l = links[edgeIndex]
  38. location.unit = elements[l.carbohydrateIndexA].unit
  39. location.element = elements[l.carbohydrateIndexA].anomericCarbon
  40. return theme.size.size(location) * linkSizeFactor
  41. }
  42. }
  43. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  44. }
  45. export const CarbohydrateLinkParams = {
  46. ...UnitsMeshParams,
  47. ...LinkCylinderParams,
  48. linkSizeFactor: PD.Numeric(0.3, { min: 0, max: 3, step: 0.01 }),
  49. }
  50. export type CarbohydrateLinkParams = typeof CarbohydrateLinkParams
  51. export function CarbohydrateLinkVisual(materialId: number): ComplexVisual<CarbohydrateLinkParams> {
  52. return ComplexMeshVisual<CarbohydrateLinkParams>({
  53. defaultProps: PD.getDefaultValues(CarbohydrateLinkParams),
  54. createGeometry: createCarbohydrateLinkCylinderMesh,
  55. createLocationIterator: CarbohydrateLinkIterator,
  56. getLoci: getLinkLoci,
  57. eachLocation: eachCarbohydrateLink,
  58. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<CarbohydrateLinkParams>, currentProps: PD.Values<CarbohydrateLinkParams>) => {
  59. state.createGeometry = (
  60. newProps.linkSizeFactor !== currentProps.linkSizeFactor ||
  61. newProps.radialSegments !== currentProps.radialSegments
  62. )
  63. }
  64. }, materialId)
  65. }
  66. function CarbohydrateLinkIterator(structure: Structure): LocationIterator {
  67. const { elements, links } = structure.carbohydrates
  68. const groupCount = links.length
  69. const instanceCount = 1
  70. const location = Link.Location()
  71. const getLocation = (groupIndex: number) => {
  72. const link = links[groupIndex]
  73. const carbA = elements[link.carbohydrateIndexA]
  74. const carbB = elements[link.carbohydrateIndexB]
  75. const indexA = OrderedSet.indexOf(carbA.unit.elements, carbA.anomericCarbon)
  76. const indexB = OrderedSet.indexOf(carbB.unit.elements, carbB.anomericCarbon)
  77. location.aUnit = carbA.unit
  78. location.aIndex = indexA as StructureElement.UnitIndex
  79. location.bUnit = carbB.unit
  80. location.bIndex = indexB as StructureElement.UnitIndex
  81. return location
  82. }
  83. return LocationIterator(groupCount, instanceCount, getLocation, true)
  84. }
  85. function getLinkLoci(pickingId: PickingId, structure: Structure, id: number) {
  86. const { objectId, groupId } = pickingId
  87. if (id === objectId) {
  88. const { links, elements } = structure.carbohydrates
  89. const l = links[groupId]
  90. const carbA = elements[l.carbohydrateIndexA]
  91. const carbB = elements[l.carbohydrateIndexB]
  92. const indexA = OrderedSet.indexOf(carbA.unit.elements, carbA.anomericCarbon)
  93. const indexB = OrderedSet.indexOf(carbB.unit.elements, carbB.anomericCarbon)
  94. if (indexA !== -1 && indexB !== -1) {
  95. return Link.Loci(structure, [
  96. Link.Location(
  97. carbA.unit, indexA as StructureElement.UnitIndex,
  98. carbB.unit, indexB as StructureElement.UnitIndex
  99. ),
  100. Link.Location(
  101. carbB.unit, indexB as StructureElement.UnitIndex,
  102. carbA.unit, indexA as StructureElement.UnitIndex
  103. )
  104. ])
  105. }
  106. }
  107. return EmptyLoci
  108. }
  109. function eachCarbohydrateLink(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean) {
  110. let changed = false
  111. if (Link.isLoci(loci)) {
  112. if (!Structure.areEquivalent(loci.structure, structure)) return false
  113. const { getLinkIndex } = structure.carbohydrates
  114. for (const l of loci.links) {
  115. const idx = getLinkIndex(l.aUnit, l.aUnit.elements[l.aIndex], l.bUnit, l.bUnit.elements[l.bIndex])
  116. if (idx !== undefined) {
  117. if (apply(Interval.ofSingleton(idx))) changed = true
  118. }
  119. }
  120. } else if (StructureElement.isLoci(loci)) {
  121. if (!Structure.areEquivalent(loci.structure, structure)) return false
  122. // TODO mark link only when both of the link elements are in a StructureElement.Loci
  123. const { getElementIndex, getLinkIndices, elements } = structure.carbohydrates
  124. for (const e of loci.elements) {
  125. OrderedSet.forEach(e.indices, v => {
  126. const carbI = getElementIndex(e.unit, e.unit.elements[v])
  127. if (carbI !== undefined) {
  128. const carb = elements[carbI]
  129. const indices = getLinkIndices(carb.unit, carb.anomericCarbon)
  130. for (let i = 0, il = indices.length; i < il; ++i) {
  131. if (apply(Interval.ofSingleton(indices[i]))) changed = true
  132. }
  133. }
  134. })
  135. }
  136. }
  137. return changed
  138. }