ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/sm_samp.c
Revision: 3.4
Committed: Wed Nov 11 12:05:40 1998 UTC (25 years, 5 months ago) by gwlarson
Content type: text/plain
Branch: MAIN
Changes since 3.3: +63 -47 lines
Log Message:
new triangulation code
changed triangle vertex order to CCW
changed numbering of triangle neighbors to match quadtree
fixed tone-mapping bug
removed errant printf() statements
redid logic for adding and testing samples with new epsilon

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 gwlarson 3.1
48 gwlarson 3.3 }
49 gwlarson 3.1
50 gwlarson 3.3 sFree(s)
51     SAMP *s;
52     {
53     if(S_BASE(s))
54     {
55     free(S_BASE(s));
56     S_BASE(s)=NULL;
57     }
58     if(samp_flag)
59     {
60     free(samp_flag);
61     samp_flag = NULL;
62     }
63     }
64     /* Initialize/clear global smL sample list for at least n samples */
65     SAMP
66     *sAlloc(nptr,extra_points)
67     int *nptr,extra_points;
68     {
69     unsigned nbytes;
70     register unsigned i;
71     SAMP *s;
72     int n;
73    
74     s = &rsL;
75 gwlarson 3.1 /* round space up to nearest power of 2 */
76 gwlarson 3.3 nbytes = (*nptr)*SAMPSIZ + extra_points*POINTSIZ + 8;
77 gwlarson 3.1 for (i = 1024; nbytes > i; i <<= 1)
78     ;
79 gwlarson 3.3 /* get number of samples that fit in block */
80     n = (i - 8 - extra_points*POINTSIZ) / SAMPSIZ;
81 gwlarson 3.1
82 gwlarson 3.3 S_BASE(s) = (char *)malloc(n*SAMPSIZ + extra_points*POINTSIZ);
83     if (!S_BASE(s))
84 gwlarson 3.4 error(SYSTEM,"sAlloc(): Unable to allocate memory");
85 gwlarson 3.1
86     /* assign larger alignment types earlier */
87 gwlarson 3.3 S_W_PT(s) = (float (*)[3])S_BASE(s);
88     S_W_DIR(s) = (int4 *)(S_W_PT(s) + n + extra_points);
89 gwlarson 3.4 S_BRT(s) = (TMbright *)(S_W_DIR(s) + n);
90 gwlarson 3.3 S_CHR(s) = (BYTE (*)[3])(S_BRT(s) + n);
91     S_RGB(s) = (BYTE (*)[3])(S_CHR(s) + n);
92     S_MAX_SAMP(s) = n;
93     S_MAX_BASE_PT(s) = n + extra_points;
94 gwlarson 3.1
95 gwlarson 3.3 /* Allocate memory for a per/sample bit flag */
96     if(!(samp_flag = (int4 *)malloc(FLAG_BYTES(n))))
97 gwlarson 3.4 error(SYSTEM,"sAlloc(): Unable to allocate flag memory");
98 gwlarson 3.1
99 gwlarson 3.3 sInit(s);
100 gwlarson 3.4 sClear_all_flags(s);
101    
102 gwlarson 3.3 *nptr = n;
103    
104     return(s);
105 gwlarson 3.1 }
106    
107    
108 gwlarson 3.3 /* Add a base point to the sample structure: only the world point
109     is added: These points are not displayed-they are used to form the
110     initial mesh
111     */
112 gwlarson 3.1 int
113 gwlarson 3.3 sAdd_base_point(s,v)
114     SAMP *s;
115     FVECT v;
116 gwlarson 3.1 {
117 gwlarson 3.3 int id;
118 gwlarson 3.1
119     /* Get pointer to next available point */
120 gwlarson 3.3 id = S_NEXT_BASE_PT(s);
121     if(id >= S_MAX_BASE_PT(s))
122 gwlarson 3.1 return(-1);
123    
124     /* Update free aux pointer */
125 gwlarson 3.3 S_NEXT_BASE_PT(s)++;
126 gwlarson 3.1
127     /* Copy vector into point */
128 gwlarson 3.3 VCOPY(S_NTH_W_PT(s,id),v);
129 gwlarson 3.1 return(id);
130     }
131    
132 gwlarson 3.4 /* Copy the values of sample n_id into sample id: Note must not call with
133     Base point n_id
134     */
135     int
136     sCopy_samp(s,n_id,id)
137     SAMP *s;
138     int n_id,id;
139     {
140    
141     #ifdef DEBUG
142     if(id <0||id >= S_MAX_SAMP(s)||n_id <0||n_id >= S_MAX_SAMP(s))
143     {
144     eputs("smReset_sample():invalid sample id\n");
145     return(0);
146     }
147     #endif
148     if(n_id == id)
149     return(1);
150     /* Copy vector into point */
151     VCOPY(S_NTH_W_PT(s,n_id),S_NTH_W_PT(s,id));
152     S_NTH_W_DIR(s,n_id) = S_NTH_W_DIR(s,id);
153    
154     S_NTH_BRT(s,n_id) = S_NTH_BRT(s,id);
155     S_NTH_CHR(s,n_id)[0] = S_NTH_CHR(s,id)[0];
156     S_NTH_CHR(s,n_id)[1] = S_NTH_CHR(s,id)[1];
157     S_NTH_CHR(s,n_id)[2] = S_NTH_CHR(s,id)[2];
158    
159     return(1);
160     }
161    
162    
163 gwlarson 3.3 /* Initialize the sample 'id' to contain world point 'p', direction 'd' and
164     color 'c'. If 'p' is NULL, a direction only is represented in 'd'. In
165     this case, the world point is set to the projection of the direction on
166     the view sphere, and the direction value is set to -1
167     */
168 gwlarson 3.1 void
169 gwlarson 3.4 sInit_samp(s,id,c,d,p,o_id,tonemap)
170 gwlarson 3.3 SAMP *s;
171 gwlarson 3.1 int id;
172     COLR c;
173     FVECT d,p;
174 gwlarson 3.4 int o_id,tonemap;
175 gwlarson 3.1 {
176 gwlarson 3.4
177     if(o_id != INVALID)
178     sCopy_samp(s,id,o_id);
179     else
180     {
181 gwlarson 3.3 /* Copy vector into point */
182     VCOPY(S_NTH_W_PT(s,id),p);
183     if(d)
184     S_NTH_W_DIR(s,id) = encodedir(d);
185     else
186     S_NTH_W_DIR(s,id) = -1;
187 gwlarson 3.1
188 gwlarson 3.4 /* calculate the brightness and chrominance,RGB will be set by
189     tonemapping
190     */
191 gwlarson 3.1 #ifndef TEST_DRIVER
192 gwlarson 3.3 tmCvColrs(&S_NTH_BRT(s,id),S_NTH_CHR(s,id),c,1);
193 gwlarson 3.1 #else
194     if(c)
195     {
196 gwlarson 3.3 S_NTH_RGB(s,id)[0] = c[0];
197     S_NTH_RGB(s,id)[1] = c[1];
198     S_NTH_RGB(s,id)[2] = c[2];
199 gwlarson 3.1 }
200     else
201     {
202 gwlarson 3.3 S_NTH_RGB(s,id)[0] = 100;
203     S_NTH_RGB(s,id)[1] = 0;
204     S_NTH_RGB(s,id)[2] = 0;
205 gwlarson 3.1 }
206     #endif
207 gwlarson 3.4 }
208 gwlarson 3.3 /* Set ACTIVE bit upon creation */
209     S_SET_FLAG(id);
210 gwlarson 3.4 #ifndef TEST_DRIVER
211     if(tonemap)
212     tmMapPixels(S_NTH_RGB(s,id),&S_NTH_BRT(s,id),S_NTH_CHR(s,id),1);
213     #endif
214 gwlarson 3.1 }
215    
216    
217 gwlarson 3.3 /* Allocate a new sample. If an existing sample was replaced: set flag */
218 gwlarson 3.1 int
219 gwlarson 3.3 sAlloc_samp(s,replaced)
220     SAMP *s;
221 gwlarson 3.2 int *replaced;
222 gwlarson 3.1
223     {
224     int id;
225    
226     if(replaced)
227 gwlarson 3.3 *replaced = 0;
228    
229 gwlarson 3.4 /* First check if there are any freed sample available */
230 gwlarson 3.3 if((id = S_FREE_SAMP(s)) != INVALID)
231 gwlarson 3.1 {
232 gwlarson 3.4 S_FREE_SAMP(s) = S_NTH_NEXT(s,id);
233 gwlarson 3.3 return(id);
234 gwlarson 3.1 }
235 gwlarson 3.3 /* If havn't reached end of sample array:return next sample */
236     if(S_NUM_SAMP(s) < S_MAX_SAMP(s))
237     id = S_NUM_SAMP(s)++;
238 gwlarson 3.1 else
239 gwlarson 3.3 {
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 gwlarson 3.4 S_REPLACE_SAMP(s) = (id +1)% S_MAX_SAMP(s);
251 gwlarson 3.3 if(replaced)
252     *replaced = 1;
253     }
254 gwlarson 3.1 return(id);
255     }
256    
257 gwlarson 3.3 /* Set the sample flag to be unused- so will get replaced on next LRU
258     iteration
259     */
260 gwlarson 3.1 int
261 gwlarson 3.3 sDelete_samp(s,id)
262     SAMP *s;
263 gwlarson 3.1 int id;
264     {
265 gwlarson 3.3 #ifdef DEBUG
266     /* Fist check for a valid id */
267     if(id >= S_MAX_SAMP(s) || id < 0)
268     return(0);
269     #endif
270 gwlarson 3.4 /* Add sample to the free list */
271     sUnalloc_samp(s,id);
272    
273 gwlarson 3.3 S_CLR_FLAG(id);
274     return(1);
275 gwlarson 3.1 }
276 gwlarson 3.4
277    
278    
279    
280    
281    
282 gwlarson 3.1
283    
284    
285    
286