Change memory layout of images

I’m working now with 3D vtkImageData with scalar set. I fill vtkFloatArray from pointer of Eigen::Matrix:

    vtkNew<vtkFloatArray> array;
    array->SetArray(TRACE->data(), TRACE->size(), 1);

The problem is that vtkImageData expects that the scalar array is filled in the order: X-axis then Y-axis and only then Z-axis.
My eigen matrix is filled differently: at first Z-axis then Y-axis and then X-axis (or maybe after Z goes X and only then Y).

As I work with quite big data (few gigabytes or at minimum few hundreds of megabytes) I wanted to avoid copying data and somehow tell to my mrmlnode (or vtkimagedata) that the scalar data has some non-standard memory order.

Rotations will not help me there…
Maybe you have some ideas?

VTK core supports arbitrary memory layouts but I think only a fraction of filters support it, so you can only use special memory layouts for very specific operations.

This C vs Fortran memory order is typically just an annoyance, something that you need to keep in mind when you cross boundaries between libraries that use different conventions. For example, this issue comes up very often with ITK/VTK vs numpy, but I don’t recall any situation that required somebody to reallocate arrays.

If you just want to display the volume then you can apply a transform that reorders coordinate system axes. For example, the transformation matrix to convert from KJI to IJK is:

0,0,1,0
0,1,0,0
1,0,0,0
0,0,0,1

By the way, resampling of a volume with reordered axes is usually a very quick operation, so it should not be perceivable for users, unless you need to keep both representations in memory and synchronize them very frequently.

1 Like