sphere_project - project point X,Y,Z to the surface of sphere radius r V = sphere_project(v,r,c) Cartesian inputs: v is the vertex matrix, Nx3 (XYZ) r is the sphere radius, 1x1 (default 1) c is the sphere centroid, 1x3 (default 0,0,0) XYZ are converted to spherical coordinates and their radius is adjusted according to r, from c toward XYZ (defined with theta,phi) V is returned as Cartesian 3D coordinates
0001 function V = sphere_project(v,r,c) 0002 0003 % sphere_project - project point X,Y,Z to the surface of sphere radius r 0004 % 0005 % V = sphere_project(v,r,c) 0006 % 0007 % Cartesian inputs: 0008 % v is the vertex matrix, Nx3 (XYZ) 0009 % r is the sphere radius, 1x1 (default 1) 0010 % c is the sphere centroid, 1x3 (default 0,0,0) 0011 % 0012 % XYZ are converted to spherical coordinates and their radius is 0013 % adjusted according to r, from c toward XYZ (defined with theta,phi) 0014 % 0015 % V is returned as Cartesian 3D coordinates 0016 % 0017 0018 % $Revision: 1.1 $ $Date: 2004/11/12 01:32:36 $ 0019 0020 % Licence: GNU GPL, no implied or express warranties 0021 % History: 06/2002, Darren.Weber_at_radiology.ucsf.edu, created 0022 % 0023 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 0024 0025 if ~exist('v','var'), 0026 msg = sprintf('SPHERE_PROJECT: No input vertices (X,Y,Z)\n'); 0027 error(msg); 0028 end 0029 0030 X = v(:,1); 0031 Y = v(:,2); 0032 Z = v(:,3); 0033 0034 if ~exist('c','var'), 0035 xo = 0; 0036 yo = 0; 0037 zo = 0; 0038 else 0039 xo = c(1); 0040 yo = c(2); 0041 zo = c(3); 0042 end 0043 0044 if ~exist('r','var'), r = 1; end 0045 0046 % alternate method is to use unit vector of V 0047 % [ n = 'magnitude(V)'; unitV = V ./ n; ] 0048 % to change the radius, multiply the unitV 0049 % by the radius required. This avoids the 0050 % use of arctan functions, which have branches. 0051 0052 0053 % Convert Cartesian X,Y,Z to spherical (radians) 0054 theta = atan2( (Y-yo), (X-xo) ); 0055 phi = atan2( sqrt( (X-xo).^2 + (Y-yo).^2 ), (Z-zo) ); 0056 % do not calc: r = sqrt( (X-xo).^2 + (Y-yo).^2 + (Z-zo).^2); 0057 0058 % Recalculate X,Y,Z for constant r, given theta & phi. 0059 R = ones(size(phi)) * r; 0060 x = R .* sin(phi) .* cos(theta); 0061 y = R .* sin(phi) .* sin(theta); 0062 z = R .* cos(phi); 0063 0064 V = [x y z]; 0065 0066 return