Skip to content

Instantly share code, notes, and snippets.

@stla
Created October 22, 2018 08:15
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/9c036c2ae9c678745b784ed8d895a518 to your computer and use it in GitHub Desktop.
Save stla/9c036c2ae9c678745b784ed8d895a518 to your computer and use it in GitHub Desktop.
Twenty Hopf tori with rgl
source('HopfTorusMesh.R') # https://gist.github.com/stla/85224a6639ccf165f11fa1a2bf987f16#file-hopftorus_mesh3d-r
source('ConeMesh00.R') # https://gist.github.com/stla/b3db4a593c9b030567bde701d45fa385
library(rgl)
Reorient_Trans <- function(Axis1, Axis2){
vX1 <- Axis1 / sqrt(c(crossprod(Axis1)))
vX2 <- Axis2 / sqrt(c(crossprod(Axis2)))
Y <- rgl:::xprod(vX1, vX2)
vY <- Y / sqrt(c(crossprod(Y)))
Z1 <- rgl:::xprod(vX1, vY)
vZ1 <- Z1 / sqrt(c(crossprod(Z1)))
Z2 <- rgl:::xprod(vX2, vY)
vZ2 <- Z2 / sqrt(c(crossprod(Z2)))
M1 <- cbind(vX1, vY, vZ1)
M2 <- rbind(vX2, vY, vZ2)
M <- M1 %*% M2
rbind(cbind(M, c(0,0,0)), c(0,0,0,1))
}
# vertices ####
phi <- (1+sqrt(5))/2
a <- 1/sqrt(3)
b <- a/phi
c <- a*phi
vertices <-
rbind(
c( a, a, a),
c( a, a, -a),
c( a, -a, a),
c(-a, -a, a),
c(-a, a, -a),
c(-a, a, a),
c( 0, b, -c),
c( 0, -b, -c),
c( 0, -b, c),
c( c, 0, -b),
c(-c, 0, -b),
c(-c, 0, b),
c( b, c, 0),
c( b, -c, 0),
c(-b, -c, 0),
c(-b, c, 0),
c( 0, b, c),
c( a, -a, -a),
c( c, 0, b),
c(-a, -a, -a)
)
# Hopf mesh
HTmesh0 <- hmesh(200, 200)
# rgl ####
colors <- randomcoloR::distinctColorPalette(20)
sc <- 0.035
HTmesh <- scale3d(HTmesh0, sc, sc, sc)
open3d(windowRect=c(50,50,550,550))
bg3d(rgb(54,57,64, maxColorValue = 255))
for(i in 1:20){
v <- vertices[i,]
M <- Reorient_Trans(c(0,0,1), v)
obj <- transform3d(HTmesh, M)
obj <- translate3d(obj, v[1], v[2], v[3])
shade3d(obj, color=colors[i])
shade3d(Cmesh(c(0,0,0), 0, v, 0.05, 6, 40, color="brown"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment