title: Smooth 3D trangular mesh in R
body: <p>I am drawing a 3D surface mesh of human face. Data can be found at <a href="https://github.com/Patricklv/Smoothing-3D-surface" rel="nofollow noreferrer">https://github.com/Patricklv/Smoothing-3D-surface</a>, where vb.xlsx contains vertices and it.xlsx contains faces.</p>
  
  <p>My R code is as follows:</p>
  
  <pre><code>library(xlsx)
  library(rgl)
  
  vb &lt;- read.xlsx("C:\\Users\\&lt;Username&gt;\\Desktop\\vb.xlsx", sheetIndex = 1, header = F)
  it &lt;- read.xlsx("C:\\Users\\&lt;Username&gt;\\Desktop\\it.xlsx", sheetIndex = 1, header = F)
  vb_mat &lt;- t(as.matrix(vb))
  vb_mat &lt;- rbind(vb_mat, 1)
  rownames(vb_mat) &lt;- c("xpts", "ypts", "zpts", "")
  
  it_mat &lt;- t(as.matrix(it))
  rownames(it_mat) &lt;- NULL
  
  vertices &lt;- c(vb_mat)
  indices &lt;- c(it_mat)
  
  try &lt;- tmesh3d(vertices = vertices, indices = indices, homogeneous = TRUE, material = NULL, normals = NULL, texcoords = NULL)
  
  shade3d(try, ambient = "darkgrey", specular = "white")
  </code></pre>
  
  <p>The resultant 3D surface is as follows:
  <a href="https://i.stack.imgur.com/LM9Iv.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LM9Iv.jpg" alt="enter image description here"></a>
  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:
  <a href="https://i.stack.imgur.com/1A8EI.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1A8EI.jpg" alt="enter image description here"></a></p>
  
  <p>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?</p>
  
  <p>I noted that the plotly package in R has some pretty handy ways to create surface mesh:</p>
  
  <pre><code>library(plotly)
  
  face &lt;- plot_ly(
      x = vb[,1], y = vb[,2], z = vb[,3],
      type = "mesh3d"
  )
  
  face
  </code></pre>
  
  <p>The resultant facial surface mesh is as below:
  <a href="https://i.stack.imgur.com/wsnGs.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wsnGs.jpg" alt="enter image description here"></a></p>
  
  <p>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?</p>