| 1 |
/* Copyright (c) 1986 Regents of the University of California */
|
| 2 |
|
| 3 |
#ifndef lint
|
| 4 |
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
#endif
|
| 6 |
|
| 7 |
/*
|
| 8 |
* readoct.c - routines to read octree information.
|
| 9 |
*
|
| 10 |
* 7/30/85
|
| 11 |
*/
|
| 12 |
|
| 13 |
#include "standard.h"
|
| 14 |
|
| 15 |
#include "octree.h"
|
| 16 |
|
| 17 |
#include "object.h"
|
| 18 |
|
| 19 |
#include "otypes.h"
|
| 20 |
|
| 21 |
double atof();
|
| 22 |
double getflt();
|
| 23 |
long getint();
|
| 24 |
char *getstr();
|
| 25 |
OCTREE getfullnode(), gettree();
|
| 26 |
|
| 27 |
static char *infn; /* input file name */
|
| 28 |
static FILE *infp; /* input file stream */
|
| 29 |
static OBJECT objorig; /* zeroeth object */
|
| 30 |
static short otypmap[NUMOTYPE+8]; /* object type map */
|
| 31 |
|
| 32 |
|
| 33 |
int
|
| 34 |
readoct(fname, load, scene, ofn) /* read in octree from file */
|
| 35 |
char *fname;
|
| 36 |
int load;
|
| 37 |
CUBE *scene;
|
| 38 |
char *ofn[];
|
| 39 |
{
|
| 40 |
char sbuf[128];
|
| 41 |
int nf;
|
| 42 |
register int i;
|
| 43 |
|
| 44 |
if (fname == NULL) {
|
| 45 |
infn = "standard input";
|
| 46 |
infp = stdin;
|
| 47 |
} else {
|
| 48 |
infn = fname;
|
| 49 |
if ((infp = fopen(fname, "r")) == NULL) {
|
| 50 |
sprintf(errmsg, "cannot open octree file \"%s\"",
|
| 51 |
fname);
|
| 52 |
error(SYSTEM, errmsg);
|
| 53 |
}
|
| 54 |
}
|
| 55 |
/* get header */
|
| 56 |
if (load & IO_INFO)
|
| 57 |
copyheader(infp, stdout);
|
| 58 |
else
|
| 59 |
getheader(infp, NULL);
|
| 60 |
/* check format */
|
| 61 |
if (getint(2) != OCTMAGIC)
|
| 62 |
octerror(USER, "invalid octree format");
|
| 63 |
/* get boundaries */
|
| 64 |
if (load & IO_BOUNDS) {
|
| 65 |
for (i = 0; i < 3; i++)
|
| 66 |
scene->cuorg[i] = atof(getstr(sbuf));
|
| 67 |
scene->cusize = atof(getstr(sbuf));
|
| 68 |
} else {
|
| 69 |
for (i = 0; i < 4; i++)
|
| 70 |
getstr(sbuf);
|
| 71 |
}
|
| 72 |
objorig = nobjects; /* set object offset */
|
| 73 |
nf = 0; /* get object files */
|
| 74 |
while (*getstr(sbuf)) {
|
| 75 |
if (load & IO_SCENE)
|
| 76 |
readobj(sbuf);
|
| 77 |
if (load & IO_FILES)
|
| 78 |
ofn[nf] = savqstr(sbuf);
|
| 79 |
nf++;
|
| 80 |
}
|
| 81 |
if (load & IO_FILES)
|
| 82 |
ofn[nf] = NULL;
|
| 83 |
|
| 84 |
if (load & IO_TREE) {
|
| 85 |
/* get the octree */
|
| 86 |
scene->cutree = gettree();
|
| 87 |
/* get the scene */
|
| 88 |
if (nf == 0 && load & IO_SCENE) {
|
| 89 |
for (i = 0; *getstr(sbuf); i++)
|
| 90 |
if ((otypmap[i] = otype(sbuf)) < 0) {
|
| 91 |
sprintf(errmsg, "unknown type \"%s\"",
|
| 92 |
sbuf);
|
| 93 |
octerror(WARNING, errmsg);
|
| 94 |
}
|
| 95 |
while (getobj() != OVOID)
|
| 96 |
;
|
| 97 |
}
|
| 98 |
}
|
| 99 |
fclose(infp);
|
| 100 |
return(nf);
|
| 101 |
}
|
| 102 |
|
| 103 |
|
| 104 |
static char *
|
| 105 |
getstr(s) /* get null-terminated string */
|
| 106 |
char *s;
|
| 107 |
{
|
| 108 |
register char *cp;
|
| 109 |
register int c;
|
| 110 |
|
| 111 |
cp = s;
|
| 112 |
while ((c = getc(infp)) != EOF)
|
| 113 |
if ((*cp++ = c) == '\0')
|
| 114 |
return(s);
|
| 115 |
|
| 116 |
octerror(USER, "truncated octree");
|
| 117 |
}
|
| 118 |
|
| 119 |
|
| 120 |
static OCTREE
|
| 121 |
getfullnode() /* get a set, return fullnode */
|
| 122 |
{
|
| 123 |
OBJECT set[MAXSET+1];
|
| 124 |
register int i;
|
| 125 |
|
| 126 |
set[0] = getint(sizeof(OBJECT));
|
| 127 |
if (set[0] > MAXSET)
|
| 128 |
octerror(USER, "bad set in getfullnode");
|
| 129 |
for (i = 1; i <= set[0]; i++)
|
| 130 |
set[i] = getint(sizeof(OBJECT)) + objorig;
|
| 131 |
return(fullnode(set));
|
| 132 |
}
|
| 133 |
|
| 134 |
|
| 135 |
static long
|
| 136 |
getint(siz) /* get a siz-byte integer */
|
| 137 |
register int siz;
|
| 138 |
{
|
| 139 |
register int c;
|
| 140 |
register long r;
|
| 141 |
|
| 142 |
c = getc(infp);
|
| 143 |
r = c&0x80 ? -1L : 0L; /* sign extend */
|
| 144 |
ungetc(c, infp);
|
| 145 |
while (siz--) {
|
| 146 |
if ((c = getc(infp)) == EOF)
|
| 147 |
octerror(USER, "truncated octree");
|
| 148 |
r <<= 8;
|
| 149 |
r |= c;
|
| 150 |
}
|
| 151 |
return(r);
|
| 152 |
}
|
| 153 |
|
| 154 |
|
| 155 |
static double
|
| 156 |
getflt() /* get a floating point number */
|
| 157 |
{
|
| 158 |
extern double ldexp();
|
| 159 |
double d;
|
| 160 |
|
| 161 |
d = (double)getint(4)/0x7fffffff;
|
| 162 |
return(ldexp(d, getint(1)));
|
| 163 |
}
|
| 164 |
|
| 165 |
|
| 166 |
static OCTREE
|
| 167 |
gettree() /* get a pre-ordered octree */
|
| 168 |
{
|
| 169 |
register OCTREE ot;
|
| 170 |
register int i;
|
| 171 |
|
| 172 |
switch (getc(infp)) {
|
| 173 |
case OT_EMPTY:
|
| 174 |
return(EMPTY);
|
| 175 |
case OT_FULL:
|
| 176 |
return(getfullnode());
|
| 177 |
case OT_TREE:
|
| 178 |
if ((ot = octalloc()) == EMPTY)
|
| 179 |
octerror(SYSTEM, "out of tree space in gettree");
|
| 180 |
for (i = 0; i < 8; i++)
|
| 181 |
octkid(ot, i) = gettree();
|
| 182 |
return(ot);
|
| 183 |
case EOF:
|
| 184 |
octerror(USER, "truncated octree");
|
| 185 |
default:
|
| 186 |
octerror(USER, "damaged octree");
|
| 187 |
}
|
| 188 |
}
|
| 189 |
|
| 190 |
|
| 191 |
static
|
| 192 |
getobj() /* get next object */
|
| 193 |
{
|
| 194 |
char sbuf[MAXSTR];
|
| 195 |
int obj;
|
| 196 |
register int i;
|
| 197 |
register OBJREC *objp;
|
| 198 |
|
| 199 |
i = getint(1);
|
| 200 |
if (i == -1)
|
| 201 |
return(OVOID); /* terminator */
|
| 202 |
if ((obj = newobject()) == OVOID)
|
| 203 |
error(SYSTEM, "out of object space");
|
| 204 |
objp = objptr(obj);
|
| 205 |
if ((objp->otype = otypmap[i]) < 0)
|
| 206 |
octerror(USER, "reference to unknown type");
|
| 207 |
if ((objp->omod = getint(sizeof(OBJECT))) != OVOID)
|
| 208 |
objp->omod += objorig;
|
| 209 |
objp->oname = savqstr(getstr(sbuf));
|
| 210 |
if (objp->oargs.nsargs = getint(2)) {
|
| 211 |
objp->oargs.sarg = (char **)bmalloc
|
| 212 |
(objp->oargs.nsargs*sizeof(char *));
|
| 213 |
if (objp->oargs.sarg == NULL)
|
| 214 |
goto memerr;
|
| 215 |
for (i = 0; i < objp->oargs.nsargs; i++)
|
| 216 |
objp->oargs.sarg[i] = savestr(getstr(sbuf));
|
| 217 |
} else
|
| 218 |
objp->oargs.sarg = NULL;
|
| 219 |
#ifdef IARGS
|
| 220 |
if (objp->oargs.niargs = getint(2)) {
|
| 221 |
objp->oargs.iarg = (long *)bmalloc
|
| 222 |
(objp->oargs.niargs*sizeof(long));
|
| 223 |
if (objp->oargs.iarg == NULL)
|
| 224 |
goto memerr;
|
| 225 |
for (i = 0; i < objp->oargs.niargs; i++)
|
| 226 |
objp->oargs.iarg[i] = getint(4);
|
| 227 |
} else
|
| 228 |
objp->oargs.iarg = NULL;
|
| 229 |
#endif
|
| 230 |
if (objp->oargs.nfargs = getint(2)) {
|
| 231 |
objp->oargs.farg = (double *)bmalloc
|
| 232 |
(objp->oargs.nfargs*sizeof(double));
|
| 233 |
if (objp->oargs.farg == NULL)
|
| 234 |
goto memerr;
|
| 235 |
for (i = 0; i < objp->oargs.nfargs; i++)
|
| 236 |
objp->oargs.farg[i] = getflt();
|
| 237 |
} else
|
| 238 |
objp->oargs.farg = NULL;
|
| 239 |
/* initialize */
|
| 240 |
objp->os = NULL;
|
| 241 |
objp->lastrno = -1;
|
| 242 |
/* insert */
|
| 243 |
insertobject(obj);
|
| 244 |
return(obj);
|
| 245 |
memerr:
|
| 246 |
error(SYSTEM, "out of memory in getobj");
|
| 247 |
}
|
| 248 |
|
| 249 |
|
| 250 |
static
|
| 251 |
octerror(etyp, msg) /* octree error */
|
| 252 |
int etyp;
|
| 253 |
char *msg;
|
| 254 |
{
|
| 255 |
char msgbuf[128];
|
| 256 |
|
| 257 |
sprintf(msgbuf, "(%s): %s", infn, msg);
|
| 258 |
error(etyp, msgbuf);
|
| 259 |
}
|