ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsupp.c
Revision: 2.24
Committed: Thu Aug 4 22:43:46 2022 UTC (20 months, 4 weeks ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.23: +25 -7 lines
Log Message:
fix: reduced aiming failures for triangular light sources

File Contents

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