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 };
71 enum class PatchType { OTHER, TRACK, PAD, PAD_TH, VIA, PLANE, HOLE_PTH, HOLE_NPTH, BOARD_EDGE, TEXT, N_TYPES };
72 
73 extern const LutEnumStr<PatchType> patch_type_lut;
74 extern const LutEnumStr<ObjectType> object_type_lut;
75 extern const LutEnumStr<Orientation> orientation_lut;
76 
85 template <typename T> class Coord {
86 public:
87  T x;
88  T y;
89 
90  using type = T;
91 
92  // WTF, but works
93  // template<typename U = T>
94  // Coord(double ix, double iy, typename std::enable_if<std::is_same<U,
95  // float>::value>::type* = 0) : x((float)ix), y((float)iy) { }
96 
97 
98  Coord(T ix, T iy) : x(ix), y(iy)
99  {
100  }
101  Coord() : x(0), y(0)
102  {
103  }
104  Coord(std::vector<T> v) : x(v.at(0)), y(v.at(1))
105  {
106  }
107  operator Coord<float>() const
108  {
109  return Coord<float>(x, y);
110  }
111  operator Coord<double>() const
112  {
113  return Coord<double>(x, y);
114  }
115  Coord<T> operator+(const Coord<T> &a) const
116  {
117  return Coord<T>(x + a.x, y + a.y);
118  }
119  Coord<T> operator-(const Coord<T> &a) const
120  {
121  return Coord<T>(x - a.x, y - a.y);
122  }
123  Coord<T> operator*(const Coord<T> &a) const
124  {
125  return Coord<T>(x * a.x, y * a.y);
126  }
127  Coord<T> operator*(T r) const
128  {
129  return Coord<T>(x * r, y * r);
130  }
131  Coord<T> operator/(T r) const
132  {
133  return Coord<T>(x / r, y / r);
134  }
135  bool operator==(const Coord<T> &a) const
136  {
137  return a.x == x && a.y == y;
138  }
139  bool operator!=(const Coord<T> &a) const
140  {
141  return !(a == *this);
142  }
143  bool operator<(const Coord<T> &a) const
144  {
145  if (x < a.x)
146  return true;
147  if (x > a.x)
148  return false;
149  return y < a.y;
150  }
151 
155  static Coord<T> min(const Coord<T> &a, const Coord<T> &b)
156  {
157  return Coord<T>(std::min(a.x, b.x), std::min(a.y, b.y));
158  }
159 
163  static Coord<T> max(const Coord<T> &a, const Coord<T> &b)
164  {
165  return Coord<T>(std::max(a.x, b.x), std::max(a.y, b.y));
166  }
167 
173  static Coord<T> euler(T r, T phi)
174  {
175  static_assert(std::is_floating_point_v<T>);
176  return {r * cos(phi), r * sin(phi)};
177  }
178 
179  Coord<T> rotate(T a) const
180  {
181  static_assert(std::is_floating_point_v<T>);
182  const T x2 = x * cos(a) - y * sin(a);
183  const T y2 = x * sin(a) + y * cos(a);
184  return {x2, y2};
185  }
186 
187  Coord<int64_t> to_coordi() const
188  {
189  static_assert(std::is_floating_point_v<T>);
190  return Coord<int64_t>(x, y);
191  }
192 
197  T dot(const Coord<T> &a) const
198  {
199  return x * a.x + y * a.y;
200  }
201 
202  T cross(const Coord<T> &other) const
203  {
204  return (x * other.y) - (y * other.x);
205  }
206 
210  T mag_sq() const
211  {
212  return x * x + y * y;
213  }
214 
215  T mag() const
216  {
217  static_assert(std::is_floating_point_v<T>);
218  return sqrt(mag_sq());
219  }
220 
221  Coord<T> normalize() const
222  {
223  static_assert(std::is_floating_point_v<T>);
224  return *this / mag();
225  }
226 
227  double magd() const
228  {
229  static_assert(std::is_integral_v<T>);
230  return sqrt(mag_sq());
231  }
232 
233  bool in_range(const Coord<T> &a, const Coord<T> &b) const
234  {
235  return x > a.x && y > a.y && x < b.x && y < b.y;
236  }
237 
238  void operator+=(const Coord<T> a)
239  {
240  x += a.x;
241  y += a.y;
242  }
243  void operator-=(const Coord<T> a)
244  {
245  x -= a.x;
246  y -= a.y;
247  }
248  void operator*=(T a)
249  {
250  x *= a;
251  y *= a;
252  }
253  /*json serialize() {
254  return {x,y};
255  }*/
256  std::array<T, 2> as_array() const
257  {
258  return {x, y};
259  }
260 };
261 
262 
263 typedef Coord<float> Coordf;
264 typedef Coord<int64_t> Coordi;
265 typedef Coord<double> Coordd;
266 
267 class Color {
268 public:
269  float r;
270  float g;
271  float b;
272  Color(double ir, double ig, double ib) : r(ir), g(ig), b(ib)
273  {
274  }
275  // Color(unsigned int ir, unsigned ig, unsigned ib): r(ir/255.), g(ig/255.),
276  // b(ib/255.) {}
277  static Color new_from_int(unsigned int ir, unsigned ig, unsigned ib)
278  {
279  return Color(ir / 255.0, ig / 255.0, ib / 255.0);
280  }
281  Color() : r(0), g(0), b(0)
282  {
283  }
284 };
285 
286 struct ColorI {
287  uint8_t r;
288  uint8_t g;
289  uint8_t b;
290 
291  bool operator<(const ColorI &other) const
292  {
293  return hashify() < other.hashify();
294  }
295 
296  Color to_color() const
297  {
298  return Color::new_from_int(r, g, b);
299  }
300 
301 private:
302  uint32_t hashify() const
303  {
304  return r | (g << 8) | (b << 16);
305  }
306 };
307 
308 constexpr int64_t operator"" _mm(long double i)
309 {
310  return i * 1e6;
311 }
312 constexpr int64_t operator"" _mm(unsigned long long int i)
313 {
314  return i * 1000000;
315 }
316 
318  explicit shallow_copy_t() = default;
319 };
320 
321 constexpr shallow_copy_t shallow_copy = shallow_copy_t();
322 
323 enum class CopyMode { DEEP, SHALLOW };
324 
325 } // namespace horizon
Class SHAPE.
Definition: shape.h:59
Definition: common.hpp:267
Your typical coordinate class.
Definition: common.hpp:85
T dot(const Coord< T > &a) const
Definition: common.hpp:197
T mag_sq() const
Definition: common.hpp:210
static Coord< T > max(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:163
static Coord< T > min(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:155
static Coord< T > euler(T r, T phi)
Definition: common.hpp:173
zip_int64_t int64_t
zip_int64_t typedef.
Definition: zip.hpp:103
zip_uint32_t uint32_t
zip_uint32_t typedef.
Definition: zip.hpp:98
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78
Definition: common.hpp:286
Definition: common.hpp:317