Debian Codegraph @

lib/common/geomprocs.h

raw
1 /**
2 * @file
3 * @ingroup common_utils
4 * @brief geometric functions (e.g. on points and boxes)
5 *
6 * with application to, but no specific dependence on graphs
7 */
8
9 /*************************************************************************
10 * Copyright (c) 2011 AT&T Intellectual Property
11 * All rights reserved. This program and the accompanying materials
12 * are made available under the terms of the Eclipse Public License v1.0
13 * which accompanies this distribution, and is available at
14 * https://www.eclipse.org/legal/epl-v10.html
15 *
16 * Contributors: Details at https://graphviz.org
17 *************************************************************************/
18
19 #pragma once
20
21 #include <math.h>
22 #include <stdbool.h>
23 #include <util/unused.h>
24
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28
29
30 #include "geom.h"
31
32 #ifdef GVDLL
33 #ifdef GVC_EXPORTS
34 #define GEOMPROCS_API __declspec(dllexport)
35 #else
36 #define GEOMPROCS_API __declspec(dllimport)
37 #endif
38 #endif
39
40 #ifndef GEOMPROCS_API
41 #define GEOMPROCS_API /* nothing */
42 #endif
43
44 /// expand box b as needed to enclose point p
45 static inline void expandbp(boxf *b, pointf p) {
46 b->LL.x = fmin(b->LL.x, p.x);
47 b->LL.y = fmin(b->LL.y, p.y);
48 b->UR.x = fmax(b->UR.x, p.x);
49 b->UR.y = fmax(b->UR.y, p.y);
50 }
51
52 /// expand box b0 as needed to enclose box b1
53 static inline void expandbb(box *b0, box b1) {
54 b0->LL.x = MIN(b0->LL.x, b1.LL.x);
55 b0->LL.y = MIN(b0->LL.y, b1.LL.y);
56 b0->UR.x = MAX(b0->UR.x, b1.UR.x);
57 b0->UR.y = MAX(b0->UR.y, b1.UR.y);
58 }
59 static inline void expandbbf(boxf *b0, boxf b1) {
60 b0->LL.x = fmin(b0->LL.x, b1.LL.x);
61 b0->LL.y = fmin(b0->LL.y, b1.LL.y);
62 b0->UR.x = fmax(b0->UR.x, b1.UR.x);
63 b0->UR.y = fmax(b0->UR.y, b1.UR.y);
64 }
65 #define EXPANDBB(b0, b1) \
66 (_Generic((b0), box *: expandbb, boxf *: expandbbf)((b0), (b1)))
67
68 GEOMPROCS_API boxf flip_rec_boxf(boxf b, pointf p);
69
70 GEOMPROCS_API double ptToLine2 (pointf l1, pointf l2, pointf p);
71
72 GEOMPROCS_API int lineToBox(pointf p1, pointf p2, boxf b);
73 GEOMPROCS_API pointf ccwrotatepf(pointf p, int ccwrot);
74 GEOMPROCS_API pointf cwrotatepf(pointf p, int cwrot);
75
76 GEOMPROCS_API void rect2poly(pointf *p);
77
78 GEOMPROCS_API int line_intersect (pointf a, pointf b, pointf c, pointf d, pointf* p);
79
80 static inline WUR point add_point(point p, point q) {
81 point r;
82
83 r.x = p.x + q.x;
84 r.y = p.y + q.y;
85 return r;
86 }
87
88 static inline WUR pointf add_pointf(pointf p, pointf q) {
89 pointf r;
90
91 r.x = p.x + q.x;
92 r.y = p.y + q.y;
93 return r;
94 }
95
96 static inline WUR pointf sub_pointf(pointf p, pointf q) {
97 pointf r;
98
99 r.x = p.x - q.x;
100 r.y = p.y - q.y;
101 return r;
102 }
103
104 static inline WUR pointf mid_pointf(pointf p, pointf q) {
105 pointf r;
106
107 r.x = (p.x + q.x) / 2.;
108 r.y = (p.y + q.y) / 2.;
109 return r;
110 }
111
112 static inline WUR pointf interpolate_pointf(double t, pointf p, pointf q) {
113 pointf r;
114
115 r.x = p.x + t * (q.x - p.x);
116 r.y = p.y + t * (q.y - p.y);
117 return r;
118 }
119
120 static inline WUR point exch_xy(point p) {
121 point r;
122
123 r.x = p.y;
124 r.y = p.x;
125 return r;
126 }
127
128 static inline WUR pointf exch_xyf(pointf p) {
129 pointf r;
130
131 r.x = p.y;
132 r.y = p.x;
133 return r;
134 }
135
136 static inline WUR bool boxf_overlap(boxf b0, boxf b1) {
137 return OVERLAP(b0, b1);
138 }
139
140 static inline WUR pointf perp (pointf p) {
141 pointf r;
142
143 r.x = -p.y;
144 r.y = p.x;
145 return r;
146 }
147
148 static inline WUR pointf scale (double c, pointf p) {
149 pointf r;
150
151 r.x = c * p.x;
152 r.y = c * p.y;
153 return r;
154 }
155
156 #undef GEOMPROCS_API
157 #ifdef __cplusplus
158 }
159 #endif