R 中的平滑 3D 三角形网格 [英] Smooth 3D trangular mesh in R

查看:30
本文介绍了R 中的平滑 3D 三角形网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制人脸的 3D 表面网格.数据可以在 在 3D 表面网格上可以很容易地看到很多三角形.我想让这个表面网格看起来更平滑,就像下图所示:

我应该在 R 中做什么来平滑表面网格,使网格看起来像第二个网格,其中可见的三角形面被平滑了?也许用于着色的 Phong 模型可以通过 misc3d 包中的 contour3d 函数工作.任何人都可以展示如何将此功能应用于我的数据?

我注意到 R 中的 plotly 包有一些非常方便的方法来创建表面网格:

图书馆(情节)面对 <- plot_ly(x = vb[,1], y = vb[,2], z = vb[,3],类型 =mesh3d")脸

生成的面部表面网格如下:

表面网格非常光滑!但是在保存 plotly.js 对象时我无法控制它的方向.如果不额外购买,我也无法将其保存为 PDF 文件.我想知道即使我没有提供人脸信息(没有提供信息,只提供了 vb),创造这个光滑表面网格的魔力是多么的神奇?如果 plotly 的魔力可以在 R 中以其他方式完成,这样我可以在保存图片时自定义方向,并且无需购买即可将其保存为 PDF 文件,同时仍然保持如此高的流畅度?

解决方案

您可以使用 rgl::addNormals() 函数使表面看起来更平滑.就做

try2 <- addNormals(try)

然后使用shade3d显示try2.

这样做是平均每个三角形面在顶点处相遇的法线;然后在整个脸上平滑地完成阴影,你会得到类似于你的其他情节之一的东西.有关演示,请参阅 example(addNormals).

顺便说一句,rgl 在 R 中使用 Gouraud 着色,但是当您使用 rglwidget() 在浏览器中显示相同的表面时使用 Phong 着色.它们很相似,但 Phong 看起来更好一些.

这是我从 Gouraud 着色示例中得到的:

这与浏览器中的 Phong 着色相同:

最后,这是我为你的表面得到的.我将显示更改为

shade3d(try2, col="darkgrey", specular = "#202020")

得到

I am drawing a 3D surface mesh of human face. Data can be found at https://github.com/Patricklv/Smoothing-3D-surface, where vb.xlsx contains vertices and it.xlsx contains faces.

My R code is as follows:

library(xlsx)
library(rgl)

vb <- read.xlsx("C:\Users\<Username>\Desktop\vb.xlsx", sheetIndex = 1, header = F)
it <- read.xlsx("C:\Users\<Username>\Desktop\it.xlsx", sheetIndex = 1, header = F)
vb_mat <- t(as.matrix(vb))
vb_mat <- rbind(vb_mat, 1)
rownames(vb_mat) <- c("xpts", "ypts", "zpts", "")

it_mat <- t(as.matrix(it))
rownames(it_mat) <- NULL

vertices <- c(vb_mat)
indices <- c(it_mat)

try <- tmesh3d(vertices = vertices, indices = indices, homogeneous = TRUE, material = NULL, normals = NULL, texcoords = NULL)

shade3d(try, ambient = "darkgrey", specular = "white")

The resultant 3D surface is as follows: A lot of triangles could be easily seen on the 3D surface mesh. I want to make this surace mesh looks smoother, just like the one shown below:

What should I do in R to smooth the surface mesh so that the mesh looks like the second one, where visible triangular faces are smoothed out? Perhaps Phong model for shading would work through contour3d function in misc3d package. Can anyone show how this function could be applied to my data?

I noted that the plotly package in R has some pretty handy ways to create surface mesh:

library(plotly)

face <- plot_ly(
    x = vb[,1], y = vb[,2], z = vb[,3],
    type = "mesh3d"
)

face

The resultant facial surface mesh is as below:

The surface mesh is very smooth! But I cannot control the orientation of the plotly.js object when saving it. Neither could I save it as PDF file without additional purchasing. I wish to know how plotly did the magic to create this smooth surface mesh, even when I did not provide face information (it information not provided, only vb provided)? If the magic done by plotly can be done in some other manner in R so that I can customize the orientation when saving pictures and can save it as PDF file without purchasing, while still maintaing such high level of smoothness?

解决方案

You can use the rgl::addNormals() function to make the surface look smoother. Just do

try2 <- addNormals(try)

and then display try2 using shade3d.

What this does is to average the normals to each triangular face that meets at a vertex; then the shading is done smoothly across the face, and you'll get something like one of your other plots. See example(addNormals) for a demo.

BTW, rgl uses Gouraud shading in R, but Phong shading when you use rglwidget() to display the same surface in a browser. They're similar, but Phong looks a bit better.

Here's what I get from the example with Gouraud shading:

Here's the same thing with Phong shading from a browser:

Finally, here's what I get for your surface. I changed the display to

shade3d(try2, col="darkgrey", specular = "#202020")

and got

这篇关于R 中的平滑 3D 三角形网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆