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.2 by greg, Wed Jul 24 12:22:05 1991 UTC vs.
Revision 2.10 by schorsch, Thu Jun 26 00:58:09 2003 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 calcultion and output for mkillum
5 > * Routines to do the actual calculation for mkillum
6   */
7  
8   #include  "mkillum.h"
12
9   #include  "face.h"
14
10   #include  "cone.h"
16
11   #include  "random.h"
12  
13  
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
14   o_default(ob, il, rt, nm)       /* default illum action */
15   OBJREC  *ob;
16   struct illum_args  *il;
# Line 53 | Line 20 | char  *nm;
20          sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"",
21                          nm, ofun[ob->otype].funame, ob->oname);
22          error(WARNING, errmsg);
23 <        if (!(il->flags & IL_LIGHT))
57 <                printobj(il->altname, ob);
23 >        printobj(il->altmat, ob);
24   }
25  
26  
# Line 64 | Line 30 | struct illum_args  *il;
30   struct rtproc  *rt;
31   char  *nm;
32   {
33 + #define MAXMISS         (5*n*il->nsamps)
34 +        int  dim[3];
35 +        int  n, nalt, nazi, h;
36 +        float  *distarr;
37 +        double  sp[2], r1, r2;
38 +        FVECT  dn, org, dir;
39 +        FVECT  u, v;
40 +        double  ur[2], vr[2];
41 +        int  nmisses;
42 +        register FACE  *fa;
43 +        register int  i, j;
44 +                                /* get/check arguments */
45 +        fa = getface(ob);
46 +        if (fa->area == 0.0) {
47 +                freeface(ob);
48 +                o_default(ob, il, rt, nm);
49 +                return;
50 +        }
51 +                                /* set up sampling */
52 +        if (il->sampdens <= 0)
53 +                nalt = nazi = 1;
54 +        else {
55 +                n = PI * il->sampdens;
56 +                nalt = sqrt(n/PI) + .5;
57 +                nazi = PI*nalt + .5;
58 +        }
59 +        n = nalt*nazi;
60 +        distarr = (float *)calloc(n, 3*sizeof(float));
61 +        if (distarr == NULL)
62 +                error(SYSTEM, "out of memory in o_face");
63 +                                /* take first edge longer than sqrt(area) */
64 +        for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) {
65 +                u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0];
66 +                u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1];
67 +                u[2] = VERTEX(fa,i)[2] - VERTEX(fa,j)[2];
68 +                if ((r1 = DOT(u,u)) >= fa->area-FTINY)
69 +                        break;
70 +        }
71 +        if (i < fa->nv) {       /* got one! -- let's align our axes */
72 +                r2 = 1.0/sqrt(r1);
73 +                u[0] *= r2; u[1] *= r2; u[2] *= r2;
74 +                fcross(v, fa->norm, u);
75 +        } else                  /* oh well, we'll just have to wing it */
76 +                mkaxes(u, v, fa->norm);
77 +                                /* now, find limits in (u,v) coordinates */
78 +        ur[0] = vr[0] = FHUGE;
79 +        ur[1] = vr[1] = -FHUGE;
80 +        for (i = 0; i < fa->nv; i++) {
81 +                r1 = DOT(VERTEX(fa,i),u);
82 +                if (r1 < ur[0]) ur[0] = r1;
83 +                if (r1 > ur[1]) ur[1] = r1;
84 +                r2 = DOT(VERTEX(fa,i),v);
85 +                if (r2 < vr[0]) vr[0] = r2;
86 +                if (r2 > vr[1]) vr[1] = r2;
87 +        }
88 +        dim[0] = random();
89 +                                /* sample polygon */
90 +        nmisses = 0;
91 +        for (dim[1] = 0; dim[1] < nalt; dim[1]++)
92 +            for (dim[2] = 0; dim[2] < nazi; dim[2]++)
93 +                for (i = 0; i < il->nsamps; i++) {
94 +                                        /* random direction */
95 +                    h = ilhash(dim, 3) + i;
96 +                    multisamp(sp, 2, urand(h));
97 +                    r1 = (dim[1] + sp[0])/nalt;
98 +                    r2 = (dim[2] + sp[1] - .5)/nazi;
99 +                    flatdir(dn, r1, r2);
100 +                    for (j = 0; j < 3; j++)
101 +                        dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j];
102 +                                        /* random location */
103 +                    do {
104 +                        multisamp(sp, 2, urand(h+4862+nmisses));
105 +                        r1 = ur[0] + (ur[1]-ur[0]) * sp[0];
106 +                        r2 = vr[0] + (vr[1]-vr[0]) * sp[1];
107 +                        for (j = 0; j < 3; j++)
108 +                            org[j] = r1*u[j] + r2*v[j]
109 +                                        + fa->offset*fa->norm[j];
110 +                    } while (!inface(org, fa) && nmisses++ < MAXMISS);
111 +                    if (nmisses > MAXMISS) {
112 +                        objerror(ob, WARNING, "bad aspect");
113 +                        rt->nrays = 0;
114 +                        freeface(ob);
115 +                        free((void *)distarr);
116 +                        o_default(ob, il, rt, nm);
117 +                        return;
118 +                    }
119 +                    for (j = 0; j < 3; j++)
120 +                        org[j] += .001*fa->norm[j];
121 +                                        /* send sample */
122 +                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
123 +                }
124 +        rayflush(rt);
125 +                                /* write out the face and its distribution */
126 +        if (average(il, distarr, nalt*nazi)) {
127 +                if (il->sampdens > 0)
128 +                        flatout(il, distarr, nalt, nazi, u, v, fa->norm);
129 +                illumout(il, ob);
130 +        } else
131 +                printobj(il->altmat, ob);
132 +                                /* clean up */
133 +        freeface(ob);
134 +        free((void *)distarr);
135 + #undef MAXMISS
136   }
137  
138  
139   o_sphere(ob, il, rt, nm)        /* make an illum sphere */
140 < OBJREC  *ob;
140 > register OBJREC  *ob;
141   struct illum_args  *il;
142   struct rtproc  *rt;
143   char  *nm;
144   {
145 <        int  dim[4];
145 >        int  dim[3];
146          int  n, nalt, nazi;
147          float  *distarr;
148 <        double  r1, r2;
149 <        FVECT  pos, dir;
148 >        double  sp[4], r1, r2, r3;
149 >        FVECT  org, dir;
150          FVECT  u, v;
151          register int  i, j;
152                                  /* check arguments */
153          if (ob->oargs.nfargs != 4)
154                  objerror(ob, USER, "bad # of arguments");
155                                  /* set up sampling */
156 <        n = 4.*PI * il->sampdens;
157 <        nalt = sqrt(n/PI) + .5;
158 <        nazi = PI*nalt + .5;
156 >        if (il->sampdens <= 0)
157 >                nalt = nazi = 1;
158 >        else {
159 >                n = 4.*PI * il->sampdens;
160 >                nalt = sqrt(2./PI*n) + .5;
161 >                nazi = PI/2.*nalt + .5;
162 >        }
163          n = nalt*nazi;
164          distarr = (float *)calloc(n, 3*sizeof(float));
165          if (distarr == NULL)
# Line 96 | Line 169 | char  *nm;
169          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
170              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
171                  for (i = 0; i < il->nsamps; i++) {
172 +                                        /* next sample point */
173 +                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
174                                          /* random direction */
175 <                    dim[3] = 1;
176 <                    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;
175 >                    r1 = (dim[1] + sp[0])/nalt;
176 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
177                      rounddir(dir, r1, r2);
178                                          /* random location */
179                      mkaxes(u, v, dir);          /* yuck! */
180 <                    dim[3] = 3;
181 <                    r1 = sqrt(urand(urind(ilhash(dim,4),i)));
182 <                    dim[3] = 4;
183 <                    r2 = 2.*PI*urand(urind(ilhash(dim,4),i));
184 <                    for (j = 0; j < 3; j++)
185 <                        org[j] = obj->oargs.farg[j] + obj->oargs.farg[3] *
186 <                                        ( r1*cos(r2)*u[j] + r1*sin(r2)*v[j]
187 <                                                - sqrt(1.01-r1*r1)*dir[j] );
188 <
180 >                    r3 = sqrt(sp[2]);
181 >                    r2 = 2.*PI*sp[3];
182 >                    r1 = r3*ob->oargs.farg[3]*cos(r2);
183 >                    r2 = r3*ob->oargs.farg[3]*sin(r2);
184 >                    r3 = ob->oargs.farg[3]*sqrt(1.01-r3*r3);
185 >                    for (j = 0; j < 3; j++) {
186 >                        org[j] = ob->oargs.farg[j] + r1*u[j] + r2*v[j] +
187 >                                        r3*dir[j];
188 >                        dir[j] = -dir[j];
189 >                    }
190                                          /* send sample */
191 <                    raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt);
191 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
192                  }
193          rayflush(rt);
194 <                                /* write out distribution */
195 <        rounddist(distarr, nalt, nazi, il, ob);
194 >                                /* write out the sphere and its distribution */
195 >        if (average(il, distarr, nalt*nazi)) {
196 >                if (il->sampdens > 0)
197 >                        roundout(il, distarr, nalt, nazi);
198 >                else
199 >                        objerror(ob, WARNING, "diffuse distribution");
200 >                illumout(il, ob);
201 >        } else
202 >                printobj(il->altmat, ob);
203                                  /* clean up */
204 <        free((char *)distarr);
204 >        free((void *)distarr);
205   }
206  
207  
# Line 130 | Line 211 | struct illum_args  *il;
211   struct rtproc  *rt;
212   char  *nm;
213   {
214 +        int  dim[3];
215 +        int  n, nalt, nazi;
216 +        float  *distarr;
217 +        double  sp[4], r1, r2, r3;
218 +        FVECT  dn, org, dir;
219 +        FVECT  u, v;
220 +        register CONE  *co;
221 +        register int  i, j;
222 +                                /* get/check arguments */
223 +        co = getcone(ob, 0);
224 +                                /* set up sampling */
225 +        if (il->sampdens <= 0)
226 +                nalt = nazi = 1;
227 +        else {
228 +                n = PI * il->sampdens;
229 +                nalt = sqrt(n/PI) + .5;
230 +                nazi = PI*nalt + .5;
231 +        }
232 +        n = nalt*nazi;
233 +        distarr = (float *)calloc(n, 3*sizeof(float));
234 +        if (distarr == NULL)
235 +                error(SYSTEM, "out of memory in o_ring");
236 +        mkaxes(u, v, co->ad);
237 +        dim[0] = random();
238 +                                /* sample disk */
239 +        for (dim[1] = 0; dim[1] < nalt; dim[1]++)
240 +            for (dim[2] = 0; dim[2] < nazi; dim[2]++)
241 +                for (i = 0; i < il->nsamps; i++) {
242 +                                        /* next sample point */
243 +                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
244 +                                        /* random direction */
245 +                    r1 = (dim[1] + sp[0])/nalt;
246 +                    r2 = (dim[2] + sp[1] - .5)/nazi;
247 +                    flatdir(dn, r1, r2);
248 +                    for (j = 0; j < 3; j++)
249 +                        dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j];
250 +                                        /* random location */
251 +                    r3 = sqrt(CO_R0(co)*CO_R0(co) +
252 +                            sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
253 +                    r2 = 2.*PI*sp[3];
254 +                    r1 = r3*cos(r2);
255 +                    r2 = r3*sin(r2);
256 +                    for (j = 0; j < 3; j++)
257 +                        org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] +
258 +                                        .001*co->ad[j];
259 +
260 +                                        /* send sample */
261 +                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
262 +                }
263 +        rayflush(rt);
264 +                                /* write out the ring and its distribution */
265 +        if (average(il, distarr, nalt*nazi)) {
266 +                if (il->sampdens > 0)
267 +                        flatout(il, distarr, nalt, nazi, u, v, co->ad);
268 +                illumout(il, ob);
269 +        } else
270 +                printobj(il->altmat, ob);
271 +                                /* clean up */
272 +        freecone(ob);
273 +        free((void *)distarr);
274   }
275  
276  
# Line 156 | Line 297 | register struct rtproc  *rt;
297  
298          if (rt->nrays <= 0)
299                  return;
300 <        i = 6*rt->nrays + 3;
301 <        rt->buf[i++] = 0.; rt->buf[i++] = 0.; rt->buf[i] = 0.;
302 <        if ( process(rt->pd, (char *)rt->buf, (char *)rt->buf,
303 <                        3*sizeof(float)*rt->nrays,
300 >        bzero(rt->buf+6*rt->nrays, 6*sizeof(float));
301 >        errno = 0;
302 >        if ( process(&(rt->pd), (char *)rt->buf, (char *)rt->buf,
303 >                        3*sizeof(float)*(rt->nrays+1),
304                          6*sizeof(float)*(rt->nrays+1)) <
305 <                        3*sizeof(float)*rt->nrays )
305 >                        3*sizeof(float)*(rt->nrays+1) )
306                  error(SYSTEM, "error reading from rtrace process");
307          i = rt->nrays;
308          while (i--) {
# Line 170 | Line 311 | register struct rtproc  *rt;
311                  rt->dest[i][2] += rt->buf[3*i+2];
312          }
313          rt->nrays = 0;
314 + }
315 +
316 +
317 + mkaxes(u, v, n)                 /* compute u and v to go with n */
318 + FVECT  u, v, n;
319 + {
320 +        register int  i;
321 +
322 +        v[0] = v[1] = v[2] = 0.0;
323 +        for (i = 0; i < 3; i++)
324 +                if (n[i] < 0.6 && n[i] > -0.6)
325 +                        break;
326 +        v[i] = 1.0;
327 +        fcross(u, v, n);
328 +        normalize(u);
329 +        fcross(v, n, u);
330 + }
331 +
332 +
333 + rounddir(dv, alt, azi)          /* compute uniform spherical direction */
334 + register FVECT  dv;
335 + double  alt, azi;
336 + {
337 +        double  d1, d2;
338 +
339 +        dv[2] = 1. - 2.*alt;
340 +        d1 = sqrt(1. - dv[2]*dv[2]);
341 +        d2 = 2.*PI * azi;
342 +        dv[0] = d1*cos(d2);
343 +        dv[1] = d1*sin(d2);
344 + }
345 +
346 +
347 + flatdir(dv, alt, azi)           /* compute uniform hemispherical direction */
348 + register FVECT  dv;
349 + double  alt, azi;
350 + {
351 +        double  d1, d2;
352 +
353 +        d1 = sqrt(alt);
354 +        d2 = 2.*PI * azi;
355 +        dv[0] = d1*cos(d2);
356 +        dv[1] = d1*sin(d2);
357 +        dv[2] = sqrt(1. - alt);
358   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines