ソースを参照

Add StructureElement.Loci.forEachLocation

dsehnal 2 年 前
コミット
2e013fafc8
2 ファイル変更21 行追加0 行削除
  1. 1 0
      CHANGELOG.md
  2. 20 0
      src/mol-model/structure/structure/element/loci.ts

+ 1 - 0
CHANGELOG.md

@@ -17,6 +17,7 @@ Note that since we don't clearly distinguish between a public and private interf
     - Update clip `defines` only when changed
     - Check for identity in structure/unit areEqual methods
     - Avoid cloning of structure representation parameters
+- Add StructureElement.Loci.forEachLocation
 
 ## [v3.28.0] - 2022-12-20
 

+ 20 - 0
src/mol-model/structure/structure/element/loci.ts

@@ -143,6 +143,26 @@ export namespace Loci {
         return Structure.create(units, { parent: loci.structure.parent });
     }
 
+    /**
+     * Iterates over all locations.
+     * The loc argument of the callback is mutable, use Location.clone() if you intend to keep
+     * the value around.
+     */
+    export function forEachLocation(loci: Loci, f: (loc: Location) => any) {
+        if (Loci.isEmpty(loci)) return;
+
+        const location = Location.create(loci.structure);
+        for (const e of loci.elements) {
+            const { unit, indices } = e;
+            location.unit = unit;
+            const { elements } = e.unit;
+            for (let i = 0, _i = OrderedSet.size(indices); i < _i; i++) {
+                location.element = elements[OrderedSet.getAt(indices, i)];
+                f(location);
+            }
+        }
+    }
+
     // TODO: there should be a version that properly supports partitioned units
     export function remap(loci: Loci, structure: Structure): Loci {
         if (structure === loci.structure) return loci;