function sc_mapping(R_T, Phi_T)
% SC_MAPPING  Illustrate afferent and efferent mapping in the SC motor map.
%
%   SC_MAPPING() plots a 3-panel figure:
%     (1) Visual space (retinal error): fixation F and target T
%     (2) SC neural space: complex-log motor map + Gaussian population
%     (3) Motor space: saccade vector (decoded displacement)
%
%   SC_MAPPING(R_T, PHI_T) uses the target at polar coordinates
%   R_T (deg) and PHI_T (deg). Default: R_T = 20 deg, PHI_T = 30 deg.
%
%   The code illustrates:
%   - Afferent mapping: visual error (R,phi) -> SC coordinates (u,v)
%   - Population coding: Gaussian bump centered at (u0,v0)
%   - Efferent mapping: population -> saccade vector (Δx,Δy)


%% Initialisation
% clear;
clc;
close all;

%% Target
% target in visual space (polar)
if nargin < 1, R_T   = 55; end   % target eccentricity (deg)
if nargin < 2, Phi_T = 22.5; end   % target direction (deg)
% Target in motor space (cartesian)
[xT,yT] = pol2cart(R_T,Phi_T);
% Target in neural space. Afferent mapping: target to SC coordinates
[uT,vT] = scmotormap(R_T,Phi_T);

% indicate angle with arc
rangle			= 0.5*R_T;
phiangle		= linspace(0,Phi_T,100);
[xangle,yangle] = pol2cart(rangle,phiangle);


%% IsoR and isoPhi contour grids
figure(1);
clf
for jj = [1 3] % 1 = visual space, 3 = motor space
	%% Iso-R-contours
	R			= [2 10 20 50 90];
	phi = -90:90;

	[R,phi] = meshgrid(R,phi);

	phi		= phi/180*pi;
	x		= R.*cos(phi);
	y		= R.*sin(phi);

	subplot(1,3,jj)
	plot(x,y,'k-','LineWidth',1.5,'Color',[.8 .8 .8]);
	hold on
	nicegraph;
	axis equal;


	%% Iso-phi-contours
	R = 0:90;
	phi = -90:45:90;

	[R,phi] = meshgrid(R,phi);

	phi		= phi/180*pi;
	x		= R.*cos(phi);
	y		= R.*sin(phi);

	plot(x',y','k-','LineWidth',1.5,'Color',[.8 .8 .8]);
	nicegraph;
	% axis equal;

	xlim([-95 95]);
	ylim([-95 95]);

end


%% Isocontours for neural space
R			= [2 10 20 50 90];
phi			= linspace(-90,90,1000);
[R,phi]		= meshgrid(R,phi);
[u,v]		= scmotormap(R,phi);

subplot(132)
plot(u,v,'k-','LineWidth',1.5,'Color',[.8 .8 .8]);
hold on

R			= linspace(0,90,1000);
phi			= -90:45:90;
[R,phi]		= meshgrid(R,phi);
[u,v]		= scmotormap(R,phi);

plot(u',v','k-','LineWidth',1.5,'Color',[.8 .8 .8]);
nicegraph;
% axis equal;

xlim([-0.5 5.5]);
ylim([-3 3]);
xlabel('u (mm)','FontSize',21);
ylabel('v (mm)','FontSize',21);

subplot(133)
set(gca,'XTick',-90:30:90,'YTick',-90:30:90);
xlabel('x','FontSize',21);
ylabel('y','FontSize',21);

%% Target


subplot(131)
plot([0 xT],[0 yT],'k-',LineWidth=2);
plot(xT,yT,'ko',LineWidth=2,MarkerFaceColor=[.7 .7 .7],MarkerSize=24);
text(xT,yT,'T',HorizontalAlignment="right",VerticalAlignment="bottom",FontSize=21,Interpreter="latex");

text(xT/2,yT/2,'$R^\circ$',HorizontalAlignment="center",VerticalAlignment="bottom",FontSize=21,Interpreter="latex");
text(xangle(70)+2,yangle(70),'$\phi^\circ$',HorizontalAlignment="left",VerticalAlignment="middle",FontSize=21,Interpreter="latex");
title('visual space');

set(gca,'XTick',[],'YTick',[])
plot(xangle,yangle,'k-',LineWidth=2,MarkerFaceColor=[.4 .4 .4])


subplot(132)
plot(uT,vT,'ko',LineWidth=2,MarkerFaceColor=[.7 .7 .7],MarkerSize=24);
set(gca,'XTick',0:5,'YTick',-3:3)

[u0,v0] = scmotormap(0,0);
plot(u0,v0,'k+',LineWidth=2,MarkerFaceColor=[.7 .7 .7],MarkerSize=24);
text(u0,v0,'$F$',HorizontalAlignment="right",VerticalAlignment="top",FontSize=21,Interpreter="latex");
title('neural space');
text(uT,vT,'T',HorizontalAlignment="right",VerticalAlignment="bottom",FontSize=21,Interpreter="latex");


subplot(131)
plot(0,0,'k+',LineWidth=2,MarkerFaceColor=[.7 .7 .7],MarkerSize=24);
text(0,0,'$F$',HorizontalAlignment="right",VerticalAlignment="top",FontSize=21,Interpreter="latex");

subplot(133)
plot([0 xT],[0 0],'k-',LineWidth=2);
plot([xT xT],[0 yT],'k-',LineWidth=2);
plot(xT,yT,'ko',LineWidth=2,MarkerFaceColor=[.7 .7 .7],MarkerSize=24);
plot(0,0,'k+',LineWidth=2,MarkerFaceColor=[.7 .7 .7],MarkerSize=24);
text(0,0,'$F$',HorizontalAlignment="right",VerticalAlignment="top",FontSize=21,Interpreter="latex");
title('motor space');

text(xT/2,0,'x',HorizontalAlignment="center",VerticalAlignment="top",FontSize=21,Interpreter="latex");
text(xT,yT/2,'y',HorizontalAlignment="left",VerticalAlignment="middle",FontSize=21,Interpreter="latex");
quiver(0,0,xT,yT,0,'r-',LineWidth=2);

text(xT/2,yT,'saccade',HorizontalAlignment="center",VerticalAlignment="middle",FontSize=21,Interpreter="latex");
text(xT,yT,'T',HorizontalAlignment="right",VerticalAlignment="bottom",FontSize=21,Interpreter="latex");


%%

%% Save
fname = fullfile('..','..','images','sc_mapping_ori.svg');
savegraph(fname,'svg');



end

function [u,v] = scmotormap(R,phi)
% SCMOTORMAP  Complex-logarithmic SC motor map.
%   [u,v] = SCMOTORMAP(R,phi) maps saccade amplitude R (deg) and direction
%   phi (deg) to SC coordinates u (mm), v (mm).

Bu = 1.4; % mm
Bv = 1.8; % mm
A  = 3;   % deg


u = Bu*log(sqrt(R.^2+2*A*R.*cosd(phi)+A^2)./A);
v = Bv*atan((R.*sind(phi))./(R.*cosd(phi)+A));
end

function [x,y] = pol2cart(R,phi)
% POL2CARTD  Polar-to-cartesian conversion with angles in degrees.

x		= R.*cosd(phi);
y		= R.*sind(phi);
end