intra-unit-link-cylinder.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 } from '../representation';
  9. import { VisualUpdateState } from '../../util';
  10. import { createLinkCylinderMesh, LinkIterator, LinkCylinderParams } from './util/link';
  11. import { Vec3 } from 'mol-math/linear-algebra';
  12. import { Loci, EmptyLoci } from 'mol-model/loci';
  13. import { UnitsMeshVisual, UnitsMeshParams, StructureGroup } from '../units-visual';
  14. import { Interval } from 'mol-data/int';
  15. import { BitFlags } from 'mol-util';
  16. import { ParamDefinition as PD } from 'mol-util/param-definition';
  17. import { Mesh } from 'mol-geo/geometry/mesh/mesh';
  18. import { PickingId } from 'mol-geo/geometry/picking';
  19. import { VisualContext } from 'mol-repr/representation';
  20. import { Theme } from 'mol-theme/theme';
  21. async function createIntraUnitLinkCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<IntraUnitLinkParams>, mesh?: Mesh) {
  22. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh)
  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. const { sizeFactor, sizeAspectRatio } = props
  29. if (!edgeCount) return Mesh.createEmpty(mesh)
  30. const vRef = Vec3.zero()
  31. const pos = unit.conformation.invariantPosition
  32. const builderProps = {
  33. linkCount: edgeCount * 2,
  34. referencePosition: (edgeIndex: number) => {
  35. let aI = a[edgeIndex], bI = b[edgeIndex];
  36. if (aI > bI) [aI, bI] = [bI, aI]
  37. for (let i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
  38. if (b[i] !== bI) return pos(elements[b[i]], vRef)
  39. }
  40. for (let i = offset[bI], il = offset[bI + 1]; i < il; ++i) {
  41. if (a[i] !== aI) return pos(elements[a[i]], vRef)
  42. }
  43. return null
  44. },
  45. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  46. pos(elements[a[edgeIndex]], posA)
  47. pos(elements[b[edgeIndex]], posB)
  48. },
  49. order: (edgeIndex: number) => _order[edgeIndex],
  50. flags: (edgeIndex: number) => BitFlags.create(_flags[edgeIndex]),
  51. radius: (edgeIndex: number) => {
  52. location.element = elements[a[edgeIndex]]
  53. return theme.size.size(location) * sizeFactor * sizeAspectRatio
  54. }
  55. }
  56. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  57. }
  58. export const IntraUnitLinkParams = {
  59. ...UnitsMeshParams,
  60. ...LinkCylinderParams,
  61. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  62. sizeAspectRatio: PD.Numeric(2/3, { min: 0, max: 3, step: 0.01 }),
  63. }
  64. export type IntraUnitLinkParams = typeof IntraUnitLinkParams
  65. export function IntraUnitLinkVisual(): UnitsVisual<IntraUnitLinkParams> {
  66. return UnitsMeshVisual<IntraUnitLinkParams>({
  67. defaultProps: PD.getDefaultValues(IntraUnitLinkParams),
  68. createGeometry: createIntraUnitLinkCylinderMesh,
  69. createLocationIterator: LinkIterator.fromGroup,
  70. getLoci: getLinkLoci,
  71. mark: markLink,
  72. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<IntraUnitLinkParams>, currentProps: PD.Values<IntraUnitLinkParams>) => {
  73. state.createGeometry = (
  74. newProps.sizeFactor !== currentProps.sizeFactor ||
  75. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  76. newProps.radialSegments !== currentProps.radialSegments ||
  77. newProps.linkScale !== currentProps.linkScale ||
  78. newProps.linkSpacing !== currentProps.linkSpacing
  79. )
  80. }
  81. })
  82. }
  83. function getLinkLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number) {
  84. const { objectId, instanceId, groupId } = pickingId
  85. if (id === objectId) {
  86. const { structure, group } = structureGroup
  87. const unit = group.units[instanceId]
  88. if (Unit.isAtomic(unit)) {
  89. return Link.Loci(structure, [
  90. Link.Location(
  91. unit, unit.links.a[groupId] as StructureElement.UnitIndex,
  92. unit, unit.links.b[groupId] as StructureElement.UnitIndex
  93. )
  94. ])
  95. }
  96. }
  97. return EmptyLoci
  98. }
  99. function markLink(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean) {
  100. let changed = false
  101. if (!Link.isLoci(loci)) return false
  102. const { structure, group } = structureGroup
  103. if (loci.structure !== structure) return false
  104. const unit = group.units[0]
  105. if (!Unit.isAtomic(unit)) return false
  106. const groupCount = unit.links.edgeCount * 2
  107. for (const b of loci.links) {
  108. const unitIdx = group.unitIndexMap.get(b.aUnit.id)
  109. if (unitIdx !== undefined) {
  110. const idx = unit.links.getDirectedEdgeIndex(b.aIndex, b.bIndex)
  111. if (idx !== -1) {
  112. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true
  113. }
  114. }
  115. }
  116. return changed
  117. }