0

I have a numpy array of numpy arrays, where the major array elements represent the vertex data for 3D meshes of variable length, with the sub-arrays representing floats for the 3D vertex data as coordinates x, y, z.

The data looks like so, using 2 mesh example.

[ array([[  0., -64.,   0.],  
   [  0.,   0,   0.],  
   [ 64., -64.,   0.],  
   [ 64.,   0.,   0.]])  
 array([[  0.,   0.,   0.],  
   [  0.,  32.,   0.],    
   [  0.,  64.,   0.],  
   [ 32.,   0.,   0.],  
   [ 32.,  32.,   0.],  
   [ 32.,  64.,   0.],  
   [ 64.,   0.,   0.],  
   [ 64.,  32.,   0.],  
   [ 64.,  64.,   0.]])]

I want to compare the vertices of each mesh to each other mesh, calculating the distance between these vertices as part of a verification check.

My following code works, but for large data sets the nested loops are incredibly slow. The number of combinations can range from 1.5 million to 500 billion.

I believe this could all be vectorized using numpy, but have been unable to be successful. Any help in vectorizing this, or other optimizations would be greatly appreciated.

The variable "vertarray" is the numpy array of numpy arrays mentioned above.

for index_mesh1 in range(0, vertarray.shape[0]-1):                               # loop for 1st mesh in comparison 
    for index_mesh2 in range(1, vertarray.shape[0]):                                # loop for 2nd mesh in comparison
        for mesh_vertex_index1 in range(0, vertarray[index_mesh1].shape[0]):        # loop for vert element of 1st mesh comparison (this will always be 3 elements x, y, z)
            for mesh_vertex_index2 in range(0, vertarray[index_mesh2].shape[0]):    # loop for vert element of 2nd mesh comparison (this will always be 3 elements x, y, z)
                mesh1_verts = vertarray[index_mesh1][mesh_vertex_index1]            # vert coordinates to compare from 1st mesh
                mesh2_verts = vertarray[index_mesh2][mesh_vertex_index2]            # vert coordinates to compare to 2nd mesh
                distance = np.linalg.norm(mesh1_verts-mesh2_verts)                  # distance between mesh 3D coordinates
2
  • What's the typical value of vertarray.shape[0] in your actual use case?
    – Divakar
    Dec 8, 2016 at 10:31
  • Thanks for the response Divakar! Typical value for vertarray.shape[0] can be upwards of 25,000.
    – dlc
    Dec 10, 2016 at 18:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.