cross-link-restraint-cylinder.ts 4.0 KB

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