Skip to content

Instantly share code, notes, and snippets.

@stla
Created October 12, 2023 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stla/2e00dbb08079bc41b7eb26544c2608f6 to your computer and use it in GitHub Desktop.
Save stla/2e00dbb08079bc41b7eb26544c2608f6 to your computer and use it in GitHub Desktop.
Hopf torus with Julia
using Meshes
using MeshViz
using LinearAlgebra
using GLMakie
using Makie
using ColorSchemes
using Printf
# parameterization of the Hopf torus
function HopfTorus(u, v, nlobes, A)
cos_v = cos(v)
sin_v = sin(v)
B = pi / 2 - (pi / 2 - A) * cos(u * nlobes)
C = u + A * sin(2 * u * nlobes)
y1 = 1 + cos(B)
y23 = sin(B) .* [cos(C), sin(C)]
y2 = y23[1]
y3 = y23[2]
x1 = y3 * cos_v + y2 * sin_v
x2 = y2 * cos_v - y3 * sin_v
x3 = y1 * sin_v
x4 = y1 * cos_v
yden = sqrt(2 * y1)
[x2 / (yden - x4), x3 / (yden - x4), x1 / (yden - x4)]
end
# make mesh
nu = 300
nv = 200
u_ = LinRange(0, 2*pi, nu+1)[1:nu]
v_ = LinRange(0, 2*pi, nv+1)[1:nv]
points = [tuple(HopfTorus(u, v, 3, 0.44)...) for u in u_ for v in v_]
norms = [norm(pt) for pt in points]
scalars =
2 * pi * (norms .- minimum(norms)) / (maximum(norms) - minimum(norms))
topo = GridTopology((nv, nu), (true, true))
mesh0 = SimpleMesh(Meshes.Point.(points), topo)
mesh = refine(mesh0, TriRefinement())
# colors palette
function fpalette(t)
u_ = (sin.(scalars .- t) .+ 1) / 2
[get(ColorSchemes.buda, u) for u in u_]
end
# draw the Hopf torus
function drawHT(nplots)
t_ = LinRange(0, 2*pi, nplots+1)[1:nplots]
for i in 1:nplots
fig, ax, _ = viz( # invisible bounding box
Meshes.Box(Meshes.Point(-9, -5, -6), Meshes.Point(9, 5, 10)); alpha = 0
)
ax.show_axis = false
colors = fpalette(t_[i])
viz!(mesh; color = colors)
scale!(ax.scene, 1.65, 1.65, 1.65)
Makie.rotate!(ax.scene, Makie.Vec3f(-1, 0, 1), -pi/4)
png = @sprintf "zzpic%03d.png" i
Makie.save(png, fig)
end
end
@stla
Copy link
Author

stla commented Oct 12, 2023

HopfTorus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment