Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inquiry Regarding why swap x-index and y-index when converting cube indices to world coordinates #807

Open
yangshiyan opened this issue Dec 31, 2024 · 1 comment
Labels

Comments

@yangshiyan
Copy link

Dear matRad experts:
I am currently working on converting PTV cube indices to word coordinates, and came across the relevant function:
function coord = matRad_cubeIndex2worldCoords(cubeIx, gridStruct)
specially, I noticed the following lines of the code:
lines 37-39:
if size(cubeIx,2) == 1
[cubeIx(:,1),cubeIx(:,2),cubeIx(:,3)] = ind2sub(gridStruct.dimensions,cubeIx);
end
and line 48:
coord = cubeIx(:,[2 1 3]) .* [gridStruct.resolution.x gridStruct.resolution.y gridStruct.resolution.z];

I would like to understand the reasoning behind using cubeIx(:,[2 1 3]) in line 48 instead of cubeIx(:,[1 2 3]). It seems that the x- and y-indices are swapped, which leads me to think that:
• coordX = yindx * resolution.x
• coordY = xindx * resolution.y

To clarify my understanding: Suppose I have a CT image with dimensions 512 x 512 x 200, and I am working with a voxel index of 5000. Using the ind2sub function, I obtain the following indices:
[xind, yind, zind] = ind2sub([512 512 200], 5000);
which yields:
• xind = 392
• yind = 10
• zind = 1
I believe the original index can be restored using the formula:
index = (zind - 1) * 512 * 512 + (yind - 1) * 512 + xind;
Then, the world coordinates would be calculated as:
• coordX = xind * resolution.x
• coordY = yind * resolution.y
Could you kindly explain why the x- and y-indices are swapped in line 48? I appreciate your insights into this matter.
Thank you very much for your time and assistance. I look forward to your response.

@wahln
Copy link
Contributor

wahln commented Jan 3, 2025

This is related / similar to the behavior of ndgrid and meshgrid in Matlab: https://stackoverflow.com/questions/19563272/practical-differences-between-meshgrid-and-ndgrid

In short: Matlab's storage order of array data blocks is interpreted as the first two indices as swapped in many Matlab functions (due on of its big tasks being imaging problems) comapred to the left handed LPS grid we use as coordinate system. So, when we handle our lps manually our x-y-z coordinates we need to swap the indices. you can think of it as x / y / z coordinates representing j / i / k indices.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants