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

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

%% generation of mic coordinates
GenerateEM32Dir;

for i = 1 : 32
    if EM32Dir(i, 4) > 180 
        EM32Dir(i, 4) = EM32Dir(i, 4) - 360;
    end
end

pointsDegrees = [ EM32Dir(:, 4), EM32Dir(:, 5) ];

%% rescaling to background image

X                   = zeros( virtualMicNumber, 1 );
Y                   = zeros( virtualMicNumber, 1 );

for i = 1 : virtualMicNumber 
    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 );

exttx               = linspace( -3/2 * backgroundWidth,     3/2 * backgroundWidth,  3 * surfpoint );
extty               = linspace( -3/2 * backgroundHeight,    3/2 * backgroundHeight, 3 * surfpoint/2 );
[ extXI, extYI ]    = meshgrid( exttx, extty );

extX                = [ -X - backgroundWidth;               -X;                           -X + backgroundWidth; ...
                         X - backgroundWidth;                X;                            X + backgroundWidth; ...
                        -X - backgroundWidth;               -X;                           -X + backgroundWidth ];

extY                = [ -Y + backgroundHeight;              -Y + backgroundHeight;        -Y + backgroundHeight; ...
                         Y;                                  Y;                            Y; ...
                        -Y - backgroundHeight;              -Y - backgroundHeight;        -Y - backgroundHeight ];

end
