--- ray/src/common/instance.c 1989/10/13 19:45:50 1.5 +++ ray/src/common/instance.c 2003/07/10 03:30:11 2.9 @@ -1,55 +1,65 @@ -/* Copyright (c) 1988 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: instance.c,v 2.9 2003/07/10 03:30:11 greg Exp $"; #endif - /* * instance.c - routines for octree objects. - * - * 11/10/88 */ +#include "copyright.h" + #include "standard.h" +#include "octree.h" + #include "object.h" #include "instance.h" +#define IO_ILLEGAL (IO_FILES|IO_INFO) + static SCENE *slist = NULL; /* list of loaded octrees */ SCENE * -getscene(sname, flags) /* load octree sname */ +getscene(sname, flags) /* get new octree reference */ char *sname; int flags; { - extern char *libpath; char *pathname; register SCENE *sc; - flags &= ~(IO_FILES|IO_INFO); /* not allowed */ + flags &= ~IO_ILLEGAL; /* not allowed */ for (sc = slist; sc != NULL; sc = sc->next) - if (!strcmp(sname, sc->name)) { - if ((sc->ldflags & flags) == flags) - return(sc); /* loaded */ - break; /* load the rest */ - } + if (!strcmp(sname, sc->name)) + break; if (sc == NULL) { sc = (SCENE *)malloc(sizeof(SCENE)); if (sc == NULL) error(SYSTEM, "out of memory in getscene"); sc->name = savestr(sname); + sc->nref = 0; sc->ldflags = 0; + sc->scube.cutree = EMPTY; + sc->scube.cuorg[0] = sc->scube.cuorg[1] = + sc->scube.cuorg[2] = 0.; + sc->scube.cusize = 0.; + sc->firstobj = sc->nobjs = 0; sc->next = slist; slist = sc; } - if ((pathname = getpath(sname, libpath, R_OK)) == NULL) { + if ((pathname = getpath(sname, getrlibpath(), R_OK)) == NULL) { sprintf(errmsg, "cannot find octree file \"%s\"", sname); error(USER, errmsg); } - readoct(pathname, flags & ~sc->ldflags, &sc->scube, NULL); + flags &= ~sc->ldflags; /* skip what's already loaded */ + if (flags & IO_SCENE) + sc->firstobj = nobjects; + if (flags) + readoct(pathname, flags, &sc->scube, NULL); + if (flags & IO_SCENE) + sc->nobjs = nobjects - sc->firstobj; sc->ldflags |= flags; + sc->nref++; /* increase reference count */ return(sc); } @@ -59,25 +69,78 @@ getinstance(o, flags) /* get instance structure */ register OBJREC *o; int flags; { - register INSTANCE *in; + register INSTANCE *ins; - if ((in = (INSTANCE *)o->os) == NULL) { - if ((in = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL) + flags &= ~IO_ILLEGAL; /* not allowed */ + if ((ins = (INSTANCE *)o->os) == NULL) { + if ((ins = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL) error(SYSTEM, "out of memory in getinstance"); if (o->oargs.nsargs < 1) objerror(o, USER, "bad # of arguments"); - if (xf(in->f.xfm, &in->f.sca, o->oargs.nsargs-1, + if (fullxf(&ins->x, o->oargs.nsargs-1, o->oargs.sarg+1) != o->oargs.nsargs-1) objerror(o, USER, "bad transform"); - if (in->f.sca < 0.0) - in->f.sca = -in->f.sca; - invxf(in->b.xfm, &in->b.sca,o->oargs.nsargs-1,o->oargs.sarg+1); - if (in->b.sca < 0.0) - in->b.sca = -in->b.sca; - in->obj = NULL; - o->os = (char *)in; + if (ins->x.f.sca < 0.0) { + ins->x.f.sca = -ins->x.f.sca; + ins->x.b.sca = -ins->x.b.sca; + } + ins->obj = NULL; + o->os = (char *)ins; } - if (in->obj == NULL || (in->obj->ldflags & flags) != flags) - in->obj = getscene(o->oargs.sarg[0], flags); - return(in); + if (ins->obj == NULL) + ins->obj = getscene(o->oargs.sarg[0], flags); + else if ((flags &= ~ins->obj->ldflags)) { + if (flags & IO_SCENE) + ins->obj->firstobj = nobjects; + if (flags) + readoct(getpath(o->oargs.sarg[0], getrlibpath(), R_OK), + flags, &ins->obj->scube, NULL); + if (flags & IO_SCENE) + ins->obj->nobjs = nobjects - ins->obj->firstobj; + ins->obj->ldflags |= flags; + } + return(ins); +} + + +void +freescene(sc) /* release a scene reference */ +SCENE *sc; +{ + SCENE shead; + register SCENE *scp; + + if (sc == NULL) + return; + if (sc->nref <= 0) + error(CONSISTENCY, "unreferenced scene in freescene"); + sc->nref--; + if (sc->nref) /* still in use? */ + return; + shead.next = slist; /* else remove from our list */ + for (scp = &shead; scp->next != NULL; scp = scp->next) + if (scp->next == sc) { + scp->next = sc->next; + sc->next = NULL; + break; + } + if (sc->next != NULL) /* can't be in list anymore */ + error(CONSISTENCY, "unlisted scene in freescene"); + slist = shead.next; + freestr(sc->name); /* free memory */ + octfree(sc->scube.cutree); + freeobjects(sc->firstobj, sc->nobjs); + free((void *)sc); +} + + +void +freeinstance(o) /* free memory associated with instance */ +OBJREC *o; +{ + if (o->os == NULL) + return; + freescene((*(INSTANCE *)o->os).obj); + free((void *)o->os); + o->os = NULL; }