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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines