| 1 |
""" |
| 2 |
.. _sphere_eversion_example: |
| 3 |
|
| 4 |
Turning the sphere inside out |
| 5 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
There are several videos online talking about how a sphere can be turned inside |
| 8 |
out in a continuous fashion, for instance in `this YouTube video |
| 9 |
<https://www.youtube.com/watch?v=OI-To1eUtuU>`_. Thanks to `an excellent paper |
| 10 |
by Adam Bednorz and Witold Bednorz, Differential and its Applications 64, 59 |
| 11 |
(2019) <https://doi.org/10.1016/j.difgeo.2019.02.004>`_ (also available `on |
| 12 |
arXiv <https://arxiv.org/abs/1711.10466>`_), we can plot this so-called |
| 13 |
eversion of a sphere (turning it inside out without pinching or tearing the |
| 14 |
surface, in other words by preserving its topology). |
| 15 |
|
| 16 |
The mathematics involved can seem a bit, well, involved. What matters is the |
| 17 |
overall process visible in the animation: first the sphere is corrugated and |
| 18 |
stretched out a bit to allow some legroom for the smooth transformation, then |
| 19 |
the lobes are twisted around through each other, and the process is reversed in |
| 20 |
order to unfold the sphere. It's not obvious that the transformation is truly |
| 21 |
smooth; this was proved in the paper by Bednorz and Bednorz. |
| 22 |
|
| 23 |
""" |
| 24 |
|
| 25 |
# sphinx_gallery_thumbnail_number = 2 |
| 26 |
import numpy as np |
| 27 |
import pyvista as pv |
| 28 |
|
| 29 |
# define some parameters |
| 30 |
n_steps = 15 # number of steps for a given stage of the animation |
| 31 |
Q = 2 / 3 # arbitrary < 1 |
| 32 |
w = 2 # arbitrary > 0 |
| 33 |
n = 2 # arbitrary integer > 1, the number of "lobes" |
| 34 |
beta = 1 # arbitrary > 1 |
| 35 |
alpha_final = 1 # arbitrary > 1 |
| 36 |
eta_final = 2 # arbitrary > 1 |
| 37 |
kappa = (n - 1) / (2 * n) |
| 38 |
|
| 39 |
# %% |
| 40 |
# Let's define the chain of mappings we'll need for implementing the eversion: |
| 41 |
|
| 42 |
|
| 43 |
def sphere_to_cylinder(theta, phi): |
| 44 |
"""Map from spherical polar coordinates to cylindrical ones. |
| 45 |
|
| 46 |
Input theta goes from -pi/2 to pi/2, phi goes from -pi to pi. |
| 47 |
Output h goes from -infinity to infinity, phi goes from -phi to phi. |
| 48 |
|
| 49 |
""" |
| 50 |
h = w * np.sin(theta) / np.cos(theta) ** n |
| 51 |
# phi is unchanged |
| 52 |
return h, phi |
| 53 |
|
| 54 |
|
| 55 |
def cylinder_to_wormhole(h, phi, t, p, q): # noqa: PLR0917 |
| 56 |
""" |
| 57 |
Map from a cylinder to an open wormhole using Eq. (4). |
| 58 |
|
| 59 |
Input h goes from -infinity to infinity, phi goes from -phi to phi. |
| 60 |
Output is an (x, y, z) point embedded in 3d space. |
| 61 |
|
| 62 |
The parameters t, p, q vary during the eversion process. |
| 63 |
Start from |t| > 1 (fixed), p = 1 and q = 0. End at p = 0, qt = +-1. |
| 64 |
|
| 65 |
""" |
| 66 |
x = t * np.cos(phi) + p * np.sin((n - 1) * phi) - h * np.sin(phi) |
| 67 |
y = t * np.sin(phi) + p * np.cos((n - 1) * phi) + h * np.cos(phi) |
| 68 |
z = h * np.sin(n * phi) - t / n * np.cos(n * phi) - q * t * h |
| 69 |
return x, y, z |
| 70 |
|
| 71 |
|
| 72 |
def close_wormhole(x0, y0, z0, eta, xi, alpha): # noqa: PLR0917 |
| 73 |
""" |
| 74 |
Close the wormhole using Eqs. (7)-(8). |
| 75 |
|
| 76 |
Input is an (x0, y0, z0) point embedded in 3d space. |
| 77 |
Output is an (x2, y2, z2) == (x'', y'', z'') point embedded in 3d space. |
| 78 |
|
| 79 |
The parameters eta, xi, alpha vary during the eversion process. |
| 80 |
|
| 81 |
""" |
| 82 |
# Eq. (7): (x, y, z) -> (x', y', z') |
| 83 |
denominator = xi + eta * (x**2 + y**2) |
| 84 |
x1 = x0 / (denominator**kappa) |
| 85 |
y1 = y0 / (denominator**kappa) |
| 86 |
z1 = z0 / denominator |
| 87 |
|
| 88 |
gamma = 2 * np.sqrt(alpha * beta) |
| 89 |
# singular case, Eq (9): |
| 90 |
if np.isclose(gamma, 0): |
| 91 |
denominator = x1**2 + y1**2 |
| 92 |
x2 = x1 / denominator |
| 93 |
y2 = y1 / denominator |
| 94 |
z2 = -z1 |
| 95 |
return x2, y2, z2 |
| 96 |
|
| 97 |
# Eq. (8): (x', y', z') -> (x'', y'', z'') |
| 98 |
exponential = np.exp(gamma * z1) |
| 99 |
numerator = alpha - beta * (x1**2 + y1**2) |
| 100 |
denominator = alpha + beta * (x1**2 + y1**2) |
| 101 |
x2 = x1 * exponential / denominator |
| 102 |
y2 = y1 * exponential / denominator |
| 103 |
z2 = ( |
| 104 |
numerator / denominator * exponential / gamma |
| 105 |
- (alpha - beta) / (alpha + beta) / gamma |
| 106 |
) |
| 107 |
return x2, y2, z2 |
| 108 |
|
| 109 |
|
| 110 |
def unfold_sphere(theta, phi, t, q, eta, lamda): # noqa: PLR0917 |
| 111 |
""" |
| 112 |
Unfold the sphere using Eqs. (12), (15), (10). |
| 113 |
|
| 114 |
Input is a (theta, phi) point in spherical coordinates. |
| 115 |
Output is an (x, y, z) point embedded in 3d space. |
| 116 |
|
| 117 |
The parameter lamda varies. Lamda = 1 is the final stage of the |
| 118 |
wormhole closing, and lamda = 0 is the recovered sphere. |
| 119 |
|
| 120 |
""" |
| 121 |
# apply Eqs. (12), (15) |
| 122 |
# fmt: off |
| 123 |
x = ( |
| 124 |
t * (1 - lamda + lamda * np.cos(theta)**n) * np.cos(phi) |
| 125 |
- lamda * w * np.sin(theta) * np.sin(phi) |
| 126 |
) |
| 127 |
x /= np.cos(theta)**n |
| 128 |
y = ( |
| 129 |
t * (1 - lamda + lamda * np.cos(theta)**n) * np.sin(phi) |
| 130 |
+ lamda * w * np.sin(theta) * np.cos(phi) |
| 131 |
) |
| 132 |
y /= np.cos(theta) ** n |
| 133 |
z = ( |
| 134 |
lamda * ( |
| 135 |
(w * np.sin(theta) * (np.sin(n * phi) - q * t)) / np.cos(theta)**n |
| 136 |
- t / n * np.cos(n * phi) |
| 137 |
) |
| 138 |
- (1 - lamda) * eta**(1 + kappa) * t * abs(t)**(2 * kappa) |
| 139 |
* np.sin(theta) / np.cos(theta)**(2 * n) |
| 140 |
) |
| 141 |
# fmt: on |
| 142 |
|
| 143 |
# apply Eq. (10) |
| 144 |
denominator = x**2 + y**2 |
| 145 |
x2 = x * eta**kappa / denominator ** (1 - kappa) |
| 146 |
y2 = y * eta**kappa / denominator ** (1 - kappa) |
| 147 |
z2 = -z / eta / denominator |
| 148 |
return x2, y2, z2 |
| 149 |
|
| 150 |
|
| 151 |
# %% |
| 152 |
# Now chain the functions by performing the process in Table 1 of the paper. |
| 153 |
# Start from the bottom for ``t = -1/Q``, keep stepping up, linearly changing |
| 154 |
# parameters that change from row to row, then at the top go from ``t = -1/Q`` |
| 155 |
# to ``t = 1/Q``, then go back from top to bottom. Save each frame to a GIF. |
| 156 |
# |
| 157 |
# We make good use of the ``backface_params`` keyword parameter of |
| 158 |
# :func:`pyvista.Plotter.add_mesh`, allowing us to plot the inside and the |
| 159 |
# outside with different colors. |
| 160 |
|
| 161 |
# plot options to use for each frame |
| 162 |
opts = dict( |
| 163 |
color='aquamarine', |
| 164 |
specular=1.0, |
| 165 |
specular_power=50.0, |
| 166 |
backface_params=dict(color='forestgreen'), |
| 167 |
smooth_shading=True, |
| 168 |
reset_camera=True, |
| 169 |
) |
| 170 |
|
| 171 |
# use a small figure window to reduce the size of the GIF |
| 172 |
pl = pv.Plotter(window_size=(300, 300)) |
| 173 |
pl.open_gif('sphere_eversion.gif') |
| 174 |
|
| 175 |
|
| 176 |
def save_frame(x, y, z): |
| 177 |
"""Generate and store a frame of the eversion.""" |
| 178 |
pl.clear() |
| 179 |
pl.enable_lightkit() |
| 180 |
pl.add_mesh(pv.StructuredGrid(x, y, z), **opts) |
| 181 |
pl.write_frame() |
| 182 |
|
| 183 |
|
| 184 |
# initial parameters, will be updated |
| 185 |
t = -1 / Q |
| 186 |
q = Q |
| 187 |
p = xi = alpha = 0 |
| 188 |
eta = 1 |
| 189 |
|
| 190 |
# sphere -> inverted wormhole |
| 191 |
theta, phi = np.mgrid[-np.pi / 2 : np.pi / 2 : 200j, -np.pi : np.pi : 400j] |
| 192 |
h, phi = sphere_to_cylinder(theta, phi) |
| 193 |
for lamda in np.linspace(0, 1, n_steps, endpoint=False): |
| 194 |
x2, y2, z2 = unfold_sphere(theta, phi, t, q, eta, lamda) |
| 195 |
save_frame(x2, y2, z2) |
| 196 |
|
| 197 |
# inverted wormhole -> unfolded wormhole |
| 198 |
x, y, z = cylinder_to_wormhole(h, phi, t, p, q) |
| 199 |
xis = np.linspace(0, 1, n_steps) |
| 200 |
alphas = np.linspace(0, alpha_final, n_steps) |
| 201 |
etas = np.linspace(1, eta_final, n_steps) |
| 202 |
for xi, alpha, eta in zip(xis, alphas, etas, strict=True): |
| 203 |
x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha) |
| 204 |
save_frame(x2, y2, z2) |
| 205 |
|
| 206 |
# unfolded wormhole -> closed wormhole |
| 207 |
for q in np.linspace(Q, 0, n_steps): |
| 208 |
p = 1 - abs(q * t) |
| 209 |
x, y, z = cylinder_to_wormhole(h, phi, t, p, q) |
| 210 |
x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha) |
| 211 |
save_frame(x2, y2, z2) |
| 212 |
|
| 213 |
# closed wormhole turned inside out (flip sign of time) |
| 214 |
# unfolded wormhole -> closed wormhole |
| 215 |
for t in np.linspace(-1 / Q, 1 / Q, n_steps): |
| 216 |
p = 1 - abs(q * t) |
| 217 |
x, y, z = cylinder_to_wormhole(h, phi, t, p, q) |
| 218 |
x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha) |
| 219 |
save_frame(x2, y2, z2) |
| 220 |
|
| 221 |
# closed wormhole -> unfolded wormhole |
| 222 |
for q in np.linspace(0, Q, n_steps + 1)[1:]: |
| 223 |
p = 1 - abs(q * t) |
| 224 |
x, y, z = cylinder_to_wormhole(h, phi, t, p, q) |
| 225 |
x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha) |
| 226 |
save_frame(x2, y2, z2) |
| 227 |
|
| 228 |
# unfolded wormhole -> inverted wormhole |
| 229 |
x, y, z = cylinder_to_wormhole(h, phi, t, p, q) |
| 230 |
xis = np.linspace(1, 0, n_steps + 1)[1:] |
| 231 |
alphas = np.linspace(alpha_final, 0, n_steps + 1)[1:] |
| 232 |
etas = np.linspace(eta_final, 1, n_steps + 1)[1:] |
| 233 |
for xi, alpha in zip(xis, alphas, strict=True): |
| 234 |
x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha) |
| 235 |
save_frame(x2, y2, z2) |
| 236 |
|
| 237 |
# inverted wormhole -> sphere |
| 238 |
for lamda in np.linspace(1, 0, n_steps + 1)[1:]: |
| 239 |
x2, y2, z2 = unfold_sphere(theta, phi, t, q, eta, lamda) |
| 240 |
save_frame(x2, y2, z2) |
| 241 |
|
| 242 |
pl.close() |
| 243 |
|
| 244 |
|
| 245 |
# %% |
| 246 |
# Looking at the still image of the middle state with ``t = 0``, we see a nice |
| 247 |
# symmetric configuration where two "inside" and two "outside" lobes of the |
| 248 |
# sphere are visible. |
| 249 |
|
| 250 |
# sphinx_gallery_start_ignore |
| 251 |
# lighting does not work for this interactive plot |
| 252 |
PYVISTA_GALLERY_FORCE_STATIC = True |
| 253 |
# sphinx_gallery_end_ignore |
| 254 |
|
| 255 |
t = q = 0 |
| 256 |
xi = p = 1 |
| 257 |
eta = eta_final |
| 258 |
alpha = alpha_final |
| 259 |
|
| 260 |
x, y, z = cylinder_to_wormhole(h, phi, t, p, q) |
| 261 |
x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha) |
| 262 |
|
| 263 |
pl = pv.Plotter(window_size=(512, 512)) |
| 264 |
pl.add_mesh(pv.StructuredGrid(x2, y2, z2), **opts) |
| 265 |
pl.show() |