cross-link-restraint-cylinder.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 { Link, Structure, StructureElement } from 'mol-model/structure';
  7. import { ComplexVisual, MeshUpdateState } from '..';
  8. import { RuntimeContext } from 'mol-task'
  9. import { LinkCylinderProps, DefaultLinkCylinderProps, createLinkCylinderMesh } from './util/link';
  10. import { Mesh } from '../../../shape/mesh';
  11. import { PickingId } from '../../../util/picking';
  12. import { Vec3 } from 'mol-math/linear-algebra';
  13. import { Loci, EmptyLoci } from 'mol-model/loci';
  14. import { ComplexMeshVisual, DefaultComplexMeshProps } from '../complex-visual';
  15. import { LocationIterator } from './util/location-iterator';
  16. import { Interval } from 'mol-data/int';
  17. import { SizeThemeProps, SizeTheme } from 'mol-view/theme/size';
  18. import { BitFlags } from 'mol-util';
  19. import { LinkType } from 'mol-model/structure/model/types';
  20. async function createCrossLinkRestraintCylinderMesh(ctx: RuntimeContext, structure: Structure, props: LinkCylinderProps, mesh?: Mesh) {
  21. const crossLinks = structure.crossLinkRestraints
  22. if (!crossLinks.count) return Mesh.createEmpty(mesh)
  23. const sizeTheme = SizeTheme(props.sizeTheme)
  24. const location = StructureElement.create()
  25. const builderProps = {
  26. linkCount: crossLinks.count,
  27. referencePosition: () => null,
  28. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  29. const b = crossLinks.pairs[edgeIndex]
  30. const uA = b.unitA, uB = b.unitB
  31. uA.conformation.position(uA.elements[b.indexA], posA)
  32. uB.conformation.position(uB.elements[b.indexB], posB)
  33. },
  34. order: () => 1,
  35. flags: () => BitFlags.create(LinkType.Flag.None),
  36. radius: (edgeIndex: number) => {
  37. const b = crossLinks.pairs[edgeIndex]
  38. location.unit = b.unitA
  39. location.element = b.unitA.elements[b.indexA]
  40. return sizeTheme.size(location)
  41. }
  42. }
  43. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  44. }
  45. export const DefaultCrossLinkRestraintProps = {
  46. ...DefaultComplexMeshProps,
  47. ...DefaultLinkCylinderProps,
  48. sizeTheme: { name: 'physical', factor: 0.3 } as SizeThemeProps,
  49. flipSided: false,
  50. flatShaded: false,
  51. }
  52. export type CrossLinkRestraintProps = typeof DefaultCrossLinkRestraintProps
  53. export function CrossLinkRestraintVisual(): ComplexVisual<CrossLinkRestraintProps> {
  54. return ComplexMeshVisual<CrossLinkRestraintProps>({
  55. defaultProps: DefaultCrossLinkRestraintProps,
  56. createMesh: createCrossLinkRestraintCylinderMesh,
  57. createLocationIterator: CrossLinkRestraintIterator,
  58. getLoci: getLinkLoci,
  59. mark: markLink,
  60. setUpdateState: (state: MeshUpdateState, newProps: CrossLinkRestraintProps, currentProps: CrossLinkRestraintProps) => {
  61. state.createMesh = newProps.radialSegments !== currentProps.radialSegments
  62. }
  63. })
  64. }
  65. function CrossLinkRestraintIterator(structure: Structure): LocationIterator {
  66. const { pairs } = structure.crossLinkRestraints
  67. const elementCount = pairs.length
  68. const instanceCount = 1
  69. const location = Link.Location()
  70. const getLocation = (elementIndex: number) => {
  71. const pair = pairs[elementIndex]
  72. location.aUnit = pair.unitA
  73. location.aIndex = pair.indexA
  74. location.bUnit = pair.unitB
  75. location.bIndex = pair.indexB
  76. return location
  77. }
  78. return LocationIterator(elementCount, instanceCount, getLocation)
  79. }
  80. function getLinkLoci(pickingId: PickingId, structure: Structure, id: number) {
  81. const { objectId, elementId } = pickingId
  82. if (id === objectId) {
  83. const pair = structure.crossLinkRestraints.pairs[elementId]
  84. if (pair) {
  85. return Link.Loci([ Link.Location(pair.unitA, pair.indexA, pair.unitB, pair.indexB) ])
  86. }
  87. }
  88. return EmptyLoci
  89. }
  90. function markLink(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean) {
  91. const crossLinks = structure.crossLinkRestraints
  92. let changed = false
  93. if (Link.isLoci(loci)) {
  94. for (const b of loci.links) {
  95. const indices = crossLinks.getPairIndices(b.aIndex, b.aUnit, b.bIndex, b.bUnit)
  96. if (indices) {
  97. for (let i = 0, il = indices.length; i < il; ++i) {
  98. if (apply(Interval.ofSingleton(indices[i]))) changed = true
  99. }
  100. }
  101. }
  102. }
  103. return changed
  104. }