function varargout = DexterityGSP(varargin)
% DEXTERITYGSP MATLAB code for DexterityGSP.fig
%      DEXTERITYGSP, by itself, creates a new DEXTERITYGSP or raises the existing
%      singleton*.
%
%      H = DEXTERITYGSP returns the handle to a new DEXTERITYGSP or the handle to
%      the existing singleton*.
%
%      DEXTERITYGSP('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DEXTERITYGSP.M with the given input arguments.
%
%      DEXTERITYGSP('Property','Value',...) creates a new DEXTERITYGSP or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before DexterityGSP_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to DexterityGSP_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help DexterityGSP

% Last Modified by GUIDE v2.5 06-Apr-2012 00:51:36

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @DexterityGSP_OpeningFcn, ...
                   'gui_OutputFcn',  @DexterityGSP_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before DexterityGSP is made visible.
function DexterityGSP_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to DexterityGSP (see VARARGIN)

% Choose default command line output for DexterityGSP
handles.output = hObject;

hDexterity = gcf;

if ( isappdata( 0 , 'Base' ) == 0 ) 
    hhelp = helpdlg( 'Before Dexterity calculation, you must start Analysis GSP');
    uiwait(hhelp);
    hAnalysisGSP = AnalysisGSP;
    set(hDexterity, 'visible' , 'off');

else

    %%%% if Legnumber < 6 then we must choose the controllable parameters.
    ConMatrix = getappdata( 0 , 'ConMatrix' );
    [ sizeNaN , LegNumber ] = LegNumbers( ConMatrix );

    if ( LegNumber < 6 ) 
        set(handles.uipanel7, 'visible' , 'on', 'Title', 'Control parameters');
        set(handles.pushbutton_Plot, 'String', 'OK' );
        set(handles.OK, 'visible', 'on');
        set(handles.OK, 'enable', 'off');
        
        set(handles.Dexterity, 'String', {['Please choose ' num2str(6-LegNumber) '  Fixed parameter(s)']} );
        set(handles.Dexterity, 'visible', 'on' );
        
        setappdata(0, 'UIPanel', 'Control');
    else
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
%%%% Return ALL control parameters
    selects = [ 1 2 3 4 5 6];
    setappdata(0, 'selects', selects );    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        set(handles.uipanel7, 'visible' , 'off','Title', 'Plot Condition Numbers');
        set(handles.pushbutton_Plot, 'String', 'PLOT ' );
        set(handles.OK, 'visible', 'on');        
        
        setappdata(0, 'UIPanel', 'Plot');
    end

end
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes DexterityGSP wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = DexterityGSP_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



% --- Executes on button press in OK.
function OK_Callback(hObject, eventdata, handles)
% hObject    handle to OK (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%%% Be carefull that we need all these datas while ploting on GUI.
%%% Thickness_Base ,  Thickness_Moving will be used.
Base = getappdata( 0 , 'Base' );
Platform = getappdata( 0 , 'Platform' );
ConMatrix = getappdata( 0 , 'ConMatrix' );
LegLimits = getappdata( 0 , 'LegLimits' );
JointLimits = getappdata( 0 , 'JointLimits' );
Thickness_Base = getappdata( 0 , 'Thickness_Base' );
Thickness_Moving = getappdata( 0 , 'Thickness_Moving' );

grid on;rotate3d on;zlabel('Z');ylabel('Y');xlabel('X');

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  px , py , pz 
px_start = str2double( get(handles.PxFrom, 'String' ));   
px_end = str2double( get(handles.PxTo, 'String' ));

py_start = str2double( get(handles.PyFrom, 'String' ));   
py_end = str2double( get(handles.PyTo, 'String' ));

pz_start = str2double( get(handles.PzFrom, 'String' ));   
pz_end = str2double( get(handles.PzTo, 'String' ));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  step_size for px , py , pz 
step_sizeX = str2double ( get(handles.step_sizeX, 'String') ); 
step_sizeY = str2double ( get(handles.step_sizeY, 'String') ); 
step_sizeZ = str2double ( get(handles.step_sizeZ, 'String') ); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Check the values for 0 that cause zero division error ? 
if ( step_sizeX==0  || step_sizeY==0 || step_sizeZ==0 ) 
    warndlg('ERROR. Step Size values must be other than 0 ');
    return;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% alpha, beta , gamma 
alpha_From  = str2double( get(handles.alphaFrom, 'String' )); 
beta_From  = str2double( get(handles.betaFrom, 'String' ));
gamma_From  = str2double( get(handles.gammaFrom, 'String' )); 

alpha_To = str2double( get(handles.alphaTo, 'String' ));
beta_To  = str2double( get(handles.betaTo, 'String' ));
gamma_To = str2double( get(handles.gammaTo, 'String' ));  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Step size for alpha, beta, gamma 
step_sizeALPHA = str2double ( get(handles.step_sizeALPHA, 'String') ); 
step_sizeBETA = str2double ( get(handles.step_sizeBETA, 'String') ); 
step_sizeGAMMA = str2double ( get(handles.step_sizeGAMMA, 'String') ); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Check the values for 0 that cause zero division error ? 
if ( step_sizeALPHA==0  || step_sizeBETA==0 || step_sizeGAMMA==0 ) 
    warndlg('ERROR. Step Size values must be other than 0 ');
    return;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Total Step size for px, py, pz, alpha, beta, gamma 
totalX = ceil( ( px_end - px_start +1 ) / step_sizeX );
totalY = ceil( ( py_end - py_start +1 ) / step_sizeY ); 
totalZ = ceil( ( pz_end - pz_start +1 ) / step_sizeZ);

totalAlpha = ceil( ( alpha_To - alpha_From +1 ) / step_sizeALPHA );
totalBeta = ceil( ( beta_To - beta_From +1 ) / step_sizeBETA );
totalGamma = ceil( ( gamma_To - gamma_From +1 ) / step_sizeGAMMA );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% condition number , normalized_condition number
condition_numbers = zeros(totalX*totalY*totalZ*totalAlpha*totalBeta*totalGamma,1); 
% mapping=zeros(totalX*totalY*totalZ*totalAlpha*totalBeta*totalGamma,1); 
mapping2=zeros(6,totalX*totalY*totalZ*totalAlpha*totalBeta*totalGamma); 
% normalized_condition_number = zeros(totalX*totalY*totalZ*totalAlpha*totalBeta*totalGamma,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

progressbar('Pz Dexterity Calculation','Py Dexterity Calculation',...
            'Px Dexterity Calculation',...
            'Alpha Dexterity Calculation','Beta Dexterity Calculation',...
            'Gamma Dexterity Calculation') 

% Create figure and set starting time 
ix=0;iy=0;iz=0;
ialpha=0;ibeta=0;igamma=0;

icondition_number = 1;

for z=pz_start : step_sizeZ : pz_end
    iz=iz+1;
    progressbar([],0) % reset 2nd bar
    iy=0;
    
    for y=py_start : step_sizeY : py_end
        iy = iy +1;
        progressbar([],[],0) % reset 3rd bar
        ix=0;
        
        for x=px_start : step_sizeX : px_end
            ix=ix+1; 
            progressbar([],[],[],0) % reset 4rd bar
            ialpha = 0;
            
        for alpha=alpha_From : step_sizeALPHA : alpha_To
            ialpha=ialpha+1;                      
            progressbar([],[],[],[],0) % reset 5rd bar          
            ibeta=0;
    
        for beta=beta_From : step_sizeBETA : beta_To
            progressbar([],[],[],[],[],0) % reset 6rd bar
            ibeta=ibeta+1; 
            igamma=0;

        for gamma=gamma_From : step_sizeGAMMA : gamma_To
            igamma=igamma+1; 
            
            Platform.currentPx = x;
            Platform.currentPy = y;
            Platform.currentPz = z; 
            Platform.Translate = [Platform.currentPx; Platform.currentPy;Platform.currentPz; ];

            Platform.currentAlpha = alpha;
            Platform.currentBeta  = beta;
            Platform.currentGamma = gamma;          
            
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %       ROTATION MATRIX , Roll-Pitch-Yaw
            Platform.Rotation = Rotation( Platform.currentAlpha, Platform.currentBeta, Platform.currentGamma , 'RPY' ) ;
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %           Inverse Kinematic of this position
            [ Leg, Platform.PBase, ErrorMsg ]= inverse_kinematic(Base, Platform, ConMatrix, LegLimits, JointLimits );
            %if ( strcmp( ErrorMsg, ' 'No ERROR' ) ~= 0 ) return end;
            if ( ErrorMsg ~= 0 ) 
                 
                 condition_numbers( icondition_number ) = 0;
                 mapping2( : , icondition_number ) = [ z ; y ; x ; alpha ; beta ; gamma ];
                 icondition_number=icondition_number+1;
                 continue;
                 
            end
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

            
            index=1;
            [ sizeNaN , LegNumber ] = LegNumbers( ConMatrix );
            identity_matrix = eye(LegNumber,LegNumber);
            Jx = zeros( LegNumber, 6 );

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%            
            %%% Screw-based Jacobian Method    
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        
           for j = 1:1:LegNumber
              if ~isnan(ConMatrix(1,j)) && ~isnan(ConMatrix(2,j))            
                Jx(index,:) = ...
                    [ cross( ( Platform.Rotation * Platform.PTable(ConMatrix(2,j),:)' )' , Leg.L_vector(j,:)' ) ...
                      Leg.L_vector(j,:)  ];             
                Jq(index,:) = identity_matrix(index,:) .* Leg.L(j);           
                
                for k = 1:1:6

                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %%% Very small values must be zero
                     if ( Jx(index, k) < 1e-14 && Jx(index, k) > -1e-14 ) 
                         Jx(index,k) = 0 ; 
                     end
                end    

                index=index+1;

                
              end
           end

           
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %%% Overall Jacobian
            if ( LegNumber < 6) 
                selects = getappdata( 0 , 'selects');            
                Jx = Jx( : , selects );  
            end
            
            Jacobian = inv(Jq)*Jx;
            Jinverse = inv(Jacobian);
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%        


            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %%%%%%%     condition number 
            W= identity_matrix * 1/LegNumber;
            K = sqrt ( trace( Jacobian*W*Jacobian' ) ) * sqrt( trace( Jinverse*W*Jinverse' ) ) ;
            %         K = sqrt( trace( Jinverse*W*Jinverse' ) ) * sqrt ( trace( J*W*J' ) );        
            condition_numbers( icondition_number ) = 1/K;
            mapping2( : , icondition_number ) = [ z ; y ; x ; alpha ; beta ; gamma ];

            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            %%%%%%%    Normalized Jacobian with Characteristic Length                  
%             phim = 80;
%             phib = 30;
%             ksi = Platform.Rm - Base.Rb * cos((phim-phib)/2); 
% 
%             L = sqrt( Platform.Rm^2 * ( Platform.currentPx^2 + Platform.currentPy^2 + 2*Platform.currentPz^2 ...
%                                   +2*Base.Rb^2*sin( (phim-phib)/2)^2 ) / ...
%                       2*( Platform.currentPx^2 + Platform.currentPy^2 + Platform.currentPz^2 + ksi^2 ) );
% 
%             JX_Normalized = [ Jx(:,1)/L Jx(:,2)/L Jx(:,3)/L Jx(:,4) Jx(:,5) Jx(:,6) ];
%             J_Normalized =inv(Jq)*JX_Normalized;
%             J_Normalized_inverse = inv( J_Normalized ) ;                    
% 
%             K2 = sqrt ( trace( J_Normalized*W*J_Normalized' ) ) * sqrt( trace( J_Normalized_inverse*W*J_Normalized_inverse' ) ) ;
%             condition_numbers2( icondition_number ) = 1/K2;
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

            setappdata( 0 , 'Platform' ,Platform );
            setappdata( 0 , 'Leg' , Leg );

            icondition_number=icondition_number+1;
            
          progressbar([],[],[],ialpha/totalAlpha) % update 3rd bar
         end %alpha     
         progressbar([],[],[],1);
            
          progressbar([],[],[],[],ibeta/totalBeta) % update 3rd bar
         end %beta
         progressbar([],[],[],[],1);

          progressbar([],[],[],[],[],igamma/totalGamma) % update 3rd bar
         end %gamma
         progressbar([],[],[],[],[],1);
         
         progressbar([],[],ix/totalX) % update 3rd bar
        end %z
       progressbar([],iy/totalY) % update 2nd bar         
    end%y
    progressbar(iz/totalZ) % update 1st bar
end%x

% progressbar(1);
% disp('WorkspaceSTEWSIM içinde hesaplama tamamlandı....');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%      currentPx ile px_end e?it oldumu ? 
Platform.currentPx = px_end;
Platform.currentPx = px_end;
Platform.currentPx = px_end;

setappdata( 0 , 'condition_numbers' , condition_numbers );
selected = getappdata(0,'Selected_Mechanism');

save(['dexterity_' selected '.mat'],'condition_numbers',...
    'px_start','px_end','py_start','py_end','pz_start','pz_end',...
    'step_sizeX','step_sizeY','step_sizeZ',...
    'alpha_From','beta_From','gamma_From','alpha_To','beta_To','gamma_To',...
    'step_sizeALPHA','step_sizeBETA','step_sizeGAMMA');

setappdata( 0 , 'mapping2' , mapping2 );
% icondition_number

% load('dexterity_6x6 GSP2.mat')
% load('dexterity_5x4 GSP1.mat')

sum_condition = 0;

for ii = 1:1:length(condition_numbers)
    if isnan( condition_numbers(ii) )
        ii;
    else
        sum_condition = sum_condition + condition_numbers(ii);
    end
end
% sum_condition;
% dexterity = sum( sum(condition_numbers) )  / length(condition_numbers);

dexterity = sum_condition  / length(condition_numbers);

set(handles.Dexterity, 'visible', 'on' );
dexterity_String = strcat('Dexterity =  ', num2str(dexterity));
set(handles.Dexterity, 'String', dexterity_String );

set(handles.uipanel7, 'visible' , 'on');
set(handles.pushbutton_Plot, 'String', 'Plot');
setappdata( 0 , 'UIPanel' , 'Plot' );






% --- Executes on button press in pushbutton_Plot.
function pushbutton_Plot_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_Plot (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
ConMatrix = getappdata( 0 , 'ConMatrix' );

%%%%    LegNumber = number of Legs in connection matrix.
%%%%    sizeNaN   = number of NaN in connection matrix.
[ sizeNaN , LegNumber ] = LegNumbers( ConMatrix );

UIPanel = getappdata( 0 , 'UIPanel');





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  px , py , pz 
px_start = str2double( get(handles.PxFrom, 'String' ));   
px_end = str2double( get(handles.PxTo, 'String' ));

py_start = str2double( get(handles.PyFrom, 'String' ));   
py_end = str2double( get(handles.PyTo, 'String' ));

pz_start = str2double( get(handles.PzFrom, 'String' ));   
pz_end = str2double( get(handles.PzTo, 'String' ));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  step_size for px , py , pz 
step_sizeX = str2double ( get(handles.step_sizeX, 'String') ); 
step_sizeY = str2double ( get(handles.step_sizeY, 'String') ); 
step_sizeZ = str2double ( get(handles.step_sizeZ, 'String') ); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Check the values for 0 that cause zero division error ? 
if ( step_sizeX==0  || step_sizeY==0 || step_sizeZ==0 ) 
    warndlg('ERROR. Step Size values cannot be 0 ');
    return;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% alpha, beta , gamma 
alpha_From  = str2double( get(handles.alphaFrom, 'String' )); 
beta_From  = str2double( get(handles.betaFrom, 'String' ));
gamma_From  = str2double( get(handles.gammaFrom, 'String' )); 

alpha_To = str2double( get(handles.alphaTo, 'String' ));
beta_To  = str2double( get(handles.betaTo, 'String' ));
gamma_To = str2double( get(handles.gammaTo, 'String' ));  
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Step size for alpha, beta, gamma 
step_sizeALPHA = str2double ( get(handles.step_sizeALPHA, 'String') ); 
step_sizeBETA = str2double ( get(handles.step_sizeBETA, 'String') ); 
step_sizeGAMMA = str2double ( get(handles.step_sizeGAMMA, 'String') ); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Check the values for 0 that cause zero division error ? 
if ( step_sizeALPHA==0  || step_sizeBETA==0 || step_sizeGAMMA==0 ) 
    warndlg('ERROR. Step Size values cannot be 0 ');
    return;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Total Step size for px, py, pz, alpha, beta, gamma 
totalX = ceil( ( px_end - px_start +1 ) / step_sizeX );
totalY = ceil( ( py_end - py_start +1 ) / step_sizeY ); 
totalZ = ceil( ( pz_end - pz_start +1 ) / step_sizeZ);

totalAlpha = ceil( ( alpha_To - alpha_From +1 ) / step_sizeALPHA );
totalBeta = ceil( ( beta_To - beta_From +1 ) / step_sizeBETA );
totalGamma = ceil( ( gamma_To - gamma_From +1 ) / step_sizeGAMMA );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ( strcmp( UIPanel , 'Plot' ) == 1 ) 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    condition_numbers = getappdata( 0 , 'condition_numbers');
%     mapping2 = getappdata( 0 , 'mapping2' );

    Px = get(handles.checkbox_Px,'Value');
    Py = get(handles.checkbox_Py,'Value');
    Pz = get(handles.checkbox_Pz,'Value');
    Alpha = get(handles.checkbox_alpha,'Value');
    Beta = get(handles.checkbox_beta,'Value');
    Gamma = get(handles.checkbox_gamma,'Value');

%     columns = [ 1 2 3 4 5 6] ;
    Limits = [ Px Py Pz Alpha Beta Gamma ];

    if ( length(find(Limits)) ~= 2 )
        warndlg('Please choose 2 parameter to plot the condition number');
        return;
    end

    param=0; 

    if ( Px == 1 ) 
        if ( param == 0 ) 
            xlabel('Px');
            param = 1;
            param1 = 1; %first parameter Px
            dongu1 = px_start : step_sizeX : px_end;
        elseif (param==1)  %%% if param==1 
            ylabel('Px');
            param=2;
            param2 = 1; %second paramter Px
            dongu2 = px_start : step_sizeX : px_end;
        elseif (param==2)
            %%% we need only two parameter. 
        end
    end

    if ( Py == 1 ) 
        if ( param == 0 ) 
            xlabel('Py');
            param = 1;
            param1 = 2; %first parameter Py
            dongu1 = py_start : step_sizeY : py_end;
        elseif (param==1)  %%% if param==1 
            ylabel('Py');
            param=2;
            param2 = 2; %second paramter Py
            dongu2 = py_start : step_sizeY : py_end;
        elseif (param==2)
            %%% we need only two parameter. 
        end
    end

    if ( Pz == 1 ) 
        if ( param == 0 ) 
            xlabel('Pz');
            param = 1;
            param1 = 3; %first parameter Pz
            dongu1 = pz_start : step_sizeZ : pz_end;
        elseif (param==1)  %%% if param==1 
            ylabel('Pz');
            param=2;
            param2 = 3; %second paramter Pz
            dongu2 = pz_start : step_sizeZ : pz_end;
        elseif (param==2)
            %%% we need only two parameter. 
        end
    end

    if ( Alpha == 1 ) 
        if ( param == 0 ) 
            xlabel('\alpha');
            param = 1;
            param1 = 4; %first parameter alpha
            dongu1 = alpha_From : step_sizeALPHA : alpha_To;
        elseif (param==1)  %%% if param==1 
            ylabel('\alpha');
            param=2;
            param2 = 4; %second paramter alpha
            dongu2 = alpha_From : step_sizeALPHA : alpha_To;
        elseif (param==2)
            %%% we need only two parameter. 
        end
    end

    if ( Beta == 1 ) 
        if ( param == 0 ) 
            xlabel('\beta');
            param = 1;
            param1 = 5; %first parameter beta
            dongu1 = beta_From : step_sizeBETA : beta_To;
        elseif (param==1)  %%% if param==1 
            ylabel('\beta');
            param=2;
            param2 = 5; %second paramter beta
            dongu2 = beta_From : step_sizeBETA : beta_To;
        elseif (param==2)
            %%% we need only two parameter. 
        end
    end

    if ( Gamma == 1 ) 
          if ( param == 0 ) 
            xlabel('\gamma');
            param = 1;
            param1 = 6; %first parameter gamma
            dongu1 = gamma_From : step_sizeGAMMA : gamma_To;
        elseif (param==1)  %%% if param==1 
            ylabel('\gamma');
            param=2;
            param2 = 6; %second paramter gamma
            dongu2 = gamma_From : step_sizeGAMMA : gamma_To;
        elseif (param==2)
            %%% we need only two parameter. 
        end
    end


    
    
    
secilen_Pz = pz_start;
secilen_Py = py_start;
secilen_Px = px_start;
secilen_alpha = alpha_From;
secilen_beta = beta_From;
secilen_gamma = gamma_From;


f = ( gamma_To - gamma_From ) / step_sizeGAMMA +1 ;
e = ( beta_To - beta_From )   / step_sizeBETA  +1 ;
d = ( alpha_To - alpha_From ) / step_sizeALPHA +1;
c = ( px_end - px_start )  / step_sizeX +1;
b = ( py_end - py_start )  / step_sizeY +1;
a = ( pz_end - pz_start )  / step_sizeZ +1;


index = 1;

for parameter1 = dongu1

    if param1 == 1  %%% Px 
        secilen_Px = parameter1;
    elseif param1 == 2 %%% Py 
        secilen_Py = parameter1;
    elseif param1 == 3 %%% Pz 
        secilen_Pz = parameter1;    
    elseif param1 == 4 %%% alpha 
        secilen_alpha = parameter1;        
    elseif param1 == 5 %%% Py 
        secilen_beta = parameter1;    
    elseif param1 == 6 %%% Py 
        secilen_gamma = parameter1;
    end
    
    for parameter2 = dongu2
        
        if param2 == 1  %%% Px 
            secilen_Px = parameter2;
        elseif param2 == 2 %%% Py 
            secilen_Py = parameter2;
        elseif param2 == 3 %%% Pz 
            secilen_Pz = parameter2;    
        elseif param2 == 4 %%% alpha 
            secilen_alpha = parameter2;        
        elseif param2 == 5 %%% Py 
            secilen_beta = parameter2;    
        elseif param2 == 6 %%% Py 
            secilen_gamma = parameter2;
        end
            
        
        
        ff = (secilen_gamma-gamma_From) / step_sizeGAMMA+1;

        ee = (secilen_beta-beta_From) / step_sizeBETA*f;  

        dd = (secilen_alpha-alpha_From) / step_sizeALPHA*e*f;

        cc = (secilen_Px-px_start)  / step_sizeX  *d*e*f;

        bb = (secilen_Py-py_start)  / step_sizeY  *c*d*e*f;

        aa = (secilen_Pz-pz_start)  / step_sizeZ  *b*c*d*e*f;


        indis = aa + bb + cc + dd + ee + ff;

%         sonuc_kontrol(:, index) = mapping2( : , indis );
        
        sonuc( index ) = condition_numbers( indis );
        index = index + 1;

    end
end


[Xplot,Yplot] = meshgrid( dongu1 , dongu2 );
[ a, b ] = size(Yplot);
surf(Xplot,Yplot, reshape(sonuc(1:a*b) ,  a , b ) );
zlabel('Dexterity');

    if param1 == 1  %%% Px 
        xlabel('Px');
    elseif param1 == 2 %%% Py 
        xlabel('Py');
    elseif param1 == 3 %%% Pz 
        xlabel('Pz');
    elseif param1 == 4 %%% alpha 
        xlabel('\alpha');
    elseif param1 == 5 %%% Py 
        xlabel('\beta');
    elseif param1 == 6 %%% Py 
        xlabel('\gamma');
    end
        
    if param2 == 1  %%% Px 
        ylabel('Px');
    elseif param2 == 2 %%% Py 
        ylabel('Py');
    elseif param2 == 3 %%% Pz 
        ylabel('Pz');
    elseif param2 == 4 %%% alpha 
        ylabel('\alpha');      
    elseif param2 == 5 %%% Py 
        ylabel('\beta');
    elseif param2 == 6 %%% Py 
        ylabel('\gamma');
    end








    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else    %%%% if UIPanel == 'Control'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    Px = get(handles.checkbox_Px,'Value');
    Py = get(handles.checkbox_Py,'Value');
    Pz = get(handles.checkbox_Pz,'Value');
    Alpha = get(handles.checkbox_alpha,'Value');
    Beta = get(handles.checkbox_beta,'Value');
    Gamma = get(handles.checkbox_gamma,'Value');

%     columns = [ 1 2 3 4 5 6] 
    Limits = [ Px Py Pz Alpha Beta Gamma ];

    if ( length(find(Limits)) ~= 6-LegNumber )
        warndlg(['Please choose ' num2str( 6-LegNumber ) '  Fixed parameter(s)']);
        return;
    end



    index=1;
    selects = [];


    if ( Px == 0 ) 
        selects(index) = 1;
        index=index+1;
    else
        set(handles.PxFrom,'enable','off');
        set(handles.PxTo,'enable','off');  
        set(handles.checkbox_Px,'enable','off');  
    end

    if ( Py == 0 ) 
        selects(index) = 2;
        index=index+1;
    else
        set(handles.PyFrom,'enable','off');
        set(handles.PyTo,'enable','off');    
        set(handles.checkbox_Py,'enable','off');          
    end

    if ( Pz == 0 ) 
        selects(index) = 3;
        index=index+1;
    else
        set(handles.PzFrom,'enable','off');
        set(handles.PzTo,'enable','off');  
        set(handles.checkbox_Pz,'enable','off');          
    end

    if ( Alpha == 0 ) 
        selects(index) = 4;
        index=index+1;
    else
        set(handles.alphaFrom,'enable','off');
        set(handles.alphaTo,'enable','off'); 
        set(handles.checkbox_alpha,'enable','off');          
    end

    if ( Beta == 0 ) 
        selects(index) = 5;
        index=index+1;
    else
        set(handles.betaFrom,'enable','off');
        set(handles.betaTo,'enable','off');     
        set(handles.checkbox_beta,'enable','off');          
    end

    if ( Gamma == 0 ) 
        selects(index) = 6;
        index=index+1;
    else
        set(handles.gammaFrom,'enable','off');
        set(handles.gammaTo,'enable','off');    
        set(handles.checkbox_gamma,'enable','off');          
    end
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
%%%% Return the control parameters
    setappdata(0, 'selects', selects );    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    set(handles.OK, 'visible', 'on');    
    set(handles.OK, 'enable', 'on');
    set(handles.uipanel7, 'visible' , 'off','Title', 'Plot Condition Numbers');
    set(handles.pushbutton_Plot, 'String', 'Plot ' );
    set(handles.Dexterity, 'visible', 'off'); 

%%%% reset uipanel and set this panel to Plot.    
    set(handles.checkbox_Px,'Value',0);
    set(handles.checkbox_Py,'Value',0);
    set(handles.checkbox_Pz,'Value',0);
    set(handles.checkbox_alpha,'Value',0);
    set(handles.checkbox_beta,'Value',0);
    set(handles.checkbox_gamma,'Value',0);  
    setappdata( 0 , 'UIPanel', 'Plot');
    
end         %%%%% End for UIPanel



function edit7_Callback(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit7 as text
%        str2double(get(hObject,'String')) returns contents of edit7 as a double


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit8_Callback(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit8 as text
%        str2double(get(hObject,'String')) returns contents of edit8 as a double


% --- Executes during object creation, after setting all properties.
function edit8_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit9_Callback(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit9 as text
%        str2double(get(hObject,'String')) returns contents of edit9 as a double


% --- Executes during object creation, after setting all properties.
function edit9_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit9 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit10_Callback(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of edit10 as text
%        str2double(get(hObject,'String')) returns contents of edit10 as a double


% --- Executes during object creation, after setting all properties.
function edit10_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox1


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox2


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function PxFrom_Callback(hObject, eventdata, handles)
% hObject    handle to PxFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PxFrom as text
%        str2double(get(hObject,'String')) returns contents of PxFrom as a double


% --- Executes during object creation, after setting all properties.
function PxFrom_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PxFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function PzFrom_Callback(hObject, eventdata, handles)
% hObject    handle to PzFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PzFrom as text
%        str2double(get(hObject,'String')) returns contents of PzFrom as a double


% --- Executes during object creation, after setting all properties.
function PzFrom_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PzFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes during object creation, after setting all properties.
function alphaFrom_CreateFcn(hObject, eventdata, handles)
% hObject    handle to alphaFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function betaFrom_Callback(hObject, eventdata, handles)
% hObject    handle to betaFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of betaFrom as text
%        str2double(get(hObject,'String')) returns contents of betaFrom as a double


% --- Executes during object creation, after setting all properties.
function betaFrom_CreateFcn(hObject, eventdata, handles)
% hObject    handle to betaFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function gammaFrom_Callback(hObject, eventdata, handles)
% hObject    handle to gammaFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of gammaFrom as text
%        str2double(get(hObject,'String')) returns contents of gammaFrom as a double


% --- Executes during object creation, after setting all properties.
function gammaFrom_CreateFcn(hObject, eventdata, handles)
% hObject    handle to gammaFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



function PyFrom_Callback(hObject, eventdata, handles)
% hObject    handle to PyFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PyFrom as text
%        str2double(get(hObject,'String')) returns contents of PyFrom as a double


% --- Executes during object creation, after setting all properties.
function PyFrom_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PyFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function step_sizeX_Callback(hObject, eventdata, handles)
% hObject    handle to step_sizeX (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of step_sizeX as text
%        str2double(get(hObject,'String')) returns contents of step_sizeX as a double


% --- Executes during object creation, after setting all properties.
function step_sizeX_CreateFcn(hObject, eventdata, handles)
% hObject    handle to step_sizeX (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function step_sizeY_Callback(hObject, eventdata, handles)
% hObject    handle to step_sizeY (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of step_sizeY as text
%        str2double(get(hObject,'String')) returns contents of step_sizeY as a double


% --- Executes during object creation, after setting all properties.
function step_sizeY_CreateFcn(hObject, eventdata, handles)
% hObject    handle to step_sizeY (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function step_sizeZ_Callback(hObject, eventdata, handles)
% hObject    handle to step_sizeZ (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of step_sizeZ as text
%        str2double(get(hObject,'String')) returns contents of step_sizeZ as a double


% --- Executes during object creation, after setting all properties.
function step_sizeZ_CreateFcn(hObject, eventdata, handles)
% hObject    handle to step_sizeZ (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function step_sizeALPHA_Callback(hObject, eventdata, handles)
% hObject    handle to step_sizeALPHA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of step_sizeALPHA as text
%        str2double(get(hObject,'String')) returns contents of step_sizeALPHA as a double


% --- Executes during object creation, after setting all properties.
function step_sizeALPHA_CreateFcn(hObject, eventdata, handles)
% hObject    handle to step_sizeALPHA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function step_sizeBETA_Callback(hObject, eventdata, handles)
% hObject    handle to step_sizeBETA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of step_sizeBETA as text
%        str2double(get(hObject,'String')) returns contents of step_sizeBETA as a double


% --- Executes during object creation, after setting all properties.
function step_sizeBETA_CreateFcn(hObject, eventdata, handles)
% hObject    handle to step_sizeBETA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function step_sizeGAMMA_Callback(hObject, eventdata, handles)
% hObject    handle to step_sizeGAMMA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of step_sizeGAMMA as text
%        str2double(get(hObject,'String')) returns contents of step_sizeGAMMA as a double


% --- Executes during object creation, after setting all properties.
function step_sizeGAMMA_CreateFcn(hObject, eventdata, handles)
% hObject    handle to step_sizeGAMMA (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function PxTo_Callback(hObject, eventdata, handles)
% hObject    handle to PxTo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PxTo as text
%        str2double(get(hObject,'String')) returns contents of PxTo as a double



function PyTo_Callback(hObject, eventdata, handles)
% hObject    handle to PyTo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PyTo as text
%        str2double(get(hObject,'String')) returns contents of PyTo as a double



function PzTo_Callback(hObject, eventdata, handles)
% hObject    handle to PzTo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of PzTo as text
%        str2double(get(hObject,'String')) returns contents of PzTo as a double



function alphaTo_Callback(hObject, eventdata, handles)
% hObject    handle to alphaTo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of alphaTo as text
%        str2double(get(hObject,'String')) returns contents of alphaTo as a double



function betaTo_Callback(hObject, eventdata, handles)
% hObject    handle to betaTo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of betaTo as text
%        str2double(get(hObject,'String')) returns contents of betaTo as a double



function gammaTo_Callback(hObject, eventdata, handles)
% hObject    handle to gammaTo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of gammaTo as text
%        str2double(get(hObject,'String')) returns contents of gammaTo as a double


% --- Executes during object creation, after setting all properties.
function hAxesDexterity_CreateFcn(hObject, eventdata, handles)
% hObject    handle to hAxesDexterity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate hAxesDexterity
setappdata(0, 'hAxesDexterity', hObject );



function alphaFrom_Callback(hObject, eventdata, handles)
% hObject    handle to alphaFrom (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of alphaFrom as text
%        str2double(get(hObject,'String')) returns contents of alphaFrom as a double


% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox3


% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox4


% --- Executes on button press in checkbox5.
function checkbox5_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox5


% --- Executes on button press in checkbox6.
function checkbox6_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox6


% --- Executes on button press in checkbox7.
function checkbox7_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox7


% --- Executes on button press in checkbox8.
function checkbox8_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox8


% --- Executes on button press in checkbox_Px.
function checkbox_Px_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_Px (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_Px


% --- Executes on button press in checkbox_Py.
function checkbox_Py_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_Py (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_Py


% --- Executes on button press in checkbox_Pz.
function checkbox_Pz_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_Pz (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_Pz


% --- Executes on button press in checkbox_alpha.
function checkbox_alpha_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_alpha (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_alpha


% --- Executes on button press in checkbox_beta.
function checkbox_beta_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_beta (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_beta


% --- Executes on button press in checkbox_gamma.
function checkbox_gamma_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_gamma (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox_gamma





% --- Executes during object creation, after setting all properties.
function figure1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: delete(hObject) closes the figure
delete(hObject);
AnalysisGSP;


% --- Executes on scroll wheel click while the figure is in focus.
function figure1_WindowScrollWheelFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  structure with the following fields (see FIGURE)
%	VerticalScrollCount: signed integer indicating direction and number of clicks
%	VerticalScrollAmount: number of lines scrolled for each click
% handles    structure with handles and user data (see GUIDATA)
