How to render a 3D ellipsoid with Graphics3D?
Using Sphere
with Scale
and Rotate
works too:
Graphics3D[Rotate[Scale[Sphere[], {5, 4, 2}, {0, 0, 0}], 60 Degree, {1, 2, 1}]]
The first triple is the scaling in the x,y,and z coordinates, the second triple is the translation, and the third triple is the axis about which to rotate. To generate a number of random ellipses:
x := RandomReal[];
Show[Table[Graphics3D[Rotate[Scale[Sphere[], {x, x, x}, {x i/6, x i/6, x i/6}],
x, {x, x, x}], Boxed -> False], {i, 25}]]
You can modify this if you need to specify the rotation in different ways, etc. As Simon Woods has suggested, probably the best way is to use GeometricTransformation
.
ellipsoid[a_, b_, center_?VectorQ, rotation_, around_?VectorQ] := Fold[
GeometricTransformation,
Sphere[],
{ScalingTransform[{a, b, b}],
RotationTransform[rotation, around],
TranslationTransform[center]}]
ellipsoid @@@ Table[{x, x, 10 {x, x, x}, x, {x, x, x}} /. x :> RandomReal[]
, {111}] // Graphics3D // AbsoluteTiming
{0.347020,
An alternative approach that generates explicit primitives instead of transformed ones uses the NURBS representation of a sphere, with all the appropriate transformations done to its control points to generate the ellipsoid:
myEllipsoid[dims : {_?Positive, _?Positive, _?Positive} : {1, 1, 1},
center : (_?VectorQ) : {0, 0, 0},
rot : {_, _?VectorQ} : {0, {1, 0, 0}}] := Block[{ctrlpts},
ctrlpts = Composition[TranslationTransform[center],
RotationTransform[Sequence @@ rot],
ScalingTransform[dims]] /@
Outer[Append[#2 #1[[1]], #1[[2]]] &,
{{0, -1}, {1, -1}, {1, 1}, {0, 1}},
{{1, 0}, {1, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {1, -1}, {1, 0}}, 1];
BSplineSurface[ctrlpts, SplineClosed -> True, SplineDegree -> 2,
SplineKnots -> {{0, 0, 0, 1/2, 1, 1, 1},
{0, 0, 0, 1/4, 1/2, 1/2, 3/4, 1, 1, 1}},
SplineWeights -> Outer[Times, {1, 1/2, 1/2, 1},
{1, 1/2, 1/2, 1, 1/2, 1/2, 1}]]]
Here's an example:
randomEllipsoid := myEllipsoid[RandomReal[1, 3], RandomReal[{-2, 2}, 3],
{RandomReal[{-π, π}],
Normalize[RandomVariate[NormalDistribution[], 3]]}]
BlockRandom[SeedRandom[42, Method -> "Legacy"];
Graphics3D[Table[{ColorData[61, RandomInteger[{1, 9}]], randomEllipsoid},
{50}], Boxed -> False, Lighting -> "Neutral"]]