1 /* geometry.h --
2  *
3  *     *********************************************************************
4  *     * Copyright (C) 1985, 1990 Regents of the University of California. *
5  *     * Permission to use, copy, modify, and distribute this              *
6  *     * software and its documentation for any purpose and without        *
7  *     * fee is hereby granted, provided that the above copyright          *
8  *     * notice appear in all copies.  The University of California        *
9  *     * makes no representations about the suitability of this            *
10  *     * software for any purpose.  It is provided "as is" without         *
11  *     * express or implied warranty.  Export of this software outside     *
12  *     * of the United States of America may require an export license.    *
13  *     *********************************************************************
14  *
15  * This module contains the basic definitions for geometrical
16  * elements:  points, rectangles, and transforms.
17  */
18 
19 /* rcsid "$Header: /usr/cvsroot/magic-8.0/utils/geometry.h,v 1.2 2009/09/10 20:32:55 tim Exp $" */
20 
21 #ifndef _GEOMETRY_H
22 #define _GEOMETRY_H 1
23 
24 /*-------------------------------------------------------------------
25  * Structure definition for Point (an x,y pair).
26  *-------------------------------------------------------------------
27  */
28 
29 typedef struct
30 {
31     int p_x;
32     int p_y;
33 } Point;
34 
35 /*-------------------------------------------------------------------
36  * Structure definition for rectangles.  A rectangle is defined
37  * by the coordinates of its lower-left and upper-right corners.
38  * Most routines that manipulate rectangles require the first
39  * point to really be the lower-left one, so be careful about this.
40  * A null rectangle is indicated by making both x-coordinates the
41  * same.
42  *-------------------------------------------------------------------
43  */
44 
45 typedef struct
46 {
47     Point r_ll;			/* Lower-left corner of rectangle. */
48     Point r_ur;			/* Upper-right corner of rectangle. */
49 } Rect;
50 
51 #define r_xbot r_ll.p_x
52 #define r_ybot r_ll.p_y
53 #define r_xtop r_ur.p_x
54 #define r_ytop r_ur.p_y
55 
56 typedef struct _linkedRect	/* A linked rectangle */
57 {
58     Rect r_r;			/* A rectangle. */
59     int  r_type;	   	/* Tile type of rectangle */
60     struct _linkedRect *r_next; /* Pointer to another linked rectangle */
61 } LinkedRect;
62 
63 /*-------------------------------------------------------------------
64  * Structure definition for geometrical transformers.  They are
65  * stored in the form described by Newman and Sproull on page 57.
66  * Magic allows only 90 degree orientations, and normally there
67  * is no scaling (scaling only occurs when transforming to pixel
68  * coordinates).  Thus the elements a, b, d, and e always have
69  * one of the following forms, where S is the scaling factor:
70  *
71  *  S  0    0 -S    -S  0    0  S    S  0    0  S    -S  0    0 -S
72  *  0  S    S  0     0 -S   -S  0    0 -S    S  0     0  S   -S  0
73  *
74  * The first four forms correspond to clockwise rotations of 0, 90,
75  * 180, and 270 degrees, and the second four correspond to the same
76  * four orientations flipped upside down (mirror across the x-axis
77  * after rotating).
78  *-------------------------------------------------------------------
79  */
80 
81 typedef struct
82 {
83     int t_a, t_b, t_c, t_d, t_e, t_f;
84 } Transform;
85 
86 /*-------------------------------------------------------------------
87  *	Definitions for positions.  Positions are small integers
88  *	used to select where text gets placed, relative to a point.
89  *-------------------------------------------------------------------
90  */
91 
92 #define GEO_CENTER	0
93 #define GEO_NORTH	1
94 #define GEO_NORTHEAST	2
95 #define GEO_EAST	3
96 #define GEO_SOUTHEAST	4
97 #define GEO_SOUTH	5
98 #define GEO_SOUTHWEST	6
99 #define GEO_WEST	7
100 #define GEO_NORTHWEST	8
101 
102 
103 /* See if two points are equal */
104 #define GEO_SAMEPOINT(p1, p2) ((p1).p_x == (p2).p_x && (p1).p_y == (p2).p_y)
105 
106 /* See if two rects are equal */
107 #define	GEO_SAMERECT(r1, r2) \
108     (GEO_SAMEPOINT((r1).r_ll, (r2).r_ll) && GEO_SAMEPOINT((r1).r_ur, (r2).r_ur))
109 
110 /*-------------------------------------------------------------------
111  *	The following macros are predicates to see if two
112  *	rectangles overlap or touch.
113  *-------------------------------------------------------------------
114  */
115 
116 /* see if the rectangles overlap (the overlap contains some area) */
117 
118 #define GEO_OVERLAP(r1, r2) \
119     (((r1)->r_xbot < (r2)->r_xtop) && ((r2)->r_xbot < (r1)->r_xtop) \
120     && ((r1)->r_ybot < (r2)->r_ytop) && ((r2)->r_ybot < (r1)->r_ytop))
121 
122 /* see if the rectangles touch (share part of a side) or overlap */
123 
124 #define GEO_TOUCH(r1, r2) \
125     (((r1)->r_xbot <= (r2)->r_xtop) && ((r2)->r_xbot <= (r1)->r_xtop) \
126     && ((r1)->r_ybot <= (r2)->r_ytop) && ((r2)->r_ybot <= (r1)->r_ytop))
127 
128 /* see if rectangle r1 completely surrounds rectangle r2.  Touching between
129  * r2 and r1 IS allowed.
130  */
131 
132 #define GEO_SURROUND(r1,r2) \
133     ( ((r2)->r_xbot >= (r1)->r_xbot) && ((r2)->r_xtop <= (r1)->r_xtop) \
134     && ((r2)->r_ybot >= (r1)->r_ybot) && ((r2)->r_ytop <= (r1)->r_ytop) )
135 
136 /* see if rectangle r1 completely surrounds rectangle r2 WITHOUT touching it.
137  */
138 
139 #define GEO_SURROUND_STRONG(r1,r2) \
140     ( ((r2)->r_xbot > (r1)->r_xbot) && ((r2)->r_xtop < (r1)->r_xtop) \
141     && ((r2)->r_ybot > (r1)->r_ybot) && ((r2)->r_ytop < (r1)->r_ytop) )
142 
143 /* See if point p in inside of or on the border of r */
144 #define GEO_ENCLOSE(p, r)	\
145     ( ((p)->p_x <= (r)->r_xtop) && ((p)->p_x >= (r)->r_xbot) &&  \
146       ((p)->p_y <= (r)->r_ytop) && ((p)->p_y >= (r)->r_ybot) )
147 
148 /* See if a label is in a given area */
149 #define GEO_LABEL_IN_AREA(lab,area) \
150     (GEO_SURROUND(area, lab) || \
151 	(GEO_RECTNULL(area) && GEO_TOUCH(lab, area) && \
152 	!(GEO_SURROUND_STRONG(lab, area))))
153 
154 /* See if a rectangle has no area. */
155 
156 #define GEO_RECTNULL(r) \
157     (((r)->r_xbot >= (r)->r_xtop) || ((r)->r_ybot >= (r)->r_ytop))
158 
159 /* Expand a rectangular area by a given amount. */
160 
161 #define GEO_EXPAND(src, amount, dst) \
162 { \
163     (dst)->r_xbot = (src)->r_xbot - amount; \
164     (dst)->r_ybot = (src)->r_ybot - amount; \
165     (dst)->r_xtop = (src)->r_xtop + amount; \
166     (dst)->r_ytop = (src)->r_ytop + amount; \
167 }
168 
169 /* Sizes of rectangles. */
170 #define GEO_WIDTH(r)    ((r)->r_xtop - (r)->r_xbot)
171 #define GEO_HEIGHT(r)   ((r)->r_ytop - (r)->r_ybot)
172 
173 
174 /*-------------------------------------------------------------------
175  *	Declarations for exported procedures:
176  *-------------------------------------------------------------------
177  */
178 
179 extern int GeoNameToPos(char *, bool, bool);
180 extern char * GeoPosToName(int);
181 extern void GeoTransRect(Transform *, Rect *, Rect *), GeoTransTrans(Transform *, Transform *, Transform *);
182 extern void GeoTransPoint(Transform *, Point *, Point *), GeoInvertTrans(Transform *, Transform *);
183 extern void GeoTranslateTrans(Transform *, int, int, Transform *);
184 extern void GeoTransTranslate(int, int, Transform *, Transform *);
185 extern int GeoTransPos(Transform *, int);
186 extern bool GeoInclude(Rect *, Rect *), GeoIncludeAll(Rect *, Rect *);
187 extern void GeoClip(Rect *, Rect *);
188 extern void GeoClipPoint(Point *, Rect *);
189 extern int GeoRectPointSide(Rect *, Point *);
190 extern int GeoRectRectSide(Rect *, Rect *);
191 extern void GeoIncludePoint(Point *, Rect *);
192 extern void GeoDecomposeTransform(Transform *, bool *, int *);
193 extern void GeoIncludeRectInBBox(Rect *, Rect *);
194 extern void GeoCanonicalRect(Rect *, Rect *);
195 
196 /*
197  *-------------------------------------------------------------------
198  *	Declarations of exported transforms and rectangles:
199  *-------------------------------------------------------------------
200  */
201 
202 extern Transform GeoIdentityTransform;
203 extern Transform GeoUpsideDownTransform;
204 extern Transform GeoSidewaysTransform;
205 extern Transform Geo90Transform;
206 extern Transform Geo180Transform;
207 extern Transform Geo270Transform;
208 extern Transform GeoRef45Transform;
209 extern Transform GeoRef135Transform;
210 
211 extern Rect GeoNullRect;
212 extern Rect GeoInvertedRect;
213 extern Point GeoOrigin;
214 
215 extern int GeoOppositePos[];
216 
217 #endif /* _GEOMETRY_H */
218