home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / RADIANCE / SRC / COMMON / INSTANCE.C < prev    next >
C/C++ Source or Header  |  1993-10-07  |  2KB  |  102 lines

  1. /* Copyright (c) 1990 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)instance.c 2.2 8/6/92 LBL";
  5. #endif
  6.  
  7. /*
  8.  *  instance.c - routines for octree objects.
  9.  *
  10.  *    11/10/88
  11.  */
  12.  
  13. #include  "standard.h"
  14.  
  15. #include  "object.h"
  16.  
  17. #include  "instance.h"
  18.  
  19. static SCENE  *slist = NULL;        /* list of loaded octrees */
  20.  
  21.  
  22. SCENE *
  23. getscene(sname, flags)            /* load octree sname */
  24. char  *sname;
  25. int  flags;
  26. {
  27.     extern char  *libpath;
  28.     char  *pathname;
  29.     register SCENE  *sc;
  30.  
  31.     flags &= ~(IO_FILES|IO_INFO);        /* not allowed */
  32.     for (sc = slist; sc != NULL; sc = sc->next)
  33.         if (!strcmp(sname, sc->name)) {
  34.             if ((sc->ldflags & flags) == flags)
  35.                 return(sc);        /* loaded */
  36.             break;            /* load the rest */
  37.         }
  38.     if (sc == NULL) {
  39.         sc = (SCENE *)malloc(sizeof(SCENE));
  40.         if (sc == NULL)
  41.             error(SYSTEM, "out of memory in getscene");
  42.         sc->name = savestr(sname);
  43.         sc->ldflags = 0;
  44.         sc->scube.cutree = EMPTY;
  45.         sc->scube.cuorg[0] = sc->scube.cuorg[1] =
  46.                 sc->scube.cuorg[2] = 0.;
  47.         sc->scube.cusize = 0.;
  48.         sc->firstobj = sc->nobjs = 0;
  49.         sc->next = slist;
  50.         slist = sc;
  51.     }
  52.     if ((pathname = getpath(sname, libpath, R_OK)) == NULL) {
  53.         sprintf(errmsg, "cannot find octree file \"%s\"", sname);
  54.         error(USER, errmsg);
  55.     }
  56.     if (flags & ~sc->ldflags & IO_SCENE)
  57.         sc->firstobj = nobjects;
  58.     readoct(pathname, flags & ~sc->ldflags, &sc->scube, NULL);
  59.     if (flags & ~sc->ldflags & IO_SCENE)
  60.         sc->nobjs = nobjects - sc->firstobj;
  61.     sc->ldflags |= flags;
  62.     return(sc);
  63. }
  64.  
  65.  
  66. INSTANCE *
  67. getinstance(o, flags)            /* get instance structure */
  68. register OBJREC  *o;
  69. int  flags;
  70. {
  71.     register INSTANCE  *in;
  72.  
  73.     if ((in = (INSTANCE *)o->os) == NULL) {
  74.         if ((in = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL)
  75.             error(SYSTEM, "out of memory in getinstance");
  76.         if (o->oargs.nsargs < 1)
  77.             objerror(o, USER, "bad # of arguments");
  78.         if (fullxf(&in->x, o->oargs.nsargs-1,
  79.                 o->oargs.sarg+1) != o->oargs.nsargs-1)
  80.             objerror(o, USER, "bad transform");
  81.         if (in->x.f.sca < 0.0)
  82.             in->x.f.sca = -in->x.f.sca;
  83.         if (in->x.b.sca < 0.0)
  84.             in->x.b.sca = -in->x.b.sca;
  85.         in->obj = NULL;
  86.         o->os = (char *)in;
  87.     }
  88.     if (in->obj == NULL || (in->obj->ldflags & flags) != flags)
  89.         in->obj = getscene(o->oargs.sarg[0], flags);
  90.     return(in);
  91. }
  92.  
  93.  
  94. freeinstance(o)        /* free memory associated with instance */
  95. OBJREC  *o;
  96. {
  97.     if (o->os == NULL)
  98.         return;
  99.     free(o->os);
  100.     o->os = NULL;
  101. }
  102.