Search code examples
rplot3drgl

R: plot edges of a triangle in 3d


I have a triangle living in a 3d space, I want to plot only the edges of the triangle in an efficient way, since I will repeat it for a huge number of triangles.

I am able to plot it as a coloured surface using the package rgl:

rgl.open()
vertices = c(
0,0,0,1,
1,1,0,1,
0,0,1,1)
col = "blue"
shade3d( tmesh3d(vertices,indices) , col=col)
bg3d(color = "white")

But what I want is just the 3 lines connecting the points.

What I tried was:

vertices = c(
  0,0,0,
  1,1,0,
  0,0,1) 
rgl.lines(x=c(vertices[1],vertices[4]),y=c(vertices[2],vertices[5]),z=c(vertices[3],vertices[6]),col="black")
        rgl.lines(x=c(vertices[4],vertices[7]),y=c(vertices[5],vertices[8]),z=c(vertices[6],vertices[9]),col="black")
        rgl.lines(x=c(vertices[7],vertices[1]),y=c(vertices[8],vertices[2]),z=c(vertices[9],vertices[3]),col="black")
        bg3d(color = "white")

However, this approach is considerably slower than the first one (around 10 times when tried on a real mesh). I am wondering, is there a way to plot with shade3d the triangles as transparent with only their edges?


Solution

  • You should just be able to something like this:

    wire3d( tmesh3d(vertices,indices) , col=col)
    

    works for me.

    Example using something I found in the rgl docs:

    library(rgl)
    
    # A trefoil knot
    open3d()
    theta <- seq(0, 2*pi, len = 25)
    cen <- cbind( sin(theta) + 2*sin(2*theta),
                  2*sin(3*theta),
                  cos(theta) - 2*cos(2*theta) )
    
    e1 <- cbind( cos(theta) + 4*cos(2*theta),
                 6*cos(3*theta), 
                 sin(theta) + 4*sin(2*theta) )
    
    knot <- cylinder3d( center=cen,e1=e1,radius = 0.8, closed = TRUE)
    
    wire3d(addNormals(subdivision3d(knot, depth = 2)), col = "green")  
    

    yields:

    enter image description here

    where as using:

    shade3d(addNormals(subdivision3d(knot, depth = 2)), col = "green")  
    

    yields:

    enter image description here