Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
if(smass > 0) {
fprintf(outf, "CM %lg %lg %lg\n", sx/smass, sy/smass, sz/smass);
fprintf(outf, "bbox center %g %g %g radius %g %g %g\n",
.5*(pmax.x[0]+pmin.x[0]), .5*(pmax.x[1]+pmin.x[1]), .5*(pmax.x[2]+pmin.x[2]),
.5*(pmax.x[0]-pmin.x[0]), .5*(pmax.x[1]-pmin.x[1]), .5*(pmax.x[2]-pmin.x[2]));
Point cen;
kira_get_center( &cen, dyn, st );
fprintf(outf, "Center %g %g %g", cen.x[0],cen.x[1],cen.x[2]);
if(has_tfm) {
Point wcen;
vtfmpoint( &wcen, &cen, &pT );
fprintf(outf, " -> %g %g %g", wcen.x[0],wcen.x[1],wcen.x[2]);
}
fprintf(outf, "\n");
}
fprintf(outf, "transformation: out = in");
if(curstate.turnrate != 0)
fprintf(outf, " * %cRotation(%g*(time-%g))",
curstate.axis, curstate.turnrate, curstate.turntime0);
if(curstate.has_T0) {
float *fp = curstate.T0.m;
fprintf(outf, " * [%g %g %g %g %g %g %g %g %g %g %g %g %g %g %g %g]",
fp[0],fp[1],fp[2],fp[3],
fp[4],fp[5],fp[6],fp[7],
fp[8],fp[9],fp[10],fp[11],
fp[12],fp[13],fp[14],fp[15]);
}
else if(curstate.turnrate == 0)
fprintf(outf, " (identity transform)");
fprintf(outf, "\n");
fprintf(outf, "group %d type %d\n", curstate.group, curstate.type);
}
float specklum( struct speck *sp ) {
return (curstate.masslum != 0) ? sp->val[SPECK_MASS]*sp->val[SPECK_MASS]*sp->val[SPECK_MASS]*curstate.masslum :
sp->val[SPECK_LUM];
}
int serveonce(char *req, FILE *outf)
{
int as_sdb = 0, as_speck = 0;
Point p;
db_star star;
char *cp;
struct stuff *st = curstate.st;
nspecks=0;
sx = sy = sz = smass = 0;
pmin.x[0]=pmin.x[1]=pmin.x[2] = 1e20;
pmax.x[0]=pmax.x[1]=pmax.x[2] = -1e20;
if(curstate.verbose)
fprintf(stdout, "REQ: %s\n", req);
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
if(outf == NULL || req == NULL) {
msg("kira server: get lost!");
if(outf) fclose(outf);
return 0;
}
if(strstr(req, "sdb")) as_sdb = 1;
else if(strstr(req, "speck")) as_speck = 1;
char *eqp;
for(eqp = req; (eqp = strchr(eqp, '=')) != NULL; ) {
char *optp, *argp;
for(optp = eqp; isalpha(optp[-1]); optp--)
;
argp = eqp+1;
cp = strchr(argp, '&');
if(cp) {
eqp = cp+1;
*cp = '\0';
}
scanopt( optp, argp );
if(cp == NULL) break;
}
memset(&star, 0, sizeof(star));
star.group = curstate.group;
star.type = curstate.type;
struct specklist *sl;
sl = kira_get_parti( &st->dyn, st, curstate.realtime );
if(sl == NULL)
return 0;
has_tfm = curstate.has_T0 || curstate.turnrate != 0;
if(curstate.turnrate != 0) {
Matrix Trot;
mrotation( &Trot, curstate.turnrate * (curstate.realtime - curstate.turntime0), curstate.axis );
mmmul( &pT, &Trot, &curstate.T0 );
} else {
pT = curstate.T0;
nspecks = 0;
for(int i = 0; i < sl->nspecks; i++) {
struct speck *sp = NextSpeck(sl->specks, sl, i);
if(sp->val[SPECK_NCLUMP] < 0)
continue;
nspecks++;
Point vel, tvel;
vel.x[0] = sp->val[SPECK_SEPVEC] * curstate.dt;
vel.x[1] = sp->val[SPECK_SEPVEC+1] * curstate.dt;
vel.x[2] = sp->val[SPECK_SEPVEC+2] * curstate.dt;
if(has_tfm) {
vtfmpoint( &p, &sp->p, &pT );
vtfmpoint( &tvel, &vel, &pT );
vel = tvel;
} else {
p = sp->p;
}
if(as_sdb) {
star.x = p.x[0];
star.y = p.x[1];
star.z = p.x[2];
star.dx = vel.x[0]; // Use velocity,
star.dy = vel.x[1]; // held in SEPVEC
star.dz = vel.x[2]; // for leaf nodes.
star.magnitude = curstate.mag0 - .921 * logf( specklum(sp) );
if(curstate.colorscale >= 0) {
star.color = (unsigned short) (curstate.colorscale * sp->val[SPECK_TLOG]);
} else {
/* packed rgb565 */
star.color = logT2rgb565( sp->val[SPECK_TLOG] );
}
star.num = (int) sp->val[SPECK_ID];
starswap(&star);
wrote = fwrite(&star, sizeof(star), 1, outf);
} else if(as_speck) {
wrote = fprintf(outf, "%g %g %g %g %g %g\n",
p.x[0],p.x[1],p.x[2],
specklum(sp), sp->val[SPECK_TLOG], sp->val[SPECK_ID]);
} else {
double m = sp->val[SPECK_MASS];
sx += p.x[0]*m;
sy += p.x[1]*m;
sz += p.x[2]*m;
smass += m;
if(pmin.x[0] > p.x[0]) pmin.x[0] = p.x[0];
if(pmin.x[1] > p.x[1]) pmin.x[1] = p.x[1];
if(pmin.x[2] > p.x[2]) pmin.x[2] = p.x[2];
if(pmax.x[0] < p.x[0]) pmax.x[0] = p.x[0];
if(pmax.x[1] < p.x[1]) pmax.x[1] = p.x[1];
if(pmax.x[2] < p.x[2]) pmax.x[2] = p.x[2];
if(wrote < 0) /* SIGPIPE? Anyway, quit writing. */
break;
}
if(!as_sdb && !as_speck) {
report( outf, &st->dyn, st );
}
if(curstate.verbose) {
char *cp = strchr(req, '\n');
if(cp) *cp = '\0';
fprintf(stdout, "REQ: %s\n", req);
report( stdout, &st->dyn, st );
fflush(stdout);
return nspecks;
}
int serverlisten( int port ) {
struct sockaddr_in asin;
static int one = 1;
int lsock;
memset(&asin, 0, sizeof(asin));
asin.sin_family = AF_INET;
asin.sin_port = htons(port);
asin.sin_addr.s_addr = INADDR_ANY;
lsock = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
if(lsock < 0) {
perror("socket");
return -1;
}
setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if(bind(lsock, (struct sockaddr *)&asin, sizeof(asin)) < 0) {
perror("bind");
return -1;
}
if(listen(lsock, 90) < 0) {
perror("listen");
return -1;
}
return lsock;
}
void serverloop( int lsock ) {
for(;;) {
struct sockaddr_in from;
FILE *inf, *outf;
char req[1280];
#if sgi
int fromlen = sizeof(from);
#else
int s = accept(lsock, (struct sockaddr *)&from, &fromlen);
if(s < 0) {
if(errno == EINTR)
continue;
perror("accept");
return;
}
inf = fdopen(s, "r");
if(fgets(req, sizeof(req), inf) == NULL) {
perror("fgets");
} else {
outf = fdopen(s, "w");
do {
serveonce( req, outf );
fflush(outf);
} while(fgets(req, sizeof(req), inf) != NULL);
fclose(outf);
}
fclose(inf);
close(s);
}
}
main(int argc, char *argv[])
{
static struct stuff tst;
curstate.st = &tst;
int c;
char opt[8];
progname = argv[0];
curstate.colorscale = -1;
curstate.has_T0 = 0;
curstate.port = 3400;
while((c = getopt(argc, argv, "T:r:t:y:g:m:c:o:p:v:")) != EOF) {
opt[0] = c;
opt[1] = '\0';
if(c == 'y') strcpy(opt, "type");
scanopt( opt, optarg );
}
if(optind != argc-1) {
fprintf(stderr, "Usage: %s [options] file.kira\n\
Options:\n\
-t time\n\
-r <axis><degreespertime>[@time0] Timed rotation about axis 'x'/'y'/'z'\n\
-T 16-numbers outcoords = kira * rotation(time) * tfm\n\
-y sdbtype\n\
-g group\n\
-m mag0 sdb mag = mag0 - log(lum)/(log(100)/5)\n\
-M masslum if nonzero, ignore bogus lum; use mass**3 * masslum instead.
-c colorscale sdb color = log10(T)*colorscale; -c -1 => RGB565 (default)\n\
-o treestyle(1 vs 2) (default -o 2 for create_interpolated_tree2)
-p portno listen for HTTP connections on that TCP port\n\
-v verbose(0 vs 1) server logs requests to its own stdout
", progname);
exit(1);
}
/* Ignore SIGPIPE signals -- don't crash if a caller disconnects. */
signal(SIGPIPE, SIG_IGN);
msg("Listening on port %d", curstate.port);
int lsock = serverlisten( curstate.port );
if(lsock < 0)
exit(1);
msg("Reading %s", argv[optind]);
kira_open( &curstate.st->dyn, curstate.st, argv[optind], 0 );
msg("Ready.");
serverloop( lsock );
}