1 |
greg |
1.1 |
#ifndef lint |
2 |
schorsch |
2.4 |
static const char RCSid[] = "$Id: o_face.c,v 2.3 2003/07/27 22:12:03 schorsch Exp $"; |
3 |
greg |
1.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* o_face.c - routines for creating octrees for polygonal faces. |
6 |
|
|
* |
7 |
|
|
* 8/27/85 |
8 |
|
|
*/ |
9 |
|
|
|
10 |
|
|
#include "standard.h" |
11 |
|
|
|
12 |
|
|
#include "octree.h" |
13 |
|
|
|
14 |
|
|
#include "object.h" |
15 |
|
|
|
16 |
|
|
#include "face.h" |
17 |
|
|
|
18 |
|
|
#include "plocate.h" |
19 |
|
|
|
20 |
|
|
/* |
21 |
|
|
* The algorithm for determining a face's intersection |
22 |
|
|
* with a cube is relatively straightforward: |
23 |
|
|
* |
24 |
|
|
* 1) Check to see if any vertices are inside the cube |
25 |
|
|
* (intersection). |
26 |
|
|
* |
27 |
|
|
* 2) Check to see if all vertices are to one side of |
28 |
|
|
* cube (no intersection). |
29 |
|
|
* |
30 |
|
|
* 3) Check to see if any portion of any edge is inside |
31 |
|
|
* cube (intersection). |
32 |
|
|
* |
33 |
|
|
* 4) Check to see if the cube cuts the plane of the |
34 |
|
|
* face and one of its edges passes through |
35 |
|
|
* the face (intersection). |
36 |
|
|
* |
37 |
|
|
* 5) If test 4 fails, we have no intersection. |
38 |
|
|
*/ |
39 |
|
|
|
40 |
schorsch |
2.4 |
/* XXX this is extern, but not declared in any header file yet */ |
41 |
|
|
int |
42 |
|
|
o_face( /* determine if face intersects cube */ |
43 |
|
|
OBJREC *o, |
44 |
|
|
CUBE *cu |
45 |
|
|
) |
46 |
greg |
1.1 |
{ |
47 |
|
|
FVECT cumin, cumax; |
48 |
|
|
FVECT v1, v2; |
49 |
|
|
double d1, d2; |
50 |
|
|
int vloc; |
51 |
|
|
register FACE *f; |
52 |
|
|
register int i, j; |
53 |
|
|
/* get face arguments */ |
54 |
|
|
f = getface(o); |
55 |
|
|
if (f->area == 0.0) /* empty face */ |
56 |
greg |
1.3 |
return(O_MISS); |
57 |
greg |
1.1 |
/* compute cube boundaries */ |
58 |
|
|
for (j = 0; j < 3; j++) |
59 |
greg |
1.4 |
cumax[j] = (cumin[j] = cu->cuorg[j]-FTINY) |
60 |
|
|
+ cu->cusize + 2.0*FTINY; |
61 |
greg |
1.1 |
|
62 |
|
|
vloc = ABOVE | BELOW; /* check vertices */ |
63 |
|
|
for (i = 0; i < f->nv; i++) |
64 |
schorsch |
2.3 |
if ( (j = plocate(VERTEX(f,i), cumin, cumax)) ) |
65 |
greg |
1.1 |
vloc &= j; |
66 |
|
|
else |
67 |
greg |
1.3 |
return(O_HIT); /* vertex inside */ |
68 |
greg |
1.1 |
|
69 |
|
|
if (vloc) /* all to one side */ |
70 |
greg |
1.3 |
return(O_MISS); |
71 |
greg |
1.1 |
|
72 |
|
|
for (i = 0; i < f->nv; i++) { /* check edges */ |
73 |
|
|
if ((j = i + 1) >= f->nv) |
74 |
|
|
j = 0; /* wrap around */ |
75 |
|
|
VCOPY(v1, VERTEX(f,i)); /* clip modifies */ |
76 |
|
|
VCOPY(v2, VERTEX(f,j)); /* the vertices! */ |
77 |
|
|
if (clip(v1, v2, cumin, cumax)) |
78 |
greg |
1.3 |
return(O_HIT); /* edge inside */ |
79 |
greg |
1.1 |
} |
80 |
|
|
/* see if cube cuts plane */ |
81 |
|
|
for (j = 0; j < 3; j++) |
82 |
|
|
if (f->norm[j] > 0.0) { |
83 |
|
|
v1[j] = cumin[j]; |
84 |
|
|
v2[j] = cumax[j]; |
85 |
|
|
} else { |
86 |
|
|
v1[j] = cumax[j]; |
87 |
|
|
v2[j] = cumin[j]; |
88 |
|
|
} |
89 |
greg |
1.2 |
if ((d1 = DOT(v1, f->norm) - f->offset) > FTINY) |
90 |
greg |
1.3 |
return(O_MISS); |
91 |
greg |
1.2 |
if ((d2 = DOT(v2, f->norm) - f->offset) < -FTINY) |
92 |
greg |
1.3 |
return(O_MISS); |
93 |
greg |
1.1 |
/* intersect face */ |
94 |
|
|
for (j = 0; j < 3; j++) |
95 |
|
|
v1[j] = (v1[j]*d2 - v2[j]*d1)/(d2 - d1); |
96 |
|
|
if (inface(v1, f)) |
97 |
greg |
1.3 |
return(O_HIT); |
98 |
greg |
1.1 |
|
99 |
greg |
1.3 |
return(O_MISS); /* no intersection */ |
100 |
greg |
1.1 |
} |