Graphviz  2.29.20120523.0446
lib/common/geom.h
Go to the documentation of this file.
00001 /* $Id$ $Revision$ */
00002 /* vim:set shiftwidth=4 ts=8: */
00003 
00004 /*************************************************************************
00005  * Copyright (c) 2011 AT&T Intellectual Property 
00006  * All rights reserved. This program and the accompanying materials
00007  * are made available under the terms of the Eclipse Public License v1.0
00008  * which accompanies this distribution, and is available at
00009  * http://www.eclipse.org/legal/epl-v10.html
00010  *
00011  * Contributors: See CVS logs. Details at http://www.graphviz.org/
00012  *************************************************************************/
00013 
00014 /* geometric types and macros (e.g. points and boxes) with application to, but
00015  * no specific dependance on graphs */
00016 
00017 #ifndef GV_GEOM_H
00018 #define GV_GEOM_H
00019 
00020 #ifdef HAVE_CONFIG_H
00021 #include "arith.h"
00022 #endif
00023 
00024 #ifdef __cplusplus
00025 extern "C" {
00026 #endif
00027     
00028 typedef struct { int x, y; } point;
00029 
00030 typedef struct pointf_s { double x, y; } pointf;
00031 
00032 /* tell pathplan/pathgeom.h */
00033 #define HAVE_POINTF_S
00034 
00035 typedef struct { point LL, UR; } box;
00036 
00037 typedef struct { pointf LL, UR; } boxf;
00038 
00039 #ifdef HAVE_CONFIG_H
00040 
00041 /* true if point p is inside box b */
00042 #define INSIDE(p,b)     (BETWEEN((b).LL.x,(p).x,(b).UR.x) && BETWEEN((b).LL.y,(p).y,(b).UR.y))
00043 
00044 /* true if boxes b0 and b1 overlap */
00045 #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))
00046 
00047 /* true if box b0 completely contains b1*/
00048 #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))
00049 
00050 /* expand box b as needed to enclose point p */
00051 #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))
00052 
00053 /* expand box b0 as needed to enclose box b1 */
00054 #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))
00055 
00056 /* clip box b0 to fit box b1 */
00057 #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))
00058 
00059 #define LEN2(a,b)               (SQR(a) + SQR(b))
00060 #define LEN(a,b)                (sqrt(LEN2((a),(b))))
00061 
00062 #define DIST2(p,q)              (LEN2(((p).x - (q).x),((p).y - (q).y)))
00063 #define DIST(p,q)               (sqrt(DIST2((p),(q))))
00064 
00065 #define POINTS_PER_INCH 72
00066 #define POINTS_PER_PC           ((double)POINTS_PER_INCH / 6)
00067 #define POINTS_PER_CM           ((double)POINTS_PER_INCH * 0.393700787)
00068 #define POINTS_PER_MM           ((double)POINTS_PER_INCH * 0.0393700787)
00069 
00070 #define POINTS(a_inches)        (ROUND((a_inches)*POINTS_PER_INCH))
00071 #define INCH2PS(a_inches)       ((a_inches)*(double)POINTS_PER_INCH)
00072 #define PS2INCH(a_points)       ((a_points)/(double)POINTS_PER_INCH)
00073 
00074 #define P2PF(p,pf)              ((pf).x = (p).x,(pf).y = (p).y)
00075 #define PF2P(pf,p)              ((p).x = ROUND((pf).x),(p).y = ROUND((pf).y))
00076 
00077 #define B2BF(b,bf)              (P2PF((b).LL,(bf).LL),P2PF((b).UR,(bf).UR))
00078 #define BF2B(bf,b)              (PF2P((bf).LL,(b).LL),PF2P((bf).UR,(b).UR))
00079 
00080 #define APPROXEQ(a,b,tol)       (ABS((a) - (b)) < (tol))
00081 #define APPROXEQPT(p,q,tol)     (DIST2((p),(q)) < SQR(tol))
00082 
00083 /* some common tolerance values */
00084 #define MILLIPOINT .001
00085 #define MICROPOINT .000001
00086 
00087 #endif
00088 
00089 #ifdef __cplusplus
00090 }
00091 #endif
00092 
00093 #endif