1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
2.5 |
static const char RCSid[] = "$Id: o_instance.c,v 2.4 2003/07/27 22:12:03 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
greg |
2.3 |
* o_instance.c - routines for creating octrees for other octrees |
6 |
greg |
1.1 |
*/ |
7 |
|
|
|
8 |
|
|
#include "standard.h" |
9 |
|
|
|
10 |
|
|
#include "object.h" |
11 |
greg |
2.2 |
|
12 |
|
|
#include "octree.h" |
13 |
greg |
1.1 |
|
14 |
|
|
#include "instance.h" |
15 |
|
|
|
16 |
greg |
2.3 |
#include "mesh.h" |
17 |
|
|
|
18 |
greg |
1.1 |
#include "plocate.h" |
19 |
|
|
|
20 |
|
|
/* |
21 |
|
|
* To determine if two cubes intersect: |
22 |
|
|
* |
23 |
|
|
* 1) Check to see if any vertices of first cube are inside the |
24 |
|
|
* second (intersection). |
25 |
|
|
* |
26 |
|
|
* 2) Check to see if all vertices of first are to one side of |
27 |
|
|
* second (no intersection). |
28 |
|
|
* |
29 |
|
|
* 3) Perform 1 and 2 with roles reversed. |
30 |
|
|
* |
31 |
|
|
* 4) Check to see if any portion of any edge of second is inside |
32 |
|
|
* first (intersection). |
33 |
|
|
* |
34 |
|
|
* 5) If test 4 fails, we have no intersection. |
35 |
|
|
* |
36 |
|
|
* Note that if we were testing two boxes, we would need |
37 |
|
|
* to check that neither had any edges inside the other to be sure. |
38 |
|
|
* Since an octree is a volume rather than a surface, we will |
39 |
|
|
* return a value of 2 if the cube is entirely within the octree. |
40 |
|
|
*/ |
41 |
|
|
|
42 |
|
|
|
43 |
schorsch |
2.5 |
static int o_cube(CUBE *cu1, FULLXF *fxf, CUBE *cu); |
44 |
|
|
|
45 |
|
|
|
46 |
greg |
2.3 |
static int |
47 |
schorsch |
2.5 |
o_cube( /* determine if cubes intersect */ |
48 |
|
|
CUBE *cu1, |
49 |
|
|
FULLXF *fxf, |
50 |
|
|
CUBE *cu |
51 |
|
|
) |
52 |
greg |
1.1 |
{ |
53 |
greg |
1.2 |
static int vstart[4] = {0, 3, 5, 6}; |
54 |
greg |
1.1 |
FVECT cumin, cumax; |
55 |
|
|
FVECT vert[8]; |
56 |
|
|
FVECT v1, v2; |
57 |
|
|
int vloc, vout; |
58 |
|
|
register int i, j; |
59 |
|
|
/* check if cube vertex in octree */ |
60 |
|
|
for (j = 0; j < 3; j++) |
61 |
greg |
2.3 |
cumax[j] = (cumin[j] = cu1->cuorg[j]) + cu1->cusize; |
62 |
greg |
1.1 |
vloc = ABOVE | BELOW; |
63 |
|
|
vout = 0; |
64 |
|
|
for (i = 0; i < 8; i++) { |
65 |
|
|
for (j = 0; j < 3; j++) { |
66 |
|
|
v1[j] = cu->cuorg[j]; |
67 |
|
|
if (i & 1<<j) |
68 |
|
|
v1[j] += cu->cusize; |
69 |
|
|
} |
70 |
greg |
2.3 |
multp3(v2, v1, fxf->b.xfm); |
71 |
schorsch |
2.4 |
if ( (j = plocate(v2, cumin, cumax)) ) |
72 |
greg |
1.1 |
vout++; |
73 |
|
|
vloc &= j; |
74 |
|
|
} |
75 |
|
|
if (vout == 0) /* all inside */ |
76 |
greg |
1.3 |
return(O_IN); |
77 |
greg |
1.1 |
if (vout < 8) /* some inside */ |
78 |
greg |
1.3 |
return(O_HIT); |
79 |
greg |
1.1 |
if (vloc) /* all to one side */ |
80 |
greg |
1.3 |
return(O_MISS); |
81 |
greg |
1.1 |
/* octree vertices in cube? */ |
82 |
|
|
for (j = 0; j < 3; j++) |
83 |
|
|
cumax[j] = (cumin[j] = cu->cuorg[j]) + cu->cusize; |
84 |
|
|
vloc = ABOVE | BELOW; |
85 |
|
|
for (i = 0; i < 8; i++) { |
86 |
|
|
for (j = 0; j < 3; j++) { |
87 |
greg |
2.3 |
v1[j] = cu1->cuorg[j]; |
88 |
greg |
1.1 |
if (i & 1<<j) |
89 |
greg |
2.3 |
v1[j] += cu1->cusize; |
90 |
greg |
1.1 |
} |
91 |
greg |
2.3 |
multp3(vert[i], v1, fxf->f.xfm); |
92 |
schorsch |
2.4 |
if ( (j = plocate(vert[i], cumin, cumax)) ) |
93 |
greg |
1.1 |
vloc &= j; |
94 |
|
|
else |
95 |
greg |
1.3 |
return(O_HIT); /* vertex inside */ |
96 |
greg |
1.1 |
} |
97 |
|
|
if (vloc) /* all to one side */ |
98 |
greg |
1.3 |
return(O_MISS); |
99 |
greg |
1.1 |
/* check edges */ |
100 |
|
|
for (i = 0; i < 4; i++) |
101 |
|
|
for (j = 0; j < 3; j++) { |
102 |
|
|
/* clip modifies vertices! */ |
103 |
|
|
VCOPY(v1, vert[vstart[i]]); |
104 |
|
|
VCOPY(v2, vert[vstart[i] ^ 1<<j]); |
105 |
|
|
if (clip(v1, v2, cumin, cumax)) |
106 |
greg |
1.3 |
return(O_HIT); /* edge inside */ |
107 |
greg |
1.1 |
} |
108 |
|
|
|
109 |
greg |
1.3 |
return(O_MISS); /* no intersection */ |
110 |
greg |
2.3 |
} |
111 |
|
|
|
112 |
|
|
|
113 |
schorsch |
2.5 |
/* XXX o_instance() is extern, but not declared in any header file */ |
114 |
greg |
2.3 |
int |
115 |
schorsch |
2.5 |
o_instance( /* determine if instance intersects */ |
116 |
|
|
OBJREC *o, |
117 |
|
|
CUBE *cu |
118 |
|
|
) |
119 |
greg |
2.3 |
{ |
120 |
|
|
INSTANCE *ins; |
121 |
|
|
/* get octree bounds */ |
122 |
|
|
ins = getinstance(o, IO_BOUNDS); |
123 |
|
|
/* call o_cube to do the work */ |
124 |
|
|
return(o_cube(&ins->obj->scube, &ins->x, cu)); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
schorsch |
2.5 |
/* XXX o_mesh() is extern, but not declared in any header file */ |
129 |
greg |
2.3 |
int |
130 |
schorsch |
2.5 |
o_mesh( /* determine if mesh intersects */ |
131 |
|
|
OBJREC *o, |
132 |
|
|
CUBE *cu |
133 |
|
|
) |
134 |
greg |
2.3 |
{ |
135 |
|
|
MESHINST *mip; |
136 |
|
|
/* get mesh bounds */ |
137 |
|
|
mip = getmeshinst(o, IO_BOUNDS); |
138 |
|
|
/* call o_cube to do the work */ |
139 |
|
|
return(o_cube(&mip->msh->mcube, &mip->x, cu)); |
140 |
greg |
1.1 |
} |