intra-unit-link-cylinder.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. if (offset[aI + 1] - offset[aI] === 1) [aI, bI] = [bI, aI]
  38. // TODO prefer reference atoms in rings
  39. for (let i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
  40. const _bI = b[i]
  41. if (_bI !== bI && _bI !== aI) return pos(elements[_bI], vRef)
  42. }
  43. for (let i = offset[bI], il = offset[bI + 1]; i < il; ++i) {
  44. const _aI = a[i]
  45. if (_aI !== aI && _aI !== bI) return pos(elements[_aI], vRef)
  46. }
  47. return null
  48. },
  49. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  50. pos(elements[a[edgeIndex]], posA)
  51. pos(elements[b[edgeIndex]], posB)
  52. },
  53. order: (edgeIndex: number) => _order[edgeIndex],
  54. flags: (edgeIndex: number) => BitFlags.create(_flags[edgeIndex]),
  55. radius: (edgeIndex: number) => {
  56. location.element = elements[a[edgeIndex]]
  57. return theme.size.size(location) * sizeFactor * sizeAspectRatio
  58. }
  59. }
  60. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  61. }
  62. export const IntraUnitLinkParams = {
  63. ...UnitsMeshParams,
  64. ...LinkCylinderParams,
  65. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  66. sizeAspectRatio: PD.Numeric(2/3, { min: 0, max: 3, step: 0.01 }),
  67. }
  68. export type IntraUnitLinkParams = typeof IntraUnitLinkParams
  69. export function IntraUnitLinkVisual(materialId: number): UnitsVisual<IntraUnitLinkParams> {
  70. return UnitsMeshVisual<IntraUnitLinkParams>({
  71. defaultProps: PD.getDefaultValues(IntraUnitLinkParams),
  72. createGeometry: createIntraUnitLinkCylinderMesh,
  73. createLocationIterator: LinkIterator.fromGroup,
  74. getLoci: getLinkLoci,
  75. eachLocation: eachLink,
  76. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<IntraUnitLinkParams>, currentProps: PD.Values<IntraUnitLinkParams>) => {
  77. state.createGeometry = (
  78. newProps.sizeFactor !== currentProps.sizeFactor ||
  79. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  80. newProps.radialSegments !== currentProps.radialSegments ||
  81. newProps.linkScale !== currentProps.linkScale ||
  82. newProps.linkSpacing !== currentProps.linkSpacing
  83. )
  84. }
  85. }, materialId)
  86. }
  87. function getLinkLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number) {
  88. const { objectId, instanceId, groupId } = pickingId
  89. if (id === objectId) {
  90. const { structure, group } = structureGroup
  91. const unit = group.units[instanceId]
  92. if (Unit.isAtomic(unit)) {
  93. return Link.Loci(structure, [
  94. Link.Location(
  95. unit, unit.links.a[groupId] as StructureElement.UnitIndex,
  96. unit, unit.links.b[groupId] as StructureElement.UnitIndex
  97. ),
  98. Link.Location(
  99. unit, unit.links.b[groupId] as StructureElement.UnitIndex,
  100. unit, unit.links.a[groupId] as StructureElement.UnitIndex
  101. )
  102. ])
  103. }
  104. }
  105. return EmptyLoci
  106. }
  107. function eachLink(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean) {
  108. let changed = false
  109. if (Link.isLoci(loci)) {
  110. const { structure, group } = structureGroup
  111. if (!Structure.areEquivalent(loci.structure, structure)) return false
  112. const unit = group.units[0]
  113. if (!Unit.isAtomic(unit)) return false
  114. const groupCount = unit.links.edgeCount * 2
  115. for (const b of loci.links) {
  116. const unitIdx = group.unitIndexMap.get(b.aUnit.id)
  117. if (unitIdx !== undefined) {
  118. const idx = unit.links.getDirectedEdgeIndex(b.aIndex, b.bIndex)
  119. if (idx !== -1) {
  120. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true
  121. }
  122. }
  123. }
  124. } else if (StructureElement.isLoci(loci)) {
  125. const { structure, group } = structureGroup
  126. if (!Structure.areEquivalent(loci.structure, structure)) return false
  127. const unit = group.units[0]
  128. if (!Unit.isAtomic(unit)) return false
  129. const groupCount = unit.links.edgeCount * 2
  130. for (const e of loci.elements) {
  131. const unitIdx = group.unitIndexMap.get(e.unit.id)
  132. if (unitIdx !== undefined) {
  133. const { offset, b } = unit.links
  134. OrderedSet.forEach(e.indices, v => {
  135. for (let t = offset[v], _t = offset[v + 1]; t < _t; t++) {
  136. if (OrderedSet.has(e.indices, b[t])) {
  137. if (apply(Interval.ofSingleton(unitIdx * groupCount + t))) changed = true
  138. }
  139. }
  140. })
  141. }
  142. }
  143. }
  144. return changed
  145. }