function earthdemo(maptype)
% function earthdemo(maptype)
%
% maptype, optional: 'texture' (default) or 'relief'
%
% Map available from
% http://www.progonos.com/furuti/MapProj/Dither/ProjPoly/Foldout/Cube/cube.html
% See the website for copyright information Carlos A. Furuti
%% Map selection
if nargin<1 || isempty(maptype)
maptype = 'texture';
end
switch maptype
case 'relief'
mapfname = 'cbGn-s100.rel.png';
offsetx = 2;
offsety = 27;
dxy = 200;
case 'texture'
mapfname = 'cbGn-s100.tex.png';
offsetx = 3;
offsety = 27;
dxy = 200;
otherwise
error('Unknown maptype <%s>, (''texture'' ''relief'')', maptype);
end
%% Read the map
disp('read the map')
fprintf('\tmap: %s\n', mapfname)
[A, map]=imread(mapfname);
A=rot90(A,1);
szA=size(A);
A=map(A+1,:);
A=reshape(A,[szA 3]);
% Plot it
fig = figure(1);
clf(fig);
ax=axes('Parent',fig);
image(A,'Parent',ax);
axis(ax,'equal');
% Project on sphere
n = dxy;
[Vertices Faces FaceID] = cubedsphere(n, 'equidistance');
%% Fill textture with RGB data
disp('fill texture')
CData = ones([size(FaceID,1) 3]);
xleft = offsetx + ((1:4)-1)*dxy;
xright = xleft + (dxy-1);
yupper = offsety + ((1:3)-1)*dxy;
ylower = yupper + (dxy-1);
% Reshape the map as long vector of RGB
A = reshape(A,[],3);
% The topology of six faces is like this
% N
% ^
% W <-o-> E
% v
% S
% +---+
% | 6 |
% +---+---+---+---+
% | 4 | 1 | 2 | 3 |
% +---+---+---+---+
% | 5 |
% +---+
%
xl = xleft([2 3 4 1 2 2]);
xr = xright([2 3 4 1 2 2]);
yu = yupper([2 2 2 2 3 1]);
yl = ylower([2 2 2 2 3 1]);
for i=1:6
x = xl(i):+1:xr(i);
y = yl(i):-1:yu(i);
[X Y] = meshgrid(x, y);
CData(FaceID==i,:) = A(sub2ind(szA,Y,X),:);
end
%% Plot the earth map on sphere
disp('plot earth')
fig = figure(2); clf(fig);
pos = get(0,'screensize'); % full screen
set(fig, 'Position', pos);
ax=axes('Parent',fig,'pos',[0 0 1 1]);
patch('Vertices',Vertices,'Faces',Faces, ...
'FaceVertexCdata',CData,'FaceColor','flat',...
'EdgeColor','none','parent',ax);
axis(ax,'equal');
axis(ax,'off');
view(ax,[155 15]);
end