ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsupp.c
Revision: 2.8
Committed: Tue Nov 7 12:40:30 1995 UTC (28 years, 5 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.7: +2 -3 lines
Log Message:
changed spotlight struct so flen<0 for distant sources

File Contents

# Content
1 /* Copyright (c) 1995 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 #define SRCINC 4 /* realloc increment for array */
22
23 SRCREC *source = NULL; /* our list of sources */
24 int nsources = 0; /* the number of sources */
25
26 SRCFUNC sfun[NUMOTYPE]; /* source dispatch table */
27
28
29 initstypes() /* initialize source dispatch table */
30 {
31 extern VSMATERIAL mirror_vs, direct1_vs, direct2_vs;
32 extern int fsetsrc(), ssetsrc(), sphsetsrc(), cylsetsrc(), rsetsrc();
33 extern int nopart(), flatpart(), cylpart();
34 extern double fgetplaneq(), rgetplaneq();
35 extern double fgetmaxdisk(), rgetmaxdisk();
36 static SOBJECT fsobj = {fsetsrc, flatpart, fgetplaneq, fgetmaxdisk};
37 static SOBJECT ssobj = {ssetsrc, nopart};
38 static SOBJECT sphsobj = {sphsetsrc, nopart};
39 static SOBJECT cylsobj = {cylsetsrc, cylpart};
40 static SOBJECT rsobj = {rsetsrc, flatpart, rgetplaneq, rgetmaxdisk};
41
42 sfun[MAT_MIRROR].mf = &mirror_vs;
43 sfun[MAT_DIRECT1].mf = &direct1_vs;
44 sfun[MAT_DIRECT2].mf = &direct2_vs;
45 sfun[OBJ_FACE].of = &fsobj;
46 sfun[OBJ_SOURCE].of = &ssobj;
47 sfun[OBJ_SPHERE].of = &sphsobj;
48 sfun[OBJ_CYLINDER].of = &cylsobj;
49 sfun[OBJ_RING].of = &rsobj;
50 }
51
52
53 int
54 newsource() /* allocate new source in our array */
55 {
56 if (nsources == 0)
57 source = (SRCREC *)malloc(SRCINC*sizeof(SRCREC));
58 else if (nsources%SRCINC == 0)
59 source = (SRCREC *)realloc((char *)source,
60 (unsigned)(nsources+SRCINC)*sizeof(SRCREC));
61 if (source == NULL)
62 return(-1);
63 source[nsources].sflags = 0;
64 source[nsources].nhits = 1;
65 source[nsources].ntests = 2; /* initial hit probability = 1/2 */
66 return(nsources++);
67 }
68
69
70 setflatss(src) /* set sampling for a flat source */
71 register SRCREC *src;
72 {
73 double mult;
74 register int i;
75
76 src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
77 for (i = 0; i < 3; i++)
78 if (src->snorm[i] < 0.6 && src->snorm[i] > -0.6)
79 break;
80 src->ss[SV][i] = 1.0;
81 fcross(src->ss[SU], src->ss[SV], src->snorm);
82 mult = .5 * sqrt( src->ss2 / DOT(src->ss[SU],src->ss[SU]) );
83 for (i = 0; i < 3; i++)
84 src->ss[SU][i] *= mult;
85 fcross(src->ss[SV], src->snorm, src->ss[SU]);
86 }
87
88
89 fsetsrc(src, so) /* set a face as a source */
90 register SRCREC *src;
91 OBJREC *so;
92 {
93 register FACE *f;
94 register int i, j;
95 double d;
96
97 src->sa.success = 2*AIMREQT-1; /* bitch on second failure */
98 src->so = so;
99 /* get the face */
100 f = getface(so);
101 /* find the center */
102 for (j = 0; j < 3; j++) {
103 src->sloc[j] = 0.0;
104 for (i = 0; i < f->nv; i++)
105 src->sloc[j] += VERTEX(f,i)[j];
106 src->sloc[j] /= (double)f->nv;
107 }
108 if (!inface(src->sloc, f))
109 objerror(so, USER, "cannot hit center");
110 src->sflags |= SFLAT;
111 VCOPY(src->snorm, f->norm);
112 src->ss2 = f->area;
113 /* find maximum radius */
114 src->srad = 0.;
115 for (i = 0; i < f->nv; i++) {
116 d = dist2(VERTEX(f,i), src->sloc);
117 if (d > src->srad)
118 src->srad = d;
119 }
120 src->srad = sqrt(src->srad);
121 /* compute size vectors */
122 if (f->nv == 4) /* parallelogram case */
123 for (j = 0; j < 3; j++) {
124 src->ss[SU][j] = .5*(VERTEX(f,1)[j]-VERTEX(f,0)[j]);
125 src->ss[SV][j] = .5*(VERTEX(f,3)[j]-VERTEX(f,0)[j]);
126 }
127 else
128 setflatss(src);
129 }
130
131
132 ssetsrc(src, so) /* set a source as a source */
133 register SRCREC *src;
134 register OBJREC *so;
135 {
136 double theta;
137
138 src->sa.success = 2*AIMREQT-1; /* bitch on second failure */
139 src->so = so;
140 if (so->oargs.nfargs != 4)
141 objerror(so, USER, "bad arguments");
142 src->sflags |= SDISTANT;
143 VCOPY(src->sloc, so->oargs.farg);
144 if (normalize(src->sloc) == 0.0)
145 objerror(so, USER, "zero direction");
146 theta = PI/180.0/2.0 * so->oargs.farg[3];
147 if (theta <= FTINY)
148 objerror(so, USER, "zero size");
149 src->ss2 = 2.0*PI * (1.0 - cos(theta));
150 /* the following is approximate */
151 src->srad = sqrt(src->ss2/PI);
152 VCOPY(src->snorm, src->sloc);
153 setflatss(src); /* hey, whatever works */
154 src->ss[SW][0] = src->ss[SW][1] = src->ss[SW][2] = 0.0;
155 }
156
157
158 sphsetsrc(src, so) /* set a sphere as a source */
159 register SRCREC *src;
160 register OBJREC *so;
161 {
162 register int i;
163
164 src->sa.success = 2*AIMREQT-1; /* bitch on second failure */
165 src->so = so;
166 if (so->oargs.nfargs != 4)
167 objerror(so, USER, "bad # arguments");
168 if (so->oargs.farg[3] <= FTINY)
169 objerror(so, USER, "illegal radius");
170 VCOPY(src->sloc, so->oargs.farg);
171 src->srad = so->oargs.farg[3];
172 src->ss2 = PI * src->srad * src->srad;
173 for (i = 0; i < 3; i++)
174 src->ss[SU][i] = src->ss[SV][i] = src->ss[SW][i] = 0.0;
175 for (i = 0; i < 3; i++)
176 src->ss[i][i] = .7236 * so->oargs.farg[3];
177 }
178
179
180 rsetsrc(src, so) /* set a ring (disk) as a source */
181 register SRCREC *src;
182 OBJREC *so;
183 {
184 register CONE *co;
185
186 src->sa.success = 2*AIMREQT-1; /* bitch on second failure */
187 src->so = so;
188 /* get the ring */
189 co = getcone(so, 0);
190 VCOPY(src->sloc, CO_P0(co));
191 if (CO_R0(co) > 0.0)
192 objerror(so, USER, "cannot hit center");
193 src->sflags |= SFLAT;
194 VCOPY(src->snorm, co->ad);
195 src->srad = CO_R1(co);
196 src->ss2 = PI * src->srad * src->srad;
197 setflatss(src);
198 }
199
200
201 cylsetsrc(src, so) /* set a cylinder as a source */
202 register SRCREC *src;
203 OBJREC *so;
204 {
205 register CONE *co;
206 register int i;
207
208 src->sa.success = 4*AIMREQT-1; /* bitch on fourth failure */
209 src->so = so;
210 /* get the cylinder */
211 co = getcone(so, 0);
212 if (CO_R0(co) > .2*co->al) /* heuristic constraint */
213 objerror(so, WARNING, "source aspect too small");
214 src->sflags |= SCYL;
215 for (i = 0; i < 3; i++)
216 src->sloc[i] = .5 * (CO_P1(co)[i] + CO_P0(co)[i]);
217 src->srad = .5*co->al;
218 src->ss2 = 2.*CO_R0(co)*co->al;
219 /* set sampling vectors */
220 for (i = 0; i < 3; i++)
221 src->ss[SU][i] = .5 * co->al * co->ad[i];
222 src->ss[SV][0] = src->ss[SV][1] = src->ss[SV][2] = 0.0;
223 for (i = 0; i < 3; i++)
224 if (co->ad[i] < 0.6 && co->ad[i] > -0.6)
225 break;
226 src->ss[SV][i] = 1.0;
227 fcross(src->ss[SW], src->ss[SV], co->ad);
228 normalize(src->ss[SW]);
229 for (i = 0; i < 3; i++)
230 src->ss[SW][i] *= .8559 * CO_R0(co);
231 fcross(src->ss[SV], src->ss[SW], co->ad);
232 }
233
234
235 SPOT *
236 makespot(m) /* make a spotlight */
237 register OBJREC *m;
238 {
239 register SPOT *ns;
240
241 if ((ns = (SPOT *)m->os) != NULL)
242 return(ns);
243 if ((ns = (SPOT *)malloc(sizeof(SPOT))) == NULL)
244 return(NULL);
245 ns->siz = 2.0*PI * (1.0 - cos(PI/180.0/2.0 * m->oargs.farg[3]));
246 VCOPY(ns->aim, m->oargs.farg+4);
247 if ((ns->flen = normalize(ns->aim)) == 0.0)
248 objerror(m, USER, "zero focus vector");
249 m->os = (char *)ns;
250 return(ns);
251 }
252
253
254 spotout(r, s) /* check if we're outside spot region */
255 register RAY *r;
256 register SPOT *s;
257 {
258 double d;
259 FVECT vd;
260
261 if (s == NULL)
262 return(0);
263 if (s->flen < -FTINY) { /* distant source */
264 vd[0] = s->aim[0] - r->rorg[0];
265 vd[1] = s->aim[1] - r->rorg[1];
266 vd[2] = s->aim[2] - r->rorg[2];
267 d = DOT(r->rdir,vd);
268 /* wrong side?
269 if (d <= FTINY)
270 return(1); */
271 d = DOT(vd,vd) - d*d;
272 if (PI*d > s->siz)
273 return(1); /* out */
274 return(0); /* OK */
275 }
276 /* local source */
277 if (s->siz < 2.0*PI * (1.0 + DOT(s->aim,r->rdir)))
278 return(1); /* out */
279 return(0); /* OK */
280 }
281
282
283 double
284 fgetmaxdisk(ocent, op) /* get center and squared radius of face */
285 FVECT ocent;
286 OBJREC *op;
287 {
288 double maxrad2;
289 double d;
290 register int i, j;
291 register FACE *f;
292
293 f = getface(op);
294 if (f->area == 0.)
295 return(0.);
296 for (i = 0; i < 3; i++) {
297 ocent[i] = 0.;
298 for (j = 0; j < f->nv; j++)
299 ocent[i] += VERTEX(f,j)[i];
300 ocent[i] /= (double)f->nv;
301 }
302 d = DOT(ocent,f->norm);
303 for (i = 0; i < 3; i++)
304 ocent[i] += (f->offset - d)*f->norm[i];
305 maxrad2 = 0.;
306 for (j = 0; j < f->nv; j++) {
307 d = dist2(VERTEX(f,j), ocent);
308 if (d > maxrad2)
309 maxrad2 = d;
310 }
311 return(maxrad2);
312 }
313
314
315 double
316 rgetmaxdisk(ocent, op) /* get center and squared radius of ring */
317 FVECT ocent;
318 OBJREC *op;
319 {
320 register CONE *co;
321
322 co = getcone(op, 0);
323 VCOPY(ocent, CO_P0(co));
324 return(CO_R1(co)*CO_R1(co));
325 }
326
327
328 double
329 fgetplaneq(nvec, op) /* get plane equation for face */
330 FVECT nvec;
331 OBJREC *op;
332 {
333 register FACE *fo;
334
335 fo = getface(op);
336 VCOPY(nvec, fo->norm);
337 return(fo->offset);
338 }
339
340
341 double
342 rgetplaneq(nvec, op) /* get plane equation for ring */
343 FVECT nvec;
344 OBJREC *op;
345 {
346 register CONE *co;
347
348 co = getcone(op, 0);
349 VCOPY(nvec, co->ad);
350 return(DOT(nvec, CO_P0(co)));
351 }
352
353
354 commonspot(sp1, sp2, org) /* set sp1 to intersection of sp1 and sp2 */
355 register SPOT *sp1, *sp2;
356 FVECT org;
357 {
358 FVECT cent;
359 double rad2, cos1, cos2;
360
361 cos1 = 1. - sp1->siz/(2.*PI);
362 cos2 = 1. - sp2->siz/(2.*PI);
363 if (sp2->siz >= 2.*PI-FTINY) /* BIG, just check overlap */
364 return(DOT(sp1->aim,sp2->aim) >= cos1*cos2 -
365 sqrt((1.-cos1*cos1)*(1.-cos2*cos2)));
366 /* compute and check disks */
367 rad2 = intercircle(cent, sp1->aim, sp2->aim,
368 1./(cos1*cos1) - 1., 1./(cos2*cos2) - 1.);
369 if (rad2 <= FTINY || normalize(cent) == 0.)
370 return(0);
371 VCOPY(sp1->aim, cent);
372 sp1->siz = 2.*PI*(1. - 1./sqrt(1.+rad2));
373 return(1);
374 }
375
376
377 commonbeam(sp1, sp2, dir) /* set sp1 to intersection of sp1 and sp2 */
378 register SPOT *sp1, *sp2;
379 FVECT dir;
380 {
381 FVECT cent, c1, c2;
382 double rad2, d;
383 register int i;
384 /* move centers to common plane */
385 d = DOT(sp1->aim, dir);
386 for (i = 0; i < 3; i++)
387 c1[i] = sp1->aim[i] - d*dir[i];
388 d = DOT(sp2->aim, dir);
389 for (i = 0; i < 3; i++)
390 c2[i] = sp2->aim[i] - d*dir[i];
391 /* compute overlap */
392 rad2 = intercircle(cent, c1, c2, sp1->siz/PI, sp2->siz/PI);
393 if (rad2 <= FTINY)
394 return(0);
395 VCOPY(sp1->aim, cent);
396 sp1->siz = PI*rad2;
397 return(1);
398 }
399
400
401 checkspot(sp, nrm) /* check spotlight for behind source */
402 register SPOT *sp; /* spotlight */
403 FVECT nrm; /* source surface normal */
404 {
405 double d, d1;
406
407 d = DOT(sp->aim, nrm);
408 if (d > FTINY) /* center in front? */
409 return(1);
410 /* else check horizon */
411 d1 = 1. - sp->siz/(2.*PI);
412 return(1.-FTINY-d*d < d1*d1);
413 }
414
415
416 double
417 spotdisk(oc, op, sp, pos) /* intersect spot with object op */
418 FVECT oc;
419 OBJREC *op;
420 register SPOT *sp;
421 FVECT pos;
422 {
423 FVECT onorm;
424 double offs, d, dist;
425 register int i;
426
427 offs = getplaneq(onorm, op);
428 d = -DOT(onorm, sp->aim);
429 if (d >= -FTINY && d <= FTINY)
430 return(0.);
431 dist = (DOT(pos, onorm) - offs)/d;
432 if (dist < 0.)
433 return(0.);
434 for (i = 0; i < 3; i++)
435 oc[i] = pos[i] + dist*sp->aim[i];
436 return(sp->siz*dist*dist/PI/(d*d));
437 }
438
439
440 double
441 beamdisk(oc, op, sp, dir) /* intersect beam with object op */
442 FVECT oc;
443 OBJREC *op;
444 register SPOT *sp;
445 FVECT dir;
446 {
447 FVECT onorm;
448 double offs, d, dist;
449 register int i;
450
451 offs = getplaneq(onorm, op);
452 d = -DOT(onorm, dir);
453 if (d >= -FTINY && d <= FTINY)
454 return(0.);
455 dist = (DOT(sp->aim, onorm) - offs)/d;
456 for (i = 0; i < 3; i++)
457 oc[i] = sp->aim[i] + dist*dir[i];
458 return(sp->siz/PI/(d*d));
459 }
460
461
462 double
463 intercircle(cc, c1, c2, r1s, r2s) /* intersect two circles */
464 FVECT cc; /* midpoint (return value) */
465 FVECT c1, c2; /* circle centers */
466 double r1s, r2s; /* radii squared */
467 {
468 double a2, d2, l;
469 FVECT disp;
470 register int i;
471
472 for (i = 0; i < 3; i++)
473 disp[i] = c2[i] - c1[i];
474 d2 = DOT(disp,disp);
475 /* circle within overlap? */
476 if (r1s < r2s) {
477 if (r2s >= r1s + d2) {
478 VCOPY(cc, c1);
479 return(r1s);
480 }
481 } else {
482 if (r1s >= r2s + d2) {
483 VCOPY(cc, c2);
484 return(r2s);
485 }
486 }
487 a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
488 /* no overlap? */
489 if (a2 <= 0.)
490 return(0.);
491 /* overlap, compute center */
492 l = sqrt((r1s - a2)/d2);
493 for (i = 0; i < 3; i++)
494 cc[i] = c1[i] + l*disp[i];
495 return(a2);
496 }