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

% Last Modified by GUIDE v2.5 28-May-2012 21:26:59

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


load('V_DATA');
load('V_CELL');

%Vx = V_x_o; Vz = V_y_o; Vy = V_z_o;

handles.Vx = Vx;
handles.Vy = Vy;
handles.Vz = Vz;

handles.Nx = Nx;
handles.Ny = Ny;

handles.Nc = Nc;
handles.Nv = Nv;

handles.x_start = x_start;
handles.x_stop = x_stop;

handles.id = 1;
handles.sel = [1, 1+Nv/4];

handles.V_cell = V_cell;
handles.REF = [Vx; Vy; Vz];

axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);





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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = BACKTRACKER_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 up_button.
function up_button_Callback(hObject, eventdata, handles)
% hObject    handle to up_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

value = str2double(get(handles.move_value, 'String'));
handles.Vy = handles.Vy + value;
axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);
guidata(hObject, handles);


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

value = str2double(get(handles.move_value, 'String'));
handles.Vy = handles.Vy - value;
axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);
guidata(hObject, handles);

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

value = str2double(get(handles.move_value, 'String'));
handles.Vx = handles.Vx - value;
axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);
guidata(hObject, handles);

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

value = str2double(get(handles.move_value, 'String'));
handles.Vx = handles.Vx + value;
axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);
guidata(hObject, handles);


function move_value_Callback(hObject, eventdata, handles)
% hObject    handle to move_value (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 move_value as text
%        str2double(get(hObject,'String')) returns contents of move_value as a double


% --- Executes during object creation, after setting all properties.
function move_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to move_value (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 scl_button.
function scl_button_Callback(hObject, eventdata, handles)
% hObject    handle to scl_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

value = str2double(get(handles.scale_value, 'String'));
handles.Vx = handles.Vx*value;
handles.Vy = handles.Vy*value;
handles.Vz = handles.Vz*value;
axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);
guidata(hObject, handles);


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

Vx = handles.Vx;
Vy = handles.Vy;
Vz = handles.Vz;

Nx = handles.Nx;
Ny = handles.Ny;
Nc = handles.Nc;
Nv = handles.Nv;

x_start = handles.x_start;
x_stop = handles.x_stop;

handles.V_cell{handles.id} = [Vx; Vy; Vz];

V_cell = handles.V_cell;
save('V_DATA', 'Vx', 'Vy', 'Vz', 'Nx', 'Ny', 'Nc', 'Nv', 'x_start', 'x_stop');
save('V_CELL', 'V_cell');


guidata(hObject, handles);

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

handles.Vx = handles.REF(1, :);
handles.Vy = handles.REF(2, :);
handles.Vz = handles.REF(3, :);

axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);

guidata(hObject, handles);

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

handles.REF = [handles.Vx;handles.Vy; handles.Vz];

guidata(hObject, handles);


function scale_value_Callback(hObject, eventdata, handles)
% hObject    handle to scale_value (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 scale_value as text
%        str2double(get(hObject,'String')) returns contents of scale_value as a double


% --- Executes during object creation, after setting all properties.
function scale_value_CreateFcn(hObject, eventdata, handles)
% hObject    handle to scale_value (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 NEXT_button.
function NEXT_button_Callback(hObject, eventdata, handles)
% hObject    handle to NEXT_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.id = handles.id + 1; 
if handles.id < 1, handles.id = 1; end
if handles.id > 26, handles.id = 26; end
set(handles.id_disp, 'String', num2str(handles.id));
load('V_CELL.mat');
handles.V_cell{handles.id} = V_cell{handles.id};
if size(handles.V_cell{handles.id}, 1) > 0,
    handles.Vx = handles.V_cell{handles.id}(1, :);
    handles.Vy = handles.V_cell{handles.id}(2, :);
    handles.Vz = handles.V_cell{handles.id}(3, :);
end
axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);
guidata(hObject, handles);


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

handles.id = handles.id - 1; 
if handles.id < 1, handles.id = 1; end
if handles.id > 26, handles.id = 26; end
set(handles.id_disp, 'String', num2str(handles.id));
load('V_CELL.mat');
handles.V_cell{handles.id} = V_cell{handles.id};
if size(handles.V_cell{handles.id}, 1) > 0,
    handles.Vx = handles.V_cell{handles.id}(1, :);
    handles.Vy = handles.V_cell{handles.id}(2, :);
    handles.Vz = handles.V_cell{handles.id}(3, :);
end
axes(handles.axes1);
set(handles.axes1, 'nextplot' ,'replace');
Plot_Image;
set(handles.axes1, 'nextplot' ,'add');
Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);

guidata(hObject, handles);


% --- Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to axes1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

beep;

guidata(hObject, handles);



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

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in move_vert_button.
function move_vert_button_Callback(hObject, eventdata, handles)
% hObject    handle to move_vert_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.msg_disp, 'String', 'Press top right to toggle move');
[x, y] = ginput(1);

if (x > 13 && y > 13)
    set(handles.msg_disp, 'String', 'Move is ON');
    
    while(1)
        [x, y] = ginput(1);
        if (x > 13 && y > 13), break; end
        R_tmp = sqrt((handles.Vx - x).^2 + (handles.Vy - y).^2);
        handles.sel = find(R_tmp == min(R_tmp), 2, 'first');
        set(handles.sel_disp, 'String', num2str(handles.sel));
        
        axes(handles.axes1);
        set(handles.axes1, 'nextplot' ,'replace');
        Plot_Image;
        set(handles.axes1, 'nextplot' ,'add');
        Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
        xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);
        
        [x, y] = ginput(1);
        if (x > 13 && y > 13), break; end
        handles.Vx(handles.sel(1)) = x;
        handles.Vx(handles.sel(2)) = x;
        
        handles.Vy(handles.sel(1)) = y;
        handles.Vy(handles.sel(2)) = y;
        
        axes(handles.axes1);
        set(handles.axes1, 'nextplot' ,'replace');
        Plot_Image;
        set(handles.axes1, 'nextplot' ,'add');
        Plot_Vert_Multi(handles.Vx, handles.Vy, handles.Vz, handles.Nx, handles.Ny, handles.x_start, handles.x_stop, handles.sel);
        xlabel('X', 'Fontsize', 20);zlabel('Z', 'Fontsize', 20);
    end
end



guidata(hObject, handles);
