1*18191Sjaap /*	ideal.h	(CWI)	1.1	85/03/01	*/
2*18191Sjaap #include <stdio.h>
3*18191Sjaap #include "stdas.h"
4*18191Sjaap #include <math.h>
5*18191Sjaap 
6*18191Sjaap extern double modf();
7*18191Sjaap 
8*18191Sjaap #define TRUE	1
9*18191Sjaap #define FALSE	0
10*18191Sjaap typedef int	boolean;
11*18191Sjaap 
12*18191Sjaap #define EPSILON 0.0001
13*18191Sjaap #define	PI	3.1415926535
14*18191Sjaap #define	INFINITY	1e30
15*18191Sjaap #define INTERSIZE	20
16*18191Sjaap #define	POSSINTER	2
17*18191Sjaap 
18*18191Sjaap #define known(x) (!(((DEPPTR)x->left)->var || ((DEPPTR)x->right)->var))
19*18191Sjaap #define fabs(z)	((z>0)?z:-(z))
20*18191Sjaap #define	iabs(z)	((z>0)?z:-(z))
21*18191Sjaap #define max(x,y) (((x)>(y))?(x):(y))
22*18191Sjaap #define min(x,y) (((x)<(y))?(x):(y))
23*18191Sjaap #define Re(z)	((DEPPTR)z->left)->coeff
24*18191Sjaap #define Im(z)	((DEPPTR)z->right)->coeff
25*18191Sjaap #define	ISREAL(z)	(z->re_name > 0)
26*18191Sjaap #define	THENAME(z)	(iabs(z->re_name))
27*18191Sjaap #define	arecollinear(a,b,c,d,e,f)	(fabs((f-b)*(c-a)-(e-a)*(d-b))<EPSILON)
28*18191Sjaap #define	between(ax,ay,bx,by,cx,cy)	((ax-bx)*(bx-cx) > 0 || (ay-by)*(by-cy) > 0)
29*18191Sjaap 
30*18191Sjaap extern int when_bug;
31*18191Sjaap extern boolean dbg;
32*18191Sjaap #define bug_on	dbg = TRUE
33*18191Sjaap #define bug_off	dbg = FALSE
34*18191Sjaap #define dprintf	if (dbg) fprintf(stderr,
35*18191Sjaap 
36*18191Sjaap extern char *filename;
37*18191Sjaap extern int lineno;
38*18191Sjaap #define	LIBFIL	1
39*18191Sjaap #define	SILENT	2
40*18191Sjaap #define	CHATTY	3
41*18191Sjaap 
42*18191Sjaap extern boolean radflag;
43*18191Sjaap extern boolean wantout;
44*18191Sjaap #define dtor(x)	x *= PI/180
45*18191Sjaap #define rtod(x)	x *= 180/PI
46*18191Sjaap 
47*18191Sjaap /* these are codes for classification of intersection points */
48*18191Sjaap #define	UNUSED		0
49*18191Sjaap #define	SIMPLE		1
50*18191Sjaap #define	AT0		2
51*18191Sjaap #define	AT1		3
52*18191Sjaap #define	COLLINEAR	4
53*18191Sjaap #define	ON0		5
54*18191Sjaap #define	ON1		6
55*18191Sjaap #define	TANGENT		7
56*18191Sjaap 
57*18191Sjaap /* these are codes for setting up the list of intersections */
58*18191Sjaap #define	INHERIT		0
59*18191Sjaap /*
60*18191Sjaap #define	SIMPLE		1
61*18191Sjaap */
62*18191Sjaap #define	EXTREMUM	2
63*18191Sjaap #define	INFLECTION	3
64*18191Sjaap #define	EXT0		4
65*18191Sjaap #define	EXT1		5
66*18191Sjaap #define	INFL0		6
67*18191Sjaap #define	INFL1		7
68*18191Sjaap #define	IGNORE		8
69*18191Sjaap 
70*18191Sjaap #define	INBEGIN		0
71*18191Sjaap #define	OUTBEGIN	1
72*18191Sjaap #define	ONBEGIN		2
73*18191Sjaap 
74*18191Sjaap /* structure definitions for data structures */
75*18191Sjaap typedef char *EXPR;
76*18191Sjaap #define	tryfree(doomed)	free((char *)doomed)
77*18191Sjaap 
78*18191Sjaap typedef struct stmtnode {	/* hooks together stmts in bodies */
79*18191Sjaap 	struct stmtnode *next;
80*18191Sjaap 	EXPR stmt;
81*18191Sjaap 	int kind;
82*18191Sjaap 	} STMTNODE, *STMTPTR;
83*18191Sjaap 
84*18191Sjaap typedef struct boxnode {	/* hooks together box definitions */
85*18191Sjaap 	struct boxnode *next;
86*18191Sjaap 	int name;
87*18191Sjaap 	STMTPTR stmtlist;
88*18191Sjaap 	} BOXNODE, *BOXPTR;
89*18191Sjaap 
90*18191Sjaap typedef struct namenode {	/* holds var lists and path names */
91*18191Sjaap 	struct namenode *next;
92*18191Sjaap 	int name;
93*18191Sjaap 	} NAMENODE, *NAMEPTR;
94*18191Sjaap 
95*18191Sjaap typedef struct exprnode {	/* points to equations and bdlists */
96*18191Sjaap 	struct exprnode *next;
97*18191Sjaap 	EXPR expr;
98*18191Sjaap 	} EXPRNODE, *EXPRPTR;
99*18191Sjaap 
100*18191Sjaap typedef struct putnode {	/* put statements */
101*18191Sjaap 	int name;
102*18191Sjaap 	BOXPTR parm;
103*18191Sjaap 	int p_or_c;
104*18191Sjaap 	} PUTNODE, *PUTPTR;
105*18191Sjaap 
106*18191Sjaap typedef struct pen_node {	/* conn ... using statements */
107*18191Sjaap 	EXPR from,
108*18191Sjaap 		to,
109*18191Sjaap 		copies,
110*18191Sjaap 		start,
111*18191Sjaap 		end;
112*18191Sjaap 	BOXPTR pen;
113*18191Sjaap 	} PEN_NODE, *PENPTR;
114*18191Sjaap 
115*18191Sjaap typedef struct miscnode {	/* opaque, draw handling */
116*18191Sjaap 	int info;
117*18191Sjaap 	} MISCNODE, *MISCPTR;
118*18191Sjaap 
119*18191Sjaap typedef struct strnode {	/* strings */
120*18191Sjaap 	int command;
121*18191Sjaap 	char *string;
122*18191Sjaap 	EXPR at;
123*18191Sjaap 	} STRNODE, *STRPTR;
124*18191Sjaap 
125*18191Sjaap typedef struct exprintl {	/* internal node of expr tree */
126*18191Sjaap 	boolean leaf;	/* always FALSE */
127*18191Sjaap 	int oper;
128*18191Sjaap 	EXPR left;
129*18191Sjaap 	EXPR right;
130*18191Sjaap 	} EXPRINTL, *INTLPTR;
131*18191Sjaap 
132*18191Sjaap typedef struct exprextl {	/* external node of expr tree */
133*18191Sjaap 	boolean leaf;	/* always TRUE */
134*18191Sjaap 	union u {
135*18191Sjaap 		struct namenode *path;
136*18191Sjaap 		float const;
137*18191Sjaap 	} info;
138*18191Sjaap 	int kind;	/* should be one of PATH, CONST */
139*18191Sjaap 	} EXPREXTL, *EXTLPTR;
140*18191Sjaap 
141*18191Sjaap typedef struct noad {	/* linked structures in which variables reside */
142*18191Sjaap 	PUTPTR defnode;
143*18191Sjaap 	struct varnode *edgevarlist;
144*18191Sjaap 	struct varnode *boxvarlist;
145*18191Sjaap 	struct linenode *linelist;
146*18191Sjaap 	struct noad *father;
147*18191Sjaap 	struct noad *brother;
148*18191Sjaap 	struct noad *son;
149*18191Sjaap 	} NOAD, *NOADPTR;
150*18191Sjaap 
151*18191Sjaap typedef struct varnode {	/* where ONE variable--the real or imag part of a var--lives */
152*18191Sjaap 	struct varnode *next;
153*18191Sjaap 	int re_name;	/* positive for real part, negative for imag part */
154*18191Sjaap 	struct depnode *deplist;
155*18191Sjaap 	} VARNODE, *VARPTR;
156*18191Sjaap 
157*18191Sjaap typedef struct depnode {	/* a term in the dependency list representation of a variable */
158*18191Sjaap 	struct depnode *next;
159*18191Sjaap 	VARPTR var;
160*18191Sjaap 	float coeff;
161*18191Sjaap 	} DEPNODE, *DEPPTR;
162*18191Sjaap 
163*18191Sjaap typedef struct linenode {	/* a line segment on linelist */
164*18191Sjaap 	struct linenode *next;
165*18191Sjaap 	int kind;	/* always LINE */
166*18191Sjaap 	float x0,
167*18191Sjaap 		y0,
168*18191Sjaap 		x1,
169*18191Sjaap 		y1;
170*18191Sjaap 	} LINENODE, *LINEPTR;
171*18191Sjaap 
172*18191Sjaap typedef struct edgenode {	/* an edge of an opaque polygon */
173*18191Sjaap 	struct edgenode *next,
174*18191Sjaap 		*prev;
175*18191Sjaap 	struct arcnode *fax;
176*18191Sjaap 	boolean flipped;
177*18191Sjaap 	float sx, sy,		/* start point */
178*18191Sjaap 		ex, ey,		/* end point */
179*18191Sjaap 		stx, sty,	/* coords of endpt of a tan vector at start */
180*18191Sjaap 		etx, ety;	/* coords of endpt of a tan vector at end */
181*18191Sjaap 	int code[POSSINTER];
182*18191Sjaap 	float alpha[POSSINTER];
183*18191Sjaap 	} EDGENODE, *EDGEPTR;
184*18191Sjaap 
185*18191Sjaap typedef struct circnode {	/* a circle on linelist */
186*18191Sjaap 	struct linenode *next;
187*18191Sjaap 	int kind;	/* always CIRCLE */
188*18191Sjaap 	float x0,
189*18191Sjaap 		y0,
190*18191Sjaap 		r;
191*18191Sjaap 	} CIRCNODE, *CIRCPTR;
192*18191Sjaap 
193*18191Sjaap typedef struct arcnode {	/* an arc on linelist */
194*18191Sjaap 	struct linenode *next;
195*18191Sjaap 	int kind;	/* always ARC */
196*18191Sjaap 	float x0,
197*18191Sjaap 		y0,
198*18191Sjaap 		x1,
199*18191Sjaap 		y1,
200*18191Sjaap 		x2,
201*18191Sjaap 		y2,
202*18191Sjaap 		theta1,
203*18191Sjaap 		theta2,
204*18191Sjaap 		radius;	/* TROFF figures out the center depending on sign of radius */
205*18191Sjaap 	} ARCNODE, *ARCPTR;
206*18191Sjaap 
207*18191Sjaap typedef struct textnode {	/* a string on linelist */
208*18191Sjaap 	struct linenode *next;
209*18191Sjaap 	int kind,	/* always STRING */
210*18191Sjaap 		command;
211*18191Sjaap 	char *string;
212*18191Sjaap 	float x0,
213*18191Sjaap 		y0;
214*18191Sjaap 	} TEXTNODE, *TEXTPTR;
215*18191Sjaap 
216*18191Sjaap typedef struct splnode {	/* a spline on linelist */
217*18191Sjaap 	struct linenode *next;
218*18191Sjaap 	int kind;	/* always SPLINE */
219*18191Sjaap 	EXPRPTR knotlist;
220*18191Sjaap 	} SPLNODE, *SPLPTR;
221*18191Sjaap 
222*18191Sjaap typedef struct eqnnode {	/* a non-linear equation residing on list */
223*18191Sjaap 	struct eqnnode *next;
224*18191Sjaap 	EXPR eqn;
225*18191Sjaap 	NOADPTR noad;
226*18191Sjaap 	} EQNNODE, *EQNPTR;
227*18191Sjaap 
228*18191Sjaap typedef struct opqnode {	/* an alpha or theta of intersection */
229*18191Sjaap 	struct opqnode *next;
230*18191Sjaap 	int code;
231*18191Sjaap 	float alpha;
232*18191Sjaap 	} OPQNODE, *OPQPTR;
233*18191Sjaap 
234*18191Sjaap /* routines in memut.c */
235*18191Sjaap extern STMTPTR stmtgen ();
236*18191Sjaap extern BOXPTR boxgen ();
237*18191Sjaap extern NAMEPTR namegen ();
238*18191Sjaap extern EXPRPTR exprgen ();
239*18191Sjaap extern PUTPTR putgen ();
240*18191Sjaap extern PENPTR pengen ();
241*18191Sjaap extern MISCPTR miscgen ();
242*18191Sjaap extern INTLPTR intlgen ();
243*18191Sjaap extern INTLPTR commagen ();
244*18191Sjaap extern EXTLPTR extlgen ();
245*18191Sjaap extern EXTLPTR fextlgen ();
246*18191Sjaap extern NOADPTR noadgen ();
247*18191Sjaap extern VARPTR vargen ();
248*18191Sjaap extern DEPPTR depgen ();
249*18191Sjaap extern LINEPTR linegen ();
250*18191Sjaap extern EDGEPTR edgeline ();
251*18191Sjaap extern LINEPTR circgen ();
252*18191Sjaap extern LINEPTR arcgen ();
253*18191Sjaap extern LINEPTR angularc ();
254*18191Sjaap extern LINEPTR pointarc ();
255*18191Sjaap extern EDGEPTR edgearc ();
256*18191Sjaap extern LINEPTR textgen ();
257*18191Sjaap extern LINEPTR splgen ();
258*18191Sjaap extern STRPTR strgen ();
259*18191Sjaap extern EQNPTR eqngen ();
260*18191Sjaap extern OPQPTR opqgen ();
261*18191Sjaap extern void nextfree ();
262*18191Sjaap extern void depfree ();
263*18191Sjaap extern void namefree ();
264*18191Sjaap extern void exprlsfree ();
265*18191Sjaap extern void linefree ();
266*18191Sjaap extern void intlfree ();
267*18191Sjaap extern void noadfree ();
268*18191Sjaap extern void varfree ();
269*18191Sjaap extern void exprfree ();
270*18191Sjaap extern void boxfree ();
271*18191Sjaap extern void emergency ();
272*18191Sjaap 
273*18191Sjaap /* routines in util.c */
274*18191Sjaap extern int lookup();
275*18191Sjaap extern char* idprint();
276*18191Sjaap extern BOXPTR findbox();
277*18191Sjaap extern INTLPTR varfind();
278*18191Sjaap extern INTLPTR pathfind();
279*18191Sjaap extern BOXPTR tail ();
280*18191Sjaap extern void forget ();
281*18191Sjaap extern void exprprint ();
282*18191Sjaap extern STMTPTR nextstmt ();
283*18191Sjaap extern EXPR bracket ();
284*18191Sjaap extern void depprint ();
285*18191Sjaap extern void dexch();
286*18191Sjaap extern void fexch();
287*18191Sjaap extern float rprin();
288*18191Sjaap extern float dprin();
289*18191Sjaap extern void angorder();
290*18191Sjaap extern STMTPTR reverse ();
291*18191Sjaap extern void impossible ();
292*18191Sjaap 
293*18191Sjaap /* routines in bldds.c */
294*18191Sjaap extern NOADPTR buildnoadtree();
295*18191Sjaap extern VARPTR buildvarlist();
296*18191Sjaap extern NOADPTR walkputlist();
297*18191Sjaap 
298*18191Sjaap /* routines in simul.c */
299*18191Sjaap extern DEPPTR depadd();
300*18191Sjaap extern DEPPTR depsubst();
301*18191Sjaap 
302*18191Sjaap /* routines in exprn.c */
303*18191Sjaap extern INTLPTR expreval();
304*18191Sjaap extern void eqndo();
305*18191Sjaap extern void depvarclean();
306*18191Sjaap extern void depvarkill();
307*18191Sjaap extern void eqneval();
308*18191Sjaap extern void nl_eval();
309*18191Sjaap 
310*18191Sjaap /* routines in action.c */
311*18191Sjaap extern LINEPTR build();
312*18191Sjaap extern LINEPTR connact();
313*18191Sjaap extern LINEPTR penact();
314*18191Sjaap extern LINEPTR drawact();
315*18191Sjaap extern LINEPTR stract();
316*18191Sjaap extern LINEPTR circact();
317*18191Sjaap extern LINEPTR arcact();
318*18191Sjaap extern LINEPTR splact();
319*18191Sjaap 
320*18191Sjaap /* routines in piece.c */
321*18191Sjaap extern void linecall ();
322*18191Sjaap extern void circcall ();
323*18191Sjaap extern void arccall ();
324*18191Sjaap extern void textcall ();
325*18191Sjaap extern void splcall ();
326*18191Sjaap extern void boundscall ();
327*18191Sjaap 
328*18191Sjaap /* routines in opaque.c */
329*18191Sjaap extern LINEPTR opqact();
330*18191Sjaap extern void opqinsert();
331*18191Sjaap extern LINEPTR lineclean();
332*18191Sjaap extern void halfplane();
333*18191Sjaap extern void triangle();
334*18191Sjaap 
335*18191Sjaap /* routines in inter.c */
336*18191Sjaap extern boolean llinter();
337*18191Sjaap extern boolean lcinter();
338*18191Sjaap extern boolean ccinter();
339*18191Sjaap 
340*18191Sjaap /* routines in opqpoly.c */
341*18191Sjaap extern void opqpoly();
342*18191Sjaap extern void polyline();
343*18191Sjaap extern void polyarc();
344*18191Sjaap extern void linetest();
345*18191Sjaap extern void arctest();
346*18191Sjaap 
347*18191Sjaap /* routines in opqcirc.c */
348*18191Sjaap extern void opqcirc();
349*18191Sjaap extern void circline();
350*18191Sjaap extern void circarc();
351*18191Sjaap 
352*18191Sjaap /* routines in opqsect.c */
353*18191Sjaap extern void opqsect();
354*18191Sjaap 
355*18191Sjaap /* routines in opqseg.c */
356*18191Sjaap extern void opqseg();
357*18191Sjaap 
358*18191Sjaap /* lexical analyzer routines */
359*18191Sjaap extern void filepush ();
360*18191Sjaap extern void filepop ();
361