I need to check if a point lies inside a bounding cuboid. The number of cuboids is very large (~4M). The code I come up with is:
import numpy as np
# set the numbers of points and cuboids
n_points = 64
n_cuboid = 4000000
# generate the test data
points = np.random.rand(1, 3, n_points)*512
cuboid_min = np.random.rand(n_cuboid, 3, 1)*512
cuboid_max = cuboid_min + np.random.rand(n_cuboid, 3, 1)*8
# main body: check if the points are inside the cuboids
inside_cuboid = np.all((points > cuboid_min) & (points < cuboid_max), axis=1)
indices = np.nonzero(inside_cuboid)
It takes 8 seconds to run np.all
and 3 seconds to run np.nonzero
on my computer. Any idea to speed up the code?