1 |
/* Copyright (c) 1994 Regents of the University of California */ |
2 |
|
3 |
/* SCCSid "$SunId$ LBL" */ |
4 |
|
5 |
/* |
6 |
* Header file for MGF interpreter |
7 |
*/ |
8 |
|
9 |
/* must include stdio.h before us */ |
10 |
|
11 |
/* Entities (order doesn't really matter) */ |
12 |
#define MG_E_COMMENT 0 |
13 |
#define MG_E_COLOR 1 |
14 |
#define MG_E_CONE 2 |
15 |
#define MG_E_CXY 3 |
16 |
#define MG_E_CYL 4 |
17 |
#define MG_E_ED 5 |
18 |
#define MG_E_FACE 6 |
19 |
#define MG_E_INCLUDE 7 |
20 |
#define MG_E_IES 8 |
21 |
#define MG_E_MATERIAL 9 |
22 |
#define MG_E_NORMAL 10 |
23 |
#define MG_E_OBJECT 11 |
24 |
#define MG_E_POINT 12 |
25 |
#define MG_E_RD 13 |
26 |
#define MG_E_RING 14 |
27 |
#define MG_E_RS 15 |
28 |
#define MG_E_SPH 16 |
29 |
#define MG_E_TD 17 |
30 |
#define MG_E_TORUS 18 |
31 |
#define MG_E_TS 19 |
32 |
#define MG_E_VERTEX 20 |
33 |
#define MG_E_XF 21 |
34 |
|
35 |
#define MG_NENTITIES 22 |
36 |
|
37 |
#define MG_NAMELIST {"#","c","cone","cxy","cyl","ed","f","i","ies",\ |
38 |
"m","n","o","p","rd","ring","rs","sph","td","torus","ts","v","xf"} |
39 |
|
40 |
#define MG_MAXELEN 6 |
41 |
|
42 |
extern char mg_ename[MG_NENTITIES][MG_MAXELEN]; |
43 |
|
44 |
/* Handler routines for each entity */ |
45 |
|
46 |
#ifdef NOPROTO |
47 |
extern int (*mg_ehand[MG_NENTITIES])(); |
48 |
#else |
49 |
extern int (*mg_ehand[MG_NENTITIES])(int argc, char **argv); |
50 |
#endif |
51 |
|
52 |
/* Error codes */ |
53 |
#define MG_OK 0 /* normal return value */ |
54 |
#define MG_EUNK 1 /* unknown entity */ |
55 |
#define MG_EARGC 2 /* wrong number of arguments */ |
56 |
#define MG_ETYPE 3 /* argument type error */ |
57 |
#define MG_EILL 4 /* illegal argument value */ |
58 |
#define MG_EUNDEF 5 /* undefined reference */ |
59 |
#define MG_ENOFILE 6 /* cannot open input file */ |
60 |
#define MG_EINCL 7 /* error in included file */ |
61 |
#define MG_EMEM 8 /* out of memory */ |
62 |
#define MG_ESEEK 9 /* file seek error */ |
63 |
#define MG_EBADMAT 10 /* bad material specification */ |
64 |
|
65 |
#define MG_NERRS 11 |
66 |
|
67 |
extern char *mg_err[MG_NERRS]; |
68 |
|
69 |
/* |
70 |
* The general process for running the parser is to fill in the mg_ehand |
71 |
* array with handlers for each entity you know how to handle. |
72 |
* Then, call mg_init to fill in the rest. This function will report |
73 |
* an error and quit if you try to support an inconsistent set of entities. |
74 |
* For each file you want to parse, call mg_load with the file name. |
75 |
* To read from standard input, use NULL as the file name. |
76 |
* For additional control over error reporting and file management, |
77 |
* use mg_open, mg_read, mg_parse and mg_close instead of mg_load. |
78 |
* To free any data structures and clear the parser, use mg_clear. |
79 |
* If there is an error, mg_load, mg_open, mg_parse, and mg_rewind |
80 |
* will return an error from the list above. In addition, mg_load |
81 |
* will report the error to stderr. The mg_read routine returns 0 |
82 |
* when the end of file has been reached. |
83 |
*/ |
84 |
|
85 |
#define MG_MAXLINE 512 /* maximum input line length */ |
86 |
#define MG_MAXARGC (MG_MAXLINE/4) /* maximum argument count */ |
87 |
|
88 |
typedef struct mg_fctxt { |
89 |
char *fname; /* file name */ |
90 |
FILE *fp; /* stream pointer */ |
91 |
char inpline[MG_MAXLINE]; /* input line */ |
92 |
int lineno; /* line number */ |
93 |
struct mg_fctxt *prev; /* previous context */ |
94 |
} MG_FCTXT; |
95 |
|
96 |
extern MG_FCTXT *mg_file; /* current file context */ |
97 |
|
98 |
#ifdef NOPROTO |
99 |
extern void mg_init(); /* fill in mg_ehand array */ |
100 |
extern int mg_load(); /* parse a file */ |
101 |
extern int mg_open(); /* open new input file */ |
102 |
extern int mg_read(); /* read next line */ |
103 |
extern int mg_parse(); /* parse current line */ |
104 |
extern int mg_rewind(); /* rewind input file */ |
105 |
extern void mg_close(); /* close input file */ |
106 |
extern void mg_clear(); /* clear parser */ |
107 |
extern int mg_iterate(); |
108 |
#else |
109 |
extern void mg_init(void); /* fill in mg_ehand array */ |
110 |
extern int mg_load(char *); /* parse a file */ |
111 |
extern int mg_open(MG_FCTXT *, char *); /* open new input file */ |
112 |
extern int mg_read(void); /* read next line */ |
113 |
extern int mg_parse(void); /* parse current line */ |
114 |
extern int mg_rewind(void); /* rewind input file */ |
115 |
extern void mg_close(void); /* close input file */ |
116 |
extern void mg_clear(void); /* clear parser */ |
117 |
extern int mg_iterate(int, char **, int (*)(void)); |
118 |
#endif |
119 |
|
120 |
#ifndef MG_NQCD |
121 |
#define MG_NQCD 5 /* default number of divisions */ |
122 |
#endif |
123 |
|
124 |
extern int mg_nqcdivs; /* divisions per quarter circle */ |
125 |
|
126 |
/* |
127 |
* The following library routines are included for your convenience: |
128 |
*/ |
129 |
|
130 |
#ifdef NOPROTO |
131 |
extern int mg_entity(); /* get entity number from its name */ |
132 |
extern int isint(); /* non-zero if integer format */ |
133 |
extern int isflt(); /* non-zero if floating point format */ |
134 |
#else |
135 |
extern int mg_entity(char *); /* get entity number from its name */ |
136 |
extern int isint(char *); /* non-zero if integer format */ |
137 |
extern int isflt(char *); /* non-zero if floating point format */ |
138 |
#endif |
139 |
|
140 |
/************************************************************************ |
141 |
* Definitions for 3-d vector manipulation functions |
142 |
*/ |
143 |
|
144 |
#ifdef SMLFLT |
145 |
#define FLOAT float |
146 |
#define FTINY (1e-3) |
147 |
#else |
148 |
#define FLOAT double |
149 |
#define FTINY (1e-6) |
150 |
#endif |
151 |
#define FHUGE (1e10) |
152 |
|
153 |
typedef FLOAT FVECT[3]; |
154 |
|
155 |
#define VCOPY(v1,v2) ((v1)[0]=(v2)[0],(v1)[1]=(v2)[1],(v1)[2]=(v2)[2]) |
156 |
#define DOT(v1,v2) ((v1)[0]*(v2)[0]+(v1)[1]*(v2)[1]+(v1)[2]*(v2)[2]) |
157 |
#define VSUM(vr,v1,v2,f) ((vr)[0]=(v1)[0]+(f)*(v2)[0], \ |
158 |
(vr)[1]=(v1)[1]+(f)*(v2)[1], \ |
159 |
(vr)[2]=(v1)[2]+(f)*(v2)[2]) |
160 |
|
161 |
#define is0vect(v) (DOT(v,v) < FTINY*FTINY) |
162 |
|
163 |
#define round0(x) if (x <= FTINY && x >= -FTINY) x = 0 |
164 |
|
165 |
#ifdef NOPROTO |
166 |
extern double normalize(); /* normalize a vector */ |
167 |
#else |
168 |
extern double normalize(FVECT); /* normalize a vector */ |
169 |
#endif |
170 |
|
171 |
/************************************************************************ |
172 |
* Definitions for context handling routines |
173 |
* (materials, colors, vectors) |
174 |
*/ |
175 |
|
176 |
typedef struct { |
177 |
float cx, cy; /* XY chromaticity coordinates */ |
178 |
} C_COLOR; /* color context */ |
179 |
|
180 |
typedef struct { |
181 |
char *name; /* material name */ |
182 |
int clock; /* incremented each change -- resettable */ |
183 |
float rd; /* diffuse reflectance */ |
184 |
C_COLOR rd_c; /* diffuse reflectance color */ |
185 |
float td; /* diffuse transmittance */ |
186 |
C_COLOR td_c; /* diffuse transmittance color */ |
187 |
float ed; /* diffuse emittance */ |
188 |
C_COLOR ed_c; /* diffuse emittance color */ |
189 |
float rs; /* specular reflectance */ |
190 |
C_COLOR rs_c; /* specular reflectance color */ |
191 |
float rs_a; /* specular reflectance roughness */ |
192 |
float ts; /* specular transmittance */ |
193 |
C_COLOR ts_c; /* specular transmittance color */ |
194 |
float ts_a; /* specular transmittance roughness */ |
195 |
} C_MATERIAL; /* material context */ |
196 |
|
197 |
typedef struct { |
198 |
FVECT p, n; /* point and normal */ |
199 |
} C_VERTEX; /* vertex context */ |
200 |
|
201 |
#define isgrey(cxy) ((cxy)->cx > .31 && (cxy)->cx < .35 && \ |
202 |
(cxy)->cy > .31 && (cxy)->cy < .35) |
203 |
|
204 |
#define C_DEFCOLOR {.333,.333} |
205 |
#define C_DEFMATERIAL {NULL,1,0.,C_DEFCOLOR,0.,C_DEFCOLOR,0.,C_DEFCOLOR,\ |
206 |
0.,C_DEFCOLOR,0.,0.,C_DEFCOLOR,0.} |
207 |
#define C_DEFVERTEX {{0.,0.,0.},{0.,0.,0.}} |
208 |
|
209 |
extern C_COLOR *c_ccolor; /* the current color */ |
210 |
extern C_MATERIAL *c_cmaterial; /* the current material */ |
211 |
extern C_VERTEX *c_cvertex; /* the current vertex */ |
212 |
|
213 |
#ifdef NOPROTO |
214 |
extern int c_hcolor(); /* handle color entity */ |
215 |
extern int c_hmaterial(); /* handle material entity */ |
216 |
extern int c_hvertex(); /* handle vertex entity */ |
217 |
extern void c_clearall(); /* clear context tables */ |
218 |
extern C_VERTEX *c_getvert(); /* get a named vertex */ |
219 |
#else |
220 |
extern int c_hcolor(int, char **); /* handle color entity */ |
221 |
extern int c_hmaterial(int, char **); /* handle material entity */ |
222 |
extern int c_hvertex(int, char **); /* handle vertex entity */ |
223 |
extern void c_clearall(void); /* clear context tables */ |
224 |
extern C_VERTEX *c_getvert(char *); /* get a named vertex */ |
225 |
#endif |
226 |
|
227 |
/************************************************************************* |
228 |
* Definitions for hierarchical object name handler |
229 |
*/ |
230 |
|
231 |
extern int obj_nnames; /* depth of name hierarchy */ |
232 |
extern char **obj_name; /* names in hierarchy */ |
233 |
|
234 |
#ifdef NOPROTO |
235 |
extern int obj_handler(); /* handle an object entity */ |
236 |
extern void obj_clear(); /* clear object stack */ |
237 |
#else |
238 |
extern int obj_handler(int, char **); /* handle an object entity */ |
239 |
extern void obj_clear(void); /* clear object stack */ |
240 |
#endif |
241 |
|
242 |
/************************************************************************** |
243 |
* Definitions for hierarchical transformation handler |
244 |
*/ |
245 |
|
246 |
typedef FLOAT MAT4[4][4]; |
247 |
|
248 |
#ifdef BSD |
249 |
#define copymat4(m4a,m4b) bcopy((char *)m4b,(char *)m4a,sizeof(MAT4)) |
250 |
#else |
251 |
#define copymat4(m4a,m4b) (void)memcpy((char *)m4a,(char *)m4b,sizeof(MAT4)) |
252 |
extern char *memcpy(); |
253 |
#endif |
254 |
|
255 |
#define MAT4IDENT { {1.,0.,0.,0.}, {0.,1.,0.,0.}, \ |
256 |
{0.,0.,1.,0.}, {0.,0.,0.,1.} } |
257 |
|
258 |
extern MAT4 m4ident; |
259 |
|
260 |
#define setident4(m4) copymat4(m4, m4ident) |
261 |
|
262 |
/* regular transformation */ |
263 |
typedef struct { |
264 |
MAT4 xfm; /* transform matrix */ |
265 |
FLOAT sca; /* scalefactor */ |
266 |
} XF; |
267 |
|
268 |
#define identxf(xp) (void)(setident4((xp)->xfm),(xp)->sca=1.0) |
269 |
|
270 |
typedef struct xf_spec { |
271 |
short xac; /* transform argument count */ |
272 |
short xav0; /* zeroeth argument in xf_argv array */ |
273 |
XF xf; /* cumulative transformation */ |
274 |
struct xf_spec *prev; /* previous transformation context */ |
275 |
} XF_SPEC; |
276 |
|
277 |
extern int xf_argc; /* total # transform args. */ |
278 |
extern char **xf_argv; /* transform arguments */ |
279 |
extern XF_SPEC *xf_context; /* current context */ |
280 |
|
281 |
/* |
282 |
* The transformation handler should do most of the work that needs |
283 |
* doing. Just pass it any xf entities, then use the associated |
284 |
* functions to transform and translate points, transform vectors |
285 |
* (without translation), rotate vectors (without scaling) and scale |
286 |
* values appropriately. |
287 |
* |
288 |
* The routines xf_xfmpoint, xf_xfmvect and xf_rotvect take two |
289 |
* 3-D vectors (which may be identical), transforms the second and |
290 |
* puts the result into the first. |
291 |
*/ |
292 |
|
293 |
#ifdef NOPROTO |
294 |
|
295 |
extern int xf_handler(); /* handle xf entity */ |
296 |
extern void xf_xfmpoint(); /* transform point */ |
297 |
extern void xf_xfmvect(); /* transform vector */ |
298 |
extern void xf_rotvect(); /* rotate vector */ |
299 |
extern double xf_scale(); /* scale a value */ |
300 |
extern void xf_clear(); /* clear xf stack */ |
301 |
|
302 |
/* The following are support routines you probably won't call directly */ |
303 |
|
304 |
extern void multmat4(); /* m4a = m4b X m4c */ |
305 |
extern void multv3(); /* v3a = v3b X m4 (vectors) */ |
306 |
extern void multp3(); /* p3a = p3b X m4 (points) */ |
307 |
extern int xf(); /* interpret transform spec. */ |
308 |
|
309 |
#else |
310 |
|
311 |
extern int xf_handler(int, char **); /* handle xf entity */ |
312 |
extern void xf_xfmpoint(FVECT, FVECT); /* transform point */ |
313 |
extern void xf_xfmvect(FVECT, FVECT); /* transform vector */ |
314 |
extern void xf_rotvect(FVECT, FVECT); /* rotate vector */ |
315 |
extern double xf_scale(double); /* scale a value */ |
316 |
extern void xf_clear(void); /* clear xf stack */ |
317 |
|
318 |
/* The following are support routines you probably won't call directly */ |
319 |
|
320 |
extern void multmat4(MAT4, MAT4, MAT4); /* m4a = m4b X m4c */ |
321 |
extern void multv3(FVECT, FVECT, MAT4); /* v3a = v3b X m4 (vectors) */ |
322 |
extern void multp3(FVECT, FVECT, MAT4); /* p3a = p3b X m4 (points) */ |
323 |
extern int xf(XF, int, char **); /* interpret transform spec. */ |
324 |
|
325 |
#endif |
326 |
|
327 |
/************************************************************************ |
328 |
* Miscellaneous definitions |
329 |
*/ |
330 |
|
331 |
#ifdef M_PI |
332 |
#define PI M_PI |
333 |
#else |
334 |
#define PI 3.14159265358979323846 |
335 |
#endif |
336 |
|
337 |
#ifdef DCL_ATOF |
338 |
extern double atof(); |
339 |
#endif |
340 |
|
341 |
#ifndef MEM_PTR |
342 |
#define MEM_PTR void * |
343 |
#endif |
344 |
|
345 |
extern MEM_PTR malloc(); |
346 |
extern MEM_PTR calloc(); |
347 |
extern MEM_PTR realloc(); |