R中的平滑3D三角形网格

帕特里克

我正在绘制人脸的3D表面网格。可以在https://github.com/Patricklv/Smoothing-3D-surface上找到数据,其中vb.xlsx包含顶点,it.xlsx包含面。

我的R代码如下:

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")

最终的3D曲面如下:在此处输入图片说明在3D曲面网格上很容易看到很多三角形。我想使此surace网格看起来更平滑,就像下面显示的那样:在此处输入图片说明

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?

user2554330

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

try2 <- addNormals(try)

and then display try2 using shade3d.

这样做的目的是对在顶点相交的每个三角形面的法线求平均。然后在整个面部平滑地进行阴影处理,您将获得与其他绘图之一类似的效果。请参阅example(addNormals)以获取演示。

BTWrgl在R中使用Gouraud底纹,但是rglwidget()在浏览器中用于显示同一曲面时使用Phong底纹它们相似,但是Phong看起来好一些。

这是我从带有Gouraud阴影的示例中得到的结果:

在此处输入图片说明

使用浏览器的Phong着色也是如此:

在此处输入图片说明

最后,这就是我为您准备的内容。我将显示更改为

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

并得到

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章