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.9 by greg, Sat Feb 22 02:07:24 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   */
# Line 26 | Line 23 | char  *nm;
23          sprintf(errmsg, "(%s): cannot make illum for %s \"%s\"",
24                          nm, ofun[ob->otype].funame, ob->oname);
25          error(WARNING, errmsg);
26 <        if (!(il->flags & IL_LIGHT))
30 <                printobj(il->altmat, ob);
26 >        printobj(il->altmat, ob);
27   }
28  
29  
# Line 38 | Line 34 | struct rtproc  *rt;
34   char  *nm;
35   {
36   #define MAXMISS         (5*n*il->nsamps)
37 <        int  dim[4];
38 <        int  n, nalt, nazi;
37 >        int  dim[3];
38 >        int  n, nalt, nazi, h;
39          float  *distarr;
40 <        double  r1, r2;
40 >        double  sp[2], r1, r2;
41          FVECT  dn, org, dir;
42          FVECT  u, v;
43          double  ur[2], vr[2];
# Line 56 | Line 52 | char  *nm;
52                  return;
53          }
54                                  /* set up sampling */
55 <        n = PI * il->sampdens;
56 <        nalt = sqrt(n/PI) + .5;
57 <        nazi = PI*nalt + .5;
55 >        if (il->sampdens <= 0)
56 >                nalt = nazi = 1;
57 >        else {
58 >                n = PI * il->sampdens;
59 >                nalt = sqrt(n/PI) + .5;
60 >                nazi = PI*nalt + .5;
61 >        }
62          n = nalt*nazi;
63          distarr = (float *)calloc(n, 3*sizeof(float));
64          if (distarr == NULL)
65                  error(SYSTEM, "out of memory in o_face");
66 <        mkaxes(u, v, fa->norm);
66 >                                /* take first edge longer than sqrt(area) */
67 >        for (j = fa->nv-1, i = 0; i < fa->nv; j = i++) {
68 >                u[0] = VERTEX(fa,i)[0] - VERTEX(fa,j)[0];
69 >                u[1] = VERTEX(fa,i)[1] - VERTEX(fa,j)[1];
70 >                u[2] = VERTEX(fa,i)[2] - VERTEX(fa,j)[2];
71 >                if ((r1 = DOT(u,u)) >= fa->area-FTINY)
72 >                        break;
73 >        }
74 >        if (i < fa->nv) {       /* got one! -- let's align our axes */
75 >                r2 = 1.0/sqrt(r1);
76 >                u[0] *= r2; u[1] *= r2; u[2] *= r2;
77 >                fcross(v, fa->norm, u);
78 >        } else                  /* oh well, we'll just have to wing it */
79 >                mkaxes(u, v, fa->norm);
80 >                                /* now, find limits in (u,v) coordinates */
81          ur[0] = vr[0] = FHUGE;
82          ur[1] = vr[1] = -FHUGE;
83          for (i = 0; i < fa->nv; i++) {
# Line 81 | Line 95 | char  *nm;
95              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
96                  for (i = 0; i < il->nsamps; i++) {
97                                          /* random direction */
98 <                    dim[3] = 1;
99 <                    r1 = (dim[1]+urand(urind(ilhash(dim,4),i)))/nalt;
100 <                    dim[3] = 2;
101 <                    r2 = (dim[2]+urand(urind(ilhash(dim,4),i)))/nazi;
98 >                    h = ilhash(dim, 3) + i;
99 >                    multisamp(sp, 2, urand(h));
100 >                    r1 = (dim[1] + sp[0])/nalt;
101 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
102                      flatdir(dn, r1, r2);
103                      for (j = 0; j < 3; j++)
104                          dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*fa->norm[j];
105                                          /* random location */
106                      do {
107 <                        dim[3] = 3;
108 <                        r1 = ur[0] + (ur[1]-ur[0]) *
109 <                                        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));
107 >                        multisamp(sp, 2, urand(h+4862+nmisses));
108 >                        r1 = ur[0] + (ur[1]-ur[0]) * sp[0];
109 >                        r2 = vr[0] + (vr[1]-vr[0]) * sp[1];
110                          for (j = 0; j < 3; j++)
111                              org[j] = r1*u[j] + r2*v[j]
112                                          + fa->offset*fa->norm[j];
# Line 104 | Line 115 | char  *nm;
115                          objerror(ob, WARNING, "bad aspect");
116                          rt->nrays = 0;
117                          freeface(ob);
118 <                        free((char *)distarr);
118 >                        free((void *)distarr);
119                          o_default(ob, il, rt, nm);
120                          return;
121                      }
122                      for (j = 0; j < 3; j++)
123                          org[j] += .001*fa->norm[j];
124                                          /* send sample */
125 <                    raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt);
125 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
126                  }
127          rayflush(rt);
128 <                                /* write out the face w/ distribution */
129 <        flatout(il, distarr, nalt, nazi, u, v, fa->norm);
130 <        illumout(il, ob);
128 >                                /* write out the face and its distribution */
129 >        if (average(il, distarr, nalt*nazi)) {
130 >                if (il->sampdens > 0)
131 >                        flatout(il, distarr, nalt, nazi, u, v, fa->norm);
132 >                illumout(il, ob);
133 >        } else
134 >                printobj(il->altmat, ob);
135                                  /* clean up */
136          freeface(ob);
137 <        free((char *)distarr);
137 >        free((void *)distarr);
138   #undef MAXMISS
139   }
140  
# Line 130 | Line 145 | struct illum_args  *il;
145   struct rtproc  *rt;
146   char  *nm;
147   {
148 <        int  dim[4];
148 >        int  dim[3];
149          int  n, nalt, nazi;
150          float  *distarr;
151 <        double  r1, r2, r3;
151 >        double  sp[4], r1, r2, r3;
152          FVECT  org, dir;
153          FVECT  u, v;
154          register int  i, j;
# Line 141 | Line 156 | char  *nm;
156          if (ob->oargs.nfargs != 4)
157                  objerror(ob, USER, "bad # of arguments");
158                                  /* set up sampling */
159 <        n = 4.*PI * il->sampdens;
160 <        nalt = sqrt(n/PI) + .5;
161 <        nazi = PI*nalt + .5;
159 >        if (il->sampdens <= 0)
160 >                nalt = nazi = 1;
161 >        else {
162 >                n = 4.*PI * il->sampdens;
163 >                nalt = sqrt(2./PI*n) + .5;
164 >                nazi = PI/2.*nalt + .5;
165 >        }
166          n = nalt*nazi;
167          distarr = (float *)calloc(n, 3*sizeof(float));
168          if (distarr == NULL)
# Line 151 | Line 170 | char  *nm;
170          dim[0] = random();
171                                  /* sample sphere */
172          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
173 <            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 <                }
173 >            for (dim[2] = 0; dim[2] < nazi; dim[2]++)
174                  for (i = 0; i < il->nsamps; i++) {
175 +                                        /* next sample point */
176 +                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
177                                          /* random direction */
178 <                    dim[3] = 1;
179 <                    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;
178 >                    r1 = (dim[1] + sp[0])/nalt;
179 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
180                      rounddir(dir, r1, r2);
181                                          /* random location */
182 <                    if (il->nsamps <= 2 || nazi <= 20)
183 <                        mkaxes(u, v, dir);              /* yuck! */
184 <                    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));
182 >                    mkaxes(u, v, dir);          /* yuck! */
183 >                    r3 = sqrt(sp[2]);
184 >                    r2 = 2.*PI*sp[3];
185                      r1 = r3*ob->oargs.farg[3]*cos(r2);
186                      r2 = r3*ob->oargs.farg[3]*sin(r2);
187                      r3 = ob->oargs.farg[3]*sqrt(1.01-r3*r3);
# Line 179 | Line 191 | char  *nm;
191                          dir[j] = -dir[j];
192                      }
193                                          /* send sample */
194 <                    raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt);
194 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
195                  }
184            }
196          rayflush(rt);
197 <                                /* write out the sphere w/ distribution */
198 <        roundout(il, distarr, nalt, nazi);
199 <        illumout(il, ob);
197 >                                /* write out the sphere and its distribution */
198 >        if (average(il, distarr, nalt*nazi)) {
199 >                if (il->sampdens > 0)
200 >                        roundout(il, distarr, nalt, nazi);
201 >                else
202 >                        objerror(ob, WARNING, "diffuse distribution");
203 >                illumout(il, ob);
204 >        } else
205 >                printobj(il->altmat, ob);
206                                  /* clean up */
207 <        free((char *)distarr);
207 >        free((void *)distarr);
208   }
209  
210  
# Line 197 | Line 214 | struct illum_args  *il;
214   struct rtproc  *rt;
215   char  *nm;
216   {
217 <        int  dim[4];
217 >        int  dim[3];
218          int  n, nalt, nazi;
219          float  *distarr;
220 <        double  r1, r2, r3;
220 >        double  sp[4], r1, r2, r3;
221          FVECT  dn, org, dir;
222          FVECT  u, v;
223          register CONE  *co;
# Line 208 | Line 225 | char  *nm;
225                                  /* get/check arguments */
226          co = getcone(ob, 0);
227                                  /* set up sampling */
228 <        n = PI * il->sampdens;
229 <        nalt = sqrt(n/PI) + .5;
230 <        nazi = PI*nalt + .5;
228 >        if (il->sampdens <= 0)
229 >                nalt = nazi = 1;
230 >        else {
231 >                n = PI * il->sampdens;
232 >                nalt = sqrt(n/PI) + .5;
233 >                nazi = PI*nalt + .5;
234 >        }
235          n = nalt*nazi;
236          distarr = (float *)calloc(n, 3*sizeof(float));
237          if (distarr == NULL)
# Line 221 | Line 242 | char  *nm;
242          for (dim[1] = 0; dim[1] < nalt; dim[1]++)
243              for (dim[2] = 0; dim[2] < nazi; dim[2]++)
244                  for (i = 0; i < il->nsamps; i++) {
245 +                                        /* next sample point */
246 +                    multisamp(sp, 4, urand(ilhash(dim,3)+i));
247                                          /* random direction */
248 <                    dim[3] = 1;
249 <                    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;
248 >                    r1 = (dim[1] + sp[0])/nalt;
249 >                    r2 = (dim[2] + sp[1] - .5)/nazi;
250                      flatdir(dn, r1, r2);
251                      for (j = 0; j < 3; j++)
252                          dir[j] = -dn[0]*u[j] - dn[1]*v[j] - dn[2]*co->ad[j];
253                                          /* random location */
233                    dim[3] = 3;
254                      r3 = sqrt(CO_R0(co)*CO_R0(co) +
255 <                                urand(urind(ilhash(dim,4),i))*
256 <                                (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));
255 >                            sp[2]*(CO_R1(co)*CO_R1(co) - CO_R0(co)*CO_R0(co)));
256 >                    r2 = 2.*PI*sp[3];
257                      r1 = r3*cos(r2);
258                      r2 = r3*sin(r2);
259                      for (j = 0; j < 3; j++)
260 <                        org[j] = CO_P0(co)[j] + r1*u[j] + r1*v[j] +
260 >                        org[j] = CO_P0(co)[j] + r1*u[j] + r2*v[j] +
261                                          .001*co->ad[j];
262  
263                                          /* send sample */
264 <                    raysamp(distarr+dim[1]*nazi+dim[2], org, dir, rt);
264 >                    raysamp(distarr+3*(dim[1]*nazi+dim[2]), org, dir, rt);
265                  }
266          rayflush(rt);
267 <                                /* write out the ring w/ distribution */
268 <        flatout(il, distarr, nalt, nazi, u, v, co->ad);
269 <        illumout(il, ob);
267 >                                /* write out the ring and its distribution */
268 >        if (average(il, distarr, nalt*nazi)) {
269 >                if (il->sampdens > 0)
270 >                        flatout(il, distarr, nalt, nazi, u, v, co->ad);
271 >                illumout(il, ob);
272 >        } else
273 >                printobj(il->altmat, ob);
274                                  /* clean up */
275          freecone(ob);
276 <        free((char *)distarr);
276 >        free((void *)distarr);
277   }
278  
279  
# Line 278 | Line 300 | register struct rtproc  *rt;
300  
301          if (rt->nrays <= 0)
302                  return;
303 <        i = 6*rt->nrays + 3;
304 <        rt->buf[i++] = 0.; rt->buf[i++] = 0.; rt->buf[i] = 0.;
303 >        bzero(rt->buf+6*rt->nrays, 6*sizeof(float));
304 >        errno = 0;
305          if ( process(rt->pd, (char *)rt->buf, (char *)rt->buf,
306 <                        3*sizeof(float)*rt->nrays,
306 >                        3*sizeof(float)*(rt->nrays+1),
307                          6*sizeof(float)*(rt->nrays+1)) <
308 <                        3*sizeof(float)*rt->nrays )
308 >                        3*sizeof(float)*(rt->nrays+1) )
309                  error(SYSTEM, "error reading from rtrace process");
310          i = rt->nrays;
311          while (i--) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines