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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines