Skip to content
Prev 311122 / 398506 Next

Question about contour3d and writeWebGL: rgl and misc3d package

On 12-11-17 3:11 PM, Duncan Murdoch wrote:
I just tried your example.  The image has 205311 vertices, so it will 
need splitting up.  It is made up of triangles, and they're fairly 
simple, so this should do it:

split_triangles <- function(ids = rgl.ids()$id, maxsize=65535) {

   if (maxsize %% 3 != 0)
     stop("maxsize must be a multiple of 3")

   save <- par3d(skipRedraw=TRUE)
   on.exit(par3d(save))

   allids <- rgl.ids()
   ids <- with(allids, id[ id %in% ids & type == "triangles" ])
   for (id in ids) {
     count <- rgl.attrib.count(id, "vertices")
     if (count <= maxsize) next
     verts <- rgl.attrib(id, "vertices")
     norms <- rgl.attrib(id, "normals")
     cols <- rgl.attrib(id, "colors")
     rgl.pop(id=id)
     while (nrow(verts) > 0) {
       n <- min(nrow(verts), maxsize)
       triangles3d(verts[1:n,], normals=norms[1:n,], 
color=rgb(cols[1:n,1], cols[1:n,2], cols[1:n,3]), alpha=cols[1:n,4])
       verts <- verts[-(1:n),,drop=FALSE]
       norms <- norms[-(1:n),]
       cols <- cols[-(1:n),]
     }
   }
}


This function isn't perfectly general, but I think it is good enough for 
contour3d output.  (It does slow down rendering noticeably:  having one 
object is easier to render than having 4.)  You might need to play with 
smaller values of maxsize.

Duncan Murdoch