ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsupp.c
Revision: 1.13
Committed: Fri Sep 13 13:44:10 1991 UTC (32 years, 7 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.12: +5 -3 lines
Log Message:
made source array allocation incremental

File Contents

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