interactions-intra-unit-cylinder.ts 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Copyright (c) 2019-2020 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 } 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, StructureGroup } 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. async function createIntraUnitInteractionsCylinderMesh(ctx: VisualContext, unit: Unit, structure: Structure, theme: Theme, props: PD.Values<InteractionsIntraUnitParams>, mesh?: Mesh) {
  24. if (!Unit.isAtomic(unit)) return Mesh.createEmpty(mesh);
  25. const location = StructureElement.Location.create(structure, unit);
  26. const interactions = InteractionsProvider.get(structure).value!;
  27. const features = interactions.unitsFeatures.get(unit.id);
  28. const contacts = interactions.unitsContacts.get(unit.id);
  29. const { x, y, z, members, offsets } = features;
  30. const { edgeCount, a, b, edgeProps: { flag } } = contacts;
  31. const { sizeFactor } = props;
  32. if (!edgeCount) return Mesh.createEmpty(mesh);
  33. const builderProps = {
  34. linkCount: edgeCount * 2,
  35. position: (posA: Vec3, posB: Vec3, edgeIndex: number) => {
  36. Vec3.set(posA, x[a[edgeIndex]], y[a[edgeIndex]], z[a[edgeIndex]]);
  37. Vec3.set(posB, x[b[edgeIndex]], y[b[edgeIndex]], z[b[edgeIndex]]);
  38. },
  39. style: (edgeIndex: number) => LinkStyle.Dashed,
  40. radius: (edgeIndex: number) => {
  41. location.element = unit.elements[members[offsets[a[edgeIndex]]]];
  42. const sizeA = theme.size.size(location);
  43. location.element = unit.elements[members[offsets[b[edgeIndex]]]];
  44. const sizeB = theme.size.size(location);
  45. return Math.min(sizeA, sizeB) * sizeFactor;
  46. },
  47. ignore: (edgeIndex: number) => flag[edgeIndex] === InteractionFlag.Filtered
  48. };
  49. const m = createLinkCylinderMesh(ctx, builderProps, props, mesh);
  50. const sphere = Sphere3D.expand(Sphere3D(), unit.boundary.sphere, 1 * sizeFactor);
  51. m.setBoundingSphere(sphere);
  52. return m;
  53. }
  54. export const InteractionsIntraUnitParams = {
  55. ...UnitsMeshParams,
  56. ...LinkCylinderParams,
  57. sizeFactor: PD.Numeric(0.3, { min: 0, max: 10, step: 0.01 }),
  58. dashCount: PD.Numeric(6, { min: 2, max: 10, step: 2 }),
  59. dashScale: PD.Numeric(0.4, { min: 0, max: 2, step: 0.1 }),
  60. };
  61. export type InteractionsIntraUnitParams = typeof InteractionsIntraUnitParams
  62. export function InteractionsIntraUnitVisual(materialId: number): UnitsVisual<InteractionsIntraUnitParams> {
  63. return UnitsMeshVisual<InteractionsIntraUnitParams>({
  64. defaultProps: PD.getDefaultValues(InteractionsIntraUnitParams),
  65. createGeometry: createIntraUnitInteractionsCylinderMesh,
  66. createLocationIterator: createInteractionsIterator,
  67. getLoci: getInteractionLoci,
  68. eachLocation: eachInteraction,
  69. setUpdateState: (state: VisualUpdateState, newProps: PD.Values<InteractionsIntraUnitParams>, currentProps: PD.Values<InteractionsIntraUnitParams>, newTheme: Theme, currentTheme: Theme, newStructureGroup: StructureGroup, currentStructureGroup: StructureGroup) => {
  70. state.createGeometry = (
  71. newProps.sizeFactor !== currentProps.sizeFactor ||
  72. newProps.dashCount !== currentProps.dashCount ||
  73. newProps.dashScale !== currentProps.dashScale ||
  74. newProps.dashCap !== currentProps.dashCap ||
  75. newProps.radialSegments !== currentProps.radialSegments
  76. );
  77. const interactionsHash = InteractionsProvider.get(newStructureGroup.structure).version;
  78. if ((state.info.interactionsHash as number) !== interactionsHash) {
  79. state.createGeometry = true;
  80. state.updateTransform = true;
  81. state.updateColor = true;
  82. state.info.interactionsHash = interactionsHash;
  83. }
  84. }
  85. }, materialId);
  86. }
  87. function getInteractionLoci(pickingId: PickingId, structureGroup: StructureGroup, id: number) {
  88. const { objectId, instanceId, groupId } = pickingId;
  89. if (id === objectId) {
  90. const { structure, group } = structureGroup;
  91. const unit = structure.unitMap.get(group.units[instanceId].id);
  92. const interactions = InteractionsProvider.get(structure).value!;
  93. const { a, b } = interactions.unitsContacts.get(unit.id);
  94. return Interactions.Loci(structure, interactions, [
  95. { unitA: unit, indexA: a[groupId], unitB: unit, indexB: b[groupId] },
  96. { unitA: unit, indexA: b[groupId], unitB: unit, indexB: a[groupId] },
  97. ]);
  98. }
  99. return EmptyLoci;
  100. }
  101. function eachInteraction(loci: Loci, structureGroup: StructureGroup, apply: (interval: Interval) => boolean, isMarking: boolean) {
  102. let changed = false;
  103. if (Interactions.isLoci(loci)) {
  104. const { structure, group } = structureGroup;
  105. if (!Structure.areEquivalent(loci.data.structure, structure)) return false;
  106. const interactions = InteractionsProvider.get(structure).value!;
  107. if (loci.data.interactions !== interactions) return false;
  108. const unit = group.units[0];
  109. const contacts = interactions.unitsContacts.get(unit.id);
  110. const groupCount = contacts.edgeCount * 2;
  111. for (const e of loci.elements) {
  112. if (e.unitA !== e.unitB) continue;
  113. const unitIdx = group.unitIndexMap.get(e.unitA.id);
  114. if (unitIdx !== undefined) {
  115. const idx = contacts.getDirectedEdgeIndex(e.indexA, e.indexB);
  116. if (idx !== -1) {
  117. if (apply(Interval.ofSingleton(unitIdx * groupCount + idx))) changed = true;
  118. }
  119. }
  120. }
  121. } else if (StructureElement.Loci.is(loci)) {
  122. const { structure, group } = structureGroup;
  123. if (!Structure.areEquivalent(loci.structure, structure)) return false;
  124. const interactions = InteractionsProvider.get(structure).value;
  125. if (!interactions) return false;
  126. const unit = group.units[0];
  127. const contacts = interactions.unitsContacts.get(unit.id);
  128. const features = interactions.unitsFeatures.get(unit.id);
  129. const groupCount = contacts.edgeCount * 2;
  130. const { offset } = contacts;
  131. const { offsets: fOffsets, indices: fIndices } = features.elementsIndex;
  132. // TODO when isMarking, all elements of contact features need to be in the loci
  133. for (const e of loci.elements) {
  134. const unitIdx = group.unitIndexMap.get(e.unit.id);
  135. if (unitIdx !== undefined) continue;
  136. if (isMarking && OrderedSet.size(e.indices) === 1) continue;
  137. OrderedSet.forEach(e.indices, v => {
  138. for (let i = fOffsets[v], il = fOffsets[v + 1]; i < il; ++i) {
  139. const fI = fIndices[i];
  140. for (let j = offset[fI], jl = offset[fI + 1]; j < jl; ++j) {
  141. if (apply(Interval.ofSingleton(unitIdx * groupCount + j))) changed = true;
  142. }
  143. }
  144. });
  145. }
  146. }
  147. return changed;
  148. }
  149. function createInteractionsIterator(structureGroup: StructureGroup): LocationIterator {
  150. const { structure, group } = structureGroup;
  151. const unit = group.units[0];
  152. const interactions = InteractionsProvider.get(structure).value!;
  153. const contacts = interactions.unitsContacts.get(unit.id);
  154. const groupCount = contacts.edgeCount * 2;
  155. const instanceCount = group.units.length;
  156. const location = Interactions.Location(interactions, structure);
  157. const { element } = location;
  158. const getLocation = (groupIndex: number, instanceIndex: number) => {
  159. const instanceUnit = group.units[instanceIndex];
  160. element.unitA = instanceUnit;
  161. element.indexA = contacts.a[groupIndex];
  162. element.unitB = instanceUnit;
  163. element.indexB = contacts.b[groupIndex];
  164. return location;
  165. };
  166. return LocationIterator(groupCount, instanceCount, 1, getLocation);
  167. }