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.6 by greg, Thu Jul 25 12:52:18 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 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  
# Line 26 | 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))
30 <                printobj(il->altmat, ob);
23 >        printobj(il->altmat, ob);
24   }
25  
26  
# Line 38 | Line 31 | struct rtproc  *rt;
31   char  *nm;
32   {
33   #define MAXMISS         (5*n*il->nsamps)
34 <        int  dim[4];
35 <        int  n, nalt, nazi;
34 >        int  dim[3];
35 >        int  n, nalt, nazi, h;
36          float  *distarr;
37 <        double  r1, r2;
37 >        double  sp[2], r1, r2;
38          FVECT  dn, org, dir;
39          FVECT  u, v;
40          double  ur[2], vr[2];
# Line 56 | Line 49 | char  *nm;
49                  return;
50          }
51                                  /* set up sampling */
52 <        n = PI * il->sampdens;
53 <        nalt = sqrt(n/PI) + .5;
54 <        nazi = PI*nalt + .5;
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 <        mkaxes(u, v, fa->norm);
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++) {
# Line 81 | Line 92 | char  *nm;
92              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
93                  for (i = 0; i < il->nsamps; i++) {
94                                          /* random direction */
95 <                    dim[3] = 1;
96 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
97 <                    dim[3] = 2;
98 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nazi;
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 <                        dim[3] = 3;
105 <                        r1 = ur[0] + (ur[1]-ur[0]) *
106 <                                        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));
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];
# Line 104 | Line 112 | char  *nm;
112                          objerror(ob, WARNING, "bad aspect");
113                          rt->nrays = 0;
114                          freeface(ob);
115 <                        free((char *)distarr);
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+dim[1]*nazi+dim[2], org, dir, rt);
122 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
123                  }
124          rayflush(rt);
125 <                                /* write out the face w/ distribution */
126 <        flatout(il, distarr, nalt, nazi, u, v, fa->norm);
127 <        illumout(il, ob);
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((char *)distarr);
134 >        free((void *)distarr);
135   #undef MAXMISS
136   }
137  
# Line 130 | Line 142 | 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, r3;
148 >        double  sp[4], r1, r2, r3;
149          FVECT  org, dir;
150          FVECT  u, v;
151          register int  i, j;
# Line 141 | Line 153 | char  *nm;
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 151 | Line 167 | char  *nm;
167          dim[0] = random();
168                                  /* sample sphere */
169          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
170 <            for (dim[2] = 0; dim[2] < nazi; dim[2]++) {
155 <                if (il->nsamps > 2 && nazi > 20) {
156 <                    rounddir(dir, (dim[1]+.5)/nalt, (dim[2]+.5)/nazi);
157 <                    mkaxes(u, v, dir);
158 <                }
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;
163 <                    dim[3] = 2;
164 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nazi;
175 >                    r1 = (dim[1] + sp[0])/nalt;
176 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
177                      rounddir(dir, r1, r2);
178                                          /* random location */
179 <                    if (il->nsamps <= 2 || nazi <= 20)
180 <                        mkaxes(u, v, dir);              /* yuck! */
181 <                    dim[3] = 3;
170 <                    r3 = sqrt(urand(urind(ilhash(dim,4),i)));
171 <                    dim[3] = 4;
172 <                    r2 = 2.*PI*urand(urind(ilhash(dim,4),i));
179 >                    mkaxes(u, v, dir);          /* yuck! */
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);
# Line 179 | Line 188 | char  *nm;
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                  }
184            }
193          rayflush(rt);
194 <                                /* write out the sphere w/ distribution */
195 <        roundout(il, distarr, nalt, nazi);
196 <        illumout(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 197 | Line 211 | struct illum_args  *il;
211   struct rtproc  *rt;
212   char  *nm;
213   {
214 <        int  dim[4];
214 >        int  dim[3];
215          int  n, nalt, nazi;
216          float  *distarr;
217 <        double  r1, r2, r3;
217 >        double  sp[4], r1, r2, r3;
218          FVECT  dn, org, dir;
219          FVECT  u, v;
220          register CONE  *co;
# Line 208 | Line 222 | char  *nm;
222                                  /* get/check arguments */
223          co = getcone(ob, 0);
224                                  /* set up sampling */
225 <        n = PI * il->sampdens;
226 <        nalt = sqrt(n/PI) + .5;
227 <        nazi = PI*nalt + .5;
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)
# Line 221 | Line 239 | char  *nm;
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 <                    dim[3] = 1;
246 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
227 <                    dim[3] = 2;
228 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nalt;
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 */
233                    dim[3] = 3;
251                      r3 = sqrt(CO_R0(co)*CO_R0(co) +
252 <                                urand(urind(ilhash(dim,4),i))*
253 <                                (CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
237 <                    dim[3] = 4;
238 <                    r2 = 2.*PI*urand(urind(ilhash(dim,4),i));
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] + r1*v[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+dim[1]*nazi+dim[2], org, dir, rt);
261 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
262                  }
263          rayflush(rt);
264 <                                /* write out the ring w/ distribution */
265 <        flatout(il, distarr, nalt, nazi, u, v, co->ad);
266 <        illumout(il, ob);
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((char *)distarr);
273 >        free((void *)distarr);
274   }
275  
276  
# Line 278 | 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--) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines