intra-unit-link-cylinder.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { LinkCylinderProps, 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.DefaultValues<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 } = 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
  54. }
  55. }
  56. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  57. }
  58. export const IntraUnitLinkParams = {
  59. ...UnitsMeshParams,
  60. ...LinkCylinderParams,
  61. sizeFactor: PD.Numeric('Size Factor', '', 0.2, 0, 10, 0.01),
  62. }
  63. export type IntraUnitLinkParams = typeof IntraUnitLinkParams
  64. export function IntraUnitLinkVisual(): UnitsVisual<IntraUnitLinkParams> {
  65. return UnitsMeshVisual<IntraUnitLinkParams>({
  66. defaultProps: PD.getDefaultValues(IntraUnitLinkParams),
  67. createGeometry: createIntraUnitLinkCylinderMesh,
  68. createLocationIterator: LinkIterator.fromGroup,
  69. getLoci: getLinkLoci,
  70. mark: markLink,
  71. setUpdateState: (state: VisualUpdateState, newProps: LinkCylinderProps, currentProps: LinkCylinderProps) => {
  72. if (newProps.linkScale !== currentProps.linkScale) state.createGeometry = true
  73. if (newProps.linkSpacing !== currentProps.linkSpacing) state.createGeometry = true
  74. if (newProps.radialSegments !== currentProps.radialSegments) state.createGeometry = true
  75. }
  76. })
  77. }
  78. function getLinkLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number) {
  79. const { objectId, instanceId, groupId } = pickingId
  80. if (id === objectId) {
  81. const { structure, group } = structureGroup
  82. const unit = group.units[instanceId]
  83. if (Unit.isAtomic(unit)) {
  84. return Link.Loci(structure, [
  85. Link.Location(
  86. unit, unit.links.a[groupId] as StructureElement.UnitIndex,
  87. unit, unit.links.b[groupId] as StructureElement.UnitIndex
  88. )
  89. ])
  90. }
  91. }
  92. return EmptyLoci
  93. }
  94. function markLink(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean) {
  95. let changed = false
  96. if (!Link.isLoci(loci)) return false
  97. const { structure, group } = structureGroup
  98. if (loci.structure !== structure) return false
  99. const unit = group.units[0]
  100. if (!Unit.isAtomic(unit)) return false
  101. const groupCount = unit.links.edgeCount * 2
  102. for (const b of loci.links) {
  103. const unitIdx = group.unitIndexMap.get(b.aUnit.id)
  104. if (unitIdx !== undefined) {
  105. const idx = unit.links.getDirectedEdgeIndex(b.aIndex, b.bIndex)
  106. if (idx !== -1) {
  107. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true
  108. }
  109. }
  110. }
  111. return changed
  112. }