intra-unit-link-cylinder.ts 4.2 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. * @author David Sehnal <david.sehnal@gmail.com>
  6. */
  7. import { Unit, Link, StructureElement } from 'mol-model/structure';
  8. import { UnitsVisual } from '..';
  9. import { RuntimeContext } from 'mol-task'
  10. import { DefaultLinkCylinderProps, LinkCylinderProps, createLinkCylinderMesh } from './util/link';
  11. import { Mesh } from '../../../shape/mesh';
  12. import { PickingId } from '../../../util/picking';
  13. import { Vec3 } from 'mol-math/linear-algebra';
  14. import { Loci, EmptyLoci } from 'mol-model/loci';
  15. import { LinkIterator } from './util/location-iterator';
  16. import { UnitsMeshVisual, DefaultUnitsMeshProps } from '../units-visual';
  17. import { Interval } from 'mol-data/int';
  18. import { SizeThemeProps, SizeTheme } from 'mol-view/theme/size';
  19. import { BitFlags } from 'mol-util';
  20. async function createIntraUnitLinkCylinderMesh(ctx: RuntimeContext, unit: Unit, props: LinkCylinderProps, mesh?: Mesh) {
  21. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh)
  22. const sizeTheme = SizeTheme(props.sizeTheme)
  23. const location = StructureElement.create(unit)
  24. const elements = unit.elements;
  25. const links = unit.links
  26. const { edgeCount, a, b, edgeProps, offset } = links
  27. const { order: _order, flags: _flags } = edgeProps
  28. if (!edgeCount) return Mesh.createEmpty(mesh)
  29. const vRef = Vec3.zero()
  30. const pos = unit.conformation.invariantPosition
  31. const builderProps = {
  32. linkCount: edgeCount * 2,
  33. referencePosition: (edgeIndex: number) => {
  34. let aI = a[edgeIndex], bI = b[edgeIndex];
  35. if (aI > bI) [aI, bI] = [bI, aI]
  36. for (let i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
  37. if (b[i] !== bI) return pos(elements[b[i]], vRef)
  38. }
  39. for (let i = offset[bI], il = offset[bI + 1]; i < il; ++i) {
  40. if (a[i] !== aI) return pos(elements[a[i]], vRef)
  41. }
  42. return null
  43. },
  44. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  45. pos(elements[a[edgeIndex]], posA)
  46. pos(elements[b[edgeIndex]], posB)
  47. },
  48. order: (edgeIndex: number) => _order[edgeIndex],
  49. flags: (edgeIndex: number) => BitFlags.create(_flags[edgeIndex]),
  50. radius: (edgeIndex: number) => {
  51. location.element = elements[a[edgeIndex]]
  52. return sizeTheme.size(location)
  53. }
  54. }
  55. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  56. }
  57. export const DefaultIntraUnitLinkProps = {
  58. ...DefaultUnitsMeshProps,
  59. ...DefaultLinkCylinderProps,
  60. sizeTheme: { name: 'physical', factor: 0.3 } as SizeThemeProps,
  61. }
  62. export type IntraUnitLinkProps = typeof DefaultIntraUnitLinkProps
  63. export function IntraUnitLinkVisual(): UnitsVisual<IntraUnitLinkProps> {
  64. return UnitsMeshVisual<IntraUnitLinkProps>({
  65. defaultProps: DefaultIntraUnitLinkProps,
  66. createMesh: createIntraUnitLinkCylinderMesh,
  67. createLocationIterator: LinkIterator.fromGroup,
  68. getLoci: getLinkLoci,
  69. mark: markLink,
  70. setUpdateState: () => {}
  71. })
  72. }
  73. function getLinkLoci(pickingId: PickingId, group: Unit.SymmetryGroup, id: number) {
  74. const { objectId, instanceId, elementId } = pickingId
  75. const unit = group.units[instanceId]
  76. if (id === objectId && Unit.isAtomic(unit)) {
  77. return Link.Loci([
  78. Link.Location(
  79. unit, unit.links.a[elementId] as StructureElement.UnitIndex,
  80. unit, unit.links.b[elementId] as StructureElement.UnitIndex
  81. )
  82. ])
  83. }
  84. return EmptyLoci
  85. }
  86. function markLink(loci: Loci, group: Unit.SymmetryGroup, apply: (interval: Interval) => boolean) {
  87. const unit = group.units[0]
  88. let changed = false
  89. if (Unit.isAtomic(unit) && Link.isLoci(loci)) {
  90. for (const b of loci.links) {
  91. const unitIdx = Unit.findUnitById(b.aUnit.id, group.units)
  92. if (unitIdx !== -1) {
  93. const idx = unit.links.getDirectedEdgeIndex(b.aIndex, b.bIndex)
  94. if (idx !== -1) {
  95. if (apply(Interval.ofSingleton(idx))) changed = true
  96. }
  97. }
  98. }
  99. }
  100. return changed
  101. }