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 |
|
|
17 |
< |
RSAMP rsL; |
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 |
< |
#define SAMPSIZ (3*sizeof(float)+sizeof(int4)+\ |
25 |
< |
sizeof(TMbright)+6*sizeof(BYTE)) |
26 |
< |
/* Initialize/clear global smL sample list for at least n samples */ |
27 |
< |
int |
28 |
< |
smAlloc_samples(sm,n,extra_points) |
29 |
< |
SM *sm; |
23 |
< |
register int n,extra_points; |
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 |
< |
unsigned nbytes; |
32 |
< |
register unsigned i; |
33 |
< |
RSAMP *rs; |
31 |
> |
if(samp_flag) |
32 |
> |
bzero((char *)samp_flag,FLAG_BYTES(S_MAX_BASE_PT(s))); |
33 |
> |
} |
34 |
|
|
35 |
< |
rs = &rsL; |
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 |
< |
SM_SAMP(sm) = rs; |
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 = n*SAMPSIZ + extra_points*SM_POINTSIZ + 8; |
72 |
> |
nbytes = (*nptr)*SAMPSIZ + extra_points*POINTSIZ + 8; |
73 |
|
for (i = 1024; nbytes > i; i <<= 1) |
74 |
|
; |
75 |
< |
n = (i - 8 - extra_points*SM_POINTSIZ) / SAMPSIZ; |
75 |
> |
/* get number of samples that fit in block */ |
76 |
> |
n = (i - 8 - extra_points*POINTSIZ) / SAMPSIZ; |
77 |
|
|
78 |
< |
RS_BASE(rs) = (char *)malloc(n*SAMPSIZ + extra_points*SM_POINTSIZ); |
79 |
< |
if (!RS_BASE(rs)) |
80 |
< |
error(SYSTEM,"smInit_samples(): Unable to allocate memory"); |
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 |
< |
RS_W_PT(rs) = (float (*)[3])RS_BASE(rs); |
84 |
< |
RS_W_DIR(rs) = (int4 *)(RS_W_PT(rs) + n + extra_points); |
85 |
< |
RS_BRT(rs) = (TMbright *)(RS_W_DIR(rs) + n + extra_points); |
86 |
< |
RS_CHR(rs) = (BYTE (*)[3])(RS_BRT(rs) + n); |
87 |
< |
RS_RGB(rs) = (BYTE (*)[3])(RS_CHR(rs) + n); |
88 |
< |
RS_MAX_SAMP(rs) = n; |
89 |
< |
RS_MAX_AUX_PT(rs) = n + extra_points; |
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 |
< |
smClear_samples(sm); |
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 |
< |
return(n); |
99 |
> |
*nptr = n; |
100 |
> |
|
101 |
> |
return(s); |
102 |
|
} |
103 |
|
|
57 |
– |
smClear_aux_samples(sm) |
58 |
– |
SM *sm; |
59 |
– |
{ |
60 |
– |
RS_NEXT_AUX_PT(SM_SAMP(sm)) = RS_MAX_SAMP(SM_SAMP(sm)); |
61 |
– |
} |
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 |
< |
smClear_samples(sm) |
111 |
< |
SM *sm; |
110 |
> |
sAdd_base_point(s,v) |
111 |
> |
SAMP *s; |
112 |
> |
FVECT v; |
113 |
|
{ |
67 |
– |
RSAMP *samp; |
68 |
– |
|
69 |
– |
samp = SM_SAMP(sm); |
70 |
– |
RS_FREE_SAMP(samp) = -1; |
71 |
– |
RS_REPLACE_SAMP(samp) = 0; |
72 |
– |
RS_NUM_SAMP(samp) = 0; |
73 |
– |
RS_TONE_MAP(samp) = 0; |
74 |
– |
smClear_aux_samples(sm); |
75 |
– |
return(1); |
76 |
– |
} |
77 |
– |
|
78 |
– |
int |
79 |
– |
smAdd_aux_point(sm,v,d) |
80 |
– |
SM *sm; |
81 |
– |
FVECT v,d; |
82 |
– |
{ |
83 |
– |
RSAMP *samp; |
114 |
|
int id; |
115 |
< |
|
86 |
< |
/* Get the SM sample array */ |
87 |
< |
samp = SM_SAMP(sm); |
115 |
> |
|
116 |
|
/* Get pointer to next available point */ |
117 |
< |
id = RS_NEXT_AUX_PT(samp); |
118 |
< |
if(id >= RS_MAX_AUX_PT(samp)) |
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 |
< |
RS_NEXT_AUX_PT(samp)++; |
122 |
> |
S_NEXT_BASE_PT(s)++; |
123 |
|
|
124 |
|
/* Copy vector into point */ |
125 |
< |
VCOPY(RS_NTH_W_PT(samp,id),v); |
98 |
< |
RS_NTH_W_DIR(samp,id) = encodedir(d); |
125 |
> |
VCOPY(S_NTH_W_PT(s,id),v); |
126 |
|
return(id); |
127 |
|
} |
128 |
|
|
129 |
< |
void |
130 |
< |
smInit_sample(sm,id,c,d,p) |
131 |
< |
SM *sm; |
132 |
< |
int id; |
133 |
< |
COLR c; |
134 |
< |
FVECT d,p; |
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 |
|
{ |
109 |
– |
RSAMP *samp; |
137 |
|
|
138 |
< |
samp = SM_SAMP(sm); |
139 |
< |
if(p) |
138 |
> |
#ifdef DEBUG |
139 |
> |
if(id <0||id >= S_MAX_SAMP(s)||n_id <0||n_id >= S_MAX_SAMP(s)) |
140 |
|
{ |
141 |
< |
/* Copy vector into point */ |
142 |
< |
VCOPY(RS_NTH_W_PT(samp,id),p); |
116 |
< |
RS_NTH_W_DIR(samp,id) = encodedir(d); |
141 |
> |
eputs("smReset_sample():invalid sample id\n"); |
142 |
> |
return(0); |
143 |
|
} |
118 |
– |
else |
119 |
– |
{ |
120 |
– |
VADD(RS_NTH_W_PT(samp,id),d,SM_VIEW_CENTER(sm)); |
121 |
– |
RS_NTH_W_DIR(samp,id) = -1; |
122 |
– |
} |
123 |
– |
|
124 |
– |
#ifndef TEST_DRIVER |
125 |
– |
tmCvColrs(&RS_NTH_BRT(samp,id),RS_NTH_CHR(samp,id),c,1); |
126 |
– |
#else |
127 |
– |
if(c) |
128 |
– |
{ |
129 |
– |
RS_NTH_RGB(samp,id)[0] = c[0]; |
130 |
– |
RS_NTH_RGB(samp,id)[1] = c[1]; |
131 |
– |
RS_NTH_RGB(samp,id)[2] = c[2]; |
132 |
– |
} |
133 |
– |
else |
134 |
– |
{ |
135 |
– |
RS_NTH_RGB(samp,id)[0] = 100; |
136 |
– |
RS_NTH_RGB(samp,id)[1] = 0; |
137 |
– |
RS_NTH_RGB(samp,id)[2] = 0; |
138 |
– |
} |
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 |
< |
smReset_sample(sm,id,n_id) |
167 |
< |
SM *sm; |
168 |
< |
int id,n_id; |
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 |
|
{ |
148 |
– |
RSAMP *samp; |
173 |
|
|
174 |
< |
samp = SM_SAMP(sm); |
175 |
< |
|
174 |
> |
if(o_id != INVALID) |
175 |
> |
sCopy_samp(s,id,o_id); |
176 |
> |
else |
177 |
> |
{ |
178 |
|
/* Copy vector into point */ |
179 |
< |
VCOPY(RS_NTH_W_PT(samp,id),RS_NTH_W_PT(samp,n_id)); |
180 |
< |
RS_NTH_W_DIR(samp,id) = RS_NTH_W_DIR(samp,n_id); |
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 |
< |
RS_NTH_BRT(samp,id) = RS_NTH_BRT(samp,n_id); |
186 |
< |
RS_NTH_CHR(samp,id)[0] = RS_NTH_CHR(samp,n_id)[0]; |
187 |
< |
RS_NTH_CHR(samp,id)[1] = RS_NTH_CHR(samp,n_id)[1]; |
188 |
< |
RS_NTH_CHR(samp,id)[2] = RS_NTH_CHR(samp,n_id)[2]; |
189 |
< |
RS_NTH_RGB(samp,id)[0] = RS_NTH_RGB(samp,n_id)[0]; |
190 |
< |
RS_NTH_RGB(samp,id)[1] = RS_NTH_RGB(samp,n_id)[1]; |
191 |
< |
RS_NTH_RGB(samp,id)[2] = RS_NTH_RGB(samp,n_id)[2]; |
192 |
< |
|
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 |
< |
rsAlloc_samp(samp,replaced) |
202 |
< |
RSAMP *samp; |
201 |
> |
sAlloc_samp(s,replaced) |
202 |
> |
SAMP *s; |
203 |
|
int *replaced; |
204 |
|
|
205 |
|
{ |
206 |
|
int id; |
207 |
|
|
208 |
< |
if(replaced) |
209 |
< |
*replaced = FALSE; |
176 |
< |
if(RS_NUM_SAMP(samp) < RS_MAX_SAMP(samp)) |
208 |
> |
/* First check if there are any freed sample available */ |
209 |
> |
if((id = S_FREE_SAMP(s)) != INVALID) |
210 |
|
{ |
211 |
< |
id = RS_NUM_SAMP(samp); |
212 |
< |
RS_NUM_SAMP(samp)++; |
213 |
< |
|
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 |
< |
else |
219 |
< |
if(RS_REPLACE_SAMP(samp) != -1) |
220 |
< |
{ |
221 |
< |
id = RS_REPLACE_SAMP(samp); |
222 |
< |
/* NOTE: NEED to do LRU later*/ |
223 |
< |
RS_REPLACE_SAMP(samp) = (id + 1)% RS_MAX_SAMP(samp); |
188 |
< |
if(replaced) |
189 |
< |
*replaced = TRUE; |
190 |
< |
} |
191 |
< |
else |
192 |
< |
if(RS_FREE_SAMP(samp) != -1) |
193 |
< |
{ |
194 |
< |
id = RS_FREE_SAMP(samp); |
195 |
< |
RS_FREE_SAMP(samp) = RS_NTH_W_DIR(samp,id); |
196 |
< |
} |
197 |
< |
else |
198 |
< |
{ |
199 |
< |
#ifdef DEBUG |
200 |
< |
eputs("rsAlloc_samp(): no samples available"); |
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 |
< |
id = -1; |
226 |
< |
} |
227 |
< |
|
205 |
< |
return(id); |
206 |
< |
} |
225 |
> |
*replaced = 0; |
226 |
> |
return(id); |
227 |
> |
} |
228 |
|
|
229 |
< |
int |
209 |
< |
rsDelete_sample(samp,id) |
210 |
< |
RSAMP *samp; |
211 |
< |
int id; |
229 |
> |
#ifdef DEBUG |
230 |
|
{ |
231 |
< |
/* First check for a valid id */ |
232 |
< |
if(id >= RS_MAX_SAMP(samp) || id < 0) |
233 |
< |
return(FALSE); |
234 |
< |
|
235 |
< |
RS_NTH_W_DIR(samp,id) = RS_FREE_SAMP(samp); |
236 |
< |
RS_FREE_SAMP(samp) = id; |
219 |
< |
|
220 |
< |
return(TRUE); |
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 |
< |
|
223 |
< |
int |
224 |
< |
smDelete_sample(sm,id) |
225 |
< |
SM *sm; |
226 |
< |
int id; |
227 |
< |
{ |
228 |
< |
RSAMP *samp; |
229 |
< |
|
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 |
< |
int replaced; |
238 |
> |
#endif |
239 |
|
|
240 |
< |
/* Get the SM sample array */ |
241 |
< |
samp = SM_SAMP(sm); |
242 |
< |
/* Get pointer to next available point */ |
243 |
< |
id = rsAlloc_samp(samp,&replaced); |
244 |
< |
if(id < 0) |
245 |
< |
return(-1); |
246 |
< |
|
247 |
< |
if(replaced) |
248 |
< |
smMesh_remove_vertex(sm,id); |
249 |
< |
|
250 |
< |
smInit_sample(sm,id,c,dir,p); |
251 |
< |
|
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 |
|
|