2

所以我试图从示例网格中找到 pyvista numpy 数组中的 k 个最近邻居。收到邻居后,我想在我的 3d 模型中实现一些区域的增长。

但不幸的是,我收到了一些奇怪的输出,您可以在下图中看到。好像我在KDTree实现上遗漏了一些东西。我正在关注类似问题的答案:https ://stackoverflow.com/a/2486341/9812286

import numpy as np 
from sklearn.neighbors import KDTree

import pyvista as pv

from pyvista import examples

# Example dataset with normals
mesh = examples.load_random_hills()

smooth = mesh

NDIM = 3
X = smooth.points
point = X[5000]

tree = KDTree(X, leaf_size=X.shape[0]+1)
# ind = tree.query_radius([point], r=10) # indices of neighbors within distance 0.3
distances, ind = tree.query([point], k=1000)

p = pv.Plotter()
p.add_mesh(smooth)

ids = np.arange(smooth.n_points)[ind[0]]
top = smooth.extract_cells(ids)
random_color = np.random.random(3)
p.add_mesh(top, color=random_color)

p.show()

具有两个不同颜色的细长斑块的表面的 3d 图

4

1 回答 1

2

你快到了:) 问题是你正在使用网格中的来构建树,然后提取单元格。当然,这些是无关的,因为点的索引在用作单元格的索引时会给你带来废话。

要么你必须extract_points

import numpy as np 
from sklearn.neighbors import KDTree

import pyvista as pv

from pyvista import examples

# Example dataset with normals
mesh = examples.load_random_hills()

smooth = mesh

NDIM = 3
X = smooth.points
point = X[5000]

tree = KDTree(X, leaf_size=X.shape[0]+1)
# ind = tree.query_radius([point], r=10) # indices of neighbors within distance 0.3
distances, ind = tree.query([point], k=1000)

p = pv.Plotter()
p.add_mesh(smooth)

ids = np.arange(smooth.n_points)[ind[0]]
top = smooth.extract_points(ids)  # changed here!
random_color = np.random.random(3)
p.add_mesh(top, color=random_color)

p.show()

在一个边缘附近带有彩色圆形区域的绘图

或者您必须首先与细胞中心合作:

import numpy as np 
from sklearn.neighbors import KDTree

import pyvista as pv

from pyvista import examples

# Example dataset with normals
mesh = examples.load_random_hills()

smooth = mesh

NDIM = 3
X = smooth.cell_centers().points  # changed here!
point = X[5000]

tree = KDTree(X, leaf_size=X.shape[0]+1)
# ind = tree.query_radius([point], r=10) # indices of neighbors within distance 0.3
distances, ind = tree.query([point], k=1000)

p = pv.Plotter()
p.add_mesh(smooth)

ids = np.arange(smooth.n_points)[ind[0]]
top = smooth.extract_cells(ids)
random_color = np.random.random(3)
p.add_mesh(top, color=random_color)

p.show()

圆形区域在中间某处着色的图形

如您所见,这两个结果不同,因为索引 5000(我们用于参考点)在索引点或索引单元格时意味着其他东西。

于 2021-01-25T21:18:16.500 回答