1

I'd like to color faces on an triangular RGL mesh based on proximity to a vertex.

The thing is, it seems that a lot of the times that the vertices are associated with faces that are very far from the actual vertex location itself, which creates a problem when I want to color faces around one vertex; the faces end up being very far from where the barycenter actually is.

What I'm doing right now is this:

  1. Compute the barycenter of all the faces in the mesh.

  2. Use the FAR package to compute the closest n barycenters to the desired point. Keep those indices.

  3. Based on the indices gathered, color those faces a certain color. The rest of the faces would be colored white.

    colors=rep('white',num_faces) colors[colored_faces]='red' mesh$material=list(color=colors)

  4. Then I would plot the mesh: plot3d(mesh)
  5. The thing is, I'm getting very odd coloring right now, is there any established way to color faces that close to a certain coordinate/vertex?

This is what the mesh currently looks like, with the red as the 'colored' faces, and the blue as the points that I would like there to be a colored face near.

Mesh

Update: Seeing this, my question has now been modified to:

How can I find the closest face to a given point? It still isn't clear to me, since the face barycenters are sometimes misleading, and don't represent actual distance to a given vertex.

Update 2:

I've added example code and a file here: Files and code

Basically the code finds the nearest faces to a given vertex of the same 3d mesh with the nearest neighbor algorithm, and then we color those faces in our color vector (remembering to color the colors 4 times):

Except, when we run this algorithm, we only color one side of the shape: like so: Odd

How can I make the coloring a bit more symmetric?

Update 3: This problem has been resolved! Please look to the unreleased version of rgl on Rforge for the newest version of rgl that allows for coloring of faces, vertices, and edges.

Update 4: Here is the new image by coloring the closest vertices (to show that the new rgl package works wonders):

Better Sink

Bruce Wang
  • 11
  • 2
  • It would be helpful if you posted a small reproducible example. We don't know what data you are starting with, so it's hard to tell you what to do with it. The example doesn't need to be complete enough to reproduce your sample figure, just something with the same structure. – user2554330 Sep 21 '18 at 10:17

1 Answers1

1

Your code to compute the centroid is incorrect. You have

#Function for computing the barycenter/centroid of a face.
compute_face_centroid=function(vertices,face){
  vertex=vertices[,face][-4,]
  centroid=apply(X=vertex,MARGIN = 1,FUN = mean)
  return(centroid)
}

This just removes the 4th row of the vertices array, which is the wrong way to convert homogeneous coordinates to Euclidean coordinates. You really need to divide the other rows by the 4th one. You can do this using the rgl function asEuclidean:

#Function for computing the barycenter/centroid of a face.
compute_face_centroid=function(vertices,face){
  vertex <- asEuclidean(t(vertices[,face]))
  apply(vertex, MARGIN = 2, FUN = mean)
}

There may also be other issues in your code, I haven't traced through everything yet.

BTW, the unreleased test version of rgl changes the way colours are handled in meshes, hopefully making that part of your code simpler. You can get it from R-forge.r-project.org if you want to try it. You can now specify colours by vertex or by face.

Edited to add:

Okay, I've taken a closer look now. I think your code was actually working. The compute_face_centroid should be corrected, but since your example always has value 1 for the final component, deleting it is okay.

The reason you got colouring different from what you expected is just that the triangles making up your mesh really vary in shape. If you plot your image as a wireframe you'll see this:

wire3d(file)

The centroids of those long thin triangles are quite far from your selected point.

enter image description here

user2554330
  • 37,248
  • 4
  • 43
  • 90
  • Hello, thanks so much for helping me look into this! I wouldn't have figured this out without your help. On a final note, would you perhaps know how the new coloring scheme works? I've downloaded the new version of rgl; would I specify the 'meshcolor'=vertex/face/edge and include one color for each vertex/edge/face index within the 'color'=colors? Thank you so much again, and please let me know if I can help if there is anything I can do to help with anything! – Bruce Wang Sep 24 '18 at 15:07
  • Yes, exactly. For your problem, I'd recommend finding the `n` closest vertices instead of centroids. `meshColor = "vertices"` is the default, but it might be safest to specify that explicitly in case the default changes when it is released. – user2554330 Sep 24 '18 at 15:59