Bladeren bron

use mean not boundary center for transform in cellpack

Alexander Rose 5 jaren geleden
bovenliggende
commit
c4697a9f45
2 gewijzigde bestanden met toevoegingen van 26 en 7 verwijderingen
  1. 4 6
      src/extensions/cellpack/model.ts
  2. 22 1
      src/extensions/cellpack/util.ts

+ 4 - 6
src/extensions/cellpack/model.ts

@@ -9,7 +9,7 @@ import { PluginContext } from '../../mol-plugin/context';
 import { PluginStateObject as PSO } from '../../mol-plugin-state/objects';
 import { ParamDefinition as PD } from '../../mol-util/param-definition';
 import { Ingredient, IngredientSource, CellPacking } from './data';
-import { getFromPdb, getFromCellPackDB, IngredientFiles, parseCif, parsePDBfile } from './util';
+import { getFromPdb, getFromCellPackDB, IngredientFiles, parseCif, parsePDBfile, getStructureMean } from './util';
 import { Model, Structure, StructureSymmetry, StructureSelection, QueryContext, Unit } from '../../mol-model/structure';
 import { trajectoryFromMmCIF, MmcifFormat } from '../../mol-model-formats/structure/mmcif';
 import { trajectoryFromPDB } from '../../mol-model-formats/structure/pdb';
@@ -308,13 +308,11 @@ async function getIngredientStructure(assetManager: AssetManager, ingredient: In
         // transform with offset and pcp
         let legacy: boolean = true;
         if (ingredient.offset || ingredient.principalAxis){
-            // center the structure
             legacy = false;
-            const boundary = structure.boundary;
-            let structureCenter: Vec3 = Vec3.zero();
-            Vec3.negate(structureCenter, boundary.sphere.center);
+            const structureMean = getStructureMean(structure);
+            Vec3.negate(structureMean, structureMean);
             const m1: Mat4 = Mat4.identity();
-            Mat4.setTranslation(m1, structureCenter);
+            Mat4.setTranslation(m1, structureMean);
             structure = Structure.transform(structure, m1);
             if (ingredient.offset){
                 if (!Vec3.exactEquals(ingredient.offset, Vec3.zero())){

+ 22 - 1
src/extensions/cellpack/util.ts

@@ -7,6 +7,8 @@
 import { CIF } from '../../mol-io/reader/cif';
 import { parsePDB } from '../../mol-io/reader/pdb/parser';
 import { AssetManager, Asset } from '../../mol-util/assets';
+import { Structure } from '../../mol-model/structure';
+import { Vec3 } from '../../mol-math/linear-algebra';
 
 export async function parseCif(data: string|Uint8Array) {
     const comp = CIF.parse(data);
@@ -49,4 +51,23 @@ export async function getFromCellPackDB(id: string, baseUrl: string, assetManage
     }
 }
 
-export type IngredientFiles = { [name: string]: Asset.File }
+export type IngredientFiles = { [name: string]: Asset.File }
+
+//
+
+export function getStructureMean(structure: Structure) {
+    let xSum = 0, ySum = 0, zSum = 0;
+    for (let i = 0, il = structure.units.length; i < il; ++i) {
+        const unit = structure.units[i];
+        const { elements } = unit;
+        const { x, y, z } = unit.conformation;
+        for (let j = 0, jl = elements.length; j < jl; ++j) {
+            const eI = elements[j];
+            xSum += x(eI);
+            ySum += y(eI);
+            zSum += z(eI);
+        }
+    }
+    const { elementCount } = structure;
+    return Vec3.create(xSum / elementCount, ySum / elementCount, zSum / elementCount);
+}