ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/srcsupp.c
Revision: 1.5
Committed: Mon Jun 24 16:00:30 1991 UTC (32 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.4: +9 -6 lines
Log Message:
made sure center point returned by fgetdisk() was in plane

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;
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 int
48 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 return(-1);
57 source[nsources].sflags = 0;
58 source[nsources].nhits = 1;
59 source[nsources].ntests = 2; /* initial hit probability = 1/2 */
60 return(nsources++);
61 }
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 double d;
172 register int i, j;
173 register FACE *f;
174
175 f = getface(op);
176 if (f->area == 0.)
177 return(0.);
178 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 d = DOT(ocent,f->norm);
185 for (i = 0; i < 3; i++)
186 ocent[i] += (f->offset - d)*f->norm[i];
187 maxrad2 = 0.;
188 for (j = 0; j < f->nv; j++) {
189 d = dist2(VERTEX(f,j), ocent);
190 if (d > maxrad2)
191 maxrad2 = d;
192 }
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 }
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 intercircle(cc, c1, c2, r1s, r2s) /* intersect two circles */
300 FVECT cc; /* midpoint (return value) */
301 FVECT c1, c2; /* circle centers */
302 double r1s, r2s; /* radii squared */
303 {
304 double a2, d2, l;
305 FVECT disp;
306 register int i;
307
308 for (i = 0; i < 3; i++)
309 disp[i] = c2[i] - c1[i];
310 d2 = DOT(disp,disp);
311 /* circle within overlap? */
312 if (r1s < r2s) {
313 if (r2s >= r1s + d2) {
314 VCOPY(cc, c1);
315 return(r1s);
316 }
317 } else {
318 if (r1s >= r2s + d2) {
319 VCOPY(cc, c2);
320 return(r2s);
321 }
322 }
323 a2 = .25*(2.*(r1s+r2s) - d2 - (r2s-r1s)*(r2s-r1s)/d2);
324 /* no overlap? */
325 if (a2 <= 0.)
326 return(0.);
327 /* overlap, compute center */
328 l = sqrt((r1s - a2)/d2);
329 for (i = 0; i < 3; i++)
330 cc[i] = c1[i] + l*disp[i];
331 return(a2);
332 }
333
334
335 sourcehit(r) /* check to see if ray hit distant source */
336 register RAY *r;
337 {
338 int first, last;
339 register int i;
340
341 if (r->rsrc >= 0) { /* check only one if aimed */
342 first = last = r->rsrc;
343 } else { /* otherwise check all */
344 first = 0; last = nsources-1;
345 }
346 for (i = first; i <= last; i++)
347 if (source[i].sflags & SDISTANT)
348 /*
349 * Check to see if ray is within
350 * solid angle of source.
351 */
352 if (2.0*PI * (1.0 - DOT(source[i].sloc,r->rdir))
353 <= source[i].ss2) {
354 r->ro = source[i].so;
355 if (!(source[i].sflags & SSKIP))
356 break;
357 }
358
359 if (r->ro != NULL) {
360 for (i = 0; i < 3; i++)
361 r->ron[i] = -r->rdir[i];
362 r->rod = 1.0;
363 r->rox = NULL;
364 return(1);
365 }
366 return(0);
367 }
368
369
370 #define wrongsource(m, r) (m->otype!=MAT_ILLUM && \
371 r->rsrc>=0 && \
372 source[r->rsrc].so!=r->ro)
373
374 #define badambient(m, r) ((r->crtype&(AMBIENT|SHADOW))==AMBIENT && \
375 !(m->otype==MAT_GLOW&&r->rot>m->oargs.farg[3]))
376
377 #define passillum(m, r) (m->otype==MAT_ILLUM && \
378 !(r->rsrc>=0&&source[r->rsrc].so==r->ro))
379
380
381 m_light(m, r) /* ray hit a light source */
382 register OBJREC *m;
383 register RAY *r;
384 {
385 /* check for over-counting */
386 if (wrongsource(m, r) || badambient(m, r))
387 return;
388 /* check for passed illum */
389 if (passillum(m, r)) {
390
391 if (m->oargs.nsargs < 1 || !strcmp(m->oargs.sarg[0], VOIDID))
392 raytrans(r);
393 else
394 rayshade(r, modifier(m->oargs.sarg[0]));
395
396 /* otherwise treat as source */
397 } else {
398 /* check for behind */
399 if (r->rod < 0.0)
400 return;
401 /* get distribution pattern */
402 raytexture(r, m->omod);
403 /* get source color */
404 setcolor(r->rcol, m->oargs.farg[0],
405 m->oargs.farg[1],
406 m->oargs.farg[2]);
407 /* modify value */
408 multcolor(r->rcol, r->pcol);
409 }
410 }