ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/octree.c
Revision: 1.1
Committed: Thu Feb 2 10:34:37 1989 UTC (35 years, 3 months ago) by greg
Content type: text/plain
Branch: MAIN
Log Message:
Initial revision

File Contents

# Content
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; /* free 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 *)malloc(
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 OCTREE
60 combine(ot) /* recursively combine nodes */
61 register OCTREE ot;
62 {
63 register int i;
64 register OCTREE ores;
65
66 if (!istree(ot)) /* not a tree */
67 return(ot);
68 ores = octkid(ot, 0) = combine(octkid(ot, 0));
69 for (i = 1; i < 8; i++)
70 if ((octkid(ot, i) = combine(octkid(ot, i))) != ores)
71 ores = ot;
72 if (!istree(ores)) /* all were identical leaves */
73 octfree(ot);
74 return(ores);
75 }
76
77
78 culocate(cu, pt) /* locate point within cube */
79 register CUBE *cu;
80 register FVECT pt;
81 {
82 register int i;
83 int branch;
84
85 while (istree(cu->cutree)) {
86 cu->cusize *= 0.5;
87 branch = 0;
88 for (i = 0; i < 3; i++)
89 if (cu->cuorg[i] + cu->cusize <= pt[i]) {
90 cu->cuorg[i] += cu->cusize;
91 branch |= 1 << i;
92 }
93 cu->cutree = octkid(cu->cutree, branch);
94 }
95 }
96
97
98 cucopy(cu1, cu2) /* copy cu2 into cu1 */
99 register CUBE *cu1, *cu2;
100 {
101 cu1->cutree = cu2->cutree;
102 cu1->cusize = cu2->cusize;
103 VCOPY(cu1->cuorg, cu2->cuorg);
104 }
105
106
107 incube(cu, pt) /* determine if a point is inside a cube */
108 register CUBE *cu;
109 register FVECT pt;
110 {
111 if (cu->cuorg[0] > pt[0] || pt[0] >= cu->cuorg[0] + cu->cusize)
112 return(0);
113 if (cu->cuorg[1] > pt[1] || pt[1] >= cu->cuorg[1] + cu->cusize)
114 return(0);
115 if (cu->cuorg[2] > pt[2] || pt[2] >= cu->cuorg[2] + cu->cusize)
116 return(0);
117 return(1);
118 }