% file function_exercise.m
%
% exercise in 
% 1. loading external data which represents a 3-D object,
% 2. translation and rotation using functions trans_pre and rot3d_pre
% 3. display using show3_post function
% 4. conversion between pre and post notation

% function syntax

% first line of file which defines the function      file name
% function[outputvariables]=name(inputvariables);     name.m
% function[rho,H,F]=motion(x,y,t);                    motion.m
% function[theta]=angleTH(x,y);                       angleTH.m
% function theta = angleTH(x,y);                      angleTH.m
% function [] = circle(r);                            circle.m
% function circle(r);                                 circle.m


% bevelcube.mat is a file which contains data.

load bevelcube;

% this loads the data, which in this case consists of two variables:
% matrix p are the vertices in post notation homogeneous coordinates
% vector c is the connection vector to show the order in which the vertices
% are connected.
%An object can be readily displayed in traditional wireframe format by
%simply drawing the vertices and connecting edge lines 
%with no regard to whether lines are visible or
%hidden. The object will be described by
%a list of vertex locations using homogeneous coordinates.
%The coordinates will be stored post notation in successive
%rows of a Nx4 Points Matrix, with columns 1-3 
%being the x.y.z coordinates and the 4-th being
%the h coordinate, and N is the number of points. 
%The edges will all be assumed to be straight lines 
%and will be defined by a
%connectivity list that contains in a linear sequence 
%the numbers of the vertices to be connected
%together in sequence. If the vertex number is negative, 
%it means to move the "pen" to the next vertex
%without drawing; if the vertex number is positive, 
%it means to draw a line from the starting vertex
%(current) to the next vertex. 
%Thus, the first and the last vertices listed in the Connectivity Matrix
%must be negative.
%
%Note that for pre notation, the Points Matrix will be 4xN.

% matrix p and vector c have been loaded into the workspace
% p is in post-notation

gpoint=p'; % convert to pre-notation by taking matrix transpose

% T1: translation
px=0.5;py=0.25;pz=0;
T1=trans_pre(-px,-py,-pz);

% T2, T3 rotations
% angles chosen to make isometric view
phi=pi/4;
theta=atan(1/sqrt(2));
%phi = -22.2*pi/180;
%theta= 20.7*pi/180;

T2=rot3d_pre(phi,2); 

T3=rot3d_pre(theta,1);

 % composite transformation
  Tcomp=T3*T2*T1; 
 
 % compute new point
 npoint=Tcomp*gpoint;
 
 % convert to post-notation
 newpt=npoint';

 % plot in 3D
 figure(1);
 show3_post(newpt,c);
 
 figure(2)
 show_post(newpt,c);
 