function varargout = SingularityGSP_eski(varargin)
% SINGULARITYGSP_ESKI MATLAB code for SingularityGSP_eski.fig
%      SINGULARITYGSP_ESKI, by itself, creates a new SINGULARITYGSP_ESKI or raises the existing
%      singleton*.
%
%      H = SINGULARITYGSP_ESKI returns the handle to a new SINGULARITYGSP_ESKI or the handle to
%      the existing singleton*.
%
%      SINGULARITYGSP_ESKI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in SINGULARITYGSP_ESKI.M with the given input arguments.
%
%      SINGULARITYGSP_ESKI('Property','Value',...) creates a new SINGULARITYGSP_ESKI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before SingularityGSP_eski_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to SingularityGSP_eski_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 SingularityGSP_eski

% Last Modified by GUIDE v2.5 09-Apr-2012 09:01:50

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @SingularityGSP_eski_OpeningFcn, ...
                   'gui_OutputFcn',  @SingularityGSP_eski_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 SingularityGSP_eski is made visible.
function SingularityGSP_eski_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 SingularityGSP_eski (see VARARGIN)

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


if ( isappdata( 0 , 'Base' ) == 0 ) 
      hDexterity = gcf;
    hhelp = helpdlg( 'Before Singularity 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');
        set(handles.pushbutton_Plot, 'String', 'OK' );
        set(handles.OK, 'visible', 'on');
    end

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

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


% --- Outputs from this function are returned to the command line.
function varargout = SingularityGSP_eski_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' );

[ sizeNaN , LegNumber ] = LegNumbers( ConMatrix );
if ( LegNumber < 6) 
        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 ]

        index=1;
        selects = [];


        if ( Px == 1 ) 
            selects(index) = 1;
            index=index+1;
        end

        if ( Py == 1 ) 
            selects(index) = 2;          
            index=index+1;
        end

        if ( Pz == 1 ) 
            selects(index) = 3;
             index=index+1;
        end

        if ( Alpha == 1 ) 
            selects(index) = 4;          
            index=index+1;
        end

        if ( Beta == 1 ) 
            selects(index) = 5;
            index=index+1;
        end

        if ( Gamma == 1 ) 
            selects(index) = 6;           
            index=index+1;
        end
        if length(selects) ~= LegNumber
            warndlg(['Please select one parameter for each leg = ' num2str(LegNumber)]);
            return;
        end
end



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' ));

Platform.currentPx = px_start;
Platform.currentPy = py_start;
Platform.currentPz = pz_start;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  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 ? 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 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' ));  

Platform.currentAlpha = alpha_From;
Platform.currentBeta  = beta_From;
Platform.currentGamma = gamma_From;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% 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') ); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


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

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% condition number , normalized_condition number
% condition_numbers = zeros(totalX*totalY*totalZ,3); 
% normalized_condition_number = zeros(totalX*totalY*totalZ,3);
% maxSize = max( [totalX,totalY,totalZ,totalAlpha,totalBeta,totalGamma] );
% condition_numbers = zeros( maxSize,  maxSize,  maxSize,  maxSize,  maxSize,  maxSize );
% normalized_condition_numbers = zeros( maxSize,  maxSize,  maxSize,  maxSize,  maxSize,  maxSize );
condition_numbers = zeros(totalX*totalY*totalZ*totalAlpha*totalBeta*totalGamma,1); 
mapping=zeros(totalX*totalY*totalZ*totalAlpha*totalBeta*totalGamma,1); 
% normalized_condition_number = zeros(totalX*totalY*totalZ*totalAlpha*totalBeta*totalGamma,1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% progressbar_n = 2; 
% progressbar_i = 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;

% progressbar(0,0,0,0,0,0);

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
            %  Ustteki harakettli platformun her bir kö?esinin konumu alttaki
            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;
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

            
            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(1:2,1:3) = ...
                    [ Leg.L_vector(j,:); ...
                    cross( ( Platform.Rotation * Platform.PTable(ConMatrix(2,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)             
                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;
            mapping{ icondition_number } = [ 'Pz=' num2str(z) ' Py=' num2str(y) ' Px=' num2str(x) ' alpha=' num2str(alpha) ' beta=' num2str(beta) ' gamma=' num2str(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 );


            trajectory_path(icondition_number,:) = [ Platform.currentPx , Platform.currentPy, Platform.currentPz ];
            trajectory_angles(icondition_number,:) = [ Platform.currentAlpha , Platform.currentBeta, Platform.currentGamma ];
            trajectory_leg_length(icondition_number,:)  = Leg.L;  
            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;

currentFigure = gcf;
% figure(1);
axes( handles.hAxesDexterity );
title('Condition Numbers');

[Xplot,Yplot] = meshgrid( pz_start:step_sizeZ:pz_end , alpha_From:step_sizeALPHA:alpha_To );
[ a, b ] =size(Yplot)
% surf(Xplot,Yplot,condition_numbers)
% surf(Xplot,Yplot, reshape(condition_numbers(1:a*b) ,  a , b ) )
surf(Xplot,Yplot, reshape(condition_numbers(1:a*b) ,  a , b ) )
xlabel('Pz');
ylabel('\alpha');
% surf(Xplot,Yplot, reshape(condition_numbers(1:a*b) ,  a , b ) ,'FaceColor','interp',...
% 'EdgeColor','none',...
% 'FaceLighting','phong')
% daspect([55 65 100])
% axis tight
% view(-50,30)
% camlight left

% ylim('manual')
% ylim([0 .1] )
% get(gca,'YLim')
% set(gca,'YLim', [ 0 1] ); 
% set(gca, 'YScale', 'log')
% get(gca)
% plot(condition_numbers);
title('Condition Numbers');

% figure(2);
% title('Normalized JAcobian');
% plot(condition_numbers2);
% title('Normalized JAcobian');            
condition_numbers
% condition_numbers2;

ylim('auto')

%UNreachable
%plot3(UNreachable(:,1),UNreachable(:,2),UNreachable(:,3));
% reachable;
%plot3(reachable(:,1),reachable(:,2),reachable(:,3));
hAxesDexterity = getappdata(0, 'hAxesDexterity');
axes( handles.hAxesDexterity );

% grid on;rotate3d on;zlabel('Z');ylabel('Y');xlabel('X');
% scatter3( reachable(1:ireachable,1) , reachable(1:ireachable,2),reachable(1:ireachable,3) );


dexterity = sum( sum(condition_numbers) )  / 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');


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 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 );

if ( LegNumber < 6) 
    
end


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



index=1;
selects = [];


if ( Px == 1 ) 
    selects(index) = 1;

    index=index+1;
end

if ( Py == 1 ) 
    selects(index) = 2;
    index=index+1;
end

if ( Pz == 1 ) 
    selects(index) = 3;
    index=index+1;
end

if ( Alpha == 1 ) 
    selects(index) = 4;
    index=index+1;
end

if ( Beta == 1 ) 
    selects(index) = 5;
    index=index+1;
end

if ( Gamma == 1 ) 
    selects(index) = 6;
    index=index+1;
end

selects
%%%% sadece iki tanesi seçilerek ekranda condition_number değerleri
%%%% gösterilmelidir. 




% --- 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;
