3

I would like to plot my model output in R

It is a triangular mesh in the format

x1 y1 z1 x2 y2 z2 x3 y3 z3 value

Where each row represents a triangle, and I would like to plot these triangles with value as the scale.

mymesh <- structure(c(0.91, 0.45, 13.93, 14.26, 14.15, 14.36, 14.36, 14.61, 
    14.58, 14.87, 14.8, 15.13, 15.02, 15.39, 15.24, 15.65, 15.46, 
    15.9, 15.69, 16.16, 0, 0.79, 12.45, 12.64, 12.34, 12.46, 12.24, 
    12.38, 12.13, 12.3, 12.03, 12.22, 11.92, 12.13, 11.81, 12.04, 
    11.69, 11.94, 11.57, 11.84, 0, 0, 208.71, 208.69, 210.7, 210.68, 
    212.68, 212.66, 214.67, 214.65, 216.66, 216.63, 218.64, 218.61, 
    220.62, 220.59, 222.61, 222.57, 224.59, 224.56, 0.45, 1.5, 14.15, 
    14.15, 14.36, 14.36, 14.58, 14.58, 14.8, 14.8, 15.02, 15.02, 
    15.24, 15.24, 15.46, 15.46, 15.69, 15.69, 15.92, 15.92, 0.79, 
    0, 12.34, 12.34, 12.24, 12.24, 12.13, 12.13, 12.03, 12.03, 11.92, 
    11.92, 11.81, 11.81, 11.69, 11.69, 11.57, 11.57, 11.45, 11.45, 
    0, 131.83, 210.7, 210.7, 212.68, 212.68, 214.67, 214.67, 216.66, 
    216.66, 218.64, 218.64, 220.62, 220.62, 222.61, 222.61, 224.59, 
    224.59, 226.58, 226.58, 1.5, 0.75, 14.26, 14.36, 14.36, 14.61, 
    14.61, 14.87, 14.87, 15.13, 15.13, 15.39, 15.39, 15.65, 15.65, 
    15.9, 15.9, 16.16, 16.16, 16.42, 0, 1.3, 12.64, 12.46, 12.46, 
    12.38, 12.38, 12.3, 12.3, 12.22, 12.22, 12.13, 12.13, 12.04, 
    12.04, 11.94, 11.94, 11.84, 11.84, 11.74, 131.83, 131.83, 208.69, 
    210.68, 210.68, 212.66, 212.66, 214.65, 214.65, 216.63, 216.63, 
    218.61, 218.61, 220.59, 220.59, 222.57, 222.57, 224.56, 224.56, 
    226.54, 375.12, 399.8, 297.84, 497.63, 368.17, 1113.19, 979, 
    781.41, 1202.21, 1113.94, 1293.77, 1391.94, 1364.52, 1151.37, 
    1690.98, 913.63, 0, 0, 0, 0), .Dim = c(20L, 10L), .Dimnames = list(
    NULL, c("x1", "y1", "z1", "x2", "y2", "z2", "x3", "y3", "z3", 
    "value")))

I have looked at the rgl, plot3D, and misc3d packages. However, these seem to be dealing with points rather than triangles (for example, the rgl.points, rgl.linestrips, and rgl.triangles functions all take x,y,z as inputs, rather than a triangle).

How can I use R to plot and color the triangles in this dataset?

David LeBauer
  • 31,011
  • 31
  • 115
  • 189
  • Have you tried [`rgl::triangles3d`](http://cran.r-project.org/web/packages/rgl/vignettes/rgl.html#primitive-shapes)? Its help page (`?triangles3d`) includes a nice example. It looks to me like (if it's what you're after) your only task will then be to reshape your data into the appropriately formatted 3-column matrix. – Josh O'Brien Apr 08 '15 at 01:11
  • 1
    @JoshO'Brien thanks for pointing that out. I had trouble finding documentation, and couldn't figure out how arguments x y z could be used on vertices of a triangle ... especially without some sort of identifier for the component points ... But apparently, triangle3d "takes each successive triple of points as the vertices of a triangle". And I also stumbled across the light3d function, which could also be a useful tool. – David LeBauer Apr 08 '15 at 02:41
  • Have you tried what I suggested? Isn't it what you expected? – DatamineR Apr 09 '15 at 17:51

2 Answers2

2

Try using the polygon() function! :) It's awesome - you can draw any shape you want. Base R plotting is actually pretty sweet sometimes.

(wrong)

Hillary Sanders
  • 5,778
  • 10
  • 33
  • 50
  • Thanks, but I am trying to construct a 3D mesh. Is that possible? – David LeBauer Apr 08 '15 at 01:00
  • Ah, just got that. My bad. Hmm. With polygon(), only if you project your coordinates yourself onto 2-D. http://blog.revolutionanalytics.com/2014/02/3d-plots-in-r.html won't work? Good luck – Hillary Sanders Apr 08 '15 at 01:11
1

I generated a plot using the shade3d and tmesh3d functions from rgl. It creates two separate bars. The different triangulars are coloured differently. Look at the plot below.

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:20){
     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)
    }

enter image description here

DatamineR
  • 10,428
  • 3
  • 25
  • 45