Home > Net >  Generate a sphere at each point in a point cloud
Generate a sphere at each point in a point cloud

Time:07-11

I have a point cloud in the form of X,Y and Z stored in a txt file as :

1.25 4.65 9.15
2.37 8.62 7.59
.
.
.

I want to generate a sphere with a determined radius at each point in the point cloud and count the number of points that lie inside that sphere, then store that number in another txt file as:

14
27
10
.
.
.

which indicates the number of points lying in the sphere in every lap. How can I do this in python?

CodePudding user response:

If you have your points as a list of lists, you can do it by summing a generator expression and using math.dist to calculate the distances:

import math
lst = [[1.25, 4.65, 9.15],
[2.37, 8.62, 7.59]]


r = 5
[sum(1 for j in lst if math.dist(i, j) <= r)-1 for i in lst]

Output:

[1, 1]

Edit: You could also use a KDTree:

from scipy.spatial import KDTree
tree = KDTree(lst)
[len(tree.query_ball_point(i, r))-1 for i in lst]
  • Related