open3d.ml.tf.ops.knn_search¶
-
open3d.ml.tf.ops.
knn_search
(points, queries, k, points_row_splits, queries_row_splits, metric='L2', ignore_query_point=False, return_distances=False, name=None)¶ Computes the indices of k nearest neighbors.
This op computes the neighborhood for each query point and returns the indices of the neighbors. The output format is compatible with the radius_search and fixed_radius_search ops and supports returning less than k neighbors if there are less than k points or ignore_query_point is enabled and the queries and points arrays are the same point cloud. The following example shows the usual case where the outputs can be reshaped to a [num_queries, k] tensor:
import tensorflow as tf import open3d.ml.tf as ml3d points = [ [0.1,0.1,0.1], [0.5,0.5,0.5], [1.7,1.7,1.7], [1.8,1.8,1.8], [0.3,2.4,1.4]] queries = [ [1.0,1.0,1.0], [0.5,2.0,2.0], [0.5,2.1,2.2], ] ans = ml3d.ops.knn_search(points, queries, k=2, points_row_splits=[0,5], queries_row_splits=[0,3], return_distances=True) # returns ans.neighbors_index = [1, 2, 4, 2, 4, 2] # ans.neighbors_row_splits = [0, 2, 4, 6] # ans.neighbors_distance = [0.75 , 1.47, 0.56, 1.62, 0.77, 1.85] # Since there are more than k points and we do not ignore any points we can # reshape the output to [num_queries, k] with neighbors_index = tf.reshape(ans.neighbors_index, [3,2]) neighbors_distance = tf.reshape(ans.neighbors_distance, [3,2]) # or with pytorch import torch import open3d.ml.torch as ml3d points = torch.Tensor([ [0.1,0.1,0.1], [0.5,0.5,0.5], [1.7,1.7,1.7], [1.8,1.8,1.8], [0.3,2.4,1.4]]) queries = torch.Tensor([ [1.0,1.0,1.0], [0.5,2.0,2.0], [0.5,2.1,2.2], ]) radii = torch.Tensor([1.0,1.0,1.0]) ans = ml3d.ops.knn_search(points, queries, k=2, points_row_splits=torch.LongTensor([0,5]), queries_row_splits=torch.LongTensor([0,3]), return_distances=True) # returns ans.neighbors_index = [1, 2, 4, 2, 4, 2] # ans.neighbors_row_splits = [0, 2, 4, 6] # ans.neighbors_distance = [0.75 , 1.47, 0.56, 1.62, 0.77, 1.85] # Since there are more than k points and we do not ignore any points we can # reshape the output to [num_queries, k] with neighbors_index = ans.neighbors_index.reshape(3,2) neighbors_distance = ans.neighbors_distance.reshape(3,2)
- Parameters
points – A Tensor. Must be one of the following types: float32, float64. The 3D positions of the input points.
queries – A Tensor. Must have the same type as points. The 3D positions of the query points.
k – A Tensor of type int32. The number of nearest neighbors to search.
points_row_splits – A Tensor of type int64. 1D vector with the row splits information if points is batched. This vector is [0, num_points] if there is only 1 batch item.
queries_row_splits – A Tensor of type int64. 1D vector with the row splits information if queries is batched. This vector is [0, num_queries] if there is only 1 batch item.
metric – An optional string from: “L1”, “L2”. Defaults to “L2”. Either L1 or L2. Default is L2
ignore_query_point –
An optional bool. Defaults to False. If true the points that coincide with the center of the
search window will be ignored. This excludes the query point if queries and
points are the same point cloud.
return_distances – An optional bool. Defaults to False. If True the distances for each neighbor will be returned in the output tensor neighbors_distances. If False a zero length Tensor will be returned for neighbors_distances.
name – A name for the operation (optional).
- Returns
A tuple of Tensor objects (neighbors_index, neighbors_row_splits, neighbors_distance).
- neighbors_index: A Tensor of type int32. The compact list of indices of the neighbors. The
corresponding query point can be inferred from the neighbor_count_prefix_sum vector. Neighbors for the same point are sorted with respect to the distance.
Note that there is no guarantee that there will be exactly k neighbors in some cases. These cases are:
There are less than k points.
ignore_query_point is True and there are multiple points with the same position.
- neighbors_row_splits: A Tensor of type int64. The exclusive prefix sum of the neighbor count for the
query points including the total neighbor count as the last element. The size of this array is the number of queries + 1.
- neighbors_distance: A Tensor. Has the same type as points. Stores the distance to each neighbor if return_distances
is True. The distances are squared only if metric is L2. This is a zero length Tensor if return_distances is False.