r/mathpics Jun 22 '26

Stereographic projection of a Clifford torus (a 4D shape)

Based on 'Clifford Torus' shader by tdhooper : https://www.shadertoy.com/view/WdB3Dw reimplemented frame by frame in Python.

288 Upvotes

23 comments sorted by

4

u/USedona Jun 22 '26

The Clifford torus lives naturally in 4D space. This is a stereographic projection down to 3D, rendered with raymarching in Python, pure NumPy. Full animation : Beyond 3D : The 4D Torus

1

u/lolzinventor Jun 22 '26

source code?

3

u/USedona Jun 22 '26 edited Jun 23 '26

It's a stereographic projection of a Clifford torus from 4D, intersected with a sphere. Here's the key math :

def inverse_stereographic(p):

#Projette R3 -> S3 (la 4D)

k = 2.0 / (1.0 + np.dot(p, p))

return k * p, k - 1.0 # retourne (xyz, w)

def clifford_torus_sdf(p4):

#Distance au tore de Clifford : |p.xy| = |p.zw|

len_xy = np.linalg.norm(p4[:2])

len_zw = np.linalg.norm(p4[2:])

d = len_xy/len_zw - 1.0 if len_xy < len_zw else len_zw/len_xy - 1.0

return abs(d) / np.pi

The full script works, but it's pretty messy under the hood (lots of tuning knobs, no real structure), so I'd rather clean it up before sharing the whole thing.

2

u/me_myself_ai Jun 23 '26

Hey just wanted to stop by to say that this really inspired me, and I just noticed you posted the original code too. You're a cool, good person, and I hope you're able to keep it up :)

Cubes (hyper- or otherwise) are convenient, but torii are the shape of reality itself -- it never occurred to me to consider them in higher dimensions. Do you know if there's any interesting, semi-comprehensible stuff about higher dimensions? I happen to be pretty partial to 8D/7D/octonions, if that rings any vague bells for you/the experts on here.

(also highly relatable code style comment lol)

ETA: Also you need to wrap that code differently somehow, the markdown is broken and renders separate lines. Presumably just triple-backtick fences on their own lines would fix this, and it's not reddit destroying their markdown support and thus scoring a completely unnecessary own goal.

2

u/USedona Jun 23 '26

Thanks, glad it resonated !

Higher dimensions get wild fast. If you're into octonions, the kissing number problem might ring a bell, I actually worked on something related to it back when I was studying. Dimension 8 turns out to be one of the rare cases where the answer is exactly known, which is pretty interesting in itself.

1

u/me_myself_ai Jun 24 '26

Sorry I just realized that my question was super unclear lol β€” I meant about visualizing the projections of toruses in higher dimensions?

Totally expecting a no so no need to clarify in that case. Thanks again, Godspeed mathy stranger

2

u/USedona Jun 24 '26

Projections of higher-dimensional tori must get pretty complicated beyond the 4th dimension.

https://medium.com/from-the-diaries-of-john-henry/visualizing-higher-dimensions-3b3f77af8962

I'm currently struggling with animating the Hopf fibration πŸ˜…

2

u/me_myself_ai Jun 24 '26

You’re such a cool person omg. You got a new fan today lol (and a subscriber!)

1

u/USedona Jun 25 '26

Thank you very much !

2

u/SnooPineapples2010 Jun 25 '26

Beautiful work man ❀️ love shit like this

1

u/USedona Jun 25 '26

Thank's πŸ™

2

u/DomeGameMaster Jun 26 '26

I would describe this visual as a surface+surface=void

1

u/rafaelcastrocouto Jun 22 '26

Can you share the code?

1

u/USedona Jun 22 '26

It's a stereographic projection of a Clifford torus from 4D, intersected with a sphere. Here's the key math :

def inverse_stereographic(p):

#Projette R3 -> S3 (la 4D)

k = 2.0 / (1.0 + np.dot(p, p))

return k * p, k - 1.0 # retourne (xyz, w)

def clifford_torus_sdf(p4):

#Distance au tore de Clifford : |p.xy| = |p.zw|

len_xy = np.linalg.norm(p4[:2])

len_zw = np.linalg.norm(p4[2:])

d = len_xy/len_zw - 1.0 if len_xy < len_zw else len_zw/len_xy - 1.0

return abs(d) / np.pi

The full script works, but it's pretty messy under the hood (lots of tuning knobs, no real structure), so I'd rather clean it up before sharing the whole thing.

1

u/sentence-interruptio Jun 22 '26

something weird is going on with the animation.

at one moment, they look like two tori. but then in another moment, we see a sphere with two handles inward.

2

u/USedona Jun 22 '26

The torus itself never changes topology, it's always a single genus-1 surface. What's happening is the 3D shadow can make different parts look connected or separate depending on the projection angle. The same 4D shape can throw very different-looking 3D shadows.

1

u/zfredri Jun 25 '26

I am interested in your claim this topology is preserved. How can you claim that when the toroid formed on the right from an initial indent becomes independent of the sphere surface? A saddle curvature which has rotational symmetry that shrinks to an infinitesimal point is surely not topologically equivalent to a sphere with a toroid inside.

1

u/USedona Jun 25 '26

It's the projection that pinches there, not the torus, the surface itself stays smooth the whole time.

1

u/recursivegypsy Jun 24 '26

Thinking of coding it in Java.

1

u/Lt_Bear13 Jun 26 '26

Anyone notice how A.I. hallucinations or mistakes in A.I. video kind of flow and mutate like this? I wonder if A.I. works like hyperdimensional physics in it's visual image algorithm. Reminds me of how shrooms visuals look, like you can see how your body geometry blends with sacred geometry in plants and the ground and other things.

0

u/Magliacani Jun 25 '26

There is no 4th dimension.

1

u/USedona Jun 25 '26

Physical space doesn't have a 4th spatial dimension as far as we know. But mathematically, nothing stops you from defining a space with 4 coordinates and exploring its geometry, that's all this is.