ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/sm_samp.c
Revision: 3.7
Committed: Sat Feb 22 02:07:25 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 3.6: +18 -13 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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