kon*_*ant 23 python performance triangulation graph-algorithm numba
给定N曲面细分中的三角形数量,我有一个N X 3 X 3数组存储(x, y, z)每个三角形的所有三个顶点的坐标.我的目标是为每个三角形找到共享相同边缘的相邻三角形.这是一个错综复杂的部分是我不重复邻居计数的整个设置.也就是说,如果三角形j已被计为三角形的邻居i,则三角形i不应再次计为三角形的邻居j.这样,我想有一个地图存储每个索引三角形的邻居列表.如果我从索引中的三角形开始i,那么索引i将有三个邻居,而所有其他的将有两个或更少.举个例子,假设我有一个存储三角形顶点的数组:
import numpy as np
vertices = np.array([[[2.0, 1.0, 3.0],[3.0, 1.0, 2.0],[1.2, 2.5, -2.0]],
[[3.0, 1.0, 2.0],[1.0, 2.0, 3.0],[1.2, -2.5, -2.0]],
[[1.0, 2.0, 3.0],[2.0, 1.0, 3.0],[3.0, 1.0, 2.0]],
[[1.0, 2.0, 3.0],[2.0, 1.0, 3.0],[2.2, 2.0, 1.0]],
[[1.0, 2.0, 3.0],[2.2, 2.0, 1.0],[4.0, 1.0, 0.0]],
[[2.0, 1.0, 3.0],[2.2, 2.0, 1.0],[-4.0, 1.0, 0.0]]])
Run Code Online (Sandbox Code Playgroud)
假设我从顶点索引开始计数2,即具有顶点的那个[[1.0, 2.0, 3.0],[2.0, 1.0, 3.0],[3.0, 1.0, 2.0]],那么,我希望我的输出类似于:
neighbour = [[], [], [0, 1, 3], [4, 5], [], []].
Run Code Online (Sandbox Code Playgroud)
更新: 根据@ Ajax1234的回答,我认为存储输出的好方法就像@ Ajax1234的演示一样.然而,在某种意义上,输出中存在模糊性,因为不可能知道哪个邻居是哪个.虽然示例数组不好,但我有二十面体的实际顶点,那么如果我从一个给定的三角形开始,我保证第一个有3个邻居,剩下两个邻居(直到所有三角形数都耗尽) .在这方面,假设我有一个以下数组:
vertices1 = [[[2, 1, 3], [3, 1, 2], [1, 2, -2]],
[[3, 1, 2], [1, 2, 3], [1, -2, 2]],
[[1, 2, 3], [2, 1, 3], [3, 1, 2]],
[[1, 2, 3], [2, 1, 3], [2, 2, 1]],
[[1, 2, 3], [2, 2, 1], [4, 1, 0]],
[[2, 1, 3], [2, 2, 1], [-4, 1, 0]],
[[3, 1, 3], [2, 2, 1], [-4, 1, 0]],
[[8, 1, 2], [1, 2, 3], [1, -2, 2]]]
Run Code Online (Sandbox Code Playgroud)
@ Ajax1234在下面的答案中显示的BFS算法给出了输出
[0, 1, 3, 7, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
而如果我只是交换最后一个元素的位置那么
vertices2 = [[[2, 1, 3], [3, 1, 2], [1, 2, -2]],
[[3, 1, 2], [1, 2, 3], [1, -2, 2]],
[[1, 2, 3], [2, 1, 3], [3, 1, 2]],
[[1, 2, 3], [2, 1, 3], [2, 2, 1]],
[[1, 2, 3], [2, 2, 1], [4, 1, 0]],
[[8, 1, 2], [1, 2, 3], [1, -2, 2]],
[[2, 1, 3], [2, 2, 1], [-4, 1, 0]],
[[3, 1, 3], [2, 2, 1], [-4, 1, 0]]]
Run Code Online (Sandbox Code Playgroud)
它给出了一个输出
[0, 1, 3, 4, 5, 6, 7].
Run Code Online (Sandbox Code Playgroud)
这有点模糊,因为在网格中的位置根本没有改变,它们只是被交换了.因此,我想以一致的方式进行搜索.例如,在指数2第一次搜索的邻居给了[0, 1, 3]两个vertices1和vertices2,现在我想搜索是在索引0,觉得没有什么,因此进入下一个单元1应该找到指数7的vertices1和指数5的vertices2.因此,该电流输出应当是[0, 1, 3, 7],[0, 1, 3, 5]对于vertices1和vertices2分别.接下来我们去索引3,依此类推.在我们用尽所有搜索之后,第一个的最终输出应该是
[0, 1, 3, 7, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
而第二个应该
[0, 1, 3, 5, 4, 6, 7].
Run Code Online (Sandbox Code Playgroud)
实现这一目标的有效方法是什么?
kon*_*ant 14
由于@ Ajax1234的指导,我找到了答案.根据您比较列表元素的方式,有一个小的复杂性.这是一种工作方法:
import numpy as np
from collections import deque
import time
d = [[[2, 1, 3], [3, 1, 2], [1, 2, -2]],
[[3, 1, 2], [1, 2, 3], [1, -2, 2]],
[[1, 2, 3], [2, 1, 3], [3, 1, 2]],
[[1, 2, 3], [2, 1, 3], [2, 2, 1]],
[[1, 2, 3], [2, 2, 1], [4, 1, 0]],
[[2, 1, 3], [2, 2, 1], [-4, 1, 0]],
[[3, 1, 3], [2, 2, 1], [-4, 1, 0]]]
def bfs(d, start):
queue = deque([d[start]])
seen = [start]
results = []
while queue:
_vertices = queue.popleft()
current = [[i, a] for i, a in enumerate(d) if len([x for x in a if x in _vertices])==2 and i not in seen]
if len(current)>0:
current_array = np.array(current, dtype=object)
current0 = list(current_array[:, 0])
current1 = list(current_array[:, 1])
results.extend(current0)
queue.extend(current1)
seen.extend(current0)
return results
time1 = time.time()
xo = bfs(d, 2)
print(time.time()-time1)
print(bfs(d, 2))
Run Code Online (Sandbox Code Playgroud)
对于大小数组(3000, 3, 3),代码当前需要18几秒钟才能运行.如果我添加@numba.jit(parallel = True, error_model='numpy'),那它实际上需要30几秒钟.可能是因为queue不支持numba.如果任何人能够建议如何更快地制作这些代码,我会很高兴.
更新
有代码,目前已被删除,并在代码运行一些冗余14秒,而不是30秒,为的d大小(4000 X 3 X 3).仍然不是很好,但是一个很好的进步,现在代码看起来更干净.
Pau*_*zer 11
如果您愿意使用该networkx库,则可以利用其快速bfs实现.我知道,添加另一个依赖是烦人的,但性能增益似乎很大,见下文.
import numpy as np
from scipy import spatial
import networkx
vertices = np.array([[[2.0, 1.0, 3.0],[3.0, 1.0, 2.0],[1.2, 2.5, -2.0]],
[[3.0, 1.0, 2.0],[1.0, 2.0, 3.0],[1.2, -2.5, -2.0]],
[[1.0, 2.0, 3.0],[2.0, 1.0, 3.0],[3.0, 1.0, 2.0]],
[[1.0, 2.0, 3.0],[2.0, 1.0, 3.0],[2.2, 2.0, 1.0]],
[[1.0, 2.0, 3.0],[2.2, 2.0, 1.0],[4.0, 1.0, 0.0]],
[[2.0, 1.0, 3.0],[2.2, 2.0, 1.0],[-4.0, 1.0, 0.0]]])
vertices1 = np.array([[[2, 1, 3], [3, 1, 2], [1, 2, -2]],
[[3, 1, 2], [1, 2, 3], [1, -2, 2]],
[[1, 2, 3], [2, 1, 3], [3, 1, 2]],
[[1, 2, 3], [2, 1, 3], [2, 2, 1]],
[[1, 2, 3], [2, 2, 1], [4, 1, 0]],
[[2, 1, 3], [2, 2, 1], [-4, 1, 0]],
[[3, 1, 3], [2, 2, 1], [-4, 1, 0]],
[[8, 1, 2], [1, 2, 3], [1, -2, 2]]])
def make(N=3000):
"""create a N random points and triangulate"""
points= np.random.uniform(-10, 10, (N, 3))
tri = spatial.Delaunay(points[:, :2])
return points[tri.simplices]
def bfs_tree(triangles, root=0, return_short=True):
"""convert triangle list to graph with vertices = triangles,
edges = pairs of triangles with shared edge and compute bfs tree
rooted at triangle number root"""
# use the old view as void trick to merge triplets, so they can
# for example be easily compared
tr_as_v = triangles.view(f'V{3*triangles.dtype.itemsize}').reshape(
triangles.shape[:-1])
# for each triangle write out its edges, this involves doubling the
# data becaues each vertex occurs twice
tr2 = np.concatenate([tr_as_v, tr_as_v], axis=1).reshape(-1, 3, 2)
# sort vertices within edges ...
tr2.sort(axis=2)
# ... and glue them together
tr2 = tr2.view(f'V{6*triangles.dtype.itemsize}').reshape(
triangles.shape[:-1])
# to find shared edges, sort them ...
idx = tr2.ravel().argsort()
tr2s = tr2.ravel()[idx]
# ... and then compare consecutive ones
pairs, = np.where(tr2s[:-1] == tr2s[1:])
assert np.all(np.diff(pairs) >= 2)
# these are the edges of the graph, the vertices are implicitly
# named 0, 1, 2, ...
edges = np.concatenate([idx[pairs,None]//3, idx[pairs+1,None]//3], axis=1)
# construct graph ...
G = networkx.Graph(edges.tolist())
# ... and let networkx do its magic
res = networkx.bfs_tree(G, root)
if return_short:
# sort by distance from root and then by actual path
sp = networkx.single_source_shortest_path(res, root)
sp = [sp[i] for i in range(len(sp))]
sp = [(len(p), p) for p in sp]
res = sorted(range(len(res.nodes)), key=sp.__getitem__)
return res
Run Code Online (Sandbox Code Playgroud)
演示:
# OP's second example:
>>> bfs_tree(vertices1, 2)
[2, 0, 1, 3, 7, 4, 5, 6]
>>>
# large random example
>>> random_data = make()
>>> random_data.shape
(5981, 3, 3)
>>> bfs = bfs_tree(random_data)
# returns instantly
Run Code Online (Sandbox Code Playgroud)
您描述的过程听起来类似于广度优先搜索,可用于查找邻居的三角形.然而,输出仅给出了索引,因为不清楚空列表如何在最终输出中结束:
from collections import deque
d = [[[2.0, 1.0, 3.0], [3.0, 1.0, 2.0], [1.2, 2.5, -2.0]], [[3.0, 1.0, 2.0], [1.0, 2.0, 3.0], [1.2, -2.5, -2.0]], [[1.0, 2.0, 3.0], [2.0, 1.0, 3.0], [3.0, 1.0, 2.0]], [[1.0, 2.0, 3.0], [2.0, 1.0, 3.0], [2.2, 2.0, 1.0]], [[1.0, 2.0, 3.0], [2.2, 2.0, 1.0], [4.0, 1.0, 0.0]], [[2.0, 1.0, 3.0], [2.2, 2.0, 1.0], [-4.0, 1.0, 0.0]]]
def bfs(d, start):
queue = deque([d[start]])
seen = [start]
results = []
while queue:
_vertices = queue.popleft()
exists_at = [i for i, a in enumerate(d) if a == _vertices][0]
current = [i for i, a in enumerate(d) if any(c in a for c in _vertices) and i != exists_at and i not in seen]
results.extend(current)
queue.extend([a for i, a in enumerate(d) if any(c in a for c in _vertices) and i != exists_at and i not in seen])
seen.extend(current)
return results
print(bfs(d, 2))
Run Code Online (Sandbox Code Playgroud)
输出:
[0, 1, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
这些函数由源代码中的注释记录.一般的文档真的很棒.当我第一次使用它时,我也发现它不是那么直接,但如果你有基础知识,它是一个功能强大且易于使用的软件包.
我假设最大的问题是如何获得干净的网格定义.如果您只有顶点坐标(如stl格式),则可能会出现问题,因为没有明确定义两个浮点数相等的点.
例
import trimesh
import numpy as np
vertices = np.array([[[2.0, 1.0, 3.0],[3.0, 1.0, 2.0],[1.2, 2.5, -2.0]],
[[3.0, 1.0, 2.0],[1.0, 2.0, 3.0],[1.2, -2.5, -2.0]],
[[1.0, 2.0, 3.0],[2.0, 1.0, 3.0],[3.0, 1.0, 2.0]],
[[1.0, 2.0, 3.0],[2.0, 1.0, 3.0],[2.2, 2.0, 1.0]],
[[1.0, 2.0, 3.0],[2.2, 2.0, 1.0],[4.0, 1.0, 0.0]],
[[2.0, 1.0, 3.0],[2.2, 2.0, 1.0],[-4.0, 1.0, 0.0]]])
#generate_faces
# I assume here that your input format is N x NPoints x xyz
faces=np.arange(vertices.shape[0]*3).reshape(-1,3)
#reshape_vertices (nx3)
vertices=vertices.reshape(-1,3)
#Create mesh object
mesh=trimesh.Trimesh(vertices=vertices, faces=faces)
# Set the tolerance to define which vertices are equal (default 1e-8)
# It is easy to prove that int(5)==int(5) but is 5.000000001 equal to 5.0 or not?
# This depends on the algorithm/ programm from which you have imported the mesh....
# To find a proper value for the tolerance and to heal the mesh if necessary, will
# likely be the most complicated part
trimesh.constants.tol.merge=tol
#merge the vertices
mesh.merge_vertices()
# At this stage you may need some sort of healing algorithm..
# eg. reconstruct the input
mesh.vertices[mesh.faces]
#get for example the faces, vertices
mesh.faces #These are indices to the vertices
mesh.vertices
# get the faces which touch each other on the edges
mesh.face_adjacency
Run Code Online (Sandbox Code Playgroud)
这给出了一个简单的2d数组,它面对共享边.我个人会使用这种格式进行进一步计算.如果你想坚持你的定义,我会创建一个nx3 numpy数组(每个三角形应该有最多3个边缘邻居).可以使用NaN或其他有意义的东西填充缺失值.
如果你真的想这样做,我可以添加一种有效的方法.
| 归档时间: |
|
| 查看次数: |
910 次 |
| 最近记录: |