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

%  License:
%
%  Please contact the author at email: fguevara at math dot utah dot edu 
%  if you find this demo useful. Please also credit the author if you use this
%  demo in public. Portions of the code are automatically generated by Matlab
%  and as far as I understand are Copyright Mathworks, inc.
%  
%  Copyright (c) 2012, Fernando Guevara Vasquez 
%  All rights reserved.
%
%  Redistribution and use in source and binary forms, with or without
%  modification, are permitted provided that the following conditions are met:
%  
%   * Redistributions of source code must retain the above copyright notice,
%   this list of conditions and the following disclaimer.
%
%   * Redistributions in binary form must reproduce the above copyright notice,
%   this list of conditions and the following disclaimer in the documentation
%   and/or other materials provided with the distribution.
%
%  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
%  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
%  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
%  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
%  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
%  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
%  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
%  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
%  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
%  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
%  POSSIBILITY OF SUCH DAMAGE.
%

% Last Modified by GUIDE v2.5 27-Mar-2012 01:04:16

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

% Choose default command line output for transf_demo
handles.output = hObject;
xmax=3;
axis(handles.axes2,[-xmax,xmax,-xmax,xmax]);
handles.xmin = -xmax; handles.xmax = xmax;
handles.ymin = -xmax; handles.ymax = xmax;
handles.c = 1;
handles.pressed=0;
handles.ray_start=[0.1,0];
handles.ray_end = [0.1,0];
handles.mode = 1;
% Update handles structure
guidata(hObject, handles);
redraw(handles);
% UIWAIT makes transf_demo wait for user response (see UIRESUME)
% uiwait(handles.figure1);


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



function redraw(handles)
c = handles.c;
axis(handles.axes2); cla;
nx = 20; ny=nx;

% display size
xmax=handles.xmax; ymax=handles.ymax;

x = linspace(-xmax,xmax,nx);
y = linspace(-ymax,ymax,ny);
ty = linspace(-ymax,ymax);
tx = linspace(-xmax,xmax);
tt = linspace(0,2*pi);
 t = linspace(0,1,256);

hold on;
for i=1:nx,
 l=F([x(i)*ones(size(ty));ty],c);
 plot(l(1,:),l(2,:),'k');
end;

for i=1:ny,
 l=F([tx;y(i)*ones(size(tx))],c);
 plot(l(1,:),l(2,:),'k');
end;

plot(2*cos(tt),2*sin(tt),'r',c*cos(tt),c*sin(tt),'r');

% plot ray
ray_dir = handles.ray_end - handles.ray_start;
ray_pt = handles.ray_start'*ones(size(t)) + ray_dir'*t;
l = F(ray_pt,c);
plot(l(1,:),l(2,:),'b','linewidth',2);

hold off;
axis equal; axis xy; box off; axis off;


function r=nrm(x) 
r=sqrt(x(1,:).^2 + x(2,:).^2);

function r=FF(x,c) 
r=((2-c)/2*ones(2,1)*nrm(x) + c).*(x./(ones(2,1)*nrm(x))); 

function r=F(x,c) 
r=FF(x,c)*diag(nrm(x)<=2)  + x*diag(nrm(x)>2);


% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(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)
handles.pressed=1;
pt = get(handles.axes2,'CurrentPoint');
if ( handles.xmin <= pt(1) && pt(1) <= handles.xmax && ...
     handles.ymin <= pt(3) && pt(3) <= handles.ymax )
  switch (handles.mode)
      case 1
       c = norm(pt([1,3]));c = max(c,0.01); c = min(c,2);handles.c=c;
      case 2
       handles.ray_start=pt([1,3]);
       handles.ray_end  =pt([1,3]);
  end;
  guidata(hObject,handles);
  redraw(handles);
end;
guidata(hObject,handles);


% --- Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(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)
if (handles.pressed)
pt = get(handles.axes2,'CurrentPoint');
if ( handles.xmin <= pt(1) && pt(1) <= handles.xmax && ...
     handles.ymin <= pt(3) && pt(3) <= handles.ymax )
 switch (handles.mode)
     case 1
  c = norm(pt([1,3]));c = max(c,0.01); c = min(c,2);handles.c=c;
     case 2
         handles.ray_end=pt([1,3]);
 end;
  guidata(hObject,handles);
  redraw(handles);
end;
guidata(hObject,handles);
end;

% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(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)
handles.pressed=0;
guidata(hObject,handles);


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


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


% --- 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)
handles.c = 1;
handles.pressed=0;
handles.ray_start=[0.1,0];
handles.ray_end = [0.1,0];
handles.mode = 1;
set(handles.popupmenu1,'Value',1);
% Update handles structure
guidata(hObject, handles);
redraw(handles);
