如何在python中的常规3D网格上找到相邻的线 [英] How to find adjacent lines on a regular 3D grid in python

查看:54
本文介绍了如何在python中的常规3D网格上找到相邻的线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有一堆点的坐标,并希望在python包中从中创建表面.我想先整理好数据,然后再将其导入包中.点来自常规网格.首先,我基于点的位置创建线.在这一步中,我仅定义创建点的线号.我的输入数据是:

I have the coordinate of a bunch of points and want to create surfaces out of them in a python package. I want to arrange my data before importing them into the package. Points are coming from a regular grid. Firstly, I am creating lines based on the location of points. In this step I just define which point numbers create my lines. My input data is:

coord = np.array(
    [[0., 0., 2.], [0., 1., 3.], [0., 2., 2.], [1., 0., 1.], [1., 1., 3.], 
     [1., 2., 1.], [2., 0., 1.], [2., 1., 1.], [3., 0., 1.], [4., 0., 1.]])

下图显示了网格点的数量(灰色)和线的数量(蓝色和红色).

The figure below shows the numbers of the grid points (gray) and the numbers of the lines (blue and red).

通过字典对线进行建模,其中键是线号,值是具有起点和终点数字的元组:

The lines are modeled through dictionaries, in which the key is the line number, and the value is a tuple with the start and end points numbers:

In [906]: blue_line
Out[906]: {1: (1, 2), 2: (2, 3), 3: (4, 5), 4: (5, 6), 5: (7, 8)}

In [907]: red_line
Out[907]: 
{6: (1, 4),
 7: (2, 5),
 8: (3, 6),
 9: (4, 7),
 10: (5, 8),
 11: (7, 9),
 12: (9, 10)}

要了解有关行的生成方式的更多信息,请查看

To learn more about how the lines are generated, check out this thread. The lines that are used to create the surfaces are stored in a list:

surfaces = [(1, 6, 3, 7), (2, 7, 4, 8), (3, 9, 5, 10)]

作为最后一步,我想找到在创建曲面时未使用或已使用但比上图中虚线的极限值更近的线数.同样,我具有创建该虚线的两点的坐标:

As the last step, I want to find the number of lines which are not used in creating the surfaces or are used but are closer than a limit the the dashed line in the figure above. Again, I have the coordinate of the two points creating that dashed line:

coord_dash = [(2., 2., 2.), (5., 0., 1.)]
adjacency_threshold = 2

我希望将这些相邻的行作为另一个列表(图中红色箭头所示):

I want to have these adjacent lines as another list (shown by a red arrow in the figure):

adjacent_lines = [4, 10, 5, 11, 12]

我只有一个粗略的主意,不知道如何用Python编写代码.我只能创建线号和曲面,并且需要帮助来找到那些闭合线.

I have only this rough idea and do not know how to code it in Python. I can only create line numbers and surfaces and need help to find those close lines.

推荐答案

确定未使用的行很简单(NumPy的

Determining what lines have not been used is straightforward (NumPy's setdiff1d comes in handy for this task):

In [924]: all_line = {**blue_line, **red_line}

In [925]: lines = list(all_line.keys())

In [926]: lines
Out[926]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

In [927]: used_lines = np.ravel(surfaces)

In [928]: used_lines
Out[928]: array([ 1,  6,  3,  7,  2,  7,  4,  8,  3,  9,  5, 10])

In [929]: unused_lines = np.setdiff1d(lines, used_lines)

In [930]: unused_lines
Out[930]: array([11, 12])

可以使用NumPy的

The adjacent lines can be obtained by using NumPy's linalg.norm:

In [954]: midpoints
Out[954]: 
{1: array([0. , 0.5, 2.5]),
 2: array([0. , 1.5, 2.5]),
 3: array([1. , 0.5, 2. ]),
 4: array([1. , 1.5, 2. ]),
 5: array([2. , 0.5, 1. ]),
 6: array([0.5, 0. , 1.5]),
 7: array([0.5, 1. , 3. ]),
 8: array([0.5, 2. , 1.5]),
 9: array([1.5, 0. , 1. ]),
 10: array([1.5, 1. , 2. ]),
 11: array([2.5, 0. , 1. ]),
 12: array([3.5, 0. , 1. ])}

In [955]: mid_dash = np.array(coord_dash).mean(axis=0)

In [956]: mid_dash
Out[956]: array([3.5, 1. , 1.5])

In [957]: adjacent_lines = []
     ...: for idx, point in midpoints.items():
     ...:     if np.linalg.norm(point - mid_dash) < adjacency_threshold:
     ...:         adjacent_lines.append(idx)

In [958]: adjacent_lines
Out[958]: [5, 11, 12]

这篇关于如何在python中的常规3D网格上找到相邻的线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆