tiagodc / TreeLS

R functions for processing individual tree TLS point clouds
GNU General Public License v3.0
82 stars 27 forks source link

Plot fitted cylinder from shapeFit #36

Open JulFrey opened 3 years ago

JulFrey commented 3 years ago

Hi, first of all thanks a lot for this wonderful piece of software. It is incredibly helpful. I am currently working on a detection for main branches and crown base height. Therefore, I was using your shapeFit-function to use ransac-cylinder fitting. Everything is working well; besides I don't fully understand how to use the output. For plotting the cylinder back into the point cloud, I was using rgl's shade3d and cylinder3d functions, but therefore I need the start and the endpoint of the cylinder. Do you know how to easily calculate it? I was trying to understand the math from Liang et al. 2012, but I didn’t really managed to get any further. Here is how far I did get:

cyl <- shapeFit(branch_seg, shape = 'cylinder', n = 20)
attach(cyl, warn.conflicts = FALSE)
n <- c(cos(phi) * sin(theta) , sin(phi) * sin(theta) , cos(theta))
ntheta <- c(cos(phi) * cos(theta) , sin(phi) * cos(theta) , -sin(theta))
nphi <- c(-sin(phi) * sin(theta) , cos(phi) * sin(theta) , 0);
nphibar <- nphi/sin(theta)
a <- ntheta * cos(alpha) + nphibar * sin(alpha)
q <- n*(Radius+rho)
center <- cyl[1,7:9]
height <- with(branch_seg@data, abs(diff(c(min(Z), max(Z))))
cords <- rbind(q-a*height + center, q+a*height + center)
{
plot(branch_seg, axis = T)
rgl::shade3d(rgl::addNormals(rgl::subdivision3d(rgl::cylinder3d(coords, radius=Radius), sides=8)), axis = T, col = 'green')
}

Thanks a lot, Julian