intra-unit-link-cylinder.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, OrderedSet } 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/visual';
  20. import { Theme } from 'mol-theme/theme';
  21. 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. eachLocation: eachLink,
  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. Link.Location(
  95. unit, unit.links.b[groupId] as StructureElement.UnitIndex,
  96. unit, unit.links.a[groupId] as StructureElement.UnitIndex
  97. )
  98. ])
  99. }
  100. }
  101. return EmptyLoci
  102. }
  103. function eachLink(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean) {
  104. let changed = false
  105. if (Link.isLoci(loci)) {
  106. const { structure, group } = structureGroup
  107. if (loci.structure !== structure) return false
  108. const unit = group.units[0]
  109. if (!Unit.isAtomic(unit)) return false
  110. const groupCount = unit.links.edgeCount * 2
  111. for (const b of loci.links) {
  112. const unitIdx = group.unitIndexMap.get(b.aUnit.id)
  113. if (unitIdx !== undefined) {
  114. const idx = unit.links.getDirectedEdgeIndex(b.aIndex, b.bIndex)
  115. if (idx !== -1) {
  116. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true
  117. }
  118. }
  119. }
  120. } else if (StructureElement.isLoci(loci)) {
  121. const { structure, group } = structureGroup
  122. if (loci.structure !== structure) return false
  123. const unit = group.units[0]
  124. if (!Unit.isAtomic(unit)) return false
  125. const groupCount = unit.links.edgeCount * 2
  126. for (const e of loci.elements) {
  127. const unitIdx = group.unitIndexMap.get(e.unit.id)
  128. if (unitIdx !== undefined) {
  129. const { offset, b } = unit.links
  130. OrderedSet.forEach(e.indices, v => {
  131. for (let t = offset[v], _t = offset[v + 1]; t < _t; t++) {
  132. if (OrderedSet.has(e.indices, b[t])) {
  133. if (apply(Interval.ofSingleton(unitIdx * groupCount + t))) changed = true
  134. }
  135. }
  136. })
  137. }
  138. }
  139. }
  140. return changed
  141. }