function CT_i_frd(action)

% stellt GUI-Interface für "direktes CT-Problem" zur Verfügung
% eigentliche Berrechnung und Animation in "ct_dcalc.m"

% Komponenten :

% 1. Buttons
%               startBnt
%               exportBnt
%               closeBnt
%               invBnt
% 2. Edits
%               phiEdt
%               stepEdt
%               inEdt
%               sizeEdt
%               alphaEdt
%               raynumEdt
%               (Matrix import)
% 3. Checkbox
%               animChk
%               traceChk


% --------------------  BEGIN  --------------------------------------------------------------

play= 1;
stop=-1;

if nargin<1,
    action='initialize';
end;


if strcmp(action,'initialize'),
    oldFigNumber=watchon;

    figNumber=figure( ...
        'Name','CT Simulation - inverses Problem', ...
        'Position',[5 5 792 592], ...
        'NumberTitle','off', ...
        'Visible','off', ...
        'MenuBar', 'none');
    axes( ...
        'Units','normalized', ...
        'Position',[0.05 0.2 0.75 0.75], ...
        'Visible','off');

    axis([-1 1 -1 1]);

% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    
%    clear global kI; % !!!!!!!!!!!!!!!!!!!!!!!
% !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    

% --------------------  GUI CONTROLS BEGIN --------------------------------------------------------

%====================================

% Information for all BUTTONS

            labelColor=[0.8 0.8 0.8];
            yInitPos=0.90;
            xPos=0.85;
            btnLen=0.10;
            btnWid=0.05;
            
        % Spacing between the button and the next command's label
            spacing=0.025;
            
        %====================================
        
        % The CONSOLE frame
        
            frmBorder=0.02;
            yPos=0.05-frmBorder;
            frmPos=[xPos-frmBorder yPos btnLen+2*frmBorder 0.9+2*frmBorder];
            h=uicontrol( ...
                         'Style','frame', ...
                         'Units','normalized', ...
                         'Position',frmPos, ...
                         'BackgroundColor',[0.50 0.50 0.50]);
    
        %====================================
        
        % The START button
    
            btnNumber=1;
            yPos=0.90-(btnNumber-1)*(btnWid+spacing);
            labelStr='Solve';
            cmdStr='start';
            callbackStr='CT_i_frd(''solve'');';
      
            btnPos=[xPos yPos-spacing btnLen btnWid];
            startBnt=uicontrol( ...
                                'Style','pushbutton', ...
                                'Units','normalized', ...
                                'Position',btnPos, ...
                                'String',labelStr, ...
                                'Enable','off', ...                                
                                'Interruptible','yes', ...
                                'Callback',callbackStr);

        %====================================
        
        % The EXPORT button
    
            btnNumber=2;
            yPos=0.90-(btnNumber-1)*(btnWid+spacing);
            labelStr='Load';
            callbackStr='CT_i_frd(''load'');';
    
            btnPos=[xPos yPos-spacing btnLen btnWid];
            exportBnt=uicontrol( ...
                                 'Style','pushbutton', ...
                                 'Units','normalized', ...
                                 'Position',btnPos, ...
                                 'Enable','on', ...
                                 'String',labelStr, ...
                                 'Callback',callbackStr);
                                                                  
        %====================================
        
        % The CLOSE button
        
            labelStr='Close';
            btnNumber=3;
            callbackStr='close(gcf)';
            yPos=0.90-(btnNumber-1)*(btnWid+spacing);    
            btnPos=[xPos yPos-spacing btnLen btnWid];
    
            closeBnt=uicontrol( ...
                                'Style','push', ...
                                'Units','normalized', ...
                                'Position',btnPos, ...
                                'string',labelStr, ...
                                'call',callbackStr);

        %====================================
        
        % The INVERSE button
        
        %    labelStr='Solve';
        %    btnNumber=4;
        %    callbackStr='CT_i_frd(''solve'');';
        %    yPos=0.90-(btnNumber-1)*(btnWid+spacing);    
        %    btnPos=[xPos yPos-spacing btnLen btnWid];
    
        %    invBnt=uicontrol( ...
        %                        'Style','push', ...
        %                        'Units','normalized', ...
        %                        'Position',btnPos, ...
        %                        'string',labelStr, ...
        %                        'call',callbackStr);
    

% ===================================
        
% Information for all EDITS

            labelColor=[0.8 0.8 0.8];
            backColor=[1 1 1];
            yEdtPos=0.5;    
            xPos=0.85;
            edtLen=0.10;
            edtWid=0.03;
            
        % Spacing between the edits and the next command's label        
            spacing=0.05;
    
        %====================================

        % The SIZE edit
    
            edtNumber=1;
            yPos=yEdtPos-(edtNumber-1)*(edtWid+spacing);
            labelStr='20';

            edtPos=[xPos yPos-spacing edtLen edtWid];
            sizeEdt=uicontrol( ...
                               'Style','edit', ...
                               'Units','normalized', ...
                               'Position',edtPos, ...
                               'String',labelStr, ...
                               'BackgroundColor',backColor, ...
                               'Interruptible','yes');


        % ===================================
    
        % The STEP edit
    
            edtNumber=2;
            yPos=yEdtPos-(edtNumber-1)*(edtWid+spacing);
            labelStr='45';

            edtPos=[xPos yPos-spacing edtLen edtWid];
            stepEdt=uicontrol( ...
                               'Style','edit', ...
                               'Units','normalized', ...
                               'Position',edtPos, ...
                               'String',labelStr, ...
                               'BackgroundColor',backColor, ...
                               'Interruptible','yes');

        % ===================================
    
        % The INTEN edit
    
            edtNumber=3;
            yPos=yEdtPos-(edtNumber-1)*(edtWid+spacing);
            labelStr='100';

            edtPos=[xPos yPos-spacing edtLen edtWid];
            inEdt=uicontrol( ...
                               'Style','edit', ...
                               'Units','normalized', ...
                               'Position',edtPos, ...
                               'String',labelStr, ...
                               'BackgroundColor',backColor, ...
                               'Interruptible','yes');

        % ===================================
    
        % The ALPHA edit
    
            edtNumber=4;
            yPos=yEdtPos-(edtNumber-1)*(edtWid+spacing);
            labelStr='30';

            edtPos=[xPos yPos-spacing edtLen edtWid];
            alphaEdt=uicontrol( ...
                               'Style','edit', ...
                               'Units','normalized', ...
                               'Position',edtPos, ...
                               'String',labelStr, ...
                               'BackgroundColor',backColor, ...
                               'Interruptible','yes');
                               
        % ===================================
    
        % The RAYNUM edit
    
            edtNumber=5;
            yPos=yEdtPos-(edtNumber-1)*(edtWid+spacing);
            labelStr='9';

            edtPos=[xPos yPos-spacing edtLen edtWid];
            raynumEdt=uicontrol( ...
                               'Style','edit', ...
                               'Units','normalized', ...
                               'Position',edtPos, ...
                               'String',labelStr, ...
                               'BackgroundColor',backColor, ...
                               'Interruptible','yes');                               

        % ===================================
    
        % The PHI edit
    
            edtNumber=6;
            yPos=yEdtPos-(edtNumber-1)*(edtWid+spacing);
            labelStr='0';

            edtPos=[xPos yPos-spacing edtLen edtWid];
            phiEdt=uicontrol( ...
                               'Style','edit', ...
                               'Units','normalized', ...
                               'Position',edtPos, ...
                               'String',labelStr, ...
                               'BackgroundColor',backColor, ...
                               'Interruptible','yes');                                                              
        
% ===================================

% Information for all LABELS
        
        YLblPos=0.48;
        LblSize=0.08;
        
        % ===================================
        
        % The SIZE label

            labelNum=1;
            yPos=YLblPos-(labelNum-1)*LblSize;
            labelPos=[xPos yPos 0.1 0.03];
            uicontrol( ...
                       'Style','text', ...
                       'Units','normalized', ...
                       'Position',labelPos, ...
                       'BackgroundColor',[0.50 0.50 0.50], ...
                       'ForegroundColor',[1 1 1], ...
                       'String','Größe');
                       
        % ===================================
        
        % The STEP label

            labelNum=2;                    
            yPos=YLblPos-(labelNum-1)*LblSize;
            labelPos=[xPos yPos 0.1 0.03];
            uicontrol( ...
                       'Style','text', ...
                       'Units','normalized', ...
                       'Position',labelPos, ...
                       'BackgroundColor',[0.50 0.50 0.50], ...
                       'ForegroundColor',[1 1 1], ...
                       'String','Schritte');
    
        %====================================
              
        % The INTEN label

            labelNum=3;
            yPos=YLblPos-(labelNum-1)*LblSize;
            labelPos=[xPos yPos 0.1 0.03];
            uicontrol( ...
                       'Style','text', ...
                       'Units','normalized', ...
                       'Position',labelPos, ...
                       'BackgroundColor',[0.50 0.50 0.50], ...
                       'ForegroundColor',[1 1 1], ...
                       'String','Intensität');
                       
        % ===================================
        
        % The ALPHA label
                       
            labelNum=4;
            yPos=YLblPos-(labelNum-1)*LblSize;
            labelPos=[xPos yPos 0.1 0.03];
            uicontrol( ...
                       'Style','text', ...
                       'Units','normalized', ...
                       'Position',labelPos, ...
                       'BackgroundColor',[0.50 0.50 0.50], ...
                       'ForegroundColor',[1 1 1], ...
                       'String','Spreizung °');   
        
        % ===================================        
        
        % The RAYNUM label

            labelNum=5;
            yPos=YLblPos-(labelNum-1)*LblSize;
            labelPos=[xPos yPos 0.1 0.03];
            uicontrol( ...
                       'Style','text', ...
                       'Units','normalized', ...
                       'Position',labelPos, ...
                       'BackgroundColor',[0.50 0.50 0.50], ...
                       'ForegroundColor',[1 1 1], ...
                       'String','Strahlen');
                       
        % ===================================
        
        % The PHI label
                       
            labelNum=6;
            yPos=YLblPos-(labelNum-1)*LblSize;
            labelPos=[xPos yPos 0.1 0.03];
            uicontrol( ...
                       'Style','text', ...
                       'Units','normalized', ...
                       'Position',labelPos, ...
                       'BackgroundColor',[0.50 0.50 0.50], ...
                       'ForegroundColor',[1 1 1], ...
                       'String','Startwinkel °');
                       
        % ===================================
        
        % The steps label
                       
            labelNum=6;
            labelPos=[0.1 0.05 0.1 0.03];
            stepsLbl= uicontrol( ...
                                 'Style','text', ...
                                 'Units','normalized', ...
                                 'Position',labelPos, ...
                                 'BackgroundColor',[0 0 0], ...
                                 'ForegroundColor',[1 1 1], ...
                                 'String','');
                       
        % ===================================
        
        % The time label
                       
            labelNum=6;
            yPos=YLblPos-(labelNum-1)*LblSize;
            labelPos=[0.37 0.05 0.1 0.03];
            timeLbl = uicontrol( ...
                                 'Style','text', ...
                                 'Units','normalized', ...
                                 'Position',labelPos, ...
                                 'BackgroundColor',[0 0 0], ...
                                 'ForegroundColor',[1 1 1], ...
                                 'String','time 0');

           
% ===================================

% Information for all CHECKBOXES
        
        YChkPos=0.6;
        ChkSize=0.08;
        
        % ===================================
                      
        % The ANIM checkbox

            labelNum=1;   
            labelPos=[xPos YChkPos 0.1 0.03];
        %    animChk = uicontrol( ...
        %                         'Style','checkbox', ...
        %                         'Units','normalized', ...
        %                         'Position',labelPos, ...
        %                         'BackgroundColor',[0.50 0.50 0.50], ...
        %                         'ForegroundColor',[1 1 1], ...
        %                         'String','Animation');
        
        %====================================

        % The ANIM checkbox

            labelNum=1;
            labelPos=[xPos YChkPos-0.05 0.1 0.03];
         %   traceChk = uicontrol( ...
         %                         'Style','checkbox', ...
         %                         'Units','normalized', ...
         %                         'Position',labelPos, ...
         %                         'BackgroundColor',[0.50 0.50 0.50], ...
         %                         'ForegroundColor',[1 1 1], ...
         %                         'String','Verlauf');
        
        %====================================

% GUI controls for the error sheet

                err_ctrl = uicontrol( ...
                               'units','normal',...
                               'position',[0.1 .09 .8*(.9-0.26) .04], ...
                               'style','slider', ...
                               'min',0, 'max',1, ...
                               'Visible', 'off', ...
                               'Callback','ct_i_frd(''error'')', ...
                               'value',0);

            btnPos=[0.65 0.08 0.12 0.06];
    
            genRnd=uicontrol( ...
                                'Style','push', ...
                                'Units','normalized', ...
                                'Position',btnPos, ...
                                'string','GenRandom', ...
                                'Visible', 'off', ...
                                'call','global I; global E0; E0 = randn(size(I)); ct_i_frd(''error'');');

        
%--------------------  GUI CONTROLS END  ------------------------------------------------------------

% Uncover the figure
    
    hndlList=[startBnt exportBnt closeBnt ...
              phiEdt stepEdt inEdt sizeEdt alphaEdt raynumEdt ...
              stepsLbl timeLbl ...
              err_ctrl genRnd ...
             ];
    
    ctmenu(1,figNumber);
    
    set(figNumber,'Visible','on', ...
        'UserData',hndlList);


    watchoff(oldFigNumber);
    figure(figNumber);
    
elseif strcmp(action,'start'),

    axHndl=gca;
    figNumber=gcf;
    hndlList=get(figNumber,'UserData');
    startBnt=hndlList(1);
    exportBnt=hndlList(2);
    closeBnt=hndlList(3);

    phiEdt=hndlList(4); 
    stepEdt=hndlList(5);
    inEdt=hndlList(6);
    sizeEdt=hndlList(7);
    alphaEdt=hndlList(8);
    raynumEdt=hndlList(9); 

    if strcmp(get(err_ctrl, 'Visible'),'on') == 1,
        set(err_ctrl, 'Visible', 'off');
        set(genRnd, 'Visible', 'off');        
    end   
   
    set(startBnt,'Enable','off');
    set(exportBnt,'Enable','on');      
        
    oldFigNumber=watchon;
    
    set(axHndl, ...
        'XLim',[0 40],'YLim',[0 40], ...
       'Userdata',play, ...
        'XTick',[],'YTick',[], ...
        'Drawmode','fast', ...
        'Visible','off', ...
        'NextPlot','add', ...
        'View',[-37.5,30]);

    phiVal = str2num(get(phiEdt, 'String'));
    stepVal = str2num(get(stepEdt, 'String'));
    inVal = str2num(get(inEdt, 'String'));
    sizeVal = str2num(get(sizeEdt, 'String'));
    alphaVal = str2num(get(alphaEdt, 'String'));
    raynumVal = str2num(get(raynumEdt, 'String'));    
    
    set(axHndl, 'Userdata', [get(animChk,'Value') get(traceChk,'Value')])        
        
    % ======= FUNCTION CALL ======
 
%        global Iout;

%        global L;
%        global kI;

        sizeVal
        stepVal
        raynumVal
        L = sparse(stepVal*raynumVal, 4*sizeVal*sizeVal);
        kI = 0;        
              
        Iout = ct_dcalc(phiVal, stepVal, inVal, sizeVal, alphaVal, raynumVal);
        
    % ======= FUNCTION CALL ======
    
    watchoff(oldFigNumber);
    set(startBnt,'Enable','on');
    set(exportBnt,'Enable','on');      

elseif strcmp(action,'load'),

    figNumber=gcf;
    hndlList=get(figNumber,'UserData');

    startBnt = hndlList(1);
    phiEdt=hndlList(4); 
    stepEdt=hndlList(5);
    inEdt=hndlList(6);
    sizeEdt=hndlList(7);
    alphaEdt=hndlList(8);
    raynumEdt=hndlList(9);
    err_ctrl = hndlList(12);
    genRnd = hndlList(13);    

    if strcmp(get(err_ctrl, 'Visible'),'on') == 1,
        set(err_ctrl, 'Visible', 'off');
        set(genRnd, 'Visible', 'off');        
    end   

    load direct.mat
    
    global L;
    global I;
    global E0;
    
    E0 = [];
    
    phiVal = s_vec(1);    
    stepVal = s_vec(2);    
    inVal = s_vec(3);    
    sizeVal = s_vec(4);    
    alphaVal = s_vec(5);    
    raynumVal = s_vec(6);    
        
    set(phiEdt, 'String', num2str(phiVal));
    set(stepEdt, 'String', num2str(stepVal));
    set(inEdt, 'String', num2str(inVal));
    set(sizeEdt, 'String', num2str(sizeVal));
    set(alphaEdt, 'String', num2str(alphaVal));
    set(raynumEdt, 'String', num2str(raynumVal));
    set(startBnt,'Enable','on');    

    disp('direct.mat loaded')
    
elseif strcmp(action,'solve'),
    oldFigNumber=watchon;
    figNumber=gcf;
    hndlList=get(figNumber,'UserData');

    phiEdt=hndlList(4); 
    stepEdt=hndlList(5);
    inEdt=hndlList(6);
    sizeEdt=hndlList(7);
    alphaEdt=hndlList(8);
    raynumEdt=hndlList(9);

    phiVal = str2num(get(phiEdt, 'String'));
    stepVal = str2num(get(stepEdt, 'String'));
    inVal = str2num(get(inEdt, 'String'));
    sizeVal = str2num(get(sizeEdt, 'String'));
    alphaVal = str2num(get(alphaEdt, 'String'));
    raynumVal = str2num(get(raynumEdt, 'String'));    
    
    stepsLbl=hndlList(10);
    timeLbl=hndlList(11);
    err_ctrl = hndlList(12);
    genRnd = hndlList(13);

    if strcmp(get(err_ctrl, 'Visible'),'on') == 1,
        set(err_ctrl, 'Visible', 'off');
        set(genRnd, 'Visible', 'off');        
    end   

    colormap(jet);

    n = sizeVal;

    global I;
    global L;
    global E0;
    
    if size(E0) ~= size(I), E0 = zeros(size(I)); end    
        
    tic;
    TT = L\(I+0.1*E0*get(err_ctrl,'Value'));
    t = toc;
    set(timeLbl, 'String', ['time ' num2str(t,6)]);       

    global T; 
    T = reshape(TT,2*n, 2*n);

    image(0.5-n,0.5-n,(T+0.5)*60);
    hold on, set(gca, 'YDir', 'normal'), hold off
    axis([-n n -n n]);
    axis('square');

    watchoff(oldFigNumber);
    
elseif strcmp(action,'phantom'),
    figNumber=gcf;
    hndlList=get(figNumber,'UserData');
    sizeEdt=hndlList(7);
    stepsLbl=hndlList(10);
    timeLbl=hndlList(11);
    err_ctrl = hndlList(12);
    genRnd = hndlList(13);    

    if strcmp(get(err_ctrl, 'Visible'),'on') == 1,
        set(err_ctrl, 'Visible', 'off');
        set(genRnd, 'Visible', 'off');        
    end   
    
    set(timeLbl, 'String', '');           
    sizeVal = str2num(get(sizeEdt, 'String'));
    n = sizeVal;

    global T;
    image(0.5-n,0.5-n,(T+0.5)*60);
    hold on, set(gca, 'YDir', 'normal'), hold off
    axis([-n n -n n])
    axis('square')
elseif strcmp(action,'coeff'),    
    figNumber=gcf;
    hndlList=get(figNumber,'UserData');
    timeLbl=hndlList(11); 
    err_ctrl = hndlList(12);
    genRnd = hndlList(13);    

    if strcmp(get(err_ctrl, 'Visible'),'on') == 1,
        set(err_ctrl, 'Visible', 'off');
        set(genRnd, 'Visible', 'off');        
    end   
      
    set(timeLbl, 'String', '');               

    global L;
    
    spy(L);
   
elseif strcmp(action,'2ddeviation'),
    figNumber=gcf;
    hndlList=get(figNumber,'UserData');
    sizeEdt=hndlList(7);
    stepsLbl=hndlList(10);
    timeLbl=hndlList(11); 
    err_ctrl = hndlList(12);
    genRnd = hndlList(13);    

    if strcmp(get(err_ctrl, 'Visible'),'on') == 1,
        set(err_ctrl, 'Visible', 'off');
        set(genRnd, 'Visible', 'off');        
    end   
      
    set(timeLbl, 'String', '');               
    sizeVal = str2num(get(sizeEdt, 'String'));
    n = sizeVal;

    global T;
    TT = reshape(T,4*n*n,1);
    SM = reshape(make_scross(n),4*n*n,1);
    plot(SM-TT);

elseif strcmp(action,'3ddeviation'),
    figNumber=gcf;
    hndlList=get(figNumber,'UserData');
    sizeEdt=hndlList(7);
    stepsLbl=hndlList(10);
    timeLbl=hndlList(11); 
    err_ctrl = hndlList(12);
    genRnd = hndlList(13);    
    
    if strcmp(get(err_ctrl, 'Visible'),'on') == 1,
        set(err_ctrl, 'Visible', 'off');
        set(genRnd, 'Visible', 'off');        
    end   
      
    set(timeLbl, 'String', '');               
    sizeVal = str2num(get(sizeEdt, 'String'));
    n = sizeVal;

    global T;
    SC = reshape(make_scross(n),2*n,2*n);
    surf(SC-T);
elseif strcmp(action,'error'),
    figNumber=gcf;
    hndlList=get(figNumber,'UserData');

    timeLbl=hndlList(11);   
    err_ctrl = hndlList(12);
    genRnd = hndlList(13);

    set(timeLbl, 'String', '');
    if strcmp(get(err_ctrl, 'Visible'),'off') == 1,
        set(err_ctrl, 'Visible', 'on');
        set(genRnd, 'Visible', 'on');        
    end

    global I;
    global E0;
    
    if size(E0) ~= size(I), E0 = zeros(size(I)); end

    E = 0.1*get(err_ctrl, 'Value')*E0;
        
    s = size(E);
    plot(E);
    A = axis;
    axis([1 s(1) A(3) A(4)]);
   
end;    % if strcmp(action, ...                                           

% --------------------  END  --------------------------------------------------------------
