Reputation: 87
I am working with LiDAR point cloud data in R and trying to calculate the robust normals for the points using a local plane-based approach. My goal is to compute the normals for each point in the point cloud and keep track of the valid indices where the normals have been successfully computed.
I have written a function called robust_normals to perform this task, which uses the RANN package to find neighbors within a search radius around each point. Then, it calculates the normal vectors using eigen decomposition on the covariance matrix of the centered neighbor points.
The problem is that when I run this function, it does not return the correct list of valid indices where the normals have been computed. Here's the current implementation of my robust_normals function:
robust_normals <- function(pts, search_radius = 10) {
kdtree <- RANN::nn2(pts, query = pts, treetype = "kd", searchtype = "radius", radius = search_radius)
normals <- matrix(NA, nrow = nrow(pts), ncol = 3)
valid_indices <- list()
for (i in 1:nrow(pts)) {
neighbors <- pts[kdtree$nn.idx[[i]], , drop = FALSE]
if (nrow(neighbors) < 2) {
cat("Not enough neighbors for point", i, "\n")
next
}
centered_neighbors <- scale(neighbors, scale = FALSE, center = pts[i, ])
cov_matrix <- cov(centered_neighbors)
if (any(is.infinite(cov_matrix)) || any(is.na(cov_matrix))) {
cat("Invalid covariance matrix for point", i, "\n")
next
}
eigen_decomp <- eigen(cov_matrix)
normals[i, ] <- eigen_decomp$vectors[, 3]
valid_indices[[length(valid_indices) + 1]] <- i
}
return(list(normals = normals, valid_indices = valid_indices))
}
The valid_indices list turns out to be empty (NULL), even though there should be valid indices where normals have been successfully computed. I am not sure why this is happening. Can someone help me identify the issue with my implementation or suggest an alternative way to compute the robust normals and keep track of the valid indices?
Upvotes: 0
Views: 187