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

% Last Modified by GUIDE v2.5 16-Aug-2005 09:57:43

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

handles.force = varargin{1};

handles.tension     = handles.force.tension;
handles.pressure    = handles.force.pressure;
handles.spont       = handles.force.spont;
handles.stepsize    = handles.force.stepsize;
handles.normal_sign = handles.force.normal_sign;
guidata(hObject, handles);

reinterp_data(hObject, eventdata, handles);
handles = guidata(hObject);

init_dialog(hObject, eventdata, handles, varargin);

% Choose default command line output for ForceDialog3D
% handles.output = 'Yes';

% Update handles structure
% guidata(hObject, handles);

% Insert custom Title and Text if specified by the user
% Hint: when choosing keywords, be sure they are not easily confused 
% with existing figure properties.  See the output of set(figure) for
% a list of figure properties.
% if(nargin > 3)
%     for index = 1:2:(nargin-3),
%         if nargin-3==index, break, end
%         switch lower(varargin{index})
%          case 'title'
%           set(hObject, 'Name', varargin{index+1});
%          case 'string'
%           set(handles.text1, 'String', varargin{index+1});
%         end
%     end
% end

% Determine the position of the dialog - centered on the callback figure
% if available, else, centered on the screen


% 
% FigPos=get(0,'DefaultFigurePosition');
% OldUnits = get(hObject, 'Units');
% set(hObject, 'Units', 'pixels');
% OldPos = get(hObject,'Position');
% FigWidth = OldPos(3);
% FigHeight = OldPos(4);
% if isempty(gcbf)
%     ScreenUnits=get(0,'Units');
%     set(0,'Units','pixels');
%     ScreenSize=get(0,'ScreenSize');
%     set(0,'Units',ScreenUnits);
% 
%     FigPos(1)=1/2*(ScreenSize(3)-FigWidth);
%     FigPos(2)=2/3*(ScreenSize(4)-FigHeight);
% else
%     GCBFOldUnits = get(gcbf,'Units');
%     set(gcbf,'Units','pixels');
%     GCBFPos = get(gcbf,'Position');
%     set(gcbf,'Units',GCBFOldUnits);
%     FigPos(1:2) = [(GCBFPos(1) + GCBFPos(3) / 2) - FigWidth / 2, ...
%                    (GCBFPos(2) + GCBFPos(4) / 2) - FigHeight / 2];
% end
% FigPos(3:4)=[FigWidth FigHeight];
% set(hObject, 'Position', FigPos);
% set(hObject, 'Units', OldUnits);
% 
% 
% 
% % Make the GUI modal
% set(handles.figure1,'WindowStyle','modal')




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

% --- Outputs from this function are returned to the command line.
function varargout = ForceDialog3D_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.force;



% The figure can be deleted now
delete(handles.figure1);

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

handles.force.tension    = handles.tension             ;
handles.force.pressure   = handles.pressure     ;
handles.force.spont      = handles.spont             ;
handles.force.stepsize   = handles.stepsize;
handles.force.normal_sign= handles.normal_sign;

guidata(hObject, handles);

close(handles.fighand);
% Use UIRESUME instead of delete because the OutputFcn needs
% to get the updated handles structure.
uiresume(handles.figure1);




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

uiresume(handles.figure1);



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



pressure = str2double(get(hObject, 'String'));
if isnan(pressure)
    errordlg('Input must be a number','Error');
    set(handles.PressureEdit,        'String',sprintf('%g',  handles.pressure));
else
    if get(handles.LockToggle,'Value')
        handles.tension = handles.tension/handles.pressure*pressure;
        set(handles.TensionEdit,        'String',sprintf('%g',  handles.tension));
    end
    handles.pressure = pressure;        
    guidata(hObject, handles);
    set(handles.PressureEdit,        'String',sprintf('%g',  handles.pressure));
    DrawDataCall( hObject, eventdata, handles);

end;

DrawDataCall(hObject, eventdata, handles);


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



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



tension = str2double(get(hObject, 'String'));
if isnan(tension)
    errordlg('Input must be a number','Error');
    set(handles.TensionEdit,        'String',sprintf('%g',  handles.tension));
else
    if get(handles.LockToggle,'Value')
        handles.pressure = handles.pressure/handles.tension*tension;
        set(handles.PressureEdit,        'String',sprintf('%g',  handles.pressure));
    end
    handles.tension = tension;
    guidata(hObject, handles);
    set(handles.TensionEdit,        'String',sprintf('%g',  handles.tension));
    DrawDataCall( hObject, eventdata, handles);

end;

DrawDataCall(hObject, eventdata, handles);


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






function init_dialog(hObject, eventdata, handles, varargin)



set(handles.PressureEdit,     'String',sprintf('%g',handles.pressure));
set(handles.TensionEdit,      'String',sprintf('%g',handles.tension));
set(handles.SpontEdit,        'String',sprintf('%g',handles.spont));
set(handles.ForceStepSizeEdit,'String',sprintf('%g',handles.stepsize));


set(handles.ColorMapPopup, 'Value', 1);

handles.fighand = figure();
guidata(hObject, handles);

DrawDataCall(hObject, eventdata, handles);


set(handles.LockToggle,'Value',0);

% --- Executes on selection change in ColorMapPopup.
function ColorMapPopup_Callback(hObject, eventdata, handles)
% hObject    handle to ColorMapPopup (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 ColorMapPopup contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ColorMapPopup



menunum = get(hObject, 'Value');
switch menunum
    case 1
        handles.colormap = 'Total Force';
       % 'default'
    case 2
        handles.colormap = 'Force Density';
        %'gray'
    case 3
        handles.colormap = 'Curvature';
        %'hot'
end
guidata(hObject, handles);




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

set(hObject, 'String', {'default', 'gray', 'hot'});


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

if get(hObject,'Value') == 0
    handles.choose_range_flag = 0;
else
    handles.choose_range_flag = 1;
end;

guidata(hObject, handles);

    
    
    
    


% --- Executes on selection change in ColorMapPopupM.
function ComputePopup_Callback(hObject, eventdata, handles)
% hObject    handle to ColorMapPopupM (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 ColorMapPopupM contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ColorMapPopupM

value = get(hObject,'Value');

DrawDataCall(hObject, eventdata, handles);


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



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

spont = str2double(get(hObject, 'String'));
if isnan(spont)
    errordlg('Input must be a number','Error');
    set(handles.SpontEdit,        'String',sprintf('%g',  handles.spont));
else
    handles.spont = spont;
    guidata(hObject, handles);
    set(handles.SpontEdit,        'String',sprintf('%g',  handles.spont));
    DrawDataCall( hObject, eventdata, handles);

end;


DrawDataCall(hObject, eventdata, handles);

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

set(hObject,'String',{'Total Force','Force Density','Curvature','tangents'});


function DrawDataCall(hObject, eventdata, handles)

    forcecalc13(handles.tension,handles.pressure,handles.spont,handles.r,get(handles.ComputePopup,'Value'),handles.fighand,handles.normal_sign);


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


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

if handles.normal_sign > 0
    handles.normal_sign = -1;
else
     handles.normal_sign = 1;
end;   

guidata(hObject,handles);

DrawDataCall( hObject, eventdata, handles);

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



stepsize = str2double(get(hObject, 'String'));
if isnan(stepsize)
    errordlg('Input must be a number','Error');
    set(handles.ForceStepSizeEdit,        'String',sprintf('%g',  handles.stepsize));
else
    handles.stepsize = stepsize;
    guidata(hObject, handles);
    set(handles.ForceStepSizeEdit,        'String',sprintf('%g',  handles.stepsize));
    
    reinterp_data(hObject, eventdata, handles);
    handles = guidata(hObject);
    
    DrawDataCall( hObject, eventdata, handles);
end;











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

function reinterp_data(hObject, eventdata, handles)


%compute ds

rlength = max(size(handles.force.r));

s = zeros(rlength,1);
ds = zeros(rlength-1,1);

for i=2:rlength;
    ds(i-1) = norm(handles.force.r(i,:)-handles.force.r((i-1),:));
    s(i) = sum(ds(1:(i-1)));
end;

smax = s(rlength);

snew = 0:handles.stepsize:smax;

if max(size(snew)) < 8
    handles.stepsize = mean(ds);
    guidata(hObject,handles);
    reinterp_data(hObject, eventdata, handles);
    handles = guidata(hObject);
else
    handles.r = interp1(s,handles.force.r,snew);
end;

guidata(hObject,handles);













