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