% Chad Myers
% ELE 571
% File: snakeGUI.m
% ------------------------
% The snakeGUI file is part of the snakeGUI program for
% demonstrating SNAKE active contours. It contains all of the user
% interface code for the program.  The associated files are:
% generate_ellipse.m, generate_snake.m, and get_delta.m.


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

% Last Modified by GUIDE v2.5 29-Sep-2002 22:11:55

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @snakeGUI_OpeningFcn, ...
                   'gui_OutputFcn',  @snakeGUI_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 snakeGUI is made visible.
function snakeGUI_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 snakeGUI (see VARARGIN)

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

% This sets up the initial plot - only do when we are invisible
% so window can get raised using snakeGUI.
%if strcmp(get(hObject,'Visible'),'off')
 %   plot(rand(5));

 initialize_plot(1);
 
 set(handles.pattern_pts_strng,'String','40');
 set(handles.snake_nodes_strng,'String','20');
 set(handles.elasticity_box,'String','.5');
 set(handles.learn_rate_box,'String','.5');
 set(handles.sigma_box,'String','.5');
 set(handles.iterations_box,'String','20');
 
 % Update handles structure
 guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = snakeGUI_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 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)
axes(handles.axes1);
cla reset;
initialize_plot(1);


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


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

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

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


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

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

set(hObject, 'String', {'plot(rand(5))', 'plot(sin(1:0.01:25))', 'comet(cos(1:.01:10))', 'bar(1:10)', 'plot(membrane)', 'surf(peaks)'});

% --- Executes on selection change in popupmenu3.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3



% --- Executes during object creation, after setting all properties.
function elasticity_box_CreateFcn(hObject, eventdata, handles)
% hObject    handle to elasticity_box (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



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


% --- Executes during object creation, after setting all properties.
function learn_rate_box_CreateFcn(hObject, eventdata, handles)
% hObject    handle to learn_rate_box (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



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


% --- Executes during object creation, after setting all properties.
function sigma_box_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sigma_box (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



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


function initialize_plot(i)
   global ellipse;
   global snake;
   global initial_snake;
   
   ellipse = generate_ellipse(40,0.0,[.5,0],[2,6]);
   snake = generate_snake(20,[.5,0],7);
   initial_snake = snake;
   
   plot(ellipse(1:length(ellipse),1),ellipse(1:length(ellipse),2),'bd','MarkerSize',3);
   hold on;
   plot(snake(1:length(snake),1),snake(1:length(snake),2), '-rp','MarkerSize',1);



% --- 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)
  global ellipse;
  global snake;
  global initial_snake;
  
  elasticity =  str2num(get(handles.elasticity_box,'String'));
  learning_rate =  str2num(get(handles.learn_rate_box,'String'));
  sigma =  str2num(get(handles.sigma_box,'String'));
  iterations = str2num(get(handles.iterations_box,'String'));
  
  warning off MATLAB:divideByZero;
  pause on;

  set(handles.pushbutton2,'Enable','off');
  set(handles.pushbutton1,'Enable','off');
  
  for j=1:iterations
     for i=1:length(snake)
         delta = get_delta(i,snake,ellipse,sigma,learning_rate,elasticity);
         snake(i,1) = snake(i,1) + delta(1);
         snake(i,2) = snake(i,2) + delta(2);
     end
    cla reset;
    plot(ellipse(1:length(ellipse),1),ellipse(1:length(ellipse),2),'bd','MarkerSize',3);
    hold on;
    plot(snake(1:length(snake),1),snake(1:length(snake),2), '-rp','MarkerSize',1);
    hold on;
    plot(initial_snake(1:length(initial_snake),1),initial_snake(1:length(initial_snake),2), '-gp','MarkerSize',1);
    pause(.05);
  end
  
  cla reset;
  plot(ellipse(1:length(ellipse),1),ellipse(1:length(ellipse),2),'bd','MarkerSize',3);
  hold on;
  plot(snake(1:length(snake),1),snake(1:length(snake),2), '-rp','MarkerSize',1);
  hold on;
  plot(initial_snake(1:length(initial_snake),1),initial_snake(1:length(initial_snake),2), '-gp','MarkerSize',1);

  set(handles.pushbutton2,'Enable','on');
  set(handles.pushbutton1,'Enable','on');

% --- Executes during object creation, after setting all properties.
function iterations_box_CreateFcn(hObject, eventdata, handles)
% hObject    handle to iterations_box (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



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


