| 1 | greg | 3.1 | /* RCSid: $Id: parser.h,v 1.39 2011/01/14 05:46:12 greg Exp $ */ | 
| 2 |  |  | /* | 
| 3 |  |  | * Header file for MGF interpreter | 
| 4 |  |  | */ | 
| 5 |  |  | #ifndef _MGF_PARSER_H_ | 
| 6 |  |  | #define _MGF_PARSER_H_ | 
| 7 |  |  |  | 
| 8 |  |  | /* must include stdio.h and stdlib.h before us */ | 
| 9 |  |  |  | 
| 10 |  |  | #include "rtmath.h" | 
| 11 |  |  | #include "ccolor.h" | 
| 12 |  |  |  | 
| 13 |  |  | #ifdef __cplusplus | 
| 14 |  |  | extern "C" { | 
| 15 |  |  | #endif | 
| 16 |  |  |  | 
| 17 |  |  | #define MG_VMAJOR       2               /* major version number */ | 
| 18 |  |  | #define MG_VMINOR       0               /* minor version number */ | 
| 19 |  |  |  | 
| 20 |  |  | /* Entities (list is only appended, never modified) */ | 
| 21 |  |  | #define MG_E_COMMENT    0               /* #            */ | 
| 22 |  |  | #define MG_E_COLOR      1               /* c            */ | 
| 23 |  |  | #define MG_E_CCT        2               /* cct          */ | 
| 24 |  |  | #define MG_E_CONE       3               /* cone         */ | 
| 25 |  |  | #define MG_E_CMIX       4               /* cmix         */ | 
| 26 |  |  | #define MG_E_CSPEC      5               /* cspec        */ | 
| 27 |  |  | #define MG_E_CXY        6               /* cxy          */ | 
| 28 |  |  | #define MG_E_CYL        7               /* cyl          */ | 
| 29 |  |  | #define MG_E_ED         8               /* ed           */ | 
| 30 |  |  | #define MG_E_FACE       9               /* f            */ | 
| 31 |  |  | #define MG_E_INCLUDE    10              /* i            */ | 
| 32 |  |  | #define MG_E_IES        11              /* ies          */ | 
| 33 |  |  | #define MG_E_IR         12              /* ir           */ | 
| 34 |  |  | #define MG_E_MATERIAL   13              /* m            */ | 
| 35 |  |  | #define MG_E_NORMAL     14              /* n            */ | 
| 36 |  |  | #define MG_E_OBJECT     15              /* o            */ | 
| 37 |  |  | #define MG_E_POINT      16              /* p            */ | 
| 38 |  |  | #define MG_E_PRISM      17              /* prism        */ | 
| 39 |  |  | #define MG_E_RD         18              /* rd           */ | 
| 40 |  |  | #define MG_E_RING       19              /* ring         */ | 
| 41 |  |  | #define MG_E_RS         20              /* rs           */ | 
| 42 |  |  | #define MG_E_SIDES      21              /* sides        */ | 
| 43 |  |  | #define MG_E_SPH        22              /* sph          */ | 
| 44 |  |  | #define MG_E_TD         23              /* td           */ | 
| 45 |  |  | #define MG_E_TORUS      24              /* torus        */ | 
| 46 |  |  | #define MG_E_TS         25              /* ts           */ | 
| 47 |  |  | #define MG_E_VERTEX     26              /* v            */ | 
| 48 |  |  | #define MG_E_XF         27              /* xf           */ | 
| 49 |  |  | /* end of Version 1 entities */ | 
| 50 |  |  | #define MG_E_FACEH      28              /* fh           */ | 
| 51 |  |  | /* end of Version 2 entities */ | 
| 52 |  |  |  | 
| 53 |  |  | #define MG_NENTITIES    29              /* total # entities */ | 
| 54 |  |  |  | 
| 55 |  |  | #define MG_NELIST       {28,29}         /* entity count for version 1 and up */ | 
| 56 |  |  |  | 
| 57 |  |  | #define MG_NAMELIST     {"#","c","cct","cone","cmix","cspec","cxy","cyl","ed",\ | 
| 58 |  |  | "f","i","ies","ir","m","n","o","p","prism","rd",\ | 
| 59 |  |  | "ring","rs","sides","sph","td","torus","ts","v","xf",\ | 
| 60 |  |  | "fh"} | 
| 61 |  |  |  | 
| 62 |  |  | #define MG_MAXELEN      6 | 
| 63 |  |  |  | 
| 64 |  |  | extern char     mg_ename[MG_NENTITIES][MG_MAXELEN]; | 
| 65 |  |  |  | 
| 66 |  |  | /* Handler routines for each entity and unknown ones */ | 
| 67 |  |  | extern int      (*mg_ehand[MG_NENTITIES])(int argc, char **argv); | 
| 68 |  |  | extern int      (*mg_uhand)(int argc, char **argv); | 
| 69 |  |  | extern int      mg_defuhand(int, char **); | 
| 70 |  |  |  | 
| 71 |  |  | extern unsigned mg_nunknown;            /* count of unknown entities */ | 
| 72 |  |  |  | 
| 73 |  |  | /* Error codes */ | 
| 74 |  |  | #define MG_OK           0               /* normal return value */ | 
| 75 |  |  | #define MG_EUNK         1               /* unknown entity */ | 
| 76 |  |  | #define MG_EARGC        2               /* wrong number of arguments */ | 
| 77 |  |  | #define MG_ETYPE        3               /* argument type error */ | 
| 78 |  |  | #define MG_EILL         4               /* illegal argument value */ | 
| 79 |  |  | #define MG_EUNDEF       5               /* undefined reference */ | 
| 80 |  |  | #define MG_ENOFILE      6               /* cannot open input file */ | 
| 81 |  |  | #define MG_EINCL        7               /* error in included file */ | 
| 82 |  |  | #define MG_EMEM         8               /* out of memory */ | 
| 83 |  |  | #define MG_ESEEK        9               /* file seek error */ | 
| 84 |  |  | #define MG_EBADMAT      10              /* bad material specification */ | 
| 85 |  |  | #define MG_ELINE        11              /* input line too long */ | 
| 86 |  |  | #define MG_ECNTXT       12              /* unmatched context close */ | 
| 87 |  |  |  | 
| 88 |  |  | #define MG_NERRS        13 | 
| 89 |  |  |  | 
| 90 |  |  | extern char     *mg_err[MG_NERRS];      /* list of error messages */ | 
| 91 |  |  |  | 
| 92 |  |  | /* | 
| 93 |  |  | * The general process for running the parser is to fill in the mg_ehand | 
| 94 |  |  | * array with handlers for each entity you know how to handle. | 
| 95 |  |  | * Then, call mg_init to fill in the rest.  This function will report | 
| 96 |  |  | * an error and quit if you try to support an inconsistent set of entities. | 
| 97 |  |  | * For each file you want to parse, call mg_load with the file name. | 
| 98 |  |  | * To read from standard input, use NULL as the file name. | 
| 99 |  |  | * For additional control over error reporting and file management, | 
| 100 |  |  | * use mg_open, mg_read, mg_parse and mg_close instead of mg_load. | 
| 101 |  |  | * To pass an entity of your own construction to the parser, use | 
| 102 |  |  | * the mg_handle function rather than the mg_ehand routines directly. | 
| 103 |  |  | * (The first argument to mg_handle is the entity #, or -1.) | 
| 104 |  |  | * To free any data structures and clear the parser, use mg_clear. | 
| 105 |  |  | * If there is an error, mg_load, mg_open, mg_parse, mg_handle and | 
| 106 |  |  | * mg_fgoto will return an error from the list above.  In addition, | 
| 107 |  |  | * mg_load will report the error to stderr.  The mg_read routine | 
| 108 |  |  | * returns 0 when the end of file has been reached. | 
| 109 |  |  | */ | 
| 110 |  |  |  | 
| 111 |  |  | #define MG_MAXLINE      4096            /* maximum input line length */ | 
| 112 |  |  | #define MG_MAXARGC      (MG_MAXLINE/4)  /* maximum argument count */ | 
| 113 |  |  |  | 
| 114 |  |  | typedef struct mg_fctxt { | 
| 115 |  |  | char    fname[96];                      /* file name */ | 
| 116 |  |  | FILE    *fp;                            /* stream pointer */ | 
| 117 |  |  | int     fid;                            /* unique file context id */ | 
| 118 |  |  | char    inpline[MG_MAXLINE];            /* input line */ | 
| 119 |  |  | int     lineno;                         /* line number */ | 
| 120 |  |  | struct mg_fctxt *prev;                  /* previous context */ | 
| 121 |  |  | } MG_FCTXT; | 
| 122 |  |  |  | 
| 123 |  |  | typedef struct { | 
| 124 |  |  | int     fid;                            /* file this position is for */ | 
| 125 |  |  | int     lineno;                         /* line number in file */ | 
| 126 |  |  | long    offset;                         /* offset from beginning */ | 
| 127 |  |  | } MG_FPOS; | 
| 128 |  |  |  | 
| 129 |  |  | extern MG_FCTXT *mg_file;               /* current file context */ | 
| 130 |  |  |  | 
| 131 |  |  | extern void     mg_init(void);          /* fill in mg_ehand array */ | 
| 132 |  |  | extern int      mg_load(char *);        /* parse a file */ | 
| 133 |  |  | extern int      mg_open(MG_FCTXT *, char *);    /* open new input file */ | 
| 134 |  |  | extern int      mg_read(void);          /* read next line */ | 
| 135 |  |  | extern int      mg_parse(void);         /* parse current line */ | 
| 136 |  |  | extern void     mg_fgetpos(MG_FPOS *);  /* get position on input file */ | 
| 137 |  |  | extern int      mg_fgoto(MG_FPOS *);    /* go to position on input file */ | 
| 138 |  |  | extern void     mg_close(void);         /* close input file */ | 
| 139 |  |  | extern void     mg_clear(void);         /* clear parser */ | 
| 140 |  |  | extern int      mg_handle(int, int, char **);   /* handle an entity */ | 
| 141 |  |  |  | 
| 142 |  |  | #ifndef MG_NQCD | 
| 143 |  |  | #define MG_NQCD         5               /* default number of divisions */ | 
| 144 |  |  | #endif | 
| 145 |  |  |  | 
| 146 |  |  | extern int      mg_nqcdivs;             /* divisions per quarter circle */ | 
| 147 |  |  |  | 
| 148 |  |  | /* | 
| 149 |  |  | * The following library routines are included for your convenience: | 
| 150 |  |  | */ | 
| 151 |  |  |  | 
| 152 |  |  | extern int mg_entity(char *);           /* get entity number from its name */ | 
| 153 |  |  | extern int isint(char *);               /* non-zero if integer format */ | 
| 154 |  |  | extern int isintd(char *, char *);      /* same with delimiter set */ | 
| 155 |  |  | extern int isflt(char *);               /* non-zero if floating point format */ | 
| 156 |  |  | extern int isfltd(char *, char *);      /* same with delimiter set */ | 
| 157 |  |  | extern int isname(char *);              /* non-zero if legal identifier name */ | 
| 158 |  |  | extern int badarg(int, char **, char *);/* check argument format */ | 
| 159 |  |  | extern int e_include(int, char **);     /* expand include entity */ | 
| 160 |  |  | extern int e_pipe(int, char **);        /* expand piped command */ | 
| 161 |  |  | extern int e_sph(int, char **);         /* expand sphere as other entities */ | 
| 162 |  |  | extern int e_torus(int, char **);       /* expand torus as other entities */ | 
| 163 |  |  | extern int e_cyl(int, char **);         /* expand cylinder as other entities */ | 
| 164 |  |  | extern int e_ring(int, char **);        /* expand ring as other entities */ | 
| 165 |  |  | extern int e_cone(int, char **);        /* expand cone as other entities */ | 
| 166 |  |  | extern int e_prism(int, char **);       /* expand prism as other entities */ | 
| 167 |  |  | extern int e_faceh(int, char **);       /* expand face w/ holes as face */ | 
| 168 |  |  |  | 
| 169 |  |  | /************************************************************************ | 
| 170 |  |  | *      Definitions for 3-d vector manipulation functions | 
| 171 |  |  | */ | 
| 172 |  |  |  | 
| 173 |  |  | #define is0vect(v)      (DOT(v,v) <= FTINY*FTINY) | 
| 174 |  |  |  | 
| 175 |  |  | #define round0(x)       if (x <= FTINY && x >= -FTINY) x = 0 | 
| 176 |  |  |  | 
| 177 |  |  | /************************************************************************ | 
| 178 |  |  | *      Definitions for context handling routines | 
| 179 |  |  | *      (materials, colors, vectors) | 
| 180 |  |  | */ | 
| 181 |  |  |  | 
| 182 |  |  | #define C_1SIDEDTHICK   0.005           /* assumed thickness of 1-sided mat. */ | 
| 183 |  |  |  | 
| 184 |  |  | typedef struct { | 
| 185 |  |  | int     clock;          /* incremented each change -- resettable */ | 
| 186 |  |  | void    *client_data;   /* pointer to private client-owned data */ | 
| 187 |  |  | int     sided;          /* 1 if surface is 1-sided, 0 for 2-sided */ | 
| 188 |  |  | float   nr, ni;         /* index of refraction, real and imaginary */ | 
| 189 |  |  | float   rd;             /* diffuse reflectance */ | 
| 190 |  |  | C_COLOR rd_c;           /* diffuse reflectance color */ | 
| 191 |  |  | float   td;             /* diffuse transmittance */ | 
| 192 |  |  | C_COLOR td_c;           /* diffuse transmittance color */ | 
| 193 |  |  | float   ed;             /* diffuse emittance */ | 
| 194 |  |  | C_COLOR ed_c;           /* diffuse emittance color */ | 
| 195 |  |  | float   rs;             /* specular reflectance */ | 
| 196 |  |  | C_COLOR rs_c;           /* specular reflectance color */ | 
| 197 |  |  | float   rs_a;           /* specular reflectance roughness */ | 
| 198 |  |  | float   ts;             /* specular transmittance */ | 
| 199 |  |  | C_COLOR ts_c;           /* specular transmittance color */ | 
| 200 |  |  | float   ts_a;           /* specular transmittance roughness */ | 
| 201 |  |  | } C_MATERIAL;           /* material context */ | 
| 202 |  |  |  | 
| 203 |  |  | typedef struct { | 
| 204 |  |  | int     clock;          /* incremented each change -- resettable */ | 
| 205 |  |  | void    *client_data;   /* pointer to private client-owned data */ | 
| 206 |  |  | FVECT   p, n;           /* point and normal */ | 
| 207 |  |  | } C_VERTEX;             /* vertex context */ | 
| 208 |  |  |  | 
| 209 |  |  | #define C_DEFMATERIAL   {1,NULL,0,1.,0.,0.,C_DEFCOLOR,0.,C_DEFCOLOR,0.,\ | 
| 210 |  |  | C_DEFCOLOR,0.,C_DEFCOLOR,0.,0.,C_DEFCOLOR,0.} | 
| 211 |  |  | #define C_DEFVERTEX     {1,NULL,{0.,0.,0.},{0.,0.,0.}} | 
| 212 |  |  |  | 
| 213 |  |  | extern C_COLOR          *c_ccolor;      /* the current color */ | 
| 214 |  |  | extern char             *c_ccname;      /* current color name */ | 
| 215 |  |  | extern C_MATERIAL       *c_cmaterial;   /* the current material */ | 
| 216 |  |  | extern char             *c_cmname;      /* current material name */ | 
| 217 |  |  | extern C_VERTEX         *c_cvertex;     /* the current vertex */ | 
| 218 |  |  | extern char             *c_cvname;      /* current vertex name */ | 
| 219 |  |  |  | 
| 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_MATERIAL       *c_getmaterial(char *); /* get a named material */ | 
| 225 |  |  | extern C_VERTEX *c_getvert(char *);             /* get a named vertex */ | 
| 226 |  |  | extern C_COLOR  *c_getcolor(char *);            /* get a named color */ | 
| 227 |  |  |  | 
| 228 |  |  | /************************************************************************* | 
| 229 |  |  | *      Definitions for hierarchical object name handler | 
| 230 |  |  | */ | 
| 231 |  |  |  | 
| 232 |  |  | extern int      obj_nnames;             /* depth of name hierarchy */ | 
| 233 |  |  | extern char     **obj_name;             /* names in hierarchy */ | 
| 234 |  |  |  | 
| 235 |  |  | extern int      obj_handler(int, char **);      /* handle an object entity */ | 
| 236 |  |  | extern void     obj_clear(void);                /* clear object stack */ | 
| 237 |  |  |  | 
| 238 |  |  | /************************************************************************** | 
| 239 |  |  | *      Definitions for hierarchical transformation handler | 
| 240 |  |  | */ | 
| 241 |  |  |  | 
| 242 |  |  | #define identxf(xp)             (void)(setident4((xp)->xfm),(xp)->sca=1.0) | 
| 243 |  |  |  | 
| 244 |  |  | #define XF_MAXDIM       8               /* maximum array dimensions */ | 
| 245 |  |  |  | 
| 246 |  |  | struct xf_array { | 
| 247 |  |  | MG_FPOS spos;                   /* starting position on input */ | 
| 248 |  |  | int     ndim;                   /* number of array dimensions */ | 
| 249 |  |  | struct { | 
| 250 |  |  | short   i, n;           /* current count and maximum */ | 
| 251 |  |  | char    arg[8];         /* string argument value */ | 
| 252 |  |  | } aarg[XF_MAXDIM]; | 
| 253 |  |  | }; | 
| 254 |  |  |  | 
| 255 |  |  | typedef struct xf_spec { | 
| 256 |  |  | long    xid;                    /* unique transform id */ | 
| 257 |  |  | short   xac;                    /* context argument count */ | 
| 258 |  |  | short   rev;                    /* boolean true if vertices reversed */ | 
| 259 |  |  | XF      xf;                     /* cumulative transformation */ | 
| 260 |  |  | struct xf_array *xarr;          /* transformation array pointer */ | 
| 261 |  |  | struct xf_spec  *prev;          /* previous transformation context */ | 
| 262 |  |  | } XF_SPEC;                      /* followed by argument buffer */ | 
| 263 |  |  |  | 
| 264 |  |  | extern XF_SPEC  *xf_context;                    /* current transform context */ | 
| 265 |  |  | extern char     **xf_argend;                    /* last transform argument */ | 
| 266 |  |  |  | 
| 267 |  |  | #define xf_ac(xf)       ((xf)==NULL ? 0 : (xf)->xac) | 
| 268 |  |  | #define xf_av(xf)       (xf_argend - (xf)->xac) | 
| 269 |  |  |  | 
| 270 |  |  | #define xf_argc         xf_ac(xf_context) | 
| 271 |  |  | #define xf_argv         xf_av(xf_context) | 
| 272 |  |  |  | 
| 273 |  |  | /* | 
| 274 |  |  | * The transformation handler should do most of the work that needs | 
| 275 |  |  | * doing.  Just pass it any xf entities, then use the associated | 
| 276 |  |  | * functions to transform and translate points, transform vectors | 
| 277 |  |  | * (without translation), rotate vectors (without scaling) and scale | 
| 278 |  |  | * values appropriately. | 
| 279 |  |  | * | 
| 280 |  |  | * The routines xf_xfmpoint, xf_xfmvect and xf_rotvect take two | 
| 281 |  |  | * 3-D vectors (which may be identical), transforms the second and | 
| 282 |  |  | * puts the result into the first. | 
| 283 |  |  | */ | 
| 284 |  |  |  | 
| 285 |  |  |  | 
| 286 |  |  | extern int      xf_handler(int, char **);       /* handle xf entity */ | 
| 287 |  |  | extern void     xf_xfmpoint(FVECT, FVECT);      /* transform point */ | 
| 288 |  |  | extern void     xf_xfmvect(FVECT, FVECT);       /* transform vector */ | 
| 289 |  |  | extern void     xf_rotvect(FVECT, FVECT);       /* rotate vector */ | 
| 290 |  |  | extern double   xf_scale(double);               /* scale a value */ | 
| 291 |  |  | extern void     xf_clear(void);                 /* clear xf stack */ | 
| 292 |  |  |  | 
| 293 |  |  | /* The following are support routines you probably won't call directly */ | 
| 294 |  |  |  | 
| 295 |  |  | XF_SPEC         *new_xf(int, char **);          /* allocate new transform */ | 
| 296 |  |  | void            free_xf(XF_SPEC *);             /* free a transform */ | 
| 297 |  |  | int             xf_aname(struct xf_array *);    /* name this instance */ | 
| 298 |  |  | long            comp_xfid(MAT4);                /* compute unique ID */ | 
| 299 |  |  | extern void     multmat4(MAT4, MAT4, MAT4);     /* m4a = m4b X m4c */ | 
| 300 |  |  | extern void     multv3(FVECT, FVECT, MAT4);     /* v3a = v3b X m4 (vectors) */ | 
| 301 |  |  | extern void     multp3(FVECT, FVECT, MAT4);     /* p3a = p3b X m4 (points) */ | 
| 302 |  |  | extern int      xf(XF *, int, char **);         /* interpret transform spec. */ | 
| 303 |  |  |  | 
| 304 |  |  | /* cvrgb.c */ | 
| 305 |  |  | extern void mgf2rgb(C_COLOR *cin, double intensity, float cout[3]); | 
| 306 |  |  |  | 
| 307 |  |  | /************************************************************************ | 
| 308 |  |  | *      Miscellaneous definitions | 
| 309 |  |  | */ | 
| 310 |  |  |  | 
| 311 |  |  | #ifdef __cplusplus | 
| 312 |  |  | } | 
| 313 |  |  | #endif | 
| 314 |  |  | #endif /* _MGF_PARSER_H_ */ | 
| 315 |  |  |  |