Skip to content

Instantly share code, notes, and snippets.

@stla
Last active October 13, 2023 10:27
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/d600b70ba1568b90ef3be55ca4e1448e to your computer and use it in GitHub Desktop.
Save stla/d600b70ba1568b90ef3be55ca4e1448e to your computer and use it in GitHub Desktop.
Higher-dimensional stuff with 'rgl' (R)
zzpic*
.Rproj.user
*.Rproj
.Rhistory
library(rgl)
library(rmarchingcubes)
a <- 0
# f <- function(x, y, z, a, t){
# (((x*cos(t)-a*sin(t))^2+y^2+z^2+(x*sin(t)+a*cos(t))^2+145/3)^2-4*(9*z^2+16*(x*sin(t)+a*cos(t))^2))^2*(((x*cos(t)-a*sin(t))^2+y^2+z^2+(x*sin(t)+a*cos(t))^2+145/3)^2+296*((x*cos(t)-a*sin(t))^2+y^2)-4*(9*z^2+16*(x*sin(t)+a*cos(t))^2)) -16*((x*cos(t)-a*sin(t))^2+y^2)*((x*cos(t)-a*sin(t))^2+y^2+z^2+(x*sin(t)+a*cos(t))^2+145/3)^2*(37*((x*cos(t)-a*sin(t))^2+y^2+z^2+(x*sin(t)+a*cos(t))^2+145/3)^2-1369*((x*cos(t)-a*sin(t))^2+y^2)-7*(225*z^2+448*(x*sin(t)+a*cos(t))^2)) -16*sqrt(3)/9*((x*cos(t)-a*sin(t))^3-3*(x*cos(t)-a*sin(t))*y^2)*(110*((x*cos(t)-a*sin(t))^2+y^2+z^2+(x*sin(t)+a*cos(t))^2+145/3)^3 -148*((x*cos(t)-a*sin(t))^2+y^2+z^2+(x*sin(t)+a*cos(t))^2+145/3)*(110*(x*cos(t)-a*sin(t))^2+110*y^2-297*z^2+480*(x*sin(t)+a*cos(t))^2)) -64*((x*cos(t)-a*sin(t))^2+y^2)*(3*(729*z^4+4096*(x*sin(t)+a*cos(t))^4)+168*((x*cos(t)-a*sin(t))^2+y^2)*(15*z^2-22*(x*sin(t)+a*cos(t))^2)) +64*(12100/27*((x*cos(t)-a*sin(t))^3-3*(x*cos(t)-a*sin(t))*y^2)^2 -7056*(3*(x*cos(t)-a*sin(t))^2*y-y^3)^2) -592240896*z^2*(x*sin(t)+a*cos(t))^2
# }
# since a = 0
f <- function(x, y, z, t){
(((x*cos(t))^2+y^2+z^2+(x*sin(t))^2+145/3)^2-4*(9*z^2+16*(x*sin(t))^2))^2*(((x*cos(t))^2+y^2+z^2+(x*sin(t))^2+145/3)^2+296*((x*cos(t))^2+y^2)-4*(9*z^2+16*(x*sin(t))^2)) -16*((x*cos(t))^2+y^2)*((x*cos(t))^2+y^2+z^2+(x*sin(t))^2+145/3)^2*(37*((x*cos(t))^2+y^2+z^2+(x*sin(t))^2+145/3)^2-1369*((x*cos(t))^2+y^2)-7*(225*z^2+448*(x*sin(t))^2)) -16*sqrt(3)/9*((x*cos(t))^3-3*(x*cos(t))*y^2)*(110*((x*cos(t))^2+y^2+z^2+(x*sin(t))^2+145/3)^3 -148*((x*cos(t))^2+y^2+z^2+(x*sin(t))^2+145/3)*(110*(x*cos(t))^2+110*y^2-297*z^2+480*(x*sin(t))^2)) -64*((x*cos(t))^2+y^2)*(3*(729*z^4+4096*(x*sin(t))^4)+168*((x*cos(t))^2+y^2)*(15*z^2-22*(x*sin(t))^2)) +64*(12100/27*((x*cos(t))^3-3*(x*cos(t))*y^2)^2 -7056*(3*(x*cos(t))^2*y-y^3)^2) -592240896*z^2*(x*sin(t))^2
}
nx <- 200; ny <- 200; nz <- 200
x <- seq(-8, 9, length.out = nx)
y <- seq(-9, 9, length.out = ny)
z <- seq(-6, 6, length.out = nz)
G <- expand.grid(x = x, y = y, z = z)
userMatrix <- rbind(
c( 0.69, -0.71, 0.12, 0),
c( 0.00, 0.16, 0.99, 0),
c(-0.72, -0.68, 0.11, 0),
c( 0.00, 0.00, 0.00, 1)
)
t_ <- seq(0, pi, length.out = 100)
for(i in seq_along(t_)){
# run the marching cubes algorithm ####
voxel <- array(with(G, f(x, y, z, t_[i])), dim = c(nx, ny, nz))
cont <- contour3d(
voxel, level = 0, x = x, y = y, z = z
)
# make the mesh
mesh <- tmesh3d(
vertices = t(cont$vertices),
indices = t(cont$triangles),
normals = cont$normals,
homogeneous = FALSE
)
# plot
open3d(windowRect = c(50, 50, 562, 562))
bg3d(
sphere = FALSE, texture = "SpaceBackground.png", col = "white"
)
par3d(userMatrix = userMatrix, zoom = 0.85)
shade3d(mesh, color = "turquoise4")
rgl.snapshot(sprintf("zzzpic%03d.png", i))
close3d()
}
# animation
command <-
"magick convert -dispose previous -delay 1x10 -duplicate 1,-2-1 zzzpic*.png ICN5D_anim01.gif"
system(command)
pngs <- list.files(".", pattern = "^zzzpic", full.names = TRUE)
file.remove(pngs)
# https://thumbs.gfycat.com/SnoopyDeliciousGuineapig-size_restricted.gif
View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

View raw

(Sorry about that, but we can’t show files that are this big right now.)

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