function cal_thickness_22808_r1

% Runs with Shim Thickness GUI.
% Created by Mark Smith
% 11/26/07
% Function calculates the shim thickness based on the GUI selected data.

% Revised 1/16/08: Inserted code to determine shim thickness for a
% temporary shim, which will be inserted during assembly in the nose region
% to stop this region from deflecting.

%{
 Revised 2/28/08: Shim labels needed to match drawing labels, i.e. E1 for 1 etc.
Also, several shims are labeled with decimal extensions: i.e. E3, E3.1, E3.2.
%}

if evalin('base','shim_pts') == 0
    msgbox('Shim Points Not Loaded');
    return
elseif evalin('base','rdata1') == 0
    msgbox('First Flange Data Not Loaded');
    return
elseif evalin('base','rdata2') == 0
     msgbox('Second Flange Data Not Loaded');
    return 
else
    shim_pts = evalin('base','shim_pts');
    data1 = evalin('base','rdata1');
    data2 = evalin('base','rdata2');
    joint_type = evalin('base','joint_type');
    switch joint_type
 % ***********************************************************************        
%  
case 'AA'
% 
% *********************************************************************** 
        crnpts=zeros(size(shim_pts));
        crnpts = shim_pts;
        % Adjust for spherical 1.5 inch diameter CCR on 1st A flange.
        d1=zeros(size(data1));
        d1=data1;
        d1(:,3)= d1(:,3)-0.75;  
        % Adjust for spherical 1.5 inch diameter CCR on 2nd A flange.
        d2=zeros(size(data2));
        d2=data2;
        d2(:,3)=d2(:,3)-0.75;    
        % Flip and Rotate A2 coil to place "on top" of A1.
        d1(:,1) =  d1(:,1);
        d1(:,2) = -d1(:,2);
        d1(:,3) = -d1(:,3);
        % Insert 4th column of ones for algorithm.
        d1(:,4)=1;
        d2(:,4)=1;
           
% ***********************************************************************        
%
case 'AB'
%  AB joint: all data needs rotation about the y-axis.
% ***********************************************************************
        phi =-20;            % rotation angle in degrees.
        phi = phi*pi/180;   % convert to radians.
        % Matrix for rotation about y-axis.
        Ry=[cos(phi) 0 sin(phi) 0;0 1 0 0;-sin(phi) 0 cos(phi) 0;0 0 0 1];
        % Need 4th column of ones for rotation operation.
        shim_pts(:,4)=1;
        data1(:,4)=1;
        data2(:,4)=1;
        % Rotate shim points.
        crnpts=zeros(size(shim_pts));
            for rotc =1:length(shim_pts)
                crnpts(rotc,:) = (Ry*shim_pts(rotc,:)')';       
            end
        % Rotate A flange data.
        d1=zeros(size(data1));
            for rotc =1:length(data1)
                d1(rotc,:) = (Ry*data1(rotc,:)')';       
            end
            % Rotate B flange data.
        d2=zeros(size(data2));
            for rotc =1:length(data2)
                d2(rotc,:)  = (Ry*data2(rotc,:)')';       
            end
        % Adjust for spherical 1.5 inch diameter CCR on A flange.
        d1(:,3)=d1(:,3)+0.75;  
        % Adjust for spherical 1.5 inch diameter CCR on B flange.
        d2(:,3)=d2(:,3)-0.75;  
 % ***********************************************************************        
%        
case 'BC'
%  BC joint: all data needs rotation about the y-axis.  
% ***********************************************************************       
        phi =-40;            % rotation angle in degrees.
        phi = phi*pi/180;   % convert to radians.
        % Matrix for rotation about y-axis.
        Ry=[cos(phi) 0 sin(phi) 0;0 1 0 0;-sin(phi) 0 cos(phi) 0;0 0 0 1];
        % Need 4th column of ones for rotation operation.
        shim_pts(:,4)=1;
        data1(:,4)=1;
        data2(:,4)=1;
        % Rotate shim points.
        crnpts=zeros(size(shim_pts));
            for rotc =1:length(shim_pts)
                crnpts(rotc,:) = (Ry*shim_pts(rotc,:)')';       
            end
        % Rotate B flange data.
        d1=zeros(size(data1));
            for rotc =1:length(data1)
                d1(rotc,:) = (Ry*data1(rotc,:)')';       
            end
            % Rotate C flange data.
        d2=zeros(size(data2));
            for rotc =1:length(data2)
                d2(rotc,:)  = (Ry*data2(rotc,:)')';       
            end
        % Adjust for spherical 1.5 inch diameter CCR on B flange.
        d1(:,3)=d1(:,3)+0.75;  
        % Adjust for spherical 1.5 inch diameter CCR on C flange.
        d2(:,3)=d2(:,3)-0.75;    
  end     % This end is for line 17 switch.
% ***********************************************************************
% Filter Points via Shim corner points.
nshims = length(shim_pts)/4;
% Sort Flange Scan Data1 via Shim corner points.
pts_shims_d1=zeros(4,4,nshims);  % stores x,y,z data per shim.
shim_p =0;                       % Initialize shim data pointer.
for c1=1:4:(nshims*4)            % counts every 4 points per shim
    shim_p =shim_p+1;
    shim_centers(shim_p,(1:3))= sum(shim_pts((c1:c1+3),(1:3)))/4;
% inpolygon function returns a logical array based on the points within a
% region specified, i.e. the region specified by the shim corner points.
shim_filter=inpolygon(d1(:,1),d1(:,2),crnpts((c1:c1+3),1)',crnpts((c1:c1+3),2)');
    pc=0;                        % point data pointer.
% Evaluate logical array and filter data points.
    for c2=1:length(shim_filter)
        if shim_filter(c2)==1
            pc=pc+1;
            pts_shims_d1(pc,:,shim_p)= d1(c2,:);
        end
    end
end
% Sort Flange Scan Data2 via Shim corner points.
pts_shims_d2=zeros(4,4,nshims);  % stores x,y,z data per shim.
shim_p =0;                       % Initialize shim number pointer.
for c1=1:4:(nshims*4)            % counts every 4 points per shim
    shim_p =shim_p+1;           
shim_filter=inpolygon(d2(:,1),d2(:,2),crnpts((c1:c1+3),1)',crnpts((c1:c1+3),2)');
    pc=0;                           % point data pointer.
% Evaluate logical array and filter data points.
    for c2=1:length(shim_filter)
        if shim_filter(c2)==1
            pc=pc+1;
            pts_shims_d2(pc,:,shim_p)= d2(c2,:);
        end
    end
end
% *************************************************************************
% Calculate the average thickness for each shim for flange data1.
% NOTE: Need to filter data to count "values" and remove zeros.
% *************************************************************************
[rws col pg] = size(pts_shims_d1);
for cc1=1:nshims
    avp=0;                  % average value pointer.
    temp_sum=0;             % temporary variable.
% Perform check for 0,0,0 vector and count real data.
    for cc2=1:rws
        if pts_shims_d1(cc2,1,cc1)~=0 && pts_shims_d1(cc2,2,cc1)~=0 && ...
                pts_shims_d1(cc2,3,cc1)~=0
            avp=avp+1;
            temp_sum(avp)=pts_shims_d1(cc2,3,cc1); % sum only Z values.
        end
    end
    avgs_d1(cc1,1) = mean(temp_sum);
end
% *************************************************************************
% Calculate the average thickness for each shim for flange data2.
% NOTE: Need to filter data to count "values" and remove zeros.
% *************************************************************************
[rws col pg] = size(pts_shims_d2);
for cc1=1:nshims
    avp=0;                  % average value pointer.
    temp_sum=0;             % temporary variable.
% Perform check for 0,0,0 vector and count real data.
    for cc2=1:rws
        if pts_shims_d2(cc2,1,cc1)~=0 && pts_shims_d2(cc2,2,cc1)~=0 && ...
                pts_shims_d2(cc2,3,cc1)~=0
            avp=avp+1;
            temp_sum(avp)=pts_shims_d2(cc2,3,cc1);  % sum only Z values.
        end
    end
    avgs_d2(cc1,1) = mean(temp_sum);
end
% *************************************************************************
% Calculate the Shim Thickness
thickness = abs(avgs_d1)+ abs(avgs_d2);
thickness =(round(thickness*1000))/1000;

% *************************************************************************
% Load Results into Base Workspace for ease of access.
assignin('base','thickness',thickness);
assignin('base','crnpts',crnpts);
assignin('base','shim_centers',shim_centers);
% *************************************************************************
% Load Thickness Values into GUI TextBox for Display.
% Zero the value textboxes 
for gd=1:34
    loc =strcat('tx',num2str(gd+70));
    set(findobj('tag',loc),'string','')
end
% Update value textboxes with current thickness values.
for gd=1:length(thickness)-1
    loc =strcat('tx',num2str(gd+70));
    set(findobj('tag',loc),'string',num2str(thickness(gd)))
end
% Update value textbox for temporary shim thickness.
set(findobj('tag','temp'),'string',num2str(thickness(end)));

end % This end is for line 7 if.


% Revision 2/28/08 ******************************** Revision 2/28/08 
labels = evalin('base','labels');

% Zero label textboxes.
for gd=89:122
    loc =strcat('text',num2str(gd));
    set(findobj('tag',loc),'string','')
end
% Update label textboxes.
for gd=1:length(thickness)-1
    loc =strcat('text',num2str(gd+88));
    set(findobj('tag',loc),'string',labels{gd,1})
end
% End Revision 2/28/08 ******************************** End Revision 2/28/08 

