elec_sph2cart - convert spherical to Cartesian coordinates Convert from spherical (theta,phi,r) to Cartesian rectangular (x,y,z) coordinates. Useage: [X,Y,Z] = elec_sph2cart(theta,phi,r,[degrees]) theta is counterclockwise rotation from +x in x-y plane (azimuth), phi is elevation with respect to z-axis (eg, Cz in 10-20 system), r is radius, degrees: 0=radians (default) or 1=degrees for theta & phi Result: (X,Y,Z) are double floating point Note: Conversion from spherical to Cartesian coordinates, given angles in radians, is: x = r .* sin(phi) .* cos(theta); y = r .* sin(phi) .* sin(theta); z = r .* cos(phi); +ve x-axis from origin through T4 (right ear) +ve y-axis from origin through Nasion (at theta = +90degrees) Results from this routine are not the same as those from the inbuilt sph2cart matlab function. Phi here is elevation with respect to z-axis. The matlab function sph2cart uses elevation with respect to xy plane.
0001 function [X,Y,Z] = elec_sph2cart(theta,phi,r,degrees) 0002 0003 % elec_sph2cart - convert spherical to Cartesian coordinates 0004 % 0005 % Convert from spherical (theta,phi,r) to Cartesian rectangular (x,y,z) 0006 % coordinates. 0007 % 0008 % Useage: [X,Y,Z] = elec_sph2cart(theta,phi,r,[degrees]) 0009 % 0010 % theta is counterclockwise rotation from +x in x-y plane (azimuth), 0011 % phi is elevation with respect to z-axis (eg, Cz in 10-20 system), 0012 % r is radius, 0013 % degrees: 0=radians (default) or 1=degrees for theta & phi 0014 % 0015 % Result: (X,Y,Z) are double floating point 0016 % 0017 % Note: Conversion from spherical to Cartesian coordinates, given 0018 % angles in radians, is: 0019 % 0020 % x = r .* sin(phi) .* cos(theta); 0021 % y = r .* sin(phi) .* sin(theta); 0022 % z = r .* cos(phi); 0023 % 0024 % +ve x-axis from origin through T4 (right ear) 0025 % +ve y-axis from origin through Nasion (at theta = +90degrees) 0026 % 0027 % Results from this routine are not the same as those from 0028 % the inbuilt sph2cart matlab function. Phi here is 0029 % elevation with respect to z-axis. The matlab function 0030 % sph2cart uses elevation with respect to xy plane. 0031 % 0032 0033 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:34 $ 0034 0035 % Licence: GNU GPL, no express or implied warranties 0036 % History: 07/2001, Darren.Weber_at_radiology.ucsf.edu 0037 % 08/2003, Darren.Weber_at_radiology.ucsf.edu 0038 % changed input option 'angle' to 'degrees' so 0039 % the logic below is clearer. 0040 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0041 0042 if ~exist('degrees','var'), degrees = 0; end 0043 0044 if degrees, 0045 % convert theta and phi from degrees to radians 0046 theta = theta * (pi/180); 0047 phi = phi * (pi/180); 0048 end 0049 0050 X = r .* sin(phi) .* cos(theta); 0051 Y = r .* sin(phi) .* sin(theta); 0052 Z = r .* cos(phi); 0053 0054 return