| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
greg |
1.61 |
static const char RCSid[] = "$Id: rtcontrib.c,v 1.60 2010/10/10 09:25:04 greg Exp $";
|
| 3 |
greg |
1.1 |
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* Gather rtrace output to compute contributions from particular sources
|
| 6 |
|
|
*/
|
| 7 |
|
|
|
| 8 |
greg |
1.4 |
#include "standard.h"
|
| 9 |
greg |
1.1 |
#include <ctype.h>
|
| 10 |
greg |
1.7 |
#include <signal.h>
|
| 11 |
greg |
1.1 |
#include "platform.h"
|
| 12 |
|
|
#include "rtprocess.h"
|
| 13 |
|
|
#include "selcall.h"
|
| 14 |
|
|
#include "color.h"
|
| 15 |
|
|
#include "resolu.h"
|
| 16 |
|
|
#include "lookup.h"
|
| 17 |
|
|
#include "calcomp.h"
|
| 18 |
|
|
|
| 19 |
greg |
1.19 |
#ifndef MAXMODLIST
|
| 20 |
greg |
1.1 |
#define MAXMODLIST 1024 /* maximum modifiers we'll track */
|
| 21 |
greg |
1.19 |
#endif
|
| 22 |
greg |
1.1 |
|
| 23 |
greg |
1.58 |
size_t treebufsiz = BUFSIZ; /* current tree buffer size */
|
| 24 |
greg |
1.1 |
|
| 25 |
greg |
1.2 |
typedef double DCOLOR[3]; /* double-precision color */
|
| 26 |
|
|
|
| 27 |
greg |
1.1 |
/*
|
| 28 |
greg |
1.31 |
* The MODCONT structure is used to accumulate ray contributions
|
| 29 |
greg |
1.1 |
* for a particular modifier, which may be subdivided into bins
|
| 30 |
greg |
1.45 |
* if binv evaluates > 0. If outspec contains a %s in it, this will
|
| 31 |
greg |
1.1 |
* be replaced with the modifier name. If outspec contains a %d in it,
|
| 32 |
|
|
* this will be used to create one output file per bin, otherwise all bins
|
| 33 |
|
|
* will be written to the same file, in order. If the global outfmt
|
| 34 |
|
|
* is 'c', then a 4-byte RGBE pixel will be output for each bin value
|
| 35 |
|
|
* and the file will conform to a RADIANCE image if xres & yres are set.
|
| 36 |
|
|
*/
|
| 37 |
|
|
typedef struct {
|
| 38 |
|
|
const char *outspec; /* output file specification */
|
| 39 |
|
|
const char *modname; /* modifier name */
|
| 40 |
|
|
EPNODE *binv; /* bin value expression */
|
| 41 |
|
|
int nbins; /* number of accumulation bins */
|
| 42 |
greg |
1.2 |
DCOLOR cbin[1]; /* contribution bins (extends struct) */
|
| 43 |
greg |
1.1 |
} MODCONT; /* modifier contribution */
|
| 44 |
|
|
|
| 45 |
|
|
static void mcfree(void *p) { epfree((*(MODCONT *)p).binv); free(p); }
|
| 46 |
|
|
|
| 47 |
|
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */
|
| 48 |
|
|
|
| 49 |
greg |
1.31 |
/*
|
| 50 |
|
|
* The STREAMOUT structure holds an open FILE pointer and a count of
|
| 51 |
greg |
1.45 |
* the number of RGB triplets per record, or 0 if unknown.
|
| 52 |
greg |
1.31 |
*/
|
| 53 |
|
|
typedef struct {
|
| 54 |
|
|
FILE *ofp; /* output file pointer */
|
| 55 |
greg |
1.45 |
int outpipe; /* output is to a pipe */
|
| 56 |
greg |
1.31 |
int reclen; /* triplets/record */
|
| 57 |
greg |
1.45 |
int xr, yr; /* output resolution for picture */
|
| 58 |
greg |
1.31 |
} STREAMOUT;
|
| 59 |
|
|
|
| 60 |
|
|
/* close output stream and free record */
|
| 61 |
|
|
static void
|
| 62 |
|
|
closestream(void *p)
|
| 63 |
|
|
{
|
| 64 |
|
|
STREAMOUT *sop = (STREAMOUT *)p;
|
| 65 |
greg |
1.33 |
int status;
|
| 66 |
greg |
1.45 |
if (sop->outpipe)
|
| 67 |
greg |
1.33 |
status = pclose(sop->ofp);
|
| 68 |
greg |
1.31 |
else
|
| 69 |
greg |
1.33 |
status = fclose(sop->ofp);
|
| 70 |
|
|
if (status)
|
| 71 |
|
|
error(SYSTEM, "error closing output stream");
|
| 72 |
greg |
1.31 |
free(p);
|
| 73 |
|
|
}
|
| 74 |
greg |
1.1 |
|
| 75 |
greg |
1.31 |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */
|
| 76 |
greg |
1.1 |
|
| 77 |
greg |
1.14 |
#define OF_MODIFIER 01
|
| 78 |
|
|
#define OF_BIN 02
|
| 79 |
|
|
|
| 80 |
greg |
1.32 |
STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen);
|
| 81 |
greg |
1.14 |
int ofname(char *oname, const char *ospec, const char *mname, int bn);
|
| 82 |
|
|
void printheader(FILE *fout, const char *info);
|
| 83 |
greg |
1.45 |
void printresolu(FILE *fout, int xr, int yr);
|
| 84 |
greg |
1.1 |
|
| 85 |
|
|
/*
|
| 86 |
|
|
* The rcont structure is used to manage i/o with a particular
|
| 87 |
|
|
* rtrace child process. Input is passed unchanged from stdin,
|
| 88 |
|
|
* and output is processed in input order and accumulated according
|
| 89 |
|
|
* to the corresponding modifier and bin number.
|
| 90 |
|
|
*/
|
| 91 |
|
|
struct rtproc {
|
| 92 |
|
|
struct rtproc *next; /* next in list of processes */
|
| 93 |
|
|
SUBPROC pd; /* rtrace pipe descriptors */
|
| 94 |
greg |
1.2 |
unsigned long raynum; /* ray number for this tree */
|
| 95 |
greg |
1.58 |
size_t bsiz; /* ray tree buffer length */
|
| 96 |
greg |
1.2 |
char *buf; /* ray tree buffer */
|
| 97 |
greg |
1.58 |
size_t nbr; /* number of bytes from rtrace */
|
| 98 |
greg |
1.5 |
}; /* rtrace process buffer */
|
| 99 |
greg |
1.1 |
|
| 100 |
|
|
/* rtrace command and defaults */
|
| 101 |
greg |
1.18 |
char *rtargv[256+2*MAXMODLIST] = { "rtrace",
|
| 102 |
greg |
1.51 |
"-dj", ".9", "-dr", "3",
|
| 103 |
greg |
1.42 |
"-ab", "1", "-ad", "350", };
|
| 104 |
greg |
1.40 |
|
| 105 |
greg |
1.13 |
int rtargc = 9;
|
| 106 |
greg |
1.1 |
/* overriding rtrace options */
|
| 107 |
greg |
1.40 |
char *myrtopts[] = { "-h-", "-x", "1", "-y", "0",
|
| 108 |
greg |
1.6 |
"-dt", "0", "-as", "0", "-aa", "0", NULL };
|
| 109 |
greg |
1.1 |
|
| 110 |
greg |
1.61 |
#define RTCOEFF "-o~~~~TmWdp" /* compute coefficients only */
|
| 111 |
|
|
#define RTCONTRIB "-o~~~~TmVdp" /* compute ray contributions */
|
| 112 |
greg |
1.40 |
|
| 113 |
greg |
1.1 |
struct rtproc rt0; /* head of rtrace process list */
|
| 114 |
|
|
|
| 115 |
|
|
struct rtproc *rt_unproc = NULL; /* unprocessed ray trees */
|
| 116 |
|
|
|
| 117 |
greg |
1.44 |
#define PERSIST_NONE 0 /* no persist file */
|
| 118 |
|
|
#define PERSIST_SINGLE 1 /* user set -P persist */
|
| 119 |
|
|
#define PERSIST_PARALL 2 /* user set -PP persist */
|
| 120 |
|
|
#define PERSIST_OURS 3 /* -PP persist belongs to us */
|
| 121 |
|
|
int persist_state = PERSIST_NONE; /* persist file state */
|
| 122 |
|
|
char persistfn[] = "pfXXXXXX"; /* our persist file name, if set */
|
| 123 |
greg |
1.1 |
|
| 124 |
|
|
int gargc; /* global argc */
|
| 125 |
|
|
char **gargv; /* global argv */
|
| 126 |
|
|
#define progname gargv[0]
|
| 127 |
|
|
|
| 128 |
|
|
char *octree; /* global octree argument */
|
| 129 |
|
|
|
| 130 |
|
|
int inpfmt = 'a'; /* input format */
|
| 131 |
|
|
int outfmt = 'a'; /* output format */
|
| 132 |
|
|
|
| 133 |
|
|
int header = 1; /* output header? */
|
| 134 |
greg |
1.35 |
int force_open = 0; /* truncate existing output? */
|
| 135 |
greg |
1.46 |
int recover = 0; /* recover previous output? */
|
| 136 |
greg |
1.49 |
int accumulate = 1; /* input rays per output record */
|
| 137 |
greg |
1.1 |
int xres = 0; /* horiz. output resolution */
|
| 138 |
|
|
int yres = 0; /* vert. output resolution */
|
| 139 |
|
|
|
| 140 |
greg |
1.49 |
int account; /* current accumulation count */
|
| 141 |
greg |
1.14 |
unsigned long raysleft; /* number of rays left to trace */
|
| 142 |
greg |
1.1 |
long waitflush; /* how long until next flush */
|
| 143 |
|
|
|
| 144 |
|
|
unsigned long lastray = 0; /* last ray number sent */
|
| 145 |
|
|
unsigned long lastdone = 0; /* last ray processed */
|
| 146 |
|
|
|
| 147 |
greg |
1.2 |
int using_stdout = 0; /* are we using stdout? */
|
| 148 |
|
|
|
| 149 |
greg |
1.1 |
const char *modname[MAXMODLIST]; /* ordered modifier name list */
|
| 150 |
|
|
int nmods = 0; /* number of modifiers */
|
| 151 |
|
|
|
| 152 |
greg |
1.29 |
#define queue_length() (lastray - lastdone)
|
| 153 |
|
|
|
| 154 |
greg |
1.31 |
MODCONT *growmodifier(MODCONT *mp, int nb);
|
| 155 |
|
|
MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt);
|
| 156 |
|
|
void addmodfile(char *fname, char *outf, char *binv, int bincnt);
|
| 157 |
greg |
1.1 |
|
| 158 |
greg |
1.5 |
void init(int np);
|
| 159 |
greg |
1.1 |
int done_rprocs(struct rtproc *rtp);
|
| 160 |
greg |
1.45 |
void reload_output(void);
|
| 161 |
greg |
1.14 |
void recover_output(FILE *fin);
|
| 162 |
|
|
void trace_contribs(FILE *fin);
|
| 163 |
greg |
1.1 |
struct rtproc *wait_rproc(void);
|
| 164 |
|
|
struct rtproc *get_rproc(void);
|
| 165 |
greg |
1.3 |
void queue_raytree(struct rtproc *rtp);
|
| 166 |
|
|
void process_queue(void);
|
| 167 |
greg |
1.1 |
|
| 168 |
greg |
1.14 |
void put_contrib(const DCOLOR cnt, FILE *fout);
|
| 169 |
greg |
1.2 |
void add_contrib(const char *modn);
|
| 170 |
greg |
1.49 |
void done_contrib(int navg);
|
| 171 |
greg |
1.1 |
|
| 172 |
greg |
1.29 |
/* return number of open rtrace processes */
|
| 173 |
|
|
static int
|
| 174 |
|
|
nrtprocs(void)
|
| 175 |
|
|
{
|
| 176 |
|
|
int nrtp = 0;
|
| 177 |
|
|
struct rtproc *rtp;
|
| 178 |
|
|
|
| 179 |
|
|
for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
|
| 180 |
|
|
nrtp += rtp->pd.running;
|
| 181 |
|
|
return(nrtp);
|
| 182 |
|
|
}
|
| 183 |
|
|
|
| 184 |
greg |
1.1 |
/* set input/output format */
|
| 185 |
|
|
static void
|
| 186 |
|
|
setformat(const char *fmt)
|
| 187 |
|
|
{
|
| 188 |
|
|
switch (fmt[0]) {
|
| 189 |
|
|
case 'f':
|
| 190 |
|
|
case 'd':
|
| 191 |
greg |
1.10 |
SET_FILE_BINARY(stdin);
|
| 192 |
|
|
/* fall through */
|
| 193 |
|
|
case 'a':
|
| 194 |
greg |
1.1 |
inpfmt = fmt[0];
|
| 195 |
|
|
break;
|
| 196 |
|
|
default:
|
| 197 |
|
|
goto fmterr;
|
| 198 |
|
|
}
|
| 199 |
|
|
switch (fmt[1]) {
|
| 200 |
|
|
case '\0':
|
| 201 |
|
|
outfmt = inpfmt;
|
| 202 |
|
|
return;
|
| 203 |
|
|
case 'a':
|
| 204 |
|
|
case 'f':
|
| 205 |
|
|
case 'd':
|
| 206 |
|
|
case 'c':
|
| 207 |
|
|
outfmt = fmt[1];
|
| 208 |
|
|
break;
|
| 209 |
|
|
default:
|
| 210 |
|
|
goto fmterr;
|
| 211 |
|
|
}
|
| 212 |
|
|
if (!fmt[2])
|
| 213 |
|
|
return;
|
| 214 |
|
|
fmterr:
|
| 215 |
|
|
sprintf(errmsg, "Illegal i/o format: -f%s", fmt);
|
| 216 |
|
|
error(USER, errmsg);
|
| 217 |
|
|
}
|
| 218 |
|
|
|
| 219 |
|
|
/* gather rays from rtrace and output contributions */
|
| 220 |
|
|
int
|
| 221 |
|
|
main(int argc, char *argv[])
|
| 222 |
|
|
{
|
| 223 |
greg |
1.40 |
int contrib = 0;
|
| 224 |
greg |
1.1 |
int nprocs = 1;
|
| 225 |
|
|
char *curout = NULL;
|
| 226 |
|
|
char *binval = NULL;
|
| 227 |
greg |
1.31 |
int bincnt = 0;
|
| 228 |
greg |
1.5 |
char fmt[8];
|
| 229 |
greg |
1.2 |
int i, j;
|
| 230 |
greg |
1.25 |
/* need at least one argument */
|
| 231 |
|
|
if (argc < 2) {
|
| 232 |
|
|
fprintf(stderr,
|
| 233 |
greg |
1.40 |
"Usage: %s [-n nprocs][-V][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
|
| 234 |
greg |
1.25 |
argv[0]);
|
| 235 |
|
|
exit(1);
|
| 236 |
|
|
}
|
| 237 |
greg |
1.2 |
/* global program name */
|
| 238 |
greg |
1.1 |
gargv = argv;
|
| 239 |
greg |
1.11 |
/* initialize calcomp routines */
|
| 240 |
greg |
1.1 |
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
|
| 241 |
|
|
esupport &= ~(E_OUTCHAN);
|
| 242 |
greg |
1.11 |
varset("PI", ':', PI);
|
| 243 |
greg |
1.1 |
/* get our options */
|
| 244 |
greg |
1.2 |
for (i = 1; i < argc-1; i++) {
|
| 245 |
|
|
/* expand arguments */
|
| 246 |
|
|
while ((j = expandarg(&argc, &argv, i)) > 0)
|
| 247 |
|
|
;
|
| 248 |
|
|
if (j < 0) {
|
| 249 |
greg |
1.52 |
fprintf(stderr, "%s: cannot expand '%s'\n",
|
| 250 |
greg |
1.2 |
argv[0], argv[i]);
|
| 251 |
|
|
exit(1);
|
| 252 |
|
|
}
|
| 253 |
|
|
if (argv[i][0] == '-')
|
| 254 |
|
|
switch (argv[i][1]) {
|
| 255 |
|
|
case 'n': /* number of processes */
|
| 256 |
greg |
1.21 |
if (argv[i][2] || i >= argc-2) break;
|
| 257 |
greg |
1.2 |
nprocs = atoi(argv[++i]);
|
| 258 |
|
|
if (nprocs <= 0)
|
| 259 |
|
|
error(USER, "illegal number of processes");
|
| 260 |
|
|
continue;
|
| 261 |
greg |
1.40 |
case 'V': /* output contributions */
|
| 262 |
|
|
switch (argv[i][2]) {
|
| 263 |
|
|
case '\0':
|
| 264 |
|
|
contrib = !contrib;
|
| 265 |
|
|
continue;
|
| 266 |
|
|
case '+': case '1':
|
| 267 |
|
|
case 'T': case 't':
|
| 268 |
|
|
case 'Y': case 'y':
|
| 269 |
|
|
contrib = 1;
|
| 270 |
|
|
continue;
|
| 271 |
|
|
case '-': case '0':
|
| 272 |
|
|
case 'F': case 'f':
|
| 273 |
|
|
case 'N': case 'n':
|
| 274 |
|
|
contrib = 0;
|
| 275 |
|
|
continue;
|
| 276 |
|
|
}
|
| 277 |
|
|
break;
|
| 278 |
greg |
1.49 |
case 'c': /* input rays per output */
|
| 279 |
|
|
if (argv[i][2] || i >= argc-2) break;
|
| 280 |
|
|
accumulate = atoi(argv[++i]);
|
| 281 |
|
|
continue;
|
| 282 |
greg |
1.40 |
case 'r': /* recover output */
|
| 283 |
|
|
if (argv[i][2]) break;
|
| 284 |
greg |
1.46 |
recover = 1;
|
| 285 |
greg |
1.40 |
continue;
|
| 286 |
greg |
1.2 |
case 'h': /* output header? */
|
| 287 |
|
|
switch (argv[i][2]) {
|
| 288 |
|
|
case '\0':
|
| 289 |
|
|
header = !header;
|
| 290 |
|
|
continue;
|
| 291 |
greg |
1.3 |
case '+': case '1':
|
| 292 |
|
|
case 'T': case 't':
|
| 293 |
|
|
case 'Y': case 'y':
|
| 294 |
greg |
1.2 |
header = 1;
|
| 295 |
|
|
continue;
|
| 296 |
greg |
1.3 |
case '-': case '0':
|
| 297 |
|
|
case 'F': case 'f':
|
| 298 |
|
|
case 'N': case 'n':
|
| 299 |
greg |
1.2 |
header = 0;
|
| 300 |
|
|
continue;
|
| 301 |
|
|
}
|
| 302 |
|
|
break;
|
| 303 |
greg |
1.35 |
case 'f': /* file or force or format */
|
| 304 |
greg |
1.2 |
if (!argv[i][2]) {
|
| 305 |
greg |
1.9 |
char *fpath;
|
| 306 |
greg |
1.21 |
if (i >= argc-2) break;
|
| 307 |
greg |
1.9 |
fpath = getpath(argv[++i],
|
| 308 |
|
|
getrlibpath(), R_OK);
|
| 309 |
|
|
if (fpath == NULL) {
|
| 310 |
|
|
sprintf(errmsg,
|
| 311 |
|
|
"cannot find file '%s'",
|
| 312 |
|
|
argv[i]);
|
| 313 |
|
|
error(USER, errmsg);
|
| 314 |
|
|
}
|
| 315 |
|
|
fcompile(fpath);
|
| 316 |
greg |
1.2 |
continue;
|
| 317 |
|
|
}
|
| 318 |
greg |
1.35 |
if (argv[i][2] == 'o') {
|
| 319 |
greg |
1.46 |
force_open = 1;
|
| 320 |
greg |
1.35 |
continue;
|
| 321 |
|
|
}
|
| 322 |
greg |
1.2 |
setformat(argv[i]+2);
|
| 323 |
|
|
continue;
|
| 324 |
|
|
case 'e': /* expression */
|
| 325 |
greg |
1.21 |
if (argv[i][2] || i >= argc-2) break;
|
| 326 |
greg |
1.2 |
scompile(argv[++i], NULL, 0);
|
| 327 |
greg |
1.1 |
continue;
|
| 328 |
greg |
1.2 |
case 'o': /* output file spec. */
|
| 329 |
greg |
1.21 |
if (argv[i][2] || i >= argc-2) break;
|
| 330 |
greg |
1.2 |
curout = argv[++i];
|
| 331 |
greg |
1.1 |
continue;
|
| 332 |
greg |
1.2 |
case 'x': /* horiz. output resolution */
|
| 333 |
greg |
1.21 |
if (argv[i][2] || i >= argc-2) break;
|
| 334 |
greg |
1.2 |
xres = atoi(argv[++i]);
|
| 335 |
greg |
1.1 |
continue;
|
| 336 |
greg |
1.2 |
case 'y': /* vert. output resolution */
|
| 337 |
greg |
1.21 |
if (argv[i][2] || i >= argc-2) break;
|
| 338 |
greg |
1.2 |
yres = atoi(argv[++i]);
|
| 339 |
|
|
continue;
|
| 340 |
greg |
1.31 |
case 'b': /* bin expression/count */
|
| 341 |
|
|
if (i >= argc-2) break;
|
| 342 |
|
|
if (argv[i][2] == 'n') {
|
| 343 |
greg |
1.54 |
bincnt = (int)(eval(argv[++i]) + .5);
|
| 344 |
greg |
1.31 |
continue;
|
| 345 |
|
|
}
|
| 346 |
|
|
if (argv[i][2]) break;
|
| 347 |
greg |
1.2 |
binval = argv[++i];
|
| 348 |
|
|
continue;
|
| 349 |
|
|
case 'm': /* modifier name */
|
| 350 |
greg |
1.21 |
if (argv[i][2] || i >= argc-2) break;
|
| 351 |
greg |
1.2 |
rtargv[rtargc++] = "-ti";
|
| 352 |
|
|
rtargv[rtargc++] = argv[++i];
|
| 353 |
greg |
1.31 |
addmodifier(argv[i], curout, binval, bincnt);
|
| 354 |
greg |
1.1 |
continue;
|
| 355 |
greg |
1.17 |
case 'M': /* modifier file */
|
| 356 |
greg |
1.21 |
if (argv[i][2] || i >= argc-2) break;
|
| 357 |
greg |
1.19 |
rtargv[rtargc++] = "-tI";
|
| 358 |
|
|
rtargv[rtargc++] = argv[++i];
|
| 359 |
greg |
1.31 |
addmodfile(argv[i], curout, binval, bincnt);
|
| 360 |
greg |
1.17 |
continue;
|
| 361 |
greg |
1.39 |
case 'P': /* persist file */
|
| 362 |
greg |
1.44 |
if (i >= argc-2) break;
|
| 363 |
|
|
persist_state = (argv[i][2] == 'P') ?
|
| 364 |
|
|
PERSIST_PARALL : PERSIST_SINGLE;
|
| 365 |
|
|
rtargv[rtargc++] = argv[i];
|
| 366 |
|
|
rtargv[rtargc++] = argv[++i];
|
| 367 |
|
|
continue;
|
| 368 |
greg |
1.1 |
}
|
| 369 |
greg |
1.2 |
rtargv[rtargc++] = argv[i]; /* assume rtrace option */
|
| 370 |
greg |
1.1 |
}
|
| 371 |
greg |
1.49 |
if (accumulate <= 0) /* no output flushing for single record */
|
| 372 |
greg |
1.45 |
xres = yres = 0;
|
| 373 |
greg |
1.2 |
/* set global argument list */
|
| 374 |
|
|
gargc = argc; gargv = argv;
|
| 375 |
greg |
1.1 |
/* add "mandatory" rtrace settings */
|
| 376 |
greg |
1.2 |
for (j = 0; myrtopts[j] != NULL; j++)
|
| 377 |
|
|
rtargv[rtargc++] = myrtopts[j];
|
| 378 |
greg |
1.40 |
rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF;
|
| 379 |
greg |
1.2 |
/* just asking for defaults? */
|
| 380 |
|
|
if (!strcmp(argv[i], "-defaults")) {
|
| 381 |
greg |
1.56 |
char nps[8], sxres[16], syres[16];
|
| 382 |
greg |
1.1 |
char *rtpath;
|
| 383 |
greg |
1.49 |
printf("-c %-5d\t\t\t# accumulated rays per record\n",
|
| 384 |
|
|
accumulate);
|
| 385 |
greg |
1.40 |
printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-',
|
| 386 |
|
|
contrib ? "contributions" : "coefficients");
|
| 387 |
greg |
1.1 |
fflush(stdout); /* report OUR options */
|
| 388 |
greg |
1.56 |
rtargv[rtargc++] = "-n";
|
| 389 |
|
|
sprintf(nps, "%d", nprocs);
|
| 390 |
|
|
rtargv[rtargc++] = nps;
|
| 391 |
greg |
1.1 |
rtargv[rtargc++] = header ? "-h+" : "-h-";
|
| 392 |
|
|
sprintf(fmt, "-f%c%c", inpfmt, outfmt);
|
| 393 |
|
|
rtargv[rtargc++] = fmt;
|
| 394 |
|
|
rtargv[rtargc++] = "-x";
|
| 395 |
|
|
sprintf(sxres, "%d", xres);
|
| 396 |
|
|
rtargv[rtargc++] = sxres;
|
| 397 |
|
|
rtargv[rtargc++] = "-y";
|
| 398 |
|
|
sprintf(syres, "%d", yres);
|
| 399 |
|
|
rtargv[rtargc++] = syres;
|
| 400 |
|
|
rtargv[rtargc++] = "-defaults";
|
| 401 |
|
|
rtargv[rtargc] = NULL;
|
| 402 |
|
|
rtpath = getpath(rtargv[0], getenv("PATH"), X_OK);
|
| 403 |
|
|
if (rtpath == NULL) {
|
| 404 |
|
|
eputs(rtargv[0]);
|
| 405 |
|
|
eputs(": command not found\n");
|
| 406 |
|
|
exit(1);
|
| 407 |
|
|
}
|
| 408 |
|
|
execv(rtpath, rtargv);
|
| 409 |
|
|
perror(rtpath); /* execv() should not return */
|
| 410 |
|
|
exit(1);
|
| 411 |
greg |
1.5 |
}
|
| 412 |
|
|
if (nprocs > 1) { /* add persist file if parallel */
|
| 413 |
greg |
1.44 |
if (persist_state == PERSIST_SINGLE)
|
| 414 |
|
|
error(USER, "use -PP option for multiple processes");
|
| 415 |
|
|
if (persist_state == PERSIST_NONE) {
|
| 416 |
|
|
rtargv[rtargc++] = "-PP";
|
| 417 |
|
|
rtargv[rtargc++] = mktemp(persistfn);
|
| 418 |
|
|
persist_state = PERSIST_OURS;
|
| 419 |
|
|
}
|
| 420 |
greg |
1.2 |
}
|
| 421 |
greg |
1.1 |
/* add format string */
|
| 422 |
|
|
sprintf(fmt, "-f%cf", inpfmt);
|
| 423 |
|
|
rtargv[rtargc++] = fmt;
|
| 424 |
|
|
/* octree argument is last */
|
| 425 |
greg |
1.2 |
if (i <= 0 || i != argc-1 || argv[i][0] == '-')
|
| 426 |
|
|
error(USER, "missing octree argument");
|
| 427 |
|
|
rtargv[rtargc++] = octree = argv[i];
|
| 428 |
greg |
1.1 |
rtargv[rtargc] = NULL;
|
| 429 |
greg |
1.46 |
/* start rtrace & recover if requested */
|
| 430 |
greg |
1.1 |
init(nprocs);
|
| 431 |
greg |
1.46 |
/* compute contributions */
|
| 432 |
|
|
trace_contribs(stdin);
|
| 433 |
|
|
/* clean up */
|
| 434 |
greg |
1.1 |
quit(0);
|
| 435 |
|
|
}
|
| 436 |
|
|
|
| 437 |
schorsch |
1.26 |
#ifndef SIGALRM
|
| 438 |
|
|
#define SIGALRM SIGTERM
|
| 439 |
|
|
#endif
|
| 440 |
greg |
1.1 |
/* kill persistent rtrace process */
|
| 441 |
|
|
static void
|
| 442 |
|
|
killpersist(void)
|
| 443 |
|
|
{
|
| 444 |
|
|
FILE *fp = fopen(persistfn, "r");
|
| 445 |
schorsch |
1.26 |
RT_PID pid;
|
| 446 |
greg |
1.1 |
|
| 447 |
|
|
if (fp == NULL)
|
| 448 |
|
|
return;
|
| 449 |
|
|
if (fscanf(fp, "%*s %d", &pid) != 1 || kill(pid, SIGALRM) < 0)
|
| 450 |
|
|
unlink(persistfn);
|
| 451 |
|
|
fclose(fp);
|
| 452 |
|
|
}
|
| 453 |
|
|
|
| 454 |
|
|
/* close rtrace processes and clean up */
|
| 455 |
|
|
int
|
| 456 |
|
|
done_rprocs(struct rtproc *rtp)
|
| 457 |
|
|
{
|
| 458 |
|
|
int st0, st1 = 0;
|
| 459 |
|
|
|
| 460 |
|
|
if (rtp->next != NULL) { /* close last opened first! */
|
| 461 |
|
|
st1 = done_rprocs(rtp->next);
|
| 462 |
|
|
free((void *)rtp->next);
|
| 463 |
|
|
rtp->next = NULL;
|
| 464 |
|
|
}
|
| 465 |
|
|
st0 = close_process(&rtp->pd);
|
| 466 |
|
|
if (st0 < 0)
|
| 467 |
|
|
error(WARNING, "unknown return status from rtrace process");
|
| 468 |
|
|
else if (st0 > 0)
|
| 469 |
|
|
return(st0);
|
| 470 |
|
|
return(st1);
|
| 471 |
|
|
}
|
| 472 |
|
|
|
| 473 |
|
|
/* exit with status */
|
| 474 |
|
|
void
|
| 475 |
|
|
quit(int status)
|
| 476 |
|
|
{
|
| 477 |
|
|
int rtstat;
|
| 478 |
|
|
|
| 479 |
greg |
1.45 |
if (persist_state == PERSIST_OURS) /* terminate waiting rtrace */
|
| 480 |
greg |
1.1 |
killpersist();
|
| 481 |
|
|
/* clean up rtrace process(es) */
|
| 482 |
|
|
rtstat = done_rprocs(&rt0);
|
| 483 |
|
|
if (status == 0)
|
| 484 |
|
|
status = rtstat;
|
| 485 |
|
|
exit(status); /* flushes all output streams */
|
| 486 |
|
|
}
|
| 487 |
|
|
|
| 488 |
greg |
1.5 |
/* start rtrace processes and initialize */
|
| 489 |
greg |
1.1 |
void
|
| 490 |
|
|
init(int np)
|
| 491 |
|
|
{
|
| 492 |
|
|
struct rtproc *rtp;
|
| 493 |
|
|
int i;
|
| 494 |
|
|
int maxbytes;
|
| 495 |
greg |
1.2 |
/* make sure we have something to do */
|
| 496 |
|
|
if (!nmods)
|
| 497 |
|
|
error(USER, "No modifiers specified");
|
| 498 |
greg |
1.1 |
/* assign ray variables */
|
| 499 |
|
|
scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
|
| 500 |
|
|
scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
|
| 501 |
|
|
/* set up signal handling */
|
| 502 |
greg |
1.3 |
signal(SIGINT, quit);
|
| 503 |
|
|
#ifdef SIGHUP
|
| 504 |
|
|
signal(SIGHUP, quit);
|
| 505 |
|
|
#endif
|
| 506 |
|
|
#ifdef SIGTERM
|
| 507 |
|
|
signal(SIGTERM, quit);
|
| 508 |
|
|
#endif
|
| 509 |
|
|
#ifdef SIGPIPE
|
| 510 |
greg |
1.1 |
signal(SIGPIPE, quit);
|
| 511 |
|
|
#endif
|
| 512 |
|
|
rtp = &rt0; /* start rtrace process(es) */
|
| 513 |
|
|
for (i = 0; i++ < np; ) {
|
| 514 |
|
|
errno = 0;
|
| 515 |
|
|
maxbytes = open_process(&rtp->pd, rtargv);
|
| 516 |
|
|
if (maxbytes == 0) {
|
| 517 |
|
|
eputs(rtargv[0]);
|
| 518 |
|
|
eputs(": command not found\n");
|
| 519 |
|
|
exit(1);
|
| 520 |
|
|
}
|
| 521 |
|
|
if (maxbytes < 0)
|
| 522 |
|
|
error(SYSTEM, "cannot start rtrace process");
|
| 523 |
|
|
if (maxbytes > treebufsiz)
|
| 524 |
|
|
treebufsiz = maxbytes;
|
| 525 |
greg |
1.2 |
rtp->raynum = 0;
|
| 526 |
greg |
1.1 |
rtp->bsiz = 0;
|
| 527 |
|
|
rtp->buf = NULL;
|
| 528 |
greg |
1.2 |
rtp->nbr = 0;
|
| 529 |
greg |
1.1 |
if (i == np) /* last process? */
|
| 530 |
|
|
break;
|
| 531 |
|
|
if (i == 1)
|
| 532 |
|
|
sleep(2); /* wait for persist file */
|
| 533 |
|
|
rtp->next = (struct rtproc *)malloc(sizeof(struct rtproc));
|
| 534 |
|
|
if (rtp->next == NULL)
|
| 535 |
|
|
error(SYSTEM, "out of memory in init");
|
| 536 |
|
|
rtp = rtp->next;
|
| 537 |
|
|
}
|
| 538 |
|
|
rtp->next = NULL; /* terminate list */
|
| 539 |
|
|
if (yres > 0) {
|
| 540 |
|
|
if (xres > 0)
|
| 541 |
greg |
1.14 |
raysleft = (unsigned long)xres*yres;
|
| 542 |
greg |
1.1 |
else
|
| 543 |
|
|
raysleft = yres;
|
| 544 |
|
|
} else
|
| 545 |
|
|
raysleft = 0;
|
| 546 |
greg |
1.49 |
if ((account = accumulate) > 0)
|
| 547 |
|
|
raysleft *= accumulate;
|
| 548 |
greg |
1.57 |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
|
| 549 |
greg |
1.46 |
if (!recover)
|
| 550 |
|
|
return;
|
| 551 |
|
|
/* recover previous values */
|
| 552 |
greg |
1.49 |
if (accumulate <= 0)
|
| 553 |
greg |
1.46 |
reload_output();
|
| 554 |
|
|
else
|
| 555 |
|
|
recover_output(stdin);
|
| 556 |
greg |
1.1 |
}
|
| 557 |
|
|
|
| 558 |
greg |
1.31 |
/* grow modifier to accommodate more bins */
|
| 559 |
|
|
MODCONT *
|
| 560 |
|
|
growmodifier(MODCONT *mp, int nb)
|
| 561 |
|
|
{
|
| 562 |
|
|
if (nb <= mp->nbins)
|
| 563 |
|
|
return mp;
|
| 564 |
|
|
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1));
|
| 565 |
|
|
if (mp == NULL)
|
| 566 |
|
|
error(SYSTEM, "out of memory in growmodifier");
|
| 567 |
|
|
memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins));
|
| 568 |
|
|
mp->nbins = nb;
|
| 569 |
|
|
return mp;
|
| 570 |
|
|
}
|
| 571 |
|
|
|
| 572 |
greg |
1.1 |
/* add modifier to our list to track */
|
| 573 |
|
|
MODCONT *
|
| 574 |
greg |
1.31 |
addmodifier(char *modn, char *outf, char *binv, int bincnt)
|
| 575 |
greg |
1.1 |
{
|
| 576 |
|
|
LUENT *lep = lu_find(&modconttab, modn);
|
| 577 |
|
|
MODCONT *mp;
|
| 578 |
greg |
1.32 |
int i;
|
| 579 |
greg |
1.1 |
|
| 580 |
|
|
if (lep->data != NULL) {
|
| 581 |
|
|
sprintf(errmsg, "duplicate modifier '%s'", modn);
|
| 582 |
|
|
error(USER, errmsg);
|
| 583 |
|
|
}
|
| 584 |
|
|
if (nmods >= MAXMODLIST)
|
| 585 |
greg |
1.54 |
error(INTERNAL, "too many modifiers");
|
| 586 |
greg |
1.1 |
modname[nmods++] = modn; /* XXX assumes static string */
|
| 587 |
|
|
lep->key = modn; /* XXX assumes static string */
|
| 588 |
|
|
mp = (MODCONT *)malloc(sizeof(MODCONT));
|
| 589 |
|
|
if (mp == NULL)
|
| 590 |
|
|
error(SYSTEM, "out of memory in addmodifier");
|
| 591 |
|
|
mp->outspec = outf; /* XXX assumes static string */
|
| 592 |
|
|
mp->modname = modn; /* XXX assumes static string */
|
| 593 |
greg |
1.54 |
if (binv == NULL)
|
| 594 |
|
|
binv = "0"; /* use single bin if unspecified */
|
| 595 |
|
|
mp->binv = eparse(binv);
|
| 596 |
|
|
if (mp->binv->type == NUM) { /* check value if constant */
|
| 597 |
|
|
bincnt = (int)(evalue(mp->binv) + 1.5);
|
| 598 |
|
|
if (bincnt != 1) {
|
| 599 |
|
|
sprintf(errmsg, "illegal non-zero constant for bin (%s)",
|
| 600 |
|
|
binv);
|
| 601 |
|
|
error(USER, errmsg);
|
| 602 |
|
|
}
|
| 603 |
|
|
}
|
| 604 |
|
|
mp->nbins = 1; /* initialize results holder */
|
| 605 |
greg |
1.1 |
setcolor(mp->cbin[0], 0., 0., 0.);
|
| 606 |
greg |
1.54 |
if (bincnt > 1)
|
| 607 |
greg |
1.31 |
mp = growmodifier(mp, bincnt);
|
| 608 |
greg |
1.32 |
lep->data = (char *)mp;
|
| 609 |
|
|
/* allocate output streams */
|
| 610 |
greg |
1.45 |
for (i = bincnt; i-- > 0; )
|
| 611 |
greg |
1.32 |
getostream(mp->outspec, mp->modname, i, 1);
|
| 612 |
greg |
1.1 |
return mp;
|
| 613 |
|
|
}
|
| 614 |
|
|
|
| 615 |
greg |
1.17 |
/* add modifiers from a file list */
|
| 616 |
|
|
void
|
| 617 |
greg |
1.31 |
addmodfile(char *fname, char *outf, char *binv, int bincnt)
|
| 618 |
greg |
1.17 |
{
|
| 619 |
|
|
char *mname[MAXMODLIST];
|
| 620 |
|
|
int i;
|
| 621 |
greg |
1.20 |
/* find the file & store strings */
|
| 622 |
|
|
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
|
| 623 |
|
|
sprintf(errmsg, "cannot find modifier file '%s'", fname);
|
| 624 |
|
|
error(SYSTEM, errmsg);
|
| 625 |
|
|
}
|
| 626 |
greg |
1.17 |
for (i = 0; mname[i]; i++) /* add each one */
|
| 627 |
greg |
1.31 |
addmodifier(mname[i], outf, binv, bincnt);
|
| 628 |
greg |
1.17 |
}
|
| 629 |
|
|
|
| 630 |
greg |
1.1 |
/* put string to stderr */
|
| 631 |
|
|
void
|
| 632 |
|
|
eputs(char *s)
|
| 633 |
|
|
{
|
| 634 |
|
|
static int midline = 0;
|
| 635 |
|
|
|
| 636 |
|
|
if (!*s) return;
|
| 637 |
|
|
if (!midline) {
|
| 638 |
|
|
fputs(progname, stderr);
|
| 639 |
|
|
fputs(": ", stderr);
|
| 640 |
|
|
}
|
| 641 |
|
|
fputs(s, stderr);
|
| 642 |
|
|
midline = s[strlen(s)-1] != '\n';
|
| 643 |
|
|
}
|
| 644 |
|
|
|
| 645 |
greg |
1.14 |
/* construct output file name and return flags whether modifier/bin present */
|
| 646 |
|
|
int
|
| 647 |
|
|
ofname(char *oname, const char *ospec, const char *mname, int bn)
|
| 648 |
|
|
{
|
| 649 |
|
|
const char *mnp = NULL;
|
| 650 |
|
|
const char *bnp = NULL;
|
| 651 |
|
|
const char *cp;
|
| 652 |
|
|
|
| 653 |
|
|
if (ospec == NULL)
|
| 654 |
|
|
return -1;
|
| 655 |
|
|
for (cp = ospec; *cp; cp++) /* check format position(s) */
|
| 656 |
|
|
if (*cp == '%') {
|
| 657 |
|
|
do
|
| 658 |
|
|
++cp;
|
| 659 |
|
|
while (isdigit(*cp));
|
| 660 |
|
|
switch (*cp) {
|
| 661 |
|
|
case '%':
|
| 662 |
|
|
break;
|
| 663 |
|
|
case 's':
|
| 664 |
|
|
if (mnp != NULL)
|
| 665 |
|
|
return -1;
|
| 666 |
|
|
mnp = cp;
|
| 667 |
|
|
break;
|
| 668 |
|
|
case 'd':
|
| 669 |
greg |
1.55 |
case 'i':
|
| 670 |
|
|
case 'o':
|
| 671 |
|
|
case 'x':
|
| 672 |
|
|
case 'X':
|
| 673 |
greg |
1.14 |
if (bnp != NULL)
|
| 674 |
|
|
return -1;
|
| 675 |
|
|
bnp = cp;
|
| 676 |
|
|
break;
|
| 677 |
|
|
default:
|
| 678 |
|
|
return -1;
|
| 679 |
|
|
}
|
| 680 |
|
|
}
|
| 681 |
|
|
if (mnp != NULL) { /* create file name */
|
| 682 |
|
|
if (bnp != NULL) {
|
| 683 |
|
|
if (bnp > mnp)
|
| 684 |
|
|
sprintf(oname, ospec, mname, bn);
|
| 685 |
|
|
else
|
| 686 |
|
|
sprintf(oname, ospec, bn, mname);
|
| 687 |
|
|
return OF_MODIFIER|OF_BIN;
|
| 688 |
|
|
}
|
| 689 |
|
|
sprintf(oname, ospec, mname);
|
| 690 |
|
|
return OF_MODIFIER;
|
| 691 |
|
|
}
|
| 692 |
|
|
if (bnp != NULL) {
|
| 693 |
|
|
sprintf(oname, ospec, bn);
|
| 694 |
|
|
return OF_BIN;
|
| 695 |
|
|
}
|
| 696 |
|
|
strcpy(oname, ospec);
|
| 697 |
|
|
return 0;
|
| 698 |
|
|
}
|
| 699 |
|
|
|
| 700 |
greg |
1.1 |
/* write header to the given output stream */
|
| 701 |
|
|
void
|
| 702 |
greg |
1.14 |
printheader(FILE *fout, const char *info)
|
| 703 |
greg |
1.1 |
{
|
| 704 |
|
|
extern char VersionID[];
|
| 705 |
|
|
FILE *fin = fopen(octree, "r");
|
| 706 |
|
|
|
| 707 |
|
|
if (fin == NULL)
|
| 708 |
|
|
quit(1);
|
| 709 |
greg |
1.2 |
checkheader(fin, "ignore", fout); /* copy octree header */
|
| 710 |
greg |
1.1 |
fclose(fin);
|
| 711 |
|
|
printargs(gargc-1, gargv, fout); /* add our command */
|
| 712 |
|
|
fprintf(fout, "SOFTWARE= %s\n", VersionID);
|
| 713 |
|
|
fputnow(fout);
|
| 714 |
greg |
1.14 |
if (info != NULL) /* add extra info if given */
|
| 715 |
|
|
fputs(info, fout);
|
| 716 |
greg |
1.1 |
switch (outfmt) { /* add output format */
|
| 717 |
|
|
case 'a':
|
| 718 |
|
|
fputformat("ascii", fout);
|
| 719 |
|
|
break;
|
| 720 |
|
|
case 'f':
|
| 721 |
|
|
fputformat("float", fout);
|
| 722 |
|
|
break;
|
| 723 |
|
|
case 'd':
|
| 724 |
|
|
fputformat("double", fout);
|
| 725 |
|
|
break;
|
| 726 |
|
|
case 'c':
|
| 727 |
|
|
fputformat(COLRFMT, fout);
|
| 728 |
|
|
break;
|
| 729 |
|
|
}
|
| 730 |
|
|
fputc('\n', fout);
|
| 731 |
greg |
1.30 |
}
|
| 732 |
|
|
|
| 733 |
|
|
/* write resolution string to given output stream */
|
| 734 |
|
|
void
|
| 735 |
greg |
1.45 |
printresolu(FILE *fout, int xr, int yr)
|
| 736 |
greg |
1.30 |
{
|
| 737 |
greg |
1.45 |
if ((xr > 0) & (yr > 0)) /* resolution string */
|
| 738 |
|
|
fprtresolu(xr, yr, fout);
|
| 739 |
greg |
1.1 |
}
|
| 740 |
|
|
|
| 741 |
greg |
1.32 |
/* Get output stream pointer (open and write header if new and noopen==0) */
|
| 742 |
greg |
1.31 |
STREAMOUT *
|
| 743 |
greg |
1.32 |
getostream(const char *ospec, const char *mname, int bn, int noopen)
|
| 744 |
greg |
1.31 |
{
|
| 745 |
greg |
1.45 |
static const DCOLOR nocontrib = BLKCOLOR;
|
| 746 |
greg |
1.31 |
static STREAMOUT stdos;
|
| 747 |
|
|
int ofl;
|
| 748 |
|
|
char oname[1024];
|
| 749 |
|
|
LUENT *lep;
|
| 750 |
|
|
STREAMOUT *sop;
|
| 751 |
greg |
1.1 |
|
| 752 |
|
|
if (ospec == NULL) { /* use stdout? */
|
| 753 |
greg |
1.32 |
if (!noopen && !using_stdout) {
|
| 754 |
greg |
1.10 |
if (outfmt != 'a')
|
| 755 |
|
|
SET_FILE_BINARY(stdout);
|
| 756 |
|
|
if (header)
|
| 757 |
greg |
1.14 |
printheader(stdout, NULL);
|
| 758 |
greg |
1.45 |
printresolu(stdout, xres, yres);
|
| 759 |
greg |
1.57 |
if (waitflush > 0)
|
| 760 |
|
|
fflush(stdout);
|
| 761 |
greg |
1.45 |
stdos.xr = xres; stdos.yr = yres;
|
| 762 |
greg |
1.31 |
using_stdout = 1;
|
| 763 |
greg |
1.10 |
}
|
| 764 |
greg |
1.31 |
stdos.ofp = stdout;
|
| 765 |
greg |
1.32 |
stdos.reclen += noopen;
|
| 766 |
greg |
1.31 |
return &stdos;
|
| 767 |
greg |
1.1 |
}
|
| 768 |
greg |
1.14 |
ofl = ofname(oname, ospec, mname, bn); /* get output name */
|
| 769 |
|
|
if (ofl < 0) {
|
| 770 |
|
|
sprintf(errmsg, "bad output format '%s'", ospec);
|
| 771 |
|
|
error(USER, errmsg);
|
| 772 |
|
|
}
|
| 773 |
|
|
lep = lu_find(&ofiletab, oname); /* look it up */
|
| 774 |
greg |
1.1 |
if (lep->key == NULL) /* new entry */
|
| 775 |
greg |
1.14 |
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
|
| 776 |
greg |
1.31 |
sop = (STREAMOUT *)lep->data;
|
| 777 |
|
|
if (sop == NULL) { /* allocate stream */
|
| 778 |
|
|
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT));
|
| 779 |
|
|
if (sop == NULL)
|
| 780 |
|
|
error(SYSTEM, "out of memory in getostream");
|
| 781 |
greg |
1.45 |
sop->outpipe = oname[0] == '!';
|
| 782 |
|
|
sop->reclen = 0;
|
| 783 |
greg |
1.32 |
sop->ofp = NULL; /* open iff noopen==0 */
|
| 784 |
greg |
1.45 |
sop->xr = xres; sop->yr = yres;
|
| 785 |
greg |
1.31 |
lep->data = (char *)sop;
|
| 786 |
greg |
1.46 |
if (!sop->outpipe & !force_open & !recover &&
|
| 787 |
|
|
access(oname, F_OK) == 0) {
|
| 788 |
greg |
1.45 |
errno = EEXIST; /* file exists */
|
| 789 |
|
|
goto openerr;
|
| 790 |
|
|
}
|
| 791 |
greg |
1.31 |
}
|
| 792 |
greg |
1.32 |
if (!noopen && sop->ofp == NULL) { /* open output stream */
|
| 793 |
greg |
1.34 |
long i;
|
| 794 |
greg |
1.14 |
if (oname[0] == '!') /* output to command */
|
| 795 |
greg |
1.31 |
sop->ofp = popen(oname+1, "w");
|
| 796 |
greg |
1.45 |
else /* else open file */
|
| 797 |
greg |
1.31 |
sop->ofp = fopen(oname, "w");
|
| 798 |
greg |
1.45 |
if (sop->ofp == NULL)
|
| 799 |
|
|
goto openerr;
|
| 800 |
greg |
1.10 |
if (outfmt != 'a')
|
| 801 |
greg |
1.31 |
SET_FILE_BINARY(sop->ofp);
|
| 802 |
greg |
1.14 |
if (header) {
|
| 803 |
|
|
char info[512];
|
| 804 |
|
|
char *cp = info;
|
| 805 |
greg |
1.32 |
if (ofl & OF_MODIFIER || sop->reclen == 1) {
|
| 806 |
greg |
1.30 |
sprintf(cp, "MODIFIER=%s\n", mname);
|
| 807 |
|
|
while (*cp) ++cp;
|
| 808 |
|
|
}
|
| 809 |
greg |
1.14 |
if (ofl & OF_BIN) {
|
| 810 |
|
|
sprintf(cp, "BIN=%d\n", bn);
|
| 811 |
|
|
while (*cp) ++cp;
|
| 812 |
|
|
}
|
| 813 |
|
|
*cp = '\0';
|
| 814 |
greg |
1.31 |
printheader(sop->ofp, info);
|
| 815 |
greg |
1.14 |
}
|
| 816 |
greg |
1.49 |
if (accumulate > 0) { /* global resolution */
|
| 817 |
greg |
1.47 |
sop->xr = xres; sop->yr = yres;
|
| 818 |
|
|
}
|
| 819 |
greg |
1.45 |
printresolu(sop->ofp, sop->xr, sop->yr);
|
| 820 |
greg |
1.2 |
/* play catch-up */
|
| 821 |
greg |
1.49 |
for (i = accumulate > 0 ? lastdone/accumulate : 0; i--; ) {
|
| 822 |
greg |
1.45 |
int j = sop->reclen;
|
| 823 |
|
|
if (j <= 0) j = 1;
|
| 824 |
|
|
while (j--)
|
| 825 |
|
|
put_contrib(nocontrib, sop->ofp);
|
| 826 |
greg |
1.2 |
if (outfmt == 'a')
|
| 827 |
greg |
1.31 |
putc('\n', sop->ofp);
|
| 828 |
greg |
1.2 |
}
|
| 829 |
greg |
1.57 |
if (waitflush > 0)
|
| 830 |
greg |
1.31 |
fflush(sop->ofp);
|
| 831 |
greg |
1.1 |
}
|
| 832 |
greg |
1.45 |
sop->reclen += noopen; /* add to length if noopen */
|
| 833 |
|
|
return sop; /* return output stream */
|
| 834 |
|
|
openerr:
|
| 835 |
|
|
sprintf(errmsg, "cannot open '%s' for writing", oname);
|
| 836 |
|
|
error(SYSTEM, errmsg);
|
| 837 |
|
|
return NULL; /* pro forma return */
|
| 838 |
greg |
1.1 |
}
|
| 839 |
|
|
|
| 840 |
|
|
/* read input ray into buffer */
|
| 841 |
|
|
int
|
| 842 |
|
|
getinp(char *buf, FILE *fp)
|
| 843 |
|
|
{
|
| 844 |
greg |
1.45 |
double dv[3], *dvp;
|
| 845 |
|
|
float *fvp;
|
| 846 |
greg |
1.4 |
char *cp;
|
| 847 |
|
|
int i;
|
| 848 |
|
|
|
| 849 |
greg |
1.1 |
switch (inpfmt) {
|
| 850 |
|
|
case 'a':
|
| 851 |
greg |
1.4 |
cp = buf; /* make sure we get 6 floats */
|
| 852 |
|
|
for (i = 0; i < 6; i++) {
|
| 853 |
|
|
if (fgetword(cp, buf+127-cp, fp) == NULL)
|
| 854 |
greg |
1.36 |
return -1;
|
| 855 |
|
|
if (i >= 3)
|
| 856 |
|
|
dv[i-3] = atof(cp);
|
| 857 |
greg |
1.4 |
if ((cp = fskip(cp)) == NULL || *cp)
|
| 858 |
greg |
1.36 |
return -1;
|
| 859 |
greg |
1.4 |
*cp++ = ' ';
|
| 860 |
|
|
}
|
| 861 |
|
|
getc(fp); /* get/put eol */
|
| 862 |
|
|
*cp-- = '\0'; *cp = '\n';
|
| 863 |
greg |
1.36 |
if (DOT(dv,dv) <= FTINY*FTINY)
|
| 864 |
|
|
return 0; /* dummy ray */
|
| 865 |
greg |
1.1 |
return strlen(buf);
|
| 866 |
|
|
case 'f':
|
| 867 |
|
|
if (fread(buf, sizeof(float), 6, fp) < 6)
|
| 868 |
greg |
1.36 |
return -1;
|
| 869 |
greg |
1.45 |
fvp = (float *)buf + 3;
|
| 870 |
|
|
if (DOT(fvp,fvp) <= FTINY*FTINY)
|
| 871 |
greg |
1.36 |
return 0; /* dummy ray */
|
| 872 |
greg |
1.1 |
return sizeof(float)*6;
|
| 873 |
|
|
case 'd':
|
| 874 |
|
|
if (fread(buf, sizeof(double), 6, fp) < 6)
|
| 875 |
greg |
1.36 |
return -1;
|
| 876 |
greg |
1.45 |
dvp = (double *)buf + 3;
|
| 877 |
|
|
if (DOT(dvp,dvp) <= FTINY*FTINY)
|
| 878 |
greg |
1.36 |
return 0; /* dummy ray */
|
| 879 |
greg |
1.1 |
return sizeof(double)*6;
|
| 880 |
|
|
}
|
| 881 |
|
|
error(INTERNAL, "botched input format");
|
| 882 |
greg |
1.36 |
return -1; /* pro forma return */
|
| 883 |
greg |
1.1 |
}
|
| 884 |
|
|
|
| 885 |
greg |
1.2 |
static float rparams[9]; /* traced ray parameters */
|
| 886 |
greg |
1.1 |
|
| 887 |
|
|
/* return channel (ray) value */
|
| 888 |
|
|
double
|
| 889 |
|
|
chanvalue(int n)
|
| 890 |
|
|
{
|
| 891 |
|
|
if (--n < 0 || n >= 6)
|
| 892 |
|
|
error(USER, "illegal channel number ($N)");
|
| 893 |
greg |
1.2 |
return rparams[n+3];
|
| 894 |
greg |
1.1 |
}
|
| 895 |
|
|
|
| 896 |
greg |
1.2 |
/* add current ray contribution to the appropriate modifier bin */
|
| 897 |
greg |
1.1 |
void
|
| 898 |
greg |
1.2 |
add_contrib(const char *modn)
|
| 899 |
greg |
1.1 |
{
|
| 900 |
|
|
LUENT *le = lu_find(&modconttab, modn);
|
| 901 |
|
|
MODCONT *mp = (MODCONT *)le->data;
|
| 902 |
|
|
int bn;
|
| 903 |
|
|
|
| 904 |
|
|
if (mp == NULL) {
|
| 905 |
|
|
sprintf(errmsg, "unexpected modifier '%s' from rtrace", modn);
|
| 906 |
|
|
error(USER, errmsg);
|
| 907 |
|
|
}
|
| 908 |
greg |
1.2 |
eclock++; /* get bin number */
|
| 909 |
greg |
1.1 |
bn = (int)(evalue(mp->binv) + .5);
|
| 910 |
|
|
if (bn <= 0)
|
| 911 |
|
|
bn = 0;
|
| 912 |
greg |
1.31 |
else if (bn >= mp->nbins) /* new bin */
|
| 913 |
|
|
le->data = (char *)(mp = growmodifier(mp, bn+1));
|
| 914 |
greg |
1.2 |
addcolor(mp->cbin[bn], rparams);
|
| 915 |
greg |
1.1 |
}
|
| 916 |
|
|
|
| 917 |
|
|
/* output newline to ASCII file and/or flush as requested */
|
| 918 |
|
|
static int
|
| 919 |
|
|
puteol(const LUENT *e, void *p)
|
| 920 |
|
|
{
|
| 921 |
greg |
1.31 |
STREAMOUT *sop = (STREAMOUT *)e->data;
|
| 922 |
greg |
1.1 |
|
| 923 |
|
|
if (outfmt == 'a')
|
| 924 |
greg |
1.31 |
putc('\n', sop->ofp);
|
| 925 |
greg |
1.1 |
if (!waitflush)
|
| 926 |
greg |
1.31 |
fflush(sop->ofp);
|
| 927 |
|
|
if (ferror(sop->ofp)) {
|
| 928 |
greg |
1.1 |
sprintf(errmsg, "write error on file '%s'", e->key);
|
| 929 |
|
|
error(SYSTEM, errmsg);
|
| 930 |
|
|
}
|
| 931 |
|
|
return 0;
|
| 932 |
|
|
}
|
| 933 |
|
|
|
| 934 |
greg |
1.2 |
/* put out ray contribution to file */
|
| 935 |
|
|
void
|
| 936 |
greg |
1.14 |
put_contrib(const DCOLOR cnt, FILE *fout)
|
| 937 |
greg |
1.2 |
{
|
| 938 |
|
|
float fv[3];
|
| 939 |
|
|
COLR cv;
|
| 940 |
|
|
|
| 941 |
|
|
switch (outfmt) {
|
| 942 |
|
|
case 'a':
|
| 943 |
|
|
fprintf(fout, "%.6e\t%.6e\t%.6e\t", cnt[0], cnt[1], cnt[2]);
|
| 944 |
|
|
break;
|
| 945 |
|
|
case 'f':
|
| 946 |
greg |
1.46 |
copycolor(fv, cnt);
|
| 947 |
greg |
1.2 |
fwrite(fv, sizeof(float), 3, fout);
|
| 948 |
|
|
break;
|
| 949 |
|
|
case 'd':
|
| 950 |
|
|
fwrite(cnt, sizeof(double), 3, fout);
|
| 951 |
|
|
break;
|
| 952 |
|
|
case 'c':
|
| 953 |
|
|
setcolr(cv, cnt[0], cnt[1], cnt[2]);
|
| 954 |
|
|
fwrite(cv, sizeof(cv), 1, fout);
|
| 955 |
|
|
break;
|
| 956 |
|
|
default:
|
| 957 |
|
|
error(INTERNAL, "botched output format");
|
| 958 |
|
|
}
|
| 959 |
|
|
}
|
| 960 |
|
|
|
| 961 |
greg |
1.45 |
/* output ray tallies and clear for next accumulation */
|
| 962 |
greg |
1.1 |
void
|
| 963 |
greg |
1.49 |
done_contrib(int navg)
|
| 964 |
greg |
1.1 |
{
|
| 965 |
greg |
1.49 |
double sf = 1.;
|
| 966 |
greg |
1.31 |
int i, j;
|
| 967 |
|
|
MODCONT *mp;
|
| 968 |
|
|
STREAMOUT *sop;
|
| 969 |
greg |
1.49 |
/* set average scaling */
|
| 970 |
|
|
if (navg > 1)
|
| 971 |
|
|
sf = 1. / (double)navg;
|
| 972 |
greg |
1.1 |
/* output modifiers in order */
|
| 973 |
|
|
for (i = 0; i < nmods; i++) {
|
| 974 |
|
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
|
| 975 |
greg |
1.49 |
if (navg > 1) /* average scaling */
|
| 976 |
|
|
for (j = mp->nbins; j--; )
|
| 977 |
|
|
scalecolor(mp->cbin[j], sf);
|
| 978 |
greg |
1.31 |
sop = getostream(mp->outspec, mp->modname, 0,0);
|
| 979 |
|
|
put_contrib(mp->cbin[0], sop->ofp);
|
| 980 |
greg |
1.12 |
if (mp->nbins > 3 && /* minor optimization */
|
| 981 |
greg |
1.31 |
sop == getostream(mp->outspec, mp->modname, 1,0))
|
| 982 |
greg |
1.12 |
for (j = 1; j < mp->nbins; j++)
|
| 983 |
greg |
1.31 |
put_contrib(mp->cbin[j], sop->ofp);
|
| 984 |
greg |
1.12 |
else
|
| 985 |
|
|
for (j = 1; j < mp->nbins; j++)
|
| 986 |
greg |
1.14 |
put_contrib(mp->cbin[j],
|
| 987 |
greg |
1.31 |
getostream(mp->outspec,mp->modname,j,0)->ofp);
|
| 988 |
greg |
1.45 |
/* clear for next time */
|
| 989 |
greg |
1.2 |
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
|
| 990 |
greg |
1.1 |
}
|
| 991 |
|
|
--waitflush; /* terminate records */
|
| 992 |
|
|
lu_doall(&ofiletab, puteol, NULL);
|
| 993 |
greg |
1.2 |
if (using_stdout & (outfmt == 'a'))
|
| 994 |
|
|
putc('\n', stdout);
|
| 995 |
|
|
if (!waitflush) {
|
| 996 |
greg |
1.57 |
waitflush = (yres > 0) & (xres > 1) ? 0 : xres;
|
| 997 |
greg |
1.2 |
if (using_stdout)
|
| 998 |
|
|
fflush(stdout);
|
| 999 |
|
|
}
|
| 1000 |
greg |
1.1 |
}
|
| 1001 |
|
|
|
| 1002 |
greg |
1.3 |
/* queue completed ray tree produced by rtrace process */
|
| 1003 |
|
|
void
|
| 1004 |
|
|
queue_raytree(struct rtproc *rtp)
|
| 1005 |
|
|
{
|
| 1006 |
|
|
struct rtproc *rtu, *rtl = NULL;
|
| 1007 |
|
|
/* insert following ray order */
|
| 1008 |
|
|
for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
|
| 1009 |
|
|
if (rtp->raynum < rtu->raynum)
|
| 1010 |
|
|
break;
|
| 1011 |
|
|
rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
|
| 1012 |
|
|
if (rtu == NULL)
|
| 1013 |
|
|
error(SYSTEM, "out of memory in queue_raytree");
|
| 1014 |
|
|
*rtu = *rtp;
|
| 1015 |
|
|
if (rtl == NULL) {
|
| 1016 |
|
|
rtu->next = rt_unproc;
|
| 1017 |
|
|
rt_unproc = rtu;
|
| 1018 |
|
|
} else {
|
| 1019 |
|
|
rtu->next = rtl->next;
|
| 1020 |
|
|
rtl->next = rtu;
|
| 1021 |
|
|
}
|
| 1022 |
|
|
rtp->raynum = 0; /* clear path for next ray tree */
|
| 1023 |
|
|
rtp->bsiz = 0;
|
| 1024 |
|
|
rtp->buf = NULL;
|
| 1025 |
|
|
rtp->nbr = 0;
|
| 1026 |
|
|
}
|
| 1027 |
|
|
|
| 1028 |
|
|
/* process completed ray trees from our queue */
|
| 1029 |
greg |
1.1 |
void
|
| 1030 |
greg |
1.3 |
process_queue(void)
|
| 1031 |
greg |
1.1 |
{
|
| 1032 |
greg |
1.3 |
char modname[128];
|
| 1033 |
|
|
/* ray-ordered queue */
|
| 1034 |
|
|
while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
|
| 1035 |
|
|
struct rtproc *rtp = rt_unproc;
|
| 1036 |
greg |
1.2 |
int n = rtp->nbr;
|
| 1037 |
greg |
1.1 |
const char *cp = rtp->buf;
|
| 1038 |
|
|
while (n > 0) { /* process rays */
|
| 1039 |
greg |
1.2 |
register char *mnp = modname;
|
| 1040 |
greg |
1.1 |
/* skip leading tabs */
|
| 1041 |
|
|
while (n > 0 && *cp == '\t') {
|
| 1042 |
|
|
cp++; n--;
|
| 1043 |
|
|
}
|
| 1044 |
greg |
1.2 |
if (!n || !(isalpha(*cp) | (*cp == '_')))
|
| 1045 |
|
|
error(USER, "bad modifier name from rtrace");
|
| 1046 |
|
|
/* get modifier name */
|
| 1047 |
greg |
1.40 |
while (n > 1 && *cp != '\t') {
|
| 1048 |
|
|
if (mnp - modname >= sizeof(modname)-2)
|
| 1049 |
|
|
error(INTERNAL, "modifier name too long");
|
| 1050 |
greg |
1.1 |
*mnp++ = *cp++; n--;
|
| 1051 |
|
|
}
|
| 1052 |
|
|
*mnp = '\0';
|
| 1053 |
greg |
1.2 |
cp++; n--; /* eat following tab */
|
| 1054 |
greg |
1.1 |
if (n < (int)(sizeof(float)*9))
|
| 1055 |
|
|
error(USER, "incomplete ray value from rtrace");
|
| 1056 |
|
|
/* add ray contribution */
|
| 1057 |
greg |
1.2 |
memcpy(rparams, cp, sizeof(float)*9);
|
| 1058 |
greg |
1.1 |
cp += sizeof(float)*9; n -= sizeof(float)*9;
|
| 1059 |
greg |
1.2 |
add_contrib(modname);
|
| 1060 |
greg |
1.1 |
}
|
| 1061 |
greg |
1.49 |
/* time to produce record? */
|
| 1062 |
|
|
if (account > 0 && !--account)
|
| 1063 |
|
|
done_contrib(account = accumulate);
|
| 1064 |
greg |
1.1 |
lastdone = rtp->raynum;
|
| 1065 |
greg |
1.36 |
if (rtp->buf != NULL) /* free up buffer space */
|
| 1066 |
|
|
free(rtp->buf);
|
| 1067 |
greg |
1.3 |
rt_unproc = rtp->next;
|
| 1068 |
|
|
free(rtp); /* done with this ray tree */
|
| 1069 |
greg |
1.1 |
}
|
| 1070 |
|
|
}
|
| 1071 |
|
|
|
| 1072 |
|
|
/* wait for rtrace process to finish with ray tree */
|
| 1073 |
|
|
struct rtproc *
|
| 1074 |
|
|
wait_rproc(void)
|
| 1075 |
|
|
{
|
| 1076 |
|
|
struct rtproc *rtfree = NULL;
|
| 1077 |
|
|
fd_set readset, errset;
|
| 1078 |
greg |
1.58 |
ssize_t nr;
|
| 1079 |
greg |
1.1 |
struct rtproc *rt;
|
| 1080 |
|
|
int n;
|
| 1081 |
|
|
|
| 1082 |
|
|
do {
|
| 1083 |
|
|
nr = 0; /* prepare select call */
|
| 1084 |
|
|
FD_ZERO(&readset); FD_ZERO(&errset); n = 0;
|
| 1085 |
|
|
for (rt = &rt0; rt != NULL; rt = rt->next) {
|
| 1086 |
|
|
if (rt->raynum) {
|
| 1087 |
|
|
FD_SET(rt->pd.r, &readset);
|
| 1088 |
|
|
++nr;
|
| 1089 |
|
|
}
|
| 1090 |
|
|
FD_SET(rt->pd.r, &errset);
|
| 1091 |
|
|
if (rt->pd.r >= n)
|
| 1092 |
|
|
n = rt->pd.r + 1;
|
| 1093 |
|
|
}
|
| 1094 |
|
|
if (!nr) /* no rays pending */
|
| 1095 |
|
|
break;
|
| 1096 |
|
|
if (nr > 1) { /* call select for multiple proc's */
|
| 1097 |
|
|
errno = 0;
|
| 1098 |
|
|
if (select(n, &readset, NULL, &errset, NULL) < 0)
|
| 1099 |
|
|
error(SYSTEM, "select call error in wait_rproc()");
|
| 1100 |
|
|
} else
|
| 1101 |
|
|
FD_ZERO(&errset);
|
| 1102 |
|
|
nr = 0;
|
| 1103 |
|
|
for (rt = &rt0; rt != NULL; rt = rt->next) {
|
| 1104 |
|
|
if (!FD_ISSET(rt->pd.r, &readset) &&
|
| 1105 |
|
|
!FD_ISSET(rt->pd.r, &errset))
|
| 1106 |
|
|
continue;
|
| 1107 |
|
|
if (rt->buf == NULL) {
|
| 1108 |
|
|
rt->bsiz = treebufsiz;
|
| 1109 |
|
|
rt->buf = (char *)malloc(treebufsiz);
|
| 1110 |
greg |
1.2 |
} else if (rt->nbr + BUFSIZ > rt->bsiz) {
|
| 1111 |
greg |
1.1 |
if (rt->bsiz + BUFSIZ <= treebufsiz)
|
| 1112 |
|
|
rt->bsiz = treebufsiz;
|
| 1113 |
|
|
else
|
| 1114 |
greg |
1.28 |
treebufsiz = rt->bsiz += BUFSIZ;
|
| 1115 |
greg |
1.1 |
rt->buf = (char *)realloc(rt->buf, rt->bsiz);
|
| 1116 |
|
|
}
|
| 1117 |
|
|
if (rt->buf == NULL)
|
| 1118 |
|
|
error(SYSTEM, "out of memory in wait_rproc");
|
| 1119 |
greg |
1.2 |
nr = read(rt->pd.r, rt->buf+rt->nbr, rt->bsiz-rt->nbr);
|
| 1120 |
|
|
if (nr <= 0)
|
| 1121 |
greg |
1.1 |
error(USER, "rtrace process died");
|
| 1122 |
greg |
1.2 |
rt->nbr += nr; /* advance & check */
|
| 1123 |
greg |
1.61 |
if (rt->nbr >= 8 && !memcmp(rt->buf+rt->nbr-8,
|
| 1124 |
|
|
"~\t~\t~\t~\t", 8)) {
|
| 1125 |
|
|
rt->nbr -= 8; /* elide terminator */
|
| 1126 |
greg |
1.3 |
queue_raytree(rt);
|
| 1127 |
greg |
1.1 |
rtfree = rt; /* ready for next ray */
|
| 1128 |
|
|
}
|
| 1129 |
|
|
}
|
| 1130 |
|
|
} while ((rtfree == NULL) & (nr > 0)); /* repeat until ready or out */
|
| 1131 |
|
|
return rtfree;
|
| 1132 |
|
|
}
|
| 1133 |
|
|
|
| 1134 |
|
|
/* return next available rtrace process */
|
| 1135 |
|
|
struct rtproc *
|
| 1136 |
|
|
get_rproc(void)
|
| 1137 |
|
|
{
|
| 1138 |
|
|
struct rtproc *rtp;
|
| 1139 |
|
|
/* check for idle rtrace */
|
| 1140 |
|
|
for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
|
| 1141 |
|
|
if (!rtp->raynum)
|
| 1142 |
|
|
return rtp;
|
| 1143 |
|
|
return wait_rproc(); /* need to wait for one */
|
| 1144 |
|
|
}
|
| 1145 |
|
|
|
| 1146 |
|
|
/* trace ray contributions (main loop) */
|
| 1147 |
|
|
void
|
| 1148 |
greg |
1.5 |
trace_contribs(FILE *fin)
|
| 1149 |
greg |
1.1 |
{
|
| 1150 |
greg |
1.49 |
static int ignore_warning_given = 0;
|
| 1151 |
greg |
1.1 |
char inpbuf[128];
|
| 1152 |
|
|
int iblen;
|
| 1153 |
|
|
struct rtproc *rtp;
|
| 1154 |
|
|
/* loop over input */
|
| 1155 |
greg |
1.36 |
while ((iblen = getinp(inpbuf, fin)) >= 0) {
|
| 1156 |
greg |
1.49 |
if (!iblen && accumulate != 1) {
|
| 1157 |
|
|
if (!ignore_warning_given++)
|
| 1158 |
|
|
error(WARNING,
|
| 1159 |
|
|
"dummy ray(s) ignored during accumulation\n");
|
| 1160 |
|
|
continue;
|
| 1161 |
|
|
}
|
| 1162 |
greg |
1.53 |
if (!iblen || /* need flush/reset? */
|
| 1163 |
greg |
1.36 |
queue_length() > 10*nrtprocs() ||
|
| 1164 |
|
|
lastray+1 < lastray) {
|
| 1165 |
greg |
1.1 |
while (wait_rproc() != NULL)
|
| 1166 |
greg |
1.3 |
process_queue();
|
| 1167 |
greg |
1.34 |
if (lastray+1 < lastray)
|
| 1168 |
|
|
lastdone = lastray = 0;
|
| 1169 |
greg |
1.1 |
}
|
| 1170 |
|
|
rtp = get_rproc(); /* get avail. rtrace process */
|
| 1171 |
greg |
1.36 |
rtp->raynum = ++lastray; /* assign ray */
|
| 1172 |
|
|
if (iblen) { /* trace ray if valid */
|
| 1173 |
|
|
writebuf(rtp->pd.w, inpbuf, iblen);
|
| 1174 |
|
|
} else { /* else bypass dummy ray */
|
| 1175 |
greg |
1.53 |
queue_raytree(rtp); /* queue empty ray/record */
|
| 1176 |
|
|
if ((yres <= 0) | (xres <= 0))
|
| 1177 |
|
|
waitflush = 1; /* flush right after */
|
| 1178 |
greg |
1.36 |
}
|
| 1179 |
greg |
1.38 |
process_queue(); /* catch up with results */
|
| 1180 |
greg |
1.14 |
if (raysleft && !--raysleft)
|
| 1181 |
greg |
1.38 |
break; /* preemptive EOI */
|
| 1182 |
greg |
1.1 |
}
|
| 1183 |
|
|
while (wait_rproc() != NULL) /* process outstanding rays */
|
| 1184 |
greg |
1.3 |
process_queue();
|
| 1185 |
greg |
1.49 |
if (accumulate <= 0)
|
| 1186 |
|
|
done_contrib(0); /* output tallies */
|
| 1187 |
|
|
else if (account < accumulate) {
|
| 1188 |
|
|
error(WARNING, "partial accumulation in final record");
|
| 1189 |
|
|
done_contrib(accumulate - account);
|
| 1190 |
|
|
}
|
| 1191 |
greg |
1.14 |
if (raysleft)
|
| 1192 |
greg |
1.2 |
error(USER, "unexpected EOF on input");
|
| 1193 |
greg |
1.14 |
lu_done(&ofiletab); /* close output files */
|
| 1194 |
|
|
}
|
| 1195 |
|
|
|
| 1196 |
greg |
1.46 |
/* get ray contribution from previous file */
|
| 1197 |
|
|
static int
|
| 1198 |
|
|
get_contrib(DCOLOR cnt, FILE *finp)
|
| 1199 |
|
|
{
|
| 1200 |
|
|
COLOR fv;
|
| 1201 |
|
|
COLR cv;
|
| 1202 |
|
|
|
| 1203 |
|
|
switch (outfmt) {
|
| 1204 |
|
|
case 'a':
|
| 1205 |
|
|
return(fscanf(finp,"%lf %lf %lf",&cnt[0],&cnt[1],&cnt[2]) == 3);
|
| 1206 |
|
|
case 'f':
|
| 1207 |
|
|
if (fread(fv, sizeof(fv[0]), 3, finp) != 3)
|
| 1208 |
|
|
return(0);
|
| 1209 |
|
|
copycolor(cnt, fv);
|
| 1210 |
|
|
return(1);
|
| 1211 |
|
|
case 'd':
|
| 1212 |
|
|
return(fread(cnt, sizeof(cnt[0]), 3, finp) == 3);
|
| 1213 |
|
|
case 'c':
|
| 1214 |
|
|
if (fread(cv, sizeof(cv), 1, finp) != 1)
|
| 1215 |
|
|
return(0);
|
| 1216 |
|
|
colr_color(fv, cv);
|
| 1217 |
|
|
copycolor(cnt, fv);
|
| 1218 |
|
|
return(1);
|
| 1219 |
|
|
default:
|
| 1220 |
|
|
error(INTERNAL, "botched output format");
|
| 1221 |
|
|
}
|
| 1222 |
|
|
return(0); /* pro forma return */
|
| 1223 |
|
|
}
|
| 1224 |
|
|
|
| 1225 |
|
|
/* close output file opened for input */
|
| 1226 |
greg |
1.14 |
static int
|
| 1227 |
greg |
1.46 |
myclose(const LUENT *e, void *p)
|
| 1228 |
greg |
1.14 |
{
|
| 1229 |
greg |
1.31 |
STREAMOUT *sop = (STREAMOUT *)e->data;
|
| 1230 |
|
|
|
| 1231 |
greg |
1.46 |
if (sop->ofp == NULL)
|
| 1232 |
greg |
1.50 |
return(0);
|
| 1233 |
greg |
1.46 |
fclose(sop->ofp);
|
| 1234 |
|
|
sop->ofp = NULL;
|
| 1235 |
greg |
1.50 |
return(0);
|
| 1236 |
greg |
1.14 |
}
|
| 1237 |
|
|
|
| 1238 |
greg |
1.45 |
/* load previously accumulated values */
|
| 1239 |
|
|
void
|
| 1240 |
|
|
reload_output(void)
|
| 1241 |
|
|
{
|
| 1242 |
|
|
int i, j;
|
| 1243 |
|
|
MODCONT *mp;
|
| 1244 |
|
|
int ofl;
|
| 1245 |
|
|
char oname[1024];
|
| 1246 |
|
|
char *fmode = "rb";
|
| 1247 |
|
|
char *outvfmt;
|
| 1248 |
|
|
LUENT *ment, *oent;
|
| 1249 |
greg |
1.46 |
int xr, yr;
|
| 1250 |
greg |
1.45 |
STREAMOUT sout;
|
| 1251 |
greg |
1.46 |
DCOLOR rgbv;
|
| 1252 |
greg |
1.45 |
|
| 1253 |
|
|
switch (outfmt) {
|
| 1254 |
|
|
case 'a':
|
| 1255 |
|
|
outvfmt = "ascii";
|
| 1256 |
|
|
fmode = "r";
|
| 1257 |
|
|
break;
|
| 1258 |
|
|
case 'f':
|
| 1259 |
|
|
outvfmt = "float";
|
| 1260 |
|
|
break;
|
| 1261 |
|
|
case 'd':
|
| 1262 |
|
|
outvfmt = "double";
|
| 1263 |
|
|
break;
|
| 1264 |
|
|
case 'c':
|
| 1265 |
|
|
outvfmt = COLRFMT;
|
| 1266 |
|
|
break;
|
| 1267 |
|
|
default:
|
| 1268 |
|
|
error(INTERNAL, "botched output format");
|
| 1269 |
|
|
return;
|
| 1270 |
|
|
}
|
| 1271 |
greg |
1.46 |
/* reload modifier values */
|
| 1272 |
greg |
1.45 |
for (i = 0; i < nmods; i++) {
|
| 1273 |
|
|
ment = lu_find(&modconttab,modname[i]);
|
| 1274 |
|
|
mp = (MODCONT *)ment->data;
|
| 1275 |
|
|
if (mp->outspec == NULL)
|
| 1276 |
|
|
error(USER, "cannot reload from stdout");
|
| 1277 |
|
|
if (mp->outspec[0] == '!')
|
| 1278 |
|
|
error(USER, "cannot reload from command");
|
| 1279 |
greg |
1.46 |
for (j = 0; ; j++) { /* load each modifier bin */
|
| 1280 |
greg |
1.45 |
ofl = ofname(oname, mp->outspec, mp->modname, j);
|
| 1281 |
|
|
if (ofl < 0)
|
| 1282 |
|
|
error(USER, "bad output file specification");
|
| 1283 |
|
|
oent = lu_find(&ofiletab, oname);
|
| 1284 |
|
|
if (oent->data != NULL) {
|
| 1285 |
|
|
sout = *(STREAMOUT *)oent->data;
|
| 1286 |
|
|
} else {
|
| 1287 |
|
|
sout.reclen = 0;
|
| 1288 |
greg |
1.46 |
sout.outpipe = 0;
|
| 1289 |
|
|
sout.xr = xres; sout.yr = yres;
|
| 1290 |
greg |
1.45 |
sout.ofp = NULL;
|
| 1291 |
|
|
}
|
| 1292 |
greg |
1.46 |
if (sout.ofp == NULL) { /* open output as input */
|
| 1293 |
|
|
sout.ofp = fopen(oname, fmode);
|
| 1294 |
|
|
if (sout.ofp == NULL) {
|
| 1295 |
|
|
if (j)
|
| 1296 |
|
|
break; /* assume end of modifier */
|
| 1297 |
|
|
sprintf(errmsg, "missing reload file '%s'",
|
| 1298 |
|
|
oname);
|
| 1299 |
|
|
error(WARNING, errmsg);
|
| 1300 |
|
|
break;
|
| 1301 |
|
|
}
|
| 1302 |
|
|
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
|
| 1303 |
|
|
sprintf(errmsg, "format mismatch for '%s'",
|
| 1304 |
|
|
oname);
|
| 1305 |
|
|
error(USER, errmsg);
|
| 1306 |
|
|
}
|
| 1307 |
|
|
if ((sout.xr > 0) & (sout.yr > 0) &&
|
| 1308 |
|
|
(!fscnresolu(&xr, &yr, sout.ofp) ||
|
| 1309 |
greg |
1.47 |
(xr != sout.xr) |
|
| 1310 |
|
|
(yr != sout.yr))) {
|
| 1311 |
greg |
1.46 |
sprintf(errmsg, "resolution mismatch for '%s'",
|
| 1312 |
|
|
oname);
|
| 1313 |
|
|
error(USER, errmsg);
|
| 1314 |
|
|
}
|
| 1315 |
greg |
1.45 |
}
|
| 1316 |
greg |
1.46 |
/* read in RGB value */
|
| 1317 |
|
|
if (!get_contrib(rgbv, sout.ofp)) {
|
| 1318 |
greg |
1.48 |
if (!j) {
|
| 1319 |
|
|
fclose(sout.ofp);
|
| 1320 |
greg |
1.46 |
break; /* ignore empty file */
|
| 1321 |
greg |
1.48 |
}
|
| 1322 |
greg |
1.47 |
if (j < mp->nbins) {
|
| 1323 |
greg |
1.46 |
sprintf(errmsg, "missing data in '%s'",
|
| 1324 |
|
|
oname);
|
| 1325 |
|
|
error(USER, errmsg);
|
| 1326 |
|
|
}
|
| 1327 |
|
|
break;
|
| 1328 |
|
|
}
|
| 1329 |
|
|
if (j >= mp->nbins) /* grow modifier size */
|
| 1330 |
|
|
ment->data = (char *)(mp = growmodifier(mp, j+1));
|
| 1331 |
|
|
copycolor(mp->cbin[j], rgbv);
|
| 1332 |
greg |
1.48 |
if (oent->key == NULL) /* new file entry */
|
| 1333 |
|
|
oent->key = strcpy((char *)
|
| 1334 |
|
|
malloc(strlen(oname)+1), oname);
|
| 1335 |
|
|
if (oent->data == NULL)
|
| 1336 |
|
|
oent->data = (char *)malloc(sizeof(STREAMOUT));
|
| 1337 |
|
|
*(STREAMOUT *)oent->data = sout;
|
| 1338 |
greg |
1.45 |
}
|
| 1339 |
|
|
}
|
| 1340 |
greg |
1.46 |
lu_doall(&ofiletab, myclose, NULL); /* close all files */
|
| 1341 |
|
|
}
|
| 1342 |
|
|
|
| 1343 |
|
|
/* seek on the given output file */
|
| 1344 |
|
|
static int
|
| 1345 |
|
|
myseeko(const LUENT *e, void *p)
|
| 1346 |
|
|
{
|
| 1347 |
|
|
STREAMOUT *sop = (STREAMOUT *)e->data;
|
| 1348 |
|
|
off_t nbytes = *(off_t *)p;
|
| 1349 |
|
|
|
| 1350 |
|
|
if (sop->reclen > 1)
|
| 1351 |
|
|
nbytes = nbytes * sop->reclen;
|
| 1352 |
|
|
if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) {
|
| 1353 |
|
|
sprintf(errmsg, "seek error on file '%s'", e->key);
|
| 1354 |
|
|
error(SYSTEM, errmsg);
|
| 1355 |
|
|
}
|
| 1356 |
|
|
return 0;
|
| 1357 |
greg |
1.45 |
}
|
| 1358 |
|
|
|
| 1359 |
greg |
1.14 |
/* recover output if possible */
|
| 1360 |
|
|
void
|
| 1361 |
|
|
recover_output(FILE *fin)
|
| 1362 |
|
|
{
|
| 1363 |
greg |
1.31 |
off_t lastout = -1;
|
| 1364 |
|
|
int outvsiz, recsiz;
|
| 1365 |
|
|
char *outvfmt;
|
| 1366 |
|
|
int i, j;
|
| 1367 |
|
|
MODCONT *mp;
|
| 1368 |
|
|
int ofl;
|
| 1369 |
|
|
char oname[1024];
|
| 1370 |
|
|
LUENT *ment, *oent;
|
| 1371 |
|
|
STREAMOUT sout;
|
| 1372 |
|
|
off_t nvals;
|
| 1373 |
|
|
int xr, yr;
|
| 1374 |
greg |
1.14 |
|
| 1375 |
|
|
switch (outfmt) {
|
| 1376 |
|
|
case 'a':
|
| 1377 |
|
|
error(USER, "cannot recover ASCII output");
|
| 1378 |
|
|
return;
|
| 1379 |
|
|
case 'f':
|
| 1380 |
|
|
outvsiz = sizeof(float)*3;
|
| 1381 |
|
|
outvfmt = "float";
|
| 1382 |
|
|
break;
|
| 1383 |
|
|
case 'd':
|
| 1384 |
|
|
outvsiz = sizeof(double)*3;
|
| 1385 |
|
|
outvfmt = "double";
|
| 1386 |
|
|
break;
|
| 1387 |
|
|
case 'c':
|
| 1388 |
|
|
outvsiz = sizeof(COLR);
|
| 1389 |
|
|
outvfmt = COLRFMT;
|
| 1390 |
|
|
break;
|
| 1391 |
|
|
default:
|
| 1392 |
|
|
error(INTERNAL, "botched output format");
|
| 1393 |
|
|
return;
|
| 1394 |
|
|
}
|
| 1395 |
|
|
/* check modifier outputs */
|
| 1396 |
|
|
for (i = 0; i < nmods; i++) {
|
| 1397 |
|
|
ment = lu_find(&modconttab,modname[i]);
|
| 1398 |
|
|
mp = (MODCONT *)ment->data;
|
| 1399 |
|
|
if (mp->outspec == NULL)
|
| 1400 |
|
|
error(USER, "cannot recover from stdout");
|
| 1401 |
greg |
1.31 |
if (mp->outspec[0] == '!')
|
| 1402 |
|
|
error(USER, "cannot recover from command");
|
| 1403 |
greg |
1.14 |
for (j = 0; ; j++) { /* check each bin's file */
|
| 1404 |
|
|
ofl = ofname(oname, mp->outspec, mp->modname, j);
|
| 1405 |
|
|
if (ofl < 0)
|
| 1406 |
|
|
error(USER, "bad output file specification");
|
| 1407 |
|
|
oent = lu_find(&ofiletab, oname);
|
| 1408 |
greg |
1.31 |
if (oent->data != NULL) {
|
| 1409 |
|
|
sout = *(STREAMOUT *)oent->data;
|
| 1410 |
|
|
} else {
|
| 1411 |
greg |
1.45 |
sout.reclen = 0;
|
| 1412 |
greg |
1.46 |
sout.outpipe = 0;
|
| 1413 |
greg |
1.31 |
sout.ofp = NULL;
|
| 1414 |
|
|
}
|
| 1415 |
|
|
if (sout.ofp != NULL) { /* already open? */
|
| 1416 |
greg |
1.14 |
if (ofl & OF_BIN)
|
| 1417 |
|
|
continue;
|
| 1418 |
|
|
break;
|
| 1419 |
|
|
}
|
| 1420 |
|
|
/* open output */
|
| 1421 |
greg |
1.31 |
sout.ofp = fopen(oname, "rb+");
|
| 1422 |
|
|
if (sout.ofp == NULL) {
|
| 1423 |
greg |
1.23 |
if (j)
|
| 1424 |
|
|
break; /* assume end of modifier */
|
| 1425 |
|
|
sprintf(errmsg, "missing recover file '%s'",
|
| 1426 |
|
|
oname);
|
| 1427 |
greg |
1.45 |
error(WARNING, errmsg);
|
| 1428 |
|
|
break;
|
| 1429 |
greg |
1.23 |
}
|
| 1430 |
greg |
1.31 |
nvals = lseek(fileno(sout.ofp), 0, SEEK_END);
|
| 1431 |
greg |
1.14 |
if (nvals <= 0) {
|
| 1432 |
|
|
lastout = 0; /* empty output, quit here */
|
| 1433 |
greg |
1.31 |
fclose(sout.ofp);
|
| 1434 |
greg |
1.14 |
break;
|
| 1435 |
|
|
}
|
| 1436 |
greg |
1.45 |
if (!sout.reclen) {
|
| 1437 |
greg |
1.31 |
if (!(ofl & OF_BIN)) {
|
| 1438 |
greg |
1.14 |
sprintf(errmsg,
|
| 1439 |
greg |
1.32 |
"need -bn to recover file '%s'",
|
| 1440 |
greg |
1.14 |
oname);
|
| 1441 |
greg |
1.32 |
error(USER, errmsg);
|
| 1442 |
greg |
1.14 |
}
|
| 1443 |
greg |
1.31 |
recsiz = outvsiz;
|
| 1444 |
|
|
} else
|
| 1445 |
|
|
recsiz = outvsiz * sout.reclen;
|
| 1446 |
|
|
|
| 1447 |
|
|
lseek(fileno(sout.ofp), 0, SEEK_SET);
|
| 1448 |
|
|
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) {
|
| 1449 |
|
|
sprintf(errmsg, "format mismatch for '%s'",
|
| 1450 |
|
|
oname);
|
| 1451 |
|
|
error(USER, errmsg);
|
| 1452 |
|
|
}
|
| 1453 |
greg |
1.48 |
sout.xr = xres; sout.yr = yres;
|
| 1454 |
|
|
if ((sout.xr > 0) & (sout.yr > 0) &&
|
| 1455 |
greg |
1.31 |
(!fscnresolu(&xr, &yr, sout.ofp) ||
|
| 1456 |
greg |
1.48 |
(xr != sout.xr) |
|
| 1457 |
|
|
(yr != sout.yr))) {
|
| 1458 |
greg |
1.31 |
sprintf(errmsg, "resolution mismatch for '%s'",
|
| 1459 |
|
|
oname);
|
| 1460 |
|
|
error(USER, errmsg);
|
| 1461 |
greg |
1.14 |
}
|
| 1462 |
greg |
1.31 |
nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz;
|
| 1463 |
|
|
if ((lastout < 0) | (nvals < lastout))
|
| 1464 |
greg |
1.14 |
lastout = nvals;
|
| 1465 |
greg |
1.31 |
if (oent->key == NULL) /* new entry */
|
| 1466 |
greg |
1.14 |
oent->key = strcpy((char *)
|
| 1467 |
|
|
malloc(strlen(oname)+1), oname);
|
| 1468 |
greg |
1.31 |
if (oent->data == NULL)
|
| 1469 |
|
|
oent->data = (char *)malloc(sizeof(STREAMOUT));
|
| 1470 |
|
|
*(STREAMOUT *)oent->data = sout;
|
| 1471 |
greg |
1.14 |
if (!(ofl & OF_BIN))
|
| 1472 |
|
|
break; /* no bin separation */
|
| 1473 |
|
|
}
|
| 1474 |
|
|
if (!lastout) { /* empty output */
|
| 1475 |
greg |
1.16 |
error(WARNING, "no previous data to recover");
|
| 1476 |
greg |
1.14 |
lu_done(&ofiletab); /* reclose all outputs */
|
| 1477 |
|
|
return;
|
| 1478 |
|
|
}
|
| 1479 |
greg |
1.31 |
if (j > mp->nbins) /* reallocate modifier bins */
|
| 1480 |
|
|
ment->data = (char *)(mp = growmodifier(mp, j));
|
| 1481 |
greg |
1.14 |
}
|
| 1482 |
greg |
1.15 |
if (lastout < 0) {
|
| 1483 |
greg |
1.16 |
error(WARNING, "no output files to recover");
|
| 1484 |
greg |
1.15 |
return;
|
| 1485 |
|
|
}
|
| 1486 |
greg |
1.49 |
if (raysleft && lastout >= raysleft/accumulate) {
|
| 1487 |
greg |
1.24 |
error(WARNING, "output appears to be complete");
|
| 1488 |
|
|
/* XXX should read & discard input? */
|
| 1489 |
|
|
quit(0);
|
| 1490 |
|
|
}
|
| 1491 |
greg |
1.14 |
/* seek on all files */
|
| 1492 |
|
|
nvals = lastout * outvsiz;
|
| 1493 |
|
|
lu_doall(&ofiletab, myseeko, &nvals);
|
| 1494 |
greg |
1.28 |
/* skip repeated input */
|
| 1495 |
greg |
1.14 |
for (nvals = 0; nvals < lastout; nvals++)
|
| 1496 |
greg |
1.36 |
if (getinp(oname, fin) < 0)
|
| 1497 |
greg |
1.14 |
error(USER, "unexpected EOF on input");
|
| 1498 |
greg |
1.49 |
lastray = lastdone = (unsigned long)lastout * accumulate;
|
| 1499 |
greg |
1.14 |
if (raysleft)
|
| 1500 |
|
|
raysleft -= lastray;
|
| 1501 |
greg |
1.1 |
}
|