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

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

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

%Plot the field
[x_close, ix_close] = min(abs(field.pos(:, 1) - x_val_req));
x_val = field.pos(ix_close(1), 1);
index_array = find(field.pos(:, 1) == x_val);
quiver(field.pos(index_array, 2), field.pos(index_array, 3), field.b(index_array, 2), field.b(index_array, 3));

%Labels
title(['B vs YZ At X = ', num2str(x_val), ' m']);
xlabel('Y Position (m)');
ylabel('Z 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
