intra-unit-link-cylinder.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, Structure } from 'mol-model/structure';
  8. import { UnitsVisual, VisualUpdateState } from '..';
  9. import { RuntimeContext } from 'mol-task'
  10. import { LinkCylinderProps, createLinkCylinderMesh, LinkIterator, LinkCylinderParams } from './util/link';
  11. import { Mesh } from '../../../geometry/mesh/mesh';
  12. import { PickingId } from '../../../geometry/picking';
  13. import { Vec3 } from 'mol-math/linear-algebra';
  14. import { Loci, EmptyLoci } from 'mol-model/loci';
  15. import { UnitsMeshVisual, UnitsMeshParams } from '../units-visual';
  16. import { Interval } from 'mol-data/int';
  17. import { SizeTheme, SizeThemeName, SizeThemeOptions } from 'mol-canvas3d/theme/size';
  18. import { BitFlags } from 'mol-util';
  19. import { SelectParam, NumberParam, paramDefaultValues } from 'mol-util/parameter';
  20. async function createIntraUnitLinkCylinderMesh(ctx: RuntimeContext, unit: Unit, structure: Structure, props: LinkCylinderProps, mesh?: Mesh) {
  21. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh)
  22. const sizeTheme = SizeTheme({ name: props.sizeTheme, value: props.sizeValue, factor: props.sizeFactor })
  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 IntraUnitLinkParams = {
  58. ...UnitsMeshParams,
  59. ...LinkCylinderParams,
  60. sizeTheme: SelectParam<SizeThemeName>('Size Theme', '', 'physical', SizeThemeOptions),
  61. sizeValue: NumberParam('Size Value', '', 0.2, 0, 10, 0.1),
  62. sizeFactor: NumberParam('Size Factor', '', 1, 0, 10, 0.1),
  63. }
  64. export const DefaultIntraUnitLinkProps = paramDefaultValues(IntraUnitLinkParams)
  65. export type IntraUnitLinkProps = typeof DefaultIntraUnitLinkProps
  66. export function IntraUnitLinkVisual(): UnitsVisual<IntraUnitLinkProps> {
  67. return UnitsMeshVisual<IntraUnitLinkProps>({
  68. defaultProps: DefaultIntraUnitLinkProps,
  69. createGeometry: createIntraUnitLinkCylinderMesh,
  70. createLocationIterator: LinkIterator.fromGroup,
  71. getLoci: getLinkLoci,
  72. mark: markLink,
  73. setUpdateState: (state: VisualUpdateState, newProps: LinkCylinderProps, currentProps: LinkCylinderProps) => {
  74. state.createGeometry = newProps.radialSegments !== currentProps.radialSegments
  75. }
  76. })
  77. }
  78. function getLinkLoci(pickingId: PickingId, group: Unit.SymmetryGroup, id: number) {
  79. const { objectId, instanceId, groupId } = pickingId
  80. const unit = group.units[instanceId]
  81. if (id === objectId && Unit.isAtomic(unit)) {
  82. return Link.Loci([
  83. Link.Location(
  84. unit, unit.links.a[groupId] as StructureElement.UnitIndex,
  85. unit, unit.links.b[groupId] as StructureElement.UnitIndex
  86. )
  87. ])
  88. }
  89. return EmptyLoci
  90. }
  91. function markLink(loci: Loci, group: Unit.SymmetryGroup, apply: (interval: Interval) => boolean) {
  92. const unit = group.units[0]
  93. let changed = false
  94. if (Unit.isAtomic(unit) && Link.isLoci(loci)) {
  95. const groupCount = unit.links.edgeCount * 2
  96. for (const b of loci.links) {
  97. const unitIdx = group.unitIndexMap.get(b.aUnit.id)
  98. if (unitIdx !== undefined) {
  99. const idx = unit.links.getDirectedEdgeIndex(b.aIndex, b.bIndex)
  100. if (idx !== -1) {
  101. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true
  102. }
  103. }
  104. }
  105. }
  106. return changed
  107. }