简体   繁体   English

将带有线框的 3D 表面从 R 导出到 HTML

[英]Export 3D surface with wireframe from R to HTML

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 表面上每个面的边缘。

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.您调用surfaceData(id)来提取表面的顶点,然后在该数组上surface2lines()来绘制线条。 For example,例如,

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

which gives this image from your data:它从您的数据中给出了这个图像:

线框图像

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属性来让它显示在表面的顶部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM