% file viewcubevanpost.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

clear all; close all;

% 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; % p is in post notation
% add points at infinity, which will show up as vanishing points in a
% perspective view
% don't use these points in a regular (non-perspective) view
gpoint=[gpoint; [1 0 0 0]; [0 1 0 0]; [0 0 1 0]; [0 0 0 0] ];



% T1: translation
%px=0.5;py=0.5;pz=0;
px=0; py=-2; pz=0;

T3=trans_post(-px,-py,-pz);

% T2, T3 rotations
% angles chosen to make isometric view
%phi=pi/4; % rotate about y axis
%theta=atan(1/sqrt(2)); % rotate about x axis
% angles for dimetric view
%phi = -22.2*pi/180;
%theta= 20.7*pi/180;
% arbitrary angles, following notes page 17
phi=0*pi/180; % about x axis
theta=30*pi/180; % about y axis

T2=rot3d_post(phi,1); 

T1=rot3d_post(theta,2);

zp=3;
T4=persp_post(zp);

 % composite transformation
  Tcomp=T1*T2*T3*T4; 
 
 % compute new point
 npoint=gpoint*Tcomp;
 
 % already in post-notation
 newpt=npoint;

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