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.9 by schorsch, Mon Jun 30 14:59:12 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1998 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * sm_samp.c
6   */
7 +
8 + #include <string.h>
9 +
10   #include "standard.h"
11 + #include "sm_flag.h"
12 + #include "rhd_sample.h"
13  
14 < #include "sm.h"
14 > SAMP rsL;
15 > int32 *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(int32)+ 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 >    memset((char *)samp_flag, '\0', 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 >  /* Must make sure n + extra_points can fit in a S_ID identifier */
79 >  if ( n > (S_ID_MAX - extra_points))
80 >    n = (S_ID_MAX - extra_points);
81  
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) = (SFLOAT(*)[3])S_BASE(s);
88 >  S_W_DIR(s) = (int32 *)(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_INFO(s) = (int *)(S_RGB(s)+n);
93 >  S_INFO1(s) = (int *)(S_INFO(s)+n+extra_points);
94 >  S_MAX_SAMP(s) = n;
95 >  S_MAX_BASE_PT(s) = n + extra_points;
96  
97 <  smClear_samples(sm);
97 >  /* Allocate memory for a per/sample bit flag */
98 >  if(!(samp_flag = (int32 *)malloc(FLAG_BYTES(n+extra_points))))
99 >    error(SYSTEM,"sAlloc(): Unable to allocate flag memory");
100 >  sInit(s);
101 >  sClear_all_flags(s);
102  
103 <  return(n);
103 >  *nptr = n;
104 >  
105 >  return(s);
106   }
107  
57 smClear_aux_samples(sm)
58 SM *sm;
59 {
60  RS_NEXT_AUX_PT(SM_SAMP(sm)) = RS_MAX_SAMP(SM_SAMP(sm));
61 }
108  
109 < int
110 < smClear_samples(sm)
111 <     SM *sm;
109 > /* Add a base point to the sample structure: only the world point
110 >   is added: These points are not displayed-they are used to form the
111 >   initial mesh
112 > */
113 > S_ID
114 > sAdd_base_point(s,v)
115 >     SAMP *s;
116 >     FVECT v;
117   {
118 <  RSAMP *samp;
118 >    S_ID id;
119  
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;
84    int id;
85    
86    /* Get the SM sample array */    
87    samp = SM_SAMP(sm);
120      /* Get pointer to next available point */
121 <    id = RS_NEXT_AUX_PT(samp);
122 <    if(id >= RS_MAX_AUX_PT(samp))
121 >    id = S_NEXT_BASE_PT(s);
122 >    if(id >= S_MAX_BASE_PT(s))
123         return(-1);
124  
125      /* Update free aux pointer */
126 <    RS_NEXT_AUX_PT(samp)++;
126 >    S_NEXT_BASE_PT(s)++;
127  
128      /* Copy vector into point */
129 <    VCOPY(RS_NTH_W_PT(samp,id),v);
98 <    RS_NTH_W_DIR(samp,id) = encodedir(d);
129 >    VCOPY(S_NTH_W_PT(s,id),v);
130      return(id);
131   }
132  
133 < void
134 < smInit_sample(sm,id,c,d,p)
135 <   SM *sm;
136 <   int id;
137 <   COLR c;
138 <   FVECT d,p;
133 > /* Copy the values of sample n_id into sample id: Note must not call with
134 >   Base point n_id
135 > */
136 > int
137 > sCopy_samp(s,n_id,id)
138 >   SAMP *s;
139 >   S_ID n_id,id;
140   {
109   RSAMP *samp;
141  
142 <   samp = SM_SAMP(sm);
143 <    if(p)
142 > #ifdef DEBUG
143 >   if(id <0||id >= S_MAX_SAMP(s)||n_id <0||n_id >= S_MAX_SAMP(s))
144     {
145 <       /* Copy vector into point */
146 <       VCOPY(RS_NTH_W_PT(samp,id),p);
116 <       RS_NTH_W_DIR(samp,id) = encodedir(d);
145 >     eputs("smReset_sample():invalid sample id\n");
146 >     return(0);
147     }
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    }
148   #endif
149 +   if(n_id == id)
150 +     return(1);
151 + /* Copy vector into point */
152 +   VCOPY(S_NTH_W_PT(s,n_id),S_NTH_W_PT(s,id));
153 +   S_NTH_W_DIR(s,n_id) = S_NTH_W_DIR(s,id);
154 +
155 +   S_NTH_BRT(s,n_id) = S_NTH_BRT(s,id);
156 +   S_NTH_CHR(s,n_id)[0] = S_NTH_CHR(s,id)[0];
157 +   S_NTH_CHR(s,n_id)[1] = S_NTH_CHR(s,id)[1];
158 +   S_NTH_CHR(s,n_id)[2] = S_NTH_CHR(s,id)[2];
159 +
160 +   return(1);
161   }
162  
163  
164 + /* Initialize the sample 'id' to contain world point 'p', direction 'd' and
165 +   color 'c'. If 'p' is NULL, a direction only is represented in 'd'. In
166 +   this case, the world point is set to the projection of the direction on
167 +   the view sphere, and the direction value is set to -1
168 + */
169   void
170 < smReset_sample(sm,id,n_id)
171 <   SM *sm;
172 <   int id,n_id;
170 > sInit_samp(s,id,c,d,p,o_id)
171 >   SAMP *s;
172 >   S_ID id;
173 >   COLR c;
174 >   FVECT d,p;
175 >   S_ID o_id;
176   {
148   RSAMP *samp;
177  
178 <   samp = SM_SAMP(sm);
179 <    
178 >  if(o_id != INVALID)
179 >    sCopy_samp(s,id,o_id);
180 >  else
181 >  {
182      /* Copy vector into point */
183 <    VCOPY(RS_NTH_W_PT(samp,id),RS_NTH_W_PT(samp,n_id));
184 <    RS_NTH_W_DIR(samp,id) = RS_NTH_W_DIR(samp,n_id);
183 >    VCOPY(S_NTH_W_PT(s,id),p);
184 >    if(d)
185 >      S_NTH_W_DIR(s,id) = encodedir(d);
186 >    else
187 >      S_NTH_W_DIR(s,id) = -1;
188  
189 <    RS_NTH_BRT(samp,id) = RS_NTH_BRT(samp,n_id);
190 <    RS_NTH_CHR(samp,id)[0] = RS_NTH_CHR(samp,n_id)[0];
191 <    RS_NTH_CHR(samp,id)[1] = RS_NTH_CHR(samp,n_id)[1];
192 <    RS_NTH_CHR(samp,id)[2] = RS_NTH_CHR(samp,n_id)[2];
193 <    RS_NTH_RGB(samp,id)[0] = RS_NTH_RGB(samp,n_id)[0];
194 <    RS_NTH_RGB(samp,id)[1] = RS_NTH_RGB(samp,n_id)[1];
195 <    RS_NTH_RGB(samp,id)[2] = RS_NTH_RGB(samp,n_id)[2];
196 <
189 >    /* calculate the brightness and chrominance,RGB will be set by
190 >       tonemapping
191 >       */
192 >    tmCvColrs(&S_NTH_BRT(s,id),S_NTH_CHR(s,id),c,1);
193 >  }
194 >    /* Set ACTIVE bit upon creation */
195 >  S_SET_FLAG(id);
196 >  if(id < S_TONE_MAP(s))
197 >    tmMapPixels(S_NTH_RGB(s,id),&S_NTH_BRT(s,id),S_NTH_CHR(s,id),1);
198   }
199  
200 < int
201 < rsAlloc_samp(samp,replaced)
202 <   RSAMP *samp;
200 >
201 > /* Allocate a new sample. If an existing sample was replaced: set flag */
202 > S_ID
203 > sAlloc_samp(s,replaced)
204 >   SAMP *s;
205     int *replaced;
206    
207   {
208 <    int id;
208 >    S_ID id;
209  
210 <    if(replaced)
211 <       *replaced = FALSE;
176 <    if(RS_NUM_SAMP(samp) < RS_MAX_SAMP(samp))
210 >    /* First check if there are any freed sample available */
211 >    if((id = S_FREE_SAMP(s)) != INVALID)
212      {
213 <        id = RS_NUM_SAMP(samp);
214 <        RS_NUM_SAMP(samp)++;
215 <        
213 >      *replaced = 0;
214 > #if 0
215 >      fprintf(stderr,"allocating previously freed sample %d\n",id);
216 > #endif
217 >      S_FREE_SAMP(s) = S_NTH_NEXT(s,id);
218 >      return(id);
219      }
220 <    else
221 <       if(RS_REPLACE_SAMP(samp) != -1)
222 <        {
223 <            id = RS_REPLACE_SAMP(samp);
224 <            /* NOTE: NEED to do LRU later*/
225 <            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");
220 >    /* If havn't reached end of sample array:return next sample */
221 >    if(S_NUM_SAMP(s) < S_MAX_SAMP(s))
222 >    {
223 >      id = S_NUM_SAMP(s)++;
224 > #if 0
225 >      fprintf(stderr,"allocating sample %d\n",id);
226   #endif
227 <                 id = -1;
228 <             }
227 >      *replaced = 0;
228 >      return(id);
229 >    }
230 >
231 > #ifdef DEBUG
232 > {
233 >  static int replace = 0;
234 >  if(replace == 0)
235 >  {
236 >    eputs("Out of samples: using replacement strategy\n");
237 >    replace =1 ;
238 >  }
239 > }
240 > #endif
241      
242 +    /* CLOCKED LRU replacement policy: Search through samples starting
243 +       with S_REPLACE_SAMP for a sample that does not have its active
244 +       bit set. Clear bit along the way
245 +     */
246 +    id = S_REPLACE_SAMP(s);
247 +    while(S_IS_FLAG(id))
248 +    {
249 +      S_CLR_FLAG(id);
250 +      id = (id +1)% S_MAX_SAMP(s);
251 +    }
252 +    S_REPLACE_SAMP(s) = (id +1)% S_MAX_SAMP(s);
253 +    *replaced = 1;
254      return(id);
255   }
256  
208 int
209 rsDelete_sample(samp,id)
210 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;
257  
220    return(TRUE);
221 }
258  
223 int
224 smDelete_sample(sm,id)
225 SM *sm;
226 int id;
227 {
228    RSAMP *samp;
259  
230    samp = SM_SAMP(sm);
231    return(rsDelete_sample(samp,id));
232 }
260  
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;
261  
242 {
243    RSAMP *samp;
244    int id;
245    int 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);
262  
254    if(replaced)
255       smMesh_remove_vertex(sm,id);
256    
257    smInit_sample(sm,id,c,dir,p);
263  
259    return(id);
260 }
264  
265  
266  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines