small.If these two conditions are met, then a square grid of side length Δ may be a good way to storeyour data. Here is how this works. First, for each of your objects, compute its axis-alignedbounding box.We can efficiently determine which cells of the grid are overlapped by thisbounding box as follows. Letpandqdenote the bounding box’s lower-left and upper-rightcorners (see Fig. 4(a)). Compute the cells of the grid that contain these points (see Fig. 4(b)).Then store a pointer to the object in all the grid cells that lie in the rectangle defined by thesetwo cells (see Fig. 4(c)). Note that this is not perfect, since the object may be associated withgrid cells that it does not actually intersect. This increases the space of the data structure,but it does not compromise the data structure’s correctness.(a)(b)(c)ΔΔqpijΔqpijFig. 4: Storing an object in a grid.Computing the indices of the grid cell that contain a given point is a simple exercise in integerarithmetic. For example, ifp= (px, py), then letj=jpxΔkandi=jpyΔk.Then, the pointplies within the grid cellG[i, j].If the diameter of most of the objects is not significantly larger than Δ, then each object willonly be associated with a constant number of grid cells. If the density of objects is not tooLecture 85Spring 2018
CMSC 425Dave Mount & Roger Eastmanhigh, then each grid square will only need to store a constant number of pointers. Thus, ifthe above assumptions are satisfied then the data structure will be relatively space efficient.Storing a Grid:As we have seen, a grid consists of a collection of cells where each cell stores aset of pointers to the objects that overlap this cell (or at least might overlap this cell). Howdo we store these cells? Here are a few ideas.d-dimensional array:The simplest implementation is to allocate ad-dimensional arraythat is sufficiently large to handle all the cells of your grid. If the distribution of objectsis relatively uniform, then it is reasonable to believe that a sizable fraction of the cellswill be nonempty.On the other hand, if the density is highly variable, there may bemany empty cells. This approach will waste space.Hash map:Each cell is identified by its indices, (i, j) for a 2-dimensional grid or (i, j, k) forthe 3-dimensional grid. Treat these indices like a key into a hash map. Whenever a newobjectois entered into some cell (i, j), we access the hash map to see whether this cellexists. If not, we generate a new cell object and add it to the hash map under the key(i, j). If so, we add a pointer toointo this hash map entry.Linear allocation:Suppose that we decide to adopt an array allocation. A straightforwardimplementation of thed-dimensional array will result in a memory layout according tohow your compiler chooses to allocate arrays, typically in what is calledrow-major order(see Fig. 5(a)). For example, if there areNcolumns, then the element (i, j) is mappedto indexi·N+jin row-major order.
You've reached the end of your free preview.
Want to read all 11 pages?
Geometry, Fig., Roger Eastman, Dave Mount, axis-aligned bounding boxes