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 
10 namespace horizon {
11 enum class Orientation { LEFT, RIGHT, UP, DOWN };
15 enum 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 };
74 enum class PatchType { OTHER, TRACK, PAD, PAD_TH, VIA, PLANE, HOLE_PTH, HOLE_NPTH, BOARD_EDGE, TEXT, NET_TIE, N_TYPES };
75 
76 extern const LutEnumStr<PatchType> patch_type_lut;
77 extern const LutEnumStr<ObjectType> object_type_lut;
78 extern const LutEnumStr<Orientation> orientation_lut;
79 
88 template <typename T> class Coord {
89 public:
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  std::conditional_t<std::is_same_v<T, float>, float, double> angle() const
237  {
238  if constexpr (std::is_same_v<T, float>)
239  return atan2f(y, x);
240  else
241  return atan2(y, x);
242  }
243 
244  bool in_range(const Coord<T> &a, const Coord<T> &b) const
245  {
246  return x > a.x && y > a.y && x < b.x && y < b.y;
247  }
248 
249  void operator+=(const Coord<T> a)
250  {
251  x += a.x;
252  y += a.y;
253  }
254  void operator-=(const Coord<T> a)
255  {
256  x -= a.x;
257  y -= a.y;
258  }
259  void operator*=(T a)
260  {
261  x *= a;
262  y *= a;
263  }
264  /*json serialize() {
265  return {x,y};
266  }*/
267  std::array<T, 2> as_array() const
268  {
269  return {x, y};
270  }
271 };
272 
273 
274 typedef Coord<float> Coordf;
275 typedef Coord<int64_t> Coordi;
276 typedef Coord<double> Coordd;
277 
278 class Color {
279 public:
280  float r;
281  float g;
282  float b;
283  Color(double ir, double ig, double ib) : r(ir), g(ig), b(ib)
284  {
285  }
286  // Color(unsigned int ir, unsigned ig, unsigned ib): r(ir/255.), g(ig/255.),
287  // b(ib/255.) {}
288  static Color new_from_int(unsigned int ir, unsigned ig, unsigned ib)
289  {
290  return Color(ir / 255.0, ig / 255.0, ib / 255.0);
291  }
292  Color() : r(0), g(0), b(0)
293  {
294  }
295 };
296 
297 struct ColorI {
298  uint8_t r;
299  uint8_t g;
300  uint8_t b;
301 
302  bool operator<(const ColorI &other) const
303  {
304  return hashify() < other.hashify();
305  }
306 
307  Color to_color() const
308  {
309  return Color::new_from_int(r, g, b);
310  }
311 
312 private:
313  uint32_t hashify() const
314  {
315  return r | (g << 8) | (b << 16);
316  }
317 };
318 
319 constexpr int64_t operator"" _mm(long double i)
320 {
321  return i * 1e6;
322 }
323 constexpr int64_t operator"" _mm(unsigned long long int i)
324 {
325  return i * 1000000;
326 }
327 
329  explicit shallow_copy_t() = default;
330 };
331 
332 constexpr shallow_copy_t shallow_copy = shallow_copy_t();
333 
334 enum class CopyMode { DEEP, SHALLOW };
335 
336 } // namespace horizon
An abstract shape on 2D plane.
Definition: shape.h:117
Definition: common.hpp:278
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 > max(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:166
static Coord< T > min(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:158
static Coord< T > euler(T r, T phi)
Definition: common.hpp:176
Definition: common.hpp:297
Definition: common.hpp:328