% Imagine a photograph sitting in the x-y plane of R^3.  
% We can project this photograph onto a 2-D subspace.  What does it look like?
% This script answers this question.


% clear all previous data and graphs
clear all
close all

% Define a matrix whose columns are a basis for a 2-D subspace of R^3 
M = [4 -2; 1 0; 0 1];

% Define an orthogonal projection of R^3 onto the subspace spanned by the columns of M
P = M*inv(M'*M)*M'

%% load image onto grid
    k = 100; % desired size of picture
    [X,Y] = meshgrid(0:k-1, 0:k-1); % (X,Y,Z) are the coordinates of domain of the transformation 
    Z = 0.*X;

    I = imrotate(imread('AFMobius.jpg'),180); % load the picture
    I = imresize(I,[k,k]); % resize the picture so it doesn't crash the machine
    
    figure; hold on; % create a figure, and display future plots on it
    surface(X,Y,Z,'FaceColor','texturemap','EdgeColor','none','Cdata',I);  % plot the plane on the x-y plane
    %surface(X,Z,Y,'FaceColor','texturemap','EdgeColor','none','Cdata',I); % plot the plane on the x-z plane
    %surface(Z,X,Y,'FaceColor','texturemap','EdgeColor','none','Cdata',I); % plot the plane on the z-x plane
    
%% MANIPULATING GRID WITH  MAP

	
	ProjType = 0; % choose this if you want an orthogonal projection
	ProjType = 1; % choose this if you want a projection onto the subspace above that is not orthogonal
 
	if ProjType == 1 %% a projection onto the plane above that is not orthogonal
		
		x = 4*X-2*Y; y = X; z = Y;
	else %% an orthogonal projection

		
		% all x,y-coords in domain
		XX = reshape(X,1,numel(X));
		YY = reshape(Y,1,numel(Y));

		% projection
		A = P*[XX;YY;zeros(size(YY))];

		% get coords
		x = A(1,:);
		y = A(2,:);
		z = A(3,:);
		
		% reshape into format surface likes
		x = reshape(x,k,k);
		y = reshape(y,k,k);
		z = reshape(z,k,k);
	end
	
	% plot the subspace, coloring each coordinate with pixels from the image
	surface(x,y,z,'FaceColor','texturemap','EdgeColor','none','Cdata',I)
    	axis equal; % make the axix equal
