cross-link-restraint-cylinder.ts 4.5 KB

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