From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Change axes of ellipsoid
Date: Fri, 14 May 2010 04:30:22 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 56
Message-ID: <hsijku$ltv$>
References: <hsi8q2$jjo$>
Reply-To: <HIDDEN>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: 1273811422 22463 (14 May 2010 04:30:22 GMT)
NNTP-Posting-Date: Fri, 14 May 2010 04:30:22 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1187260
Xref: comp.soft-sys.matlab:635700

"Els " <> wrote in message <hsi8q2$jjo$>...
> I want to rotate a standard ellipsoid, constructed by the next code:
> -------------
> % construct of center ellipsoid
> a= rand(1,3)*10
> cx=a(1)
> cy=a(2)
> cz=a(3)
> % construct the three different radii
> b= rand(1,3)*10
> rx=b(1)
> ry=b(2)
> rz=b(3)
> % construct ellipsoid
> [x,y,z] = ellipsoid(cx,cy,cz,rx,ry,rz,25)
> h=surf(x,y,z)
> alpha(0.05)
> hold on
> -------------------------
> The ellipsoid has 3 axes peripendicular to cx, cy, and cz. 
> But now I have a line, how doi I rotate my ellipsoid so that the old x axes is rotated to the line. And furthermore, the surface points (x,y,z) will change as well. How do I get the rotated surface points (xRot,yRot,zRot)
- - - - - - - - - -
  I assume your "line" runs through the origin.  Let t be a 3D row vector pointing along this line in the direction toward which the positive x-axis will rotate.  The arrays x, y, and z here are your arrays.  Then do this:

% Calculate necessary rotation data
t = t/norm(t); % Make t a unit vector
u = [1,0,0]; % Unit vector along the positive x-axis
w = cross(u,t); % The axis of rotation
cosa = dot(u,t); % Cosine of the angle of rotation
sina = norm(w); % Sine of the angle of rotation
w = w/sina; % Make w a unit vector
v = cross(w,u); % Unit vector orthogonal to u and w

% Proceed with the rotation transformation
n = numel(x); % Number of points in surface
p = [x(:),y(:),z(:)]; % Create n x 3 array of ellipsoid surface points
u = repmat(u,n,1); % Match sizes of u, v, and w to that of p
v = repmat(v,n,1);
w = repmat(w,n,1);
wp = cross(w,p,2);
q = dot(p,w,2)*w + cosa*cross(wp,w,2) + sina*wp; % Rotate p
xr = q(:,1); yr = q(:,2); zr = q(:,3); % Extract components
xr = reshape(xr,size(x)); % These are the rotated coordinates
yr = reshape(yr,size(x)); % in mesh format again
zr = reshape(zr,size(x));

% Now plot the rotated surface

  I'm sure this can be made a lot more streamlined, but it should get you there.  Unless I have made some blunder - I haven't had time to test it.  You can do that for me.

  Note: If your "line" doesn't run through the origin, you can first translate everything so that it does.  Afterward the rotation you can translate back again by the same amount.  I was too lazy to do that for you.

Roger Stafford