| 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 |
extern double atof(); |
| 22 |
static double getflt(); |
| 23 |
static long getint(); |
| 24 |
static char *getstr(); |
| 25 |
static 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 |
extern int fputs(); |
| 41 |
char sbuf[512]; |
| 42 |
int nf; |
| 43 |
OBJECT fnobjects; |
| 44 |
register int i; |
| 45 |
|
| 46 |
if (fname == NULL) { |
| 47 |
infn = "standard input"; |
| 48 |
infp = stdin; |
| 49 |
} else { |
| 50 |
infn = fname; |
| 51 |
if ((infp = fopen(fname, "r")) == NULL) { |
| 52 |
sprintf(errmsg, "cannot open octree file \"%s\"", |
| 53 |
fname); |
| 54 |
error(SYSTEM, errmsg); |
| 55 |
} |
| 56 |
} |
| 57 |
/* get header */ |
| 58 |
if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : NULL) < 0) |
| 59 |
octerror(USER, "not an octree"); |
| 60 |
/* check format */ |
| 61 |
if (getint(2) != OCTMAGIC) |
| 62 |
octerror(USER, "incompatible 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 |
/* get number of objects */ |
| 84 |
fnobjects = getint(sizeof(OBJECT)); |
| 85 |
|
| 86 |
if (load & IO_TREE) { |
| 87 |
/* get the octree */ |
| 88 |
scene->cutree = gettree(); |
| 89 |
/* get the scene */ |
| 90 |
if (nf == 0 && load & IO_SCENE) { |
| 91 |
for (i = 0; *getstr(sbuf); i++) |
| 92 |
if ((otypmap[i] = otype(sbuf)) < 0) { |
| 93 |
sprintf(errmsg, "unknown type \"%s\"", |
| 94 |
sbuf); |
| 95 |
octerror(WARNING, errmsg); |
| 96 |
} |
| 97 |
while (getobj() != OVOID) |
| 98 |
; |
| 99 |
} |
| 100 |
} |
| 101 |
fclose(infp); |
| 102 |
/* consistency checks */ |
| 103 |
if (load & IO_SCENE) { |
| 104 |
/* check object count */ |
| 105 |
if (nobjects != objorig+fnobjects) |
| 106 |
octerror(USER, "bad object count; octree stale?"); |
| 107 |
/* check for non-surfaces */ |
| 108 |
if (nonsurfinset(objorig, fnobjects)) |
| 109 |
octerror(USER, "non-surface in set; octree stale?"); |
| 110 |
} |
| 111 |
return(nf); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
static char * |
| 116 |
getstr(s) /* get null-terminated string */ |
| 117 |
char *s; |
| 118 |
{ |
| 119 |
register char *cp; |
| 120 |
register int c; |
| 121 |
|
| 122 |
cp = s; |
| 123 |
while ((c = getc(infp)) != EOF) |
| 124 |
if ((*cp++ = c) == '\0') |
| 125 |
return(s); |
| 126 |
|
| 127 |
octerror(USER, "truncated octree"); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
static OCTREE |
| 132 |
getfullnode() /* get a set, return fullnode */ |
| 133 |
{ |
| 134 |
OBJECT set[MAXSET+1]; |
| 135 |
register int i; |
| 136 |
|
| 137 |
set[0] = getint(sizeof(OBJECT)); |
| 138 |
if (set[0] > MAXSET) |
| 139 |
octerror(USER, "bad set in getfullnode"); |
| 140 |
for (i = 1; i <= set[0]; i++) |
| 141 |
set[i] = getint(sizeof(OBJECT)) + objorig; |
| 142 |
return(fullnode(set)); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
static long |
| 147 |
getint(siz) /* get a siz-byte integer */ |
| 148 |
register int siz; |
| 149 |
{ |
| 150 |
register int c; |
| 151 |
register long r; |
| 152 |
|
| 153 |
if ((c = getc(infp)) == EOF) |
| 154 |
goto end_file; |
| 155 |
r = 0x80&c ? -1<<8|c : c; /* sign extend */ |
| 156 |
while (--siz > 0) { |
| 157 |
if ((c = getc(infp)) == EOF) |
| 158 |
goto end_file; |
| 159 |
r <<= 8; |
| 160 |
r |= c; |
| 161 |
} |
| 162 |
return(r); |
| 163 |
end_file: |
| 164 |
octerror(USER, "truncated octree"); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
static double |
| 169 |
getflt() /* get a floating point number */ |
| 170 |
{ |
| 171 |
extern double ldexp(); |
| 172 |
double d; |
| 173 |
|
| 174 |
d = (double)getint(4)/0x7fffffff; |
| 175 |
return(ldexp(d, (int)getint(1))); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
static OCTREE |
| 180 |
gettree() /* get a pre-ordered octree */ |
| 181 |
{ |
| 182 |
register OCTREE ot; |
| 183 |
register int i; |
| 184 |
|
| 185 |
switch (getc(infp)) { |
| 186 |
case OT_EMPTY: |
| 187 |
return(EMPTY); |
| 188 |
case OT_FULL: |
| 189 |
return(getfullnode()); |
| 190 |
case OT_TREE: |
| 191 |
if ((ot = octalloc()) == EMPTY) |
| 192 |
octerror(SYSTEM, "out of tree space in gettree"); |
| 193 |
for (i = 0; i < 8; i++) |
| 194 |
octkid(ot, i) = gettree(); |
| 195 |
return(ot); |
| 196 |
case EOF: |
| 197 |
octerror(USER, "truncated octree"); |
| 198 |
default: |
| 199 |
octerror(USER, "damaged octree"); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
static |
| 205 |
getobj() /* get next object */ |
| 206 |
{ |
| 207 |
char sbuf[MAXSTR]; |
| 208 |
int obj; |
| 209 |
register int i; |
| 210 |
register OBJREC *objp; |
| 211 |
|
| 212 |
i = getint(1); |
| 213 |
if (i == -1) |
| 214 |
return(OVOID); /* terminator */ |
| 215 |
if ((obj = newobject()) == OVOID) |
| 216 |
error(SYSTEM, "out of object space"); |
| 217 |
objp = objptr(obj); |
| 218 |
if ((objp->otype = otypmap[i]) < 0) |
| 219 |
octerror(USER, "reference to unknown type"); |
| 220 |
if ((objp->omod = getint(sizeof(OBJECT))) != OVOID) |
| 221 |
objp->omod += objorig; |
| 222 |
objp->oname = savqstr(getstr(sbuf)); |
| 223 |
if (objp->oargs.nsargs = getint(2)) { |
| 224 |
objp->oargs.sarg = (char **)bmalloc |
| 225 |
(objp->oargs.nsargs*sizeof(char *)); |
| 226 |
if (objp->oargs.sarg == NULL) |
| 227 |
goto memerr; |
| 228 |
for (i = 0; i < objp->oargs.nsargs; i++) |
| 229 |
objp->oargs.sarg[i] = savestr(getstr(sbuf)); |
| 230 |
} else |
| 231 |
objp->oargs.sarg = NULL; |
| 232 |
#ifdef IARGS |
| 233 |
if (objp->oargs.niargs = getint(2)) { |
| 234 |
objp->oargs.iarg = (long *)bmalloc |
| 235 |
(objp->oargs.niargs*sizeof(long)); |
| 236 |
if (objp->oargs.iarg == NULL) |
| 237 |
goto memerr; |
| 238 |
for (i = 0; i < objp->oargs.niargs; i++) |
| 239 |
objp->oargs.iarg[i] = getint(4); |
| 240 |
} else |
| 241 |
objp->oargs.iarg = NULL; |
| 242 |
#endif |
| 243 |
if (objp->oargs.nfargs = getint(2)) { |
| 244 |
objp->oargs.farg = (double *)bmalloc |
| 245 |
(objp->oargs.nfargs*sizeof(double)); |
| 246 |
if (objp->oargs.farg == NULL) |
| 247 |
goto memerr; |
| 248 |
for (i = 0; i < objp->oargs.nfargs; i++) |
| 249 |
objp->oargs.farg[i] = getflt(); |
| 250 |
} else |
| 251 |
objp->oargs.farg = NULL; |
| 252 |
/* initialize */ |
| 253 |
objp->os = NULL; |
| 254 |
objp->lastrno = -1; |
| 255 |
/* insert */ |
| 256 |
insertobject(obj); |
| 257 |
return(obj); |
| 258 |
memerr: |
| 259 |
error(SYSTEM, "out of memory in getobj"); |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
static |
| 264 |
octerror(etyp, msg) /* octree error */ |
| 265 |
int etyp; |
| 266 |
char *msg; |
| 267 |
{ |
| 268 |
char msgbuf[128]; |
| 269 |
|
| 270 |
sprintf(msgbuf, "(%s): %s", infn, msg); |
| 271 |
error(etyp, msgbuf); |
| 272 |
} |