function [alphas,l,rho,d12,ix] = matRad_siddonRayTracer(isocenter,resolution,sourcePoint,targetPoint,cubes)
0001 function [alphas,l,rho,d12,ix] = matRad_siddonRayTracer(isocenter, ...
0002 resolution, ...
0003 sourcePoint, ...
0004 targetPoint, ...
0005 cubes)
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 sourcePoint = sourcePoint + isocenter;
0054 targetPoint = targetPoint + isocenter;
0055
0056
0057 [yNumPlanes, xNumPlanes, zNumPlanes] = size(cubes{1});
0058 xNumPlanes = xNumPlanes + 1;
0059 yNumPlanes = yNumPlanes + 1;
0060 zNumPlanes = zNumPlanes + 1;
0061
0062
0063
0064 d12 = norm(sourcePoint-targetPoint);
0065
0066
0067
0068
0069 xPlane_1 = .5*resolution.x;
0070 yPlane_1 = .5*resolution.y;
0071 zPlane_1 = .5*resolution.z;
0072
0073
0074 xPlane_end = (xNumPlanes - .5)*resolution.x;
0075 yPlane_end = (yNumPlanes - .5)*resolution.y;
0076 zPlane_end = (zNumPlanes - .5)*resolution.z;
0077
0078
0079
0080
0081 if targetPoint(1) ~= sourcePoint(1)
0082 aX_1 = (xPlane_1 - sourcePoint(1)) / (targetPoint(1) - sourcePoint(1));
0083 aX_end = (xPlane_end - sourcePoint(1)) / (targetPoint(1) - sourcePoint(1));
0084 else
0085 aX_1 = [];
0086 aX_end = [];
0087 end
0088 if targetPoint(2) ~= sourcePoint(2)
0089 aY_1 = (yPlane_1 - sourcePoint(2)) / (targetPoint(2) - sourcePoint(2));
0090 aY_end = (yPlane_end - sourcePoint(2)) / (targetPoint(2) - sourcePoint(2));
0091 else
0092 aY_1 = [];
0093 aY_end = [];
0094 end
0095 if targetPoint(3) ~= sourcePoint(3)
0096 aZ_1 = (zPlane_1 - sourcePoint(3)) / (targetPoint(3) - sourcePoint(3));
0097 aZ_end = (zPlane_end - sourcePoint(3)) / (targetPoint(3) - sourcePoint(3));
0098 else
0099 aZ_1 = [];
0100 aZ_end = [];
0101 end
0102
0103
0104
0105
0106 alpha_min = max([0 min(aX_1,aX_end) min(aY_1,aY_end) min(aZ_1,aZ_end)]);
0107 alpha_max = min([1 max(aX_1,aX_end) max(aY_1,aY_end) max(aZ_1,aZ_end)]);
0108
0109
0110
0111
0112 if targetPoint(1) == sourcePoint(1)
0113 i_min = []; i_max = [];
0114 elseif targetPoint(1) > sourcePoint(1)
0115 i_min = xNumPlanes - (xPlane_end - alpha_min * (targetPoint(1) - sourcePoint(1)) - sourcePoint(1))/resolution.x;
0116 i_max = 1 + (sourcePoint(1) + alpha_max * (targetPoint(1) - sourcePoint(1)) - xPlane_1)/resolution.x;
0117
0118 i_min = ceil(1/1000*(round(1000*i_min)));
0119 i_max = floor(1/1000*(round(1000*i_max)));
0120 else
0121 i_min = xNumPlanes - (xPlane_end - alpha_max * (targetPoint(1) - sourcePoint(1)) - sourcePoint(1))/resolution.x;
0122 i_max = 1 + (sourcePoint(1) + alpha_min * (targetPoint(1) - sourcePoint(1)) - xPlane_1)/resolution.x;
0123 i_min = ceil(1/1000*(round(1000*i_min)));
0124 i_max = floor(1/1000*(round(1000*i_max)));
0125 end
0126 if targetPoint(2) == sourcePoint(2)
0127 j_min = []; j_max = [];
0128 elseif targetPoint(2) > sourcePoint(2)
0129 j_min = yNumPlanes - (yPlane_end - alpha_min * (targetPoint(2) - sourcePoint(2)) - sourcePoint(2))/resolution.y;
0130 j_max = 1 + (sourcePoint(2) + alpha_max * (targetPoint(2) - sourcePoint(2)) - yPlane_1)/resolution.y;
0131 j_min = ceil(1/1000*(round(1000*j_min)));
0132 j_max = floor(1/1000*(round(1000*j_max)));
0133 else
0134 j_min = yNumPlanes - (yPlane_end - alpha_max * (targetPoint(2) - sourcePoint(2)) - sourcePoint(2))/resolution.y;
0135 j_max = 1 + (sourcePoint(2) + alpha_min * (targetPoint(2) - sourcePoint(2)) - yPlane_1)/resolution.y;
0136 j_min = ceil(1/1000*(round(1000*j_min)));
0137 j_max = floor(1/1000*(round(1000*j_max)));
0138 end
0139 if targetPoint(3) == sourcePoint(3)
0140 k_min = []; k_max = [];
0141 elseif targetPoint(3) >= sourcePoint(3)
0142 k_min = zNumPlanes - (zPlane_end - alpha_min * (targetPoint(3) - sourcePoint(3)) - sourcePoint(3))/resolution.z;
0143 k_max = 1 + (sourcePoint(3) + alpha_max * (targetPoint(3) - sourcePoint(3)) - zPlane_1)/resolution.z;
0144 k_min = ceil(1/1000*(round(1000*k_min)));
0145 k_max = floor(1/1000*(round(1000*k_max)));
0146 else
0147 k_min = zNumPlanes - (zPlane_end - alpha_max * (targetPoint(3) - sourcePoint(3)) - sourcePoint(3))/resolution.z;
0148 k_max = 1 + (sourcePoint(3) + alpha_min * (targetPoint(3) - sourcePoint(3)) - zPlane_1)/resolution.z;
0149 k_min = ceil(1/1000*(round(1000*k_min)));
0150 k_max = floor(1/1000*(round(1000*k_max)));
0151 end
0152
0153
0154
0155
0156 if i_min ~= i_max
0157 if targetPoint(1) > sourcePoint(1)
0158 alpha_x = (resolution.x*(i_min:1:i_max)-sourcePoint(1)-.5*resolution.x)/(targetPoint(1)-sourcePoint(1));
0159 else
0160 alpha_x = (resolution.x*(i_max:-1:i_min)-sourcePoint(1)-.5*resolution.x)/(targetPoint(1)-sourcePoint(1));
0161 end
0162 else
0163 alpha_x = [];
0164 end
0165 if j_min ~= j_max
0166 if targetPoint(2) > sourcePoint(2)
0167 alpha_y = (resolution.y*(j_min:1:j_max)-sourcePoint(2)-.5*resolution.y)/(targetPoint(2)-sourcePoint(2));
0168 else
0169 alpha_y = (resolution.y*(j_max:-1:j_min)-sourcePoint(2)-.5*resolution.y)/(targetPoint(2)-sourcePoint(2));
0170 end
0171 else
0172 alpha_y = [];
0173 end
0174 if k_min ~= k_max
0175 if targetPoint(3) > sourcePoint(3)
0176 alpha_z = (resolution.z*(k_min:1:k_max)-sourcePoint(3)-.5*resolution.z)/(targetPoint(3)-sourcePoint(3));
0177 else
0178 alpha_z = (resolution.z*(k_max:-1:k_min)-sourcePoint(3)-.5*resolution.z)/(targetPoint(3)-sourcePoint(3));
0179 end
0180 else
0181 alpha_z = [];
0182 end
0183
0184
0185
0186 alphas = unique([alpha_min alpha_x alpha_y alpha_z alpha_max]);
0187
0188
0189
0190 l = d12*diff(alphas);
0191
0192
0193
0194 alphas_mid = .5*(alphas(1:end-1)+alphas(2:end));
0195
0196
0197
0198 i_mm = sourcePoint(1) + alphas_mid*(targetPoint(1) - sourcePoint(1));
0199 j_mm = sourcePoint(2) + alphas_mid*(targetPoint(2) - sourcePoint(2));
0200 k_mm = sourcePoint(3) + alphas_mid*(targetPoint(3) - sourcePoint(3));
0201
0202 i = round(i_mm/resolution.x);
0203 j = round(j_mm/resolution.y);
0204 k = round(k_mm/resolution.z);
0205
0206
0207 i(i<=0) = 1; j(j<=0) = 1; k(k<=0) = 1;
0208 i(i>xNumPlanes-1) = xNumPlanes-1;
0209 j(j>yNumPlanes-1) = yNumPlanes-1;
0210 k(k>zNumPlanes-1) = zNumPlanes-1;
0211
0212
0213 ix = j + (i-1)*size(cubes{1},1) + (k-1)*size(cubes{1},1)*size(cubes{1},2);
0214
0215
0216 rho = cell(numel(cubes),1);
0217 for i = 1:numel(cubes)
0218 rho{i} = cubes{i}(ix);
0219 end