cross-link-restraint-cylinder.ts 4.7 KB

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