rrgl

How to check/decide/segment whether a point is inside a 3d cone?


How do I know which points are inside the cone, for example for the following bunny data?

enter image description here enter image description here

library("onion")
library("rgl")

#bunny data
data(bunny)

#cone
pts <- cbind(c(0, 0), c(0, 0.23), c(0, 0))# the centers
radii <- c(0.0, 0.15)
cone <- cylinder3d(pts, radii, sides = 20)

#visualize
points3d(bunny, col="pink", alpha = 0.3)
shade3d(cone, col = "lightblue", alpha = 0.9)


Solution

  • This is more of a geometry question than a programming question. For any given point in the x, y, z space, the points where x^2 + z^2 < (0.15/0.23 * y)^2 will lie inside the cone you have defined, so you can do:

    data(bunny)
    
    pts <- cbind(c(0, 0), c(0, 0.23), c(0, 0))
    radii <- c(0.0, 0.15)
    cone <- cylinder3d(pts, radii, sides = 100)
    
    cone_bunny <- bunny[bunny[,1]^2 + bunny[,3]^2 < (0.15/0.23 * bunny[,2])^2,]
    
    points3d(cone_bunny, col="pink", alpha = 0.5)
    shade3d(cone, col = "lightblue", alpha = 0.3)
    

    enter image description here

    I'm not sure if you were looking for a more general solution for intersecting 3D shapes - that is far more difficult, but many simple geometric intersections can be calculated from straightforward algebra.


    Addendum

    It was pointed out in the comments that you may not want an actual cone, but rather a 20-sided pyramid as shown in your example. This is also amenable to a mathematical solution:

    cone <- cylinder3d(pts, radii, sides = 20) 
    
    lims <- (0.15/0.23 * bunny[,2])^2/cos(atan2(bunny[,3], bunny[,1]) %% 
             (2*pi)/20 - pi/20)
    
    cone_bunny <- bunny[bunny[,1]^2 + bunny[,3]^2 < lims,]
    
    points3d(cone_bunny, col="pink", alpha = 0.5)
    shade3d(cone, col = "lightblue", alpha = 0.3)
    

    enter image description here