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<float> euler(float r, float phi)
167  {
168  return Coord<float>(r * cos(phi), r * sin(phi));
169  }
170 
171  Coord<float> rotate(float a) const
172  {
173  const float x2 = x * cos(a) - y * sin(a);
174  const float y2 = x * sin(a) + y * cos(a);
175  return {x2, y2};
176  }
177 
182  T dot(const Coord<T> &a) const
183  {
184  return x * a.x + y * a.y;
185  }
186 
187  T cross(const Coord<T> &other) const
188  {
189  return (x * other.y) - (y * other.x);
190  }
191 
195  T mag_sq() const
196  {
197  return x * x + y * y;
198  }
199 
200  bool in_range(const Coord<T> &a, const Coord<T> &b) const
201  {
202  return x > a.x && y > a.y && x < b.x && y < b.y;
203  }
204 
205  void operator+=(const Coord<T> a)
206  {
207  x += a.x;
208  y += a.y;
209  }
210  void operator-=(const Coord<T> a)
211  {
212  x -= a.x;
213  y -= a.y;
214  }
215  void operator*=(T a)
216  {
217  x *= a;
218  y *= a;
219  }
220  /*json serialize() {
221  return {x,y};
222  }*/
223  std::array<T, 2> as_array() const
224  {
225  return {x, y};
226  }
227 };
228 
229 
230 typedef Coord<float> Coordf;
231 typedef Coord<int64_t> Coordi;
232 typedef Coord<double> Coordd;
233 
234 class Color {
235 public:
236  float r;
237  float g;
238  float b;
239  Color(double ir, double ig, double ib) : r(ir), g(ig), b(ib)
240  {
241  }
242  // Color(unsigned int ir, unsigned ig, unsigned ib): r(ir/255.), g(ig/255.),
243  // b(ib/255.) {}
244  static Color new_from_int(unsigned int ir, unsigned ig, unsigned ib)
245  {
246  return Color(ir / 255.0, ig / 255.0, ib / 255.0);
247  }
248  Color() : r(0), g(0), b(0)
249  {
250  }
251 };
252 
253 struct ColorI {
254  uint8_t r;
255  uint8_t g;
256  uint8_t b;
257 
258  bool operator<(const ColorI &other) const
259  {
260  return hashify() < other.hashify();
261  }
262 
263  Color to_color() const
264  {
265  return Color::new_from_int(r, g, b);
266  }
267 
268 private:
269  uint32_t hashify() const
270  {
271  return r | (g << 8) | (b << 16);
272  }
273 };
274 
275 constexpr int64_t operator"" _mm(long double i)
276 {
277  return i * 1e6;
278 }
279 constexpr int64_t operator"" _mm(unsigned long long int i)
280 {
281  return i * 1000000;
282 }
283 
285  explicit shallow_copy_t() = default;
286 };
287 
288 constexpr shallow_copy_t shallow_copy = shallow_copy_t();
289 
290 enum class CopyMode { DEEP, SHALLOW };
291 
292 } // namespace horizon
Class SHAPE.
Definition: shape.h:59
Definition: common.hpp:234
Your typical coordinate class.
Definition: common.hpp:78
T dot(const Coord< T > &a) const
Definition: common.hpp:182
T mag_sq() const
Definition: common.hpp:195
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< float > euler(float r, float 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:253
Definition: common.hpp:284