Search code examples
rprojectionrglplane

rgl: draw plane spanned by two 3D vectors


I'm having trouble understanding the arguments for rgl::plane3d required to draw a plane spanned by two vectors (x0, x1), going through a given point (O=origin). This is for a diagram explaining projection.

The rgl documentation doesn't provide enough examples for me to understand what to specify.

enter image description here

Here is my MWE:

library(matlib)
library(rgl)
rgl::open3d()
O <- c(0, 0, 0)
x0 <- c(1, 1, 1)
x1 <- c(0, 1, 1)
y <- c(1, 1, 3)
XX <- rbind(x0=x0, x1=x1)
matlib::vectors3d(XX, lwd=2)
matlib::vectors3d(y, labels=c("", "y"), color="red", lwd=3)
# how to specify the plane spanned by x0, x1 ???
# planes3d(..., col="gray",  alpha=0.2)
# draw projection of y on plane of XX
py <-  matlib::Proj(y, t(XX))
rgl::segments3d(rbind( y, py))
rgl::segments3d(rbind( O, py))

Solution

  • To find the plane that is parallel to both x0 and x1, find the cross product of these two vectors, we can do it manually by since it's R:

    library(pracma)
    cross(x1,x2)
    [1]  0 -1  1
    

    So the equation of the plane perpendicular to this is basically any vector whose dot product will give you 0, meaning:

    0*x + -1*y + 1*z = 0
    -y + z = 0
    

    You can read more about the explanation here. Or in your scenario you can think of it as you need a y = z plane (because x is different).

    So if you look at the documentation, it says:

    ‘planes3d’ and ‘rgl.planes’ draw planes using the parametrization a x + b y + c z + d = 0.

    We don't have an offset so d = 0, and that leaves us with a = 0, b= -1 and c= 1:

    plot3d(rbind(0,x1),type="l",xlim=c(0,3),ylim=c(0,3),
    zlim=c(0,3),xlab="x",ylab="y",zlab="z")
    lines3d(rbind(0,y),col="red")
    lines3d(rbind(0,x0))
    
    py <-  matlib::Proj(y, t(XX))
    segments3d(rbind( y, py),col="gray")
    segments3d(rbind( O, py),col="gray")
    
    planes3d(a=0,b=-1,c=1,col="turquoise",alpha=0.2)
    

    enter image description here

    enter image description here