简体   繁体   中英

Select points within a cylinder from a 3D point cloud

I have large point files (point clouds?) that have X, Y, Z and 4th 'Value' column. From these point clouds, I want to select points that belong into a shape (eg a cylinder) and change the value. I can use.loc and identify the points in a box area, but cannot figure out how to do it for cylinders (or other arbitrary shape). in the example below I create a small point cloud, and then select the points within a box defined by X, Y, Z limits - my current need is to define a cylinder with height of 10 and radius of 1 (the cylinder should be along the X axis, starting from -5, and centered around X=0, and Z = 3) any suggestions appreciated

import numpy as np# for array data processing
import pandas as pd
from matplotlib import pyplot as plt
X, Y, Z, V = np.mgrid[-10:10:10j, -2:2:10j, 0:5:10j, 1:1:10j]
data = np.array([X, Y, Z, V]).reshape(4, -1).T
points = pd.DataFrame(data, columns = ['X', 'Y', 'Z', 'Value'])
#boundaries of interest
Xl = 5
Yl = 1
Ztop = 4
Zbottom = 2
#in box
pointsneeded = points.loc[(points['X'] >= -Xl) & (points['X'] <= Xl) & (points['Y'] >= -Yl) & (points['Y'] <= Yl) 
                       & (points['Z'] >= Zbottom) & (points['Z'] <= Ztop)]

#visualize
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
scat = ax.scatter(points['X'], points['Y'], points['Z'], c=points['Value'], alpha=0.1)
scat1 = ax.scatter(pointsneeded['X'], pointsneeded['Y'], pointsneeded['Z'], c='r', alpha=0.5)
plt.show()

Split the cylinder into 2 problem: (1) (x, y) is inside a circle and (2) z value is between certain range

For example for point p(x, y, z)

def inside_circle(x, y, circle_radius):
  return sqrt(x*x+y*y) <= circle_radius:

def between_range(z, min_z, max_z):
  return min_z <= z <= max_z

radius, min_z, max_z = ...some number...
points_to_keep = list()
for x, y, z in your_dataset:
  if inside_circle(x,y) and between_range(z, min_z, max_z):
    points_to_keep.append([x,y,z])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM