Skip to content Skip to sidebar Skip to footer

How To Find Adjacent Lines On A Regular 3d Grid In 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

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

Post a Comment for "How To Find Adjacent Lines On A Regular 3d Grid In Python"