classdef Coating
    %COATING Defines coating of the surface in the system
    %   Detailed explanation goes here
    
    properties
    Name% None = bare glass use Fresnels Formulas, JM = user defined jones matrix , and The name of the coating for the real coatings   
    WavelengthsUsed % vector of wavelengths used in the coating table in accending order
    AnglesUsed % vector of incidence angles used in coating table. In accending order in radians
    CoatingTable % a 3D matrix of trasmission and reflection coefficients of s and p components for each combination of wavelength and angle.
    % dimension1 = [wavLen1, wavLen2, ...], dimension2 = [Angle1, Angle2, ...] and dimension3 = [Ts,Tp,Rs,Rp]

    end
    
    methods        
    function [JTM] = getJonesTransmissionMatrix(Coating,wavLen,Angle)  
    % values greater than the maximum specified values take the maximum value and those below the minimum take the minimum value
    if wavLen < WavelengthsUsed(1)  
         wavLen = WavelengthsUsed(1); 
    elseif wavLen > WavelengthsUsed(len(WavelengthsUsed))  
         wavLen = WavelengthsUsed(len(WavelengthsUsed)); 
    end    
    if Angle < AnglesUsed(1)  
         Angle = AnglesUsed(1); 
    elseif Angle > AnglesUsed(len(AnglesUsed))  
         Angle = AnglesUsed(len(AnglesUsed)); 
    end    
    %get the indices of neighboring nodes for the given wavelengths and angle in the coating table
    y1index=find(AnglesUsed>Angle,1)-1; 
    y2index=find(AnglesUsed>Angle,1);         

    x1index=find(WavelengthsUsed>Angle,1)-1; 
    x2index=find(WavelengthsUsed>Angle,1);


    TsMatrix = Coating.CoatingTable(:,:,1); 
    TpMatrix  = Coating.CoatingTable(:,:,2);

    % coordinates of node values for interpolation
    X = [WavelengthsUsed(x1index),WavelengthsUsed(x2index)]; 
    Y = [cos(AnglesUsed(y1index)),cos(AnglesUsed(y2index))];
    % matrix of  node values 
    VTs = [TsMatrix(x1index,y1index)  TsMatrix(x2index,y1index);TsMatrix(x1index,y2index)  TsMatrix(x2index,y2index)];
    VTp = [TpMatrix(x1index,y1index)  TpMatrix(x2index,y1index);TpMatrix(x1index,y2index)  TpMatrix(x2index,y2index);]

    %the coordinates of query points
    Xq = wavLen; 
    Yq = cos(Angle); 

     %the linear interpolation of Ts and Tp
    Ts = interp(X,Y,VTs,Xq,Yq);
    Tp = interp(X,Y,VTp,Xq,Yq);

    %return the jones transmission matrix
    JTM = [Ts,0;0,Tp];
    end    

 
    function [JRM] = getJonesReflectionMatrix(Coating,wavLen,Angle)  
    % values greater than the maximum specified values take the maximum value and those below the minimum take the minimum value
    if wavLen < WavelengthsUsed(1)  
         wavLen = WavelengthsUsed(1); 
    elseif wavLen > WavelengthsUsed(len(WavelengthsUsed))  
         wavLen = WavelengthsUsed(len(WavelengthsUsed)); 
    end    
    if Angle < AnglesUsed(1)  
         Angle = AnglesUsed(1); 
    elseif Angle > AnglesUsed(len(AnglesUsed))  
         Angle = AnglesUsed(len(AnglesUsed)); 
    end    
    %get the indices of neighboring nodes for the given wavelengths and angle in the coating table
    y1index=find(AnglesUsed>Angle,1)-1; 
    y2index=find(AnglesUsed>Angle,1);         

    x1index=find(WavelengthsUsed>Angle,1)-1; 
    x2index=find(WavelengthsUsed>Angle,1);


    RsMatrix = Coating.CoatingTable(:,:,1); 
    RpMatrix  = Coating.CoatingTable(:,:,2);

    % coordinates of node values for interpolation
    X = [WavelengthsUsed(x1index),WavelengthsUsed(x2index)]; 
    Y = [cos(AnglesUsed(y1index)),cos(AnglesUsed(y2index))];
    % matrix of  node values 
    VRs = [RsMatrix(x1index,y1index)  RsMatrix(x2index,y1index);RsMatrix(x1index,y2index)  RsMatrix(x2index,y2index)];
    VRp = [RpMatrix(x1index,y1index)  RpMatrix(x2index,y1index);RpMatrix(x1index,y2index)  RpMatrix(x2index,y2index);]

    %the coordinates of query points
    Xq = wavLen; 
    Yq = cos(Angle); 

     %the linear interpolation of Ts and Tp
    Ts = interp(X,Y,VRs,Xq,Yq);
    Tp = interp(X,Y,VRp,Xq,Yq);

    %return the jones transmission matrix
    JRM = [Rs,0;0,Rp];
    end






function validData = validateCoatingData() 
%1. If data for an angle of incidence of 0.0 degrees is defined, 
%the S and P data should be identical, since S and P are 
%indistinguishable at normal incidence. 
%2. All wavelengths listed must be identical for each angle. The 
%same ascending order values for the wavelengths is required at 
%each angle for accurate interpolation. An error message will 
%result if the wavelengths are not properly defined.
end







    end
    
end