%PLOT_EXAMPLE  Drawing example [script].

% (c) 2012-02-26 Martin Matousek
% Last change: $Date$
%              $Revision$


%% Init
disp('Creating example figures ...')
if ~exist( 'out.m', 'dir' ), mkdir( 'out.m' ); end
close all


%% General (non-euclidean) 2D plot
fig1 = prepare_figure( 1, [ 1, 1 ], [500, 400] );

% Some data ...
t = 0:0.1:5.3; % [s]
g_earth = 9.81; % m/s^2
g_moon = 1.63; % m/s^2
s_earth = 0.5 * g_earth * t.^2;
s_moon = 0.5 * g_moon * t.^2;

% Plot them

plot( t, s_earth, 'b', t, s_moon, 'k' );
xlabel( 'time [s]' )
ylabel( 'distance [m]' )
legend( 'Earth', 'Moon', 'location', 'northwest' )
title('Free-fall trajectory comparison')
axis tight


%% Plot with bitmap image
fig2 = prepare_figure( 2, [ 501, 1 ], [500, 400] );

% Some data ...
r = rand( 1, 37 ) * 50 + 50;
r(end) = r(1);
a = linspace( 0, 2*pi, 37 );
x = cos( a ) .* r + 300;
y = sin( a ) .* r + 200;

% Read and show the image
im = imread( 'daliborka.jpg' );
image( im );
hold on % next drawing commands are appended

% Draw some entities
h1 = plot( 100, 100, 'rx', 'linewidth', 2 );
h2 = plot( 200, 200, 'o', 'color', [ 1 1 0 ], 'linewidth', 2 );
h3 = plot( x, y, 'm.-' );
text( 641, 152, 'Tower', 'backgroundcolor', [1 1 0.8], ...
      'VerticalAlignment', 'bottom' )
h4 = plot( 641, 152, 'r.' );

hold off
axis image
legend( [h1 h2 h3 h4], 'A point', 'Also a point', 'Random polygon', ...
        'A point with label', ...
        'location', 'southeast' );
title( 'Daliborka' )
axis tight


%% 3D euclidean plot
fig3 = prepare_figure( 3, [ 1, 401 ], [330, 330] );

X1 = rand( 3, 100 ) * 4 - 2; % random 3D points
X2 = rand( 3, 50 ) * 0.2 - 0.1; % random 3D points

plot3( [ 0 0 0; 1 0 0 ], [ 0 0 0; 0 1 0 ], [ 0 0 0; 0 0 1 ], 'k', ...
       'linewidth', 2 )
hold on
h1 = plot3( X1(1,:), X1(2,:), X1(3,:), 'r.' );
h2 = plot3( X2(1,:), X2(2,:), X2(3,:), 'b.' );

text( 1, 0, 0, '\delta_x' )
text( 0, 1, 0, '\delta_y' )
text( 0, 0, 1, '\delta_z' )
hold off
axis equal
grid on
xlabel( 'x' );
ylabel( 'y' );
zlabel( 'z' );
title( 'Coordinate system delta and 3D points' );
legend( [h1 h2], 'Far points', 'Near points', 'location', 'southwest' )
axis tight


%% 3D euclidean plot - var 2
fig4 = prepare_figure( 4, [ 331, 401 ], [330, 330] );

% the same as above
plot3( [ 0 0 0; 1 0 0 ], [ 0 0 0; 0 1 0 ], [ 0 0 0; 0 0 1 ], 'k', ...
       'linewidth', 2 )
hold on
h1 = plot3( X1(1,:), X1(2,:), X1(3,:), 'r.' );
h2 = plot3( X2(1,:), X2(2,:), X2(3,:), 'b.' );

text( 1, 0, 0, '\delta_x' )
text( 0, 1, 0, '\delta_y' )
text( 0, 0, 1, '\delta_z' )
hold off
axis equal
grid on
xlabel( 'x' );
ylabel( 'y' );
zlabel( 'z' );

% zoom
set( gca, 'xlim', [-0.3 0.3] );
set( gca, 'ylim', [-0.3 0.3] );
set( gca, 'zlim', [-0.3 0.3] );

title( 'Coordinate system delta and 3D points (zoom-in)' );


%% Vectors
fig5 = prepare_figure( 5, [ 661, 401 ], [330, 330] );

o1 = [1.5; -1.5; 1 ];
o2 = [-1;2;0];

B1 = [ 1, 0.6, -0.1; 0.2, -0.2, 2;1, -0.5, 0 ]';
B2 = [ -1, -0.5, 0; 0, 0, 1.5; 0, -1, 0.5 ]';
dup = [1,1,1];

x = [-2;-2;0.5];

x1 = x - o1;
x2 = x - o2;

% point
plot3( x(1), x(2), x(3), 'k.' )
hold on
text( x(1), x(2), x(3), 'x' )

% basis 1
quiver3( o1(1,dup), o1(2,dup), o1(3,dup), ...
         B1(1,:), B1(2,:), B1(3,:), 0, 'b'  ) % 0 .. no scaling !

% basis 2
quiver3( o2(1,dup), o2(2,dup), o2(3,dup), ...
         B2(1,:), B2(2,:), B2(3,:), 0, 'r' )

% point vectors
quiver3( o1(1), o1(2), o1(3), x1(1), x1(2), x1(3), 0, 'g' )
quiver3( o2(1), o2(2), o2(3), x2(1), x2(2), x2(3), 0, 'm' )

% texts
for i = 1:3
  text( o1(1)+1.2*B1(1,i), ...
        o1(2)+1.2*B1(2,i), ...
        o1(3)+1.2*B1(3,i), ['b1', char('x'+i-1) ] );
  text( o2(1)+1.2*B2(1,i), ...
        o2(2)+1.2*B2(2,i), ...
        o2(3)+1.2*B2(3,i), ['b2', char('x'+i-1) ] );
end

hold off
axis equal
set( gca, 'xlim', [-3, 4], 'ylim', [-3, 3], 'zlim', [-1, 4] )

grid on
xlabel( 'x' );
ylabel( 'y' );
zlabel( 'z' );

title( 'A point described in two coordinate systems' );


%% export
fig_export( fig1, 'out.m/ex_graph.png' )
fig_export( fig1, 'out.m/ex_graph.pdf' )

fig_export( fig2, 'out.m/ex_image.png' )
fig_export( fig2, 'out.m/ex_image.pdf' )

fig_export( fig3, 'out.m/ex_3d.png' )
fig_export( fig3, 'out.m/ex_3d.pdf' )

fig_export( fig5, 'out.m/ex_vectors.png' )
fig_export( fig5, 'out.m/ex_vectors.pdf' )


%% finish
disp( 'Creating example figures: finished.' )
