ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
Revision: 2.107
Committed: Wed Nov 15 18:02:53 2023 UTC (6 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.106: +85 -33 lines
Log Message:
feat(rpict,rtrace,rcontrib,rtpict): Hyperspectral rendering (except photon map)

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.107 static const char RCSid[] = "$Id: rtrace.c,v 2.106 2023/02/06 22:40:21 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * rtrace.c - program and variables for individual ray tracing.
6 greg 2.27 */
7    
8 greg 2.28 #include "copyright.h"
9 greg 1.1
10     /*
11     * Input is in the form:
12     *
13     * xorg yorg zorg xdir ydir zdir
14     *
15     * The direction need not be normalized. Output is flexible.
16 greg 1.7 * If the direction vector is (0,0,0), then the output is flushed.
17 greg 1.1 * All values default to ascii representation of real
18     * numbers. Binary representations can be selected
19     * with '-ff' for float or '-fd' for double. By default,
20 greg 1.13 * radiance is computed. The '-i' or '-I' options indicate that
21 greg 1.1 * irradiance values are desired.
22     */
23 greg 2.32
24     #include <time.h>
25 greg 1.1
26 schorsch 2.30 #include "platform.h"
27 greg 1.1 #include "ray.h"
28 schorsch 2.37 #include "ambient.h"
29     #include "source.h"
30 greg 1.1 #include "otypes.h"
31 greg 2.72 #include "otspecial.h"
32 greg 2.6 #include "resolu.h"
33 schorsch 2.49 #include "random.h"
34 greg 2.6
35 greg 2.57 extern int inform; /* input format */
36     extern int outform; /* output format */
37     extern char *outvals; /* output values */
38 greg 2.27
39 greg 2.57 extern int imm_irrad; /* compute immediate irradiance? */
40     extern int lim_dist; /* limit distance? */
41 greg 1.14
42 greg 2.57 extern char *tralist[]; /* list of modifers to trace (or no) */
43     extern int traincl; /* include == 1, exclude == 0 */
44 greg 1.13
45 greg 2.57 extern int hresolu; /* horizontal resolution */
46     extern int vresolu; /* vertical resolution */
47 greg 1.1
48 greg 2.105 extern int castonly; /* only doing ray-casting? */
49 greg 2.55
50 greg 2.107 extern double (*sens_curve)(SCOLOR scol); /* spectral conversion for 1-channel */
51     extern double out_scalefactor; /* output calibration scale factor */
52     extern RGBPRIMP out_prims; /* output color primitives (NULL if spectral) */
53    
54 greg 2.46 #ifndef MAXTSET
55 greg 2.55 #define MAXTSET 8191 /* maximum number in trace set */
56 greg 2.46 #endif
57 greg 2.15 OBJECT traset[MAXTSET+1]={0}; /* trace include/exclude set */
58    
59 greg 2.102 static int Tflag = 0; /* source tracing enabled? */
60    
61 greg 1.1 static RAY thisray; /* for our convenience */
62    
63 greg 2.89 static FILE *inpfp = NULL; /* input stream pointer */
64    
65     static FVECT *inp_queue = NULL; /* ray input queue if flushing */
66     static int inp_qpos = 0; /* next ray to return */
67     static int inp_qend = 0; /* number of rays in this work group */
68    
69 greg 2.65 typedef void putf_t(RREAL *v, int n);
70 greg 2.107 static putf_t puta, putd, putf;
71 greg 1.1
72 schorsch 2.37 typedef void oputf_t(RAY *r);
73 greg 2.51 static oputf_t oputo, oputd, oputv, oputV, oputl, oputL, oputc, oputp,
74 greg 2.73 oputr, oputR, oputx, oputX, oputn, oputN, oputs,
75     oputw, oputW, oputm, oputM, oputtilde;
76 schorsch 2.37
77 greg 2.92 extern void tranotify(OBJECT obj);
78     static int is_fifo(FILE *fp);
79 schorsch 2.37 static void bogusray(void);
80 greg 2.59 static void raycast(RAY *r);
81     static void rayirrad(RAY *r);
82     static void rtcompute(FVECT org, FVECT dir, double dmax);
83 greg 2.57 static int printvals(RAY *r);
84     static int getvec(FVECT vec, int fmt, FILE *fp);
85 greg 2.89 static int iszerovec(const FVECT vec);
86     static double nextray(FVECT org, FVECT dir);
87 greg 2.57 static void tabin(RAY *r);
88     static void ourtrace(RAY *r);
89 greg 1.1
90 greg 2.107 static void putscolor(const COLORV *scol);
91    
92 greg 2.73 static oputf_t *ray_out[32], *every_out[32];
93 schorsch 2.37 static putf_t *putreal;
94 greg 2.27
95 greg 1.1
96 greg 2.27 void
97 schorsch 2.37 quit( /* quit program */
98     int code
99     )
100 greg 1.1 {
101 greg 2.57 if (ray_pnprocs > 0) /* close children if any */
102     ray_pclose(0);
103 greg 2.99 else if (ray_pnprocs < 0)
104     _exit(code); /* avoid flush() in child */
105 greg 2.57 #ifndef NON_POSIX
106 greg 2.99 else {
107 greg 2.58 headclean(); /* delete header file */
108     pfclean(); /* clean up persist files */
109     }
110 greg 2.11 #endif
111 greg 1.1 exit(code);
112     }
113    
114    
115 greg 2.106 const char *
116 schorsch 2.37 formstr( /* return format identifier */
117     int f
118     )
119 greg 2.6 {
120     switch (f) {
121     case 'a': return("ascii");
122     case 'f': return("float");
123     case 'd': return("double");
124 greg 2.107 case 'c':
125     if (out_prims == NULL)
126     return(SPECFMT);
127     if (out_prims == xyzprims)
128     return(CIEFMT);
129     return(COLRFMT);
130 greg 2.6 }
131     return("unknown");
132     }
133    
134    
135 greg 2.102 static void
136     trace_sources(void) /* trace rays to light sources, also */
137     {
138     int sn;
139    
140     for (sn = 0; sn < nsources; sn++)
141     source[sn].sflags |= SFOLLOW;
142     }
143    
144    
145 greg 2.92 void
146 schorsch 2.37 rtrace( /* trace rays from file */
147 greg 2.59 char *fname,
148     int nproc
149 schorsch 2.37 )
150 greg 1.1 {
151 greg 2.45 unsigned long vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu
152 greg 2.66 : (unsigned long)vresolu;
153 greg 2.85 long nextflush = (!vresolu | (hresolu <= 1)) * hresolu;
154 greg 2.79 int something2flush = 0;
155 greg 1.1 FILE *fp;
156 gregl 2.25 double d;
157 greg 1.2 FVECT orig, direc;
158 greg 1.1 /* set up input */
159     if (fname == NULL)
160 greg 2.89 inpfp = stdin;
161     else if ((inpfp = fopen(fname, "r")) == NULL) {
162 greg 1.1 sprintf(errmsg, "cannot open input file \"%s\"", fname);
163     error(SYSTEM, errmsg);
164     }
165 greg 2.89 #ifdef getc_unlocked
166     flockfile(inpfp); /* avoid lock/unlock overhead */
167     flockfile(stdout);
168     #endif
169 greg 2.8 if (inform != 'a')
170 greg 2.89 SET_FILE_BINARY(inpfp);
171 greg 1.1 /* set up output */
172 greg 2.94 if (castonly || every_out[0] != NULL)
173 greg 2.59 nproc = 1; /* don't bother multiprocessing */
174 greg 2.104 if (every_out[0] != NULL) {
175     trace = ourtrace; /* enable full tree tracing */
176     if (Tflag) /* light sources, too? */
177     trace_sources();
178     }
179 greg 2.79 if ((nextflush > 0) & (nproc > nextflush)) {
180     error(WARNING, "reducing number of processes to match flush interval");
181     nproc = nextflush;
182     }
183 greg 2.59 if (nproc > 1) { /* start multiprocessing */
184     ray_popen(nproc);
185 greg 2.58 ray_fifo_out = printvals;
186 greg 2.59 }
187 greg 2.12 if (hresolu > 0) {
188     if (vresolu > 0)
189     fprtresolu(hresolu, vresolu, stdout);
190 greg 2.78 else
191     fflush(stdout);
192 greg 2.12 }
193 greg 2.89 /* process input rays */
194     while ((d = nextray(orig, direc)) >= 0.0) {
195 greg 2.87 if (d == 0.0) { /* flush request? */
196 greg 2.85 if (something2flush) {
197 greg 2.74 if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
198 greg 2.71 error(USER, "child(ren) died");
199 greg 2.87 bogusray();
200 gregl 2.24 fflush(stdout);
201 greg 2.86 nextflush = (!vresolu | (hresolu <= 1)) * hresolu;
202 greg 2.79 something2flush = 0;
203 greg 2.87 } else
204     bogusray();
205 greg 2.57 } else { /* compute and print */
206 greg 2.59 rtcompute(orig, direc, lim_dist ? d : 0.0);
207 greg 2.89 if (!--nextflush) { /* flush if time */
208 greg 2.74 if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
209 greg 2.71 error(USER, "child(ren) died");
210 gregl 2.24 fflush(stdout);
211     nextflush = hresolu;
212 greg 2.78 } else
213 greg 2.79 something2flush = 1;
214 greg 1.1 }
215     if (ferror(stdout))
216     error(SYSTEM, "write error");
217 greg 2.45 if (vcount && !--vcount) /* check for end */
218 greg 1.1 break;
219     }
220 greg 2.74 if (ray_pnprocs > 1) { /* clean up children */
221 greg 2.59 if (ray_fifo_flush() < 0)
222     error(USER, "unable to complete processing");
223     ray_pclose(0);
224     }
225 greg 2.89 if (vcount)
226     error(WARNING, "unexpected EOF on input");
227 greg 2.50 if (fflush(stdout) < 0)
228     error(SYSTEM, "write error");
229 greg 2.89 if (fname != NULL) {
230     fclose(inpfp);
231     inpfp = NULL;
232     }
233     nextray(NULL, NULL);
234 greg 1.1 }
235    
236    
237 greg 2.91 int
238     setrtoutput(void) /* set up output tables, return #comp */
239 greg 1.1 {
240 greg 2.91 char *vs = outvals;
241 greg 2.56 oputf_t **table = ray_out;
242 greg 2.107 const int nco = (sens_curve != NULL) ? 1 :
243     (out_prims != NULL) ? 3 : NCSAMP;
244 greg 2.91 int ncomp = 0;
245    
246     if (!*vs)
247     error(USER, "empty output specification");
248 greg 1.1
249 greg 2.103 switch (outform) { /* make sure (*putreal)() calls someone! */
250     case 'a': putreal = puta; break;
251     case 'f': putreal = putf; break;
252     case 'd': putreal = putd; break;
253     case 'c':
254     if (outvals[1] || !strchr("vrx", outvals[0]))
255     error(USER, "color format only with -ov, -or, -ox");
256 greg 2.107 break;
257 greg 2.103 default:
258     error(CONSISTENCY, "botched output format");
259     }
260 greg 2.94 castonly = 1; /* sets castonly as side-effect */
261 greg 2.91 do
262     switch (*vs) {
263 greg 2.40 case 'T': /* trace sources */
264 greg 2.102 Tflag++;
265 greg 2.40 /* fall through */
266 greg 1.1 case 't': /* trace */
267 greg 2.91 if (!vs[1]) break;
268 greg 1.1 *table = NULL;
269     table = every_out;
270 greg 1.11 castonly = 0;
271 greg 1.1 break;
272     case 'o': /* origin */
273     *table++ = oputo;
274 greg 2.91 ncomp += 3;
275 greg 1.1 break;
276     case 'd': /* direction */
277     *table++ = oputd;
278 greg 2.91 ncomp += 3;
279 greg 1.1 break;
280 greg 2.73 case 'r': /* reflected contrib. */
281     *table++ = oputr;
282 greg 2.107 ncomp += nco;
283 greg 2.73 castonly = 0;
284     break;
285     case 'R': /* reflected distance */
286     *table++ = oputR;
287 greg 2.91 ncomp++;
288 greg 2.73 castonly = 0;
289     break;
290     case 'x': /* xmit contrib. */
291     *table++ = oputx;
292 greg 2.107 ncomp += nco;
293 greg 2.73 castonly = 0;
294     break;
295     case 'X': /* xmit distance */
296     *table++ = oputX;
297 greg 2.91 ncomp++;
298 greg 2.73 castonly = 0;
299     break;
300 greg 1.1 case 'v': /* value */
301     *table++ = oputv;
302 greg 2.107 ncomp += nco;
303 greg 1.11 castonly = 0;
304 greg 1.1 break;
305 greg 2.51 case 'V': /* contribution */
306     *table++ = oputV;
307 greg 2.107 ncomp += nco;
308 greg 2.94 castonly = 0;
309 greg 2.51 if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
310     error(WARNING,
311     "-otV accuracy depends on -aa 0 -as 0");
312     break;
313 greg 2.5 case 'l': /* effective distance */
314 greg 1.1 *table++ = oputl;
315 greg 2.91 ncomp++;
316 greg 1.11 castonly = 0;
317 greg 1.1 break;
318 greg 2.29 case 'c': /* local coordinates */
319     *table++ = oputc;
320 greg 2.91 ncomp += 2;
321 greg 2.29 break;
322 greg 2.5 case 'L': /* single ray length */
323     *table++ = oputL;
324 greg 2.91 ncomp++;
325 greg 2.5 break;
326 greg 1.1 case 'p': /* point */
327     *table++ = oputp;
328 greg 2.91 ncomp += 3;
329 greg 1.1 break;
330 greg 2.9 case 'n': /* perturbed normal */
331 greg 1.1 *table++ = oputn;
332 greg 2.91 ncomp += 3;
333 greg 2.9 castonly = 0;
334 greg 1.1 break;
335 greg 2.9 case 'N': /* unperturbed normal */
336     *table++ = oputN;
337 greg 2.91 ncomp += 3;
338 greg 2.9 break;
339 greg 1.1 case 's': /* surface */
340     *table++ = oputs;
341 greg 2.91 ncomp++;
342 greg 1.1 break;
343     case 'w': /* weight */
344     *table++ = oputw;
345 greg 2.91 ncomp++;
346 greg 1.1 break;
347 greg 2.40 case 'W': /* coefficient */
348     *table++ = oputW;
349 greg 2.107 ncomp += nco;
350 greg 2.73 castonly = 0;
351 greg 2.80 if (ambounce > 0 && (ambacc > FTINY) | (ambssamp > 0))
352 greg 2.40 error(WARNING,
353 greg 2.41 "-otW accuracy depends on -aa 0 -as 0");
354 greg 2.40 break;
355 greg 1.1 case 'm': /* modifier */
356     *table++ = oputm;
357 greg 2.91 ncomp++;
358 greg 1.1 break;
359 greg 2.39 case 'M': /* material */
360     *table++ = oputM;
361 greg 2.91 ncomp++;
362 greg 2.39 break;
363 greg 2.42 case '~': /* tilde */
364     *table++ = oputtilde;
365 greg 2.41 break;
366 greg 2.91 default:
367     sprintf(errmsg, "unrecognized output option '%c'", *vs);
368     error(USER, errmsg);
369 greg 1.1 }
370 greg 2.91 while (*++vs);
371    
372 greg 1.1 *table = NULL;
373 greg 2.91 if (*every_out != NULL)
374     ncomp = 0;
375 greg 2.81 /* compatibility */
376 greg 2.94 if ((do_irrad | imm_irrad) && castonly)
377     error(USER, "-I+ and -i+ options require some value output");
378 greg 2.81 for (table = ray_out; *table != NULL; table++) {
379     if ((*table == oputV) | (*table == oputW))
380     error(WARNING, "-oVW options require trace mode");
381     if ((do_irrad | imm_irrad) &&
382     (*table == oputr) | (*table == oputR) |
383     (*table == oputx) | (*table == oputX))
384     error(WARNING, "-orRxX options incompatible with -I+ and -i+");
385     }
386 greg 2.91 return(ncomp);
387 gregl 2.24 }
388    
389    
390 schorsch 2.37 static void
391     bogusray(void) /* print out empty record */
392 gregl 2.24 {
393 greg 2.40 rayorigin(&thisray, PRIMARY, NULL, NULL);
394 gregl 2.24 printvals(&thisray);
395 greg 1.1 }
396    
397    
398 schorsch 2.37 static void
399 greg 2.59 raycast( /* compute first ray intersection only */
400     RAY *r
401 schorsch 2.37 )
402 greg 1.1 {
403 greg 2.59 if (!localhit(r, &thescene)) {
404     if (r->ro == &Aftplane) { /* clipped */
405     r->ro = NULL;
406     r->rot = FHUGE;
407     } else
408     sourcehit(r);
409 greg 2.57 }
410 greg 1.1 }
411    
412    
413 schorsch 2.37 static void
414 greg 2.59 rayirrad( /* compute irradiance rather than radiance */
415     RAY *r
416     )
417     {
418 greg 2.62 void (*old_revf)(RAY *) = r->revf;
419 greg 2.75 /* pretend we hit surface */
420 greg 2.76 r->rxt = r->rot = 1e-5;
421 greg 2.59 VSUM(r->rop, r->rorg, r->rdir, r->rot);
422     r->ron[0] = -r->rdir[0];
423     r->ron[1] = -r->rdir[1];
424     r->ron[2] = -r->rdir[2];
425     r->rod = 1.0;
426 greg 2.62 /* compute result */
427 greg 2.61 r->revf = raytrace;
428 greg 2.59 (*ofun[Lamb.otype].funp)(&Lamb, r);
429 greg 2.62 r->revf = old_revf;
430 greg 2.59 }
431    
432    
433     static void
434     rtcompute( /* compute and print ray value(s) */
435 schorsch 2.37 FVECT org,
436 greg 2.59 FVECT dir,
437     double dmax
438 schorsch 2.37 )
439 greg 1.1 {
440 greg 2.60 /* set up ray */
441     if (imm_irrad) {
442 greg 2.59 VSUM(thisray.rorg, org, dir, 1.1e-4);
443     thisray.rdir[0] = -dir[0];
444     thisray.rdir[1] = -dir[1];
445     thisray.rdir[2] = -dir[2];
446     thisray.rmax = 0.0;
447     } else {
448     VCOPY(thisray.rorg, org);
449     VCOPY(thisray.rdir, dir);
450     thisray.rmax = dmax;
451     }
452 greg 2.101 rayorigin(&thisray, PRIMARY, NULL, NULL);
453     if (imm_irrad)
454     thisray.revf = rayirrad;
455     else if (castonly)
456     thisray.revf = raycast;
457 greg 2.59 if (ray_pnprocs > 1) { /* multiprocessing FIFO? */
458     if (ray_fifo_in(&thisray) < 0)
459     error(USER, "lost children");
460     return;
461     }
462     samplendx++; /* else do it ourselves */
463     rayvalue(&thisray);
464 greg 2.16 printvals(&thisray);
465     }
466    
467    
468 greg 2.57 static int
469 schorsch 2.37 printvals( /* print requested ray values */
470     RAY *r
471     )
472 greg 2.16 {
473 greg 2.56 oputf_t **tp;
474 greg 2.16
475     if (ray_out[0] == NULL)
476 greg 2.57 return(0);
477 greg 2.16 for (tp = ray_out; *tp != NULL; tp++)
478     (**tp)(r);
479 greg 1.1 if (outform == 'a')
480     putchar('\n');
481 greg 2.57 return(1);
482 greg 1.1 }
483    
484    
485 schorsch 2.37 static int
486 greg 2.92 is_fifo( /* check if file pointer connected to pipe */
487     FILE *fp
488     )
489     {
490     #ifdef S_ISFIFO
491     struct stat sbuf;
492    
493     if (fstat(fileno(fp), &sbuf) < 0)
494     error(SYSTEM, "fstat() failed on input stream");
495     return(S_ISFIFO(sbuf.st_mode));
496     #else
497     return (fp == stdin); /* just a guess, really */
498     #endif
499     }
500    
501    
502     static int
503 greg 2.89 getvec( /* get a vector from fp */
504 greg 2.56 FVECT vec,
505 schorsch 2.37 int fmt,
506     FILE *fp
507     )
508 greg 1.1 {
509     static float vf[3];
510 greg 2.5 static double vd[3];
511 greg 1.19 char buf[32];
512 greg 2.56 int i;
513 greg 1.1
514     switch (fmt) {
515     case 'a': /* ascii */
516 greg 1.19 for (i = 0; i < 3; i++) {
517     if (fgetword(buf, sizeof(buf), fp) == NULL ||
518     !isflt(buf))
519     return(-1);
520     vec[i] = atof(buf);
521     }
522 greg 1.1 break;
523     case 'f': /* binary float */
524 greg 2.70 if (getbinary(vf, sizeof(float), 3, fp) != 3)
525 greg 1.1 return(-1);
526 greg 2.58 VCOPY(vec, vf);
527 greg 1.1 break;
528     case 'd': /* binary double */
529 greg 2.70 if (getbinary(vd, sizeof(double), 3, fp) != 3)
530 greg 1.1 return(-1);
531 greg 2.58 VCOPY(vec, vd);
532 greg 1.1 break;
533 greg 2.6 default:
534     error(CONSISTENCY, "botched input format");
535 greg 1.1 }
536     return(0);
537     }
538    
539    
540 greg 2.89 static int
541     iszerovec(const FVECT vec)
542     {
543     return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0);
544     }
545    
546    
547     static double
548     nextray( /* return next ray in work group (-1.0 if EOF) */
549     FVECT org,
550     FVECT dir
551     )
552     {
553     const size_t qlength = !vresolu * hresolu;
554    
555     if ((org == NULL) | (dir == NULL)) {
556     if (inp_queue != NULL) /* asking to free queue */
557     free(inp_queue);
558     inp_queue = NULL;
559     inp_qpos = inp_qend = 0;
560     return(-1.);
561     }
562     if (!inp_qend) { /* initialize FIFO queue */
563     int rsiz = 6*20; /* conservative ascii ray size */
564     if (inform == 'f') rsiz = 6*sizeof(float);
565     else if (inform == 'd') rsiz = 6*sizeof(double);
566 greg 2.92 /* check against pipe limit */
567     if (qlength*rsiz > 512 && is_fifo(inpfp))
568 greg 2.89 inp_queue = (FVECT *)malloc(sizeof(FVECT)*2*qlength);
569     inp_qend = -(inp_queue == NULL); /* flag for no queue */
570     }
571     if (inp_qend < 0) { /* not queuing? */
572     if (getvec(org, inform, inpfp) < 0 ||
573     getvec(dir, inform, inpfp) < 0)
574     return(-1.);
575     return normalize(dir);
576     }
577     if (inp_qpos >= inp_qend) { /* need to refill input queue? */
578     for (inp_qend = 0; inp_qend < qlength; inp_qend++) {
579     if (getvec(inp_queue[2*inp_qend], inform, inpfp) < 0
580     || getvec(inp_queue[2*inp_qend+1],
581     inform, inpfp) < 0)
582     break; /* hit EOF */
583     if (iszerovec(inp_queue[2*inp_qend+1])) {
584     ++inp_qend; /* flush request */
585     break;
586     }
587     }
588     inp_qpos = 0;
589     }
590     if (inp_qpos >= inp_qend) /* unexpected EOF? */
591     return(-1.);
592     VCOPY(org, inp_queue[2*inp_qpos]);
593     VCOPY(dir, inp_queue[2*inp_qpos+1]);
594     ++inp_qpos;
595     return normalize(dir);
596     }
597    
598    
599 greg 2.57 void
600 schorsch 2.37 tranotify( /* record new modifier */
601     OBJECT obj
602     )
603 greg 2.15 {
604     static int hitlimit = 0;
605 greg 2.56 OBJREC *o = objptr(obj);
606     char **tralp;
607 greg 2.15
608 greg 2.27 if (obj == OVOID) { /* starting over */
609     traset[0] = 0;
610     hitlimit = 0;
611     return;
612     }
613 greg 2.15 if (hitlimit || !ismodifier(o->otype))
614     return;
615     for (tralp = tralist; *tralp != NULL; tralp++)
616     if (!strcmp(o->oname, *tralp)) {
617     if (traset[0] >= MAXTSET) {
618     error(WARNING, "too many modifiers in trace list");
619     hitlimit++;
620     return; /* should this be fatal? */
621     }
622     insertelem(traset, obj);
623     return;
624     }
625     }
626    
627    
628 greg 2.27 static void
629 schorsch 2.37 ourtrace( /* print ray values */
630     RAY *r
631     )
632 greg 1.1 {
633 greg 2.56 oputf_t **tp;
634 greg 1.1
635     if (every_out[0] == NULL)
636 greg 2.15 return;
637 greg 2.16 if (r->ro == NULL) {
638     if (traincl == 1)
639     return;
640     } else if (traincl != -1 && traincl != inset(traset, r->ro->omod))
641 greg 1.1 return;
642     tabin(r);
643     for (tp = every_out; *tp != NULL; tp++)
644     (**tp)(r);
645 greg 2.41 if (outform == 'a')
646     putchar('\n');
647 greg 1.1 }
648    
649    
650 greg 2.27 static void
651 schorsch 2.37 tabin( /* tab in appropriate amount */
652     RAY *r
653     )
654 greg 1.1 {
655 greg 2.40 const RAY *rp;
656 greg 1.1
657     for (rp = r->parent; rp != NULL; rp = rp->parent)
658     putchar('\t');
659     }
660    
661    
662 greg 2.27 static void
663 schorsch 2.37 oputo( /* print origin */
664     RAY *r
665     )
666 greg 1.1 {
667 greg 2.65 (*putreal)(r->rorg, 3);
668 greg 1.1 }
669    
670    
671 greg 2.27 static void
672 schorsch 2.37 oputd( /* print direction */
673     RAY *r
674     )
675 greg 1.1 {
676 greg 2.65 (*putreal)(r->rdir, 3);
677 greg 1.1 }
678    
679    
680 greg 2.27 static void
681 greg 2.73 oputr( /* print mirrored contribution */
682     RAY *r
683     )
684     {
685     RREAL cval[3];
686    
687     cval[0] = colval(r->mcol,RED);
688     cval[1] = colval(r->mcol,GRN);
689     cval[2] = colval(r->mcol,BLU);
690     (*putreal)(cval, 3);
691     }
692    
693    
694    
695     static void
696     oputR( /* print mirrored distance */
697     RAY *r
698     )
699     {
700     (*putreal)(&r->rmt, 1);
701     }
702    
703    
704     static void
705     oputx( /* print unmirrored contribution */
706     RAY *r
707     )
708     {
709 greg 2.107 SCOLOR cdiff;
710    
711     copyscolor(cdiff, r->rcol);
712     sopscolor(cdiff, -=, r->mcol);
713 greg 2.73
714 greg 2.107 putscolor(cdiff);
715 greg 2.73 }
716    
717    
718     static void
719     oputX( /* print unmirrored distance */
720     RAY *r
721     )
722     {
723     (*putreal)(&r->rxt, 1);
724     }
725    
726    
727     static void
728 schorsch 2.37 oputv( /* print value */
729     RAY *r
730     )
731 greg 1.1 {
732 greg 2.107 putscolor(r->rcol);
733 greg 1.1 }
734    
735    
736 greg 2.27 static void
737 greg 2.51 oputV( /* print value contribution */
738     RAY *r
739     )
740     {
741 greg 2.107 SCOLOR contr;
742 greg 2.51
743     raycontrib(contr, r, PRIMARY);
744 greg 2.107 smultscolor(contr, r->rcol);
745     putscolor(contr);
746 greg 2.51 }
747    
748    
749     static void
750 schorsch 2.37 oputl( /* print effective distance */
751     RAY *r
752     )
753 greg 1.1 {
754 greg 2.73 RREAL d = raydistance(r);
755    
756     (*putreal)(&d, 1);
757 greg 2.5 }
758    
759    
760 greg 2.27 static void
761 schorsch 2.37 oputL( /* print single ray length */
762     RAY *r
763     )
764 greg 2.5 {
765 greg 2.65 (*putreal)(&r->rot, 1);
766 greg 1.1 }
767    
768    
769 greg 2.27 static void
770 schorsch 2.37 oputc( /* print local coordinates */
771     RAY *r
772     )
773 greg 2.29 {
774 greg 2.65 (*putreal)(r->uv, 2);
775 greg 2.29 }
776    
777    
778 greg 2.65 static RREAL vdummy[3] = {0.0, 0.0, 0.0};
779    
780    
781 greg 2.29 static void
782 greg 2.100 oputp( /* print intersection point */
783 schorsch 2.37 RAY *r
784     )
785 greg 1.1 {
786 greg 2.100 (*putreal)(r->rop, 3); /* set to ray origin if distant or no hit */
787 greg 1.1 }
788    
789    
790 greg 2.27 static void
791 schorsch 2.37 oputN( /* print unperturbed normal */
792     RAY *r
793     )
794 greg 1.1 {
795 greg 2.100 if (r->ro == NULL) { /* zero vector if clipped or no hit */
796     (*putreal)(vdummy, 3);
797     return;
798     }
799     if (r->rflips & 1) { /* undo any flippin' flips */
800     FVECT unrm;
801     unrm[0] = -r->ron[0];
802     unrm[1] = -r->ron[1];
803     unrm[2] = -r->ron[2];
804     (*putreal)(unrm, 3);
805 greg 2.82 } else
806 greg 2.100 (*putreal)(r->ron, 3);
807 greg 2.9 }
808    
809    
810 greg 2.27 static void
811 schorsch 2.37 oputn( /* print perturbed normal */
812     RAY *r
813     )
814 greg 2.9 {
815     FVECT pnorm;
816    
817 greg 2.100 if (r->ro == NULL) { /* clipped or no hit */
818 greg 2.65 (*putreal)(vdummy, 3);
819 greg 2.9 return;
820     }
821     raynormal(pnorm, r);
822 greg 2.65 (*putreal)(pnorm, 3);
823 greg 1.1 }
824    
825    
826 greg 2.27 static void
827 schorsch 2.37 oputs( /* print name */
828     RAY *r
829     )
830 greg 1.1 {
831     if (r->ro != NULL)
832     fputs(r->ro->oname, stdout);
833     else
834     putchar('*');
835     putchar('\t');
836     }
837    
838    
839 greg 2.27 static void
840 schorsch 2.37 oputw( /* print weight */
841     RAY *r
842     )
843 greg 1.1 {
844 greg 2.65 RREAL rwt = r->rweight;
845    
846     (*putreal)(&rwt, 1);
847 greg 1.1 }
848    
849    
850 greg 2.27 static void
851 greg 2.51 oputW( /* print coefficient */
852 greg 2.40 RAY *r
853     )
854     {
855 greg 2.107 SCOLOR contr;
856 greg 2.67 /* shadow ray not on source? */
857     if (r->rsrc >= 0 && source[r->rsrc].so != r->ro)
858 greg 2.107 scolorblack(contr);
859 greg 2.67 else
860     raycontrib(contr, r, PRIMARY);
861 greg 2.40
862 greg 2.107 putscolor(contr);
863 greg 2.40 }
864    
865    
866     static void
867 schorsch 2.37 oputm( /* print modifier */
868     RAY *r
869     )
870 greg 1.1 {
871     if (r->ro != NULL)
872 greg 2.23 if (r->ro->omod != OVOID)
873     fputs(objptr(r->ro->omod)->oname, stdout);
874     else
875     fputs(VOIDID, stdout);
876 greg 1.1 else
877     putchar('*');
878     putchar('\t');
879     }
880    
881    
882 greg 2.27 static void
883 greg 2.39 oputM( /* print material */
884     RAY *r
885     )
886     {
887     OBJREC *mat;
888    
889     if (r->ro != NULL) {
890     if ((mat = findmaterial(r->ro)) != NULL)
891     fputs(mat->oname, stdout);
892     else
893     fputs(VOIDID, stdout);
894     } else
895     putchar('*');
896     putchar('\t');
897     }
898    
899    
900     static void
901 greg 2.42 oputtilde( /* output tilde (spacer) */
902 greg 2.41 RAY *r
903     )
904     {
905 greg 2.42 fputs("~\t", stdout);
906 greg 2.41 }
907    
908    
909     static void
910 greg 2.65 puta( /* print ascii value(s) */
911     RREAL *v, int n
912 schorsch 2.37 )
913 greg 1.1 {
914 greg 2.65 if (n == 3) {
915     printf("%e\t%e\t%e\t", v[0], v[1], v[2]);
916     return;
917     }
918     while (n--)
919     printf("%e\t", *v++);
920 greg 1.1 }
921    
922    
923 greg 2.27 static void
924 greg 2.80 putd(RREAL *v, int n) /* output binary double(s) */
925 greg 1.1 {
926 greg 2.68 #ifdef SMLFLT
927     double da[3];
928     int i;
929    
930     if (n > 3)
931 greg 2.65 error(INTERNAL, "code error in putd()");
932 greg 2.68 for (i = n; i--; )
933     da[i] = v[i];
934 greg 2.69 putbinary(da, sizeof(double), n, stdout);
935 greg 2.68 #else
936 greg 2.69 putbinary(v, sizeof(RREAL), n, stdout);
937 greg 2.68 #endif
938 greg 1.1 }
939    
940    
941 greg 2.27 static void
942 greg 2.80 putf(RREAL *v, int n) /* output binary float(s) */
943 greg 1.1 {
944 greg 2.68 #ifndef SMLFLT
945 greg 2.65 float fa[3];
946     int i;
947 greg 1.1
948 greg 2.65 if (n > 3)
949     error(INTERNAL, "code error in putf()");
950     for (i = n; i--; )
951     fa[i] = v[i];
952 greg 2.69 putbinary(fa, sizeof(float), n, stdout);
953 greg 2.68 #else
954 greg 2.69 putbinary(v, sizeof(RREAL), n, stdout);
955 greg 2.68 #endif
956 greg 1.1 }
957 greg 2.80
958    
959     static void
960 greg 2.107 putscolor(const COLORV *scol) /* output (spectral) color */
961 greg 2.80 {
962 greg 2.107 static COLORMAT xyz2myrgbmat;
963     SCOLOR my_scol;
964     COLOR col;
965     /* apply scalefactor if any */
966     if (out_scalefactor != 1.) {
967     copyscolor(my_scol, scol);
968     scalescolor(my_scol, out_scalefactor);
969     scol = my_scol;
970     }
971     if (sens_curve != NULL) { /* single channel output */
972     RREAL v = (*sens_curve)(scol);
973     (*putreal)(&v, 1);
974     return;
975     }
976     if (out_prims == NULL) { /* full spectral reporting */
977     if (outform == 'c') {
978     SCOLR sclr;
979     scolor_scolr(sclr, scol);
980     putbinary(sclr, LSCOLR, 1, stdout);
981     } else if (sizeof(RREAL) != sizeof(COLORV)) {
982     RREAL sreal[MAXCSAMP];
983     int i = NCSAMP;
984     while (i--) sreal[i] = scol[i];
985     (*putreal)(sreal, NCSAMP);
986     } else
987     (*putreal)((RREAL *)scol, NCSAMP);
988     return;
989     }
990     if (out_prims == xyzprims) {
991     scolor_cie(col, scol);
992     } else if (out_prims == stdprims) {
993     scolor_rgb(col, scol);
994     } else {
995     COLOR xyz;
996     if (xyz2myrgbmat[0][0] == 0)
997     compxyz2rgbWBmat(xyz2myrgbmat, out_prims);
998     scolor_cie(xyz, scol);
999     colortrans(col, xyz2myrgbmat, xyz);
1000     clipgamut(col, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite);
1001     }
1002     if (outform == 'c') {
1003     COLR clr;
1004     setcolr(clr, colval(col,RED), colval(col,GRN), colval(col,BLU));
1005     putbinary(clr, sizeof(COLR), 1, stdout);
1006     } else if (sizeof(RREAL) != sizeof(COLORV)) {
1007     RREAL creal[3];
1008     copycolor(creal, col);
1009     (*putreal)(creal, 3);
1010     } else
1011     (*putreal)((RREAL *)col, 3);
1012 greg 2.80 }