structure-set.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author David Sehnal <david.sehnal@gmail.com>
  5. */
  6. import { Structure, Unit, StructureElement } from '../../structure'
  7. import { SortedArray } from 'mol-data/int';
  8. import { StructureSubsetBuilder } from '../../structure/util/subset-builder';
  9. export function structureUnion(source: Structure, structures: Structure[]) {
  10. if (structures.length === 0) return Structure.Empty;
  11. if (structures.length === 1) return structures[0];
  12. const unitMap = new Map<number, StructureElement.Set>();
  13. const fullUnits = new Set<number>();
  14. for (const { units } of structures) {
  15. for (let i = 0, _i = units.length; i < _i; i++) {
  16. const u = units[i];
  17. if (unitMap.has(u.id)) {
  18. // check if there is anything more to union in this particual unit.
  19. if (fullUnits.has(u.id)) continue;
  20. const merged = SortedArray.union(unitMap.get(u.id)!, u.elements);
  21. unitMap.set(u.id, merged);
  22. if (merged.length === source.unitMap.get(u.id).elements.length) fullUnits.add(u.id);
  23. } else {
  24. unitMap.set(u.id, u.elements);
  25. if (u.elements.length === source.unitMap.get(u.id).elements.length) fullUnits.add(u.id);
  26. }
  27. }
  28. }
  29. const builder = source.subsetBuilder(true);
  30. unitMap.forEach(buildUnion, builder);
  31. return builder.getStructure();
  32. }
  33. function buildUnion(this: StructureSubsetBuilder, elements: StructureElement.Set, id: number) {
  34. this.setUnit(id, elements);
  35. }
  36. export function structureAreIntersecting(sA: Structure, sB: Structure): boolean {
  37. if (sA === sB) return true;
  38. let a, b;
  39. if (sA.units.length < sB.units.length) { a = sA; b = sB; }
  40. else { a = sB; b = sA; }
  41. const aU = a.units, bU = b.unitMap;
  42. for (let i = 0, _i = aU.length; i < _i; i++) {
  43. const u = aU[i];
  44. if (!bU.has(u.id)) continue;
  45. const v = bU.get(u.id);
  46. if (SortedArray.areIntersecting(u.elements, v.elements)) return true;
  47. }
  48. return false;
  49. }
  50. export function structureIntersect(sA: Structure, sB: Structure): Structure {
  51. if (sA === sB) return sA;
  52. if (!structureAreIntersecting(sA, sB)) return Structure.Empty;
  53. let a, b;
  54. if (sA.units.length < sB.units.length) { a = sA; b = sB; }
  55. else { a = sB; b = sA; }
  56. const aU = a.units, bU = b.unitMap;
  57. const units: Unit[] = [];
  58. for (let i = 0, _i = aU.length; i < _i; i++) {
  59. const u = aU[i];
  60. if (!bU.has(u.id)) continue;
  61. const v = bU.get(u.id);
  62. if (SortedArray.areIntersecting(u.elements, v.elements)) {
  63. const int = SortedArray.intersect(u.elements, v.elements);
  64. units[units.length] = u.getChild(int);
  65. }
  66. }
  67. return Structure.create(units);
  68. }
  69. export function structureSubtract(a: Structure, b: Structure): Structure {
  70. if (a === b) return Structure.Empty;
  71. if (!structureAreIntersecting(a, b)) return a;
  72. const aU = a.units, bU = b.unitMap;
  73. const units: Unit[] = [];
  74. for (let i = 0, _i = aU.length; i < _i; i++) {
  75. const u = aU[i];
  76. if (!bU.has(u.id)) continue;
  77. const v = bU.get(u.id);
  78. const sub = SortedArray.intersect(u.elements, v.elements);
  79. if (sub.length > 0) {
  80. units[units.length] = u.getChild(sub);
  81. }
  82. }
  83. return Structure.create(units);
  84. }