bond-intra-unit-cylinder.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /**
  2. * Copyright (c) 2018-2020 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 { ParamDefinition as PD } from '../../../mol-util/param-definition';
  8. import { VisualContext } from '../../visual';
  9. import { Unit, Structure, StructureElement, Bond } from '../../../mol-model/structure';
  10. import { Theme } from '../../../mol-theme/theme';
  11. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  12. import { Vec3 } from '../../../mol-math/linear-algebra';
  13. import { BitFlags, arrayEqual } from '../../../mol-util';
  14. import { createLinkCylinderMesh, LinkCylinderStyle } from './util/link';
  15. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual, StructureGroup } from '../units-visual';
  16. import { VisualUpdateState } from '../../util';
  17. import { PickingId } from '../../../mol-geo/geometry/picking';
  18. import { EmptyLoci, Loci } from '../../../mol-model/loci';
  19. import { Interval, OrderedSet } from '../../../mol-data/int';
  20. import { isHydrogen } from './util/common';
  21. import { BondType } from '../../../mol-model/structure/model/types';
  22. import { ignoreBondType, BondCylinderParams, BondIterator } from './util/bond';
  23. function createIntraUnitBondCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<IntraUnitBondParams>, mesh?: Mesh) {
  24. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh)
  25. const location = StructureElement.Location.create(structure, unit)
  26. const elements = unit.elements;
  27. const bonds = unit.bonds
  28. const { edgeCount, a, b, edgeProps, offset } = bonds
  29. const { order: _order, flags: _flags } = edgeProps
  30. const { sizeFactor, sizeAspectRatio, ignoreHydrogens, includeTypes, excludeTypes } = props
  31. const include = BondType.fromNames(includeTypes)
  32. const exclude = BondType.fromNames(excludeTypes)
  33. const ignoreHydrogen = ignoreHydrogens ? (edgeIndex: number) => {
  34. return isHydrogen(unit, elements[a[edgeIndex]]) || isHydrogen(unit, elements[b[edgeIndex]])
  35. } : () => false
  36. if (!edgeCount) return Mesh.createEmpty(mesh)
  37. const vRef = Vec3.zero()
  38. const pos = unit.conformation.invariantPosition
  39. const builderProps = {
  40. linkCount: edgeCount * 2,
  41. referencePosition: (edgeIndex: number) => {
  42. let aI = a[edgeIndex], bI = b[edgeIndex];
  43. if (aI > bI) [aI, bI] = [bI, aI]
  44. if (offset[aI + 1] - offset[aI] === 1) [aI, bI] = [bI, aI]
  45. // TODO prefer reference atoms in rings
  46. for (let i = offset[aI], il = offset[aI + 1]; i < il; ++i) {
  47. const _bI = b[i]
  48. if (_bI !== bI && _bI !== aI) return pos(elements[_bI], vRef)
  49. }
  50. for (let i = offset[bI], il = offset[bI + 1]; i < il; ++i) {
  51. const _aI = a[i]
  52. if (_aI !== aI && _aI !== bI) return pos(elements[_aI], vRef)
  53. }
  54. return null
  55. },
  56. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  57. pos(elements[a[edgeIndex]], posA)
  58. pos(elements[b[edgeIndex]], posB)
  59. },
  60. style: (edgeIndex: number) => {
  61. const o = _order[edgeIndex]
  62. const f = BitFlags.create(_flags[edgeIndex])
  63. if (BondType.is(f, BondType.Flag.MetallicCoordination) || BondType.is(f, BondType.Flag.HydrogenBond)) {
  64. // show metall coordinations and hydrogen bonds with dashed cylinders
  65. return LinkCylinderStyle.Dashed
  66. } else if (o === 2) {
  67. return LinkCylinderStyle.Double
  68. } else if (o === 3) {
  69. return LinkCylinderStyle.Triple
  70. } else {
  71. return LinkCylinderStyle.Solid
  72. }
  73. },
  74. radius: (edgeIndex: number) => {
  75. location.element = elements[a[edgeIndex]]
  76. const sizeA = theme.size.size(location)
  77. location.element = elements[b[edgeIndex]]
  78. const sizeB = theme.size.size(location)
  79. return Math.min(sizeA, sizeB) * sizeFactor * sizeAspectRatio
  80. },
  81. ignore: (edgeIndex: number) => ignoreHydrogen(edgeIndex) || ignoreBondType(include, exclude, _flags[edgeIndex])
  82. }
  83. return createLinkCylinderMesh(ctx, builderProps, props, mesh)
  84. }
  85. export const IntraUnitBondParams = {
  86. ...UnitsMeshParams,
  87. ...BondCylinderParams,
  88. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  89. sizeAspectRatio: PD.Numeric(2/3, { min: 0, max: 3, step: 0.01 }),
  90. ignoreHydrogens: PD.Boolean(false),
  91. }
  92. export type IntraUnitBondParams = typeof IntraUnitBondParams
  93. export function IntraUnitBondVisual(materialId: number): UnitsVisual<IntraUnitBondParams> {
  94. return UnitsMeshVisual<IntraUnitBondParams>({
  95. defaultProps: PD.getDefaultValues(IntraUnitBondParams),
  96. createGeometry: createIntraUnitBondCylinderMesh,
  97. createLocationIterator: BondIterator.fromGroup,
  98. getLoci: getBondLoci,
  99. eachLocation: eachBond,
  100. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<IntraUnitBondParams>, currentProps: PD.Values<IntraUnitBondParams>) => {
  101. state.createGeometry = (
  102. newProps.sizeFactor !== currentProps.sizeFactor ||
  103. newProps.sizeAspectRatio !== currentProps.sizeAspectRatio ||
  104. newProps.radialSegments !== currentProps.radialSegments ||
  105. newProps.linkScale !== currentProps.linkScale ||
  106. newProps.linkSpacing !== currentProps.linkSpacing ||
  107. newProps.ignoreHydrogens !== currentProps.ignoreHydrogens ||
  108. newProps.linkCap !== currentProps.linkCap ||
  109. !arrayEqual(newProps.includeTypes, currentProps.includeTypes) ||
  110. !arrayEqual(newProps.excludeTypes, currentProps.excludeTypes)
  111. )
  112. }
  113. }, materialId)
  114. }
  115. function getBondLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number) {
  116. const { objectId, instanceId, groupId } = pickingId
  117. if (id === objectId) {
  118. const { structure, group } = structureGroup
  119. const unit = group.units[instanceId]
  120. if (Unit.isAtomic(unit)) {
  121. return Bond.Loci(structure, [
  122. Bond.Location(
  123. structure, unit, unit.bonds.a[groupId] as StructureElement.UnitIndex,
  124. structure, unit, unit.bonds.b[groupId] as StructureElement.UnitIndex
  125. ),
  126. Bond.Location(
  127. structure, unit, unit.bonds.b[groupId] as StructureElement.UnitIndex,
  128. structure, unit, unit.bonds.a[groupId] as StructureElement.UnitIndex
  129. )
  130. ])
  131. }
  132. }
  133. return EmptyLoci
  134. }
  135. function eachBond(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean) {
  136. let changed = false
  137. if (Bond.isLoci(loci)) {
  138. const { structure, group } = structureGroup
  139. if (!Structure.areEquivalent(loci.structure, structure)) return false
  140. const unit = group.units[0]
  141. if (!Unit.isAtomic(unit)) return false
  142. const groupCount = unit.bonds.edgeCount * 2
  143. for (const b of loci.bonds) {
  144. const unitIdx = group.unitIndexMap.get(b.aUnit.id)
  145. if (unitIdx !== undefined) {
  146. const idx = unit.bonds.getDirectedEdgeIndex(b.aIndex, b.bIndex)
  147. if (idx !== -1) {
  148. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true
  149. }
  150. }
  151. }
  152. } else if (StructureElement.Loci.is(loci)) {
  153. const { structure, group } = structureGroup
  154. if (!Structure.areEquivalent(loci.structure, structure)) return false
  155. const unit = group.units[0]
  156. if (!Unit.isAtomic(unit)) return false
  157. const groupCount = unit.bonds.edgeCount * 2
  158. for (const e of loci.elements) {
  159. const unitIdx = group.unitIndexMap.get(e.unit.id)
  160. if (unitIdx !== undefined) {
  161. const { offset, b } = unit.bonds
  162. OrderedSet.forEach(e.indices, v => {
  163. for (let t = offset[v], _t = offset[v + 1]; t < _t; t++) {
  164. if (OrderedSet.has(e.indices, b[t])) {
  165. if (apply(Interval.ofSingleton(unitIdx * groupCount + t))) changed = true
  166. }
  167. }
  168. })
  169. }
  170. }
  171. }
  172. return changed
  173. }