| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
greg |
1.3 |
static const char RCSid[] = "$Id$";
|
| 3 |
greg |
1.1 |
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* scanner.c - program to simulate bi-directional scanner.
|
| 6 |
|
|
*
|
| 7 |
|
|
* 6/10/86
|
| 8 |
|
|
*/
|
| 9 |
|
|
|
| 10 |
greg |
1.3 |
#include <stdio.h>
|
| 11 |
|
|
|
| 12 |
|
|
#include <stdlib.h>
|
| 13 |
greg |
1.1 |
|
| 14 |
greg |
1.3 |
#include <ctype.h>
|
| 15 |
greg |
1.1 |
|
| 16 |
greg |
1.3 |
#include <signal.h>
|
| 17 |
greg |
1.1 |
|
| 18 |
|
|
#include "random.h"
|
| 19 |
|
|
|
| 20 |
|
|
|
| 21 |
|
|
#define PI 3.14159265358979324
|
| 22 |
|
|
|
| 23 |
|
|
#define ANGLE short
|
| 24 |
|
|
#define AEND -1
|
| 25 |
|
|
|
| 26 |
|
|
#define atorad(a) ((PI/180.0) * (a))
|
| 27 |
|
|
|
| 28 |
|
|
char otcom[128] = "oconv";
|
| 29 |
|
|
char rtcom[256] = "rtrace -bf -ar .25 -ad 128";
|
| 30 |
|
|
|
| 31 |
|
|
char *sourcetemp, /* temp files */
|
| 32 |
|
|
*octreetemp;
|
| 33 |
|
|
|
| 34 |
|
|
ANGLE alpha[181] = {90, AEND};
|
| 35 |
|
|
ANGLE beta[181] = {30, 60, 90, 120, 150, AEND};
|
| 36 |
|
|
ANGLE gamma[181] = {45, AEND};
|
| 37 |
|
|
|
| 38 |
|
|
char *target; /* target file name */
|
| 39 |
|
|
double targetw = 3.0; /* target width (inches) */
|
| 40 |
|
|
double targeth = 3.0; /* target height (inches) */
|
| 41 |
|
|
int xres = 16; /* x sample resolution */
|
| 42 |
|
|
int yres = 16; /* y sample resolution */
|
| 43 |
|
|
|
| 44 |
|
|
|
| 45 |
|
|
main(argc, argv)
|
| 46 |
|
|
int argc;
|
| 47 |
|
|
char *argv[];
|
| 48 |
|
|
{
|
| 49 |
|
|
char *strcat(), *mktemp();
|
| 50 |
|
|
int quit();
|
| 51 |
|
|
int i;
|
| 52 |
|
|
|
| 53 |
|
|
signal(SIGHUP, quit);
|
| 54 |
|
|
signal(SIGINT, quit);
|
| 55 |
|
|
signal(SIGTERM, quit);
|
| 56 |
|
|
signal(SIGXCPU, SIG_IGN);
|
| 57 |
|
|
signal(SIGXFSZ, SIG_IGN);
|
| 58 |
|
|
|
| 59 |
|
|
for (i = 1; i < argc && argv[i][0] == '-'; i++)
|
| 60 |
|
|
switch (argv[i][1]) {
|
| 61 |
|
|
case 't': /* target */
|
| 62 |
|
|
switch (argv[i][2]) {
|
| 63 |
|
|
case 'w': /* width */
|
| 64 |
|
|
targetw = atof(argv[++i]);
|
| 65 |
|
|
break;
|
| 66 |
|
|
case 'h': /* height */
|
| 67 |
|
|
targeth = atof(argv[++i]);
|
| 68 |
|
|
break;
|
| 69 |
|
|
default:
|
| 70 |
|
|
goto badopt;
|
| 71 |
|
|
}
|
| 72 |
|
|
break;
|
| 73 |
|
|
case 'a':
|
| 74 |
|
|
if (!strcmp(argv[i], "-alpha"))
|
| 75 |
|
|
setscan(alpha, argv[++i]);
|
| 76 |
|
|
else
|
| 77 |
|
|
switch (argv[i][2]) {
|
| 78 |
|
|
case 'd': /* ambient divisions */
|
| 79 |
|
|
strcat(rtcom, " -ad ");
|
| 80 |
|
|
strcat(rtcom, argv[++i]);
|
| 81 |
|
|
break;
|
| 82 |
|
|
case 'r': /* ambient radius */
|
| 83 |
|
|
strcat(rtcom, " -ar ");
|
| 84 |
|
|
strcat(rtcom, argv[++i]);
|
| 85 |
|
|
break;
|
| 86 |
|
|
default:
|
| 87 |
|
|
goto badopt;
|
| 88 |
|
|
}
|
| 89 |
|
|
break;
|
| 90 |
|
|
case 'b':
|
| 91 |
|
|
if (!strcmp(argv[i], "-beta"))
|
| 92 |
|
|
setscan(beta, argv[++i]);
|
| 93 |
|
|
else
|
| 94 |
|
|
goto badopt;
|
| 95 |
|
|
break;
|
| 96 |
|
|
case 'g':
|
| 97 |
|
|
if (!strcmp(argv[i], "-gamma"))
|
| 98 |
|
|
setscan(gamma, argv[++i]);
|
| 99 |
|
|
else
|
| 100 |
|
|
goto badopt;
|
| 101 |
|
|
break;
|
| 102 |
|
|
case 'x': /* x resolution */
|
| 103 |
|
|
xres = atoi(argv[++i]);
|
| 104 |
|
|
break;
|
| 105 |
|
|
case 'y': /* y resolution */
|
| 106 |
|
|
yres = atoi(argv[++i]);
|
| 107 |
|
|
break;
|
| 108 |
|
|
case 'l':
|
| 109 |
|
|
switch (argv[i][2]) {
|
| 110 |
|
|
case 'w': /* limit weight */
|
| 111 |
|
|
strcat(rtcom, " -lw ");
|
| 112 |
|
|
strcat(rtcom, argv[++i]);
|
| 113 |
|
|
break;
|
| 114 |
|
|
case 'r': /* limit recursion */
|
| 115 |
|
|
strcat(rtcom, " -lr ");
|
| 116 |
|
|
strcat(rtcom, argv[++i]);
|
| 117 |
|
|
break;
|
| 118 |
|
|
default:
|
| 119 |
|
|
goto badopt;
|
| 120 |
|
|
}
|
| 121 |
|
|
break;
|
| 122 |
|
|
default:;
|
| 123 |
|
|
badopt:
|
| 124 |
|
|
fprintf(stderr, "%s: bad option: %s\n",
|
| 125 |
|
|
argv[0], argv[i]);
|
| 126 |
|
|
quit(1);
|
| 127 |
|
|
}
|
| 128 |
|
|
|
| 129 |
|
|
if (i >= argc) {
|
| 130 |
|
|
fprintf(stderr, "%s: missing target file\n", argv[0]);
|
| 131 |
|
|
quit(1);
|
| 132 |
|
|
} else if (i < argc-1) {
|
| 133 |
|
|
fprintf(stderr, "%s: too many target files\n", argv[0]);
|
| 134 |
|
|
quit(1);
|
| 135 |
|
|
}
|
| 136 |
|
|
|
| 137 |
|
|
target = argv[i];
|
| 138 |
|
|
sourcetemp = mktemp("/usr/tmp/soXXXXXX");
|
| 139 |
|
|
octreetemp = mktemp("/usr/tmp/ocXXXXXX");
|
| 140 |
|
|
strcat(strcat(otcom, " "), target);
|
| 141 |
|
|
strcat(strcat(otcom, " "), sourcetemp);
|
| 142 |
|
|
strcat(strcat(otcom, " > "), octreetemp);
|
| 143 |
|
|
strcat(strcat(rtcom, " "), octreetemp);
|
| 144 |
|
|
|
| 145 |
|
|
doscan();
|
| 146 |
|
|
|
| 147 |
|
|
quit(0);
|
| 148 |
|
|
}
|
| 149 |
|
|
|
| 150 |
|
|
|
| 151 |
greg |
1.3 |
void
|
| 152 |
greg |
1.1 |
quit(code) /* unlink temp files and exit */
|
| 153 |
|
|
int code;
|
| 154 |
|
|
{
|
| 155 |
|
|
int i;
|
| 156 |
|
|
|
| 157 |
|
|
unlink(sourcetemp);
|
| 158 |
|
|
unlink(octreetemp);
|
| 159 |
|
|
exit(code);
|
| 160 |
|
|
}
|
| 161 |
|
|
|
| 162 |
|
|
|
| 163 |
|
|
setscan(ang, arg) /* set up scan according to arg */
|
| 164 |
|
|
register ANGLE *ang;
|
| 165 |
|
|
register char *arg;
|
| 166 |
|
|
{
|
| 167 |
|
|
int start = 0, finish = -1, step = 1;
|
| 168 |
|
|
|
| 169 |
|
|
for ( ; ; ) {
|
| 170 |
|
|
switch (*arg) {
|
| 171 |
|
|
case '\0':
|
| 172 |
|
|
case ',':
|
| 173 |
|
|
while (start <= finish) {
|
| 174 |
|
|
*ang++ = start;
|
| 175 |
|
|
start += step;
|
| 176 |
|
|
}
|
| 177 |
|
|
if (*arg) {
|
| 178 |
|
|
arg++;
|
| 179 |
|
|
continue;
|
| 180 |
|
|
} else {
|
| 181 |
|
|
*ang = AEND;
|
| 182 |
|
|
return;
|
| 183 |
|
|
}
|
| 184 |
|
|
case '0':
|
| 185 |
|
|
case '1':
|
| 186 |
|
|
case '2':
|
| 187 |
|
|
case '3':
|
| 188 |
|
|
case '4':
|
| 189 |
|
|
case '5':
|
| 190 |
|
|
case '6':
|
| 191 |
|
|
case '7':
|
| 192 |
|
|
case '8':
|
| 193 |
|
|
case '9':
|
| 194 |
|
|
start = atoi(arg);
|
| 195 |
|
|
finish = start;
|
| 196 |
|
|
step = 1;
|
| 197 |
|
|
break;
|
| 198 |
|
|
case '-':
|
| 199 |
|
|
finish = atoi(++arg);
|
| 200 |
|
|
break;
|
| 201 |
|
|
case ':':
|
| 202 |
|
|
step = atoi(++arg);
|
| 203 |
|
|
break;
|
| 204 |
|
|
default:
|
| 205 |
|
|
fprintf(stderr, "Scan specification error\n");
|
| 206 |
|
|
quit(1);
|
| 207 |
|
|
}
|
| 208 |
|
|
while (isdigit(*arg))
|
| 209 |
|
|
arg++;
|
| 210 |
|
|
}
|
| 211 |
|
|
}
|
| 212 |
|
|
|
| 213 |
|
|
|
| 214 |
|
|
doscan() /* do scan for target */
|
| 215 |
|
|
{
|
| 216 |
|
|
FILE *fopen(), *scanstart();
|
| 217 |
|
|
double readsample();
|
| 218 |
|
|
FILE *fp;
|
| 219 |
|
|
ANGLE *a, *b, *g;
|
| 220 |
|
|
|
| 221 |
|
|
printf("Alpha\tBeta\tGamma\tDistribution for \"%s\"\n", target);
|
| 222 |
|
|
for (g = gamma; *g != AEND; g++) {
|
| 223 |
|
|
fp = scanstart(*g);
|
| 224 |
|
|
for (b = beta; *b != AEND; b++) /* read data */
|
| 225 |
|
|
for (a = alpha; *a != AEND; a++)
|
| 226 |
|
|
printf("%d\t%d\t%d\t%f\n",
|
| 227 |
|
|
*a, *b, *g, readsample(fp));
|
| 228 |
|
|
scanend(fp);
|
| 229 |
|
|
}
|
| 230 |
|
|
}
|
| 231 |
|
|
|
| 232 |
|
|
|
| 233 |
|
|
FILE *
|
| 234 |
|
|
scanstart(g) /* open scanner pipeline */
|
| 235 |
|
|
ANGLE g;
|
| 236 |
|
|
{
|
| 237 |
|
|
char *fgets();
|
| 238 |
|
|
int p1[2], p2[2];
|
| 239 |
|
|
int pid;
|
| 240 |
|
|
FILE *fp;
|
| 241 |
|
|
char buf[128];
|
| 242 |
|
|
|
| 243 |
|
|
makesource(g); /* make source */
|
| 244 |
|
|
if (system(otcom)) /* generate new octree */
|
| 245 |
|
|
goto err;
|
| 246 |
|
|
|
| 247 |
|
|
if (pipe(p1) < 0) /* get pipe1 */
|
| 248 |
|
|
goto err;
|
| 249 |
|
|
if ((pid = fork()) == 0) { /* if child1 */
|
| 250 |
|
|
close(p1[0]);
|
| 251 |
|
|
if (p1[1] != 1) { /* connect stdout to pipe1 */
|
| 252 |
|
|
dup2(p1[1], 1);
|
| 253 |
|
|
close(p1[1]);
|
| 254 |
|
|
}
|
| 255 |
|
|
if (pipe(p2) < 0) /* get pipe2 */
|
| 256 |
|
|
goto err;
|
| 257 |
|
|
if ((pid = fork()) == 0) { /* if child2 */
|
| 258 |
|
|
close(p2[0]);
|
| 259 |
|
|
if ((fp = fdopen(p2[1], "w")) == NULL)
|
| 260 |
|
|
goto err;
|
| 261 |
|
|
sendsamples(fp); /* send our samples to pipe2 */
|
| 262 |
|
|
fclose(fp);
|
| 263 |
|
|
_exit(0);
|
| 264 |
|
|
}
|
| 265 |
|
|
if (pid == -1)
|
| 266 |
|
|
goto err;
|
| 267 |
|
|
close(p2[1]);
|
| 268 |
|
|
|
| 269 |
|
|
if (p2[0] != 0) { /* for child1 pipe2 to stdin */
|
| 270 |
|
|
dup2(p2[0], 0);
|
| 271 |
|
|
close(p2[0]);
|
| 272 |
|
|
}
|
| 273 |
|
|
execl("/bin/sh", "sh", "-c", rtcom, 0); /* run ray tracer */
|
| 274 |
|
|
goto err;
|
| 275 |
|
|
}
|
| 276 |
|
|
if (pid == -1)
|
| 277 |
|
|
goto err;
|
| 278 |
|
|
close(p1[1]);
|
| 279 |
|
|
if ((fp = fdopen(p1[0], "r")) == NULL) /* parent reads pipe1 */
|
| 280 |
|
|
goto err;
|
| 281 |
|
|
while (fgets(buf, sizeof(buf), fp) != NULL && buf[0] != '\n')
|
| 282 |
|
|
/* discard header */;
|
| 283 |
|
|
return(fp);
|
| 284 |
|
|
err:
|
| 285 |
|
|
fprintf(stderr, "System error in scanstart\n");
|
| 286 |
|
|
quit(1);
|
| 287 |
|
|
}
|
| 288 |
|
|
|
| 289 |
|
|
|
| 290 |
|
|
scanend(fp) /* done with scanner input */
|
| 291 |
|
|
FILE *fp;
|
| 292 |
|
|
{
|
| 293 |
|
|
fclose(fp);
|
| 294 |
|
|
}
|
| 295 |
|
|
|
| 296 |
|
|
|
| 297 |
|
|
makesource(g) /* make a source (output normalized) */
|
| 298 |
|
|
ANGLE g;
|
| 299 |
|
|
{
|
| 300 |
|
|
FILE *fp, *fopen();
|
| 301 |
|
|
double srcdir[3], sin(), cos();
|
| 302 |
|
|
|
| 303 |
|
|
srcdir[0] = sin(atorad(g));
|
| 304 |
|
|
srcdir[1] = 0.0;
|
| 305 |
|
|
srcdir[2] = -cos(atorad(g));
|
| 306 |
|
|
if ((fp = fopen(sourcetemp, "w")) == NULL) {
|
| 307 |
|
|
fprintf(stderr, "%s: cannot create\n", sourcetemp);
|
| 308 |
|
|
quit(1);
|
| 309 |
|
|
}
|
| 310 |
|
|
fprintf(fp, "0 0 0 1\n");
|
| 311 |
|
|
fprintf(fp, "\nvoid light scanner_light\n");
|
| 312 |
|
|
fprintf(fp, "0\n0\n3 3283 3283 3283\n");
|
| 313 |
|
|
fprintf(fp, "\nscanner_light source scanner_source\n");
|
| 314 |
|
|
fprintf(fp, "0\n0\n4 %f %f %f 2\n", srcdir[0], srcdir[1], srcdir[2]);
|
| 315 |
|
|
fclose(fp);
|
| 316 |
|
|
}
|
| 317 |
|
|
|
| 318 |
|
|
|
| 319 |
|
|
sendsamples(fp) /* send our samples to fp */
|
| 320 |
|
|
FILE *fp;
|
| 321 |
|
|
{
|
| 322 |
|
|
ANGLE *a, *b;
|
| 323 |
|
|
|
| 324 |
|
|
for (b = beta; *b != AEND; b++)
|
| 325 |
|
|
for (a = alpha; *a != AEND; a++)
|
| 326 |
|
|
writesample(fp, *a, *b);
|
| 327 |
|
|
}
|
| 328 |
|
|
|
| 329 |
|
|
|
| 330 |
|
|
writesample(fp, a, b) /* write out sample ray grid */
|
| 331 |
|
|
FILE *fp;
|
| 332 |
|
|
ANGLE a, b;
|
| 333 |
|
|
{
|
| 334 |
|
|
double sin(), cos();
|
| 335 |
|
|
float sp[6];
|
| 336 |
|
|
int i, j;
|
| 337 |
|
|
|
| 338 |
|
|
sp[3] = -sin(atorad(b)-(PI/2.0)) * sin(atorad(a));
|
| 339 |
|
|
sp[4] = -cos(atorad(a));
|
| 340 |
|
|
sp[5] = -sin(atorad(b)) * sin(atorad(a));
|
| 341 |
|
|
|
| 342 |
|
|
for (j = 0; j < yres; j++)
|
| 343 |
|
|
for (i = 0; i < xres; i++) {
|
| 344 |
|
|
sp[0] = -100.0*sp[3] +
|
| 345 |
|
|
(i - xres/2.0 + frandom())*targetw/xres;
|
| 346 |
|
|
sp[1] = -100.0*sp[4] +
|
| 347 |
|
|
(j - yres/2.0 + frandom())*targeth/yres;
|
| 348 |
|
|
sp[2] = -100.0*sp[5];
|
| 349 |
|
|
fwrite(sp, sizeof(*sp), 6, fp);
|
| 350 |
|
|
if (ferror(fp)) {
|
| 351 |
|
|
fprintf(stderr, "Data write error\n");
|
| 352 |
|
|
quit(1);
|
| 353 |
|
|
}
|
| 354 |
|
|
}
|
| 355 |
|
|
}
|
| 356 |
|
|
|
| 357 |
|
|
|
| 358 |
|
|
double
|
| 359 |
|
|
readsample(fp) /* read in sample ray grid */
|
| 360 |
|
|
FILE *fp;
|
| 361 |
|
|
{
|
| 362 |
|
|
double sum;
|
| 363 |
|
|
float col[3];
|
| 364 |
|
|
int i;
|
| 365 |
|
|
|
| 366 |
|
|
sum = 0.0;
|
| 367 |
|
|
i = xres*yres;
|
| 368 |
|
|
while (i--) {
|
| 369 |
|
|
if (fread(col, sizeof(*col), 3, fp) != 3) {
|
| 370 |
|
|
fprintf(stderr, "Data read error\n");
|
| 371 |
|
|
quit(1);
|
| 372 |
|
|
}
|
| 373 |
|
|
sum += col[1];
|
| 374 |
|
|
}
|
| 375 |
|
|
return(sum / (xres*yres));
|
| 376 |
|
|
}
|