function [p,t] = rotate(filename, phi, theta)

% ROTATE rotates the initial mesh element saved in 
% FILENAME through angles PHI and THETA in spherical 
% coordinates. All angles are in degrees.

% Usage: rotate('plate.mat', PHI, THETA) or
%        rotate('plate', PHI, THETA)
% where THETA and PHI are in degrees.

% Load the data.
load(filename);

if(phi==inf | theta==inf)
    error('Angle should be less than infinity.');
else
    % Convert the angles to radians.
    phi = phi*pi/180;
    theta = theta*pi/180;
    
    % Rotate the mesh through angle PHI about Z axis.
    % (In XY plane)
    P(1,:)=p(1,:)*cos(phi)-p(2,:)*sin(phi);
    P(2,:)=p(1,:)*sin(phi)+p(2,:)*cos(phi);
    P(3,:)=p(3,:);
    p=P;
    
    % Rotate the mesh through angle THETA about Y axis.
    % (In XZ plane)
    P(1,:)=p(1,:)*cos(theta)-p(3,:)*sin(theta);
    P(3,:)=p(1,:)*sin(theta)+p(3,:)*cos(theta);
    P(2,:)=p(2,:);
    p=P;
end

% Save and view the result.
save rotate p t;
viewer rotate
view(45,45)

 