From 1a04125463d305b46ed07b9298aa62a6c3812566 Mon Sep 17 00:00:00 2001 From: David Sehnal <david.sehnal@gmail.com> Date: Wed, 24 Oct 2018 15:14:21 +0200 Subject: [PATCH] Fixed parsing density server data --- src/mol-math/linear-algebra/tensor.ts | 21 +++++++++++++++++++ .../volume/formats/density-server.ts | 20 ++++++++++++++---- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/mol-math/linear-algebra/tensor.ts b/src/mol-math/linear-algebra/tensor.ts index 0909730c5..f1936a505 100644 --- a/src/mol-math/linear-algebra/tensor.ts +++ b/src/mol-math/linear-algebra/tensor.ts @@ -202,4 +202,25 @@ export namespace Tensor { } return o; } + + // Convers "slow to fast" axis order to "fast to slow" and vice versa. + export function invertAxisOrder(v: number[]) { + const ret: number[] = []; + for (let i = 0; i < v.length; i++) { + ret[i] = v[v.length - i - 1]; + } + return ret; + } + + export function getCanonicalAxisIndicesFastToSlow(order: number[]) { + const indices = new Int32Array(order.length) as any as number[]; + for (let i = 0; i < order.length; i++) indices[order[i]] = i; + return indices; + } + + export function getCanonicalAxisIndicesSlowToFast(order: number[]) { + const indices = new Int32Array(order.length) as any as number[]; + for (let i = 0; i < order.length; i++) indices[order[order.length - i - 1]] = i; + return indices; + } } \ No newline at end of file diff --git a/src/mol-model/volume/formats/density-server.ts b/src/mol-model/volume/formats/density-server.ts index 0b1cb234d..10f3609bc 100644 --- a/src/mol-model/volume/formats/density-server.ts +++ b/src/mol-model/volume/formats/density-server.ts @@ -10,6 +10,10 @@ import { Task } from 'mol-task'; import { SpacegroupCell, Box3D } from 'mol-math/geometry'; import { Tensor, Vec3 } from 'mol-math/linear-algebra'; +function normalizeOrder(v: number[], indices: number[]) { + return Vec3.create(v[indices[0]], v[indices[1]], v[indices[2]]) +} + function parseDensityServerData(source: DensityServer_Data_Database): Task<VolumeData> { return Task.create<VolumeData>('Parse Volume Data', async ctx => { const { volume_data_3d_info: info, volume_data_3d: values } = source; @@ -19,11 +23,19 @@ function parseDensityServerData(source: DensityServer_Data_Database): Task<Volum Vec3.scale(Vec3.zero(), Vec3.ofArray(info.spacegroup_cell_angles.value(0)), Math.PI / 180) ); - const tensorSpace = Tensor.Space(info.sample_count.value(0), info.axis_order.value(0), Float32Array); - const data = Tensor.create(tensorSpace, Tensor.Data1(values.values.toArray())); + const axis_order_fast_to_slow = info.axis_order.value(0); + + const indices = Tensor.getCanonicalAxisIndicesFastToSlow(axis_order_fast_to_slow); + + // sample count is in "axis order" and needs to be reordered + const sample_count = normalizeOrder(info.sample_count.value(0), indices); + const tensorSpace = Tensor.Space(sample_count, Tensor.invertAxisOrder(axis_order_fast_to_slow), Float32Array); + + const data = Tensor.create(tensorSpace, Tensor.Data1(values.values.toArray({ array: Float32Array }))); - const origin = Vec3.ofArray(info.origin.value(0)) - const dimensions = Vec3.ofArray(info.dimensions.value(0)); + // origin and dimensions are in "axis order" and need to be reordered + const origin = normalizeOrder(info.origin.value(0), indices) + const dimensions = normalizeOrder(info.dimensions.value(0), indices); return { cell, -- GitLab