2 years ago
#61708
Joshhh
Intersection between a uniform grid and a non uniform grid
I have two grids - one is uniform and another is not. I am trying to efficiently compute the intersection between the two grids, all the intersection between every pair of cells in the uniform grid and non-uniform grid.
My current implementation iterates over the cells in the uniform grid, and computes the intersection with all of the cells in the non uniform grid that can possibly intersect. As this is a part of a larger code, I cannot paste it all here, so I wrote exactly what each variable means.
#computes the volume of intersection between a cube with
#edge coordinates a = [x_min, x_max, y_min, y_max, z_min,z_max]
#and a list of cubes, b[i,] = [x_(i,min), x_(i,max), y_(i,min), y_(i,max), z_(i,min),z_(i,max)]
#where i< n_non_uniform. Returns a (n_non_uniform,) sized array.
#non positive numbers indicate disjoint cubes.
def intersect_3d_multi(a,b):
side_one = np.maximum(np.minimum(a[1],b[:,1])-np.maximum(a[0],b[:,0]),0)
side_two = np.maximum(np.minimum(a[3],b[:,3])-np.maximum(a[2],b[:,2]),0)
side_three = np.maximum(np.minimum(a[5],b[:,5])-np.maximum(a[4],b[:,4]),0)
return side_one*side_two*side_three
#ngrid - number of cells along one dimension
#grid_rects - a (ngrid**3,6) array, containing the (x,y,z) coordinates of two opposing edges
#of cells in the uniform grid
#r - a (n_non_uniform,6) array, containing the (x,y,z) coordinates of two opposing edges
#of cells in the non uniform grid, which contains n_non_uniform cells
num_of_intersects = np.zeros((ngrid,ngrid,ngrid))
for n in range(ngrid**3):
grid_i = n % ngrid
grid_j = (n // ngrid) % ngrid
grid_k = n // (ngrid**2)
grid_cell_intersect = intersect_3d_multi(grid_rects[n,:],r)
num_of_intersects[grid_i,grid_j,grid_k] = (grid_cell_intersect > 0).sum() #for each cell
#in the uniform grid, count how many cells of the non uniform grid intersect it
This runs fine and does the job, the problem is that I want to have a 200x200x200
uniform grid, and the non uniform grid can contain up to 2 million cells, so I am looking for a more efficient way of doing that, that will not take ages to run.
python
numpy
intersection
0 Answers
Your Answer