ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/sm_samp.c
(Generate patch)

Comparing ray/src/hd/sm_samp.c (file contents):
Revision 3.2 by gwlarson, Fri Sep 11 11:52:27 1998 UTC vs.
Revision 3.3 by gwlarson, Tue Oct 6 18:16:54 1998 UTC

# Line 8 | Line 8 | static char SCCSid[] = "$SunId$ SGI";
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_SAMP(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 >  sClear_all_flags(s);
48  
49 <  SM_SAMP(sm) = rs;
49 > }
50  
51 + sFree(s)
52 + SAMP *s;
53 + {
54 +    if(S_BASE(s))
55 +    {
56 +        free(S_BASE(s));
57 +        S_BASE(s)=NULL;
58 +    }
59 +    if(samp_flag)
60 +    {
61 +        free(samp_flag);
62 +        samp_flag = NULL;
63 +    }
64 + }
65 +        /* Initialize/clear global smL sample list for at least n samples */
66 + SAMP
67 + *sAlloc(nptr,extra_points)
68 + int *nptr,extra_points;
69 + {
70 +  unsigned nbytes;
71 +  register unsigned     i;
72 +  SAMP *s;
73 +  int n;
74 +  
75 +  s = &rsL;
76    /* round space up to nearest power of 2 */
77 <  nbytes = n*SAMPSIZ + extra_points*SM_POINTSIZ + 8;
77 >  nbytes = (*nptr)*SAMPSIZ + extra_points*POINTSIZ + 8;
78    for (i = 1024; nbytes > i; i <<= 1)
79                  ;
80 <  n = (i - 8 - extra_points*SM_POINTSIZ) / SAMPSIZ;    
80 >  /* get number of samples that fit in block */
81 >  n = (i - 8 - extra_points*POINTSIZ) / SAMPSIZ;        
82  
83 <  RS_BASE(rs) = (char *)malloc(n*SAMPSIZ + extra_points*SM_POINTSIZ);
84 <  if (!RS_BASE(rs))
83 >  S_BASE(s) = (char *)malloc(n*SAMPSIZ + extra_points*POINTSIZ);
84 >  if (!S_BASE(s))
85      error(SYSTEM,"smInit_samples(): Unable to allocate memory");
86  
87    /* assign larger alignment types earlier */
88 <  RS_W_PT(rs) = (float (*)[3])RS_BASE(rs);
89 <  RS_W_DIR(rs) = (int4 *)(RS_W_PT(rs) + n + extra_points);
90 <  RS_BRT(rs) = (TMbright *)(RS_W_DIR(rs) + n + extra_points);
91 <  RS_CHR(rs) = (BYTE (*)[3])(RS_BRT(rs) + n);
92 <  RS_RGB(rs) = (BYTE (*)[3])(RS_CHR(rs) + n);
93 <  RS_MAX_SAMP(rs) = n;
94 <  RS_MAX_AUX_PT(rs) = n + extra_points;
88 >  S_W_PT(s) = (float (*)[3])S_BASE(s);
89 >  S_W_DIR(s) = (int4 *)(S_W_PT(s) + n + extra_points);
90 > #ifndef HP_VERSION
91 > S_BRT(s) = (TMbright *)(S_W_DIR(s) + n);
92 >  S_CHR(s) = (BYTE (*)[3])(S_BRT(s) + n);
93 > #else
94 >  S_CHR(s) = (BYTE (*)[3])(S_W_DIR(s) + n);
95 > #endif
96 >  S_RGB(s) = (BYTE (*)[3])(S_CHR(s) + n);
97 >  S_MAX_SAMP(s) = n;
98 >  S_MAX_BASE_PT(s) = n + extra_points;
99  
100 <  smClear_samples(sm);
100 >  /* Allocate memory for a per/sample bit flag */
101 >  if(!(samp_flag = (int4 *)malloc(FLAG_BYTES(n))))
102 >    error(SYSTEM,"smInit_samples(): Unable to allocate flag memory");
103  
104 <  return(n);
104 >  sInit(s);
105 >  *nptr = n;
106 >  
107 >  return(s);
108   }
109  
57 smClear_aux_samples(sm)
58 SM *sm;
59 {
60  RS_NEXT_AUX_PT(SM_SAMP(sm)) = RS_MAX_SAMP(SM_SAMP(sm));
61 }
110  
111 + /* Add a base point to the sample structure: only the world point
112 +   is added: These points are not displayed-they are used to form the
113 +   initial mesh
114 + */
115   int
116 < smClear_samples(sm)
117 <     SM *sm;
116 > sAdd_base_point(s,v)
117 >     SAMP *s;
118 >     FVECT v;
119   {
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;
120      int id;
121 <    
86 <    /* Get the SM sample array */    
87 <    samp = SM_SAMP(sm);
121 >
122      /* Get pointer to next available point */
123 <    id = RS_NEXT_AUX_PT(samp);
124 <    if(id >= RS_MAX_AUX_PT(samp))
123 >    id = S_NEXT_BASE_PT(s);
124 >    if(id >= S_MAX_BASE_PT(s))
125         return(-1);
126  
127      /* Update free aux pointer */
128 <    RS_NEXT_AUX_PT(samp)++;
128 >    S_NEXT_BASE_PT(s)++;
129  
130      /* Copy vector into point */
131 <    VCOPY(RS_NTH_W_PT(samp,id),v);
98 <    RS_NTH_W_DIR(samp,id) = encodedir(d);
131 >    VCOPY(S_NTH_W_PT(s,id),v);
132      return(id);
133   }
134  
135 + /* Initialize the sample 'id' to contain world point 'p', direction 'd' and
136 +   color 'c'. If 'p' is NULL, a direction only is represented in 'd'. In
137 +   this case, the world point is set to the projection of the direction on
138 +   the view sphere, and the direction value is set to -1
139 + */
140   void
141 < smInit_sample(sm,id,c,d,p)
142 <   SM *sm;
141 > sInit_samp(s,id,c,d,p)
142 >   SAMP *s;
143     int id;
144     COLR c;
145     FVECT d,p;
146   {
147 <   RSAMP *samp;
148 <
149 <   samp = SM_SAMP(sm);
150 <    if(p)
151 <   {
114 <       /* Copy vector into point */
115 <       VCOPY(RS_NTH_W_PT(samp,id),p);
116 <       RS_NTH_W_DIR(samp,id) = encodedir(d);
117 <   }
147 >    /* Copy vector into point */
148 >    VCOPY(S_NTH_W_PT(s,id),p);
149 >    if(d)
150 >      S_NTH_W_DIR(s,id) = encodedir(d);
151 >  /* direction only */
152      else
153 <       {
154 <           VADD(RS_NTH_W_PT(samp,id),d,SM_VIEW_CENTER(sm));
155 <           RS_NTH_W_DIR(samp,id) = -1;
122 <       }
123 <    
153 >      S_NTH_W_DIR(s,id) = -1;
154 >
155 >   /* calculate the brightness and chrominance */
156   #ifndef TEST_DRIVER
157 <    tmCvColrs(&RS_NTH_BRT(samp,id),RS_NTH_CHR(samp,id),c,1);
157 >    tmCvColrs(&S_NTH_BRT(s,id),S_NTH_CHR(s,id),c,1);
158   #else
159      if(c)
160      {
161 <        RS_NTH_RGB(samp,id)[0] = c[0];
162 <        RS_NTH_RGB(samp,id)[1] = c[1];
163 <        RS_NTH_RGB(samp,id)[2] = c[2];
161 >      S_NTH_RGB(s,id)[0] = c[0];
162 >      S_NTH_RGB(s,id)[1] = c[1];
163 >      S_NTH_RGB(s,id)[2] = c[2];
164      }
165      else
166      {
167 <        RS_NTH_RGB(samp,id)[0] = 100;
168 <       RS_NTH_RGB(samp,id)[1] = 0;
169 <       RS_NTH_RGB(samp,id)[2] = 0;
167 >        S_NTH_RGB(s,id)[0] = 100;
168 >       S_NTH_RGB(s,id)[1] = 0;
169 >       S_NTH_RGB(s,id)[2] = 0;
170      }
171   #endif
172 +    /* Set ACTIVE bit upon creation */
173 +    S_SET_FLAG(id);
174 +
175   }
176  
177  
178 < void
179 < smReset_sample(sm,id,n_id)
180 <   SM *sm;
181 <   int id,n_id;
178 > /* Copy the values of sample n_id into sample id: Note must not call with
179 >   Base point n_id
180 > */
181 > int
182 > sReset_samp(s,n_id,id)
183 >   SAMP *s;
184 >   int n_id,id;
185   {
148   RSAMP *samp;
186  
187 <   samp = SM_SAMP(sm);
188 <    
189 <    /* Copy vector into point */
190 <    VCOPY(RS_NTH_W_PT(samp,id),RS_NTH_W_PT(samp,n_id));
191 <    RS_NTH_W_DIR(samp,id) = RS_NTH_W_DIR(samp,n_id);
187 > #ifdef DEBUG
188 >   if(id <0||id >= S_MAX_SAMP(s)||n_id <0||n_id >= S_MAX_SAMP(s))
189 >   {
190 >     eputs("smReset_sample():invalid sample id\n");
191 >     return(0);
192 >   }
193 > #endif
194  
195 <    RS_NTH_BRT(samp,id) = RS_NTH_BRT(samp,n_id);
196 <    RS_NTH_CHR(samp,id)[0] = RS_NTH_CHR(samp,n_id)[0];
197 <    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];
195 > /* Copy vector into point */
196 >   VCOPY(S_NTH_W_PT(s,n_id),S_NTH_W_PT(s,id));
197 >   S_NTH_W_DIR(s,n_id) = S_NTH_W_DIR(s,id);
198  
199 + #ifndef HP_VERSION
200 +   S_NTH_BRT(s,n_id) = S_NTH_BRT(s,id);
201 + #endif
202 +   S_NTH_CHR(s,n_id)[0] = S_NTH_CHR(s,id)[0];
203 +   S_NTH_CHR(s,n_id)[1] = S_NTH_CHR(s,id)[1];
204 +   S_NTH_CHR(s,n_id)[2] = S_NTH_CHR(s,id)[2];
205 +   return(1);
206   }
207  
208 + /* Allocate a new sample. If an existing sample was replaced: set flag */
209   int
210 < rsAlloc_samp(samp,replaced)
211 <   RSAMP *samp;
210 > sAlloc_samp(s,replaced)
211 >   SAMP *s;
212     int *replaced;
213    
214   {
215      int id;
216  
217      if(replaced)
218 <       *replaced = FALSE;
219 <    if(RS_NUM_SAMP(samp) < RS_MAX_SAMP(samp))
218 >       *replaced = 0;
219 >
220 >    if((id = S_FREE_SAMP(s)) != INVALID)
221      {
222 <        id = RS_NUM_SAMP(samp);
223 <        RS_NUM_SAMP(samp)++;
180 <        
222 >      S_FREE_SAMP(s) = INVALID;
223 >      return(id);
224      }
225 +    /* If havn't reached end of sample array:return next sample */
226 +    if(S_NUM_SAMP(s) < S_MAX_SAMP(s))
227 +        id = S_NUM_SAMP(s)++;
228      else
229 <       if(RS_REPLACE_SAMP(samp) != -1)
230 <        {
231 <            id = RS_REPLACE_SAMP(samp);
232 <            /* NOTE: NEED to do LRU later*/
233 <            RS_REPLACE_SAMP(samp) = (id + 1)% RS_MAX_SAMP(samp);
234 <            if(replaced)
235 <               *replaced = TRUE;
236 <        }
237 <       else
238 <          if(RS_FREE_SAMP(samp) != -1)
239 <             {
240 <                 id = RS_FREE_SAMP(samp);
241 <                 RS_FREE_SAMP(samp) = RS_NTH_W_DIR(samp,id);
242 <             }
243 <          else
198 <             {
199 < #ifdef DEBUG
200 <                 eputs("rsAlloc_samp(): no samples available");
201 < #endif
202 <                 id = -1;
203 <             }
204 <    
229 >    {
230 >      /* CLOCKED LRU replacement policy: Search through samples starting
231 >         with S_REPLACE_SAMP for a sample that does not have its active
232 >         bit set. Clear bit along the way
233 >       */
234 >      id = S_REPLACE_SAMP(s);
235 >      while(S_IS_FLAG(id))
236 >      {
237 >        S_CLR_FLAG(id);
238 >        id = (id +1)% S_MAX_SAMP(s);
239 >      }
240 >      S_REPLACE_SAMP(s) = id = (id +1)% S_MAX_SAMP(s);
241 >      if(replaced)
242 >        *replaced = 1;
243 >    }
244      return(id);
245   }
246  
247 < int
248 < rsDelete_sample(samp,id)
249 < RSAMP *samp;
211 < int id;
212 < {
213 <    /* First check for a valid id */
214 <    if(id >= RS_MAX_SAMP(samp) || id < 0)
215 <       return(FALSE);
216 <    
217 <    RS_NTH_W_DIR(samp,id) = RS_FREE_SAMP(samp);
218 <    RS_FREE_SAMP(samp) = id;
247 > /* Set the sample flag to be unused- so will get replaced on next LRU
248 >   iteration
249 >   */
250  
251 <    return(TRUE);
221 < }
222 <
251 > /* NOTE: Do we want a free list as well? */
252   int
253 < smDelete_sample(sm,id)
254 < SM *sm;
253 > sDelete_samp(s,id)
254 > SAMP *s;
255   int id;
256   {
257 <    RSAMP *samp;
258 <
259 <    samp = SM_SAMP(sm);
260 <    return(rsDelete_sample(samp,id));
261 < }
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;
257 > #ifdef DEBUG
258 >    /* Fist check for a valid id */
259 >    if(id >= S_MAX_SAMP(s) || id < 0)
260 >       return(0);
261 > #endif
262      
263 <    /* Get the SM sample array */    
264 <    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);
263 >    S_CLR_FLAG(id);
264 >    return(1);
265   }
261
262
263
264
266  
267  
268  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines