The key point here is that the point cloud is not rendered with actual XY coordinates because rgl
is working with float
instead of double
and this create artifacts when displaying the point cloud. The coordinates are thus shifted to get a nice rendering.
To display your segments you must shift their coordinates as well. plot
returns the offset values applied. In the following example I assumed that you wanted to plot the trunks of the trees
library(lidR)
library(rgl)
LASfile <- system.file("extdata", "MixedConifer.laz", package="lidR")
las <- readLAS(LASfile, select = "xyz", filter = "-drop_z_below 0")
ttops <- tree_detection(las, lmf(ws = 5))
x = coordinates(ttops)[,1]
y = coordinates(ttops)[,2]
z = ttops$Z
offset = plot(las)
add_treetops3d(offset, ttops)
xoffset = offset[1]
yoffset = offset[2]
for (i in 1:length(ttops))
{
X = c(x[i], x[i]) - xoffset
Y = c(y[i], y[i]) - yoffset
Z = c(0, z[i])
segments3d(X, Y, Z, col = "white", size = 2)
}