1 /*
2     Copyright (c) 2005-2021 Intel Corporation
3 
4     Licensed under the Apache License, Version 2.0 (the "License");
5     you may not use this file except in compliance with the License.
6     You may obtain a copy of the License at
7 
8         http://www.apache.org/licenses/LICENSE-2.0
9 
10     Unless required by applicable law or agreed to in writing, software
11     distributed under the License is distributed on an "AS IS" BASIS,
12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13     See the License for the specific language governing permissions and
14     limitations under the License.
15 */
16 
17 /*
18     The original source for this example is
19     Copyright (c) 1994-2008 John E. Stone
20     All rights reserved.
21 
22     Redistribution and use in source and binary forms, with or without
23     modification, are permitted provided that the following conditions
24     are met:
25     1. Redistributions of source code must retain the above copyright
26        notice, this list of conditions and the following disclaimer.
27     2. Redistributions in binary form must reproduce the above copyright
28        notice, this list of conditions and the following disclaimer in the
29        documentation and/or other materials provided with the distribution.
30     3. The name of the author may not be used to endorse or promote products
31        derived from this software without specific prior written permission.
32 
33     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
34     OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35     WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36     ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
37     DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39     OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41     LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42     OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43     SUCH DAMAGE.
44 */
45 
46 /*
47  * grid.h - spatial subdivision efficiency structures
48  *
49  * $Id: grid.h,v 1.2 2007-02-22 17:54:15 Exp $
50  *
51  */
52 
53 int engrid_scene(object **list);
54 object *newgrid(int xsize, int ysize, int zsize, vector min, vector max);
55 
56 #ifdef GRID_PRIVATE
57 
58 typedef struct objectlist {
59     struct objectlist *next; /* next link in the list */
60     object *obj; /* the actual object */
61 } objectlist;
62 
63 typedef struct {
64     unsigned int id; /* Unique Object serial number */
65     void *nextobj; /* pointer to next object in list */
66     object_methods *methods; /* this object's methods */
67     texture *tex; /* object texture */
68     int xsize; /* number of cells along the X direction */
69     int ysize; /* number of cells along the Y direction */
70     int zsize; /* number of cells along the Z direction */
71     vector min; /* the minimum coords for the box containing the grid */
72     vector max; /* the maximum coords for the box containing the grid */
73     vector voxsize; /* the size of a grid cell/voxel */
74     object *objects; /* all objects contained in the grid */
75     objectlist **cells; /* the grid cells themselves */
76 } grid;
77 
78 typedef struct {
79     int x; /* Voxel X address */
80     int y; /* Voxel Y address */
81     int z; /* Voxel Z address */
82 } gridindex;
83 
84 /*
85  * Convert from voxel number along X/Y/Z to corresponding coordinate.
86  */
87 #define voxel2x(g, X) ((X) * (g->voxsize.x) + (g->min.x))
88 #define voxel2y(g, Y) ((Y) * (g->voxsize.y) + (g->min.y))
89 #define voxel2z(g, Z) ((Z) * (g->voxsize.z) + (g->min.z))
90 
91 /*
92  * And vice-versa.
93  */
94 #define x2voxel(g, x) (((x)-g->min.x) / g->voxsize.x)
95 #define y2voxel(g, y) (((y)-g->min.y) / g->voxsize.y)
96 #define z2voxel(g, z) (((z)-g->min.z) / g->voxsize.z)
97 
98 static int grid_bbox(void *obj, vector *min, vector *max);
99 static void grid_free(void *v);
100 
101 static int cellbound(grid *g, gridindex *index, vector *cmin, vector *cmax);
102 
103 void engrid_objlist(grid *g, object **list);
104 static int engrid_object(grid *g, object *obj);
105 
106 static int engrid_objectlist(grid *g, objectlist **list);
107 static int engrid_cell(grid *, gridindex *);
108 
109 static int pos2grid(grid *g, vector *pos, gridindex *index);
110 static void grid_intersect(grid *, ray *);
111 static void voxel_intersect(grid *g, ray *ry, int voxaddr);
112 static int grid_bounds_intersect(grid *g, ray *ry, flt *near, flt *far);
113 
114 #endif
115