substance.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright (c) 2021 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. import { Loci } from '../mol-model/loci';
  7. import { Structure, StructureElement } from '../mol-model/structure';
  8. import { Script } from '../mol-script/script';
  9. import { Material } from '../mol-util/material';
  10. import { shallowEqual } from '../mol-util/object';
  11. export { Substance };
  12. type Substance = { readonly layers: ReadonlyArray<Substance.Layer> }
  13. function Substance(layers: ReadonlyArray<Substance.Layer>): Substance {
  14. return { layers };
  15. }
  16. namespace Substance {
  17. export type Layer = { readonly loci: StructureElement.Loci, readonly material: Material, readonly clear: boolean }
  18. export const Empty: Substance = { layers: [] };
  19. export function areEqual(sA: Substance, sB: Substance) {
  20. if (sA.layers.length === 0 && sB.layers.length === 0) return true;
  21. if (sA.layers.length !== sB.layers.length) return false;
  22. for (let i = 0, il = sA.layers.length; i < il; ++i) {
  23. if (sA.layers[i].clear !== sB.layers[i].clear) return false;
  24. if (!shallowEqual(sA.layers[i].material, sB.layers[i].material)) return false;
  25. if (!Loci.areEqual(sA.layers[i].loci, sB.layers[i].loci)) return false;
  26. }
  27. return true;
  28. }
  29. export function isEmpty(overpaint: Substance) {
  30. return overpaint.layers.length === 0;
  31. }
  32. export function remap(substance: Substance, structure: Structure) {
  33. const layers: Substance.Layer[] = [];
  34. for (const layer of substance.layers) {
  35. let { loci, material, clear } = layer;
  36. loci = StructureElement.Loci.remap(loci, structure);
  37. if (!StructureElement.Loci.isEmpty(loci)) {
  38. layers.push({ loci, material, clear });
  39. }
  40. }
  41. return { layers };
  42. }
  43. export function merge(substance: Substance): Substance {
  44. if (isEmpty(substance)) return substance;
  45. const { structure } = substance.layers[0].loci;
  46. let clearLoci: StructureElement.Loci | undefined = void 0;
  47. const map = new Map<Material, StructureElement.Loci>();
  48. let shadowed = StructureElement.Loci.none(structure);
  49. for (let i = 0, il = substance.layers.length; i < il; ++i) {
  50. let { loci, material, clear } = substance.layers[il - i - 1]; // process from end
  51. loci = StructureElement.Loci.subtract(loci, shadowed);
  52. shadowed = StructureElement.Loci.union(loci, shadowed);
  53. if (!StructureElement.Loci.isEmpty(loci)) {
  54. if (clear) {
  55. clearLoci = clearLoci
  56. ? StructureElement.Loci.union(loci, clearLoci)
  57. : loci;
  58. } else {
  59. if (map.has(material)) {
  60. loci = StructureElement.Loci.union(loci, map.get(material)!);
  61. }
  62. map.set(material, loci);
  63. }
  64. }
  65. }
  66. const layers: Substance.Layer[] = [];
  67. if (clearLoci) {
  68. layers.push({ loci: clearLoci, material: Material(), clear: true });
  69. }
  70. map.forEach((loci, material) => {
  71. layers.push({ loci, material, clear: false });
  72. });
  73. return { layers };
  74. }
  75. export function filter(substance: Substance, filter: Structure): Substance {
  76. if (isEmpty(substance)) return substance;
  77. const { structure } = substance.layers[0].loci;
  78. const layers: Substance.Layer[] = [];
  79. for (const layer of substance.layers) {
  80. let { loci, material, clear } = layer;
  81. // filter by first map to the `filter` structure and
  82. // then map back to the original structure of the substance loci
  83. const filtered = StructureElement.Loci.remap(loci, filter);
  84. loci = StructureElement.Loci.remap(filtered, structure);
  85. if (!StructureElement.Loci.isEmpty(loci)) {
  86. layers.push({ loci, material, clear });
  87. }
  88. }
  89. return { layers };
  90. }
  91. export type ScriptLayer = { script: Script, material: Material, clear: boolean }
  92. export function ofScript(scriptLayers: ScriptLayer[], structure: Structure): Substance {
  93. const layers: Substance.Layer[] = [];
  94. for (let i = 0, il = scriptLayers.length; i < il; ++i) {
  95. const { script, material, clear } = scriptLayers[i];
  96. const loci = Script.toLoci(script, structure);
  97. if (!StructureElement.Loci.isEmpty(loci)) {
  98. layers.push({ loci, material, clear });
  99. }
  100. }
  101. return { layers };
  102. }
  103. export type BundleLayer = { bundle: StructureElement.Bundle, material: Material, clear: boolean }
  104. export function ofBundle(bundleLayers: BundleLayer[], structure: Structure): Substance {
  105. const layers: Substance.Layer[] = [];
  106. for (let i = 0, il = bundleLayers.length; i < il; ++i) {
  107. const { bundle, material, clear } = bundleLayers[i];
  108. const loci = StructureElement.Bundle.toLoci(bundle, structure.root);
  109. layers.push({ loci, material, clear });
  110. }
  111. return { layers };
  112. }
  113. export function toBundle(overpaint: Substance) {
  114. const layers: BundleLayer[] = [];
  115. for (let i = 0, il = overpaint.layers.length; i < il; ++i) {
  116. const { loci, material, clear } = overpaint.layers[i];
  117. const bundle = StructureElement.Bundle.fromLoci(loci);
  118. layers.push({ bundle, material, clear });
  119. }
  120. return { layers };
  121. }
  122. }