inter-unit-link-cylinder.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 { createLinkCylinderMesh, LinkIterator, 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 { ParamDefinition as PD } from 'mol-util/param-definition';
  16. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  17. import { PickingId } from 'mol-geo/geometry/picking';
  18. import { VisualContext } from 'mol-repr/representation';
  19. import { Theme } from 'mol-theme/theme';
  20. async function createInterUnitLinkCylinderMesh(ctx: VisualContext, structure: Structure, theme: Theme, props: PD.Values<InterUnitLinkParams>, mesh?: Mesh) {
  21. const links = structure.links
  22. const { bondCount, bonds } = links
  23. const { sizeFactor } = props
  24. if (!bondCount) return Mesh.createEmpty(mesh)
  25. const location = StructureElement.create()
  26. const builderProps = {
  27. linkCount: bondCount,
  28. referencePosition: (edgeIndex: number) => null, // TODO
  29. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  30. const b = bonds[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: (edgeIndex: number) => bonds[edgeIndex].order,
  36. flags: (edgeIndex: number) => BitFlags.create(bonds[edgeIndex].flag),
  37. radius: (edgeIndex: number) => {
  38. const b = bonds[edgeIndex]
  39. location.unit = b.unitA
  40. location.element = b.unitA.elements[b.indexA]
  41. return theme.size.size(location) * sizeFactor
  42. }
  43. }
  44. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  45. }
  46. export const InterUnitLinkParams = {
  47. ...ComplexMeshParams,
  48. ...LinkCylinderParams,
  49. sizeFactor: PD.Numeric(0.2, { min: 0, max: 10, step: 0.01 }),
  50. }
  51. export type InterUnitLinkParams = typeof InterUnitLinkParams
  52. export function InterUnitLinkVisual(): ComplexVisual<InterUnitLinkParams> {
  53. return ComplexMeshVisual<InterUnitLinkParams>({
  54. defaultProps: PD.getDefaultValues(InterUnitLinkParams),
  55. createGeometry: createInterUnitLinkCylinderMesh,
  56. createLocationIterator: LinkIterator.fromStructure,
  57. getLoci: getLinkLoci,
  58. mark: markLink,
  59. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<InterUnitLinkParams>, currentProps: PD.Values<InterUnitLinkParams>) => {
  60. if (newProps.linkScale !== currentProps.linkScale) state.createGeometry = true
  61. if (newProps.linkSpacing !== currentProps.linkSpacing) state.createGeometry = true
  62. if (newProps.radialSegments !== currentProps.radialSegments) state.createGeometry = true
  63. }
  64. })
  65. }
  66. function getLinkLoci(pickingId: PickingId, structure: Structure, id: number) {
  67. const { objectId, groupId } = pickingId
  68. if (id === objectId) {
  69. const bond = structure.links.bonds[groupId]
  70. return Link.Loci(structure, [
  71. Link.Location(
  72. bond.unitA, bond.indexA as StructureElement.UnitIndex,
  73. bond.unitB, bond.indexB as StructureElement.UnitIndex
  74. )
  75. ])
  76. }
  77. return EmptyLoci
  78. }
  79. function markLink(loci: Loci, structure: Structure, apply: (interval: Interval) => boolean) {
  80. let changed = false
  81. if (!Link.isLoci(loci)) return false
  82. for (const b of loci.links) {
  83. const idx = structure.links.getBondIndex(b.aIndex, b.aUnit, b.bIndex, b.bUnit)
  84. if (idx !== -1) {
  85. if (apply(Interval.ofSingleton(idx))) changed = true
  86. }
  87. }
  88. return changed
  89. }