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.3 by greg, Wed Jul 24 13:32:05 1991 UTC vs.
Revision 2.4 by greg, Thu Aug 13 10:03:19 1992 UTC

# Line 5 | Line 5 | static char SCCSid[] = "$SunId$ LBL";
5   #endif
6  
7   /*
8 < * Routines to do the actual calcultion and output for mkillum
8 > * Routines to do the actual calculation for mkillum
9   */
10  
11   #include  "mkillum.h"
# Line 17 | Line 17 | static char SCCSid[] = "$SunId$ LBL";
17   #include  "random.h"
18  
19  
20 printobj(mod, obj)              /* print out an object */
21 char  *mod;
22 register OBJREC  *obj;
23 {
24        register int  i;
25
26        printf("\n%s %s %s", mod, ofun[obj->otype].funame, obj->oname);
27        printf("\n%d", obj->oargs.nsargs);
28        for (i = 0; i < obj->oargs.nsargs; i++)
29                printf(" %s", obj->oargs.sarg[i]);
30 #ifdef  IARGS
31        printf("\n%d", obj->oargs.niargs);
32        for (i = 0; i < obj->oargs.niargs; i++)
33                printf(" %d", obj->oargs.iarg[i]);
34 #else
35        printf("\n0");
36 #endif
37        printf("\n%d", obj->oargs.nfargs);
38        for (i = 0; i < obj->oargs.nfargs; i++) {
39                if (i%3 == 0)
40                        putchar('\n');
41                printf(" %18.12g", obj->oargs.farg[i]);
42        }
43        putchar('\n');
44 }
45
46
20   o_default(ob, il, rt, nm)       /* default illum action */
21   OBJREC  *ob;
22   struct illum_args  *il;
# Line 53 | Line 26 | char  *nm;
26          sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"",
27                          nm, ofun[ob->otype].funame, ob->oname);
28          error(WARNING, errmsg);
29 <        if (!(il->flags & IL_LIGHT))
57 <                printobj(il->altname, ob);
29 >        printobj(il->altmat, ob);
30   }
31  
32  
# Line 65 | Line 37 | struct rtproc  *rt;
37   char  *nm;
38   {
39   #define MAXMISS         (5*n*il->nsamps)
40 <        int  dim[4];
41 <        int  n, nalt, nazi;
40 >        int  dim[3];
41 >        int  n, nalt, nazi, h;
42          float  *distarr;
43 <        double  r1, r2;
44 <        FVECT  dn, pos, dir;
43 >        double  sp[2], r1, r2;
44 >        FVECT  dn, org, dir;
45          FVECT  u, v;
46          double  ur[2], vr[2];
47          int  nmisses;
# Line 83 | Line 55 | char  *nm;
55                  return;
56          }
57                                  /* set up sampling */
58 <        n = PI * il->sampdens;
59 <        nalt = sqrt(n/PI) + .5;
60 <        nazi = PI*nalt + .5;
58 >        if (il->sampdens <= 0)
59 >                nalt = nazi = 1;
60 >        else {
61 >                n = PI * il->sampdens;
62 >                nalt = sqrt(n/PI) + .5;
63 >                nazi = PI*nalt + .5;
64 >        }
65          n = nalt*nazi;
66          distarr = (float *)calloc(n, 3*sizeof(float));
67          if (distarr == NULL)
68                  error(SYSTEM, "out of memory in o_face");
69 <        mkaxes(u, v, fa->norm);
69 >                                /* take first edge longer than sqrt(area) */
70 >        for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) {
71 >                u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0];
72 >                u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1];
73 >                u[2] = VERTEX(fa,i)[2] - VERTEX(fa,j)[2];
74 >                if (DOT(u,u) >= fa->area-FTINY)
75 >                        break;
76 >        }
77 >        if (i < fa->nv) {       /* got one! -- let's align our axes */
78 >                normalize(u);
79 >                fcross(v, fa->norm, u);
80 >        } else                  /* oh well, we'll just have to wing it */
81 >                mkaxes(u, v, fa->norm);
82 >                                /* now, find limits in (u,v) coordinates */
83          ur[0] = vr[0] = FHUGE;
84          ur[1] = vr[1] = -FHUGE;
85          for (i = 0; i < fa->nv; i++) {
# Line 108 | Line 97 | char  *nm;
97              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
98                  for (i = 0; i < il->nsamps; i++) {
99                                          /* random direction */
100 <                    dim[3] = 1;
101 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
102 <                    dim[3] = 2;
103 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nalt;
100 >                    h = ilhash(dim, 3) + i;
101 >                    multisamp(sp, 2, urand(h));
102 >                    r1 = (dim[1] + sp[0])/nalt;
103 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
104                      flatdir(dn, r1, r2);
105                      for (j = 0; j < 3; j++)
106 <                        dir[j] = dn[0]*u[j] + dn[1]*v[j] - dn[2]*fa->norm[j];
106 >                        dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j];
107                                          /* random location */
108                      do {
109 <                        dim[3] = 3;
110 <                        r1 = ur[0] +
111 <                                (ur[1]-ur[0])*urand(urind(ilhash(dim,4),i));
123 <                        dim[3] = 4;
124 <                        r2 = vr[0] +
125 <                                (vr[1]-vr[0])*urand(urind(ilhash(dim,4),i));
109 >                        multisamp(sp, 2, urand(h+4862+nmisses));
110 >                        r1 = ur[0] + (ur[1]-ur[0]) * sp[0];
111 >                        r2 = vr[0] + (vr[1]-vr[0]) * sp[1];
112                          for (j = 0; j < 3; j++)
113                              org[j] = r1*u[j] + r2*v[j]
114                                          + fa->offset*fa->norm[j];
# Line 138 | Line 124 | char  *nm;
124                      for (j = 0; j < 3; j++)
125                          org[j] += .001*fa->norm[j];
126                                          /* send sample */
127 <                    raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt);
127 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
128                  }
129          rayflush(rt);
130 <                                /* write out the distribution */
131 <        flatdist(distarr, nalt, nazi, il, ob);
130 >                                /* write out the face and its distribution */
131 >        if (average(il, distarr, nalt*nazi)) {
132 >                if (il->sampdens > 0)
133 >                        flatout(il, distarr, nalt, nazi, u, v, fa->norm);
134 >                illumout(il, ob);
135 >        } else
136 >                printobj(il->altmat, ob);
137                                  /* clean up */
138          freeface(ob);
139          free((char *)distarr);
# Line 156 | Line 147 | struct illum_args  *il;
147   struct rtproc  *rt;
148   char  *nm;
149   {
150 <        int  dim[4];
150 >        int  dim[3];
151          int  n, nalt, nazi;
152          float  *distarr;
153 <        double  r1, r2;
154 <        FVECT  pos, dir;
153 >        double  sp[4], r1, r2, r3;
154 >        FVECT  org, dir;
155          FVECT  u, v;
156          register int  i, j;
157                                  /* check arguments */
158          if (ob->oargs.nfargs != 4)
159                  objerror(ob, USER, "bad # of arguments");
160                                  /* set up sampling */
161 <        n = 4.*PI * il->sampdens;
162 <        nalt = sqrt(n/PI) + .5;
163 <        nazi = PI*nalt + .5;
161 >        if (il->sampdens <= 0)
162 >                nalt = nazi = 1;
163 >        else {
164 >                n = 4.*PI * il->sampdens;
165 >                nalt = sqrt(n/PI) + .5;
166 >                nazi = PI*nalt + .5;
167 >        }
168          n = nalt*nazi;
169          distarr = (float *)calloc(n, 3*sizeof(float));
170          if (distarr == NULL)
# Line 179 | Line 174 | char  *nm;
174          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
175              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
176                  for (i = 0; i < il->nsamps; i++) {
177 +                                        /* next sample point */
178 +                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
179                                          /* random direction */
180 <                    dim[3] = 1;
181 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
185 <                    dim[3] = 2;
186 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nalt;
180 >                    r1 = (dim[1] + sp[0])/nalt;
181 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
182                      rounddir(dir, r1, r2);
183                                          /* random location */
184                      mkaxes(u, v, dir);          /* yuck! */
185 <                    dim[3] = 3;
186 <                    r1 = sqrt(urand(urind(ilhash(dim,4),i)));
187 <                    dim[3] = 4;
188 <                    r2 = 2.*PI*urand(urind(ilhash(dim,4),i));
189 <                    for (j = 0; j < 3; j++)
190 <                        org[j] = obj->oargs.farg[j] + obj->oargs.farg[3] *
191 <                                        ( r1*cos(r2)*u[j] + r1*sin(r2)*v[j]
192 <                                                - sqrt(1.01-r1*r1)*dir[j] );
193 <
185 >                    r3 = sqrt(sp[2]);
186 >                    r2 = 2.*PI*sp[3];
187 >                    r1 = r3*ob->oargs.farg[3]*cos(r2);
188 >                    r2 = r3*ob->oargs.farg[3]*sin(r2);
189 >                    r3 = ob->oargs.farg[3]*sqrt(1.01-r3*r3);
190 >                    for (j = 0; j < 3; j++) {
191 >                        org[j] = ob->oargs.farg[j] + r1*u[j] + r2*v[j] +
192 >                                        r3*dir[j];
193 >                        dir[j] = -dir[j];
194 >                    }
195                                          /* send sample */
196 <                    raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt);
196 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
197                  }
198          rayflush(rt);
199 <                                /* write out the distribution */
200 <        rounddist(distarr, nalt, nazi, il, ob);
199 >                                /* write out the sphere and its distribution */
200 >        if (average(il, distarr, nalt*nazi)) {
201 >                if (il->sampdens > 0)
202 >                        roundout(il, distarr, nalt, nazi);
203 >                else
204 >                        objerror(ob, WARNING, "diffuse distribution");
205 >                illumout(il, ob);
206 >        } else
207 >                printobj(il->altmat, ob);
208                                  /* clean up */
209          free((char *)distarr);
210   }
# Line 213 | Line 216 | struct illum_args  *il;
216   struct rtproc  *rt;
217   char  *nm;
218   {
219 <        int  dim[4];
219 >        int  dim[3];
220          int  n, nalt, nazi;
221          float  *distarr;
222 <        double  r1, r2;
223 <        FVECT  dn, pos, dir;
222 >        double  sp[4], r1, r2, r3;
223 >        FVECT  dn, org, dir;
224          FVECT  u, v;
225          register CONE  *co;
226          register int  i, j;
227                                  /* get/check arguments */
228          co = getcone(ob, 0);
229                                  /* set up sampling */
230 <        n = PI * il->sampdens;
231 <        nalt = sqrt(n/PI) + .5;
232 <        nazi = PI*nalt + .5;
230 >        if (il->sampdens <= 0)
231 >                nalt = nazi = 1;
232 >        else {
233 >                n = PI * il->sampdens;
234 >                nalt = sqrt(n/PI) + .5;
235 >                nazi = PI*nalt + .5;
236 >        }
237          n = nalt*nazi;
238          distarr = (float *)calloc(n, 3*sizeof(float));
239          if (distarr == NULL)
# Line 237 | Line 244 | char  *nm;
244          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
245              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
246                  for (i = 0; i < il->nsamps; i++) {
247 +                                        /* next sample point */
248 +                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
249                                          /* random direction */
250 <                    dim[3] = 1;
251 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
243 <                    dim[3] = 2;
244 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nalt;
250 >                    r1 = (dim[1] + sp[0])/nalt;
251 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
252                      flatdir(dn, r1, r2);
253                      for (j = 0; j < 3; j++)
254 <                        dir[j] = dn[0]*u[j] + dn[1]*v[j] - dn[2]*co->ad[j];
254 >                        dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j];
255                                          /* random location */
256 <                    dim[3] = 3;
257 <                    r1 = sqrt(CO_R0(co)*CO_R0(co) +
258 <                                urand(urind(ilhash(dim,4),i))*
259 <                                (CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
260 <                    dim[3] = 4;
254 <                    r2 = 2.*PI*urand(urind(ilhash(dim,4),i));
256 >                    r3 = sqrt(CO_R0(co)*CO_R0(co) +
257 >                            sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
258 >                    r2 = 2.*PI*sp[3];
259 >                    r1 = r3*cos(r2);
260 >                    r2 = r3*sin(r2);
261                      for (j = 0; j < 3; j++)
262 <                        org[j] = CO_P0(co)[j] +
263 <                                        r1*cos(r2)*u[j] + r1*sin(r2)*v[j]
258 <                                        + .001*co->ad[j];
262 >                        org[j] = CO_P0(co)[j] + r1*u[j] + r1*v[j] +
263 >                                        .001*co->ad[j];
264  
265                                          /* send sample */
266 <                    raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt);
266 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
267                  }
268          rayflush(rt);
269 <                                /* write out the distribution */
270 <        flatdist(distarr, nalt, nazi, il, ob);
269 >                                /* write out the ring and its distribution */
270 >        if (average(il, distarr, nalt*nazi)) {
271 >                if (il->sampdens > 0)
272 >                        flatout(il, distarr, nalt, nazi, u, v, co->ad);
273 >                illumout(il, ob);
274 >        } else
275 >                printobj(il->altmat, ob);
276                                  /* clean up */
277          freecone(ob);
278          free((char *)distarr);
# Line 292 | Line 302 | register struct rtproc  *rt;
302  
303          if (rt->nrays <= 0)
304                  return;
305 <        i = 6*rt->nrays + 3;
306 <        rt->buf[i++] = 0.; rt->buf[i++] = 0.; rt->buf[i] = 0.;
305 >        bzero(rt->buf+6*rt->nrays, 6*sizeof(float));
306 >        errno = 0;
307          if ( process(rt->pd, (char *)rt->buf, (char *)rt->buf,
308                          3*sizeof(float)*rt->nrays,
309                          6*sizeof(float)*(rt->nrays+1)) <
# Line 306 | Line 316 | register struct rtproc  *rt;
316                  rt->dest[i][2] += rt->buf[3*i+2];
317          }
318          rt->nrays = 0;
319 + }
320 +
321 +
322 + mkaxes(u, v, n)                 /* compute u and v to go with n */
323 + FVECT  u, v, n;
324 + {
325 +        register int  i;
326 +
327 +        v[0] = v[1] = v[2] = 0.0;
328 +        for (i = 0; i < 3; i++)
329 +                if (n[i] < 0.6 && n[i] > -0.6)
330 +                        break;
331 +        v[i] = 1.0;
332 +        fcross(u, v, n);
333 +        normalize(u);
334 +        fcross(v, n, u);
335 + }
336 +
337 +
338 + rounddir(dv, alt, azi)          /* compute uniform spherical direction */
339 + register FVECT  dv;
340 + double  alt, azi;
341 + {
342 +        double  d1, d2;
343 +
344 +        dv[2] = 1. - 2.*alt;
345 +        d1 = sqrt(1. - dv[2]*dv[2]);
346 +        d2 = 2.*PI * azi;
347 +        dv[0] = d1*cos(d2);
348 +        dv[1] = d1*sin(d2);
349 + }
350 +
351 +
352 + flatdir(dv, alt, azi)           /* compute uniform hemispherical direction */
353 + register FVECT  dv;
354 + double  alt, azi;
355 + {
356 +        double  d1, d2;
357 +
358 +        d1 = sqrt(alt);
359 +        d2 = 2.*PI * azi;
360 +        dv[0] = d1*cos(d2);
361 +        dv[1] = d1*sin(d2);
362 +        dv[2] = sqrt(1. - alt);
363   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines