1 | /* $Id$ $Revision$ */ |
2 | /* vim:set shiftwidth=4 ts=8: */ |
3 | |
4 | /************************************************************************* |
5 | * Copyright (c) 2011 AT&T Intellectual Property |
6 | * All rights reserved. This program and the accompanying materials |
7 | * are made available under the terms of the Eclipse Public License v1.0 |
8 | * which accompanies this distribution, and is available at |
9 | * http://www.eclipse.org/legal/epl-v10.html |
10 | * |
11 | * Contributors: See CVS logs. Details at http://www.graphviz.org/ |
12 | *************************************************************************/ |
13 | |
14 | /* geometric types and macros (e.g. points and boxes) with application to, but |
15 | * no specific dependence on graphs */ |
16 | |
17 | #ifndef GV_GEOM_H |
18 | #define GV_GEOM_H |
19 | |
20 | #include "arith.h" |
21 | |
22 | #ifdef __cplusplus |
23 | extern "C" { |
24 | #endif |
25 | |
26 | typedef struct { int x, y; } point; |
27 | |
28 | typedef struct pointf_s { double x, y; } pointf; |
29 | |
30 | /* tell pathplan/pathgeom.h */ |
31 | #define HAVE_POINTF_S |
32 | |
33 | typedef struct { point LL, UR; } box; |
34 | |
35 | typedef struct { pointf LL, UR; } boxf; |
36 | |
37 | |
38 | /* true if point p is inside box b */ |
39 | #define INSIDE(p,b) (BETWEEN((b).LL.x,(p).x,(b).UR.x) && BETWEEN((b).LL.y,(p).y,(b).UR.y)) |
40 | |
41 | /* true if boxes b0 and b1 overlap */ |
42 | #define OVERLAP(b0,b1) (((b0).UR.x >= (b1).LL.x) && ((b1).UR.x >= (b0).LL.x) && ((b0).UR.y >= (b1).LL.y) && ((b1).UR.y >= (b0).LL.y)) |
43 | |
44 | /* true if box b0 completely contains b1*/ |
45 | #define CONTAINS(b0,b1) (((b0).UR.x >= (b1).UR.x) && ((b0).UR.y >= (b1).UR.y) && ((b0).LL.x <= (b1).LL.x) && ((b0).LL.y <= (b1).LL.y)) |
46 | |
47 | /* expand box b as needed to enclose point p */ |
48 | #define EXPANDBP(b, p) ((b).LL.x = MIN((b).LL.x, (p).x), (b).LL.y = MIN((b).LL.y, (p).y), (b).UR.x = MAX((b).UR.x, (p).x), (b).UR.y = MAX((b).UR.y, (p).y)) |
49 | |
50 | /* expand box b0 as needed to enclose box b1 */ |
51 | #define EXPANDBB(b0, b1) ((b0).LL.x = MIN((b0).LL.x, (b1).LL.x), (b0).LL.y = MIN((b0).LL.y, (b1).LL.y), (b0).UR.x = MAX((b0).UR.x, (b1).UR.x), (b0).UR.y = MAX((b0).UR.y, (b1).UR.y)) |
52 | |
53 | /* clip box b0 to fit box b1 */ |
54 | #define CLIPBB(b0, b1) ((b0).LL.x = MAX((b0).LL.x, (b1).LL.x), (b0).LL.y = MAX((b0).LL.y, (b1).LL.y), (b0).UR.x = MIN((b0).UR.x, (b1).UR.x), (b0).UR.y = MIN((b0).UR.y, (b1).UR.y)) |
55 | |
56 | #define LEN2(a,b) (SQR(a) + SQR(b)) |
57 | #define LEN(a,b) (sqrt(LEN2((a),(b)))) |
58 | |
59 | #define DIST2(p,q) (LEN2(((p).x - (q).x),((p).y - (q).y))) |
60 | #define DIST(p,q) (sqrt(DIST2((p),(q)))) |
61 | |
62 | #define POINTS_PER_INCH 72 |
63 | #define POINTS_PER_PC ((double)POINTS_PER_INCH / 6) |
64 | #define POINTS_PER_CM ((double)POINTS_PER_INCH * 0.393700787) |
65 | #define POINTS_PER_MM ((double)POINTS_PER_INCH * 0.0393700787) |
66 | |
67 | #define POINTS(a_inches) (ROUND((a_inches)*POINTS_PER_INCH)) |
68 | #define INCH2PS(a_inches) ((a_inches)*(double)POINTS_PER_INCH) |
69 | #define PS2INCH(a_points) ((a_points)/(double)POINTS_PER_INCH) |
70 | |
71 | #define P2PF(p,pf) ((pf).x = (p).x,(pf).y = (p).y) |
72 | #define PF2P(pf,p) ((p).x = ROUND((pf).x),(p).y = ROUND((pf).y)) |
73 | |
74 | #define B2BF(b,bf) (P2PF((b).LL,(bf).LL),P2PF((b).UR,(bf).UR)) |
75 | #define BF2B(bf,b) (PF2P((bf).LL,(b).LL),PF2P((bf).UR,(b).UR)) |
76 | |
77 | #define APPROXEQ(a,b,tol) (ABS((a) - (b)) < (tol)) |
78 | #define APPROXEQPT(p,q,tol) (DIST2((p),(q)) < SQR(tol)) |
79 | |
80 | /* some common tolerance values */ |
81 | #define MILLIPOINT .001 |
82 | #define MICROPOINT .000001 |
83 | |
84 | #ifdef __cplusplus |
85 | } |
86 | #endif |
87 | |
88 | #endif |
89 | |