ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/sm_samp.c
Revision: 3.3
Committed: Tue Oct 6 18:16:54 1998 UTC (25 years, 6 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.2: +180 -179 lines
Log Message:
new triangulate routine
added smTestSample to check for occlusion
added frustum culling before rebuild
changed base quadtree to use octahedron and created new point locate
added "sample active" flags and implemented LRU replacement
started handling case of too many triangles
set sizes are now unbounded
changed all quadtree pointers to quadtrees

File Contents

# User Rev Content
1 gwlarson 3.1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ SGI";
5     #endif
6    
7     /*
8     * sm_samp.c
9     */
10     #include "standard.h"
11 gwlarson 3.3 #include "sm_flag.h"
12     #include "rhd_sample.h"
13 gwlarson 3.1
14 gwlarson 3.3 SAMP rsL;
15     int4 *samp_flag=NULL;
16 gwlarson 3.1
17    
18 gwlarson 3.3 /* Each sample has a world coord point, and direction, brightness,chrominance,
19     and RGB triples
20     */
21     #ifndef HP_VERSION
22     #define TMSIZE sizeof(TMbright)
23     #else
24     #define TMSIZE 0
25     #endif
26     #define SAMPSIZ (3*sizeof(float)+sizeof(int4)+ 6*sizeof(BYTE) + TMSIZE)
27 gwlarson 3.1
28 gwlarson 3.3 /* Extra points just need direction and world space point */
29     #define POINTSIZ (3*sizeof(float))
30    
31     /* Clear the flags for all samples */
32     sClear_all_flags(s)
33     SAMP *s;
34 gwlarson 3.1 {
35 gwlarson 3.3 if(samp_flag)
36     bzero((char *)samp_flag,FLAG_BYTES(S_MAX_SAMP(s)));
37     }
38 gwlarson 3.1
39 gwlarson 3.3 sInit(s)
40     SAMP *s;
41     {
42     S_REPLACE_SAMP(s) = 0;
43     S_NUM_SAMP(s) = 0;
44     S_TONE_MAP(s) = 0;
45     S_FREE_SAMP(s) = -1;
46     sClear_base_points(s);
47     sClear_all_flags(s);
48 gwlarson 3.1
49 gwlarson 3.3 }
50 gwlarson 3.1
51 gwlarson 3.3 sFree(s)
52     SAMP *s;
53     {
54     if(S_BASE(s))
55     {
56     free(S_BASE(s));
57     S_BASE(s)=NULL;
58     }
59     if(samp_flag)
60     {
61     free(samp_flag);
62     samp_flag = NULL;
63     }
64     }
65     /* Initialize/clear global smL sample list for at least n samples */
66     SAMP
67     *sAlloc(nptr,extra_points)
68     int *nptr,extra_points;
69     {
70     unsigned nbytes;
71     register unsigned i;
72     SAMP *s;
73     int n;
74    
75     s = &rsL;
76 gwlarson 3.1 /* round space up to nearest power of 2 */
77 gwlarson 3.3 nbytes = (*nptr)*SAMPSIZ + extra_points*POINTSIZ + 8;
78 gwlarson 3.1 for (i = 1024; nbytes > i; i <<= 1)
79     ;
80 gwlarson 3.3 /* get number of samples that fit in block */
81     n = (i - 8 - extra_points*POINTSIZ) / SAMPSIZ;
82 gwlarson 3.1
83 gwlarson 3.3 S_BASE(s) = (char *)malloc(n*SAMPSIZ + extra_points*POINTSIZ);
84     if (!S_BASE(s))
85 gwlarson 3.1 error(SYSTEM,"smInit_samples(): Unable to allocate memory");
86    
87     /* assign larger alignment types earlier */
88 gwlarson 3.3 S_W_PT(s) = (float (*)[3])S_BASE(s);
89     S_W_DIR(s) = (int4 *)(S_W_PT(s) + n + extra_points);
90     #ifndef HP_VERSION
91     S_BRT(s) = (TMbright *)(S_W_DIR(s) + n);
92     S_CHR(s) = (BYTE (*)[3])(S_BRT(s) + n);
93     #else
94     S_CHR(s) = (BYTE (*)[3])(S_W_DIR(s) + n);
95     #endif
96     S_RGB(s) = (BYTE (*)[3])(S_CHR(s) + n);
97     S_MAX_SAMP(s) = n;
98     S_MAX_BASE_PT(s) = n + extra_points;
99 gwlarson 3.1
100 gwlarson 3.3 /* Allocate memory for a per/sample bit flag */
101     if(!(samp_flag = (int4 *)malloc(FLAG_BYTES(n))))
102     error(SYSTEM,"smInit_samples(): Unable to allocate flag memory");
103 gwlarson 3.1
104 gwlarson 3.3 sInit(s);
105     *nptr = n;
106    
107     return(s);
108 gwlarson 3.1 }
109    
110    
111 gwlarson 3.3 /* Add a base point to the sample structure: only the world point
112     is added: These points are not displayed-they are used to form the
113     initial mesh
114     */
115 gwlarson 3.1 int
116 gwlarson 3.3 sAdd_base_point(s,v)
117     SAMP *s;
118     FVECT v;
119 gwlarson 3.1 {
120 gwlarson 3.3 int id;
121 gwlarson 3.1
122     /* Get pointer to next available point */
123 gwlarson 3.3 id = S_NEXT_BASE_PT(s);
124     if(id >= S_MAX_BASE_PT(s))
125 gwlarson 3.1 return(-1);
126    
127     /* Update free aux pointer */
128 gwlarson 3.3 S_NEXT_BASE_PT(s)++;
129 gwlarson 3.1
130     /* Copy vector into point */
131 gwlarson 3.3 VCOPY(S_NTH_W_PT(s,id),v);
132 gwlarson 3.1 return(id);
133     }
134    
135 gwlarson 3.3 /* Initialize the sample 'id' to contain world point 'p', direction 'd' and
136     color 'c'. If 'p' is NULL, a direction only is represented in 'd'. In
137     this case, the world point is set to the projection of the direction on
138     the view sphere, and the direction value is set to -1
139     */
140 gwlarson 3.1 void
141 gwlarson 3.3 sInit_samp(s,id,c,d,p)
142     SAMP *s;
143 gwlarson 3.1 int id;
144     COLR c;
145     FVECT d,p;
146     {
147 gwlarson 3.3 /* Copy vector into point */
148     VCOPY(S_NTH_W_PT(s,id),p);
149     if(d)
150     S_NTH_W_DIR(s,id) = encodedir(d);
151     /* direction only */
152     else
153     S_NTH_W_DIR(s,id) = -1;
154 gwlarson 3.1
155 gwlarson 3.3 /* calculate the brightness and chrominance */
156 gwlarson 3.1 #ifndef TEST_DRIVER
157 gwlarson 3.3 tmCvColrs(&S_NTH_BRT(s,id),S_NTH_CHR(s,id),c,1);
158 gwlarson 3.1 #else
159     if(c)
160     {
161 gwlarson 3.3 S_NTH_RGB(s,id)[0] = c[0];
162     S_NTH_RGB(s,id)[1] = c[1];
163     S_NTH_RGB(s,id)[2] = c[2];
164 gwlarson 3.1 }
165     else
166     {
167 gwlarson 3.3 S_NTH_RGB(s,id)[0] = 100;
168     S_NTH_RGB(s,id)[1] = 0;
169     S_NTH_RGB(s,id)[2] = 0;
170 gwlarson 3.1 }
171     #endif
172 gwlarson 3.3 /* Set ACTIVE bit upon creation */
173     S_SET_FLAG(id);
174    
175 gwlarson 3.1 }
176    
177    
178 gwlarson 3.3 /* Copy the values of sample n_id into sample id: Note must not call with
179     Base point n_id
180     */
181     int
182     sReset_samp(s,n_id,id)
183     SAMP *s;
184     int n_id,id;
185 gwlarson 3.1 {
186    
187 gwlarson 3.3 #ifdef DEBUG
188     if(id <0||id >= S_MAX_SAMP(s)||n_id <0||n_id >= S_MAX_SAMP(s))
189     {
190     eputs("smReset_sample():invalid sample id\n");
191     return(0);
192     }
193     #endif
194 gwlarson 3.1
195 gwlarson 3.3 /* Copy vector into point */
196     VCOPY(S_NTH_W_PT(s,n_id),S_NTH_W_PT(s,id));
197     S_NTH_W_DIR(s,n_id) = S_NTH_W_DIR(s,id);
198 gwlarson 3.1
199 gwlarson 3.3 #ifndef HP_VERSION
200     S_NTH_BRT(s,n_id) = S_NTH_BRT(s,id);
201     #endif
202     S_NTH_CHR(s,n_id)[0] = S_NTH_CHR(s,id)[0];
203     S_NTH_CHR(s,n_id)[1] = S_NTH_CHR(s,id)[1];
204     S_NTH_CHR(s,n_id)[2] = S_NTH_CHR(s,id)[2];
205     return(1);
206 gwlarson 3.1 }
207    
208 gwlarson 3.3 /* Allocate a new sample. If an existing sample was replaced: set flag */
209 gwlarson 3.1 int
210 gwlarson 3.3 sAlloc_samp(s,replaced)
211     SAMP *s;
212 gwlarson 3.2 int *replaced;
213 gwlarson 3.1
214     {
215     int id;
216    
217     if(replaced)
218 gwlarson 3.3 *replaced = 0;
219    
220     if((id = S_FREE_SAMP(s)) != INVALID)
221 gwlarson 3.1 {
222 gwlarson 3.3 S_FREE_SAMP(s) = INVALID;
223     return(id);
224 gwlarson 3.1 }
225 gwlarson 3.3 /* If havn't reached end of sample array:return next sample */
226     if(S_NUM_SAMP(s) < S_MAX_SAMP(s))
227     id = S_NUM_SAMP(s)++;
228 gwlarson 3.1 else
229 gwlarson 3.3 {
230     /* CLOCKED LRU replacement policy: Search through samples starting
231     with S_REPLACE_SAMP for a sample that does not have its active
232     bit set. Clear bit along the way
233     */
234     id = S_REPLACE_SAMP(s);
235     while(S_IS_FLAG(id))
236     {
237     S_CLR_FLAG(id);
238     id = (id +1)% S_MAX_SAMP(s);
239     }
240     S_REPLACE_SAMP(s) = id = (id +1)% S_MAX_SAMP(s);
241     if(replaced)
242     *replaced = 1;
243     }
244 gwlarson 3.1 return(id);
245     }
246    
247 gwlarson 3.3 /* Set the sample flag to be unused- so will get replaced on next LRU
248     iteration
249     */
250 gwlarson 3.1
251 gwlarson 3.3 /* NOTE: Do we want a free list as well? */
252 gwlarson 3.1 int
253 gwlarson 3.3 sDelete_samp(s,id)
254     SAMP *s;
255 gwlarson 3.1 int id;
256     {
257 gwlarson 3.3 #ifdef DEBUG
258     /* Fist check for a valid id */
259     if(id >= S_MAX_SAMP(s) || id < 0)
260     return(0);
261     #endif
262 gwlarson 3.1
263 gwlarson 3.3 S_CLR_FLAG(id);
264     return(1);
265 gwlarson 3.1 }
266    
267    
268    
269    
270