| 1 |
/* Copyright (c) 1986 Regents of the University of California */
|
| 2 |
|
| 3 |
#ifndef lint
|
| 4 |
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
#endif
|
| 6 |
|
| 7 |
/*
|
| 8 |
* octree.c - routines dealing with octrees and cubes.
|
| 9 |
*
|
| 10 |
* 7/28/85
|
| 11 |
*/
|
| 12 |
|
| 13 |
#include "standard.h"
|
| 14 |
|
| 15 |
#include "octree.h"
|
| 16 |
|
| 17 |
OCTREE *octblock[MAXOBLK]; /* our octree */
|
| 18 |
static OCTREE ofreelist = EMPTY; /* freed octree nodes */
|
| 19 |
static OCTREE treetop = 0; /* next free node */
|
| 20 |
|
| 21 |
|
| 22 |
OCTREE
|
| 23 |
octalloc() /* allocate an octree */
|
| 24 |
{
|
| 25 |
register OCTREE freet;
|
| 26 |
|
| 27 |
if ((freet = ofreelist) != EMPTY) {
|
| 28 |
ofreelist = octkid(freet, 0);
|
| 29 |
return(freet);
|
| 30 |
}
|
| 31 |
freet = treetop;
|
| 32 |
if (octti(freet) == 0) {
|
| 33 |
errno = 0;
|
| 34 |
if (octbi(freet) >= MAXOBLK)
|
| 35 |
return(EMPTY);
|
| 36 |
if ((octblock[octbi(freet)] = (OCTREE *)bmalloc(
|
| 37 |
(unsigned)256*8*sizeof(OCTREE))) == NULL)
|
| 38 |
return(EMPTY);
|
| 39 |
}
|
| 40 |
treetop += 8;
|
| 41 |
return(freet);
|
| 42 |
}
|
| 43 |
|
| 44 |
|
| 45 |
octfree(ot) /* free an octree */
|
| 46 |
register OCTREE ot;
|
| 47 |
{
|
| 48 |
register int i;
|
| 49 |
|
| 50 |
if (!istree(ot))
|
| 51 |
return;
|
| 52 |
for (i = 0; i < 8; i++)
|
| 53 |
octfree(octkid(ot, i));
|
| 54 |
octkid(ot, 0) = ofreelist;
|
| 55 |
ofreelist = ot;
|
| 56 |
}
|
| 57 |
|
| 58 |
|
| 59 |
octdone() /* free EVERYTHING */
|
| 60 |
{
|
| 61 |
register int i;
|
| 62 |
|
| 63 |
for (i = 0; i < MAXOBLK; i++) {
|
| 64 |
bfree((char *)octblock[i], (unsigned)256*8*sizeof(OCTREE));
|
| 65 |
octblock[i] = NULL;
|
| 66 |
}
|
| 67 |
ofreelist = EMPTY;
|
| 68 |
treetop = 0;
|
| 69 |
}
|
| 70 |
|
| 71 |
|
| 72 |
OCTREE
|
| 73 |
combine(ot) /* recursively combine nodes */
|
| 74 |
register OCTREE ot;
|
| 75 |
{
|
| 76 |
register int i;
|
| 77 |
register OCTREE ores;
|
| 78 |
|
| 79 |
if (!istree(ot)) /* not a tree */
|
| 80 |
return(ot);
|
| 81 |
ores = octkid(ot, 0) = combine(octkid(ot, 0));
|
| 82 |
for (i = 1; i < 8; i++)
|
| 83 |
if ((octkid(ot, i) = combine(octkid(ot, i))) != ores)
|
| 84 |
ores = ot;
|
| 85 |
if (!istree(ores)) { /* all were identical leaves */
|
| 86 |
octkid(ot, 0) = ofreelist;
|
| 87 |
ofreelist = ot;
|
| 88 |
}
|
| 89 |
return(ores);
|
| 90 |
}
|
| 91 |
|
| 92 |
|
| 93 |
culocate(cu, pt) /* locate point within cube */
|
| 94 |
register CUBE *cu;
|
| 95 |
register FVECT pt;
|
| 96 |
{
|
| 97 |
register int i;
|
| 98 |
int branch;
|
| 99 |
|
| 100 |
while (istree(cu->cutree)) {
|
| 101 |
cu->cusize *= 0.5;
|
| 102 |
branch = 0;
|
| 103 |
for (i = 0; i < 3; i++)
|
| 104 |
if (cu->cuorg[i] + cu->cusize <= pt[i]) {
|
| 105 |
cu->cuorg[i] += cu->cusize;
|
| 106 |
branch |= 1 << i;
|
| 107 |
}
|
| 108 |
cu->cutree = octkid(cu->cutree, branch);
|
| 109 |
}
|
| 110 |
}
|
| 111 |
|
| 112 |
|
| 113 |
cucopy(cu1, cu2) /* copy cu2 into cu1 */
|
| 114 |
register CUBE *cu1, *cu2;
|
| 115 |
{
|
| 116 |
cu1->cutree = cu2->cutree;
|
| 117 |
cu1->cusize = cu2->cusize;
|
| 118 |
VCOPY(cu1->cuorg, cu2->cuorg);
|
| 119 |
}
|
| 120 |
|
| 121 |
|
| 122 |
incube(cu, pt) /* determine if a point is inside a cube */
|
| 123 |
register CUBE *cu;
|
| 124 |
register FVECT pt;
|
| 125 |
{
|
| 126 |
if (cu->cuorg[0] > pt[0] || pt[0] >= cu->cuorg[0] + cu->cusize)
|
| 127 |
return(0);
|
| 128 |
if (cu->cuorg[1] > pt[1] || pt[1] >= cu->cuorg[1] + cu->cusize)
|
| 129 |
return(0);
|
| 130 |
if (cu->cuorg[2] > pt[2] || pt[2] >= cu->cuorg[2] + cu->cusize)
|
| 131 |
return(0);
|
| 132 |
return(1);
|
| 133 |
}
|