| 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 functions (e.g. on points and boxes) with application to, but |
| 15 | * no specific dependence on graphs */ |
| 16 | |
| 17 | #ifndef GV_GEOMPROCS_H |
| 18 | #define GV_GEOMPROCS_H |
| 19 | |
| 20 | #ifdef __cplusplus |
| 21 | extern "C" { |
| 22 | #endif |
| 23 | |
| 24 | |
| 25 | #include "geom.h" |
| 26 | |
| 27 | #ifdef _WIN32 |
| 28 | #ifdef GVDLL |
| 29 | #define extern __declspec(dllexport) |
| 30 | #else |
| 31 | #define extern __declspec(dllimport) |
| 32 | #endif |
| 33 | #endif |
| 34 | |
| 35 | extern box mkbox(point p, point q); |
| 36 | extern boxf mkboxf(pointf p, pointf q); |
| 37 | |
| 38 | extern box flip_rec_box(box b, point p); |
| 39 | extern boxf flip_rec_boxf(boxf b, pointf p); |
| 40 | |
| 41 | extern double ptToLine2 (pointf l1, pointf l2, pointf p); |
| 42 | |
| 43 | extern int lineToBox(pointf p1, pointf p2, boxf b); |
| 44 | |
| 45 | extern point ccwrotatep(point p, int ccwrot); |
| 46 | extern pointf ccwrotatepf(pointf p, int ccwrot); |
| 47 | |
| 48 | extern point cwrotatep(point p, int cwrot); |
| 49 | extern pointf cwrotatepf(pointf p, int cwrot); |
| 50 | |
| 51 | extern void rect2poly(pointf *p); |
| 52 | |
| 53 | extern int line_intersect (pointf a, pointf b, pointf c, pointf d, pointf* p); |
| 54 | |
| 55 | #if defined(_WIN32) |
| 56 | #define inline __inline |
| 57 | #endif |
| 58 | |
| 59 | |
| 60 | static inline point pointof(int x, int y) |
| 61 | { |
| 62 | point r; |
| 63 | |
| 64 | r.x = x; |
| 65 | r.y = y; |
| 66 | return r; |
| 67 | } |
| 68 | |
| 69 | static inline pointf pointfof(double x, double y) |
| 70 | { |
| 71 | pointf r; |
| 72 | |
| 73 | r.x = x; |
| 74 | r.y = y; |
| 75 | return r; |
| 76 | } |
| 77 | |
| 78 | static inline box boxof(int llx, int lly, int urx, int ury) |
| 79 | { |
| 80 | box b; |
| 81 | |
| 82 | b.LL.x = llx, b.LL.y = lly; |
| 83 | b.UR.x = urx, b.UR.y = ury; |
| 84 | return b; |
| 85 | } |
| 86 | |
| 87 | static inline boxf boxfof(double llx, double lly, double urx, double ury) |
| 88 | { |
| 89 | boxf b; |
| 90 | |
| 91 | b.LL.x = llx, b.LL.y = lly; |
| 92 | b.UR.x = urx, b.UR.y = ury; |
| 93 | return b; |
| 94 | } |
| 95 | |
| 96 | static inline point add_point(point p, point q) |
| 97 | { |
| 98 | point r; |
| 99 | |
| 100 | r.x = p.x + q.x; |
| 101 | r.y = p.y + q.y; |
| 102 | return r; |
| 103 | } |
| 104 | |
| 105 | static inline pointf add_pointf(pointf p, pointf q) |
| 106 | { |
| 107 | pointf r; |
| 108 | |
| 109 | r.x = p.x + q.x; |
| 110 | r.y = p.y + q.y; |
| 111 | return r; |
| 112 | } |
| 113 | |
| 114 | static inline point sub_point(point p, point q) |
| 115 | { |
| 116 | point r; |
| 117 | |
| 118 | r.x = p.x - q.x; |
| 119 | r.y = p.y - q.y; |
| 120 | return r; |
| 121 | } |
| 122 | |
| 123 | static inline pointf sub_pointf(pointf p, pointf q) |
| 124 | { |
| 125 | pointf r; |
| 126 | |
| 127 | r.x = p.x - q.x; |
| 128 | r.y = p.y - q.y; |
| 129 | return r; |
| 130 | } |
| 131 | |
| 132 | /* for +ve coord values, this rounds towards p */ |
| 133 | static inline point mid_point(point p, point q) |
| 134 | { |
| 135 | point r; |
| 136 | |
| 137 | r.x = (p.x + q.x) / 2; |
| 138 | r.y = (p.y + q.y) / 2; |
| 139 | return r; |
| 140 | } |
| 141 | |
| 142 | static inline pointf mid_pointf(pointf p, pointf q) |
| 143 | { |
| 144 | pointf r; |
| 145 | |
| 146 | r.x = (p.x + q.x) / 2.; |
| 147 | r.y = (p.y + q.y) / 2.; |
| 148 | return r; |
| 149 | } |
| 150 | |
| 151 | static inline pointf interpolate_pointf(double t, pointf p, pointf q) |
| 152 | { |
| 153 | pointf r; |
| 154 | |
| 155 | r.x = p.x + t * (q.x - p.x); |
| 156 | r.y = p.y + t * (q.y - p.y); |
| 157 | return r; |
| 158 | } |
| 159 | |
| 160 | static inline point exch_xy(point p) |
| 161 | { |
| 162 | point r; |
| 163 | |
| 164 | r.x = p.y; |
| 165 | r.y = p.x; |
| 166 | return r; |
| 167 | } |
| 168 | |
| 169 | static inline pointf exch_xyf(pointf p) |
| 170 | { |
| 171 | pointf r; |
| 172 | |
| 173 | r.x = p.y; |
| 174 | r.y = p.x; |
| 175 | return r; |
| 176 | } |
| 177 | |
| 178 | static inline box box_bb(box b0, box b1) |
| 179 | { |
| 180 | box b; |
| 181 | |
| 182 | b.LL.x = MIN(b0.LL.x, b1.LL.x); |
| 183 | b.LL.y = MIN(b0.LL.y, b1.LL.y); |
| 184 | b.UR.x = MAX(b0.UR.x, b1.UR.x); |
| 185 | b.UR.y = MAX(b0.UR.y, b1.UR.y); |
| 186 | |
| 187 | return b; |
| 188 | } |
| 189 | |
| 190 | static inline boxf boxf_bb(boxf b0, boxf b1) |
| 191 | { |
| 192 | boxf b; |
| 193 | |
| 194 | b.LL.x = MIN(b0.LL.x, b1.LL.x); |
| 195 | b.LL.y = MIN(b0.LL.y, b1.LL.y); |
| 196 | b.UR.x = MAX(b0.UR.x, b1.UR.x); |
| 197 | b.UR.y = MAX(b0.UR.y, b1.UR.y); |
| 198 | |
| 199 | return b; |
| 200 | } |
| 201 | |
| 202 | static inline box box_intersect(box b0, box b1) |
| 203 | { |
| 204 | box b; |
| 205 | |
| 206 | b.LL.x = MAX(b0.LL.x, b1.LL.x); |
| 207 | b.LL.y = MAX(b0.LL.y, b1.LL.y); |
| 208 | b.UR.x = MIN(b0.UR.x, b1.UR.x); |
| 209 | b.UR.y = MIN(b0.UR.y, b1.UR.y); |
| 210 | |
| 211 | return b; |
| 212 | } |
| 213 | |
| 214 | static inline boxf boxf_intersect(boxf b0, boxf b1) |
| 215 | { |
| 216 | boxf b; |
| 217 | |
| 218 | b.LL.x = MAX(b0.LL.x, b1.LL.x); |
| 219 | b.LL.y = MAX(b0.LL.y, b1.LL.y); |
| 220 | b.UR.x = MIN(b0.UR.x, b1.UR.x); |
| 221 | b.UR.y = MIN(b0.UR.y, b1.UR.y); |
| 222 | |
| 223 | return b; |
| 224 | } |
| 225 | |
| 226 | static inline int box_overlap(box b0, box b1) |
| 227 | { |
| 228 | return OVERLAP(b0, b1); |
| 229 | } |
| 230 | |
| 231 | static inline int boxf_overlap(boxf b0, boxf b1) |
| 232 | { |
| 233 | return OVERLAP(b0, b1); |
| 234 | } |
| 235 | |
| 236 | static inline int box_contains(box b0, box b1) |
| 237 | { |
| 238 | return CONTAINS(b0, b1); |
| 239 | } |
| 240 | |
| 241 | static inline int boxf_contains(boxf b0, boxf b1) |
| 242 | { |
| 243 | return CONTAINS(b0, b1); |
| 244 | } |
| 245 | |
| 246 | static inline pointf perp (pointf p) |
| 247 | { |
| 248 | pointf r; |
| 249 | |
| 250 | r.x = -p.y; |
| 251 | r.y = p.x; |
| 252 | return r; |
| 253 | } |
| 254 | |
| 255 | static inline pointf scale (double c, pointf p) |
| 256 | { |
| 257 | pointf r; |
| 258 | |
| 259 | r.x = c * p.x; |
| 260 | r.y = c * p.y; |
| 261 | return r; |
| 262 | } |
| 263 | #ifdef WIN32_STATIC |
| 264 | #undef inline |
| 265 | #endif |
| 266 | |
| 267 | #undef extern |
| 268 | #ifdef __cplusplus |
| 269 | } |
| 270 | #endif |
| 271 | |
| 272 | #endif |
| 273 | |