Browse Source

moved loci normalization/granularity to model/loci

Alexander Rose 5 years ago
parent
commit
90255b1a1b
2 changed files with 59 additions and 32 deletions
  1. 52 0
      src/mol-model/loci.ts
  2. 7 32
      src/mol-plugin/util/interactivity.ts

+ 52 - 0
src/mol-model/loci.ts

@@ -12,6 +12,7 @@ import { CentroidHelper } from '../mol-math/geometry/centroid-helper';
 import { Vec3 } from '../mol-math/linear-algebra';
 import { OrderedSet } from '../mol-data/int';
 import { Structure } from './structure/structure';
+import { capitalize } from '../mol-util/string';
 
 /** A Loci that includes every loci */
 export const EveryLoci = { kind: 'every-loci' as 'every-loci' }
@@ -152,4 +153,55 @@ namespace Loci {
         const boundingSphere = getBoundingSphere(loci, tmpSphere3D)
         return boundingSphere ? Vec3.copy(center || Vec3.zero(), boundingSphere.center) : undefined
     }
+
+    //
+
+    const Granularity = {
+        'element': (loci: Loci) => loci,
+        'residue': (loci: Loci) => {
+            return StructureElement.Loci.is(loci)
+                ? StructureElement.Loci.extendToWholeResidues(loci, true)
+                : loci
+        },
+        'chain': (loci: Loci) => {
+            return StructureElement.Loci.is(loci)
+                ? StructureElement.Loci.extendToWholeChains(loci)
+                : loci
+        },
+        'structure': (loci: Loci) => {
+            return StructureElement.Loci.is(loci)
+                ? Structure.toStructureElementLoci(loci.structure)
+                : loci
+        }
+    }
+    export type Granularity = keyof typeof Granularity
+    export const GranularityOptions = Object.keys(Granularity).map(n => [n, capitalize(n)]) as [Granularity, string][]
+
+    export function applyGranularity(loci: Loci, granularity: Granularity) {
+        return Granularity[granularity](loci)
+    }
+
+    /**
+     * Converts structure related loci to StructureElement.Loci and applies
+     * granularity if given
+    */
+    export function normalize(loci: Loci, granularity?: Granularity) {
+        if (granularity !== 'element' && Link.isLoci(loci)) {
+            // convert Link.Loci to a StructureElement.Loci so granularity can be applied
+            loci = Link.toStructureElementLoci(loci)
+        }
+        if (Structure.isLoci(loci)) {
+            // convert to StructureElement.Loci
+            loci = Structure.toStructureElementLoci(loci.structure)
+        }
+        if (StructureElement.Loci.is(loci)) {
+            // ensure the root structure is used
+            loci = StructureElement.Loci.remap(loci, loci.structure.root)
+        }
+        if (granularity) {
+            // needs to be applied AFTER remapping to root
+            loci = applyGranularity(loci, granularity)
+        }
+        return loci
+    }
 }

+ 7 - 32
src/mol-plugin/util/interactivity.ts

@@ -8,14 +8,13 @@
 import { Loci as ModelLoci, EmptyLoci, EveryLoci, isEmptyLoci } from '../../mol-model/loci';
 import { ModifiersKeys, ButtonsType } from '../../mol-util/input/input-observer';
 import { Representation } from '../../mol-repr/representation';
-import { StructureElement, Link } from '../../mol-model/structure';
+import { StructureElement } from '../../mol-model/structure';
 import { MarkerAction } from '../../mol-util/marker-action';
 import { StructureElementSelectionManager } from './structure-element-selection';
 import { PluginContext } from '../context';
-import { StructureElement as SE, Structure } from '../../mol-model/structure';
+import { Structure } from '../../mol-model/structure';
 import { ParamDefinition as PD } from '../../mol-util/param-definition';
 import { PluginCommands } from '../command';
-import { capitalize } from '../../mol-util/string';
 
 export { Interactivity }
 
@@ -52,17 +51,8 @@ namespace Interactivity {
         export const Empty: Loci = { loci: EmptyLoci };
     }
 
-    const Granularity = {
-        'element': (loci: ModelLoci) => loci,
-        'residue': (loci: ModelLoci) => SE.Loci.is(loci) ? SE.Loci.extendToWholeResidues(loci, true) : loci,
-        'chain': (loci: ModelLoci) => SE.Loci.is(loci) ? SE.Loci.extendToWholeChains(loci) : loci,
-        'structure': (loci: ModelLoci) => SE.Loci.is(loci) ? Structure.toStructureElementLoci(loci.structure) : loci
-    }
-    type Granularity = keyof typeof Granularity
-    const GranularityOptions = Object.keys(Granularity).map(n => [n, capitalize(n)]) as [Granularity, string][]
-
     export const Params = {
-        granularity: PD.Select('residue', GranularityOptions, { description: 'Controls if selections are expanded to whole residues, chains, structures, or left as atoms and coarse elements' }),
+        granularity: PD.Select('residue', ModelLoci.GranularityOptions, { description: 'Controls if selections are expanded to whole residues, chains, structures, or left as atoms and coarse elements' }),
     }
     export type Params = typeof Params
     export type Props = PD.Values<Params>
@@ -91,25 +81,10 @@ namespace Interactivity {
             // TODO clear, then re-apply remaining providers
         }
 
-        normalizedLoci(interactivityLoci: Loci, applyGranularity = true) {
-            let { loci, repr } = interactivityLoci
-            if (this.props.granularity !== 'element' && Link.isLoci(loci)) {
-                // convert Link.Loci to a StructureElement.Loci so granularity can be applied
-                loci = Link.toStructureElementLoci(loci)
-            }
-            if (Structure.isLoci(loci)) {
-                // convert to StructureElement.Loci
-                loci = Structure.toStructureElementLoci(loci.structure)
-            }
-            if (StructureElement.Loci.is(loci)) {
-                // ensure the root structure is used
-                loci = StructureElement.Loci.remap(loci, loci.structure.root)
-            }
-            if (applyGranularity) {
-                // needs to be applied AFTER remapping to root
-                loci = Granularity[this.props.granularity](loci)
-            }
-            return { loci, repr }
+        protected normalizedLoci(interactivityLoci: Loci, applyGranularity = true) {
+            const { loci, repr } = interactivityLoci
+            const granularity =  applyGranularity ? this.props.granularity : undefined
+            return { loci: ModelLoci.normalize(loci, granularity), repr }
         }
 
         protected mark(current: Loci<ModelLoci>, action: MarkerAction) {