ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtrace.c
(Generate patch)

Comparing ray/src/rt/rtrace.c (file contents):
Revision 2.27 by greg, Sat Feb 22 02:07:29 2003 UTC vs.
Revision 2.110 by greg, Mon Dec 11 18:33:53 2023 UTC

# Line 5 | Line 5 | static const char      RCSid[] = "$Id$";
5   *  rtrace.c - program and variables for individual ray tracing.
6   */
7  
8 < /* ====================================================================
9 < * The Radiance Software License, Version 1.0
10 < *
11 < * Copyright (c) 1990 - 2002 The Regents of the University of California,
12 < * through Lawrence Berkeley National Laboratory.   All rights reserved.
13 < *
14 < * Redistribution and use in source and binary forms, with or without
15 < * modification, are permitted provided that the following conditions
16 < * are met:
17 < *
18 < * 1. Redistributions of source code must retain the above copyright
19 < *         notice, this list of conditions and the following disclaimer.
20 < *
21 < * 2. Redistributions in binary form must reproduce the above copyright
22 < *       notice, this list of conditions and the following disclaimer in
23 < *       the documentation and/or other materials provided with the
24 < *       distribution.
25 < *
26 < * 3. The end-user documentation included with the redistribution,
27 < *           if any, must include the following acknowledgment:
28 < *             "This product includes Radiance software
29 < *                 (http://radsite.lbl.gov/)
30 < *                 developed by the Lawrence Berkeley National Laboratory
31 < *               (http://www.lbl.gov/)."
32 < *       Alternately, this acknowledgment may appear in the software itself,
33 < *       if and wherever such third-party acknowledgments normally appear.
34 < *
35 < * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
36 < *       and "The Regents of the University of California" must
37 < *       not be used to endorse or promote products derived from this
38 < *       software without prior written permission. For written
39 < *       permission, please contact [email protected].
40 < *
41 < * 5. Products derived from this software may not be called "Radiance",
42 < *       nor may "Radiance" appear in their name, without prior written
43 < *       permission of Lawrence Berkeley National Laboratory.
44 < *
45 < * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
46 < * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
47 < * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
48 < * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
49 < * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
50 < * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
51 < * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
52 < * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
53 < * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 < * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
55 < * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 < * SUCH DAMAGE.
57 < * ====================================================================
58 < *
59 < * This software consists of voluntary contributions made by many
60 < * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
61 < * information on Lawrence Berkeley National Laboratory, please see
62 < * <http://www.lbl.gov/>.
63 < */
8 > #include "copyright.h"
9  
10   /*
11   *  Input is in the form:
# Line 76 | Line 21 | static const char      RCSid[] = "$Id$";
21   *  irradiance values are desired.
22   */
23  
24 < #include  "ray.h"
24 > #include  <time.h>
25  
26 + #include  "platform.h"
27 + #include  "ray.h"
28 + #include  "ambient.h"
29 + #include  "source.h"
30   #include  "otypes.h"
31 <
31 > #include  "otspecial.h"
32   #include  "resolu.h"
33 + #include  "random.h"
34  
35 < CUBE  thescene;                         /* our scene */
36 < OBJECT  nsceneobjs;                     /* number of objects in our scene */
35 > extern int  inform;                     /* input format */
36 > extern int  outform;                    /* output format */
37 > extern char  *outvals;                  /* output values */
38  
39 < int  dimlist[MAXDIM];                   /* sampling dimensions */
40 < int  ndims = 0;                         /* number of sampling dimensions */
90 < int  samplendx = 0;                     /* index for this sample */
39 > extern int  imm_irrad;                  /* compute immediate irradiance? */
40 > extern int  lim_dist;                   /* limit distance? */
41  
42 < int  imm_irrad = 0;                     /* compute immediate irradiance? */
43 < int  lim_dist = 0;                      /* limit distance? */
42 > extern char  *tralist[];                /* list of modifers to trace (or no) */
43 > extern int  traincl;                    /* include == 1, exclude == 0 */
44  
45 < int  inform = 'a';                      /* input format */
46 < int  outform = 'a';                     /* output format */
97 < char  *outvals = "v";                   /* output specification */
45 > extern int  hresolu;                    /* horizontal resolution */
46 > extern int  vresolu;                    /* vertical resolution */
47  
48 < int  do_irrad = 0;                      /* compute irradiance? */
48 > extern int  castonly;                   /* only doing ray-casting? */
49  
50 < void  (*trace)() = NULL;                /* trace call */
50 > 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 < extern void  ambnotify(), tranotify();
55 < void  (*addobjnotify[])() = {ambnotify, tranotify, NULL};
56 < char  *tralist[128];                    /* list of modifers to trace (or no) */
106 < int  traincl = -1;                      /* include == 1, exclude == 0 */
107 < #define  MAXTSET        511             /* maximum number in trace set */
54 > #ifndef  MAXTSET
55 > #define  MAXTSET        8191            /* maximum number in trace set */
56 > #endif
57   OBJECT  traset[MAXTSET+1]={0};          /* trace include/exclude set */
58  
59 < int  hresolu = 0;                       /* horizontal (scan) size */
111 < int  vresolu = 0;                       /* vertical resolution */
59 > static int  Tflag = 0;                  /* source tracing enabled? */
60  
61 < double  dstrsrc = 0.0;                  /* square source distribution */
114 < double  shadthresh = .05;               /* shadow threshold */
115 < double  shadcert = .5;                  /* shadow certainty */
116 < int  directrelay = 2;                   /* number of source relays */
117 < int  vspretest = 512;                   /* virtual source pretest density */
118 < int  directvis = 1;                     /* sources visible? */
119 < double  srcsizerat = .2;                /* maximum ratio source size/dist. */
61 > static RAY  thisray;                    /* for our convenience */
62  
63 < COLOR  cextinction = BLKCOLOR;          /* global extinction coefficient */
122 < COLOR  salbedo = BLKCOLOR;              /* global scattering albedo */
123 < double  seccg = 0.;                     /* global scattering eccentricity */
124 < double  ssampdist = 0.;                 /* scatter sampling distance */
63 > static FILE  *inpfp = NULL;             /* input stream pointer */
64  
65 < double  specthresh = .15;               /* specular sampling threshold */
66 < double  specjitter = 1.;                /* specular sampling jitter */
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 < int  backvis = 1;                       /* back face visibility */
69 > typedef void putf_t(RREAL *v, int n);
70 > static putf_t puta, putd, putf;
71  
72 < int  maxdepth = 6;                      /* maximum recursion depth */
73 < double  minweight = 4e-3;               /* minimum ray weight */
72 > typedef void oputf_t(RAY *r);
73 > static oputf_t  oputo, oputd, oputv, oputV, oputl, oputL, oputc, oputp,
74 >                oputr, oputR, oputx, oputX, oputn, oputN, oputs,
75 >                oputw, oputW, oputm, oputM, oputtilde;
76  
77 < char  *ambfile = NULL;                  /* ambient file name */
78 < COLOR  ambval = BLKCOLOR;               /* ambient value */
79 < int  ambvwt = 0;                        /* initial weight for ambient value */
80 < double  ambacc = 0.2;                   /* ambient accuracy */
81 < int  ambres = 128;                      /* ambient resolution */
82 < int  ambdiv = 512;                      /* ambient divisions */
83 < int  ambssamp = 0;                      /* ambient super-samples */
84 < int  ambounce = 0;                      /* ambient bounces */
85 < char  *amblist[128];                    /* ambient include/exclude list */
86 < int  ambincl = -1;                      /* include == 1, exclude == 0 */
77 > extern void tranotify(OBJECT obj);
78 > static int is_fifo(FILE *fp);
79 > static void bogusray(void);
80 > static void raycast(RAY *r);
81 > static void rayirrad(RAY *r);
82 > static void rtcompute(FVECT org, FVECT dir, double dmax);
83 > static int printvals(RAY *r);
84 > static int getvec(FVECT vec, int fmt, FILE *fp);
85 > static int iszerovec(const FVECT vec);
86 > static double nextray(FVECT org, FVECT dir);
87 > static void tabin(RAY *r);
88 > static void ourtrace(RAY *r);
89  
90 + static void  putscolor(COLORV *scol);
91  
92 < static RAY  thisray;                    /* for our convenience */
92 > static oputf_t *ray_out[32], *every_out[32];
93 > static putf_t *putreal;
94  
148 static void  oputo(), oputd(), oputv(), oputl(), oputL(),
149                oputp(), oputn(), oputN(), oputs(), oputw(), oputm();
95  
151 static void  ourtrace(), tabin();
152 static void  (*ray_out[16])(), (*every_out[16])();
153 static int  castonly = 0;
154
155 static void  puta(), putf(), putd();
156
157 static void  (*putreal)();
158
159 void    bogusray(), rad(), irrad(), printvals();
160
161
96   void
97 < quit(code)                      /* quit program */
98 < int  code;
97 > quit(                   /* quit program */
98 >        int  code
99 > )
100   {
101 < #ifndef  NIX
102 <        headclean();            /* delete header file */
103 <        pfclean();              /* clean up persist files */
101 >        if (ray_pnprocs > 0)    /* close children if any */
102 >                ray_pclose(0);          
103 >        else if (ray_pnprocs < 0)
104 >                _exit(code);    /* avoid flush() in child */
105 > #ifndef  NON_POSIX
106 >        else {
107 >                headclean();    /* delete header file */
108 >                pfclean();      /* clean up persist files */
109 >        }
110   #endif
111          exit(code);
112   }
113  
114  
115 < char *
116 < formstr(f)                              /* return format identifier */
117 < int  f;
115 > const char *
116 > formstr(                                /* return format identifier */
117 >        int  f
118 > )
119   {
120          switch (f) {
121          case 'a': return("ascii");
122          case 'f': return("float");
123          case 'd': return("double");
124 <        case 'c': return(COLRFMT);
124 >        case 'c':
125 >                if (out_prims == NULL)
126 >                        return(SPECFMT);
127 >                if (out_prims == xyzprims)
128 >                        return(CIEFMT);
129 >                return(COLRFMT);
130          }
131          return("unknown");
132   }
133  
134  
135 + 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   void
146 < rtrace(fname)                           /* trace rays from file */
147 < char  *fname;
146 > rtrace(                         /* trace rays from file */
147 >        char  *fname,
148 >        int  nproc
149 > )
150   {
151 <        long  vcount = hresolu>1 ? hresolu*vresolu : vresolu;
152 <        long  nextflush = hresolu;
151 >        unsigned long  vcount = (hresolu > 1) ? (unsigned long)hresolu*vresolu
152 >                                              : (unsigned long)vresolu;
153 >        long  nextflush = (!vresolu | (hresolu <= 1)) * hresolu;
154 >        int  something2flush = 0;
155          FILE  *fp;
156          double  d;
157          FVECT  orig, direc;
158                                          /* set up input */
159          if (fname == NULL)
160 <                fp = stdin;
161 <        else if ((fp = fopen(fname, "r")) == NULL) {
160 >                inpfp = stdin;
161 >        else if ((inpfp = fopen(fname, "r")) == NULL) {
162                  sprintf(errmsg, "cannot open input file \"%s\"", fname);
163                  error(SYSTEM, errmsg);
164          }
165 < #ifdef MSDOS
166 <        if (inform != 'a')
167 <                setmode(fileno(fp), O_BINARY);
165 > #ifdef getc_unlocked
166 >        flockfile(inpfp);               /* avoid lock/unlock overhead */
167 >        flockfile(stdout);
168   #endif
169 +        if (inform != 'a')
170 +                SET_FILE_BINARY(inpfp);
171                                          /* set up output */
172 <        setoutput(outvals);
173 <        switch (outform) {
174 <        case 'a': putreal = puta; break;
175 <        case 'f': putreal = putf; break;
176 <        case 'd': putreal = putd; break;
177 <        case 'c':
215 <                if (strcmp(outvals, "v"))
216 <                        error(USER, "color format with value output only");
217 <                break;
218 <        default:
219 <                error(CONSISTENCY, "botched output format");
172 >        if (castonly || every_out[0] != NULL)
173 >                nproc = 1;              /* don't bother multiprocessing */
174 >        if (every_out[0] != NULL) {
175 >                trace = ourtrace;       /* enable full tree tracing */
176 >                if (Tflag)              /* light sources, too? */
177 >                        trace_sources();
178          }
179 +        if ((nextflush > 0) & (nproc > nextflush)) {
180 +                error(WARNING, "reducing number of processes to match flush interval");
181 +                nproc = nextflush;
182 +        }
183 +        if (nproc > 1) {                /* start multiprocessing */
184 +                ray_popen(nproc);
185 +                ray_fifo_out = printvals;
186 +        }
187          if (hresolu > 0) {
188                  if (vresolu > 0)
189                          fprtresolu(hresolu, vresolu, stdout);
190 <                fflush(stdout);
190 >                else
191 >                        fflush(stdout);
192          }
193 <                                        /* process file */
194 <        while (getvec(orig, inform, fp) == 0 &&
195 <                        getvec(direc, inform, fp) == 0) {
196 <
197 <                d = normalize(direc);
198 <                if (d == 0.0) {                         /* zero ==> flush */
199 <                        bogusray();
233 <                        if (--nextflush <= 0 || vcount <= 0) {
193 >                                        /* process input rays */
194 >        while ((d = nextray(orig, direc)) >= 0.0) {
195 >                if (d == 0.0) {                         /* flush request? */
196 >                        if (something2flush) {
197 >                                if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
198 >                                        error(USER, "child(ren) died");
199 >                                bogusray();
200                                  fflush(stdout);
201 <                                nextflush = hresolu;
202 <                        }
203 <                } else {
204 <                        samplendx++;
205 <                                                        /* compute and print */
206 <                        if (imm_irrad)
207 <                                irrad(orig, direc);
208 <                        else
209 <                                rad(orig, direc, lim_dist ? d : 0.0);
244 <                                                        /* flush if time */
245 <                        if (--nextflush == 0) {
201 >                                nextflush = (!vresolu | (hresolu <= 1)) * hresolu;
202 >                                something2flush = 0;
203 >                        } else
204 >                                bogusray();
205 >                } else {                                /* compute and print */
206 >                        rtcompute(orig, direc, lim_dist ? d : 0.0);
207 >                        if (!--nextflush) {             /* flush if time */
208 >                                if (ray_pnprocs > 1 && ray_fifo_flush() < 0)
209 >                                        error(USER, "child(ren) died");
210                                  fflush(stdout);
211                                  nextflush = hresolu;
212 <                        }
212 >                        } else
213 >                                something2flush = 1;
214                  }
215                  if (ferror(stdout))
216                          error(SYSTEM, "write error");
217 <                if (--vcount == 0)                      /* check for end */
217 >                if (vcount && !--vcount)                /* check for end */
218                          break;
219          }
220 <        fflush(stdout);
221 <        if (vcount > 0)
222 <                error(USER, "read error");
223 <        if (fname != NULL)
224 <                fclose(fp);
220 >        if (ray_pnprocs > 1) {                          /* clean up children */
221 >                if (ray_fifo_flush() < 0)
222 >                        error(USER, "unable to complete processing");
223 >                ray_pclose(0);
224 >        }
225 >        if (vcount)
226 >                error(WARNING, "unexpected EOF on input");
227 >        if (fflush(stdout) < 0)
228 >                error(SYSTEM, "write error");
229 >        if (fname != NULL) {
230 >                fclose(inpfp);
231 >                inpfp = NULL;
232 >        }
233 >        nextray(NULL, NULL);
234   }
235  
236  
237 < setoutput(vs)                           /* set up output tables */
238 < register char  *vs;
237 > int
238 > setrtoutput(void)                       /* set up output tables, return #comp */
239   {
240 <        extern void  (*trace)();
241 <        register void (**table)() = ray_out;
240 >        char  *vs = outvals;
241 >        oputf_t **table = ray_out;
242 >        const int       nco = (sens_curve != NULL) ? 1 :
243 >                                (out_prims != NULL) ? 3 : NCSAMP;
244 >        int  ncomp = 0;
245  
246 <        castonly = 1;
247 <        while (*vs)
248 <                switch (*vs++) {
246 >        if (!*vs)
247 >                error(USER, "empty output specification");
248 >
249 >        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 >                if (nco < 3)
257 >                        error(USER, "color format incompatible with -pY, -pS, -pM");
258 >                break;
259 >        default:
260 >                error(CONSISTENCY, "botched output format");
261 >        }
262 >        castonly = 1;                   /* sets castonly as side-effect */
263 >        do
264 >                switch (*vs) {
265 >                case 'T':                               /* trace sources */
266 >                        Tflag++;
267 >                        /* fall through */
268                  case 't':                               /* trace */
269 +                        if (!vs[1]) break;
270                          *table = NULL;
271                          table = every_out;
275                        trace = ourtrace;
272                          castonly = 0;
273                          break;
274                  case 'o':                               /* origin */
275                          *table++ = oputo;
276 +                        ncomp += 3;
277                          break;
278                  case 'd':                               /* direction */
279                          *table++ = oputd;
280 +                        ncomp += 3;
281                          break;
282 +                case 'r':                               /* reflected contrib. */
283 +                        *table++ = oputr;
284 +                        ncomp += nco;
285 +                        castonly = 0;
286 +                        break;
287 +                case 'R':                               /* reflected distance */
288 +                        *table++ = oputR;
289 +                        ncomp++;
290 +                        castonly = 0;
291 +                        break;
292 +                case 'x':                               /* xmit contrib. */
293 +                        *table++ = oputx;
294 +                        ncomp += nco;
295 +                        castonly = 0;
296 +                        break;
297 +                case 'X':                               /* xmit distance */
298 +                        *table++ = oputX;
299 +                        ncomp++;
300 +                        castonly = 0;
301 +                        break;
302                  case 'v':                               /* value */
303                          *table++ = oputv;
304 +                        ncomp += nco;
305                          castonly = 0;
306                          break;
307 +                case 'V':                               /* contribution */
308 +                        *table++ = oputV;
309 +                        ncomp += nco;
310 +                        castonly = 0;
311 +                        if (ambounce > 0 && (ambacc > FTINY || ambssamp > 0))
312 +                                error(WARNING,
313 +                                        "-otV accuracy depends on -aa 0 -as 0");
314 +                        break;
315                  case 'l':                               /* effective distance */
316                          *table++ = oputl;
317 +                        ncomp++;
318                          castonly = 0;
319                          break;
320 +                case 'c':                               /* local coordinates */
321 +                        *table++ = oputc;
322 +                        ncomp += 2;
323 +                        break;
324                  case 'L':                               /* single ray length */
325                          *table++ = oputL;
326 +                        ncomp++;
327                          break;
328                  case 'p':                               /* point */
329                          *table++ = oputp;
330 +                        ncomp += 3;
331                          break;
332                  case 'n':                               /* perturbed normal */
333                          *table++ = oputn;
334 +                        ncomp += 3;
335                          castonly = 0;
336                          break;
337                  case 'N':                               /* unperturbed normal */
338                          *table++ = oputN;
339 +                        ncomp += 3;
340                          break;
341                  case 's':                               /* surface */
342                          *table++ = oputs;
343 +                        ncomp++;
344                          break;
345                  case 'w':                               /* weight */
346                          *table++ = oputw;
347 +                        ncomp++;
348                          break;
349 +                case 'W':                               /* coefficient */
350 +                        *table++ = oputW;
351 +                        ncomp += nco;
352 +                        castonly = 0;
353 +                        if (ambounce > 0 && (ambacc > FTINY) | (ambssamp > 0))
354 +                                error(WARNING,
355 +                                        "-otW accuracy depends on -aa 0 -as 0");
356 +                        break;
357                  case 'm':                               /* modifier */
358                          *table++ = oputm;
359 +                        ncomp++;
360                          break;
361 +                case 'M':                               /* material */
362 +                        *table++ = oputM;
363 +                        ncomp++;
364 +                        break;
365 +                case '~':                               /* tilde */
366 +                        *table++ = oputtilde;
367 +                        break;
368 +                default:
369 +                        sprintf(errmsg, "unrecognized output option '%c'", *vs);
370 +                        error(USER, errmsg);
371                  }
372 +        while (*++vs);
373 +
374          *table = NULL;
375 +        if (*every_out != NULL)
376 +                ncomp = 0;
377 +                                                        /* compatibility */
378 +        if ((do_irrad | imm_irrad) && castonly)
379 +                error(USER, "-I+ and -i+ options require some value output");
380 +        for (table = ray_out; *table != NULL; table++) {
381 +                if ((*table == oputV) | (*table == oputW))
382 +                        error(WARNING, "-oVW options require trace mode");
383 +                if ((do_irrad | imm_irrad) &&
384 +                                (*table == oputr) | (*table == oputR) |
385 +                                (*table == oputx) | (*table == oputX))
386 +                        error(WARNING, "-orRxX options incompatible with -I+ and -i+");
387 +        }
388 +        return(ncomp);
389   }
390  
391  
392 < void
393 < bogusray()                      /* print out empty record */
392 > static void
393 > bogusray(void)                  /* print out empty record */
394   {
395 <        thisray.rorg[0] = thisray.rorg[1] = thisray.rorg[2] =
323 <        thisray.rdir[0] = thisray.rdir[1] = thisray.rdir[2] = 0.0;
324 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
395 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
396          printvals(&thisray);
397   }
398  
399  
400 < void
401 < rad(org, dir, dmax)             /* compute and print ray value(s) */
402 < FVECT  org, dir;
403 < double  dmax;
400 > static void
401 > raycast(                        /* compute first ray intersection only */
402 >        RAY *r
403 > )
404   {
405 <        VCOPY(thisray.rorg, org);
406 <        VCOPY(thisray.rdir, dir);
407 <        thisray.rmax = dmax;
408 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
409 <        if (castonly) {
410 <                if (!localhit(&thisray, &thescene))
411 <                        if (thisray.ro == &Aftplane) {  /* clipped */
341 <                                thisray.ro = NULL;
342 <                                thisray.rot = FHUGE;
343 <                        } else
344 <                                sourcehit(&thisray);
345 <        } else
346 <                rayvalue(&thisray);
347 <        printvals(&thisray);
405 >        if (!localhit(r, &thescene)) {
406 >                if (r->ro == &Aftplane) {       /* clipped */
407 >                        r->ro = NULL;
408 >                        r->rot = FHUGE;
409 >                } else
410 >                        sourcehit(r);
411 >        }
412   }
413  
414  
415 < void
416 < irrad(org, dir)                 /* compute immediate irradiance value */
417 < FVECT  org, dir;
415 > static void
416 > rayirrad(                       /* compute irradiance rather than radiance */
417 >        RAY *r
418 > )
419   {
420 <        register int  i;
420 >        void    (*old_revf)(RAY *) = r->revf;
421 >                                        /* pretend we hit surface */
422 >        r->rxt = r->rot = 1e-5;
423 >        VSUM(r->rop, r->rorg, r->rdir, r->rot);
424 >        r->ron[0] = -r->rdir[0];
425 >        r->ron[1] = -r->rdir[1];
426 >        r->ron[2] = -r->rdir[2];
427 >        r->rod = 1.0;
428 >                                        /* compute result */
429 >        r->revf = raytrace;
430 >        (*ofun[Lamb.otype].funp)(&Lamb, r);
431 >        r->revf = old_revf;
432 > }
433  
434 <        for (i = 0; i < 3; i++) {
435 <                thisray.rorg[i] = org[i] + dir[i];
436 <                thisray.rdir[i] = -dir[i];
434 >
435 > static void
436 > rtcompute(                      /* compute and print ray value(s) */
437 >        FVECT  org,
438 >        FVECT  dir,
439 >        double  dmax
440 > )
441 > {
442 >                                        /* set up ray */
443 >        if (imm_irrad) {
444 >                VSUM(thisray.rorg, org, dir, 1.1e-4);
445 >                thisray.rdir[0] = -dir[0];
446 >                thisray.rdir[1] = -dir[1];
447 >                thisray.rdir[2] = -dir[2];
448 >                thisray.rmax = 0.0;
449 >        } else {
450 >                VCOPY(thisray.rorg, org);
451 >                VCOPY(thisray.rdir, dir);
452 >                thisray.rmax = dmax;
453          }
454 <        rayorigin(&thisray, NULL, PRIMARY, 1.0);
455 <                                        /* pretend we hit surface */
456 <        thisray.rot = 1.0-1e-4;
457 <        thisray.rod = 1.0;
458 <        VCOPY(thisray.ron, dir);
459 <        for (i = 0; i < 3; i++)         /* fudge factor */
460 <                thisray.rop[i] = org[i] + 1e-4*dir[i];
461 <                                        /* compute and print */
462 <        (*ofun[Lamb.otype].funp)(&Lamb, &thisray);
454 >        rayorigin(&thisray, PRIMARY, NULL, NULL);
455 >        if (imm_irrad)
456 >                thisray.revf = rayirrad;
457 >        else if (castonly)
458 >                thisray.revf = raycast;
459 >        if (ray_pnprocs > 1) {          /* multiprocessing FIFO? */
460 >                if (ray_fifo_in(&thisray) < 0)
461 >                        error(USER, "lost children");
462 >                return;
463 >        }
464 >        samplendx++;                    /* else do it ourselves */
465 >        rayvalue(&thisray);
466          printvals(&thisray);
467   }
468  
469  
470 < void
471 < printvals(r)                    /* print requested ray values */
472 < RAY  *r;
470 > static int
471 > printvals(                      /* print requested ray values */
472 >        RAY  *r
473 > )
474   {
475 <        register void  (**tp)();
475 >        oputf_t **tp;
476  
477          if (ray_out[0] == NULL)
478 <                return;
478 >                return(0);
479          for (tp = ray_out; *tp != NULL; tp++)
480                  (**tp)(r);
481          if (outform == 'a')
482                  putchar('\n');
483 +        return(1);
484   }
485  
486  
487 < int
488 < getvec(vec, fmt, fp)            /* get a vector from fp */
489 < register FVECT  vec;
490 < int  fmt;
393 < FILE  *fp;
487 > static int
488 > is_fifo(                /* check if file pointer connected to pipe */
489 >        FILE *fp
490 > )
491   {
492 + #ifdef S_ISFIFO
493 +        struct stat  sbuf;
494 +
495 +        if (fstat(fileno(fp), &sbuf) < 0)
496 +                error(SYSTEM, "fstat() failed on input stream");
497 +        return(S_ISFIFO(sbuf.st_mode));
498 + #else
499 +        return (fp == stdin);           /* just a guess, really */
500 + #endif
501 + }
502 +
503 +
504 + static int
505 + getvec(                 /* get a vector from fp */
506 +        FVECT  vec,
507 +        int  fmt,
508 +        FILE  *fp
509 + )
510 + {
511          static float  vf[3];
512          static double  vd[3];
513          char  buf[32];
514 <        register int  i;
514 >        int  i;
515  
516          switch (fmt) {
517          case 'a':                                       /* ascii */
# Line 407 | Line 523 | FILE  *fp;
523                  }
524                  break;
525          case 'f':                                       /* binary float */
526 <                if (fread((char *)vf, sizeof(float), 3, fp) != 3)
526 >                if (getbinary(vf, sizeof(float), 3, fp) != 3)
527                          return(-1);
528 <                vec[0] = vf[0]; vec[1] = vf[1]; vec[2] = vf[2];
528 >                VCOPY(vec, vf);
529                  break;
530          case 'd':                                       /* binary double */
531 <                if (fread((char *)vd, sizeof(double), 3, fp) != 3)
531 >                if (getbinary(vd, sizeof(double), 3, fp) != 3)
532                          return(-1);
533 <                vec[0] = vd[0]; vec[1] = vd[1]; vec[2] = vd[2];
533 >                VCOPY(vec, vd);
534                  break;
535          default:
536                  error(CONSISTENCY, "botched input format");
# Line 423 | Line 539 | FILE  *fp;
539   }
540  
541  
542 + static int
543 + iszerovec(const FVECT vec)
544 + {
545 +        return (vec[0] == 0.0) & (vec[1] == 0.0) & (vec[2] == 0.0);
546 + }
547 +
548 +
549 + static double
550 + nextray(                /* return next ray in work group (-1.0 if EOF) */
551 +        FVECT org,
552 +        FVECT dir
553 + )
554 + {
555 +        const size_t    qlength = !vresolu * hresolu;
556 +
557 +        if ((org == NULL) | (dir == NULL)) {
558 +                if (inp_queue != NULL)  /* asking to free queue */
559 +                        free(inp_queue);
560 +                inp_queue = NULL;
561 +                inp_qpos = inp_qend = 0;
562 +                return(-1.);
563 +        }
564 +        if (!inp_qend) {                /* initialize FIFO queue */
565 +                int     rsiz = 6*20;    /* conservative ascii ray size */
566 +                if (inform == 'f') rsiz = 6*sizeof(float);
567 +                else if (inform == 'd') rsiz = 6*sizeof(double);
568 +                                        /* check against pipe limit */
569 +                if (qlength*rsiz > 512 && is_fifo(inpfp))
570 +                        inp_queue = (FVECT *)malloc(sizeof(FVECT)*2*qlength);
571 +                inp_qend = -(inp_queue == NULL);        /* flag for no queue */
572 +        }
573 +        if (inp_qend < 0) {             /* not queuing? */
574 +                if (getvec(org, inform, inpfp) < 0 ||
575 +                                getvec(dir, inform, inpfp) < 0)
576 +                        return(-1.);
577 +                return normalize(dir);
578 +        }
579 +        if (inp_qpos >= inp_qend) {     /* need to refill input queue? */
580 +                for (inp_qend = 0; inp_qend < qlength; inp_qend++) {
581 +                        if (getvec(inp_queue[2*inp_qend], inform, inpfp) < 0
582 +                                        || getvec(inp_queue[2*inp_qend+1],
583 +                                                        inform, inpfp) < 0)
584 +                                break;          /* hit EOF */
585 +                        if (iszerovec(inp_queue[2*inp_qend+1])) {
586 +                                ++inp_qend;     /* flush request */
587 +                                break;
588 +                        }
589 +                }
590 +                inp_qpos = 0;
591 +        }
592 +        if (inp_qpos >= inp_qend)       /* unexpected EOF? */
593 +                return(-1.);
594 +        VCOPY(org, inp_queue[2*inp_qpos]);
595 +        VCOPY(dir, inp_queue[2*inp_qpos+1]);
596 +        ++inp_qpos;
597 +        return normalize(dir);
598 + }
599 +
600 +
601   void
602 < tranotify(obj)                  /* record new modifier */
603 < OBJECT  obj;
602 > tranotify(                      /* record new modifier */
603 >        OBJECT  obj
604 > )
605   {
606          static int  hitlimit = 0;
607 <        register OBJREC  *o = objptr(obj);
608 <        register char  **tralp;
607 >        OBJREC   *o = objptr(obj);
608 >        char  **tralp;
609  
610          if (obj == OVOID) {             /* starting over */
611                  traset[0] = 0;
# Line 452 | Line 628 | OBJECT obj;
628  
629  
630   static void
631 < ourtrace(r)                             /* print ray values */
632 < RAY  *r;
631 > ourtrace(                               /* print ray values */
632 >        RAY  *r
633 > )
634   {
635 <        register void  (**tp)();
635 >        oputf_t **tp;
636  
637          if (every_out[0] == NULL)
638                  return;
# Line 467 | Line 644 | RAY  *r;
644          tabin(r);
645          for (tp = every_out; *tp != NULL; tp++)
646                  (**tp)(r);
647 <        putchar('\n');
647 >        if (outform == 'a')
648 >                putchar('\n');
649   }
650  
651  
652   static void
653 < tabin(r)                                /* tab in appropriate amount */
654 < RAY  *r;
653 > tabin(                          /* tab in appropriate amount */
654 >        RAY  *r
655 > )
656   {
657 <        register RAY  *rp;
657 >        const RAY  *rp;
658  
659          for (rp = r->parent; rp != NULL; rp = rp->parent)
660                  putchar('\t');
# Line 483 | Line 662 | RAY  *r;
662  
663  
664   static void
665 < oputo(r)                                /* print origin */
666 < register RAY  *r;
665 > oputo(                          /* print origin */
666 >        RAY  *r
667 > )
668   {
669 <        (*putreal)(r->rorg[0]);
490 <        (*putreal)(r->rorg[1]);
491 <        (*putreal)(r->rorg[2]);
669 >        (*putreal)(r->rorg, 3);
670   }
671  
672  
673   static void
674 < oputd(r)                                /* print direction */
675 < register RAY  *r;
674 > oputd(                          /* print direction */
675 >        RAY  *r
676 > )
677   {
678 <        (*putreal)(r->rdir[0]);
500 <        (*putreal)(r->rdir[1]);
501 <        (*putreal)(r->rdir[2]);
678 >        (*putreal)(r->rdir, 3);
679   }
680  
681  
682   static void
683 < oputv(r)                                /* print value */
684 < register RAY  *r;
683 > oputr(                          /* print mirrored contribution */
684 >        RAY  *r
685 > )
686   {
687 <        COLR  cout;
688 <        
689 <        if (outform == 'c') {
690 <                setcolr(cout,   colval(r->rcol,RED),
691 <                                colval(r->rcol,GRN),
692 <                                colval(r->rcol,BLU));
515 <                fwrite((char *)cout, sizeof(cout), 1, stdout);
516 <                return;
517 <        }
518 <        (*putreal)(colval(r->rcol,RED));
519 <        (*putreal)(colval(r->rcol,GRN));
520 <        (*putreal)(colval(r->rcol,BLU));
687 >        RREAL   cval[3];
688 >
689 >        cval[0] = colval(r->mcol,RED);
690 >        cval[1] = colval(r->mcol,GRN);
691 >        cval[2] = colval(r->mcol,BLU);
692 >        (*putreal)(cval, 3);
693   }
694  
695  
696 +
697   static void
698 < oputl(r)                                /* print effective distance */
699 < register RAY  *r;
698 > oputR(                          /* print mirrored distance */
699 >        RAY  *r
700 > )
701   {
702 <        (*putreal)(r->rt);
702 >        (*putreal)(&r->rmt, 1);
703   }
704  
705  
706   static void
707 < oputL(r)                                /* print single ray length */
708 < register RAY  *r;
707 > oputx(                          /* print unmirrored contribution */
708 >        RAY  *r
709 > )
710   {
711 <        (*putreal)(r->rot);
711 >        SCOLOR  cdiff;
712 >
713 >        copyscolor(cdiff, r->rcol);
714 >        sopscolor(cdiff, -=, r->mcol);
715 >
716 >        putscolor(cdiff);
717   }
718  
719  
720   static void
721 < oputp(r)                                /* print point */
722 < register RAY  *r;
721 > oputX(                          /* print unmirrored distance */
722 >        RAY  *r
723 > )
724   {
725 <        if (r->rot < FHUGE) {
545 <                (*putreal)(r->rop[0]);
546 <                (*putreal)(r->rop[1]);
547 <                (*putreal)(r->rop[2]);
548 <        } else {
549 <                (*putreal)(0.0);
550 <                (*putreal)(0.0);
551 <                (*putreal)(0.0);
552 <        }
725 >        (*putreal)(&r->rxt, 1);
726   }
727  
728  
729   static void
730 < oputN(r)                                /* print unperturbed normal */
731 < register RAY  *r;
730 > oputv(                          /* print value */
731 >        RAY  *r
732 > )
733   {
734 <        if (r->rot < FHUGE) {
735 <                (*putreal)(r->ron[0]);
736 <                (*putreal)(r->ron[1]);
737 <                (*putreal)(r->ron[2]);
738 <        } else {
739 <                (*putreal)(0.0);
740 <                (*putreal)(0.0);
741 <                (*putreal)(0.0);
734 >        putscolor(r->rcol);
735 > }
736 >
737 >
738 > static void
739 > oputV(                          /* print value contribution */
740 >        RAY *r
741 > )
742 > {
743 >        SCOLOR  contr;
744 >
745 >        raycontrib(contr, r, PRIMARY);
746 >        smultscolor(contr, r->rcol);
747 >        putscolor(contr);
748 > }
749 >
750 >
751 > static void
752 > oputl(                          /* print effective distance */
753 >        RAY  *r
754 > )
755 > {
756 >        RREAL   d = raydistance(r);
757 >
758 >        (*putreal)(&d, 1);
759 > }
760 >
761 >
762 > static void
763 > oputL(                          /* print single ray length */
764 >        RAY  *r
765 > )
766 > {
767 >        (*putreal)(&r->rot, 1);
768 > }
769 >
770 >
771 > static void
772 > oputc(                          /* print local coordinates */
773 >        RAY  *r
774 > )
775 > {
776 >        (*putreal)(r->uv, 2);
777 > }
778 >
779 >
780 > static RREAL    vdummy[3] = {0.0, 0.0, 0.0};
781 >
782 >
783 > static void
784 > oputp(                          /* print intersection point */
785 >        RAY  *r
786 > )
787 > {
788 >        (*putreal)(r->rop, 3);  /* set to ray origin if distant or no hit */
789 > }
790 >
791 >
792 > static void
793 > oputN(                          /* print unperturbed normal */
794 >        RAY  *r
795 > )
796 > {
797 >        if (r->ro == NULL) {    /* zero vector if clipped or no hit */
798 >                (*putreal)(vdummy, 3);
799 >                return;
800          }
801 +        if (r->rflips & 1) {    /* undo any flippin' flips */
802 +                FVECT   unrm;
803 +                unrm[0] = -r->ron[0];
804 +                unrm[1] = -r->ron[1];
805 +                unrm[2] = -r->ron[2];
806 +                (*putreal)(unrm, 3);
807 +        } else
808 +                (*putreal)(r->ron, 3);
809   }
810  
811  
812   static void
813 < oputn(r)                                /* print perturbed normal */
814 < RAY  *r;
813 > oputn(                          /* print perturbed normal */
814 >        RAY  *r
815 > )
816   {
817          FVECT  pnorm;
818  
819 <        if (r->rot >= FHUGE) {
820 <                (*putreal)(0.0);
580 <                (*putreal)(0.0);
581 <                (*putreal)(0.0);
819 >        if (r->ro == NULL) {    /* clipped or no hit */
820 >                (*putreal)(vdummy, 3);
821                  return;
822          }
823          raynormal(pnorm, r);
824 <        (*putreal)(pnorm[0]);
586 <        (*putreal)(pnorm[1]);
587 <        (*putreal)(pnorm[2]);
824 >        (*putreal)(pnorm, 3);
825   }
826  
827  
828   static void
829 < oputs(r)                                /* print name */
830 < register RAY  *r;
829 > oputs(                          /* print name */
830 >        RAY  *r
831 > )
832   {
833          if (r->ro != NULL)
834                  fputs(r->ro->oname, stdout);
# Line 601 | Line 839 | register RAY  *r;
839  
840  
841   static void
842 < oputw(r)                                /* print weight */
843 < register RAY  *r;
842 > oputw(                          /* print weight */
843 >        RAY  *r
844 > )
845   {
846 <        (*putreal)(r->rweight);
846 >        RREAL   rwt = r->rweight;
847 >        
848 >        (*putreal)(&rwt, 1);
849   }
850  
851  
852   static void
853 < oputm(r)                                /* print modifier */
854 < register RAY  *r;
853 > oputW(                          /* print coefficient */
854 >        RAY  *r
855 > )
856   {
857 +        SCOLOR  contr;
858 +                                /* shadow ray not on source? */
859 +        if (r->rsrc >= 0 && source[r->rsrc].so != r->ro)
860 +                scolorblack(contr);
861 +        else
862 +                raycontrib(contr, r, PRIMARY);
863 +
864 +        putscolor(contr);
865 + }
866 +
867 +
868 + static void
869 + oputm(                          /* print modifier */
870 +        RAY  *r
871 + )
872 + {
873          if (r->ro != NULL)
874                  if (r->ro->omod != OVOID)
875                          fputs(objptr(r->ro->omod)->oname, stdout);
# Line 624 | Line 882 | register RAY  *r;
882  
883  
884   static void
885 < puta(v)                         /* print ascii value */
886 < double  v;
885 > oputM(                          /* print material */
886 >        RAY  *r
887 > )
888   {
889 <        printf("%e\t", v);
889 >        OBJREC  *mat;
890 >
891 >        if (r->ro != NULL) {
892 >                if ((mat = findmaterial(r->ro)) != NULL)
893 >                        fputs(mat->oname, stdout);
894 >                else
895 >                        fputs(VOIDID, stdout);
896 >        } else
897 >                putchar('*');
898 >        putchar('\t');
899   }
900  
901  
902   static void
903 < putd(v)                         /* print binary double */
904 < double  v;
903 > oputtilde(                      /* output tilde (spacer) */
904 >        RAY  *r
905 > )
906   {
907 <        fwrite((char *)&v, sizeof(v), 1, stdout);
907 >        fputs("~\t", stdout);
908   }
909  
910  
911   static void
912 < putf(v)                         /* print binary float */
913 < double  v;
912 > puta(                           /* print ascii value(s) */
913 >        RREAL *v, int n
914 > )
915   {
916 <        float f = v;
916 >        if (n == 3) {
917 >                printf("%e\t%e\t%e\t", v[0], v[1], v[2]);
918 >                return;
919 >        }
920 >        while (n--)
921 >                printf("%e\t", *v++);
922 > }
923  
924 <        fwrite((char *)&f, sizeof(f), 1, stdout);
924 >
925 > static void
926 > putd(RREAL *v, int n)           /* output binary double(s) */
927 > {
928 > #ifdef  SMLFLT
929 >        double  da[MAXCSAMP];
930 >        int     i;
931 >
932 >        if (n > MAXCSAMP)
933 >                error(INTERNAL, "code error in putd()");
934 >        for (i = n; i--; )
935 >                da[i] = v[i];
936 >        putbinary(da, sizeof(double), n, stdout);
937 > #else
938 >        putbinary(v, sizeof(RREAL), n, stdout);
939 > #endif
940 > }
941 >
942 >
943 > static void
944 > putf(RREAL *v, int n)           /* output binary float(s) */
945 > {
946 > #ifndef SMLFLT
947 >        float   fa[MAXCSAMP];
948 >        int     i;
949 >
950 >        if (n > MAXCSAMP)
951 >                error(INTERNAL, "code error in putf()");
952 >        for (i = n; i--; )
953 >                fa[i] = v[i];
954 >        putbinary(fa, sizeof(float), n, stdout);
955 > #else
956 >        putbinary(v, sizeof(RREAL), n, stdout);
957 > #endif
958 > }
959 >
960 >
961 > static void
962 > putscolor(COLORV *scol)         /* output (spectral) color */
963 > {
964 >        static COLORMAT xyz2myrgbmat;
965 >        SCOLOR          my_scol;
966 >        COLOR           col;
967 >                                        /* apply scalefactor if any */
968 >        if (out_scalefactor != 1.) {
969 >                copyscolor(my_scol, scol);
970 >                scalescolor(my_scol, out_scalefactor);
971 >                scol = my_scol;
972 >        }
973 >        if (sens_curve != NULL) {       /* single channel output */
974 >                RREAL   v = (*sens_curve)(scol);
975 >                (*putreal)(&v, 1);
976 >                return;
977 >        }
978 >        if (out_prims == NULL) {        /* full spectral reporting */
979 >                if (outform == 'c') {
980 >                        SCOLR   sclr;
981 >                        scolor_scolr(sclr, scol);
982 >                        putbinary(sclr, LSCOLR, 1, stdout);
983 >                } else if (sizeof(RREAL) != sizeof(COLORV)) {
984 >                        RREAL   sreal[MAXCSAMP];
985 >                        int     i = NCSAMP;
986 >                        while (i--) sreal[i] = scol[i];
987 >                        (*putreal)(sreal, NCSAMP);
988 >                } else
989 >                        (*putreal)((RREAL *)scol, NCSAMP);
990 >                return;
991 >        }
992 >        if (out_prims == xyzprims) {
993 >                scolor_cie(col, scol);
994 >        } else if (out_prims == stdprims) {
995 >                scolor_rgb(col, scol);
996 >        } else {
997 >                COLOR   xyz;
998 >                if (xyz2myrgbmat[0][0] == 0)
999 >                        compxyz2rgbWBmat(xyz2myrgbmat, out_prims);
1000 >                scolor_cie(xyz, scol);
1001 >                colortrans(col, xyz2myrgbmat, xyz);
1002 >                clipgamut(col, xyz[CIEY], CGAMUT_LOWER, cblack, cwhite);
1003 >        }
1004 >        if (outform == 'c') {
1005 >                COLR    clr;
1006 >                setcolr(clr, colval(col,RED), colval(col,GRN), colval(col,BLU));
1007 >                putbinary(clr, sizeof(COLR), 1, stdout);
1008 >        } else if (sizeof(RREAL) != sizeof(COLORV)) {
1009 >                RREAL   creal[3];
1010 >                copycolor(creal, col);
1011 >                (*putreal)(creal, 3);
1012 >        } else
1013 >                (*putreal)((RREAL *)col, 3);
1014   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines