%**************************************************************************
%This function plots the magnetic field in the z-x plane whose y position
%is specified.  The closest y 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
%y_val_req, requested y position of slice (m)
%
%Zachary Wolf, 4/13/10

function pm_block_rect_plot_bv_vs_zx(plt_file, block, charge, field, y_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(3), p2g(3)], [p1g(1), p2g(1)]);
    line([p1g(3), p3g(3)], [p1g(1), p3g(1)]);
    line([p1g(3), p5g(3)], [p1g(1), p5g(1)]);
    line([p2g(3), p4g(3)], [p2g(1), p4g(1)]);
    line([p2g(3), p6g(3)], [p2g(1), p6g(1)]);
    line([p3g(3), p4g(3)], [p3g(1), p4g(1)]);
    line([p3g(3), p7g(3)], [p3g(1), p7g(1)]);
    line([p4g(3), p8g(3)], [p4g(1), p8g(1)]);
    line([p5g(3), p6g(3)], [p5g(1), p6g(1)]);
    line([p5g(3), p7g(3)], [p5g(1), p7g(1)]);
    line([p6g(3), p8g(3)], [p6g(1), p8g(1)]);
    line([p7g(3), p8g(3)], [p7g(1), p8g(1)]);
end

%Draw the projection of the charges
for ii = 1 : length(charge)
    if (charge(ii).q > 0)
        plot(charge(ii).pos(3), charge(ii).pos(1), 'r+');
    end
    if (charge(ii).q < 0)
        plot(charge(ii).pos(3), charge(ii).pos(1), 'kx');
    end
end

%Plot the field
[y_close, iy_close] = min(abs(field.pos(:, 2) - y_val_req));
y_val = field.pos(iy_close(1), 2);
index_array = find(field.pos(:, 2) == y_val);
quiver(field.pos(index_array, 3), field.pos(index_array, 1), field.b(index_array, 3), field.b(index_array, 1));

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

%Set axes scale
x_limits = xlim;
y_limits = ylim;
lim = max(abs([x_limits, y_limits]));
xlim([-lim, lim]);
ylim([-lim, lim]);

%Finalize
grid on;
zoom on;
hold off;

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

%Done
