ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/instance.c
(Generate patch)

Comparing ray/src/common/instance.c (file contents):
Revision 1.5 by greg, Fri Oct 13 19:45:50 1989 UTC vs.
Revision 2.9 by greg, Thu Jul 10 03:30:11 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines