ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/sm_samp.c
Revision: 3.6
Committed: Thu Jun 10 15:22:24 1999 UTC (24 years, 10 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.5: +40 -62 lines
Log Message:
Implemented sample quadtree in place of triangle quadtree
Made geometric predicates more robust
Added #define LORES which utilizes a single precision floating point
  sample array, the default is a double sample array
Added topology DEBUG commands (for DEBUG > 1)
Made code optimizations

File Contents

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