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.1 by gwlarson, Wed Aug 19 17:45:24 1998 UTC vs.
Revision 3.7 by greg, Sat Feb 22 02:07:25 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   #include "standard.h"
8 + #include "sm_flag.h"
9 + #include "rhd_sample.h"
10  
11 < #include "sm.h"
11 > SAMP rsL;
12 > int4 *samp_flag=NULL;
13  
14 < RSAMP rsL;
14 > /* Each sample has a world coord point, and direction, brightness,chrominance,
15 >   and RGB triples
16 > */
17  
18 + #define TMSIZE sizeof(TMbright)
19 + #define SAMPSIZ (3*sizeof(SFLOAT)+sizeof(int4)+ 6*sizeof(BYTE) + TMSIZE + 2*sizeof(int))
20  
21 < #define SAMPSIZ (3*sizeof(float)+sizeof(int4)+\
22 <                        sizeof(TMbright)+6*sizeof(BYTE))
23 < /* Initialize/clear global smL sample list for at least n samples */
24 < int
25 < smAlloc_samples(sm,n,extra_points)
26 < SM *sm;
23 < register int n,extra_points;
21 > /* Extra points world space point, vert flag and qt flag */
22 > #define POINTSIZ (3*sizeof(SFLOAT) + 2*sizeof(int))
23 >
24 > /* Clear the flags for all samples */
25 > sClear_all_flags(s)
26 > SAMP *s;
27   {
28 <  unsigned nbytes;
29 <  register unsigned     i;
30 <  RSAMP *rs;
28 >  if(samp_flag)
29 >    bzero((char *)samp_flag,FLAG_BYTES(S_MAX_BASE_PT(s)));
30 > }
31  
32 <  rs = &rsL;
32 > 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  
41 <  SM_SAMP(sm) = rs;
41 > }
42  
43 + 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    /* round space up to nearest power of 2 */
69 <  nbytes = n*SAMPSIZ + extra_points*SM_POINTSIZ + 8;
69 >  nbytes = (*nptr)*SAMPSIZ + extra_points*POINTSIZ + 8;
70    for (i = 1024; nbytes > i; i <<= 1)
71                  ;
72 <  n = (i - 8 - extra_points*SM_POINTSIZ) / SAMPSIZ;    
72 >  /* get number of samples that fit in block */
73 >  n = (i - 8 - extra_points*POINTSIZ) / SAMPSIZ;        
74  
75 <  RS_BASE(rs) = (char *)malloc(n*SAMPSIZ + extra_points*SM_POINTSIZ);
76 <  if (!RS_BASE(rs))
77 <    error(SYSTEM,"smInit_samples(): Unable to allocate memory");
75 >  /* 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 +  S_BASE(s) = (char *)malloc(n*SAMPSIZ + extra_points*POINTSIZ);
80 +  if (!S_BASE(s))
81 +    error(SYSTEM,"sAlloc(): Unable to allocate memory");
82 +
83    /* assign larger alignment types earlier */
84 <  RS_W_PT(rs) = (float (*)[3])RS_BASE(rs);
85 <  RS_W_DIR(rs) = (int4 *)(RS_W_PT(rs) + n + extra_points);
86 <  RS_BRT(rs) = (TMbright *)(RS_W_DIR(rs) + n + extra_points);
87 <  RS_CHR(rs) = (BYTE (*)[3])(RS_BRT(rs) + n);
88 <  RS_RGB(rs) = (BYTE (*)[3])(RS_CHR(rs) + n);
89 <  RS_MAX_SAMP(rs) = n;
90 <  RS_MAX_AUX_PT(rs) = n + extra_points;
84 >  S_W_PT(s) = (SFLOAT(*)[3])S_BASE(s);
85 >  S_W_DIR(s) = (int4 *)(S_W_PT(s) + n + extra_points);
86 >  S_BRT(s) = (TMbright *)(S_W_DIR(s) + n);
87 >  S_CHR(s) = (BYTE (*)[3])(S_BRT(s) + n);
88 >  S_RGB(s) = (BYTE (*)[3])(S_CHR(s) + n);
89 >  S_INFO(s) = (int *)(S_RGB(s)+n);
90 >  S_INFO1(s) = (int *)(S_INFO(s)+n+extra_points);
91 >  S_MAX_SAMP(s) = n;
92 >  S_MAX_BASE_PT(s) = n + extra_points;
93  
94 <  smClear_samples(sm);
94 >  /* Allocate memory for a per/sample bit flag */
95 >  if(!(samp_flag = (int4 *)malloc(FLAG_BYTES(n+extra_points))))
96 >    error(SYSTEM,"sAlloc(): Unable to allocate flag memory");
97 >  sInit(s);
98 >  sClear_all_flags(s);
99  
100 <  return(n);
100 >  *nptr = n;
101 >  
102 >  return(s);
103   }
104  
57 smClear_aux_samples(sm)
58 SM *sm;
59 {
60  RS_NEXT_AUX_PT(SM_SAMP(sm)) = RS_MAX_SAMP(SM_SAMP(sm));
61 }
105  
106 < int
107 < smClear_samples(sm)
108 <     SM *sm;
106 > /* 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 > S_ID
111 > sAdd_base_point(s,v)
112 >     SAMP *s;
113 >     FVECT v;
114   {
115 <  RSAMP *samp;
115 >    S_ID id;
116  
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);
117      /* Get pointer to next available point */
118 <    id = RS_NEXT_AUX_PT(samp);
119 <    if(id >= RS_MAX_AUX_PT(samp))
118 >    id = S_NEXT_BASE_PT(s);
119 >    if(id >= S_MAX_BASE_PT(s))
120         return(-1);
121  
122      /* Update free aux pointer */
123 <    RS_NEXT_AUX_PT(samp)++;
123 >    S_NEXT_BASE_PT(s)++;
124  
125      /* Copy vector into point */
126 <    VCOPY(RS_NTH_W_PT(samp,id),v);
98 <    RS_NTH_W_DIR(samp,id) = encodedir(d);
126 >    VCOPY(S_NTH_W_PT(s,id),v);
127      return(id);
128   }
129  
130 < void
131 < smInit_sample(sm,id,c,d,p)
132 <   SM *sm;
133 <   int id;
134 <   COLR c;
135 <   FVECT d,p;
130 > /* 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 >   S_ID n_id,id;
137   {
109   RSAMP *samp;
138  
139 <   samp = SM_SAMP(sm);
140 <    if(p)
139 > #ifdef DEBUG
140 >   if(id <0||id >= S_MAX_SAMP(s)||n_id <0||n_id >= S_MAX_SAMP(s))
141     {
142 <       /* Copy vector into point */
143 <       VCOPY(RS_NTH_W_PT(samp,id),p);
116 <       RS_NTH_W_DIR(samp,id) = encodedir(d);
142 >     eputs("smReset_sample():invalid sample id\n");
143 >     return(0);
144     }
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    }
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 + /* 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   void
167 < smReset_sample(sm,id,n_id)
168 <   SM *sm;
169 <   int id,n_id;
167 > sInit_samp(s,id,c,d,p,o_id)
168 >   SAMP *s;
169 >   S_ID id;
170 >   COLR c;
171 >   FVECT d,p;
172 >   S_ID o_id;
173   {
148   RSAMP *samp;
174  
175 <   samp = SM_SAMP(sm);
176 <    
175 >  if(o_id != INVALID)
176 >    sCopy_samp(s,id,o_id);
177 >  else
178 >  {
179      /* Copy vector into point */
180 <    VCOPY(RS_NTH_W_PT(samp,id),RS_NTH_W_PT(samp,n_id));
181 <    RS_NTH_W_DIR(samp,id) = RS_NTH_W_DIR(samp,n_id);
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  
186 <    RS_NTH_BRT(samp,id) = RS_NTH_BRT(samp,n_id);
187 <    RS_NTH_CHR(samp,id)[0] = RS_NTH_CHR(samp,n_id)[0];
188 <    RS_NTH_CHR(samp,id)[1] = RS_NTH_CHR(samp,n_id)[1];
189 <    RS_NTH_CHR(samp,id)[2] = RS_NTH_CHR(samp,n_id)[2];
190 <    RS_NTH_RGB(samp,id)[0] = RS_NTH_RGB(samp,n_id)[0];
191 <    RS_NTH_RGB(samp,id)[1] = RS_NTH_RGB(samp,n_id)[1];
192 <    RS_NTH_RGB(samp,id)[2] = RS_NTH_RGB(samp,n_id)[2];
193 <
186 >    /* calculate the brightness and chrominance,RGB will be set by
187 >       tonemapping
188 >       */
189 >    tmCvColrs(&S_NTH_BRT(s,id),S_NTH_CHR(s,id),c,1);
190 >  }
191 >    /* Set ACTIVE bit upon creation */
192 >  S_SET_FLAG(id);
193 >  if(id < S_TONE_MAP(s))
194 >    tmMapPixels(S_NTH_RGB(s,id),&S_NTH_BRT(s,id),S_NTH_CHR(s,id),1);
195   }
196  
197 < int
198 < rsAlloc_samp(samp,replaced)
199 <   RSAMP *samp;
200 <   char *replaced;
197 >
198 > /* Allocate a new sample. If an existing sample was replaced: set flag */
199 > S_ID
200 > sAlloc_samp(s,replaced)
201 >   SAMP *s;
202 >   int *replaced;
203    
204   {
205 <    int id;
205 >    S_ID id;
206  
207 <    if(replaced)
208 <       *replaced = FALSE;
176 <    if(RS_NUM_SAMP(samp) < RS_MAX_SAMP(samp))
207 >    /* First check if there are any freed sample available */
208 >    if((id = S_FREE_SAMP(s)) != INVALID)
209      {
210 <        id = RS_NUM_SAMP(samp);
211 <        RS_NUM_SAMP(samp)++;
212 <        
210 >      *replaced = 0;
211 > #if 0
212 >      fprintf(stderr,"allocating previously freed sample %d\n",id);
213 > #endif
214 >      S_FREE_SAMP(s) = S_NTH_NEXT(s,id);
215 >      return(id);
216      }
217 <    else
218 <       if(RS_REPLACE_SAMP(samp) != -1)
219 <        {
220 <            id = RS_REPLACE_SAMP(samp);
221 <            /* NOTE: NEED to do LRU later*/
222 <            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");
217 >    /* If havn't reached end of sample array:return next sample */
218 >    if(S_NUM_SAMP(s) < S_MAX_SAMP(s))
219 >    {
220 >      id = S_NUM_SAMP(s)++;
221 > #if 0
222 >      fprintf(stderr,"allocating sample %d\n",id);
223   #endif
224 <                 id = -1;
225 <             }
224 >      *replaced = 0;
225 >      return(id);
226 >    }
227 >
228 > #ifdef DEBUG
229 > {
230 >  static int replace = 0;
231 >  if(replace == 0)
232 >  {
233 >    eputs("Out of samples: using replacement strategy\n");
234 >    replace =1 ;
235 >  }
236 > }
237 > #endif
238      
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   }
253  
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;
254  
220    return(TRUE);
221 }
255  
223 int
224 smDelete_sample(sm,id)
225 SM *sm;
226 int id;
227 {
228    RSAMP *samp;
256  
230    samp = SM_SAMP(sm);
231    return(rsDelete_sample(samp,id));
232 }
257  
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;
258  
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);
259  
254    if(replaced)
255       smMesh_remove_vertex(sm,id);
256    
257    smInit_sample(sm,id,c,dir,p);
260  
259    return(id);
260 }
261  
262  
263  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines