Created
September 29, 2023 13:57
-
-
Save stla/32d5898e053432ce40e44303c2c779f0 to your computer and use it in GitHub Desktop.
Clifford torus handle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(rgl) | |
library(cgalMeshes) | |
p <- function(theta, phi) { | |
c( | |
cos(theta)*cos(phi), cos(theta)*sin(phi), | |
sin(theta)*cos(phi), sin(theta)*sin(phi) | |
) | |
} | |
s <- Vectorize(function(theta, phi) { | |
q <- p(theta, phi) | |
q[1L:3L] / (1 - q[4L]) | |
}) | |
# with blue and yellow ribbons | |
open3d(windowRect = 50 + c(0, 0, 512, 512), zoom = 0.95) | |
for(i in c(1:5, 11:15)) { | |
a <- 2 * (i-1) * pi/30 | |
mesh <- parametricMesh( | |
s, c(0, 2*pi), c(a, a + pi/30), periodic = c(TRUE, FALSE), | |
nu = 300, nv = 20 | |
) | |
shade3d(addNormals(mesh), col = "navy") | |
mesh <- parametricMesh( | |
s, c(0, 2*pi), c(a + pi/30, a + pi/15), periodic = c(TRUE, FALSE), | |
nu = 300, nv = 20 | |
) | |
shade3d(addNormals(mesh), col = "yellow") | |
} | |
# tubular | |
theta_ <- seq(0, 2*pi, length.out = 300L) | |
open3d(windowRect = 50 + c(0, 0, 512, 512), zoom = 0.9) | |
k <- 1L | |
for(i in c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L)) { | |
k <- -k | |
phi <- pi/2 + k * (pi/9 + i * pi/15) | |
pts <- t(s(theta_, phi)) | |
mesh <- cylinder3d(pts, radius = 0.07, sides = 30, closed = TRUE) | |
shade3d(addNormals(mesh), col = "navy") | |
} | |
# # -- if you want an animation | |
M <- par3d("userMatrix") | |
movie3d( | |
par3dinterp( | |
time = seq(0, 1, len = 9), | |
userMatrix = list( | |
M, | |
rotate3d(M, pi, 1, 0, 0), | |
rotate3d(M, pi, 1, 1, 0), | |
rotate3d(M, pi, 1, 1, 1), | |
rotate3d(M, pi, 0, 1, 1), | |
rotate3d(M, pi, 0, 1, 0), | |
rotate3d(M, pi, 1, 0, 1), | |
rotate3d(M, pi, 0, 0, 1), | |
M | |
) | |
), | |
fps = 100, | |
duration = 1, | |
dir = ".", | |
movie = "zzpic", | |
convert = FALSE, webshot = FALSE | |
) | |
library(gifski) | |
gifski( | |
png_files = Sys.glob("zzpic*.png"), | |
gif_file = "CliffordTorusHandle_tubular.gif", | |
width = 512, | |
height = 512, | |
delay = 1/8 | |
) | |
file.remove(Sys.glob("zzpic*.png")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment