function varargout = Trace(varargin)

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

% Last Modified by GUIDE v2.5 19-Aug-2005 10:42:34

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

% Choose default command line output for Trace


handles.output = hObject;

% Update handles structure
guidata(hObject, handles);


if nargin ==4
    handles.imdata = varargin{1};
    guidata(hObject, handles);
end;

initialize_gui(hObject, eventdata, handles);
% guidata(hObject, handles);


 

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




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

OpenDataCall(hObject, eventdata, handles);


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

SaveTraceImage_Callback(hObject, eventdata, handles)

% 
%     
% if handles.trace_flag == 0;
%      errordlg('No trace to save','Error');
% else
%     [file,pathloc] = uiputfile('*.txt','Save Trace');
%     if ~isequal(file, 0)
%         r = handles.r(1:handles.vertex_count,:);
%         save([pathloc,file],'r','-ascii');
%     end;
% end;

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


% pixelsize = str2double(get(hObject, 'String'));
% if isnan(pixelsize)
%     set(hObject, 'String', 0);
%     errordlg('Input must be a number','Error');
% end

pixelsize_old = handles.pixelsize;

[handles.pixelsize,rescale_flag]=PixelsizeDialog(handles.pixelsize);

scale = handles.pixelsize/pixelsize_old;

if rescale_flag
% Save the new volume value
    handles.stepsize = handles.stepsize*scale;
    handles.r_hit         =     handles.r_hit*scale;
    handles.explore_range =     handles.explore_range*scale;
end;

handles.r = handles.r*scale;
if isfield(handles,'endpoint')
    handles.endpoint = handles.endpoint*scale;
end;
handles.contourlength = handles.contourlength*scale;


figure(handles.figure_handle);
xa = xlim;
ya = ylim;
axis([xa,ya]*scale);


if isfield( handles, 'width' );
    handles.width.r1 = scale*handles.width.r1;
    handles.width.r2 = scale*handles.width.r2;
end;


guidata(hObject,handles);
DrawDataCall(hObject, eventdata, handles);
UpdateStepSize();
UpdateContourLength();



% --- Executes during object creation, after setting all properties.
function PixelSizeEdit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PixelSizeEdit (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 ZoomInButton.
function ZoomInButton_Callback(hObject, eventdata, handles)
% hObject    handle to ZoomInButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 
% CommandCall(hObject, eventdata, handles,'Choose zoom location');
% [x,y] = ginput(1);
% xa = xlim();
% ya = ylim();
% deltax = (xa(2)-xa(1))/4;
% deltay = (xa(2)-xa(1))/4;
% axis([x-deltax,x+deltax,y-deltay,y+deltay]);
% %axis equal;
% CommandCall(hObject, eventdata, handles,'');
% 
% AdjustScrollBar(hObject, eventdata, handles);


% --- Executes on button press in ZoomOutButton.
function ZoomOutButton_Callback(hObject, eventdata, handles)
% hObject    handle to ZoomOutButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% 
% 
% xa = xlim();
% ya = ylim();
% deltax = (xa(2)-xa(1));
% deltay = (xa(2)-xa(1));
% axis([mean(xa)-deltax,mean(xa)+deltax,mean(ya)-deltay,mean(ya)+deltay]);
% %axis equal;
% 
% 
% AdjustScrollBar(hObject, eventdata, handles);

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


InitTraceCall(hObject, eventdata, handles);


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

TraceStepCall(hObject, eventdata, handles);

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


stepsize = str2double(get(hObject, 'String'));
if isnan(stepsize)
    set(hObject, 'String', 0);
    errordlg('Input must be a number','Error');
end

% Save the new volume value
handles.stepsize = stepsize;
guidata(hObject,handles)



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

RemoveStepCall(hObject, eventdata, handles);



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




AddAnchorCall(hObject, eventdata, handles);

% --- Executes on slider movement.
function XSlider_Callback(hObject, eventdata, handles)
% hObject    handle to XSlider (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

xs = get(hObject,'Value');
xsmin = get(hObject,'Min');
xsmax = get(hObject,'Max');

xs = (xs-xsmin)/(xsmax-xsmin);

xa = xlim();

DeltaX = xa(2)-xa(1);

A = (handles.im_size_x_pixel*handles.pixelsize)+DeltaX;
B = -DeltaX;

xmin = A*xs+B;
xmax = xmin+DeltaX;
xlim([xmin,xmax]);




% --- Executes during object creation, after setting all properties.
function XSlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to XSlider (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 YSlider_Callback(hObject, eventdata, handles)
% hObject    handle to YSlider (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



ys = get(hObject,'Value');
ysmin = get(hObject,'Min');
ysmax = get(hObject,'Max');

ys = 1-(ys-ysmin)/(ysmax-ysmin);

ya = ylim();

DeltaY = ya(2)-ya(1);

A = (handles.im_size_y_pixel*handles.pixelsize)+DeltaY;
B = -DeltaY;

ymin = A*ys+B;
ymax = ymin+DeltaY;
ylim([ymin,ymax]);






% --- Executes during object creation, after setting all properties.
function YSlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to YSlider (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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function OpenDataCall(hObject, eventdata, handles)


[file,pathloc] = uigetfile({'*.mat';'*.tiff';'*.tif';'*.txt'});
if ~isequal(file, 0)
    str_length = max(size(file));
    
    if str_length < 3
        errordlg('File name too short.','Error');
    else
        flag = 1;
        switch file((str_length-2):str_length)
        
            case 'tif'
                handles.imdata = imread([pathloc,file]);
            case 'iff'
                handles.imdata = imread([pathloc,file]);
            case 'txt'
                 handles.imdata = load([pathloc,file]);
            case 'mat'
                handles2 = load([pathloc,file]);
                
                flag = 2;
               handles.vertex_count      = handles2.handles.vertex_count;
               handles.max_vertex_count  = handles2.handles.max_vertex_count;
               handles.endpoint          = handles2.handles.endpoint;
               handles.choose_range_flag = handles2.handles.choose_range_flag;
               handles.r                 = handles2.handles.r;
               if isfield(handles,'axis_y')
                handles.axis_y            = handles2.handles.axis_y;
               end;
               handles.open_flag         = handles2.handles.open_flag; 
               handles.trace_flag        = handles2.handles.trace_flag ;
               handles.t                 = handles2.handles.t;
               handles.stepsize          = handles2.handles.stepsize;
               handles.pixelsize         = handles2.handles.pixelsize;
               handles.axis_of_sym_flag  = handles2.handles.axis_of_sym_flag;
               handles.axis_of_sym_flag_show = handles2.handles.axis_of_sym_flag_show;
               handles.axis_image_flag   = handles2.handles.axis_image_flag;
               handles.contourlength     = handles2.handles.contourlength;
               handles.show_forces_flag  = handles2.handles.show_forces_flag;
               handles.stepsize          = handles2.handles.stepsize;
               handles.max_iter          = handles2.handles.max_iter;
               handles.interp_cell_number= handles2.handles.interp_cell_number;
               handles.explore_range     = handles2.handles.explore_range;
               handles.r_hit             = handles2.handles.r_hit;
               handles.colormap          = handles2.handles.colormap;
               handles.force             = handles2.handles.force;
               handles.imdata            = handles2.handles.imdata;
               handles.im                = handles2.handles.im;
               handles.choose_range_min  = handles2.handles.choose_range_min;
               handles.choose_range_max  = handles2.handles.choose_range_max;
               handles.im_size_y_pixel   = handles2.handles.im_size_y_pixel; 
               handles.im_size_x_pixel   = handles2.handles.im_size_x_pixel;
               

               handles.force.tension
               handles2.handles.force.tension
               
               if isfield(handles2.handles,'width');
                       handles.width.r1 = handles2.handles.width.r1;
                       handles.width.r2 = handles2.handles.width.r2;
               end;
               
            otherwise
                 errordlg(['cannot load ', file((str_length-2):str_length)],'Error');
                 flag  = 0;
        end;

    end;
    if flag ==1
        [handles.im_size_y_pixel,handles.im_size_x_pixel] = size(handles.imdata);
        handles.im = double(handles.imdata);

        handles.open_flag = 1;

        handles.choose_range_min = min(min(handles.imdata));
        handles.choose_range_max = max(max(handles.imdata));

        guidata(hObject, handles);
     
        DrawDataCall(hObject, eventdata, handles);
        %handles = guidata(hObjects);
        ActivateScrollBar(hObject, eventdata, handles);
    elseif flag ==2
        guidata(hObject, handles);     
        DrawDataCall(hObject, eventdata, handles);
        %handles = guidata(hObjects);
    end;
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function DrawDataCall(hObject, eventdata, handles)

if ~handles.figure_flag
    handles.figure_handle = figure();
    handles.figure_flag = 1;
end;

figure(handles.figure_handle);

if handles.draw_flag
    xa = xlim();
    ya = ylim();
end;

cla;
x=((1:handles.im_size_x_pixel))*handles.pixelsize;
y=((1:handles.im_size_y_pixel))*handles.pixelsize;


if handles.choose_range_flag == 0
    imagesc(x,y,handles.imdata);
else
    imagesc(x,y,handles.imdata,[handles.choose_range_min,handles.choose_range_max]);
end;
colormap( handles.colormap );

hold on;

if handles.draw_flag == 0;
    axis equal
else    
    axis([xa,ya]);
end;

handles.draw_flag = 1;
guidata(hObject, handles);

if handles.trace_flag == 1;
    i = handles.vertex_count;
    r = handles.r;
    plot(r(1:i,1),r(1:i,2),'.k');
    plot(r(i,1)+[0,handles.t(1)]*handles.stepsize,r(i,2)+[0,handles.t(2)]*handles.stepsize,'w');
end;

if isfield(handles,'endpoint')
    plot(handles.endpoint(1),handles.endpoint(2),'wx');
end;


if handles.axis_of_sym_flag && handles.axis_of_sym_flag_show
    plot([0,handles.im_size_x_pixel]*handles.pixelsize,[0 0]+handles.axis_y,'--r');
end;

if (handles.axis_image_flag && handles.trace_flag && handles.axis_of_sym_flag);
    i = handles.vertex_count;
    r = handles.r;
    plot(r(1:i,1),2*handles.axis_y-r(1:i,2),'.r');
end;

if isfield( handles, 'width' );
    num = size(handles.width.r1);
    num = num(1)
    for i = 1:num
        x = [handles.width.r1(i,1),handles.width.r2(i,1)];
        y = [handles.width.r1(i,2),handles.width.r2(i,2)];
        plot( x,y,'g');
    end;
else
end;

drawnow;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function InitTraceCall(hObject, eventdata, handles)
 
%handles

if ~handles.open_flag;
       errordlg('Image must be open.','Error');
else 

handles.trace_flag = 0;
guidata(hObject, handles);

DrawDataCall(hObject, eventdata, handles);

CommandCall(hObject, eventdata, handles,'Click for initial vertex position');
figure(handles.figure_handle);
[x,y] = ginput(1);
plot(x,y,'w.');
r = [x,y];
handles.r(1,:) =r;
guidata(hObject, handles);

CommandCall(hObject, eventdata, handles,'Click for initial tangent direction');
[x,y] = ginput(1);
plot([r(1,1),x],[r(1,2),y],'w');
tt = [x,y] - r(1,:);
handles.t = tt/sqrt(sum(tt.*tt));

handles.trace_flag = 1;
handles.vertex_count = 1;
handles.contourlength = 0;

plot(r(1),r(2),'k.');

guidata(hObject, handles);
CommandCall(hObject, eventdata, handles,'');

%handles
end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function CommandCall(hObject, eventdata, handles,command_name)

set(handles.command_text, 'String', command_name);
guidata(hObject, handles);


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

function DoTraceStep(hObject, eventdata, handles)

%'eval'
c = handles.vertex_count;
    
    [handles.r(c+1,:),handles.t] = TraceStep(handles.r(c,:),handles.t,handles.im',handles.stepsize,handles.explore_range,handles.interp_cell_number,handles.max_iter,handles.pixelsize);
%  handles.r(c+1,1) = handles.r(c,1)+handles.stepsize*handles.t(1);
%  handles.r(c+1,2) = handles.r(c,2)+handles.stepsize*handles.t(2);
 

 handles.vertex_count = c+1;
 handles.contourlength = handles.contourlength+handles.stepsize;
 guidata(hObject, handles);

 
 %handles.vertex_count 
 DrawDataCall(hObject, eventdata, handles);
 %handles.vertex_count 
 
 
%     
%     'dotrace'
%     handles.vertex_count
    
%     plot(handles.r(c+1,1),handles.r(c+1,2),'k.');
%     drawnow;

    
    
UpdateContourLength(hObject, eventdata, handles);

function TraceStepCall(hObject, eventdata, handles)


%handles 

if handles.trace_flag == 0;
    errordlg('Need to init a trace.','Error');
elseif handles.vertex_count == handles.max_vertex_count
    errordlg('Max vertex number reached. This can be changed in trace preferences.','Error');    
else
   DoTraceStep(hObject, eventdata, handles);
   %DrawDataCall(hObject, eventdata, handles);
end;



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




function RemoveStepCall(hObject, eventdata, handles)

switch(handles.vertex_count)
    case 0;
        errordlg('No trace.','Error');
    case 1;
        handles.vertex_count = 0;
        handles.trace_flag   = 0;
    case 2;
        handles.vertex_count = handles.vertex_count-1;
        handles.contourlength = handles.contourlength-norm(handles.r(handles.vertex_count+1,:)-handles.r(handles.vertex_count,:));
    otherwise;
        handles.vertex_count = handles.vertex_count-1;
        handles.t = handles.r(handles.vertex_count,:)-handles.r(handles.vertex_count-1,:);
        handles.t = handles.t/norm(handles.t);
        handles.contourlength = handles.contourlength-norm(handles.r(handles.vertex_count+1,:)-handles.r(handles.vertex_count,:));
        
end;

guidata(hObject, handles);
DrawDataCall(hObject, eventdata, handles);
        
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function initialize_gui(hObject, eventdata, handles)
% If the metricdata field is present and the reset flag is false, it means
% we are we are just re-initializing a GUI by calling it from the cmd line
% while it is up. So, bail out as we dont want to reset the 

%'init called'


%handles

set(handles.command_text, 'String', '');

handles.vertex_count = 0;
handles.max_vertex_count  = 1000;

handles.endpoint = [-300,-300];

handles.figure_flag       = 0;
handles.break_flag        = 0;
handles.trace_to_end_flag = 0;
handles.scroll_bar_flag   = 0;
handles.choose_range_flag = 0;
handles.r            = zeros(handles.max_vertex_count,2);
handles.open_flag    = 0;
handles.trace_flag   = 0;
handles.t            = [0,0];
handles.stepsize     = 4;
handles.pixelsize    = 1;
handles.draw_flag    = 0;
handles.axis_of_sym_flag = 0;
handles.axis_of_sym_flag_show = 0;
handles.axis_image_flag = 0;
handles.contourlength = 0;
handles.show_forces_flag = 0;


handles.stepsize           = 4;
handles.max_iter           = 3;
handles.interp_cell_number = 10;
handles.explore_range      = 6;
handles.r_hit              = 10;

handles.colormap  = 'default';

handles.force.tension  = 0.0;
handles.force.pressure = 0.0;
handles.force.spont    = 0.0;
handles.force.normal_sign = 1;
handles.force.stepsize = 10;
handles.force.r = [0 0];

handles.force.mesh.angle_number = 30;

guidata(hObject, handles);


%handles

if isfield(handles,'imdata')
  OpenPassedData(hObject, eventdata, handles);
end;

UpdateStepSize(hObject, eventdata, handles)
UpdateContourLength(hObject, eventdata, handles)




% Update handles structure

function AddAnchor_Callback(hObject, eventdata, handles);
if handles.trace_flag == 0;
    errordlg('Need to init a trace.','Error'); 
elseif handles.vertex_count == handles.max_vertex_count
    errordlg('Max vertex number reached. This can be changed in trace preferences.','Error');    
else
    CommandCall(hObject, eventdata, handles,'Click in next vertex direction');
   
    figure(handles.figure_handle);
    [x,y] = ginput(1);
    r = [x,y];

    
    handles = guidata(hObject);

    t = r-handles.r(handles.vertex_count,:);
    t = t/norm(t);
    r = handles.r(handles.vertex_count,:)+handles.stepsize*t;
    vertex_count = handles.vertex_count+1;

    handles = guidata(hObject);
    handles.t = t;
    handles.r(vertex_count,:) = r;
    handles.vertex_count = vertex_count;
    guidata(hObject,handles);


    CommandCall(hObject, eventdata, handles,'');
    DrawDataCall(hObject, eventdata, handles);
    UpdateContourLength(hObject, eventdata, handles);
end

%handles


% --- Executes when figure1 is resized.
function figure1_ResizeFcn(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)



% Check for "enter" or "escape"
% if isequal(get(hObject,'CurrentKey'),'escape')
%     % User said no by hitting escape
%     handles.output = 'No';
%     
%     % Update handles structure
%     guidata(hObject, handles);
%     
%     uiresume(handles.figure1);
% end    
%     
% if isequal(get(hObject,'CurrentKey'),'return')
%     uiresume(handles.figure1);
% end    


% --- Executes on key press over figure1 with no controls selected.
function figure1_KeyPressFcn(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)

keydown = get(hObject,'CurrentKey');

%keydown

switch keydown;
    case 'equal'
        TraceStepCall(hObject, eventdata, handles);
    case 'hyphen'
        RemoveStepCall(hObject, eventdata, handles);
    case 'leftbracket'
        ZoomInButton_Callback(hObject, eventdata, handles);
    case 'rightbracket'
        ZoomOutButton_Callback(hObject, eventdata, handles);
    case 'b'
        BreakButton_Callback(hObject, eventdata, handles);
end;


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


% --------------------------------------------------------------------
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 DefineAxisOfSymMenu_Callback(hObject, eventdata, handles)
% hObject    handle to DefineAxisOfSymMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)



handles.axis_of_sym_flag = 0;
guidata(hObject, handles);

DrawDataCall(hObject, eventdata, handles);

handles.axis_of_sym_flag = 1;
guidata(hObject, handles);

CommandCall(hObject, eventdata, handles,'Click on y position of axis.');
[x,y] = ginput(1);
plot(x,y,'r.');
r = [x,y];
handles.axis_y =r(2);
guidata(hObject, handles);

CommandCall(hObject, eventdata, handles,'');

ShowAxisMenu_Callback(hObject, eventdata, handles);




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

handles.axis_image_flag = 1;
guidata(hObject, handles);

DrawDataCall(hObject, eventdata, handles);



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


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


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


if ~handles.trace_flag;
     errordlg('No trace to analyze','Error');
elseif ~handles.axis_of_sym_flag
     errordlg('Axis of symetry must be set.','Error');
else
%     
%         figure(1);
%         clf;
        r = handles.r(1:handles.vertex_count,:);
        x = abs(r(:,2)-handles.axis_y);
        y = r(:,1);
        handles.force.r = [x,y];
        guidata(hObject, handles);

                [handles.force] = ForceDialog(handles.force);

%         forcecalc11(0,0,0,r);

guidata(hObject, handles);

end;




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


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

if handles.open_flag == 0
        errordlg('File must be open to open colors.','Error'); 
else
    [handles.choose_range_flag,handles.choose_range_max,handles.choose_range_min,handles.colormap] = ColorDialog(handles.choose_range_flag,handles.choose_range_max,handles.choose_range_min,handles.colormap,handles.im);
    guidata(hObject, handles);
    
    
    handles.colormap
    DrawDataCall(hObject, eventdata, handles);

end;



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


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

old_max_vertex_count = handles.max_vertex_count;
[handles.stepsize,handles.max_vertex_count,handles.max_iter,handles.interp_cell_number,handles.explore_range,handles.r_hit]=TracePreferenceDialog(handles.stepsize,handles.max_vertex_count,handles.max_iter,handles.interp_cell_number,handles.explore_range,handles.r_hit);

if(old_max_vertex_count ~= handles.max_vertex_count)
    r_old = handles.r;
    handles.r = zeros(handles.max_vertex_count,2);
    looplength = min(handles.vertex_count,handles.max_vertex_count);            
    handles.r(1:looplength,:) = r_old(1:looplength,:);
    guidata(hObject, handles);
    if handles.draw_flag == 1
        DrawDataCall(hObject, eventdata, handles); 
    end;
else
    guidata(hObject, handles);
end;

UpdateStepSize(hObject, eventdata, handles);


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


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


ViewHistDialog(handles.r(1:handles.vertex_count,:));

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



pixelsize_old = handles.pixelsize;

[handles.pixelsize,rescale_flag]=PixelsizeDialog(handles.pixelsize);

scale = handles.pixelsize/pixelsize_old;

if rescale_flag
% Save the new volume value
    handles.stepsize = handles.stepsize*scale;
    handles.r_hit         =     handles.r_hit*scale;
    handles.explore_range =     handles.explore_range*scale;
end;

handles.r = handles.r*scale;
if isfield(handles,'endpoint')
    handles.endpoint = handles.endpoint*scale;
end;
handles.contourlength = handles.contourlength*scale;

figure(handles.figure_handle);
xa = xlim;
ya = ylim;
axis([xa,ya]*scale);

if isfield(handles, 'axis_y')
    handles.axis_y = handles.axis_y*scale;
end;


if isfield( handles, 'width' );
    handles.width.r1 = scale*handles.width.r1;
    handles.width.r2 = scale*handles.width.r2;
end;


guidata(hObject,handles);
DrawDataCall(hObject, eventdata, handles);
UpdateStepSize(hObject, eventdata, handles);
UpdateContourLength(hObject, eventdata, handles);



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


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


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


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


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


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


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







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


SetEndPoint_Callback(hObject, eventdata, handles);

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


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

SetEndPoint_Callback(hObject, eventdata, handles);

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

if handles.trace_flag == 0 
    errordlg('Must init the trace first.','Error');
%elseif isfield( handles,'endpoint' ) ==0
%
%    isfield( handles,'endpoint' )
%    errordlg('The end point must be set to use this function.','Error');
else
   DoTraceToEndPoint( hObject, eventdata, handles);
   
end;

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


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


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


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


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


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

TraceHelpDialog();

function UpdateContourLength(hObject, eventdata, handles);

set(handles.ContourLengthMeter,'String',sprintf('Contour Length\n%g um', handles.contourlength));

function UpdateStepSize(hObject, eventdata, handles);

set(handles.StepSizeMeter,'String',sprintf('Step Size\n%g um', handles.stepsize));


function HomeView_Callback(hObject, eventdata, handles)

if handles.draw_flag ==1;
    
    axis([0,handles.im_size_x_pixel,0,handles.im_size_y_pixel]*handles.pixelsize);
    axis equal;
    AdjustScrollBar(hObject, eventdata, handles);
    
end

function ActivateScrollBar(hObject, eventdata, handles)

% set(handles.XSlider,'Visible','on');
% set(handles.YSlider,'Visible','on');
% 
% AdjustScrollBar(hObject, eventdata, handles);

function AdjustScrollBar(hObject, eventdata, handles)

% 
% xs    = get(handles.XSlider,'Value')
% xsmin = get(handles.XSlider,'Min')
% xsmax = get(handles.handles.YSlider,'Max')
% 
% ys    = get(handles.YSlider,'Value')
% ysmin = get(handles.YSlider,'Min')
% ysmax = get(handles.YSlider,'Max')


% xa = xlim();
% DeltaX = xa(2)-xa(1);
% A = (handles.im_size_x_pixel*handles.pixelsize)+DeltaX;
% B = -DeltaX;
% 
% x = (xa(1)-B)/A;
% 
% x=max(x,0);
% x=min(x,1);
% 
% ya = ylim();
% DeltaY = ya(2)-ya(1);
% A = (handles.im_size_y_pixel*handles.pixelsize)+DeltaY;
% B = -DeltaY;
% y = 1-(ya(1)-B)/A;
% 
% y=max(y,0);
% y=min(y,1);
% 
% set(handles.XSlider,'Value',x);
% XSlider_Callback(handles.XSlider, eventdata, handles);
% 
% set(handles.YSlider,'Value',y);
% YSlider_Callback(handles.YSlider, eventdata, handles);


function OpenPassedData(hObject, eventdata, handles);


if size(handles.imdata)~=0
%     handles.imdata = datain;
    [handles.im_size_y_pixel,handles.im_size_x_pixel] = size(handles.imdata);
    handles.im = double(handles.imdata);

    handles.open_flag = 1;

    handles.choose_range_min = min(min(handles.imdata));
    handles.choose_range_max = max(max(handles.imdata));

    guidata(hObject, handles);
     
    DrawDataCall(hObject, eventdata, handles);
    ActivateScrollBar(hObject, eventdata, handles);

end;


function SetEndPoint_Callback(hObject, eventdata, handles);



%'called' 

CommandCall(hObject, eventdata, handles,'Click for endpoint position');

figure(handles.figure_handle);
[x,y] = ginput(1);
% plot(x,y,'wx');
r = [x,y];
handles.endpoint =r;
guidata(hObject, handles);
CommandCall(hObject, eventdata, handles,'');
DrawDataCall(hObject, eventdata, handles);
%handles




function DoTraceToEndPoint(hObject, eventdata, handles)

c    = handles.vertex_count;
cmax = handles.max_vertex_count;

r    = handles.r;
r_current = r(c,:);
t    = handles.t;
endpoint = handles.endpoint;
r_hit = handles.r_hit;
contourlength = handles.contourlength;
handles.trace_to_end_flag = 1;
guidata(hObject,handles);

while( (c ~= cmax) && (norm(r_current-endpoint)>r_hit) && ~handles.break_flag ) 
    [r_current,t] = TraceStep(r_current,t,handles.im',handles.stepsize,handles.explore_range,handles.interp_cell_number,handles.max_iter,handles.pixelsize);
    c = c+1;
    r(c,:)=r_current;
    contourlength = contourlength+handles.stepsize;
    %plot(r_current(1),r_current(2),'.k');
    %drawnow;
%end;

    handles = guidata(hObject);

 
    
    handles.r = r;
    handles.vertex_count = c;
    handles.contourlength = contourlength;
    handles.t = t;

    guidata(hObject,handles);

   DrawDataCall(hObject, eventdata, handles);
    UpdateContourLength(hObject, eventdata, handles);




end;
 DrawDataCall(hObject, eventdata, handles);

handles.break_flag = 0;    
handles.trace_to_end_flag = 0;
guidata(hObject,handles);




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


handles.axis_of_sym_flag_show = 0;
guidata(hObject, handles);
DrawDataCall(hObject, eventdata, handles);


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

if handles.axis_of_sym_flag == 0
    errordlg('Axis must be defined first.','Error');
else
    handles.axis_of_sym_flag_show = 1;
    guidata(hObject, handles);
    DrawDataCall(hObject, eventdata, handles);
end;
% --------------------------------------------------------------------
function HideImageMenu_Callback(hObject, eventdata, handles)
% hObject    handle to HideImageMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


handles.axis_image_flag = 0;
guidata(hObject, handles);

DrawDataCall(hObject, eventdata, handles);


% --------------------------------------------------------------------
function PrintMenu_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenu (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 VesicleMenu_Callback(hObject, eventdata, handles)
% hObject    handle to VesicleMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


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

if ~handles.trace_flag;
     errordlg('No trace to save','Error');
elseif ~handles.axis_of_sym_flag
     errordlg('Axis of symetry must be set.','Error');
else    
    [file,pathloc] = uiputfile('*.txt','Save Trace');
    if ~isequal(file, 0)
        r = handles.r(1:handles.vertex_count,:);
        x = abs(r(:,2)-handles.axis_y);
        y = r(:,1);
        r = [x,y];
        save([pathloc,file],'r','-ascii');
    end;
end;


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

if handles.trace_to_end_flag
    handles.break_flag = 1;
    guidata(hObject,handles);
end;


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

if ~handles.open_flag
    errordlg('Image must be open.','Error');
else
    pixrad = 3;


    CommandCall(hObject, eventdata, handles,'Click on a pixel');
    [x,y] = ginput(1);

    xp = round(x/handles.pixelsize);
    yp = round(y/handles.pixelsize);

    background = mean(mean(handles.im((yp-pixrad):(yp+pixrad),(xp-pixrad):(xp+pixrad))));


    handles.im = handles.im-background;
    handles.imdata = handles.imdata-round(background);

    handles.choose_range_min = handles.choose_range_min-background;
    handles.choose_range_max = handles.choose_range_max-background;

    guidata(hObject, handles);

    CommandCall(hObject, eventdata, handles,'');
    DrawDataCall(hObject, eventdata, handles);
end;

%handles


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

BreakButton_Callback(hObject, eventdata, handles);


% --- Executes on button press in AddAnchorToggle.
function AddAnchorToggle_Callback(hObject, eventdata, handles)
% hObject    handle to AddAnchorToggle (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 AddAnchorToggle

if get(hObject,'Value')
    while( get(hObject,'Value'))
        AddAnchor_Callback(hObject, eventdata, handles);
        handles = guidata(hObject);
    end;
end;
        
        


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


if ~handles.trace_flag;
     errordlg('No trace to save','Error');
elseif ~handles.axis_of_sym_flag
     errordlg('Axis of symetry must be set.','Error');
else    
    [file,pathloc] = uiputfile('*.txt','Save Trace');
    if ~isequal(file, 0)
        
        r = handles.r(1:handles.vertex_count,:);
        x = abs(r(:,2)-handles.axis_y);
        y = r(:,1);
        handles.force.r = [x,y];
        
        handles = MeshMaker(hObject, eventdata, handles);
        
        WriteObjFile([pathloc,file], handles.force.mesh.r, handles.force.mesh.faces);
        guidata(hObject, handles);

    end;
end;


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



if ~handles.trace_flag;
     errordlg('No trace to analyze','Error');
elseif ~handles.axis_of_sym_flag
     errordlg('Axis of symetry must be set.','Error');
else

    
     r = handles.r(1:handles.vertex_count,:);
        x = abs(r(:,2)-handles.axis_y);
        y = r(:,1);
        handles.force.r = [x,y];
        
  handles = MeshMaker(hObject, eventdata, handles);  
  [handles.force] = ForceDialog3D(handles.force);

%         forcecalc11(0,0,0,r);

  guidata(hObject, handles);

end;


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



    
if handles.trace_flag == 0;
     errordlg('No trace to save','Error');
else
    [file,pathloc] = uiputfile('*.mat','Save Trace');
    if ~isequal(file, 0)
        save([pathloc,file],'handles','-MAT');
        r = handles.r(1:handles.vertex_count,:);
        save([pathloc,file,'.txt'],'r','-ascii');
    end;
end;


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


handles = FWidth(hObject, eventdata, handles);

guidata(hObject, handles);



DrawDataCall(hObject, eventdata, handles);

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

if isfield(handles, 'width')
    handles = rmfield( handles, 'width' );
end;


  guidata(hObject, handles);

  if isfield(handles, 'width')
   
    'error'
end;

  
  DrawDataCall(hObject, eventdata, handles);