We first create 2 ellipsoids with random center and random orientation.
center = {{x1, y1, z1}, {x2, y2, z2}} =
RandomInteger[{-10, 10}, {2, 3}];
axlen = RandomInteger[{1, 6}, {3, 2}] // Transpose
tr1 = AffineTransform[{rm1 =
RotationMatrix[RandomReal[{-1, 1}, {2, 3}]], center[[1]]}];
tr2 = AffineTransform[{rm2 =
RotationMatrix[RandomReal[{-1, 1}, {2, 3}]], center[[2]]}];
ax1 = (IdentityMatrix[3] axlen[[1]]) . Transpose@rm1;
ax2 = (IdentityMatrix[3] axlen[[2]]) . Transpose@rm2 ;
e1 = tr1[Ellipsoid[{0, 0, 0}, axlen[[1]]]];
e2 = tr2[Ellipsoid[{0, 0, 0}, axlen[[2]]]];
Graphics3D[{Opacity[0.5], e1, e2,
Line[{e1[[1]], e1[[1]] + #} & /@ ax1],
Line[{e2[[1]], e2[[1]] + #} & /@ ax2]}]
Where center
is a list of the 2 centers and ax1
and ax2
are lists of the half axes of the 2 ellipsoids.
If we normalize the axes, we get orthogonal rotation matrices that transforms the coordinate aligned axes to the actual orientations.
rot1 = Normalize /@ ax1;
rot2 = Normalize /@ ax2;
E.g. the coordinate aligned axes (axlen[[1]] IdentityMatrix[3])
would be rotated into ax1
by:
(axlen[[1]] IdentityMatrix[3]) . rot1 == ax1
(*True*)
Note that we multiply by the rotation matrix rot1
from the right, because we are working with row vectors.
On the other hand, the inverse of rot1
, what is the same as the transposed, will transform ax1
back to the coordinated aligned half axes.
Therefore, to rotate half axes ax2
to the orientation of ax1
we can first rotate back to coordinates alignment by Transpose[r2]
and in a second rotation to alignment with ax1 by: r1
:
rot12= Transpose[rot2] . rot1;
ax3= ax2 . rot12
With this we can now define the affine translation that maps ellipsoid e2 on the center and direction of ellipsoid e1:
tr3 = AffineTransform[{rot12,
center[[1]] - center[[2]] . Transpose[rot12]}];
Note that the center of e2
is rotated by r12
and we need to undo this rotation by Transpose[r12]
.
Now we can apply the transformation and draw the result:
e3 = tr3[e2];
Graphics3D[{Opacity[0.5], e1, e3,
Line[{e1[[1]], e1[[1]] + #} & /@ ax1],
Line[{e3[[1]], e3[[1]] + #} & /@ ax3]}]