interactions-inter-unit-cylinder.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /**
  2. * Copyright (c) 2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  7. import { VisualContext } from '../../visual';
  8. import { Structure } from '../../../mol-model/structure';
  9. import { Theme } from '../../../mol-theme/theme';
  10. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  11. import { Vec3 } from '../../../mol-math/linear-algebra';
  12. import { createBondCylinderMesh, BondCylinderParams } from './util/bond';
  13. import { ComplexMeshParams, ComplexVisual, ComplexMeshVisual } from '../complex-visual';
  14. import { VisualUpdateState } from '../../util';
  15. import { PickingId } from '../../../mol-geo/geometry/picking';
  16. import { EmptyLoci, Loci } from '../../../mol-model/loci';
  17. import { Interval } from '../../../mol-data/int';
  18. import { BondType } from '../../../mol-model/structure/model/types';
  19. import { Interactions } from '../../../mol-model-props/computed/interactions/interactions';
  20. import { InteractionsProvider } from '../../../mol-model-props/computed/interactions';
  21. import { LocationIterator } from '../../../mol-geo/util/location-iterator';
  22. function createInterUnitInteractionCylinderMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<InteractionsInterUnitParams>, mesh?: Mesh) {
  23. if (!structure.hasAtomic) return Mesh.createEmpty(mesh)
  24. const interactions = InteractionsProvider.getValue(structure).value!
  25. const { links, unitsFeatures } = interactions
  26. const { edgeCount, edges } = links
  27. const { sizeFactor } = props
  28. if (!edgeCount) return Mesh.createEmpty(mesh)
  29. const builderProps = {
  30. bondCount: edgeCount,
  31. referencePosition: () => null,
  32. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  33. const { unitA, indexA, unitB, indexB } = edges[edgeIndex]
  34. const fA = unitsFeatures.get(unitA.id)
  35. const fB = unitsFeatures.get(unitB.id)
  36. Vec3.set(posA, fA.x[indexA], fA.y[indexA], fA.z[indexA])
  37. Vec3.set(posB, fB.x[indexB], fB.y[indexB], fB.z[indexB])
  38. },
  39. order: (edgeIndex: number) => 1,
  40. flags: (edgeIndex: number) => BondType.Flag.MetallicCoordination, // TODO
  41. radius: (edgeIndex: number) => sizeFactor,
  42. ignore: () => false
  43. }
  44. return createBondCylinderMesh(ctx, builderProps, props, mesh)
  45. }
  46. export const InteractionsInterUnitParams = {
  47. ...ComplexMeshParams,
  48. ...BondCylinderParams,
  49. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  50. }
  51. export type InteractionsInterUnitParams = typeof InteractionsInterUnitParams
  52. export function InteractionsInterUnitVisual(materialId: number): ComplexVisual<InteractionsInterUnitParams> {
  53. return ComplexMeshVisual<InteractionsInterUnitParams>({
  54. defaultProps: PD.getDefaultValues(InteractionsInterUnitParams),
  55. createGeometry: createInterUnitInteractionCylinderMesh,
  56. createLocationIterator: createInteractionsIterator,
  57. getLoci: getInteractionLoci,
  58. eachLocation: eachInteraction,
  59. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<InteractionsInterUnitParams>, currentProps: PD.Values<InteractionsInterUnitParams>) => {
  60. state.createGeometry = (
  61. newProps.sizeFactor !== currentProps.sizeFactor ||
  62. newProps.radialSegments !== currentProps.radialSegments
  63. )
  64. }
  65. }, materialId)
  66. }
  67. function getInteractionLoci(pickingId: PickingId, structure: Structure, id: number) {
  68. const { objectId, groupId } = pickingId
  69. if (id === objectId) {
  70. const interactions = InteractionsProvider.getValue(structure).value!
  71. const l = interactions.links.edges[groupId]
  72. return Interactions.Loci(structure, interactions, [
  73. { unitA: l.unitA, indexA: l.indexA, unitB: l.unitB, indexB: l.indexB },
  74. { unitA: l.unitB, indexA: l.indexB, unitB: l.unitA, indexB: l.indexA },
  75. ])
  76. }
  77. return EmptyLoci
  78. }
  79. function eachInteraction(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean) {
  80. let changed = false
  81. if (Interactions.isLoci(loci)) {
  82. if (!Structure.areEquivalent(loci.structure, structure)) return false
  83. const interactions = InteractionsProvider.getValue(structure).value!
  84. if (loci.interactions !== interactions) return false
  85. const { links } = interactions
  86. for (const l of loci.links) {
  87. const idx = links.getEdgeIndex(l.indexA, l.unitA, l.indexB, l.unitB)
  88. if (idx !== -1) {
  89. if (apply(Interval.ofSingleton(idx))) changed = true
  90. }
  91. }
  92. }
  93. return changed
  94. }
  95. function createInteractionsIterator(structure: Structure): LocationIterator {
  96. const interactions = InteractionsProvider.getValue(structure).value!
  97. const { links } = interactions
  98. const groupCount = links.edgeCount
  99. const instanceCount = 1
  100. const location = Interactions.Location(interactions)
  101. const getLocation = (groupIndex: number) => {
  102. const link = links.edges[groupIndex]
  103. location.unitA = link.unitA
  104. location.indexA = link.indexA
  105. location.unitB = link.unitB
  106. location.indexB = link.indexB
  107. return location
  108. }
  109. return LocationIterator(groupCount, instanceCount, getLocation, true)
  110. }