% GridBuilder64.m Generate the plot grid for acoustic surf mapping with a 
% T60 design geometry + 4 microphones aiming at:
% idx	 az	   el
%  61	  0	   90
%  62	  0   -90
%  63 	  0  	0
%  64   180	    0
%
% INPUT
% alpha = original map
%
% OUTPUT
% extended map = alpha   alpha   alpha
%
% Author: Daniel Pinardi, Universitiy of Parma, 05/2023
% Last revision: Daniel Pinardi, 05/2023

function [ extX, extY, extXI, extYI, XI, YI, X, Y ] = GridBuilder64( backgroundWidth, backgroundHeight, surfpoint, virtualMicNumber )

%% generation of mic coordinates
[ ~, pointsRadiant ]		= getTdesign( 10 );

pointsDegrees               = round ( pointsRadiant / pi * 180 );           % conversion rad -> degrees
pointsDegrees( 61, : )		= [   0  90 ];
pointsDegrees( 62, : )		= [   0 -90 ];
pointsDegrees( 63, : )		= [   0   0 ];
pointsDegrees( 64, : )		= [ 180   0 ];

% duplicate the points at bottom and top for having the same interpolation value at poles
pointsDegrees( 65, : )		= [   36   90 ];
pointsDegrees( 66, : )		= [   72   90 ];
pointsDegrees( 67, : )		= [  108   90 ];
pointsDegrees( 68, : )		= [  144   90 ];
pointsDegrees( 69, : )		= [  180   90 ];
pointsDegrees( 70, : )		= [ -144   90 ];
pointsDegrees( 71, : )		= [ -108   90 ];
pointsDegrees( 72, : )		= [  -72   90 ];
pointsDegrees( 73, : )		= [  -36   90 ];

pointsDegrees( 74, : )		= [   36  -90 ];
pointsDegrees( 75, : )		= [   72  -90 ];
pointsDegrees( 76, : )		= [  108  -90 ];
pointsDegrees( 77, : )		= [  144  -90 ];
pointsDegrees( 78, : )		= [  180  -90 ];
pointsDegrees( 79, : )		= [ -144  -90 ];
pointsDegrees( 80, : )		= [ -108  -90 ];
pointsDegrees( 81, : )		= [  -72  -90 ];
pointsDegrees( 82, : )		= [  -36  -90 ];


%% rescaling to background image
virtualMicNumberExtended    = length( pointsDegrees );
X                           = zeros( virtualMicNumberExtended, 1 );
Y                           = zeros( virtualMicNumberExtended, 1 );

for i = 1 : virtualMicNumberExtended 
    X(i)            = round( pointsDegrees( i, 1 ) * backgroundWidth / 360 ); 
    Y(i)            = round( pointsDegrees( i, 2 ) * backgroundHeight / 180 );  
end

%% grid generation

tx                  = linspace( -backgroundWidth/2,     backgroundWidth/2,  surfpoint );
ty                  = linspace( -backgroundHeight/2,    backgroundHeight/2, surfpoint/2 );
[ XI, YI ]          = meshgrid( tx, ty );
[ extXI, extYI ]    = meshgrid( tx, ty );

extX                = [ X - backgroundWidth;        X;      X + backgroundWidth ];
extY                = [ Y;                          Y;                        Y ];
                   
end
