%
% SetCovPDF - Al Holder, 1/26/05
% 	This function assigns to each angle a value (probability)
%	as suggested by Matthias Ehrgott.  The information is weakly
%	informed.
%

function [pdf] = SetCovPDF(Prob, SCtype, BalancedPDF);

%
% Set Cover Tolerance - Only for SCORES - independent of Target cover tol
%

CoverTolerance = 10^(-1);

%
% Get Tumor Matrix and convert to 0-1 (1 if entry >0)
%

CriticalDoseMatrix = Prob.DoseMatrix(Prob.CriticalRowIndex, :);
NormalDoseMatrix = Prob.DoseMatrix(Prob.RegularRowIndex, :);

SC_DoseMatrix = zeros(size(CriticalDoseMatrix));
SR_DoseMatrix = zeros(size(NormalDoseMatrix));

SC_DoseMatrix(find(CriticalDoseMatrix >= CoverTolerance)) = 1;
SR_DoseMatrix(find(NormalDoseMatrix >= CoverTolerance)) = 1;

%
% Depending on the technique, scale the scoring 
% method on something
%

if strcmp(SCtype , 'ScaledWeak')
%
% Scale by Dose
%
   SC_DoseMatrix = SC_DoseMatrix .* CriticalDoseMatrix;
elseif strcmp(SCtype, 'Scoring')
%
% Make the Weak Scoring PDF
%
   SC_DoseMatrix = SC_DoseMatrix .* CriticalDoseMatrix;
   SC_DoseMatrix = diag(Prob.CUB) * ...
      (1 ./ max(SC_DoseMatrix, 10^(-10)*ones(size(SC_DoseMatrix)) ) );
   SR_DoseMatrix = SR_DoseMatrix .* NormalDoseMatrix;
   SR_DoseMatrix = diag(Prob.RUB) * ...
      (1 ./ max(SR_DoseMatrix, 10^(-10)*ones(size(SR_DoseMatrix)) ) );
   MinRatio = min([SC_DoseMatrix ; SR_DoseMatrix]);
   TMRdose = Prob.DoseMatrix(Prob.TumorRowIndex, :);
   pdf = zeros(1,Prob.NumAngles);
   ColInd = 1;
   for a = 1:Prob.NumAngles
      vect = TMRdose(:, ColInd : ColInd + Prob.NumPencils(a) - 1) * ...
             MinRatio(ColInd : ColInd + Prob.NumPencils(a) - 1)';
      vect = (1 / max(Prob.TUB)) * vect;
      pdf(a) = sum(vect)^2 / length(Prob.TumorRowIndex);
      ColInd = ColInd + Prob.NumPencils(a);
   end   
   return
elseif strcmp(SCtype, 'Informed')
%
% Form the informed pdf as suggested by Matthias and his
% former student.  I don't think this is meaningful.
%
% Start by making sure that this works
if isempty(BalancedPDF)
   LogMesg('RAD -> No BalancedPDF to make Informed SetCoverPDF3')
   return
else
   pdf = zeros(1, length(Prob.NumAngles));
   BalancedPDF = max(BalancedPDF) - BalancedPDF;
   ColInd = 1;
   for a = 1:Prob.NumAngles
      % Find the number of sub-beams that strike a critical structure
      NumMeaningfulSubBeams = ...
         length(find(SC_DoseMatrix(:, ColInd : ColInd + Prob.NumPencils(a) - 1) == 1));
      % Find the numeber of critical dose points struck
      NumMeaningfulDosePoints = ...
         length(find(sum(SC_DoseMatrix(:, ColInd : ColInd + Prob.NumPencils(a) - 1)') >= 1));
      % Blend everything in a funny manner
      pdf(a) = BalancedPDF(a) * log(1 + NumMeaningfulSubBeams + ...
                  NumMeaningfulDosePoints);
      % Invert to make a meaningful pdf
      pdf = max(pdf) - pdf;
      ColInd = ColInd + Prob.NumPencils(a);
      % return so make sure we don't do anything else
   end
   return
end

elseif ~strcmp(SCtype, 'UnScaledWeak')
   LogMesg('RAD -> Unknown SCtype in SetCovPDF.m')
end

%
% Grab the weakly informed pdf
%

pdf = zeros(1, length(Prob.NumAngles));
ColInd = 1;
for a = 1:Prob.NumAngles
   pdf(a) = sum( sum(SC_DoseMatrix(:, ColInd : ColInd + Prob.NumPencils(a) - 1)')' ./ Prob.CUB);
   ColInd = ColInd + Prob.NumPencils(a);
end

%
% Need to Invert this to make sense probabalistically
%
pdf = max(pdf) - pdf;
