aboutsummaryrefslogtreecommitdiff
path: root/generic/Geo.h
blob: c0ad55af10e15482fa1e550b31d8726dd772fbf1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * Geo.h -- Header for common geometric routines.
 *
 * Authors		: Patrick Lecoanet.
 * Creation date	:
 *
 * $Id$
 */

/*
 *  Copyright (c) 1993 - 1999 CENA, Patrick Lecoanet --
 *
 * This code is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this code; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */


#ifndef _Geo_h
#define _Geo_h


#include "Attrs.h"
#include "List.h"

#include <math.h>
#include <limits.h>


#ifndef M_PI
#define M_PI		3.14159265358979323846264338327
#endif
#ifndef M_PI_2
#define M_PI_2		1.57079632679489661923
#endif
#ifndef M_PI_4
#define M_PI_4		0.78539816339744830962
#endif

#define PRECISION_LIMIT		1.0e-10
#define X_PRECISION_LIMIT	5.0e-2
#define ZN_LINE_END_POINTS	6

/*
 * Constants used to specify circle approximation quality.
 */
#define ZN_CIRCLE_COARSE 0
#define ZN_CIRCLE_MEDIUM ZN_CIRCLE_COARSE+1
#define ZN_CIRCLE_FINE ZN_CIRCLE_MEDIUM+1
#define ZN_CIRCLE_FINER ZN_CIRCLE_FINE+1
#define ZN_CIRCLE_FINEST ZN_CIRCLE_FINER+1
  

typedef struct {
  float	x, y;
} ZnPoint;

typedef struct {
  float	x, y, w, h;
} ZnRect;

/*
 * ZnBBox: orig is into the area while corner is not.
 * Thus the test: ((bbox.orig.x == bbox.corner.x) ||
 *                 (bbox.orig.y == bbox.corner.y))
 * tells whether the bbox is empty or not.
 * When interpreting bboxes the X coordinate system is
 * the norm. x goes from left toward the right and y
 * goes from the top toward the bottom. Bboxes are
 * always axes aligned.
 */
typedef struct {
  ZnPoint	orig, corner;
} ZnBBox;

typedef struct {
  unsigned int	num_points;
  ZnPoint	*points;
  char		*controls;
  ZnBool	cw;
} ZnContour;

/*
 * contour1 can be used to store a single contour
 * without having to alloc the contours array.
 */
typedef struct {
  unsigned int	num_contours;
  ZnContour	*contours;
  ZnContour	contour1;
} ZnPoly;

/*
 * Keep this enum in sync with op_strings in Contour()
 * in tkZinc.c.
 */
typedef enum {
  ZN_CONTOUR_ADD, ZN_CONTOUR_REMOVE
} ZnContourCmd;

typedef struct {
  unsigned int	num_points;
  ZnPoint	*points;
  ZnBool	fan;	/* When using a fan, all contour vertices must be
			 * included to describe the contour as a polygon
			 * (clipping code use that to speed up region
			 * rendering) and the center must be the first
			 * vertex. */
} ZnStrip;
  
typedef struct {
  unsigned int	num_strips;
  ZnStrip	*strips;
  ZnStrip	strip1;
} ZnTriStrip;


#ifndef MIN
#define MIN(a, b) 	((a) <= (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) 	((a) >= (b) ? (a) : (b))
#endif
#ifndef ABS
#define ABS(a) 		((a) < 0 ? -(a) : (a))
#endif

#define ZnDegRad(angle) \
  (M_PI * (double) (angle) / 180.0)
#define ZnRadDeg(angle) \
  (fmod((angle) * 180.0 / M_PI, 360.0))
#define ZnRadDeg360(angle) \
  (fmod(ZnRadDeg(angle)+360.0,360.0))

#define ZnNearestInt(d) \
  (((int) ((d) + (((d) > 0) ? 0.5 : -0.5))))

void
ZnPolyInit(ZnPoly	*poly);
void
ZnPolyContour1(ZnPoly		*poly,
	       ZnPoint		*pts,
	       unsigned int	num_pts,
	       ZnBool		cw);
void
ZnPolySet(ZnPoly	*poly1,
	  ZnPoly	*poly2);
void
ZnPolyFree(ZnPoly	*poly);
void
ZnTriStrip1(ZnTriStrip		*tristrip,
	    ZnPoint		*pts,
	    unsigned int	num_pts,
	    ZnBool		fan);
void
ZnTriFree(ZnTriStrip	*tristrip);

void
ZnAnchor2Origin(ZnPoint		*position,
		ZnDim		width,
		ZnDim		height,
		Tk_Anchor	anchor,
		ZnPoint		*origin);
void
ZnOrigin2Anchor(ZnPoint		*origin,
		ZnDim		width,
		ZnDim		height,
		Tk_Anchor	anchor,
		ZnPoint		*position);
void ZnRectOrigin2Anchor(ZnPoint *rect, Tk_Anchor anchor, ZnPoint *position);
void
ZnBBox2XRect(ZnBBox	*bbox,
	     XRectangle	*rect);
void
ZnGetStringBBox(char	*str,
		Tk_Font	font,
		ZnPos	x,
		ZnPos	y,
		ZnBBox	*str_bbox);
void
ZnResetBBox(ZnBBox *bbox);
void
ZnCopyBBox(ZnBBox *bbox_from,
	   ZnBBox *bbox_to);
void
ZnIntersectBBox(ZnBBox *bbox1,
		ZnBBox *bbox2,
		ZnBBox *bbox_inter);
ZnBool
ZnIsEmptyBBox(ZnBBox *bbox);
void
ZnAddBBoxToBBox(ZnBBox *bbox,
		ZnBBox *bbox2);
void
ZnAddPointToBBox(ZnBBox	*bbox,
		 ZnPos	px,
		 ZnPos	py);
void
ZnAddPointsToBBox(ZnBBox     *bbox,
		  ZnPoint     *points,
		  unsigned int num_points);
void
ZnAddStringToBBox(ZnBBox	*bbox,
		  char		*str,
		  Tk_Font	font,
		  ZnPos		cx,
		  ZnPos		cy);
ZnBool
ZnPointInBBox(ZnBBox	*bbox,
	      ZnPos	x,
	      ZnPos	y);

int
ZnLineInBBox(ZnPoint	*p1,
	     ZnPoint	*p2,
	     ZnBBox	*bbox);

int
ZnBBoxInBBox(ZnBBox	*bbox1,
	     ZnBBox	*bbox2);

int
ZnPolylineInBBox(ZnPoint	*points,
		 unsigned int	num_points,
		 ZnDim		width,
		 int		cap_style,
		 int		join_style,
		 ZnBBox		*bbox);

int
ZnPolygonInBBox(ZnPoint		*points,
		unsigned int	num_points,
		ZnBBox		*bbox,
		ZnBool		*area_enclosed);

int
ZnOvalInBBox(ZnPoint	*center,
	     ZnDim	width,
	     ZnDim	height,
	     ZnBBox	*bbox);

ZnBool
ZnHorizLineToArc(ZnReal	x1,
		 ZnReal	x2,
		 ZnReal	y,
		 ZnReal	rx,
		 ZnReal	ry,
		 int	start_angle,
		 int	angle_extent);

ZnBool
ZnVertLineToArc(ZnReal	x,
		ZnReal	y1,
		ZnReal	y2,
		ZnReal	rx,
		ZnReal	ry,
		int	start_angle,
		int	angle_extent);

ZnBool
ZnPointInAngle(int	start_angle,
	       int	angle_extent,
	       ZnPoint	*p);

void
ZnPointPolarToCartesian(ZnReal	heading,
			ZnReal	rho,
			ZnReal	theta,
			ZnReal	*delta_x,
			ZnReal	*delta_y);

ZnReal
ZnProjectionToAngle(ZnReal	dx,
		    ZnReal	dy);

ZnDim
ZnRectangleToPointDist(ZnBBox	*bbox,
		       ZnPoint	*p);
ZnDim ZnLineToPointDist(ZnPoint *p1, ZnPoint *p2, ZnPoint *p, ZnPoint *closest);

ZnDim
ZnPolygonToPointDist(ZnPoint		*points,
		     unsigned int	num_points,
		     ZnPoint		*p);

ZnDim
ZnPolylineToPointDist(ZnPoint		*points,
		      unsigned int	num_points,
		      ZnDim		width,
		      int		cap_style,
		      int		join_style,		   
		      ZnPoint		*p);

ZnDim
ZnOvalToPointDist(ZnPoint	*center,
		  ZnDim		width,
		  ZnDim		height,
		  ZnDim		line_width,
		  ZnPoint	*p);

void
ZnGetButtPoints(ZnPoint	*p1,
		ZnPoint	*p2,
		ZnDim	width,
		ZnBool	projecting,
		ZnPoint	*c1,
		ZnPoint	*c2);

ZnBool
ZnGetMiterPoints(ZnPoint	*p1,
		 ZnPoint	*p2,
		 ZnPoint	*p3,
		 ZnDim		width,
		 ZnPoint	*c1,
		 ZnPoint	*c2);

ZnBool
ZnIntersectLines(ZnPoint	*a1,
		 ZnPoint	*a2,
		 ZnPoint	*b1,
		 ZnPoint	*b2,
		 ZnPoint	*pi);

void
ZnShiftLine(ZnPoint	*p1,
	    ZnPoint	*p2,
	    ZnDim	dist,
	    ZnPoint	*p3,
	    ZnPoint	*p4);

void
ZnInsetPolygon(ZnPoint		*p,
	       unsigned int	num_points,
	       ZnDim		inset);

void
ZnSmoothPathWithBezier(ZnPoint		*from_points,
		       unsigned int	num_points,
		       ZnList		to_points);

void
ZnGetBezierPoints(ZnPoint	*p1,
		  ZnPoint	*c1,
		  ZnPoint	*c2,
		  ZnPoint	*p2,
		  ZnList	to_points,
		  double	eps);
void
ZnGetBezierPath(ZnList	from_points,
		ZnList	to_points);


ZnPoint *
ZnGetCirclePoints(int		type,
		  int		quality,
		  ZnReal	start_angle,
		  ZnReal	angle_extent,
		  unsigned int	*num_points,
		  ZnList	point_list);

void
ZnGetArcPath(ZnReal	start_angle,
	     ZnReal	end_angle,
	     int	type,
	     ZnList	to_points);

void
ZnFitBezier(ZnPoint	*pts,
	    unsigned int num_points,
	    ZnReal	error,
	    ZnList	controls);

ZnBool
ZnTestCCW(ZnPoint		*p,
	  unsigned int	num_points);


#endif	/* _Geo_h */