I have coordinates (x,y,z) for a evenly spaced 3D grid. Given a random 3D point (x,y,z) somewhere on the grid, how to find which cube has the point. I need to know the 8 corners of the cube.
I have the grid point data in a list of vectors in c++.
Thanks.
Assuming equal length for the grid cubes in all dimentions, you can get the coordinate closest to ohe origo by calculating
Where
gx,gy,gzare the grid cube coordinates closest to the origo (I’m assumingx,y,z>=0 here),%is the modulus operator andlis the length of the grid cubes.Note: You can perform the calculations this way as well:
gx = static_cast<int>(x)/l*l;(static_cast<>to account for non-integerx)Then the 8 corners of the grid cube
(x, y, z)falls into are: