Horizon
common.hpp
1#pragma once
2#include <stdint.h>
3#include <vector>
4#include <algorithm>
5#include <type_traits>
6#include <math.h>
7#include <array>
8#include "lut.hpp"
9
10namespace horizon {
11enum class Orientation { LEFT, RIGHT, UP, DOWN };
15enum class ObjectType {
16 INVALID,
17 JUNCTION,
18 LINE,
19 SYMBOL_PIN,
20 ARC,
21 SCHEMATIC_SYMBOL,
22 TEXT,
23 LINE_NET,
24 COMPONENT,
25 NET,
26 NET_LABEL,
27 POWER_SYMBOL,
28 BUS,
29 BUS_LABEL,
30 BUS_RIPPER,
31 POLYGON,
32 POLYGON_VERTEX,
33 POLYGON_EDGE,
34 POLYGON_ARC_CENTER,
35 HOLE,
36 PAD,
37 BOARD_PACKAGE,
38 TRACK,
39 VIA,
40 SHAPE,
41 BOARD,
42 SCHEMATIC,
43 UNIT,
44 ENTITY,
45 SYMBOL,
46 PACKAGE,
47 PADSTACK,
48 PART,
49 PLANE,
50 DIMENSION,
51 NET_CLASS,
52 BOARD_HOLE,
53 MODEL_3D,
54 FRAME,
55 KEEPOUT,
56 CONNECTION_LINE,
57 AIRWIRE,
58 BOARD_PANEL,
59 PICTURE,
60 DECAL,
61 BOARD_DECAL,
62 PROJECT,
63 BLOCK,
64 BLOCKS,
65 BLOCK_INSTANCE,
66 BLOCK_SYMBOL,
67 BLOCK_SYMBOL_PORT,
68 SCHEMATIC_BLOCK_SYMBOL,
69 POOL,
70 NET_TIE,
71 SCHEMATIC_NET_TIE,
72 BOARD_NET_TIE,
73};
74enum class PatchType { OTHER, TRACK, PAD, PAD_TH, VIA, PLANE, HOLE_PTH, HOLE_NPTH, BOARD_EDGE, TEXT, NET_TIE, N_TYPES };
75
76extern const LutEnumStr<PatchType> patch_type_lut;
77extern const LutEnumStr<ObjectType> object_type_lut;
78extern const LutEnumStr<Orientation> orientation_lut;
79
88template <typename T> class Coord {
89public:
90 T x;
91 T y;
92
93 using type = T;
94
95 // WTF, but works
96 // template<typename U = T>
97 // Coord(double ix, double iy, typename std::enable_if<std::is_same<U,
98 // float>::value>::type* = 0) : x((float)ix), y((float)iy) { }
99
100
101 Coord(T ix, T iy) : x(ix), y(iy)
102 {
103 }
104 Coord() : x(0), y(0)
105 {
106 }
107 Coord(std::vector<T> v) : x(v.at(0)), y(v.at(1))
108 {
109 }
110 operator Coord<float>() const
111 {
112 return Coord<float>(x, y);
113 }
114 operator Coord<double>() const
115 {
116 return Coord<double>(x, y);
117 }
118 Coord<T> operator+(const Coord<T> &a) const
119 {
120 return Coord<T>(x + a.x, y + a.y);
121 }
122 Coord<T> operator-(const Coord<T> &a) const
123 {
124 return Coord<T>(x - a.x, y - a.y);
125 }
126 Coord<T> operator*(const Coord<T> &a) const
127 {
128 return Coord<T>(x * a.x, y * a.y);
129 }
130 Coord<T> operator*(T r) const
131 {
132 return Coord<T>(x * r, y * r);
133 }
134 Coord<T> operator/(T r) const
135 {
136 return Coord<T>(x / r, y / r);
137 }
138 bool operator==(const Coord<T> &a) const
139 {
140 return a.x == x && a.y == y;
141 }
142 bool operator!=(const Coord<T> &a) const
143 {
144 return !(a == *this);
145 }
146 bool operator<(const Coord<T> &a) const
147 {
148 if (x < a.x)
149 return true;
150 if (x > a.x)
151 return false;
152 return y < a.y;
153 }
154
158 static Coord<T> min(const Coord<T> &a, const Coord<T> &b)
159 {
160 return Coord<T>(std::min(a.x, b.x), std::min(a.y, b.y));
161 }
162
166 static Coord<T> max(const Coord<T> &a, const Coord<T> &b)
167 {
168 return Coord<T>(std::max(a.x, b.x), std::max(a.y, b.y));
169 }
170
176 static Coord<T> euler(T r, T phi)
177 {
178 static_assert(std::is_floating_point_v<T>);
179 return {r * cos(phi), r * sin(phi)};
180 }
181
182 Coord<T> rotate(T a) const
183 {
184 static_assert(std::is_floating_point_v<T>);
185 const T x2 = x * cos(a) - y * sin(a);
186 const T y2 = x * sin(a) + y * cos(a);
187 return {x2, y2};
188 }
189
190 Coord<int64_t> to_coordi() const
191 {
192 static_assert(std::is_floating_point_v<T>);
193 return Coord<int64_t>(x, y);
194 }
195
200 T dot(const Coord<T> &a) const
201 {
202 return x * a.x + y * a.y;
203 }
204
205 T cross(const Coord<T> &other) const
206 {
207 return (x * other.y) - (y * other.x);
208 }
209
213 T mag_sq() const
214 {
215 return x * x + y * y;
216 }
217
218 T mag() const
219 {
220 static_assert(std::is_floating_point_v<T>);
221 return sqrt(mag_sq());
222 }
223
224 Coord<T> normalize() const
225 {
226 static_assert(std::is_floating_point_v<T>);
227 return *this / mag();
228 }
229
230 double magd() const
231 {
232 static_assert(std::is_integral_v<T>);
233 return sqrt(mag_sq());
234 }
235
236 bool in_range(const Coord<T> &a, const Coord<T> &b) const
237 {
238 return x > a.x && y > a.y && x < b.x && y < b.y;
239 }
240
241 void operator+=(const Coord<T> a)
242 {
243 x += a.x;
244 y += a.y;
245 }
246 void operator-=(const Coord<T> a)
247 {
248 x -= a.x;
249 y -= a.y;
250 }
251 void operator*=(T a)
252 {
253 x *= a;
254 y *= a;
255 }
256 /*json serialize() {
257 return {x,y};
258 }*/
259 std::array<T, 2> as_array() const
260 {
261 return {x, y};
262 }
263};
264
265
266typedef Coord<float> Coordf;
267typedef Coord<int64_t> Coordi;
268typedef Coord<double> Coordd;
269
270class Color {
271public:
272 float r;
273 float g;
274 float b;
275 Color(double ir, double ig, double ib) : r(ir), g(ig), b(ib)
276 {
277 }
278 // Color(unsigned int ir, unsigned ig, unsigned ib): r(ir/255.), g(ig/255.),
279 // b(ib/255.) {}
280 static Color new_from_int(unsigned int ir, unsigned ig, unsigned ib)
281 {
282 return Color(ir / 255.0, ig / 255.0, ib / 255.0);
283 }
284 Color() : r(0), g(0), b(0)
285 {
286 }
287};
288
289struct ColorI {
290 uint8_t r;
291 uint8_t g;
292 uint8_t b;
293
294 bool operator<(const ColorI &other) const
295 {
296 return hashify() < other.hashify();
297 }
298
299 Color to_color() const
300 {
301 return Color::new_from_int(r, g, b);
302 }
303
304private:
305 uint32_t hashify() const
306 {
307 return r | (g << 8) | (b << 16);
308 }
309};
310
311constexpr int64_t operator"" _mm(long double i)
312{
313 return i * 1e6;
314}
315constexpr int64_t operator"" _mm(unsigned long long int i)
316{
317 return i * 1000000;
318}
319
321 explicit shallow_copy_t() = default;
322};
323
324constexpr shallow_copy_t shallow_copy = shallow_copy_t();
325
326enum class CopyMode { DEEP, SHALLOW };
327
328} // namespace horizon
Class SHAPE.
Definition: shape.h:59
Definition: common.hpp:270
Your typical coordinate class.
Definition: common.hpp:88
T dot(const Coord< T > &a) const
Definition: common.hpp:200
T mag_sq() const
Definition: common.hpp:213
static Coord< T > min(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:158
static Coord< T > max(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:166
static Coord< T > euler(T r, T phi)
Definition: common.hpp:176
Definition: common.hpp:289
Definition: common.hpp:320