Skip to content

Commit 343e7cc

Browse files
Dev rc/v3 bug fix (e0404#785)
* Minor bug, update cube dimension in writeMHD to make dimension permeutation effective * Minor bug fix, misisng matRad_cfg * Modify use of cube permutation and transformation matrix * Add permutation to vtk writer --------- Co-authored-by: Niklas Wahl <[email protected]>
1 parent b249e6e commit 343e7cc

File tree

5 files changed

+61
-25
lines changed

5 files changed

+61
-25
lines changed

matRad/IO/matRad_readMHD.m

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,15 @@
6868
T = zeros(3);
6969
T(:) = cell2mat(tmp);
7070

71+
if ~isequal(T, diag(ones(1,numel(dimensions))))
72+
matRad_cfg.dispWarning('Non identity transformation matrix detected in the loaded cube. This might lead to reconstruction inconsistency.')
73+
end
74+
7175
% Apply Matlab permutation
76+
% This ensures that the cube is reverted back to the matLab standard
77+
% indexing
7278
Tmatlab = [0 1 0; 1 0 0; 0 0 1];
73-
%T = T * [0 1 0; 1 0 0; 0 0 1];
79+
T = T*Tmatlab;
7480

7581
% get data type
7682
idx = find(~cellfun(@isempty,strfind(s{1}, 'ElementType')),1,'first');
@@ -84,20 +90,18 @@
8490
fseek(headerFileHandle,-S.bytes*prod(dimensions),'eof');
8591
cube = fread(headerFileHandle,prod(dimensions),type,endian);
8692
cube = reshape(cube,dimensions);
87-
cube = permute(cube,[2 1 3]);
88-
%matRad_cfg.dispError('MHA not implemented!');
93+
cube = permute(cube,abs([1 2 3]*T));
8994
else
9095
%% read data
9196
[filepath,~,~] = fileparts(filename);
9297
dataFileHandle = fopen(fullfile(filepath,dataFilename),'r');
9398
cube = reshape(fread(dataFileHandle,inf,type,endian),dimensions);
94-
cube = permute(cube,[2 1 3]);
95-
%cube = flip(cube,1);
99+
cube = permute(cube,abs([1 2 3]*T));
96100
fclose(dataFileHandle);
97101
end
98102
fclose(headerFileHandle);
99103
metadata.resolution = resolution;
100-
metadata.cubeDim = dimensions * Tmatlab;
104+
metadata.cubeDim = dimensions * T;
101105

102106
end
103107

matRad/IO/matRad_writeMHA.m

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,28 @@ function matRad_writeMHA(filepath,cube,metadata)
4848
%cleaner = onCleanup(@() fclose(fid));
4949

5050
%We perform the permutation
51-
if isfield(metadata,'axisPermutation')
52-
cube = permute(cube,metadata.axisPermutation);
51+
if ~isfield(metadata, 'axisPermutation')
52+
% This reverts the matRlab conventianl indexing
53+
axisPermutation = [2,1,3];
54+
elseif ~isequal(metadata.axisPermutation, [2,1,3])
55+
matRad_cfg.dispWarning('Unconventianal permutation of patient indexing, this might cause inconsistency');
56+
axisPermutation = metadata.axisPermutation;
5357
end
5458

55-
%Set up Transform Matrix
56-
T=zeros(4);
57-
ixOnes = sub2ind([4 4],metadata.axisPermutation,[1 2 3]);
58-
T(ixOnes) = 1;
59-
T(4,4) = 1;
59+
% Force the permutation here according to the axis permutation
60+
cube = permute(cube, axisPermutation);
61+
62+
% Need to permute the dimensions as well
63+
dimensions = size(cube);
64+
65+
% Note in the cube permutation:
66+
% Permutation of the cube is enforced here to standdard indexing so that
67+
% reconstruction of the cube for further use does not rely on the use of
68+
% a transformation matrix, which is now only the identity matrix.
69+
70+
%The transformation matrix is now the unit matrix
71+
transformMatrix = diag(ones(1,numel(dimensions)));
72+
tmString = sprintf(' %d',transformMatrix(:));
6073

6174
%Correct for coordinate system
6275
switch metadata.coordinateSystem
@@ -68,12 +81,6 @@ function matRad_writeMHA(filepath,cube,metadata)
6881
matRad_cfg.dispError('Only LPS currently supported for export!');
6982
end
7083

71-
%Now add Translation
72-
%The transformation matrix is now the unit matrix
73-
%transformMatrix = diag(ones(1,numel(dimensions)));
74-
%tmString = sprintf(' %d',transformMatrix(:));
75-
76-
tmString = sprintf(' %d',T(1:3,1:3));
7784

7885
%Determine the endian
7986
[~,~,endian] = computer;

matRad/IO/matRad_writeMHD.m

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,25 @@ function matRad_writeMHD(filepath,cube,metadata)
4747
end
4848

4949
%We perform the permutation
50-
if isfield(metadata,'axisPermutation')
51-
cube = permute(cube,metadata.axisPermutation);
50+
if ~isfield(metadata, 'axisPermutation')
51+
% This reverts the matRlab conventianl indexing
52+
axisPermutation = [2,1,3];
53+
elseif ~isequal(metadata.axisPermutation, [2,1,3])
54+
matRad_cfg.dispWarning('Unconventianal permutation of patient indexing, this might cause inconsistency');
55+
axisPermutation = metadata.axisPermutation;
5256
end
57+
58+
% Force the permutation here according to the axis permutation
59+
cube = permute(cube, axisPermutation);
60+
61+
% Need to permute the dimensions as well
62+
dimensions = size(cube);
63+
64+
% Note in the cube permutation:
65+
% Permutation of the cube is enforced here to standdard indexing so that
66+
% reconstruction of the cube for further use does not rely on the use of
67+
% a transformation matrix, which is now only the identity matrix.
68+
5369
%The transformation matrix is now the unit matrix
5470
transformMatrix = diag(ones(1,numel(dimensions)));
5571
tmString = sprintf(' %d',transformMatrix(:));

matRad/IO/matRad_writeVTK.m

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,20 @@ function matRad_writeVTK(filepath,cube,metadata)
4444
end
4545
cleaner = onCleanup(@() fclose(fid));
4646

47-
%We perform
48-
if isfield(metadata,'axisPermutation')
49-
cube = permute(cube,metadata.axisPermutation);
47+
%We perform the permutation
48+
if ~isfield(metadata, 'axisPermutation')
49+
% This reverts the matRlab conventianl indexing
50+
axisPermutation = [2,1,3];
51+
elseif ~isequal(metadata.axisPermutation, [2,1,3])
52+
matRad_cfg.dispWarning('Unconventianal permutation of patient indexing, this might cause inconsistency');
53+
axisPermutation = metadata.axisPermutation;
5054
end
5155

56+
% Force the permutation here according to the axis permutation
57+
cube = permute(cube, axisPermutation);
58+
59+
% Need to permute the dimensions as well
60+
dimensions = size(cube);
5261

5362
fprintf(fid, '# vtk DataFile Version 3.0\n');
5463
fprintf(fid, 'vtk output\n');

matRad/steering/matRad_StfGeneratorBase.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ function assignPropertiesFromPln(this,pln,warnWhenPropertyChanged)
227227
methods (Access = protected)
228228
function initialize(this)
229229
%Do nothing
230-
230+
matRad_cfg = MatRad_Config.instance();
231231
% get machine
232232
if ~isstruct(this.machine)
233233
try

0 commit comments

Comments
 (0)