% GridBuilder122.m Generate the plot grid for acoustic surf mapping
% This script generates a grid for plotting acoustic surf maps from 
% EigenMike recorded IR. The scheme of generated grid is reported below.
%
% INPUT
% alpha = original map
%
%
% OUTPUT
% extended map = alpha   alpha   alpha
%
%
% Authors: Daniel Pinardi, Marco Binelli.
% Universitiy of Parma.
% 2017

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

%% generation of mic coordinates
pointsRadiant               = GenerateUniformTestDir( 2 );
pointsDegrees               = round ( pointsRadiant / pi * 180 );           % conversion rad -> degrees

% duplicate 8 times the points 21 at top and 32 at bottom for having the same interpolation value at poles
for i = virtualMicNumber +1 : virtualMicNumber +9
    pointsDegrees( i, 1 )   = pointsDegrees(i-26,1);
    pointsDegrees( i, 2 )   = 90;
end

for i = virtualMicNumber +10 : virtualMicNumber +18
    pointsDegrees(i,1)      = -146+36*(i-42);
    pointsDegrees(i,2)      = -90;
end

%% 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
