Patrick
Patrick

Reputation: 1227

Plot specified area of a surface mesh with color

I have data for 3D facia surface mesh. Data are available here, where vb.xlsx contains coordinates for 7160 3D vertices and it.xlsx contains face information. The color coding.txt is a matrix of 7160*1 with elements of either 1 or 2. I want the surface area (not just vertices!) enclosed by vertices which are coded 1 to be plotted in a different color from areas covered by vertices coded 2.

For example, if vertices covering nose and upper lip are coded 1 and other facial areas are coded 2, then I wish to make a plot as below:

enter image description here

where surface area of nose and upper lip is green and other regions are gray.

My code to generate gray color 3D facial surface mesh is as follows:

library(geomorph)
library(xlsx)
library(rgl)

# Import data
vb <- read.xlsx("E:\\...\\vb.xlsx", sheetIndex = 1, header = F)
it <- read.xlsx("E:\\...\\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)

try2 <- addNormals(try)

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

Now, I want to plot surface area containing vertices coded 1 with green color and area containing vertices coded 2 with pink color. How should I modify my R code?

Thank you.

Upvotes: 0

Views: 290

Answers (2)

user2554330
user2554330

Reputation: 44957

The tricky thing in colouring surfaces is deciding how to use the colours. You can colour by vertex, by edge, or by face. You want to do it by vertex.

As long as you are using rgl version 0.100.2 or newer, this is relatively easy. Specify the colours one per vertex, and use argument meshColor = "vertices" to tell rgl that's what you did. For example,

shade3d(try2, col=c("green", "pink")[col], meshColor = "vertices", specular = "#202020")

which gives

screen capture

This version of rgl isn't on CRAN yet, but it's available on R-forge and Github.

Upvotes: 2

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84659

Is it the expected result?

library(rgl)
library(readxl)
# Import data
vb <- read_xlsx("vb.xlsx", sheet = 1, col_names = FALSE)
it <- read_xlsx("it.xlsx", sheet = 1, col_names = FALSE)
colorCoding <- read.table("Color coding.txt")$V1
colors <- c("darkgrey","midnightblue")[colorCoding]

vb_mat <- rbind(t(as.matrix(vb)), 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)

mesh <- addNormals(
  tmesh3d(vertices = vertices, indices = indices, homogeneous = TRUE,
          material = list(color=colors)))
shade3d(mesh, specular = "#202020")

enter image description here

Upvotes: 0

Related Questions