{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Turning the sphere inside out {#sphere_eversion_example}\n\nThere are several videos online talking about how a sphere can be turned\ninside out in a continuous fashion, for instance in [this YouTube\nvideo](https://www.youtube.com/watch?v=OI-To1eUtuU). Thanks to [an\nexcellent paper by Adam Bednorz and Witold Bednorz, Differential and its\nApplications 64, 59\n(2019)](https://doi.org/10.1016/j.difgeo.2019.02.004) (also available\n[on arXiv](https://arxiv.org/abs/1711.10466)), we can plot this\nso-called eversion of a sphere (turning it inside out without pinching\nor tearing the surface, in other words by preserving its topology).\n\nThe mathematics involved can seem a bit, well, involved. What matters is\nthe overall process visible in the animation: first the sphere is\ncorrugated and stretched out a bit to allow some legroom for the smooth\ntransformation, then the lobes are twisted around through each other,\nand the process is reversed in order to unfold the sphere. It\\'s not\nobvious that the transformation is truly smooth; this was proved in the\npaper by Bednorz and Bednorz.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\n\nimport pyvista as pv\n\n# define some parameters\nn_steps = 30 # number of steps for a given stage of the animation\nQ = 2 / 3 # arbitrary < 1\nw = 2 # arbitrary > 0\nn = 2 # arbitrary integer > 1, the number of \"lobes\"\nbeta = 1 # arbitrary > 1\nalpha_final = 1 # arbitrary > 1\neta_final = 2 # arbitrary > 1\nkappa = (n - 1) / (2 * n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let\\'s define the chain of mappings we\\'ll need for implementing the\neversion:\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "def sphere_to_cylinder(theta, phi):\n \"\"\"Map from spherical polar coordinates to cylindrical ones.\n\n Input theta goes from -pi/2 to pi/2, phi goes from -pi to pi.\n Output h goes from -infinity to infinity, phi goes from -phi to phi.\n\n \"\"\"\n h = w * np.sin(theta) / np.cos(theta) ** n\n # phi is unchanged\n return h, phi\n\n\ndef cylinder_to_wormhole(h, phi, t, p, q):\n \"\"\"Map from a cylinder to an open wormhole using Eq. (4).\n\n Input h goes from -infinity to infinity, phi goes from -phi to phi.\n Output is an (x, y, z) point embedded in 3d space.\n\n The parameters t, p, q vary during the eversion process.\n Start from |t| > 1 (fixed), p = 1 and q = 0. End at p = 0, qt = +-1.\n\n \"\"\"\n x = t * np.cos(phi) + p * np.sin((n - 1) * phi) - h * np.sin(phi)\n y = t * np.sin(phi) + p * np.cos((n - 1) * phi) + h * np.cos(phi)\n z = h * np.sin(n * phi) - t / n * np.cos(n * phi) - q * t * h\n return x, y, z\n\n\ndef close_wormhole(x0, y0, z0, eta, xi, alpha):\n \"\"\"Close the wormhole using Eqs. (7)-(8).\n\n Input is an (x0, y0, z0) point embedded in 3d space.\n Output is an (x2, y2, z2) == (x'', y'', z'') point embedded in 3d space.\n\n The parameters eta, xi, alpha vary during the eversion process.\n\n \"\"\"\n # Eq. (7): (x, y, z) -> (x', y', z')\n denominator = xi + eta * (x**2 + y**2)\n x1 = x0 / (denominator**kappa)\n y1 = y0 / (denominator**kappa)\n z1 = z0 / denominator\n\n gamma = 2 * np.sqrt(alpha * beta)\n # singular case, Eq (9):\n if np.isclose(gamma, 0):\n denominator = x1**2 + y1**2\n x2 = x1 / denominator\n y2 = y1 / denominator\n z2 = -z1\n return x2, y2, z2\n\n # Eq. (8): (x', y', z') -> (x'', y'', z'')\n exponential = np.exp(gamma * z1)\n numerator = alpha - beta * (x1**2 + y1**2)\n denominator = alpha + beta * (x1**2 + y1**2)\n x2 = x1 * exponential / denominator\n y2 = y1 * exponential / denominator\n z2 = numerator / denominator * exponential / gamma - (alpha - beta) / (alpha + beta) / gamma\n return x2, y2, z2\n\n\ndef unfold_sphere(theta, phi, t, q, eta, lamda):\n \"\"\"Unfold the sphere using Eqs. (12), (15), (10).\n\n Input is a (theta, phi) point in spherical coordinates.\n Output is an (x, y, z) point embedded in 3d space.\n\n The parameter lamda varies. Lamda = 1 is the final stage of the\n wormhole closing, and lamda = 0 is the recovered sphere.\n\n \"\"\"\n # apply Eqs. (12), (15)\n # fmt: off\n x = (\n t * (1 - lamda + lamda * np.cos(theta)**n) * np.cos(phi)\n - lamda * w * np.sin(theta) * np.sin(phi)\n )\n x /= np.cos(theta)**n\n y = (\n t * (1 - lamda + lamda * np.cos(theta)**n) * np.sin(phi)\n + lamda * w * np.sin(theta) * np.cos(phi)\n )\n y /= np.cos(theta) ** n\n z = (\n lamda * (\n (w * np.sin(theta) * (np.sin(n * phi) - q * t)) / np.cos(theta)**n\n - t / n * np.cos(n * phi)\n )\n - (1 - lamda) * eta**(1 + kappa) * t * abs(t)**(2 * kappa)\n * np.sin(theta) / np.cos(theta)**(2 * n) # noqa: E131\n )\n # fmt: on\n\n # apply Eq. (10)\n denominator = x**2 + y**2\n x2 = x * eta**kappa / denominator ** (1 - kappa)\n y2 = y * eta**kappa / denominator ** (1 - kappa)\n z2 = -z / eta / denominator\n return x2, y2, z2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now chain the functions by performing the process in Table 1 of the\npaper. Start from the bottom for `t = -1/Q`, keep stepping up, linearly\nchanging parameters that change from row to row, then at the top go from\n`t = -1/Q` to `t = 1/Q`, then go back from top to bottom. Save each\nframe to a GIF.\n\nWe make good use of the `backface_params` keyword parameter of\n`pyvista.Plotter.add_mesh`{.interpreted-text role=\"func\"}, allowing us\nto plot the inside and the outside with different colors.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# plot options to use for each frame\nopts = dict(\n color='aquamarine',\n specular=1.0,\n specular_power=50.0,\n backface_params=dict(color='forestgreen'),\n smooth_shading=True,\n reset_camera=True,\n)\n\n# use a small figure window to reduce the size of the GIF\nplotter = pv.Plotter(window_size=(300, 300))\nplotter.open_gif('sphere_eversion.gif')\n\n\ndef save_frame(x, y, z):\n \"\"\"Helper to generate and store a frame of the eversion.\"\"\"\n plotter.clear()\n plotter.enable_lightkit()\n plotter.add_mesh(pv.StructuredGrid(x, y, z), **opts)\n plotter.write_frame()\n\n\n# initial parameters, will be updated\nt = -1 / Q\nq = Q\np = xi = alpha = 0\neta = 1\n\n# sphere -> inverted wormhole\ntheta, phi = np.mgrid[-np.pi / 2 : np.pi / 2 : 200j, -np.pi : np.pi : 400j]\nh, phi = sphere_to_cylinder(theta, phi)\nfor lamda in np.linspace(0, 1, n_steps, endpoint=False):\n x2, y2, z2 = unfold_sphere(theta, phi, t, q, eta, lamda)\n save_frame(x2, y2, z2)\n\n# inverted wormhole -> unfolded wormhole\nx, y, z = cylinder_to_wormhole(h, phi, t, p, q)\nxis = np.linspace(0, 1, n_steps)\nalphas = np.linspace(0, alpha_final, n_steps)\netas = np.linspace(1, eta_final, n_steps)\nfor xi, alpha, eta in zip(xis, alphas, etas):\n x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha)\n save_frame(x2, y2, z2)\n\n# unfolded wormhole -> closed wormhole\nfor q in np.linspace(Q, 0, n_steps):\n p = 1 - abs(q * t)\n x, y, z = cylinder_to_wormhole(h, phi, t, p, q)\n x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha)\n save_frame(x2, y2, z2)\n\n# closed wormhole turned inside out (flip sign of time)\n# unfolded wormhole -> closed wormhole\nfor t in np.linspace(-1 / Q, 1 / Q, n_steps):\n p = 1 - abs(q * t)\n x, y, z = cylinder_to_wormhole(h, phi, t, p, q)\n x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha)\n save_frame(x2, y2, z2)\n\n# closed wormhole -> unfolded wormhole\nfor q in np.linspace(0, Q, n_steps + 1)[1:]:\n p = 1 - abs(q * t)\n x, y, z = cylinder_to_wormhole(h, phi, t, p, q)\n x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha)\n save_frame(x2, y2, z2)\n\n# unfolded wormhole -> inverted wormhole\nx, y, z = cylinder_to_wormhole(h, phi, t, p, q)\nxis = np.linspace(1, 0, n_steps + 1)[1:]\nalphas = np.linspace(alpha_final, 0, n_steps + 1)[1:]\netas = np.linspace(eta_final, 1, n_steps + 1)[1:]\nfor xi, alpha in zip(xis, alphas):\n x2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha)\n save_frame(x2, y2, z2)\n\n# inverted wormhole -> sphere\nfor lamda in np.linspace(1, 0, n_steps + 1)[1:]:\n x2, y2, z2 = unfold_sphere(theta, phi, t, q, eta, lamda)\n save_frame(x2, y2, z2)\n\nplotter.close()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Looking at the still image of the middle state with `t = 0`, we see a\nnice symmetric configuration where two \\\"inside\\\" and two \\\"outside\\\"\nlobes of the sphere are visible.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "t = q = 0\nxi = p = 1\neta = eta_final\nalpha = alpha_final\n\nx, y, z = cylinder_to_wormhole(h, phi, t, p, q)\nx2, y2, z2 = close_wormhole(x, y, z, eta, xi, alpha)\n\nplotter = pv.Plotter(window_size=(512, 512))\nplotter.add_mesh(pv.StructuredGrid(x2, y2, z2), **opts)\nplotter.show()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.2" } }, "nbformat": 4, "nbformat_minor": 0 }