| 1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
| 2 |
– |
|
| 1 |
|
#ifndef lint |
| 2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
| 2 |
> |
static const char RCSid[] = "$Id$"; |
| 3 |
|
#endif |
| 6 |
– |
|
| 4 |
|
/* |
| 5 |
|
* readoct.c - routines to read octree information. |
| 9 |
– |
* |
| 10 |
– |
* 7/30/85 |
| 6 |
|
*/ |
| 7 |
|
|
| 8 |
+ |
#include "copyright.h" |
| 9 |
+ |
|
| 10 |
|
#include "standard.h" |
| 11 |
|
|
| 12 |
|
#include "octree.h" |
| 18 |
|
static double ogetflt(); |
| 19 |
|
static long ogetint(); |
| 20 |
|
static char *ogetstr(); |
| 21 |
< |
static int getobj(), octerror(); |
| 21 |
> |
static int nonsurfinset(); |
| 22 |
> |
static int getobj(), octerror(), skiptree(); |
| 23 |
|
static OCTREE getfullnode(), gettree(); |
| 24 |
|
|
| 25 |
< |
static char *infn; /* input file name */ |
| 25 |
> |
static char *infn; /* input file specification */ |
| 26 |
|
static FILE *infp; /* input file stream */ |
| 27 |
|
static int objsize; /* size of stored OBJECT's */ |
| 28 |
|
static OBJECT objorig; /* zeroeth object */ |
| 29 |
+ |
static OBJECT fnobjects; /* number of objects in this file */ |
| 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; |
| 34 |
> |
readoct(inpspec, load, scene, ofn) /* read in octree file or stream */ |
| 35 |
> |
char *inpspec; |
| 36 |
|
int load; |
| 37 |
|
CUBE *scene; |
| 38 |
|
char *ofn[]; |
| 39 |
|
{ |
| 41 |
– |
extern int fputs(); |
| 40 |
|
char sbuf[512]; |
| 41 |
|
int nf; |
| 44 |
– |
OBJECT fnobjects; |
| 42 |
|
register int i; |
| 43 |
|
long m; |
| 44 |
|
|
| 45 |
< |
if (fname == NULL) { |
| 45 |
> |
if (inpspec == NULL) { |
| 46 |
|
infn = "standard input"; |
| 47 |
|
infp = stdin; |
| 48 |
+ |
} else if (inpspec[0] == '!') { |
| 49 |
+ |
infn = inpspec; |
| 50 |
+ |
if ((infp = popen(inpspec+1, "r")) == NULL) { |
| 51 |
+ |
sprintf(errmsg, "cannot execute \"%s\"", inpspec); |
| 52 |
+ |
error(SYSTEM, errmsg); |
| 53 |
+ |
} |
| 54 |
|
} else { |
| 55 |
< |
infn = fname; |
| 56 |
< |
if ((infp = fopen(fname, "r")) == NULL) { |
| 55 |
> |
infn = inpspec; |
| 56 |
> |
if ((infp = fopen(inpspec, "r")) == NULL) { |
| 57 |
|
sprintf(errmsg, "cannot open octree file \"%s\"", |
| 58 |
< |
fname); |
| 58 |
> |
inpspec); |
| 59 |
|
error(SYSTEM, errmsg); |
| 60 |
|
} |
| 61 |
|
} |
| 62 |
+ |
#ifdef MSDOS |
| 63 |
+ |
setmode(fileno(infp), O_BINARY); |
| 64 |
+ |
#endif |
| 65 |
|
/* get header */ |
| 66 |
< |
if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : NULL) < 0) |
| 66 |
> |
if (checkheader(infp, OCTFMT, load&IO_INFO ? stdout : (FILE *)NULL) < 0) |
| 67 |
|
octerror(USER, "not an octree"); |
| 68 |
|
/* check format */ |
| 69 |
|
if ((objsize = ogetint(2)-OCTMAGIC) <= 0 || |
| 94 |
|
if (fnobjects != m) |
| 95 |
|
octerror(USER, "too many objects"); |
| 96 |
|
|
| 97 |
< |
if (load & IO_TREE) { |
| 92 |
< |
/* get the octree */ |
| 97 |
> |
if (load & IO_TREE) /* get the octree */ |
| 98 |
|
scene->cutree = gettree(); |
| 99 |
< |
if (load & IO_SCENE) /* get the scene */ |
| 100 |
< |
if (nf == 0) { |
| 101 |
< |
for (i = 0; *ogetstr(sbuf); i++) |
| 102 |
< |
if ((otypmap[i] = otype(sbuf)) < 0) { |
| 103 |
< |
sprintf(errmsg, "unknown type \"%s\"", |
| 104 |
< |
sbuf); |
| 105 |
< |
octerror(WARNING, errmsg); |
| 106 |
< |
} |
| 107 |
< |
while (getobj() != OVOID) |
| 108 |
< |
; |
| 109 |
< |
} else { /* consistency checks */ |
| 110 |
< |
/* check object count */ |
| 111 |
< |
if (nobjects != objorig+fnobjects) |
| 112 |
< |
octerror(USER, "bad object count; octree stale?"); |
| 113 |
< |
/* check for non-surfaces */ |
| 114 |
< |
if (nonsurfinset(objorig, fnobjects)) |
| 115 |
< |
octerror(USER, "modifier in tree; octree stale?"); |
| 116 |
< |
} |
| 117 |
< |
} |
| 118 |
< |
fclose(infp); |
| 99 |
> |
else if (load & IO_SCENE && nf == 0) |
| 100 |
> |
skiptree(); |
| 101 |
> |
|
| 102 |
> |
if (load & IO_SCENE) /* get the scene */ |
| 103 |
> |
if (nf == 0) { |
| 104 |
> |
for (i = 0; *ogetstr(sbuf); i++) |
| 105 |
> |
if ((otypmap[i] = otype(sbuf)) < 0) { |
| 106 |
> |
sprintf(errmsg, "unknown type \"%s\"", sbuf); |
| 107 |
> |
octerror(WARNING, errmsg); |
| 108 |
> |
} |
| 109 |
> |
while (getobj() != OVOID) |
| 110 |
> |
; |
| 111 |
> |
} else { /* consistency checks */ |
| 112 |
> |
/* check object count */ |
| 113 |
> |
if (nobjects != objorig+fnobjects) |
| 114 |
> |
octerror(USER, "bad object count; octree stale?"); |
| 115 |
> |
/* check for non-surfaces */ |
| 116 |
> |
if (dosets(nonsurfinset)) |
| 117 |
> |
octerror(USER, "modifier in tree; octree stale?"); |
| 118 |
> |
} |
| 119 |
> |
/* close the input */ |
| 120 |
> |
if (infn[0] == '!') |
| 121 |
> |
pclose(infp); |
| 122 |
> |
else |
| 123 |
> |
fclose(infp); |
| 124 |
|
return(nf); |
| 125 |
|
} |
| 126 |
|
|
| 140 |
|
static OCTREE |
| 141 |
|
getfullnode() /* get a set, return fullnode */ |
| 142 |
|
{ |
| 143 |
< |
OBJECT set[MAXSET+1]; |
| 143 |
> |
OBJECT set[MAXSET+1]; |
| 144 |
|
register int i; |
| 145 |
|
register long m; |
| 146 |
|
|
| 173 |
|
ogetflt() /* get a floating point number */ |
| 174 |
|
{ |
| 175 |
|
extern double getflt(); |
| 176 |
< |
double r; |
| 176 |
> |
double r; |
| 177 |
|
|
| 178 |
|
r = getflt(infp); |
| 179 |
|
if (feof(infp)) |
| 185 |
|
static OCTREE |
| 186 |
|
gettree() /* get a pre-ordered octree */ |
| 187 |
|
{ |
| 188 |
< |
register OCTREE ot; |
| 188 |
> |
register OCTREE ot; |
| 189 |
|
register int i; |
| 190 |
|
|
| 191 |
|
switch (getc(infp)) { |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
|
| 210 |
+ |
static int |
| 211 |
+ |
nonsurfinset(os) /* check set for modifier */ |
| 212 |
+ |
register OBJECT *os; |
| 213 |
+ |
{ |
| 214 |
+ |
register OBJECT s; |
| 215 |
+ |
register int i; |
| 216 |
+ |
|
| 217 |
+ |
for (i = *os; i-- > 0; ) |
| 218 |
+ |
if ((s = *++os) >= objorig && s < objorig+fnobjects && |
| 219 |
+ |
ismodifier(objptr(s)->otype)) |
| 220 |
+ |
return(1); |
| 221 |
+ |
return(0); |
| 222 |
+ |
} |
| 223 |
+ |
|
| 224 |
+ |
|
| 225 |
|
static |
| 226 |
+ |
skiptree() /* skip octree on input */ |
| 227 |
+ |
{ |
| 228 |
+ |
register int i; |
| 229 |
+ |
|
| 230 |
+ |
switch (getc(infp)) { |
| 231 |
+ |
case OT_EMPTY: |
| 232 |
+ |
return; |
| 233 |
+ |
case OT_FULL: |
| 234 |
+ |
for (i = ogetint(objsize)*objsize; i-- > 0; ) |
| 235 |
+ |
if (getc(infp) == EOF) |
| 236 |
+ |
octerror(USER, "truncated octree"); |
| 237 |
+ |
return; |
| 238 |
+ |
case OT_TREE: |
| 239 |
+ |
for (i = 0; i < 8; i++) |
| 240 |
+ |
skiptree(); |
| 241 |
+ |
return; |
| 242 |
+ |
case EOF: |
| 243 |
+ |
octerror(USER, "truncated octree"); |
| 244 |
+ |
default: |
| 245 |
+ |
octerror(USER, "damaged octree"); |
| 246 |
+ |
} |
| 247 |
+ |
} |
| 248 |
+ |
|
| 249 |
+ |
|
| 250 |
+ |
static |
| 251 |
|
getobj() /* get next object */ |
| 252 |
|
{ |
| 253 |
|
char sbuf[MAXSTR]; |
| 254 |
|
int obj; |
| 255 |
|
register int i; |
| 256 |
|
register long m; |
| 257 |
< |
register OBJREC *objp; |
| 257 |
> |
register OBJREC *objp; |
| 258 |
|
|
| 259 |
|
i = ogetint(1); |
| 260 |
|
if (i == -1) |
| 272 |
|
objp->omod = m; |
| 273 |
|
objp->oname = savqstr(ogetstr(sbuf)); |
| 274 |
|
if (objp->oargs.nsargs = ogetint(2)) { |
| 275 |
< |
objp->oargs.sarg = (char **)bmalloc |
| 275 |
> |
objp->oargs.sarg = (char **)malloc |
| 276 |
|
(objp->oargs.nsargs*sizeof(char *)); |
| 277 |
|
if (objp->oargs.sarg == NULL) |
| 278 |
|
goto memerr; |
| 280 |
|
objp->oargs.sarg[i] = savestr(ogetstr(sbuf)); |
| 281 |
|
} else |
| 282 |
|
objp->oargs.sarg = NULL; |
| 283 |
< |
#ifdef IARGS |
| 283 |
> |
#ifdef IARGS |
| 284 |
|
if (objp->oargs.niargs = ogetint(2)) { |
| 285 |
< |
objp->oargs.iarg = (long *)bmalloc |
| 285 |
> |
objp->oargs.iarg = (long *)malloc |
| 286 |
|
(objp->oargs.niargs*sizeof(long)); |
| 287 |
|
if (objp->oargs.iarg == NULL) |
| 288 |
|
goto memerr; |
| 292 |
|
objp->oargs.iarg = NULL; |
| 293 |
|
#endif |
| 294 |
|
if (objp->oargs.nfargs = ogetint(2)) { |
| 295 |
< |
objp->oargs.farg = (FLOAT *)bmalloc |
| 295 |
> |
objp->oargs.farg = (FLOAT *)malloc |
| 296 |
|
(objp->oargs.nfargs*sizeof(FLOAT)); |
| 297 |
|
if (objp->oargs.farg == NULL) |
| 298 |
|
goto memerr; |
| 302 |
|
objp->oargs.farg = NULL; |
| 303 |
|
/* initialize */ |
| 304 |
|
objp->os = NULL; |
| 255 |
– |
objp->lastrno = -1; |
| 305 |
|
/* insert */ |
| 306 |
|
insertobject(obj); |
| 307 |
|
return(obj); |