8 |
|
* sm_samp.c |
9 |
|
*/ |
10 |
|
#include "standard.h" |
11 |
+ |
#include "sm_flag.h" |
12 |
+ |
#include "rhd_sample.h" |
13 |
|
|
14 |
< |
#include "sm.h" |
14 |
> |
SAMP rsL; |
15 |
> |
int4 *samp_flag=NULL; |
16 |
|
|
14 |
– |
RSAMP rsL; |
17 |
|
|
18 |
+ |
/* 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 |
|
|
28 |
< |
#define SAMPSIZ (3*sizeof(float)+sizeof(int4)+\ |
29 |
< |
sizeof(TMbright)+6*sizeof(BYTE)) |
30 |
< |
/* Initialize/clear global smL sample list for at least n samples */ |
31 |
< |
int |
32 |
< |
smAlloc_samples(sm,n,extra_points) |
33 |
< |
SM *sm; |
23 |
< |
register int n,extra_points; |
28 |
> |
/* 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 |
|
{ |
35 |
< |
unsigned nbytes; |
36 |
< |
register unsigned i; |
37 |
< |
RSAMP *rs; |
35 |
> |
if(samp_flag) |
36 |
> |
bzero((char *)samp_flag,FLAG_BYTES(S_MAX_BASE_PT(s))); |
37 |
> |
} |
38 |
|
|
39 |
< |
rs = &rsL; |
39 |
> |
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 |
|
|
48 |
< |
SM_SAMP(sm) = rs; |
48 |
> |
} |
49 |
|
|
50 |
+ |
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 |
|
/* round space up to nearest power of 2 */ |
76 |
< |
nbytes = n*SAMPSIZ + extra_points*SM_POINTSIZ + 8; |
76 |
> |
nbytes = (*nptr)*SAMPSIZ + extra_points*POINTSIZ + 8; |
77 |
|
for (i = 1024; nbytes > i; i <<= 1) |
78 |
|
; |
79 |
< |
n = (i - 8 - extra_points*SM_POINTSIZ) / SAMPSIZ; |
79 |
> |
/* get number of samples that fit in block */ |
80 |
> |
n = (i - 8 - extra_points*POINTSIZ) / SAMPSIZ; |
81 |
|
|
82 |
< |
RS_BASE(rs) = (char *)malloc(n*SAMPSIZ + extra_points*SM_POINTSIZ); |
83 |
< |
if (!RS_BASE(rs)) |
84 |
< |
error(SYSTEM,"smInit_samples(): Unable to allocate memory"); |
82 |
> |
S_BASE(s) = (char *)malloc(n*SAMPSIZ + extra_points*POINTSIZ); |
83 |
> |
if (!S_BASE(s)) |
84 |
> |
error(SYSTEM,"sAlloc(): Unable to allocate memory"); |
85 |
|
|
86 |
|
/* assign larger alignment types earlier */ |
87 |
< |
RS_W_PT(rs) = (float (*)[3])RS_BASE(rs); |
88 |
< |
RS_W_DIR(rs) = (int4 *)(RS_W_PT(rs) + n + extra_points); |
89 |
< |
RS_BRT(rs) = (TMbright *)(RS_W_DIR(rs) + n + extra_points); |
90 |
< |
RS_CHR(rs) = (BYTE (*)[3])(RS_BRT(rs) + n); |
91 |
< |
RS_RGB(rs) = (BYTE (*)[3])(RS_CHR(rs) + n); |
92 |
< |
RS_MAX_SAMP(rs) = n; |
93 |
< |
RS_MAX_AUX_PT(rs) = n + extra_points; |
87 |
> |
S_W_PT(s) = (float (*)[3])S_BASE(s); |
88 |
> |
S_W_DIR(s) = (int4 *)(S_W_PT(s) + n + extra_points); |
89 |
> |
S_BRT(s) = (TMbright *)(S_W_DIR(s) + n); |
90 |
> |
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 |
|
|
95 |
< |
smClear_samples(sm); |
95 |
> |
/* Allocate memory for a per/sample bit flag */ |
96 |
> |
if(!(samp_flag = (int4 *)malloc(FLAG_BYTES(n+extra_points)))) |
97 |
> |
error(SYSTEM,"sAlloc(): Unable to allocate flag memory"); |
98 |
|
|
99 |
< |
return(n); |
100 |
< |
} |
99 |
> |
sInit(s); |
100 |
> |
sClear_all_flags(s); |
101 |
|
|
102 |
< |
smClear_aux_samples(sm) |
103 |
< |
SM *sm; |
104 |
< |
{ |
60 |
< |
RS_NEXT_AUX_PT(SM_SAMP(sm)) = RS_MAX_SAMP(SM_SAMP(sm)); |
102 |
> |
*nptr = n; |
103 |
> |
|
104 |
> |
return(s); |
105 |
|
} |
106 |
|
|
63 |
– |
int |
64 |
– |
smClear_samples(sm) |
65 |
– |
SM *sm; |
66 |
– |
{ |
67 |
– |
RSAMP *samp; |
107 |
|
|
108 |
< |
samp = SM_SAMP(sm); |
109 |
< |
RS_FREE_SAMP(samp) = -1; |
110 |
< |
RS_REPLACE_SAMP(samp) = 0; |
111 |
< |
RS_NUM_SAMP(samp) = 0; |
73 |
< |
RS_TONE_MAP(samp) = 0; |
74 |
< |
smClear_aux_samples(sm); |
75 |
< |
return(1); |
76 |
< |
} |
77 |
< |
|
108 |
> |
/* 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 |
|
int |
113 |
< |
smAdd_aux_point(sm,v,d) |
114 |
< |
SM *sm; |
115 |
< |
FVECT v,d; |
113 |
> |
sAdd_base_point(s,v) |
114 |
> |
SAMP *s; |
115 |
> |
FVECT v; |
116 |
|
{ |
83 |
– |
RSAMP *samp; |
117 |
|
int id; |
118 |
< |
|
86 |
< |
/* Get the SM sample array */ |
87 |
< |
samp = SM_SAMP(sm); |
118 |
> |
|
119 |
|
/* Get pointer to next available point */ |
120 |
< |
id = RS_NEXT_AUX_PT(samp); |
121 |
< |
if(id >= RS_MAX_AUX_PT(samp)) |
120 |
> |
id = S_NEXT_BASE_PT(s); |
121 |
> |
if(id >= S_MAX_BASE_PT(s)) |
122 |
|
return(-1); |
123 |
|
|
124 |
|
/* Update free aux pointer */ |
125 |
< |
RS_NEXT_AUX_PT(samp)++; |
125 |
> |
S_NEXT_BASE_PT(s)++; |
126 |
|
|
127 |
|
/* Copy vector into point */ |
128 |
< |
VCOPY(RS_NTH_W_PT(samp,id),v); |
98 |
< |
RS_NTH_W_DIR(samp,id) = encodedir(d); |
128 |
> |
VCOPY(S_NTH_W_PT(s,id),v); |
129 |
|
return(id); |
130 |
|
} |
131 |
|
|
132 |
+ |
/* 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 |
+ |
/* 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 |
|
void |
169 |
< |
smInit_sample(sm,id,c,d,p) |
170 |
< |
SM *sm; |
169 |
> |
sInit_samp(s,id,c,d,p,o_id) |
170 |
> |
SAMP *s; |
171 |
|
int id; |
172 |
|
COLR c; |
173 |
|
FVECT d,p; |
174 |
+ |
int o_id; |
175 |
|
{ |
109 |
– |
RSAMP *samp; |
176 |
|
|
177 |
< |
samp = SM_SAMP(sm); |
178 |
< |
if(p) |
179 |
< |
{ |
180 |
< |
/* Copy vector into point */ |
181 |
< |
VCOPY(RS_NTH_W_PT(samp,id),p); |
182 |
< |
RS_NTH_W_DIR(samp,id) = encodedir(d); |
183 |
< |
} |
177 |
> |
if(o_id != INVALID) |
178 |
> |
sCopy_samp(s,id,o_id); |
179 |
> |
else |
180 |
> |
{ |
181 |
> |
/* 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 |
< |
{ |
187 |
< |
VADD(RS_NTH_W_PT(samp,id),d,SM_VIEW_CENTER(sm)); |
188 |
< |
RS_NTH_W_DIR(samp,id) = -1; |
189 |
< |
} |
190 |
< |
|
186 |
> |
S_NTH_W_DIR(s,id) = -1; |
187 |
> |
|
188 |
> |
/* calculate the brightness and chrominance,RGB will be set by |
189 |
> |
tonemapping |
190 |
> |
*/ |
191 |
|
#ifndef TEST_DRIVER |
192 |
< |
tmCvColrs(&RS_NTH_BRT(samp,id),RS_NTH_CHR(samp,id),c,1); |
192 |
> |
tmCvColrs(&S_NTH_BRT(s,id),S_NTH_CHR(s,id),c,1); |
193 |
|
#else |
194 |
|
if(c) |
195 |
|
{ |
196 |
< |
RS_NTH_RGB(samp,id)[0] = c[0]; |
197 |
< |
RS_NTH_RGB(samp,id)[1] = c[1]; |
198 |
< |
RS_NTH_RGB(samp,id)[2] = c[2]; |
196 |
> |
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 |
|
} |
200 |
|
else |
201 |
|
{ |
202 |
< |
RS_NTH_RGB(samp,id)[0] = 100; |
203 |
< |
RS_NTH_RGB(samp,id)[1] = 0; |
204 |
< |
RS_NTH_RGB(samp,id)[2] = 0; |
202 |
> |
S_NTH_RGB(s,id)[0] = 100; |
203 |
> |
S_NTH_RGB(s,id)[1] = 0; |
204 |
> |
S_NTH_RGB(s,id)[2] = 0; |
205 |
|
} |
206 |
|
#endif |
207 |
+ |
} |
208 |
+ |
/* Set ACTIVE bit upon creation */ |
209 |
+ |
S_SET_FLAG(id); |
210 |
+ |
#ifndef TEST_DRIVER |
211 |
+ |
if(id < S_TONE_MAP(s)) |
212 |
+ |
tmMapPixels(S_NTH_RGB(s,id),&S_NTH_BRT(s,id),S_NTH_CHR(s,id),1); |
213 |
+ |
#endif |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
< |
void |
144 |
< |
smReset_sample(sm,id,n_id) |
145 |
< |
SM *sm; |
146 |
< |
int id,n_id; |
147 |
< |
{ |
148 |
< |
RSAMP *samp; |
149 |
< |
|
150 |
< |
samp = SM_SAMP(sm); |
151 |
< |
|
152 |
< |
/* Copy vector into point */ |
153 |
< |
VCOPY(RS_NTH_W_PT(samp,id),RS_NTH_W_PT(samp,n_id)); |
154 |
< |
RS_NTH_W_DIR(samp,id) = RS_NTH_W_DIR(samp,n_id); |
155 |
< |
|
156 |
< |
RS_NTH_BRT(samp,id) = RS_NTH_BRT(samp,n_id); |
157 |
< |
RS_NTH_CHR(samp,id)[0] = RS_NTH_CHR(samp,n_id)[0]; |
158 |
< |
RS_NTH_CHR(samp,id)[1] = RS_NTH_CHR(samp,n_id)[1]; |
159 |
< |
RS_NTH_CHR(samp,id)[2] = RS_NTH_CHR(samp,n_id)[2]; |
160 |
< |
RS_NTH_RGB(samp,id)[0] = RS_NTH_RGB(samp,n_id)[0]; |
161 |
< |
RS_NTH_RGB(samp,id)[1] = RS_NTH_RGB(samp,n_id)[1]; |
162 |
< |
RS_NTH_RGB(samp,id)[2] = RS_NTH_RGB(samp,n_id)[2]; |
163 |
< |
|
164 |
< |
} |
165 |
< |
|
217 |
> |
/* Allocate a new sample. If an existing sample was replaced: set flag */ |
218 |
|
int |
219 |
< |
rsAlloc_samp(samp,replaced) |
220 |
< |
RSAMP *samp; |
221 |
< |
char *replaced; |
219 |
> |
sAlloc_samp(s,replaced) |
220 |
> |
SAMP *s; |
221 |
> |
int *replaced; |
222 |
|
|
223 |
|
{ |
224 |
|
int id; |
225 |
|
|
226 |
|
if(replaced) |
227 |
< |
*replaced = FALSE; |
228 |
< |
if(RS_NUM_SAMP(samp) < RS_MAX_SAMP(samp)) |
227 |
> |
*replaced = 0; |
228 |
> |
|
229 |
> |
/* First check if there are any freed sample available */ |
230 |
> |
if((id = S_FREE_SAMP(s)) != INVALID) |
231 |
|
{ |
232 |
< |
id = RS_NUM_SAMP(samp); |
233 |
< |
RS_NUM_SAMP(samp)++; |
180 |
< |
|
232 |
> |
S_FREE_SAMP(s) = S_NTH_NEXT(s,id); |
233 |
> |
return(id); |
234 |
|
} |
235 |
+ |
/* 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 |
|
else |
239 |
< |
if(RS_REPLACE_SAMP(samp) != -1) |
240 |
< |
{ |
241 |
< |
id = RS_REPLACE_SAMP(samp); |
242 |
< |
/* NOTE: NEED to do LRU later*/ |
243 |
< |
RS_REPLACE_SAMP(samp) = (id + 1)% RS_MAX_SAMP(samp); |
244 |
< |
if(replaced) |
245 |
< |
*replaced = TRUE; |
246 |
< |
} |
247 |
< |
else |
248 |
< |
if(RS_FREE_SAMP(samp) != -1) |
249 |
< |
{ |
250 |
< |
id = RS_FREE_SAMP(samp); |
251 |
< |
RS_FREE_SAMP(samp) = RS_NTH_W_DIR(samp,id); |
252 |
< |
} |
253 |
< |
else |
198 |
< |
{ |
199 |
< |
#ifdef DEBUG |
200 |
< |
eputs("rsAlloc_samp(): no samples available"); |
201 |
< |
#endif |
202 |
< |
id = -1; |
203 |
< |
} |
204 |
< |
|
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 |
> |
if(replaced) |
252 |
> |
*replaced = 1; |
253 |
> |
} |
254 |
|
return(id); |
255 |
|
} |
256 |
|
|
257 |
+ |
/* Set the sample flag to be unused- so will get replaced on next LRU |
258 |
+ |
iteration |
259 |
+ |
*/ |
260 |
|
int |
261 |
< |
rsDelete_sample(samp,id) |
262 |
< |
RSAMP *samp; |
261 |
> |
sDelete_samp(s,id) |
262 |
> |
SAMP *s; |
263 |
|
int id; |
264 |
|
{ |
265 |
< |
/* First check for a valid id */ |
266 |
< |
if(id >= RS_MAX_SAMP(samp) || id < 0) |
267 |
< |
return(FALSE); |
268 |
< |
|
269 |
< |
RS_NTH_W_DIR(samp,id) = RS_FREE_SAMP(samp); |
270 |
< |
RS_FREE_SAMP(samp) = id; |
265 |
> |
#ifdef DEBUG |
266 |
> |
/* Fist check for a valid id */ |
267 |
> |
if(id >= S_MAX_SAMP(s) || id < 0) |
268 |
> |
return(0); |
269 |
> |
#endif |
270 |
> |
/* Add sample to the free list */ |
271 |
> |
sUnalloc_samp(s,id); |
272 |
|
|
273 |
< |
return(TRUE); |
273 |
> |
S_CLR_FLAG(id); |
274 |
> |
return(1); |
275 |
|
} |
276 |
|
|
223 |
– |
int |
224 |
– |
smDelete_sample(sm,id) |
225 |
– |
SM *sm; |
226 |
– |
int id; |
227 |
– |
{ |
228 |
– |
RSAMP *samp; |
277 |
|
|
230 |
– |
samp = SM_SAMP(sm); |
231 |
– |
return(rsDelete_sample(samp,id)); |
232 |
– |
} |
233 |
– |
|
234 |
– |
/* NEEDS: to add free list- and bit for clocked LRU */ |
235 |
– |
int |
236 |
– |
smAdd_sample_point(sm,c,dir,p) |
237 |
– |
SM *sm; |
238 |
– |
COLR c; |
239 |
– |
FVECT dir; |
240 |
– |
FVECT p; |
241 |
– |
|
242 |
– |
{ |
243 |
– |
RSAMP *samp; |
244 |
– |
int id; |
245 |
– |
char replaced; |
246 |
– |
|
247 |
– |
/* Get the SM sample array */ |
248 |
– |
samp = SM_SAMP(sm); |
249 |
– |
/* Get pointer to next available point */ |
250 |
– |
id = rsAlloc_samp(samp,&replaced); |
251 |
– |
if(id < 0) |
252 |
– |
return(-1); |
253 |
– |
|
254 |
– |
if(replaced) |
255 |
– |
smMesh_remove_vertex(sm,id); |
256 |
– |
|
257 |
– |
smInit_sample(sm,id,c,dir,p); |
258 |
– |
|
259 |
– |
return(id); |
260 |
– |
} |
278 |
|
|
279 |
|
|
280 |
|
|