0

I need to represent a surface in a 3-dimensional plot using R.

I have a matrix "nodes" (Nx3) having in each row the x,y,z coordinates of each of the N points and a matrix "triangles" (Mx3) having in each row the indexes of the 3 points that are the vertices of the triangle.

For the moment I can plot only the points using rgl

plot3d(nodes)

So what I want to do is to represent this surface, I know that in Matlab there is some nice function but I couldn't find any equivalent in R!

Then my goal is to head a heatmap or colormap on the triangulated surface, for example given the value of a certain function at each of the nodes

MarioB
  • 67
  • 7
  • I don't use R much, but I imagine this would be easy enough to do. [Here][1] is an answer on SO that uses a rectangular grid, but it might be adaptable to the triangular mesh you have. Could you post a little bit of code so I can generate a mesh like yours and exeriment? [1]: http://stackoverflow.com/questions/3786189/r-4d-plot-x-y-z-colours –  Mar 09 '17 at 16:22
  • The person who posted that answer I linked mentioned using rgl so maybe you can work with that. I just searched for "4d plotting in R" and came up with a few results, that might help you out if you havent searched for that yet. –  Mar 09 '17 at 16:23
  • Thank you Julian, I added a link to a gdrive repository where I uploaded the files and the output I get using matlab! – MarioB Mar 09 '17 at 16:36
  • found another solution. You Would have to transform your data first. http://stackoverflow.com/questions/29502268/how-can-i-import-and-plot-a-triangular-mesh-in-r. I can't open the files you linked, they are too large. –  Mar 09 '17 at 17:12

1 Answers1

1

I stole this code from here and just tweaked it to suit your purposes. Ill call the triangles T and _T. Each triangle is made of three coordinates, (x1,y1,z1), (x2,y2,z2) and (x3,y3,z3), and every triange has a number c corresponding to its color. This example plots only two triangles. In the mymesh structure the triangles are stored as (x1,_x1,y1,_y1,z1,_z1,...,z3,_z3,c,_c). The dimension is 2x10 because there are two triangles and a triangle is represented by 10 numbers, nine for coordinates and one for the color.

mymesh <- structure(c(0,0,0,0,0,0,1,0,0,1,0,0,1,1,1,1,0,0,100,200),
                    .Dim = c(2, 10),
                    .Dimnames = list( NULL, c("x1", "y1", "z1",
                                              "x2", "y2", "z2",
                                              "x3", "y3", "z3",
                                              "value")
                    )
)

library(rgl)
rgl.open()
i <- 1
vertices <- c(mymesh[i,1:3],1,mymesh[i,4:6],1,mymesh[i,7:9],1)
indices <- c( 1, 2, 3)
shade3d( tmesh3d(vertices,indices) , col=1)
bg3d(color = "white")
for(i in 2:2){
  vertices <- c(mymesh[i,1:3],1,mymesh[i,4:6],1,mymesh[i,7:9],1)
  indices <- c( 1, 2, 3)
  shade3d( tmesh3d(vertices,indices) , col= i)
}

The output is

two 3d triangles that can be rotated and stuff

There's probably a better way. I don't really know R, and the coordinates seem to be stored in a stupid way because R uses column major order. You can change it so that you enter the coordinates in a more natural way i.e. (x1,y1,z1,x2,..._y3,_z3,_c) and then pass the transpose of this to the structure.

Community
  • 1
  • 1