使用线框从R到HTML导出3D曲面

时间:2022-09-10 21:58:29

The writeWebGL function from the {rgl} package can be used to export interactive 3D plots to HTML (see example below).

{rgl}包中的writeWebGL函数可用于将交互式3D绘图导出为HTML(请参见下面的示例)。

    require(rgl)
    jet.colors <-colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan",
                 "#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
    colorzjet <- jet.colors(100)
    data(volcano)
    z <- 5 * volcano        # Exaggerate the relief
    x <- 10 * (1:nrow(z))   # 10 meter spacing (S to N)
    y <- 10 * (1:ncol(z))  

    open3d()
    bg3d("white")
    material3d(col="black")
    persp3d(x, y, z, col = colorzjet[ findInterval(z, seq(min(z), max(z), length=100))], aspect="iso",axes = TRUE, box = FALSE, smooth=FALSE,lit=FALSE,front="fill")
    surface3d(x, y, z, front = "lines",col="black", lit=FALSE)

    browseURL(paste("file://", writeWebGL(dir=file.path("C:/", "webGL"), width=700), sep=""))

It does not, however, currently support wireframe or point rendering, meaning that 3D surfaces will be exported as polygons without visible edges.

但是,它目前不支持线框或点渲染,这意味着3D曲面将导出为没有可见边的多边形。

Does anyone know a way around that? I'd like my HTML file to show the edges of each facet on the 3D surface.

有没有人知道这方面的方法?我希望我的HTML文件显示3D表面上每个面的边缘。

1 个解决方案

#1


1  

You'll need to draw the points or edges using low level functions. It's fairly easy to do points:

您需要使用低级函数绘制点或边。分数很容易:

id <- surface3d(x, y, z, front = "lines",col="black", lit=FALSE)
vertices <- rgl.attrib(id, "vertices")
points3d(vertices)

Doing the wireframe is more complicated, because you need to join the right vertices. Here are a couple of functions that can do it.

做线框更复杂,因为你需要加入正确的顶点。这里有几个可以做到的功能。

surfaceData <- function(id) {
  vertices <- rgl.attrib(id, "vertices")
  dim <- rgl.attrib(id, "dim")
  array(vertices, c(dim, 3))
}

surface2lines <- function(data, ...) {
  vertices <- NULL
  for (i in seq_len(dim(data)[1]))
    vertices <- rbind(vertices, data[i,,], c(NA, NA, NA)) 
  for (j in seq_len(dim(data)[2]))
    vertices <- rbind(vertices, data[,j,], c(NA, NA, NA))
  lines3d(vertices, ...) 
}

You call surfaceData(id) to extract the vertices of a surface, then surface2lines() on that array to plot the lines. For example,

您调用surfaceData(id)来提取曲面的顶点,然后调用该数组上的surface2lines()来绘制线条。例如,

id <- surface3d(x, y, z, front = "lines",col="black", lit=FALSE)
d <- surfaceData(id)
open3d()
surface2lines(d)

which gives this image from your data:

从您的数据中提供此图像:

使用线框从R到HTML导出3D曲面

You could overplot this on the surface; you'll likely need to play with the depth_test property to get it to show up on top of the surface.

你可以在表面上过度绘制;您可能需要使用depth_test属性才能让它显示在表面之上。

#1


1  

You'll need to draw the points or edges using low level functions. It's fairly easy to do points:

您需要使用低级函数绘制点或边。分数很容易:

id <- surface3d(x, y, z, front = "lines",col="black", lit=FALSE)
vertices <- rgl.attrib(id, "vertices")
points3d(vertices)

Doing the wireframe is more complicated, because you need to join the right vertices. Here are a couple of functions that can do it.

做线框更复杂,因为你需要加入正确的顶点。这里有几个可以做到的功能。

surfaceData <- function(id) {
  vertices <- rgl.attrib(id, "vertices")
  dim <- rgl.attrib(id, "dim")
  array(vertices, c(dim, 3))
}

surface2lines <- function(data, ...) {
  vertices <- NULL
  for (i in seq_len(dim(data)[1]))
    vertices <- rbind(vertices, data[i,,], c(NA, NA, NA)) 
  for (j in seq_len(dim(data)[2]))
    vertices <- rbind(vertices, data[,j,], c(NA, NA, NA))
  lines3d(vertices, ...) 
}

You call surfaceData(id) to extract the vertices of a surface, then surface2lines() on that array to plot the lines. For example,

您调用surfaceData(id)来提取曲面的顶点,然后调用该数组上的surface2lines()来绘制线条。例如,

id <- surface3d(x, y, z, front = "lines",col="black", lit=FALSE)
d <- surfaceData(id)
open3d()
surface2lines(d)

which gives this image from your data:

从您的数据中提供此图像:

使用线框从R到HTML导出3D曲面

You could overplot this on the surface; you'll likely need to play with the depth_test property to get it to show up on top of the surface.

你可以在表面上过度绘制;您可能需要使用depth_test属性才能让它显示在表面之上。