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

% Last Modified by GUIDE v2.5 30-Dec-2013 21:38:48

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



cd DATA\
List_tmp = ls;
cd ..

DATALIST = cell(1);data_count = 1;
for i=1:size(List_tmp, 1),
    if ~isempty(strfind(List_tmp(i, :), '.mat')), 
        L = List_tmp(i, :);
        t = L(end);
        while ~strcmp(t, 't'),
            L = L(1:(end-1));t = L(end);
        end   
        DATALIST{data_count} = ['DATA\', L];
        if strcmp(L,'99999999_CALIBRATE.mat'), calib_id = data_count;end
        data_count = data_count + 1;
    end
end



s_order = load('time_order.mat');
s_order.time_order = [calib_id, s_order.time_order];
%[12 17 27 40 3 26 33 13 37 42 9 29 30 11 32 19 20 15 21 35 4 36 2 24 41 44 31 7 8 10 18 1 16 28 23 25 34 38 39];
%time_order = 1:length(DATALIST);

for i=1:length(s_order.time_order),
    handles.DATALIST{i} = DATALIST{s_order.time_order(i)};
end

handles.DATALIST;
set(handles.Dataset_slider, 'Max', length(handles.DATALIST));
set(handles.Dataset_slider, 'Min', 1);
set(handles.Dataset_slider, 'Value', 1);
set(handles.Dataset_slider, 'SliderStep', [1/(length(handles.DATALIST)-1) 3/(length(handles.DATALIST)-1)]);
handles.dt = round(get(handles.Dataset_slider, 'Value'));
set(handles.Dataset_disp, 'String', handles.dt);

a = load(handles.DATALIST{handles.dt});
handles.data = a.(handles.DATALIST{handles.dt}(15:(end-4)));

set(handles.Channel_slider, 'Max', handles.data.NumberOfChannel);
set(handles.Channel_slider, 'Min', 1);
set(handles.Channel_slider, 'SliderStep', [1/(handles.data.NumberOfChannel-1) 1/(handles.data.NumberOfChannel-1)]);
set(handles.Channel_slider, 'Value', 1);
set(handles.Channel_disp, 'String', [num2str(get(handles.Channel_slider, 'Value')), '/', num2str(handles.data.NumberOfChannel)]);

handles.channel = round(get(handles.Channel_slider, 'Value'));
set(handles.Z_slider, 'Max', size(handles.data.Image{handles.channel}, 3));
set(handles.Z_slider, 'Min', 1);
set(handles.Z_slider, 'SliderStep', [1/(size(handles.data.Image{handles.channel}, 3)-1) 3/(size(handles.data.Image{handles.channel}, 3)-1)]);
set(handles.Z_slider, 'Value', 1);
set(handles.Z_disp, 'String', num2str(get(handles.Z_slider, 'Value')));


s_tmp = handles.DATALIST{handles.dt};
s_tmp = ['SURFACES\surface_', s_tmp(6:end)];
if exist(s_tmp, 'file')
    load(s_tmp);
    handles.x_surface = x_surf;
    handles.y_surface = y_surf;
    if exist('meso_id', 'var'),
        handles.mesoderm_id = meso_id;
        clear('x_surf', 'y_surf', 'meso_id');
    else
        handles.mesoderm_id = [-1, -1];
        clear('x_surf', 'y_surf');
    end
else
    handles.x_surface = [1,1];
    handles.y_surface = [1,1024];
end

set(handles.add_vertex_but, 'enable', 'off');
set(handles.move_vertex_but, 'enable', 'off');
set(handles.delete_vertex_but, 'enable', 'off');
set(handles.push_left_but, 'enable', 'off');
set(handles.push_right_but, 'enable', 'off');
set(handles.mesoderm_select, 'enable', 'off');

handles.mesoderm_id = [-1, -1];
handles.surface_flag = get(handles.surface_show, 'Value');
axis([1 1024 1 1024]);
Plot_stuff;


% Add listeners to change the picture with changing slider channel and Z
handles.hListener_1 = addlistener(handles.Channel_slider, 'Value','PostSet', @(s, e) plot_things(handles));
handles.hListener_2 = addlistener(handles.Z_slider, 'Value','PostSet', @(s, e) plot_things(handles));

set(handles.Dataset_disp, 'String', [num2str(handles.dt), ' ', handles.DATALIST{handles.dt}]);

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = FIT_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 slider movement.
function Dataset_slider_Callback(hObject, eventdata, handles)
% hObject    handle to Dataset_slider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

handles.dt = round(get(handles.Dataset_slider, 'Value'));
set(handles.Dataset_disp, 'String', [num2str(handles.dt), ' ', handles.DATALIST{handles.dt}]);

a = load(handles.DATALIST{handles.dt});handles.data = a.(handles.DATALIST{handles.dt}(15:(end-4)));

handles.mesoderm_id = [-1, -1];
s_tmp = handles.DATALIST{handles.dt};
s_tmp = ['SURFACES\surface_', s_tmp(6:end)];
if exist(s_tmp, 'file')
    load(s_tmp);
    handles.x_surface = x_surf;
    handles.y_surface = y_surf;
    if exist('meso_id', 'var'),
        handles.mesoderm_id = meso_id;
        clear('meso_id');
    end
    clear('x_surf', 'y_surf');
else
    handles.x_surface = [1,1];
    handles.y_surface = [1,1024];
end

if handles.ch > handles.data.NumberOfChannel,
    handles.ch = handles.data.NumberOfChannel;
    set(handles.Channel_slider, 'Value', handles.ch);
end

if handles.z > size(handles.data.Image{handles.ch}, 3),
    handles.z = size(handles.data.Image{handles.ch}, 3);
    set(handles.Z_slider, 'Value', handles.z);
end

set(handles.Channel_slider, 'Max', handles.data.NumberOfChannel);
set(handles.Channel_slider, 'Min', 1);
set(handles.Channel_slider, 'SliderStep', [1/(handles.data.NumberOfChannel-1) 1/(handles.data.NumberOfChannel-1)]);

handles.channel = round(get(handles.Channel_slider, 'Value'));
set(handles.Z_slider, 'Max', size(handles.data.Image{handles.channel}, 3));
set(handles.Z_slider, 'Min', 1);
set(handles.Z_slider, 'SliderStep', [1/(size(handles.data.Image{handles.channel}, 3)-1) 3/(size(handles.data.Image{handles.channel}, 3)-1)]);

Plot_stuff

delete(handles.hListener_1);
delete(handles.hListener_2);

handles.hListener_1 = addlistener(handles.Channel_slider, 'Value','PostSet', @(s, e) plot_things(handles));
handles.hListener_2 = addlistener(handles.Z_slider, 'Value','PostSet', @(s, e) plot_things(handles));

guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function Dataset_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Dataset_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 slider movement.
function Channel_slider_Callback(hObject, eventdata, handles)
% hObject    handle to Channel_slider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
%Plot_stuff;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function Channel_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Channel_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 slider movement.
function Z_slider_Callback(hObject, eventdata, handles)
% hObject    handle to Z_slider (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
%Plot_stuff;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function Z_slider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to Z_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 place_surface_but.
function place_surface_but_Callback(hObject, eventdata, handles)
% hObject    handle to place_surface_but (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.x_surface = [];
handles.y_surface = [];

[handles.x_surface, handles.y_surface] = ginput;

handles = plot_things(handles);
   
Plot_stuff;

delete(handles.hListener_1);
delete(handles.hListener_2);

handles.hListener_1 = addlistener(handles.Channel_slider, 'Value','PostSet', @(s, e) plot_things(handles));
handles.hListener_2 = addlistener(handles.Z_slider, 'Value','PostSet', @(s, e) plot_things(handles));

s_tmp = handles.DATALIST{handles.dt};
s_tmp = ['SURFACES\surface_', s_tmp(6:(end-4))];
x_surf = handles.x_surface;y_surf = handles.y_surface;
meso_id = handles.mesoderm_id;
save(s_tmp, 'x_surf', 'y_surf', 'meso_id');
clear('x_surf', 'y_surf', 'meso_id');

guidata(hObject, handles);


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

handles.ch = round(get(handles.Channel_slider, 'Value'));set(handles.Channel_slider, 'Value', handles.ch);
handles.z = round(get(handles.Z_slider, 'Value'));set(handles.Z_slider, 'Value', handles.z);


w_in = str2double(get(handles.width_input, 'String'));
Traj = [handles.x_surface, handles.y_surface];

id1 = min(handles.mesoderm_id);id2 = max(handles.mesoderm_id);
Meso_traj = [handles.x_surface(id1:id2), handles.y_surface(id1:id2)];
Traj_tmp = [handles.x_surface(1:id2), handles.y_surface(1:id2)];

Img = handles.data.Image{handles.ch}(:, :, handles.z);

[im_cell_total, im_total] = Unfold_by_surf(Img, Traj, w_in);
[~, Meso_tmp1] = Unfold_by_surf(Img, Meso_traj, w_in);
[~, Meso_tmp2] = Unfold_by_surf(Img, Traj_tmp, w_in);
meso_cut_id2 = size(Meso_tmp2, 2); meso_cut_id1 = meso_cut_id2 - size(Meso_tmp1, 2);

Z_stack_num = size(handles.data.Image{handles.ch}, 3);
im_total_stack = uint8(repmat(zeros(size(im_total)), [1,1,Z_stack_num]));
s = size(im_total_stack);
im_total_reslice = uint8(zeros(s(3), s(2), s(1)));

for z = 1:Z_stack_num,
    Img = handles.data.Image{handles.ch}(:, :, z);
    [~, im_total_stack(:, :, z)] = Unfold_by_surf(Img, Traj, w_in);
    im_total_reslice(z, :, :) = im_total_stack(:, :, z)';
    disp([num2str(round(z/Z_stack_num*100)), ' % done']);
end
im_total_max = uint8(sum(im_total_reslice, 3)/size(im_total_reslice, 3));
im_total_meso_max = im_total_max(:, meso_cut_id1:meso_cut_id2);
s_tmp = handles.DATALIST{handles.dt};
stack_save_file = ['STACKS\stack_',s_tmp(6:(end-4))];
reslice_save_file = ['STACKS\reslice_',s_tmp(6:(end-4))];
max_save_file = ['STACKS\Max_',s_tmp(6:(end-4))];
max_meso_save_file = ['STACKS\Meso_Max_',s_tmp(6:(end-4))];
mat_save_file = ['STACKS\DATA_',s_tmp(6:(end-4)), '.mat'];


save(mat_save_file, 'im_cell_total','im_total', 'im_total_stack', 'im_total_reslice',...
    'im_total_max', 'im_total_meso_max', 'meso_cut_id1', 'meso_cut_id2');

save_as_stack(im_total_stack, stack_save_file);
save_as_stack(im_total_reslice, reslice_save_file);
save_as_stack(im_total_max, max_save_file);
save_as_stack(im_total_meso_max, max_meso_save_file);
figure, hold on;
image(im_total_max), axis equal;

guidata(hObject, handles);



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


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

[x, y] = ginput(2);

[~, id] = min(sqrt((x(1) - handles.x_surface).^2 + (y(1) - handles.y_surface).^2));

handles.x_surface(id) = x(2); handles.y_surface(id) = y(2);

Plot_stuff;

delete(handles.hListener_1);
delete(handles.hListener_2);

handles.hListener_1 = addlistener(handles.Channel_slider, 'Value','PostSet', @(s, e) plot_things(handles));
handles.hListener_2 = addlistener(handles.Z_slider, 'Value','PostSet', @(s, e) plot_things(handles));

s_tmp = handles.DATALIST{handles.dt};
s_tmp = ['SURFACES\surface_', s_tmp(6:(end-4))];
x_surf = handles.x_surface;y_surf = handles.y_surface;
meso_id = handles.mesoderm_id;
save(s_tmp, 'x_surf', 'y_surf', 'meso_id');
clear('x_surf', 'y_surf', 'meso_id');

guidata(hObject, handles);


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

[x, y] = ginput(1);

D = sqrt((handles.x_surface-x).^2 + (handles.y_surface-y).^2);
[D, id] = sort(D);

if id(1) > 1 && id(1) < length(D),
    if id(1) > id(2),
        handles.x_surface = [handles.x_surface(1:id(2)); x; handles.x_surface(id(1):end)];
        handles.y_surface = [handles.y_surface(1:id(2)); y; handles.y_surface(id(1):end)];
    else
        handles.x_surface = [handles.x_surface(1:id(1)); x; handles.x_surface(id(2):end)];
        handles.y_surface = [handles.y_surface(1:id(1)); y; handles.y_surface(id(2):end)];
    end
elseif id(1) == length(D),
    if D(2) >= sqrt((handles.x_surface(id(1)) - handles.x_surface(id(2))).^2 + (handles.y_surface(id(1)) - handles.y_surface(id(2))).^2),
        handles.x_surface = [handles.x_surface;x];handles.y_surface = [handles.y_surface;y];
    else
        handles.x_surface = [handles.x_surface(1:(end-1));x;handles.x_surface(end)];
        handles.y_surface = [handles.y_surface(1:(end-1));y;handles.y_surface(end)];
    end
elseif id(1) == 1,
    if D(2) >= sqrt((handles.x_surface(id(1)) - handles.x_surface(id(2))).^2 + (handles.y_surface(id(1)) - handles.y_surface(id(2))).^2),
        handles.x_surface = [x;handles.x_surface];handles.y_surface = [x;handles.y_surface];
    else
        handles.x_surface = [handles.x_surface(1);x;handles.x_surface(2:end)];
        handles.y_surface = [handles.y_surface(1);y;handles.y_surface(2:end)];
    end
end

Plot_stuff;

delete(handles.hListener_1);
delete(handles.hListener_2);

handles.hListener_1 = addlistener(handles.Channel_slider, 'Value','PostSet', @(s, e) plot_things(handles));
handles.hListener_2 = addlistener(handles.Z_slider, 'Value','PostSet', @(s, e) plot_things(handles));

s_tmp = handles.DATALIST{handles.dt};
s_tmp = ['SURFACES\surface_', s_tmp(6:(end-4))];
x_surf = handles.x_surface;y_surf = handles.y_surface;
meso_id = handles.mesoderm_id;
save(s_tmp, 'x_surf', 'y_surf', 'meso_id');
clear('x_surf', 'y_surf', 'meso_id');

guidata(hObject, handles);


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

[x, y] = ginput(1);
[~, id] = min(sqrt((x - handles.x_surface).^2 + (y - handles.y_surface).^2));

handles.x_surface = [handles.x_surface(1:(id-1));handles.x_surface((id+1):end)];
handles.y_surface = [handles.y_surface(1:(id-1));handles.y_surface((id+1):end)];

Plot_stuff;

delete(handles.hListener_1);
delete(handles.hListener_2);

handles.hListener_1 = addlistener(handles.Channel_slider, 'Value','PostSet', @(s, e) plot_things(handles));
handles.hListener_2 = addlistener(handles.Z_slider, 'Value','PostSet', @(s, e) plot_things(handles));

s_tmp = handles.DATALIST{handles.dt};
s_tmp = ['SURFACES\surface_', s_tmp(6:(end-4))];
x_surf = handles.x_surface;y_surf = handles.y_surface;
meso_id = handles.mesoderm_id;
save(s_tmp, 'x_surf', 'y_surf', 'meso_id');
clear('x_surf', 'y_surf', 'meso_id');

guidata(hObject, handles);


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

handles.dt = round(get(handles.Dataset_slider, 'Value'));

load('time_order.mat');

if handles.dt > 1,
    D_tmp = handles.DATALIST{handles.dt-1};id_tmp = time_order(handles.dt-1);
    handles.DATALIST{handles.dt-1} = handles.DATALIST{handles.dt};
    handles.DATALIST{handles.dt} = D_tmp;
    time_order(handles.dt-1) = time_order(handles.dt);time_order(handles.dt) = id_tmp;
    handles.dt = handles.dt-1;
    set(handles.Dataset_slider, 'Value', round(handles.dt));
    set(handles.Dataset_disp, 'String', [num2str(handles.dt), ' ', handles.DATALIST{handles.dt}]);
end

save time_order time_order;

guidata(hObject, handles);


% --- Executes on button press in push_right_but.
function push_right_but_Callback(hObject, eventdata, handles)
% hObject    handle to push_right_but (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.dt = round(get(handles.Dataset_slider, 'Value'));

load('time_order.mat');

if handles.dt < length(handles.DATALIST), 
    D_tmp = handles.DATALIST{handles.dt+1};id_tmp = time_order(handles.dt+1);
    handles.DATALIST{handles.dt+1} = handles.DATALIST{handles.dt};
    handles.DATALIST{handles.dt} = D_tmp;
    time_order(handles.dt+1) = time_order(handles.dt);time_order(handles.dt) = id_tmp;
    handles.dt = handles.dt+1;
    set(handles.Dataset_slider, 'Value', round(handles.dt));
    set(handles.Dataset_disp, 'String', [num2str(handles.dt), ' ', handles.DATALIST{handles.dt}]);
end

save time_order time_order;

guidata(hObject, handles);


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

if strcmp(get(handles.push_left_but, 'enable'), 'off'),
    set(handles.push_left_but, 'enable', 'on');
    set(handles.push_right_but, 'enable', 'on');
else
    set(handles.push_left_but, 'enable', 'off');
    set(handles.push_right_but, 'enable', 'off');
end

guidata(hObject, handles);


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

if strcmp(get(handles.add_vertex_but, 'enable'), 'off') && ~isempty(handles.x_surface),
    set(handles.add_vertex_but, 'enable', 'on');
    set(handles.move_vertex_but, 'enable', 'on');
    set(handles.delete_vertex_but, 'enable', 'on');
    set(handles.mesoderm_select, 'enable', 'on');
else
    set(handles.add_vertex_but, 'enable', 'off');
    set(handles.move_vertex_but, 'enable', 'off');
    set(handles.delete_vertex_but, 'enable', 'off');
    set(handles.mesoderm_select, 'enable', 'off');
end

guidata(hObject, handles);


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

[x, y] = ginput(2);
[~, handles.mesoderm_id(1)] = min(sqrt((x(1) - handles.x_surface).^2 + (y(1) - handles.y_surface).^2));
[~, handles.mesoderm_id(2)] = min(sqrt((x(2) - handles.x_surface).^2 + (y(2) - handles.y_surface).^2));

Plot_stuff;

delete(handles.hListener_1);
delete(handles.hListener_2);

handles.hListener_1 = addlistener(handles.Channel_slider, 'Value','PostSet', @(s, e) plot_things(handles));
handles.hListener_2 = addlistener(handles.Z_slider, 'Value','PostSet', @(s, e) plot_things(handles));

s_tmp = handles.DATALIST{handles.dt};
s_tmp = ['SURFACES\surface_', s_tmp(6:(end-4))];
x_surf = handles.x_surface;y_surf = handles.y_surface;
meso_id = handles.mesoderm_id;
save(s_tmp, 'x_surf', 'y_surf', 'meso_id');
clear('x_surf', 'y_surf', 'meso_id');

guidata(hObject, handles);


% --- Executes on button press in surface_show.
function surface_show_Callback(hObject, eventdata, handles)
% hObject    handle to surface_show (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 surface_show
handles.surface_flag = get(handles.surface_show, 'Value');

guidata(hObject, handles);