function [theta,varphi]=SPHARMangles(sphere);
%
%function [theta,varphi]=SPHARMangles(sphere);
%
%Given a spherical mesh, it generates Euler angles \theta and \varphi used
%in Chung et al. (2007), which follows the most widely used mathematical 
%convention and it is different from MATLAB convenstion.
%
% sphere: spherical mesh
% theta, varphi: spherical angles
%
%
% (C) Moo K. Chung 
%  mkchung@wisc.edu
%  Department of Biostatisics and Medical Informatics
%  University of Wisconsin, Madison
%
%Reference: 
% Chung, M.K., Dalton, K.M., Shen, L., L., Evans, A.C., Davidson, R.J. 2007. 
% Weighted Fourier series representation and its application to quantifying the 
% amount of gray matter. IEEE Transactions on Medical Imaging 26:566-581. 

n_vertex=size(sphere.vertices,1);
c=mean(sphere.vertices);  %mass center
sphere.vertices=sphere.vertices-kron(ones(n_vertex,1),c);  % translation

[theta,varphi,r] = cart2sph(sphere.vertices(:,1),sphere.vertices(:,2),sphere.vertices(:,3));

% MATLAB coordinate systems are different from the convention used in the
% TMI paper.
temp = theta;
theta = pi/2 - varphi;
varphi = pi + temp;

%figure_wire(sphere,'yellow')

