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

Comparing ray/src/gen/mkillum2.c (file contents):
Revision 1.8 by greg, Thu Jul 25 14:43:15 1991 UTC vs.
Revision 2.18 by greg, Thu Sep 13 06:31:21 2007 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1991 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Routines to do the actual calculation for mkillum
6   */
7  
8 < #include  "mkillum.h"
8 > #include <string.h>
9  
10 + #include  "mkillum.h"
11   #include  "face.h"
14
12   #include  "cone.h"
16
13   #include  "random.h"
14  
15  
16 < o_default(ob, il, rt, nm)       /* default illum action */
17 < OBJREC  *ob;
18 < struct illum_args  *il;
19 < struct rtproc  *rt;
20 < char  *nm;
16 > static void mkaxes(FVECT u, FVECT v, FVECT n);
17 > static void rounddir(FVECT dv, double alt, double azi);
18 > static void flatdir(FVECT dv, double alt, double azi);
19 >
20 >
21 > static COLORV * distarr = NULL;         /* distribution array */
22 > static int      distsiz = 0;
23 >
24 >
25 > static void
26 > newdist(                        /* allocate & clear distribution array */
27 >        int siz
28 > )
29   {
30 +        if (siz == 0) {
31 +                if (distsiz > 0)
32 +                        free((void *)distarr);
33 +                distarr = NULL;
34 +                distsiz = 0;
35 +                return;
36 +        }
37 +        if (distsiz < siz) {
38 +                free((void *)distarr);
39 +                distarr = (COLORV *)malloc(sizeof(COLORV)*3*siz);
40 +                if (distarr == NULL)
41 +                        error(SYSTEM, "Out of memory in distalloc");
42 +                distsiz = siz;
43 +        }
44 +        memset(distarr, '\0', sizeof(COLORV)*3*siz);
45 + }
46 +
47 +
48 + static int
49 + process_ray(RAY *r, int rv)
50 + {
51 +        COLORV  *colp;
52 +
53 +        if (rv == 0)
54 +                return(0);
55 +        if (rv < 0)
56 +                error(USER, "Ray tracing process died");
57 +        if (r->rno >= distsiz)
58 +                error(INTERNAL, "Bad returned index in process_ray");
59 +        colp = &distarr[r->rno * 3];
60 +        addcolor(colp, r->rcol);
61 +        return(1);
62 + }
63 +
64 +
65 + static void
66 + raysamp(        /* queue a ray sample */
67 +        int  ndx,
68 +        FVECT  org,
69 +        FVECT  dir
70 + )
71 + {
72 +        RAY     myRay;
73 +        int     rv;
74 +
75 +        if ((ndx < 0) | (ndx >= distsiz))
76 +                error(INTERNAL, "Bad index in raysamp");
77 +        VCOPY(myRay.rorg, org);
78 +        VCOPY(myRay.rdir, dir);
79 +        myRay.rmax = .0;
80 +        rayorigin(&myRay, PRIMARY, NULL, NULL);
81 +        myRay.rno = ndx;
82 +                                        /* queue ray, check result */
83 +        process_ray(&myRay, ray_pqueue(&myRay));
84 + }
85 +
86 +
87 + static void
88 + rayclean()                      /* finish all pending rays */
89 + {
90 +        RAY     myRay;
91 +
92 +        while (process_ray(&myRay, ray_presult(&myRay, 0)))
93 +                ;
94 + }
95 +
96 +
97 + int /* XXX type conflict with otypes.h */
98 + my_default(     /* default illum action */
99 +        OBJREC  *ob,
100 +        struct illum_args  *il,
101 +        char  *nm
102 + )
103 + {
104          sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"",
105                          nm, ofun[ob->otype].funame, ob->oname);
106          error(WARNING, errmsg);
107 <        if (!(il->flags & IL_LIGHT))
108 <                printobj(il->altmat, ob);
107 >        printobj(il->altmat, ob);
108 >        return(1);
109   }
110  
111  
112 < o_face(ob, il, rt, nm)          /* make an illum face */
113 < OBJREC  *ob;
114 < struct illum_args  *il;
115 < struct rtproc  *rt;
116 < char  *nm;
112 > int
113 > my_face(                /* make an illum face */
114 >        OBJREC  *ob,
115 >        struct illum_args  *il,
116 >        char  *nm
117 > )
118   {
119   #define MAXMISS         (5*n*il->nsamps)
120 <        int  dim[4];
121 <        int  n, nalt, nazi;
122 <        float  *distarr;
44 <        double  r1, r2;
120 >        int  dim[3];
121 >        int  n, nalt, nazi, h;
122 >        double  sp[2], r1, r2;
123          FVECT  dn, org, dir;
124          FVECT  u, v;
125          double  ur[2], vr[2];
# Line 52 | Line 130 | char  *nm;
130          fa = getface(ob);
131          if (fa->area == 0.0) {
132                  freeface(ob);
133 <                o_default(ob, il, rt, nm);
56 <                return;
133 >                return(o_default(ob, il, nm));
134          }
135                                  /* set up sampling */
136 <        n = PI * il->sampdens;
137 <        nalt = sqrt(n/PI) + .5;
138 <        nazi = PI*nalt + .5;
136 >        if (il->sampdens <= 0)
137 >                nalt = nazi = 1;
138 >        else {
139 >                n = PI * il->sampdens;
140 >                nalt = sqrt(n/PI) + .5;
141 >                nazi = PI*nalt + .5;
142 >        }
143          n = nalt*nazi;
144 <        distarr = (float *)calloc(n, 3*sizeof(float));
145 <        if (distarr == NULL)
146 <                error(SYSTEM, "out of memory in o_face");
147 <        mkaxes(u, v, fa->norm);
144 >        newdist(n);
145 >                                /* take first edge longer than sqrt(area) */
146 >        for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) {
147 >                u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0];
148 >                u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1];
149 >                u[2] = VERTEX(fa,i)[2] - VERTEX(fa,j)[2];
150 >                if ((r1 = DOT(u,u)) >= fa->area-FTINY)
151 >                        break;
152 >        }
153 >        if (i < fa->nv) {       /* got one! -- let's align our axes */
154 >                r2 = 1.0/sqrt(r1);
155 >                u[0] *= r2; u[1] *= r2; u[2] *= r2;
156 >                fcross(v, fa->norm, u);
157 >        } else                  /* oh well, we'll just have to wing it */
158 >                mkaxes(u, v, fa->norm);
159 >                                /* now, find limits in (u,v) coordinates */
160          ur[0] = vr[0] = FHUGE;
161          ur[1] = vr[1] = -FHUGE;
162          for (i = 0; i < fa->nv; i++) {
# Line 81 | Line 174 | char  *nm;
174              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
175                  for (i = 0; i < il->nsamps; i++) {
176                                          /* random direction */
177 <                    dim[3] = 1;
178 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
179 <                    dim[3] = 2;
180 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nazi;
177 >                    h = ilhash(dim, 3) + i;
178 >                    multisamp(sp, 2, urand(h));
179 >                    r1 = (dim[1] + sp[0])/nalt;
180 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
181                      flatdir(dn, r1, r2);
182                      for (j = 0; j < 3; j++)
183                          dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j];
184                                          /* random location */
185                      do {
186 <                        dim[3] = 3;
187 <                        r1 = ur[0] + (ur[1]-ur[0]) *
188 <                                        urand(urind(ilhash(dim,4),i+nmisses));
96 <                        dim[3] = 4;
97 <                        r2 = vr[0] + (vr[1]-vr[0]) *
98 <                                        urand(urind(ilhash(dim,4),i+nmisses));
186 >                        multisamp(sp, 2, urand(h+4862+nmisses));
187 >                        r1 = ur[0] + (ur[1]-ur[0]) * sp[0];
188 >                        r2 = vr[0] + (vr[1]-vr[0]) * sp[1];
189                          for (j = 0; j < 3; j++)
190                              org[j] = r1*u[j] + r2*v[j]
191                                          + fa->offset*fa->norm[j];
192                      } while (!inface(org, fa) && nmisses++ < MAXMISS);
193                      if (nmisses > MAXMISS) {
194                          objerror(ob, WARNING, "bad aspect");
195 <                        rt->nrays = 0;
195 >                        rayclean();
196                          freeface(ob);
197 <                        free((char *)distarr);
198 <                        o_default(ob, il, rt, nm);
109 <                        return;
197 >                        free((void *)distarr);
198 >                        return(o_default(ob, il, nm));
199                      }
200                      for (j = 0; j < 3; j++)
201                          org[j] += .001*fa->norm[j];
202                                          /* send sample */
203 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
203 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
204                  }
205 <        rayflush(rt);
206 <                                /* write out the face w/ distribution */
207 <        flatout(il, distarr, nalt, nazi, u, v, fa->norm);
208 <        illumout(il, ob);
205 >        rayclean();
206 >                                /* write out the face and its distribution */
207 >        if (average(il, distarr, nalt*nazi)) {
208 >                if (il->sampdens > 0)
209 >                        flatout(il, distarr, nalt, nazi, u, v, fa->norm);
210 >                illumout(il, ob);
211 >        } else
212 >                printobj(il->altmat, ob);
213                                  /* clean up */
214          freeface(ob);
215 <        free((char *)distarr);
215 >        return(0);
216   #undef MAXMISS
217   }
218  
219  
220 < o_sphere(ob, il, rt, nm)        /* make an illum sphere */
221 < register OBJREC  *ob;
222 < struct illum_args  *il;
223 < struct rtproc  *rt;
224 < char  *nm;
220 > int
221 > my_sphere(      /* make an illum sphere */
222 >        register OBJREC  *ob,
223 >        struct illum_args  *il,
224 >        char  *nm
225 > )
226   {
227 <        int  dim[4];
227 >        int  dim[3];
228          int  n, nalt, nazi;
229 <        float  *distarr;
136 <        double  r1, r2, r3;
229 >        double  sp[4], r1, r2, r3;
230          FVECT  org, dir;
231          FVECT  u, v;
232          register int  i, j;
# Line 141 | Line 234 | char  *nm;
234          if (ob->oargs.nfargs != 4)
235                  objerror(ob, USER, "bad # of arguments");
236                                  /* set up sampling */
237 <        n = 4.*PI * il->sampdens;
238 <        nalt = sqrt(n/PI) + .5;
239 <        nazi = PI*nalt + .5;
237 >        if (il->sampdens <= 0)
238 >                nalt = nazi = 1;
239 >        else {
240 >                n = 4.*PI * il->sampdens;
241 >                nalt = sqrt(2./PI*n) + .5;
242 >                nazi = PI/2.*nalt + .5;
243 >        }
244          n = nalt*nazi;
245 <        distarr = (float *)calloc(n, 3*sizeof(float));
149 <        if (distarr == NULL)
150 <                error(SYSTEM, "out of memory in o_sphere");
245 >        newdist(n);
246          dim[0] = random();
247                                  /* sample sphere */
248          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
249              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
250                  for (i = 0; i < il->nsamps; i++) {
251 +                                        /* next sample point */
252 +                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
253                                          /* random direction */
254 <                    dim[3] = 1;
255 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
159 <                    dim[3] = 2;
160 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nazi;
254 >                    r1 = (dim[1] + sp[0])/nalt;
255 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
256                      rounddir(dir, r1, r2);
257                                          /* random location */
258                      mkaxes(u, v, dir);          /* yuck! */
259 <                    dim[3] = 3;
260 <                    r3 = sqrt(urand(urind(ilhash(dim,4),i)));
166 <                    dim[3] = 4;
167 <                    r2 = 2.*PI*urand(urind(ilhash(dim,4),i));
259 >                    r3 = sqrt(sp[2]);
260 >                    r2 = 2.*PI*sp[3];
261                      r1 = r3*ob->oargs.farg[3]*cos(r2);
262                      r2 = r3*ob->oargs.farg[3]*sin(r2);
263                      r3 = ob->oargs.farg[3]*sqrt(1.01-r3*r3);
# Line 174 | Line 267 | char  *nm;
267                          dir[j] = -dir[j];
268                      }
269                                          /* send sample */
270 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
270 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
271                  }
272 <        rayflush(rt);
273 <                                /* write out the sphere w/ distribution */
274 <        roundout(il, distarr, nalt, nazi);
275 <        illumout(il, ob);
272 >        rayclean();
273 >                                /* write out the sphere and its distribution */
274 >        if (average(il, distarr, nalt*nazi)) {
275 >                if (il->sampdens > 0)
276 >                        roundout(il, distarr, nalt, nazi);
277 >                else
278 >                        objerror(ob, WARNING, "diffuse distribution");
279 >                illumout(il, ob);
280 >        } else
281 >                printobj(il->altmat, ob);
282                                  /* clean up */
283 <        free((char *)distarr);
283 >        return(1);
284   }
285  
286  
287 < o_ring(ob, il, rt, nm)          /* make an illum ring */
288 < OBJREC  *ob;
289 < struct illum_args  *il;
290 < struct rtproc  *rt;
291 < char  *nm;
287 > int
288 > my_ring(                /* make an illum ring */
289 >        OBJREC  *ob,
290 >        struct illum_args  *il,
291 >        char  *nm
292 > )
293   {
294 <        int  dim[4];
294 >        int  dim[3];
295          int  n, nalt, nazi;
296 <        float  *distarr;
197 <        double  r1, r2, r3;
296 >        double  sp[4], r1, r2, r3;
297          FVECT  dn, org, dir;
298          FVECT  u, v;
299          register CONE  *co;
# Line 202 | Line 301 | char  *nm;
301                                  /* get/check arguments */
302          co = getcone(ob, 0);
303                                  /* set up sampling */
304 <        n = PI * il->sampdens;
305 <        nalt = sqrt(n/PI) + .5;
306 <        nazi = PI*nalt + .5;
304 >        if (il->sampdens <= 0)
305 >                nalt = nazi = 1;
306 >        else {
307 >                n = PI * il->sampdens;
308 >                nalt = sqrt(n/PI) + .5;
309 >                nazi = PI*nalt + .5;
310 >        }
311          n = nalt*nazi;
312 <        distarr = (float *)calloc(n, 3*sizeof(float));
210 <        if (distarr == NULL)
211 <                error(SYSTEM, "out of memory in o_ring");
312 >        newdist(n);
313          mkaxes(u, v, co->ad);
314          dim[0] = random();
315                                  /* sample disk */
316          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
317              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
318                  for (i = 0; i < il->nsamps; i++) {
319 +                                        /* next sample point */
320 +                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
321                                          /* random direction */
322 <                    dim[3] = 1;
323 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
221 <                    dim[3] = 2;
222 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nalt;
322 >                    r1 = (dim[1] + sp[0])/nalt;
323 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
324                      flatdir(dn, r1, r2);
325                      for (j = 0; j < 3; j++)
326                          dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j];
327                                          /* random location */
227                    dim[3] = 3;
328                      r3 = sqrt(CO_R0(co)*CO_R0(co) +
329 <                                urand(urind(ilhash(dim,4),i))*
330 <                                (CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
231 <                    dim[3] = 4;
232 <                    r2 = 2.*PI*urand(urind(ilhash(dim,4),i));
329 >                            sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
330 >                    r2 = 2.*PI*sp[3];
331                      r1 = r3*cos(r2);
332                      r2 = r3*sin(r2);
333                      for (j = 0; j < 3; j++)
334 <                        org[j] = CO_P0(co)[j] + r1*u[j] + r1*v[j] +
334 >                        org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] +
335                                          .001*co->ad[j];
336  
337                                          /* send sample */
338 <                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
338 >                    raysamp(dim[1]*nazi+dim[2], org, dir);
339                  }
340 <        rayflush(rt);
341 <                                /* write out the ring w/ distribution */
342 <        flatout(il, distarr, nalt, nazi, u, v, co->ad);
343 <        illumout(il, ob);
340 >        rayclean();
341 >                                /* write out the ring and its distribution */
342 >        if (average(il, distarr, nalt*nazi)) {
343 >                if (il->sampdens > 0)
344 >                        flatout(il, distarr, nalt, nazi, u, v, co->ad);
345 >                illumout(il, ob);
346 >        } else
347 >                printobj(il->altmat, ob);
348                                  /* clean up */
349          freecone(ob);
350 <        free((char *)distarr);
350 >        return(1);
351   }
352  
353  
354 < raysamp(res, org, dir, rt)      /* compute a ray sample */
355 < float  res[3];
356 < FVECT  org, dir;
357 < register struct rtproc  *rt;
354 > static void
355 > mkaxes(                 /* compute u and v to go with n */
356 >        FVECT  u,
357 >        FVECT  v,
358 >        FVECT  n
359 > )
360   {
257        register float  *fp;
258
259        if (rt->nrays == rt->bsiz)
260                rayflush(rt);
261        rt->dest[rt->nrays] = res;
262        fp = rt->buf + 6*rt->nrays++;
263        *fp++ = org[0]; *fp++ = org[1]; *fp++ = org[2];
264        *fp++ = dir[0]; *fp++ = dir[1]; *fp = dir[2];
265 }
266
267
268 rayflush(rt)                    /* flush buffered rays */
269 register struct rtproc  *rt;
270 {
361          register int  i;
362  
273        if (rt->nrays <= 0)
274                return;
275        i = 6*rt->nrays + 3;
276        rt->buf[i++] = 0.; rt->buf[i++] = 0.; rt->buf[i] = 0.;
277        if ( process(rt->pd, (char *)rt->buf, (char *)rt->buf,
278                        3*sizeof(float)*rt->nrays,
279                        6*sizeof(float)*(rt->nrays+1)) <
280                        3*sizeof(float)*rt->nrays )
281                error(SYSTEM, "error reading from rtrace process");
282        i = rt->nrays;
283        while (i--) {
284                rt->dest[i][0] += rt->buf[3*i];
285                rt->dest[i][1] += rt->buf[3*i+1];
286                rt->dest[i][2] += rt->buf[3*i+2];
287        }
288        rt->nrays = 0;
289 }
290
291
292 mkaxes(u, v, n)                 /* compute u and v to go with n */
293 FVECT  u, v, n;
294 {
295        register int  i;
296
363          v[0] = v[1] = v[2] = 0.0;
364          for (i = 0; i < 3; i++)
365                  if (n[i] < 0.6 && n[i] > -0.6)
# Line 305 | Line 371 | FVECT  u, v, n;
371   }
372  
373  
374 < rounddir(dv, alt, azi)          /* compute uniform spherical direction */
375 < register FVECT  dv;
376 < double  alt, azi;
374 > static void
375 > rounddir(               /* compute uniform spherical direction */
376 >        register FVECT  dv,
377 >        double  alt,
378 >        double  azi
379 > )
380   {
381          double  d1, d2;
382  
# Line 319 | Line 388 | double  alt, azi;
388   }
389  
390  
391 < flatdir(dv, alt, azi)           /* compute uniform hemispherical direction */
392 < register FVECT  dv;
393 < double  alt, azi;
391 > static void
392 > flatdir(                /* compute uniform hemispherical direction */
393 >        register FVECT  dv,
394 >        double  alt,
395 >        double  azi
396 > )
397   {
398          double  d1, d2;
399  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines