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 2.1 by greg, Tue Nov 12 16:54:54 1991 UTC vs.
Revision 2.11 by greg, Thu Feb 12 18:55:50 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1990 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  "standard.h"
8 > #include "copyright.h"
9  
10 < #include  "object.h"
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)                  /* load octree sname */
25 > getscene(sname, flags)                  /* get new octree reference */
26   char  *sname;
27   int  flags;
28   {
27        extern char  *libpath;
29          char  *pathname;
30          register SCENE  *sc;
31  
32 <        flags &= ~(IO_FILES|IO_INFO);           /* not allowed */
32 >        flags &= ~IO_ILLEGAL;           /* not allowed */
33          for (sc = slist; sc != NULL; sc = sc->next)
34 <                if (!strcmp(sname, sc->name)) {
35 <                        if ((sc->ldflags & flags) == flags)
35 <                                return(sc);             /* loaded */
36 <                        break;                  /* load the rest */
37 <                }
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, libpath, R_OK)) == NULL) {
51 >        if ((pathname = getpath(sname, getrlibpath(), R_OK)) == NULL) {
52                  sprintf(errmsg, "cannot find octree file \"%s\"", sname);
53                  error(USER, errmsg);
54          }
55 <        readoct(pathname, flags & ~sc->ldflags, &sc->scube, NULL);
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  
# Line 59 | Line 70 | getinstance(o, flags)                  /* get instance structure */
70   register OBJREC  *o;
71   int  flags;
72   {
73 <        register INSTANCE  *in;
73 >        register INSTANCE  *ins;
74  
75 <        if ((in = (INSTANCE *)o->os) == NULL) {
76 <                if ((in = (INSTANCE *)malloc(sizeof(INSTANCE))) == NULL)
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(&in->x, o->oargs.nsargs-1,
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 (in->x.f.sca < 0.0)
85 <                        in->x.f.sca = -in->x.f.sca;
86 <                if (in->x.b.sca < 0.0)
87 <                        in->x.b.sca = -in->x.b.sca;
88 <                in->obj = NULL;
89 <                o->os = (char *)in;
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 (in->obj == NULL || (in->obj->ldflags & flags) != flags)
92 <                in->obj = getscene(o->oargs.sarg[0], flags);
93 <        return(in);
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 <        free(o->os);
144 >        freescene((*(INSTANCE *)o->os).obj);
145 >        free((void *)o->os);
146          o->os = NULL;
147   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines