0001 function stf = matRad_computeSSD(stf,ct,mode)
0002
0003
0004
0005
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 matRad_cfg = MatRad_Config.instance();
0034
0035 if nargin < 3
0036 mode = 'first';
0037 end
0038
0039
0040 boolShowWarning = true;
0041
0042
0043 densityThreshold = matRad_cfg.propDoseCalc.defaultSsdDensityThreshold;
0044
0045 if strcmp(mode,'first')
0046
0047 for i = 1:size(stf,2)
0048 SSD = cell(1,stf(i).numOfRays);
0049 for j = 1:stf(i).numOfRays
0050 [alpha,~,rho,~,~] = matRad_siddonRayTracer(stf(i).isoCenter, ...
0051 ct.resolution, ...
0052 stf(i).sourcePoint, ...
0053 stf(i).ray(j).targetPoint, ...
0054 {ct.cube{1}});
0055 ixSSD = find(rho{1} > densityThreshold,1,'first');
0056
0057 if boolShowWarning
0058 if isempty(ixSSD)
0059 matRad_cfg.dispWarning('ray does not hit patient. Trying to fix afterwards...');
0060 boolShowWarning = false;
0061 elseif ixSSD(1) == 1
0062 matRad_cfg.dispWarning('Surface for SSD calculation starts directly in first voxel of CT!');
0063 boolShowWarning = false;
0064 end
0065 end
0066
0067
0068 SSD{j} = double(2 * stf(i).SAD * alpha(ixSSD));
0069 stf(i).ray(j).SSD = SSD{j};
0070 end
0071
0072
0073 SSDnotSet = find(cellfun('isempty',SSD));
0074 if ~isempty(SSDnotSet)
0075 rayPos_bev = reshape([stf(i).ray(:).rayPos_bev]',[3 stf(i).numOfRays])';
0076 for j = SSDnotSet
0077 stf(i).ray(j).SSD = matRad_closestNeighbourSSD(rayPos_bev, SSD, rayPos_bev(j,:));
0078 end
0079 end
0080 end
0081 else
0082 matRad_cfg.dispError('mode not defined for SSD calculation');
0083 end
0084
0085 end
0086
0087
0088 function bestSSD = matRad_closestNeighbourSSD(rayPos, SSD, currPos)
0089 vDistances = sum((rayPos - repmat(currPos,size(rayPos,1),1)).^2,2);
0090 [~, vIdx] = sort(vDistances);
0091 for ix = vIdx'
0092 bestSSD = SSD{ix};
0093
0094 if ~any(isempty(bestSSD))
0095 break
0096 end
0097 end
0098 if any(isempty(bestSSD))
0099 matRad_cfg = MatRad_Config.instance();
0100 matRad_cfg.dispError('Could not fix SSD calculation.');
0101 end
0102 end
0103