3

I will have a 3-d grid of points (defined by Cartesian vectors). For any given coordinate within the grid, I wish to find the 8 grid points making the cuboid which surrounds the given coordinate. I also need the distances between the vertices of the cuboid and the given coordinate. I have found a way of doing this for a meshgrid with regular spacings, but not for irregular spacings. I do not yet have an example of the irregularly spaced grid data, I just know that the algorithm will have to deal with them eventually. My solution for the regularly spaced points is based off of this post, Finding index of nearest point in numpy arrays of x and y coordinates and is as follows:

import scipy as sp
import numpy as np

x, y, z = np.mgrid[0:5, 0:10, 0:20]
# Example 3-d grid of points.

b = np.dstack((x.ravel(), y.ravel(), z.ravel()))[0]
tree = sp.spatial.cKDTree(b)
example_coord = np.array([1.5, 3.5, 5.5])    
d, i = tree.query((example_coord), 8)
# i being the indices of the closest grid points, d being their distance from the 
# given coordinate, example_coord

b[i[0]], d[0]
# This gives one of the points of the surrounding cuboid and its distance from
# example_coord

I am looking to make this algorithm run as efficiently as possible as it will need to be run a lot. Thanks in advance for your help.

Community
  • 1
  • 1
F. Burns
  • 31
  • 3

0 Answers0