Dipole
Dipole

Reputation: 1910

Convert point to line data using a list of point indices

I have a list of integers that denotes the indices of a set of 3D points that belong to a line like so:

pt_id = [n1, id1_1,...,id1_n1,n2,id2_1,...,id2_n2,n3,...]

What this means is that the first line is made up of n1 points with row indices id1_1 through id1_n1 and the second line is made up of n2 points with row indices id2_1 through id2_n2. The row indices refer to the row index of the array of points. In essence the first point of the first line is given by pts[id1_1,:], the second point of the first line is given by pts[id1_2,:] and the first point of the second line is given by pts[id2_1,:] and so on.

The points are given as a numpy array where each row is a point coordinate

pts = [[pt1x, pt1y, pt1z],
       [pt2x, pt2y, pt2z],
       ...
       [ptNx, ptNy, ptNz]]

Im looking for the most efficient way to collect the points that belong to the same line into an array. Example:

     pts = [[0.0, 0.0, 0.0],                                                                                                                                                                                       
            [0.0, 1.0, 0.0],
            [0.0, 2.0, 0.0],
            [1.0, 0.0, 0.0],
            [1.0, 1.0, 0.0],
            [1.0, 2.0, 0.0],
            [1.0, 3.0, 0.0],
            [2.0, 0.0, 0.0],
            [3.0, 2.5, 0.0]]
     pts = np.asarray(pts)
     pt_id = [3,0,1,2,4,3,4,5,6,2,7,8]

The results of the processing should give me in this case 3 lines:

line[0,:,:] =  [[0.0, 0.0, 0.0],                                                                                                                                                                                       
                [0.0, 1.0, 0.0],
                [0.0, 2.0, 0.0]]

line[1,:,:] =  [[1.0, 0.0, 0.0],
                [1.0, 1.0, 0.0],
                [1.0, 2.0, 0.0],
                [1.0, 3.0, 0.0]]

line[2,:,:] =  [[2.0, 0.0, 0.0],
                [3.0, 2.5, 0.0]]

When I plot the individual lines that are contained in the result I should get

enter image description here

Upvotes: 0

Views: 506

Answers (1)

Flabetvibes
Flabetvibes

Reputation: 3194

Here is the most efficient code which collect the points that belong to the same line into an array:

import numpy as np

pts = [[0.0, 0.0, 0.0],
       [0.0, 1.0, 0.0],
       [0.0, 2.0, 0.0],
       [1.0, 0.0, 0.0],
       [1.0, 1.0, 0.0],
       [1.0, 2.0, 0.0],
       [1.0, 3.0, 0.0],
       [2.0, 0.0, 0.0],
       [3.0, 2.5, 0.0]]
pts = np.asarray(pts)
pt_id = [3,0,1,2,4,3,4,5,6,2,7,8]

def get_line(k):
    # find i, the index of pt_id such that pt_id[i] = nk
    i = 0
    for j in range(0, k):
        i = i + pt_id[i] + 1
    # read the value of nk
    nk = pt_id[i]
    # return the points whose indices are [idk_1, ..., idk_nk]
    return pts[pt_id[i+1:i+1+nk], :]

print(get_line(0))
print(get_line(1))
print(get_line(2))

The result is exactly what you want:

[[ 0.  0.  0.]
 [ 0.  1.  0.]
 [ 0.  2.  0.]]

[[ 1.  0.  0.]
 [ 1.  1.  0.]
 [ 1.  2.  0.]
 [ 1.  3.  0.]]

[[ 2.   0.   0. ]
 [ 3.   2.5  0. ]]

I hope this will help you.

Upvotes: 1

Related Questions