r3dmeshrgl

Retrieving the indices of the centers with rgl::cylinder3d


I'm drawing a spherical Lissajous curve with rgl::cylinder3d:

library(rgl)
library(primes)

l <- function(m1, m2, alpha = 0) {
  g <- gcd(m1, m2)
  t_ <- seq(0, 2*pi/g, length.out = 400L)
  cbind(
    sin(m2 * t_) * cos(m1 * t_ - alpha * pi),
    sin(m2 * t_) * sin(m1 * t_ - alpha * pi),
    cos(m2 * t_)
  )
}

pts <- l(3, 2)
mesh <- addNormals(cylinder3d(
  pts, radius = 0.1, closed = TRUE, sides = 30L
))
shade3d(mesh, color = "yellow")

enter image description here

I'd like to assign a color to each vertex according to its value of t_ in the function l. In other words, given a vertex of the mesh, I want to retrieve the index of pts corresponding to this vertex.


Solution

  • I've found:

    ii <- rep(1:400, each = 30L) # 400=nrow(pts); 30=sides
    fcolor <- colorRamp(viridisLite::inferno(200))
    cols <- fcolor(ii/400)
    cols <- rgb(cols[, 1L], cols[, 2L], cols[, 3L], maxColorValue = 255)
    mesh$material <- list(color = cols)
    shade3d(mesh, meshColor = "vertices")
    

    enter image description here