| 1 | #ifndef lint | 
| 2 | static const char RCSid[] = "$Id: instance.c,v 2.10 2003/11/14 17:22:06 schorsch Exp $"; | 
| 3 | #endif | 
| 4 | /* | 
| 5 | *  instance.c - routines for octree objects. | 
| 6 | */ | 
| 7 |  | 
| 8 | #include "copyright.h" | 
| 9 |  | 
| 10 | #include  "rtmath.h" | 
| 11 | #include  "rterror.h" | 
| 12 | #include  "rtio.h" | 
| 13 | #include  "paths.h" | 
| 14 |  | 
| 15 | #include  "octree.h" | 
| 16 | #include  "object.h" | 
| 17 | #include  "instance.h" | 
| 18 |  | 
| 19 | #define  IO_ILLEGAL     (IO_FILES|IO_INFO) | 
| 20 |  | 
| 21 | static SCENE  *slist = NULL;            /* list of loaded octrees */ | 
| 22 |  | 
| 23 |  | 
| 24 | SCENE * | 
| 25 | getscene(sname, flags)                  /* get new octree reference */ | 
| 26 | char  *sname; | 
| 27 | int  flags; | 
| 28 | { | 
| 29 | char  *pathname; | 
| 30 | register SCENE  *sc; | 
| 31 |  | 
| 32 | flags &= ~IO_ILLEGAL;           /* not allowed */ | 
| 33 | for (sc = slist; sc != NULL; sc = sc->next) | 
| 34 | if (!strcmp(sname, sc->name)) | 
| 35 | break; | 
| 36 | if (sc == NULL) { | 
| 37 | sc = (SCENE *)malloc(sizeof(SCENE)); | 
| 38 | if (sc == NULL) | 
| 39 | error(SYSTEM, "out of memory in getscene"); | 
| 40 | sc->name = savestr(sname); | 
| 41 | sc->nref = 0; | 
| 42 | sc->ldflags = 0; | 
| 43 | sc->scube.cutree = EMPTY; | 
| 44 | sc->scube.cuorg[0] = sc->scube.cuorg[1] = | 
| 45 | sc->scube.cuorg[2] = 0.; | 
| 46 | sc->scube.cusize = 0.; | 
| 47 | sc->firstobj = sc->nobjs = 0; | 
| 48 | sc->next = slist; | 
| 49 | slist = sc; | 
| 50 | } | 
| 51 | if ((pathname = getpath(sname, getrlibpath(), R_OK)) == NULL) { | 
| 52 | sprintf(errmsg, "cannot find octree file \"%s\"", sname); | 
| 53 | error(USER, errmsg); | 
| 54 | } | 
| 55 | flags &= ~sc->ldflags;          /* skip what's already loaded */ | 
| 56 | if (flags & IO_SCENE) | 
| 57 | sc->firstobj = nobjects; | 
| 58 | if (flags) | 
| 59 | readoct(pathname, flags, &sc->scube, NULL); | 
| 60 | if (flags & IO_SCENE) | 
| 61 | sc->nobjs = nobjects - sc->firstobj; | 
| 62 | sc->ldflags |= flags; | 
| 63 | sc->nref++;                     /* increase reference count */ | 
| 64 | return(sc); | 
| 65 | } | 
| 66 |  | 
| 67 |  | 
| 68 | INSTANCE * | 
| 69 | getinstance(o, flags)                   /* get instance structure */ | 
| 70 | register OBJREC  *o; | 
| 71 | int  flags; | 
| 72 | { | 
| 73 | register INSTANCE  *ins; | 
| 74 |  | 
| 75 | flags &= ~IO_ILLEGAL;           /* not allowed */ | 
| 76 | if ((ins = (INSTANCE *)o->os) == NULL) { | 
| 77 | if ((ins = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL) | 
| 78 | error(SYSTEM, "out of memory in getinstance"); | 
| 79 | if (o->oargs.nsargs < 1) | 
| 80 | objerror(o, USER, "bad # of arguments"); | 
| 81 | if (fullxf(&ins->x, o->oargs.nsargs-1, | 
| 82 | o->oargs.sarg+1) != o->oargs.nsargs-1) | 
| 83 | objerror(o, USER, "bad transform"); | 
| 84 | if (ins->x.f.sca < 0.0) { | 
| 85 | ins->x.f.sca = -ins->x.f.sca; | 
| 86 | ins->x.b.sca = -ins->x.b.sca; | 
| 87 | } | 
| 88 | ins->obj = NULL; | 
| 89 | o->os = (char *)ins; | 
| 90 | } | 
| 91 | if (ins->obj == NULL) | 
| 92 | ins->obj = getscene(o->oargs.sarg[0], flags); | 
| 93 | else if ((flags &= ~ins->obj->ldflags)) { | 
| 94 | if (flags & IO_SCENE) | 
| 95 | ins->obj->firstobj = nobjects; | 
| 96 | if (flags) | 
| 97 | readoct(getpath(o->oargs.sarg[0], getrlibpath(), R_OK), | 
| 98 | flags, &ins->obj->scube, NULL); | 
| 99 | if (flags & IO_SCENE) | 
| 100 | ins->obj->nobjs = nobjects - ins->obj->firstobj; | 
| 101 | ins->obj->ldflags |= flags; | 
| 102 | } | 
| 103 | return(ins); | 
| 104 | } | 
| 105 |  | 
| 106 |  | 
| 107 | void | 
| 108 | freescene(sc)           /* release a scene reference */ | 
| 109 | SCENE *sc; | 
| 110 | { | 
| 111 | SCENE  shead; | 
| 112 | register SCENE  *scp; | 
| 113 |  | 
| 114 | if (sc == NULL) | 
| 115 | return; | 
| 116 | if (sc->nref <= 0) | 
| 117 | error(CONSISTENCY, "unreferenced scene in freescene"); | 
| 118 | sc->nref--; | 
| 119 | if (sc->nref)                   /* still in use? */ | 
| 120 | return; | 
| 121 | shead.next = slist;             /* else remove from our list */ | 
| 122 | for (scp = &shead; scp->next != NULL; scp = scp->next) | 
| 123 | if (scp->next == sc) { | 
| 124 | scp->next = sc->next; | 
| 125 | sc->next = NULL; | 
| 126 | break; | 
| 127 | } | 
| 128 | if (sc->next != NULL)           /* can't be in list anymore */ | 
| 129 | error(CONSISTENCY, "unlisted scene in freescene"); | 
| 130 | slist = shead.next; | 
| 131 | freestr(sc->name);              /* free memory */ | 
| 132 | octfree(sc->scube.cutree); | 
| 133 | freeobjects(sc->firstobj, sc->nobjs); | 
| 134 | free((void *)sc); | 
| 135 | } | 
| 136 |  | 
| 137 |  | 
| 138 | void | 
| 139 | freeinstance(o)         /* free memory associated with instance */ | 
| 140 | OBJREC  *o; | 
| 141 | { | 
| 142 | if (o->os == NULL) | 
| 143 | return; | 
| 144 | freescene((*(INSTANCE *)o->os).obj); | 
| 145 | free((void *)o->os); | 
| 146 | o->os = NULL; | 
| 147 | } |