interactions-intra-unit-cylinder.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Copyright (c) 2019-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Unit, Structure, StructureElement } from '../../../mol-model/structure';
  7. import { Vec3 } from '../../../mol-math/linear-algebra';
  8. import { Loci, EmptyLoci } from '../../../mol-model/loci';
  9. import { Interval, OrderedSet, SortedArray } from '../../../mol-data/int';
  10. import { ParamDefinition as PD } from '../../../mol-util/param-definition';
  11. import { Mesh } from '../../../mol-geo/geometry/mesh/mesh';
  12. import { PickingId } from '../../../mol-geo/geometry/picking';
  13. import { VisualContext } from '../../../mol-repr/visual';
  14. import { Theme } from '../../../mol-theme/theme';
  15. import { InteractionsProvider } from '../interactions';
  16. import { createLinkCylinderMesh, LinkCylinderParams, LinkStyle } from '../../../mol-repr/structure/visual/util/link';
  17. import { UnitsMeshParams, UnitsVisual, UnitsMeshVisual } from '../../../mol-repr/structure/units-visual';
  18. import { VisualUpdateState } from '../../../mol-repr/util';
  19. import { LocationIterator } from '../../../mol-geo/util/location-iterator';
  20. import { Interactions } from '../interactions/interactions';
  21. import { InteractionFlag } from '../interactions/common';
  22. import { Sphere3D } from '../../../mol-math/geometry';
  23. import { StructureGroup } from '../../../mol-repr/structure/visual/util/common';
  24. async function createIntraUnitInteractionsCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<InteractionsIntraUnitParams>, mesh?: Mesh) {
  25. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh);
  26. const { child } = structure;
  27. const childUnit = child?.unitMap.get(unit.id);
  28. if (child && !childUnit) return Mesh.createEmpty(mesh);
  29. const location = StructureElement.Location.create(structure, unit);
  30. const interactions = InteractionsProvider.get(structure).value!;
  31. const features = interactions.unitsFeatures.get(unit.id);
  32. const contacts = interactions.unitsContacts.get(unit.id);
  33. const { x, y, z, members, offsets } = features;
  34. const { edgeCount, a, b, edgeProps: { flag } } = contacts;
  35. const { sizeFactor } = props;
  36. if (!edgeCount) return Mesh.createEmpty(mesh);
  37. const builderProps = {
  38. linkCount: edgeCount * 2,
  39. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  40. Vec3.set(posA, x[a[edgeIndex]], y[a[edgeIndex]], z[a[edgeIndex]]);
  41. Vec3.set(posB, x[b[edgeIndex]], y[b[edgeIndex]], z[b[edgeIndex]]);
  42. },
  43. style: (edgeIndex: number) => LinkStyle.Dashed,
  44. radius: (edgeIndex: number) => {
  45. location.element = unit.elements[members[offsets[a[edgeIndex]]]];
  46. const sizeA = theme.size.size(location);
  47. location.element = unit.elements[members[offsets[b[edgeIndex]]]];
  48. const sizeB = theme.size.size(location);
  49. return Math.min(sizeA, sizeB) * sizeFactor;
  50. },
  51. ignore: (edgeIndex: number) => (
  52. flag[edgeIndex] === InteractionFlag.Filtered ||
  53. // TODO: check all members
  54. (!!childUnit && !SortedArray.has(childUnit.elements, unit.elements[members[offsets[a[edgeIndex]]]]))
  55. )
  56. };
  57. const m = createLinkCylinderMesh(ctx, builderProps, props, mesh);
  58. const sphere = Sphere3D.expand(Sphere3D(), (childUnit ?? unit).boundary.sphere, 1 * sizeFactor);
  59. m.setBoundingSphere(sphere);
  60. return m;
  61. }
  62. export const InteractionsIntraUnitParams = {
  63. ...UnitsMeshParams,
  64. ...LinkCylinderParams,
  65. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  66. dashCount: PD.Numeric(6, { min: 2, max: 10, step: 2 }),
  67. dashScale: PD.Numeric(0.4, { min: 0, max: 2, step: 0.1 }),
  68. includeParent: PD.Boolean(false),
  69. };
  70. export type InteractionsIntraUnitParams = typeof InteractionsIntraUnitParams
  71. export function InteractionsIntraUnitVisual(materialId: number): UnitsVisual<InteractionsIntraUnitParams> {
  72. return UnitsMeshVisual<InteractionsIntraUnitParams>({
  73. defaultProps: PD.getDefaultValues(InteractionsIntraUnitParams),
  74. createGeometry: createIntraUnitInteractionsCylinderMesh,
  75. createLocationIterator: createInteractionsIterator,
  76. getLoci: getInteractionLoci,
  77. eachLocation: eachInteraction,
  78. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<InteractionsIntraUnitParams>, currentProps: PD.Values<InteractionsIntraUnitParams>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  79. state.createGeometry = (
  80. newProps.sizeFactor !== currentProps.sizeFactor ||
  81. newProps.dashCount !== currentProps.dashCount ||
  82. newProps.dashScale !== currentProps.dashScale ||
  83. newProps.dashCap !== currentProps.dashCap ||
  84. newProps.radialSegments !== currentProps.radialSegments
  85. );
  86. const interactionsHash = InteractionsProvider.get(newStructureGroup.structure).version;
  87. if ((state.info.interactionsHash as number) !== interactionsHash) {
  88. state.createGeometry = true;
  89. state.updateTransform = true;
  90. state.updateColor = true;
  91. state.info.interactionsHash = interactionsHash;
  92. }
  93. }
  94. }, materialId);
  95. }
  96. function getInteractionLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number) {
  97. const { objectId, instanceId, groupId } = pickingId;
  98. if (id === objectId) {
  99. const { structure, group } = structureGroup;
  100. const unit = structure.unitMap.get(group.units[instanceId].id);
  101. const interactions = InteractionsProvider.get(structure).value!;
  102. const { a, b } = interactions.unitsContacts.get(unit.id);
  103. return Interactions.Loci(structure, interactions, [
  104. { unitA: unit, indexA: a[groupId], unitB: unit, indexB: b[groupId] },
  105. { unitA: unit, indexA: b[groupId], unitB: unit, indexB: a[groupId] },
  106. ]);
  107. }
  108. return EmptyLoci;
  109. }
  110. const __contactIndicesSet = new Set<number>();
  111. function eachInteraction(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean, isMarking: boolean) {
  112. let changed = false;
  113. if (Interactions.isLoci(loci)) {
  114. const { structure, group } = structureGroup;
  115. if (!Structure.areEquivalent(loci.data.structure, structure)) return false;
  116. const interactions = InteractionsProvider.get(structure).value!;
  117. if (loci.data.interactions !== interactions) return false;
  118. const unit = group.units[0];
  119. const contacts = interactions.unitsContacts.get(unit.id);
  120. const groupCount = contacts.edgeCount * 2;
  121. for (const e of loci.elements) {
  122. if (e.unitA !== e.unitB) continue;
  123. const unitIdx = group.unitIndexMap.get(e.unitA.id);
  124. if (unitIdx !== undefined) {
  125. const idx = contacts.getDirectedEdgeIndex(e.indexA, e.indexB);
  126. if (idx !== -1) {
  127. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true;
  128. }
  129. }
  130. }
  131. } else if (StructureElement.Loci.is(loci)) {
  132. const { structure, group } = structureGroup;
  133. if (!Structure.areEquivalent(loci.structure, structure)) return false;
  134. const interactions = InteractionsProvider.get(structure).value;
  135. if (!interactions) return false;
  136. const unit = group.units[0];
  137. const contacts = interactions.unitsContacts.get(unit.id);
  138. const features = interactions.unitsFeatures.get(unit.id);
  139. const groupCount = contacts.edgeCount * 2;
  140. const { offset } = contacts;
  141. const { offsets: fOffsets, indices: fIndices } = features.elementsIndex;
  142. const { members, offsets } = features;
  143. for (const e of loci.elements) {
  144. const unitIdx = group.unitIndexMap.get(e.unit.id);
  145. if (unitIdx === undefined) continue;
  146. __contactIndicesSet.clear();
  147. OrderedSet.forEach(e.indices, v => {
  148. for (let i = fOffsets[v], il = fOffsets[v + 1]; i < il; ++i) {
  149. const fI = fIndices[i];
  150. for (let j = offset[fI], jl = offset[fI + 1]; j < jl; ++j) {
  151. __contactIndicesSet.add(j);
  152. }
  153. }
  154. });
  155. __contactIndicesSet.forEach(i => {
  156. if (isMarking) {
  157. const fA = contacts.a[i];
  158. for (let j = offsets[fA], jl = offsets[fA + 1]; j < jl; ++j) {
  159. if (!OrderedSet.has(e.indices, members[j])) return;
  160. }
  161. const fB = contacts.b[i];
  162. for (let j = offsets[fB], jl = offsets[fB + 1]; j < jl; ++j) {
  163. if (!OrderedSet.has(e.indices, members[j])) return;
  164. }
  165. }
  166. if (apply(Interval.ofSingleton(unitIdx * groupCount + i))) changed = true;
  167. });
  168. }
  169. }
  170. return changed;
  171. }
  172. function createInteractionsIterator(structureGroup: StructureGroup): LocationIterator {
  173. const { structure, group } = structureGroup;
  174. const unit = group.units[0];
  175. const interactions = InteractionsProvider.get(structure).value!;
  176. const contacts = interactions.unitsContacts.get(unit.id);
  177. const groupCount = contacts.edgeCount * 2;
  178. const instanceCount = group.units.length;
  179. const location = Interactions.Location(interactions, structure);
  180. const { element } = location;
  181. const getLocation = (groupIndex: number, instanceIndex: number) => {
  182. const instanceUnit = group.units[instanceIndex];
  183. element.unitA = instanceUnit;
  184. element.indexA = contacts.a[groupIndex];
  185. element.unitB = instanceUnit;
  186. element.indexB = contacts.b[groupIndex];
  187. return location;
  188. };
  189. return LocationIterator(groupCount, instanceCount, 1, getLocation);
  190. }