| 1 | greg | 1.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 | greg | 1.2 | #define MG_EBADMAT      10              /* bad material specification */ | 
| 64 | greg | 1.1 |  | 
| 65 | greg | 1.2 | #define MG_NERRS        11 | 
| 66 | greg | 1.1 |  | 
| 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 | greg | 1.2 | #ifdef SMLFLT | 
| 145 |  |  | #define FLOAT   float | 
| 146 |  |  | #else | 
| 147 |  |  | #define FLOAT   double | 
| 148 |  |  | #endif | 
| 149 | greg | 1.1 |  | 
| 150 | greg | 1.2 | typedef FLOAT  FVECT[3]; | 
| 151 |  |  |  | 
| 152 | greg | 1.1 | #ifdef NOPROTO | 
| 153 |  |  | extern double   normalize();            /* normalize a vector */ | 
| 154 |  |  | #else | 
| 155 |  |  | extern double   normalize(FVECT);       /* normalize a vector */ | 
| 156 |  |  | #endif | 
| 157 |  |  |  | 
| 158 |  |  | /************************************************************************ | 
| 159 |  |  | *      Definitions for context handling routines | 
| 160 |  |  | *      (materials, colors, vectors) | 
| 161 |  |  | */ | 
| 162 |  |  |  | 
| 163 |  |  | typedef struct { | 
| 164 | greg | 1.2 | float   cx, cy;         /* XY chromaticity coordinates */ | 
| 165 | greg | 1.1 | } C_COLOR;              /* color context */ | 
| 166 |  |  |  | 
| 167 |  |  | typedef struct { | 
| 168 | greg | 1.2 | char    *name;          /* material name */ | 
| 169 |  |  | int     clock;          /* incremented each change -- resettable */ | 
| 170 |  |  | float   rd;             /* diffuse reflectance */ | 
| 171 | greg | 1.1 | C_COLOR rd_c;           /* diffuse reflectance color */ | 
| 172 | greg | 1.2 | float   td;             /* diffuse transmittance */ | 
| 173 | greg | 1.1 | C_COLOR td_c;           /* diffuse transmittance color */ | 
| 174 | greg | 1.2 | float   ed;             /* diffuse emittance */ | 
| 175 | greg | 1.1 | C_COLOR ed_c;           /* diffuse emittance color */ | 
| 176 | greg | 1.2 | float   rs;             /* specular reflectance */ | 
| 177 | greg | 1.1 | C_COLOR rs_c;           /* specular reflectance color */ | 
| 178 | greg | 1.2 | float   rs_a;           /* specular reflectance roughness */ | 
| 179 |  |  | float   ts;             /* specular transmittance */ | 
| 180 | greg | 1.1 | C_COLOR ts_c;           /* specular transmittance color */ | 
| 181 | greg | 1.2 | float   ts_a;           /* specular transmittance roughness */ | 
| 182 | greg | 1.1 | } C_MATERIAL;           /* material context */ | 
| 183 |  |  |  | 
| 184 |  |  | typedef struct { | 
| 185 |  |  | FVECT   p, n;           /* point and normal */ | 
| 186 |  |  | } C_VERTEX;             /* vertex context */ | 
| 187 |  |  |  | 
| 188 |  |  | #define C_DEFCOLOR      {.333,.333} | 
| 189 | greg | 1.2 | #define C_DEFMATERIAL   {NULL,1,0.,C_DEFCOLOR,0.,C_DEFCOLOR,0.,C_DEFCOLOR,\ | 
| 190 |  |  | 0.,C_DEFCOLOR,0.,0.,C_DEFCOLOR,0.} | 
| 191 | greg | 1.1 | #define C_DEFVERTEX     {{0.,0.,0.},{0.,0.,0.}} | 
| 192 |  |  |  | 
| 193 |  |  | extern C_COLOR          *c_ccolor;      /* the current color */ | 
| 194 |  |  | extern C_MATERIAL       *c_cmaterial;   /* the current material */ | 
| 195 |  |  | extern C_VERTEX         *c_cvertex;     /* the current vertex */ | 
| 196 |  |  |  | 
| 197 |  |  | #ifdef NOPROTO | 
| 198 |  |  | extern int      c_hcolor();                     /* handle color entity */ | 
| 199 |  |  | extern int      c_hmaterial();                  /* handle material entity */ | 
| 200 |  |  | extern int      c_hvertex();                    /* handle vertex entity */ | 
| 201 |  |  | extern void     c_clearall();                   /* clear context tables */ | 
| 202 |  |  | extern C_VERTEX *c_getvert();                   /* get a named vertex */ | 
| 203 |  |  | #else | 
| 204 |  |  | extern int      c_hcolor(int, char **);         /* handle color entity */ | 
| 205 |  |  | extern int      c_hmaterial(int, char **);      /* handle material entity */ | 
| 206 |  |  | extern int      c_hvertex(int, char **);        /* handle vertex entity */ | 
| 207 |  |  | extern void     c_clearall(void);               /* clear context tables */ | 
| 208 |  |  | extern C_VERTEX *c_getvert(char *);             /* get a named vertex */ | 
| 209 |  |  | #endif | 
| 210 |  |  |  | 
| 211 |  |  | /************************************************************************* | 
| 212 |  |  | *      Definitions for hierarchical object name handler | 
| 213 |  |  | */ | 
| 214 |  |  |  | 
| 215 |  |  | extern int      obj_nnames;             /* depth of name hierarchy */ | 
| 216 |  |  | extern char     **obj_name;             /* names in hierarchy */ | 
| 217 |  |  |  | 
| 218 |  |  | #ifdef NOPROTO | 
| 219 |  |  | extern int      obj_handler();                  /* handle an object entity */ | 
| 220 | greg | 1.2 | extern void     obj_clear();                    /* clear object stack */ | 
| 221 | greg | 1.1 | #else | 
| 222 |  |  | extern int      obj_handler(int, char **);      /* handle an object entity */ | 
| 223 | greg | 1.2 | extern void     obj_clear(void);                /* clear object stack */ | 
| 224 | greg | 1.1 | #endif | 
| 225 |  |  |  | 
| 226 |  |  | /************************************************************************** | 
| 227 |  |  | *      Definitions for hierarchical transformation handler | 
| 228 |  |  | */ | 
| 229 |  |  |  | 
| 230 | greg | 1.2 | typedef FLOAT  MAT4[4][4]; | 
| 231 | greg | 1.1 |  | 
| 232 |  |  | #ifdef  BSD | 
| 233 |  |  | #define  copymat4(m4a,m4b)      bcopy((char *)m4b,(char *)m4a,sizeof(MAT4)) | 
| 234 |  |  | #else | 
| 235 |  |  | #define  copymat4(m4a,m4b)      (void)memcpy((char *)m4a,(char *)m4b,sizeof(MAT4)) | 
| 236 |  |  | extern char  *memcpy(); | 
| 237 |  |  | #endif | 
| 238 |  |  |  | 
| 239 |  |  | #define  MAT4IDENT              { {1.,0.,0.,0.}, {0.,1.,0.,0.}, \ | 
| 240 |  |  | {0.,0.,1.,0.}, {0.,0.,0.,1.} } | 
| 241 |  |  |  | 
| 242 |  |  | extern MAT4  m4ident; | 
| 243 |  |  |  | 
| 244 |  |  | #define  setident4(m4)          copymat4(m4, m4ident) | 
| 245 |  |  |  | 
| 246 |  |  | /* regular transformation */ | 
| 247 |  |  | typedef struct { | 
| 248 |  |  | MAT4  xfm;                              /* transform matrix */ | 
| 249 | greg | 1.2 | FLOAT  sca;                             /* scalefactor */ | 
| 250 | greg | 1.1 | }  XF; | 
| 251 |  |  |  | 
| 252 |  |  | #define identxf(xp)             (void)(setident4((xp)->xfm),(xp)->sca=1.0) | 
| 253 |  |  |  | 
| 254 |  |  | typedef struct xf_spec { | 
| 255 |  |  | short   xac;            /* transform argument count */ | 
| 256 |  |  | short   xav0;           /* zeroeth argument in xf_argv array */ | 
| 257 |  |  | XF      xf;             /* cumulative transformation */ | 
| 258 |  |  | struct xf_spec  *prev;  /* previous transformation context */ | 
| 259 |  |  | } XF_SPEC; | 
| 260 |  |  |  | 
| 261 |  |  | extern int      xf_argc;                        /* total # transform args. */ | 
| 262 |  |  | extern char     **xf_argv;                      /* transform arguments */ | 
| 263 |  |  | extern XF_SPEC  *xf_context;                    /* current context */ | 
| 264 |  |  |  | 
| 265 |  |  | /* | 
| 266 |  |  | * The transformation handler should do most of the work that needs | 
| 267 |  |  | * doing.  Just pass it any xf entities, then use the associated | 
| 268 |  |  | * functions to transform and translate points, transform vectors | 
| 269 |  |  | * (without translation), rotate vectors (without scaling) and scale | 
| 270 |  |  | * values appropriately. | 
| 271 |  |  | * | 
| 272 |  |  | * The routines xf_xfmpoint, xf_xfmvect and xf_rotvect take two | 
| 273 |  |  | * 3-D vectors (which may be identical), transforms the second and | 
| 274 |  |  | * puts the result into the first. | 
| 275 |  |  | */ | 
| 276 |  |  |  | 
| 277 |  |  | #ifdef NOPROTO | 
| 278 |  |  |  | 
| 279 |  |  | extern int      xf_handler();           /* handle xf entity */ | 
| 280 |  |  | extern void     xf_xfmpoint();          /* transform point */ | 
| 281 |  |  | extern void     xf_xfmvect();           /* transform vector */ | 
| 282 |  |  | extern void     xf_rotvect();           /* rotate vector */ | 
| 283 |  |  | extern double   xf_scale();             /* scale a value */ | 
| 284 | greg | 1.2 | extern void     xf_clear();             /* clear xf stack */ | 
| 285 | greg | 1.1 |  | 
| 286 |  |  | /* The following are support routines you probably won't call directly */ | 
| 287 |  |  |  | 
| 288 |  |  | extern void     multmat4();             /* m4a = m4b X m4c */ | 
| 289 |  |  | extern void     multv3();               /* v3a = v3b X m4 (vectors) */ | 
| 290 |  |  | extern void     multp3();               /* p3a = p3b X m4 (points) */ | 
| 291 |  |  | extern int      xf();                   /* interpret transform spec. */ | 
| 292 |  |  |  | 
| 293 |  |  | #else | 
| 294 |  |  |  | 
| 295 | greg | 1.2 | extern int      xf_handler(int, char **);       /* handle xf entity */ | 
| 296 |  |  | extern void     xf_xfmpoint(FVECT, FVECT);      /* transform point */ | 
| 297 |  |  | extern void     xf_xfmvect(FVECT, FVECT);       /* transform vector */ | 
| 298 |  |  | extern void     xf_rotvect(FVECT, FVECT);       /* rotate vector */ | 
| 299 |  |  | extern double   xf_scale(double);               /* scale a value */ | 
| 300 |  |  | extern void     xf_clear(void);                 /* clear xf stack */ | 
| 301 | greg | 1.1 |  | 
| 302 |  |  | /* The following are support routines you probably won't call directly */ | 
| 303 |  |  |  | 
| 304 |  |  | extern void     multmat4(MAT4, MAT4, MAT4);     /* m4a = m4b X m4c */ | 
| 305 |  |  | extern void     multv3(FVECT, FVECT, MAT4);     /* v3a = v3b X m4 (vectors) */ | 
| 306 |  |  | extern void     multp3(FVECT, FVECT, MAT4);     /* p3a = p3b X m4 (points) */ | 
| 307 |  |  | extern int      xf(XF, int, char **);           /* interpret transform spec. */ | 
| 308 |  |  |  | 
| 309 |  |  | #endif | 
| 310 |  |  |  | 
| 311 |  |  | /************************************************************************ | 
| 312 |  |  | *      Miscellaneous definitions | 
| 313 |  |  | */ | 
| 314 |  |  |  | 
| 315 |  |  | #ifdef  M_PI | 
| 316 |  |  | #define  PI             M_PI | 
| 317 |  |  | #else | 
| 318 |  |  | #define  PI             3.14159265358979323846 | 
| 319 |  |  | #endif | 
| 320 |  |  |  | 
| 321 |  |  | #ifdef DCL_ATOF | 
| 322 |  |  | extern double   atof(); | 
| 323 |  |  | #endif | 
| 324 |  |  |  | 
| 325 |  |  | #ifndef MEM_PTR | 
| 326 |  |  | #define MEM_PTR         void * | 
| 327 |  |  | #endif | 
| 328 |  |  |  | 
| 329 |  |  | extern MEM_PTR  malloc(); | 
| 330 |  |  | extern MEM_PTR  calloc(); | 
| 331 |  |  | extern MEM_PTR  realloc(); |