%**************************************************************************
%This function plots the magnetic field in the x-y plane whose z position
%is specified.  The closest z position with data points is used.
%
%Input:
%plt_file, plot file for results
%block, structure containing the block information
%charge, structure containing the magnetic charge information
%field, structure containing the magnetic field information
%z_val_req, requested z position of slice (m)
%
%Zachary Wolf, 2/8/11

function virtual_shim_plot_bv_vs_xy(plt_file, block, charge, field, z_val_req)

%Initialize
figure;
hold on;

%Draw the projection of the blocks
for ii = 1 : length(block)
    p1 = [- block(ii).dim(1) / 2,   block(ii).dim(2) / 2,   block(ii).dim(3) / 2];
    p2 = [  block(ii).dim(1) / 2,   block(ii).dim(2) / 2,   block(ii).dim(3) / 2];
    p3 = [- block(ii).dim(1) / 2, - block(ii).dim(2) / 2,   block(ii).dim(3) / 2];
    p4 = [  block(ii).dim(1) / 2, - block(ii).dim(2) / 2,   block(ii).dim(3) / 2];
    p5 = [- block(ii).dim(1) / 2,   block(ii).dim(2) / 2, - block(ii).dim(3) / 2];
    p6 = [  block(ii).dim(1) / 2,   block(ii).dim(2) / 2, - block(ii).dim(3) / 2];
    p7 = [- block(ii).dim(1) / 2, - block(ii).dim(2) / 2, - block(ii).dim(3) / 2];
    p8 = [  block(ii).dim(1) / 2, - block(ii).dim(2) / 2, - block(ii).dim(3) / 2];
    p1g = p1 * block(ii).rot + block(ii).trans;
    p2g = p2 * block(ii).rot + block(ii).trans;
    p3g = p3 * block(ii).rot + block(ii).trans;
    p4g = p4 * block(ii).rot + block(ii).trans;
    p5g = p5 * block(ii).rot + block(ii).trans;
    p6g = p6 * block(ii).rot + block(ii).trans;
    p7g = p7 * block(ii).rot + block(ii).trans;
    p8g = p8 * block(ii).rot + block(ii).trans;
    line([p1g(1), p2g(1)], [p1g(2), p2g(2)]);
    line([p1g(1), p3g(1)], [p1g(2), p3g(2)]);
    line([p1g(1), p5g(1)], [p1g(2), p5g(2)]);
    line([p2g(1), p4g(1)], [p2g(2), p4g(2)]);
    line([p2g(1), p6g(1)], [p2g(2), p6g(2)]);
    line([p3g(1), p4g(1)], [p3g(2), p4g(2)]);
    line([p3g(1), p7g(1)], [p3g(2), p7g(2)]);
    line([p4g(1), p8g(1)], [p4g(2), p8g(2)]);
    line([p5g(1), p6g(1)], [p5g(2), p6g(2)]);
    line([p5g(1), p7g(1)], [p5g(2), p7g(2)]);
    line([p6g(1), p8g(1)], [p6g(2), p8g(2)]);
    line([p7g(1), p8g(1)], [p7g(2), p8g(2)]);
end

%Draw the projection of the charges
%tic;
q = [charge.q];
x = [charge.x];
y = [charge.y];
z = [charge.z];
index_array = find(q > 0);
plot(x(index_array), y(index_array), 'r+');
index_array = find(q < 0);
plot(x(index_array), y(index_array), 'kx');

%Plot the field
[z_close, iz_close] = min(abs(field.pos(:, 3) - z_val_req));
z_val = field.pos(iz_close(1), 3);
index_array = find(field.pos(:, 3) == z_val);
quiver(field.pos(index_array, 1), field.pos(index_array, 2), field.b(index_array, 1), field.b(index_array, 2));

%Labels
title(['B vs XY At Z = ', num2str(z_val), ' m']);
xlabel('X Position (m)');
ylabel('Y Position (m)');

%Finalize
grid on;
zoom on;
hold off;

%Print
orient landscape;
print('-append', '-dpsc', plt_file);

%Done
