ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/meta/segment.c
Revision: 1.1
Committed: Sat Feb 22 02:07:26 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2     static const char RCSid[] = "$Id$";
3     #endif
4     /*
5     * Routines for segment storage and inclusion for meta-files
6     *
7     * 12/18/84
8     */
9    
10    
11     #include "meta.h"
12    
13    
14     #define NODEC 0 /* null declaration (must be 0) */
15    
16     #define MAXDEC 2048 /* maximum number of declarations */
17    
18     #define NHASH 101 /* hash size (prime) */
19    
20    
21     static struct declaration {
22     char *sname; /* segment name */
23     PLIST sprims; /* segment primitives */
24     int context; /* declaration containing us */
25     int nexthash; /* next in hash list */
26     } dectabl[MAXDEC];
27    
28     static int hashtabl[NHASH];
29    
30     static int dectop = 0; /* top of declaration table */
31    
32     static int curdec = NODEC; /* current declaration */
33    
34    
35    
36    
37    
38     static int
39     hash(s) /* hash a string */
40    
41     register char *s;
42    
43     {
44     register int hval = 0;
45    
46     while (*s)
47     hval += *s++;
48    
49     return(hval % NHASH);
50     }
51    
52    
53    
54    
55    
56     static int
57     lookup(name) /* find name in declaration table */
58    
59     register char *name;
60    
61     {
62     register int curd;
63    
64     if (name == NULL)
65     error(USER, "illegal null segment name in lookup");
66    
67     for (curd = hashtabl[hash(name)]; curd != NODEC; curd = dectabl[curd].nexthash)
68     if (strcmp(name, dectabl[curd].sname) == 0)
69     break;
70    
71     return(curd);
72     }
73    
74    
75    
76    
77     inseg() /* return TRUE if currently in a segment */
78    
79     {
80    
81     return(curdec != NODEC);
82     }
83    
84    
85    
86    
87     openseg(name) /* open a new segment */
88    
89     char *name;
90    
91     {
92     register int olddec;
93    
94     if ((olddec = lookup(name)) != NODEC &&
95     dectabl[olddec].context == curdec) /* redefined segment */
96     plfree(&dectabl[olddec].sprims);
97    
98     if (++dectop >= MAXDEC)
99     error(SYSTEM, "too many segments in openseg");
100    
101     dectabl[dectop].sname = savestr(name);
102     /* save previous context */
103     dectabl[dectop].context = curdec;
104     curdec = dectop;
105    
106     }
107    
108    
109    
110    
111    
112     segprim(p) /* store primitive in current segment */
113    
114     register PRIMITIVE *p;
115    
116     {
117     register PRIMITIVE *newp;
118    
119     if (!inseg())
120     error(SYSTEM, "illegal call to segprim");
121    
122     switch (p->com) {
123    
124     case POPEN:
125     openseg(p->args);
126     break;
127    
128     case PSEG:
129     segment(p, segprim);
130     break;
131    
132     case PCLOSE:
133     closeseg();
134     break;
135    
136     default:
137     if ((newp = palloc()) == NULL)
138     error(SYSTEM, "memory limit exceeded in segprim");
139     mcopy((char *)newp, (char *)p, sizeof(PRIMITIVE));
140     newp->args = savestr(p->args);
141     add(newp, &dectabl[curdec].sprims);
142     break;
143     }
144    
145     }
146    
147    
148    
149     closeseg() /* close the current segment */
150    
151     {
152     register int i;
153    
154     if (!inseg())
155     error(SYSTEM, "illegal call to closeseg");
156    
157     /* undefine internal declarations */
158     for (i = dectop; i > curdec; i--) {
159     hashtabl[hash(dectabl[i].sname)] = dectabl[i].nexthash;
160     freestr(dectabl[i].sname);
161     plfree(&dectabl[i].sprims);
162     }
163     dectop = curdec;
164     /* define this declaration */
165     i = hash(dectabl[curdec].sname);
166     dectabl[curdec].nexthash = hashtabl[i];
167     hashtabl[i] = curdec;
168     /* return context */
169     curdec = dectabl[curdec].context;
170    
171     }
172    
173    
174    
175    
176     segment(p, funcp) /* expand segment p */
177     PRIMITIVE *p;
178     int (*funcp)();
179    
180     {
181     int decln;
182     PRIMITIVE curp;
183     register PRIMITIVE *sp;
184    
185     if ((decln = lookup(p->args)) == NODEC) {
186     sprintf(errmsg, "reference to undefined segment \"%s\" in segment",
187     p->args);
188     error(USER, errmsg);
189     }
190    
191     for (sp = dectabl[decln].sprims.ptop; sp != NULL; sp = sp->pnext)
192    
193     if (isglob(sp->com))
194    
195     (*funcp)(sp);
196    
197     else {
198    
199     switch (curp.com = sp->com) {
200    
201     case PSEG:
202     case PVSTR:
203     case PTFILL:
204     case PPFILL:
205     curp.arg0 = (sp->arg0 & 0100) |
206     (((sp->arg0 & 060) + p->arg0) & 060);
207     break;
208    
209     case PLSEG:
210     if (p->arg0 & 020)
211     curp.arg0 = (~sp->arg0 & 0100) | (sp->arg0 & 060);
212     else
213     curp.arg0 = sp->arg0 & 0160;
214     break;
215    
216     default:
217     curp.arg0 = sp->arg0 & 0160;
218     break;
219     }
220     if (p->arg0 & 014)
221     curp.arg0 |= p->arg0 & 014;
222     else
223     curp.arg0 |= sp->arg0 & 014;
224     if (p->arg0 & 03)
225     curp.arg0 |= p->arg0 & 03;
226     else
227     curp.arg0 |= sp->arg0 & 03;
228    
229     curp.xy[XMN] = xlate(XMN, sp, p);
230     curp.xy[YMN] = xlate(YMN, sp, p);
231     curp.xy[XMX] = xlate(XMX, sp, p);
232     curp.xy[YMX] = xlate(YMX, sp, p);
233     curp.args = sp->args;
234    
235     (*funcp)(&curp);
236     }
237    
238     }
239    
240    
241    
242    
243    
244     int
245     xlate(extrema, p, px) /* return extrema from p through px */
246    
247     short extrema;
248     PRIMITIVE *p;
249     register PRIMITIVE *px;
250    
251     {
252     short oldex;
253     int val;
254    
255     if (((oldex = (extrema + 4 - ((px->arg0 >> 4) & 03))%4) ^ extrema) & 02)
256     val = (XYSIZE-1) - p->xy[oldex];
257     else
258     val = p->xy[oldex];
259    
260     if (extrema & 01)
261     return(CONV(val, px->xy[YMX] - px->xy[YMN]) + px->xy[YMN]);
262     else
263     return(CONV(val, px->xy[XMX] - px->xy[XMN]) + px->xy[XMN]);
264     }