How to find adjacent lines on a regular 3D grid in python

两盒软妹~` 提交于 2021-01-27 07:24:43

问题


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]

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.


回答1:


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])

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]


来源:https://stackoverflow.com/questions/65614219/how-to-find-adjacent-lines-on-a-regular-3d-grid-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!