ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsupp.c
Revision: 1.6
Committed: Tue Jun 25 13:11:55 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.5: +46 -0 lines
Log Message:
fixed problems with virtual source pretesting

File Contents

# User Rev Content
1 greg 1.1 /* Copyright (c) 1991 Regents of the University of California */
2    
3     #ifndef lint
4     static char SCCSid[] = "$SunId$ LBL";
5     #endif
6    
7     /*
8     * Support routines for source objects and materials
9     */
10    
11     #include "ray.h"
12    
13     #include "otypes.h"
14    
15     #include "source.h"
16    
17     #include "cone.h"
18    
19     #include "face.h"
20    
21    
22     SRCREC *source = NULL; /* our list of sources */
23     int nsources = 0; /* the number of sources */
24    
25     SRCFUNC sfun[NUMOTYPE]; /* source dispatch table */
26    
27    
28     initstypes() /* initialize source dispatch table */
29     {
30     extern VSMATERIAL mirror_vs;
31     extern int fsetsrc(), ssetsrc(), sphsetsrc(), rsetsrc();
32     extern double fgetplaneq(), rgetplaneq();
33     extern double fgetmaxdisk(), rgetmaxdisk();
34     static SOBJECT fsobj = {fsetsrc, fgetplaneq, fgetmaxdisk};
35     static SOBJECT ssobj = {ssetsrc};
36     static SOBJECT sphsobj = {sphsetsrc};
37     static SOBJECT rsobj = {rsetsrc, rgetplaneq, rgetmaxdisk};
38    
39     sfun[MAT_MIRROR].mf = &mirror_vs;
40     sfun[OBJ_FACE].of = &fsobj;
41     sfun[OBJ_SOURCE].of = &ssobj;
42     sfun[OBJ_SPHERE].of = &sphsobj;
43     sfun[OBJ_RING].of = &rsobj;
44     }
45    
46    
47 greg 1.2 int
48 greg 1.1 newsource() /* allocate new source in our array */
49     {
50     if (nsources == 0)
51     source = (SRCREC *)malloc(sizeof(SRCREC));
52     else
53     source = (SRCREC *)realloc((char *)source,
54     (unsigned)(nsources+1)*sizeof(SRCREC));
55     if (source == NULL)
56 greg 1.2 return(-1);
57 greg 1.1 source[nsources].sflags = 0;
58     source[nsources].nhits = 1;
59     source[nsources].ntests = 2; /* initial hit probability = 1/2 */
60 greg 1.2 return(nsources++);
61 greg 1.1 }
62    
63    
64     fsetsrc(src, so) /* set a face as a source */
65     register SRCREC *src;
66     OBJREC *so;
67     {
68     register FACE *f;
69     register int i, j;
70    
71     src->sa.success = 2*AIMREQT-1; /* bitch on second failure */
72     src->so = so;
73     /* get the face */
74     f = getface(so);
75     /* find the center */
76     for (j = 0; j < 3; j++) {
77     src->sloc[j] = 0.0;
78     for (i = 0; i < f->nv; i++)
79     src->sloc[j] += VERTEX(f,i)[j];
80     src->sloc[j] /= (double)f->nv;
81     }
82     if (!inface(src->sloc, f))
83     objerror(so, USER, "cannot hit center");
84     src->sflags |= SFLAT;
85     VCOPY(src->snorm, f->norm);
86     src->ss = sqrt(f->area / PI);
87     src->ss2 = f->area;
88     }
89    
90    
91     ssetsrc(src, so) /* set a source as a source */
92     register SRCREC *src;
93     register OBJREC *so;
94     {
95     double theta;
96    
97     src->sa.success = 2*AIMREQT-1; /* bitch on second failure */
98     src->so = so;
99     if (so->oargs.nfargs != 4)
100     objerror(so, USER, "bad arguments");
101     src->sflags |= SDISTANT;
102     VCOPY(src->sloc, so->oargs.farg);
103     if (normalize(src->sloc) == 0.0)
104     objerror(so, USER, "zero direction");
105     theta = PI/180.0/2.0 * so->oargs.farg[3];
106     if (theta <= FTINY)
107     objerror(so, USER, "zero size");
108     src->ss = theta >= PI/4 ? 1.0 : tan(theta);
109     src->ss2 = 2.0*PI * (1.0 - cos(theta));
110     }
111    
112    
113     sphsetsrc(src, so) /* set a sphere as a source */
114     register SRCREC *src;
115     register OBJREC *so;
116     {
117     src->sa.success = 2*AIMREQT-1; /* bitch on second failure */
118     src->so = so;
119     if (so->oargs.nfargs != 4)
120     objerror(so, USER, "bad # arguments");
121     if (so->oargs.farg[3] <= FTINY)
122     objerror(so, USER, "illegal radius");
123     VCOPY(src->sloc, so->oargs.farg);
124     src->ss = so->oargs.farg[3];
125     src->ss2 = PI * src->ss * src->ss;
126     }
127    
128    
129     rsetsrc(src, so) /* set a ring (disk) as a source */
130     register SRCREC *src;
131     OBJREC *so;
132     {
133     register CONE *co;
134    
135     src->sa.success = 2*AIMREQT-1; /* bitch on second failure */
136     src->so = so;
137     /* get the ring */
138     co = getcone(so, 0);
139     VCOPY(src->sloc, CO_P0(co));
140     if (CO_R0(co) > 0.0)
141     objerror(so, USER, "cannot hit center");
142     src->sflags |= SFLAT;
143     VCOPY(src->snorm, co->ad);
144     src->ss = CO_R1(co);
145     src->ss2 = PI * src->ss * src->ss;
146     }
147    
148    
149     SPOT *
150     makespot(m) /* make a spotlight */
151     register OBJREC *m;
152     {
153     register SPOT *ns;
154    
155     if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
156     return(NULL);
157     ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
158     VCOPY(ns->aim, m->oargs.farg+4);
159     if ((ns->flen = normalize(ns->aim)) == 0.0)
160     objerror(m, USER, "zero focus vector");
161     return(ns);
162     }
163    
164    
165     double
166     fgetmaxdisk(ocent, op) /* get center and squared radius of face */
167     FVECT ocent;
168     OBJREC *op;
169     {
170     double maxrad2;
171 greg 1.5 double d;
172 greg 1.1 register int i, j;
173     register FACE *f;
174    
175     f = getface(op);
176 greg 1.5 if (f->area == 0.)
177     return(0.);
178 greg 1.1 for (i = 0; i < 3; i++) {
179     ocent[i] = 0.;
180     for (j = 0; j < f->nv; j++)
181     ocent[i] += VERTEX(f,j)[i];
182     ocent[i] /= (double)f->nv;
183     }
184 greg 1.5 d = DOT(ocent,f->norm);
185     for (i = 0; i < 3; i++)
186     ocent[i] += (f->offset - d)*f->norm[i];
187 greg 1.1 maxrad2 = 0.;
188     for (j = 0; j < f->nv; j++) {
189 greg 1.5 d = dist2(VERTEX(f,j), ocent);
190     if (d > maxrad2)
191     maxrad2 = d;
192 greg 1.1 }
193     return(maxrad2);
194     }
195    
196    
197     double
198     rgetmaxdisk(ocent, op) /* get center and squared radius of ring */
199     FVECT ocent;
200     OBJREC *op;
201     {
202     register CONE *co;
203    
204     co = getcone(op, 0);
205     VCOPY(ocent, CO_P0(co));
206     return(CO_R1(co)*CO_R1(co));
207     }
208    
209    
210     double
211     fgetplaneq(nvec, op) /* get plane equation for face */
212     FVECT nvec;
213     OBJREC *op;
214     {
215     register FACE *fo;
216    
217     fo = getface(op);
218     VCOPY(nvec, fo->norm);
219     return(fo->offset);
220     }
221    
222    
223     double
224     rgetplaneq(nvec, op) /* get plane equation for ring */
225     FVECT nvec;
226     OBJREC *op;
227     {
228     register CONE *co;
229    
230     co = getcone(op, 0);
231     VCOPY(nvec, co->ad);
232     return(DOT(nvec, CO_P0(co)));
233 greg 1.4 }
234    
235    
236     commonspot(sp1, sp2, org) /* set sp1 to intersection of sp1 and sp2 */
237     register SPOT *sp1, *sp2;
238     FVECT org;
239     {
240     FVECT cent;
241     double rad2, cos1, cos2;
242    
243     cos1 = 1. - sp1->siz/(2.*PI);
244     cos2 = 1. - sp2->siz/(2.*PI);
245     if (sp2->siz >= 2.*PI-FTINY) /* BIG, just check overlap */
246     return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 -
247     sqrt((1.-cos1*cos1)*(1.-cos2*cos2)));
248     /* compute and check disks */
249     rad2 = intercircle(cent, sp1->aim, sp2->aim,
250     1./(cos1*cos1) - 1., 1./(cos2*cos2) - 1.);
251     if (rad2 <= FTINY || normalize(cent) == 0.)
252     return(0);
253     VCOPY(sp1->aim, cent);
254     sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
255     return(1);
256     }
257    
258    
259     commonbeam(sp1, sp2, dir) /* set sp1 to intersection of sp1 and sp2 */
260     register SPOT *sp1, *sp2;
261     FVECT dir;
262     {
263     FVECT cent, c1, c2;
264     double rad2, d;
265     register int i;
266     /* move centers to common plane */
267     d = DOT(sp1->aim, dir);
268     for (i = 0; i < 3; i++)
269     c1[i] = sp1->aim[i] - d*dir[i];
270     d = DOT(sp2->aim, dir);
271     for (i = 0; i < 3; i++)
272     c2[i] = sp2->aim[i] - d*dir[i];
273     /* compute overlap */
274     rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
275     if (rad2 <= FTINY)
276     return(0);
277     VCOPY(sp1->aim, cent);
278     sp1->siz = PI*rad2;
279     return(1);
280     }
281    
282    
283     checkspot(sp, nrm) /* check spotlight for behind source */
284     register SPOT *sp; /* spotlight */
285     FVECT nrm; /* source surface normal */
286     {
287     double d, d1;
288    
289     d = DOT(sp->aim, nrm);
290     if (d > FTINY) /* center in front? */
291     return(0);
292     /* else check horizon */
293     d1 = 1. - sp->siz/(2.*PI);
294     return(1.-FTINY-d*d > d1*d1);
295     }
296    
297    
298     double
299 greg 1.6 spotdisk(oc, op, sp, pos) /* intersect spot with object op */
300     FVECT oc;
301     OBJREC *op;
302     register SPOT *sp;
303     FVECT pos;
304     {
305     FVECT onorm;
306     double offs, d, dist;
307     register int i;
308    
309     offs = getplaneq(onorm, op);
310     d = -DOT(onorm, sp->aim);
311     if (d >= -FTINY && d <= FTINY)
312     return(0.);
313     dist = (DOT(pos, onorm) - offs)/d;
314     if (dist < 0.)
315     return(0.);
316     for (i = 0; i < 3; i++)
317     oc[i] = pos[i] + dist*sp->aim[i];
318     return(sp->siz*dist*dist/PI/(d*d));
319     }
320    
321    
322     double
323     beamdisk(oc, op, sp, dir) /* intersect beam with object op */
324     FVECT oc;
325     OBJREC *op;
326     register SPOT *sp;
327     FVECT dir;
328     {
329     FVECT onorm;
330     double offs, d, dist;
331     register int i;
332    
333     offs = getplaneq(onorm, op);
334     d = -DOT(onorm, dir);
335     if (d >= -FTINY && d <= FTINY)
336     return(0.);
337     dist = (DOT(sp->aim, onorm) - offs)/d;
338     for (i = 0; i < 3; i++)
339     oc[i] = sp->aim[i] + dist*dir[i];
340     return(sp->siz/PI/(d*d));
341     }
342    
343    
344     double
345 greg 1.4 intercircle(cc, c1, c2, r1s, r2s) /* intersect two circles */
346     FVECT cc; /* midpoint (return value) */
347     FVECT c1, c2; /* circle centers */
348     double r1s, r2s; /* radii squared */
349     {
350     double a2, d2, l;
351     FVECT disp;
352     register int i;
353    
354     for (i = 0; i < 3; i++)
355     disp[i] = c2[i] - c1[i];
356     d2 = DOT(disp,disp);
357     /* circle within overlap? */
358     if (r1s < r2s) {
359     if (r2s >= r1s + d2) {
360     VCOPY(cc, c1);
361     return(r1s);
362     }
363     } else {
364     if (r1s >= r2s + d2) {
365     VCOPY(cc, c2);
366     return(r2s);
367     }
368     }
369     a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
370     /* no overlap? */
371     if (a2 <= 0.)
372     return(0.);
373     /* overlap, compute center */
374     l = sqrt((r1s - a2)/d2);
375     for (i = 0; i < 3; i++)
376     cc[i] = c1[i] + l*disp[i];
377     return(a2);
378 greg 1.1 }
379    
380    
381     sourcehit(r) /* check to see if ray hit distant source */
382     register RAY *r;
383     {
384     int first, last;
385     register int i;
386    
387     if (r->rsrc >= 0) { /* check only one if aimed */
388     first = last = r->rsrc;
389     } else { /* otherwise check all */
390     first = 0; last = nsources-1;
391     }
392     for (i = first; i <= last; i++)
393     if (source[i].sflags & SDISTANT)
394     /*
395     * Check to see if ray is within
396     * solid angle of source.
397     */
398     if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
399     <= source[i].ss2) {
400     r->ro = source[i].so;
401     if (!(source[i].sflags & SSKIP))
402     break;
403     }
404    
405     if (r->ro != NULL) {
406     for (i = 0; i < 3; i++)
407     r->ron[i] = -r->rdir[i];
408     r->rod = 1.0;
409     r->rox = NULL;
410     return(1);
411     }
412     return(0);
413     }
414    
415    
416     #define wrongsource(m, r) (m->otype!=MAT_ILLUM && \
417     r->rsrc>=0 && \
418     source[r->rsrc].so!=r->ro)
419    
420     #define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
421     !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
422    
423     #define passillum(m, r) (m->otype==MAT_ILLUM && \
424     !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
425    
426    
427     m_light(m, r) /* ray hit a light source */
428     register OBJREC *m;
429     register RAY *r;
430     {
431     /* check for over-counting */
432     if (wrongsource(m, r) || badambient(m, r))
433     return;
434     /* check for passed illum */
435     if (passillum(m, r)) {
436    
437     if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
438     raytrans(r);
439     else
440     rayshade(r, modifier(m->oargs.sarg[0]));
441    
442     /* otherwise treat as source */
443     } else {
444     /* check for behind */
445     if (r->rod < 0.0)
446     return;
447     /* get distribution pattern */
448     raytexture(r, m->omod);
449     /* get source color */
450     setcolor(r->rcol, m->oargs.farg[0],
451     m->oargs.farg[1],
452     m->oargs.farg[2]);
453     /* modify value */
454     multcolor(r->rcol, r->pcol);
455     }
456     }