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  // WTF, but works
84  // template<typename U = T>
85  // Coord(double ix, double iy, typename std::enable_if<std::is_same<U,
86  // float>::value>::type* = 0) : x((float)ix), y((float)iy) { }
87 
88 
89  Coord(T ix, T iy) : x(ix), y(iy)
90  {
91  }
92  Coord() : x(0), y(0)
93  {
94  }
95  Coord(std::vector<T> v) : x(v.at(0)), y(v.at(1))
96  {
97  }
98  operator Coord<float>() const
99  {
100  return Coord<float>(x, y);
101  }
102  operator Coord<double>() const
103  {
104  return Coord<double>(x, y);
105  }
106  Coord<T> operator+(const Coord<T> &a) const
107  {
108  return Coord<T>(x + a.x, y + a.y);
109  }
110  Coord<T> operator-(const Coord<T> &a) const
111  {
112  return Coord<T>(x - a.x, y - a.y);
113  }
114  Coord<T> operator*(const Coord<T> &a) const
115  {
116  return Coord<T>(x * a.x, y * a.y);
117  }
118  Coord<T> operator*(T r) const
119  {
120  return Coord<T>(x * r, y * r);
121  }
122  Coord<T> operator/(T r) const
123  {
124  return Coord<T>(x / r, y / r);
125  }
126  bool operator==(const Coord<T> &a) const
127  {
128  return a.x == x && a.y == y;
129  }
130  bool operator!=(const Coord<T> &a) const
131  {
132  return !(a == *this);
133  }
134  bool operator<(const Coord<T> &a) const
135  {
136  if (x < a.x)
137  return true;
138  if (x > a.x)
139  return false;
140  return y < a.y;
141  }
142 
146  static Coord<T> min(const Coord<T> &a, const Coord<T> &b)
147  {
148  return Coord<T>(std::min(a.x, b.x), std::min(a.y, b.y));
149  }
150 
154  static Coord<T> max(const Coord<T> &a, const Coord<T> &b)
155  {
156  return Coord<T>(std::max(a.x, b.x), std::max(a.y, b.y));
157  }
158 
164  static Coord<float> euler(float r, float phi)
165  {
166  return Coord<float>(r * cos(phi), r * sin(phi));
167  }
168 
173  T dot(const Coord<T> &a) const
174  {
175  return x * a.x + y * a.y;
176  }
177 
181  T mag_sq() const
182  {
183  return x * x + y * y;
184  }
185 
186  bool in_range(const Coord<T> &a, const Coord<T> &b) const
187  {
188  return x > a.x && y > a.y && x < b.x && y < b.y;
189  }
190 
191  void operator+=(const Coord<T> a)
192  {
193  x += a.x;
194  y += a.y;
195  }
196  void operator-=(const Coord<T> a)
197  {
198  x -= a.x;
199  y -= a.y;
200  }
201  void operator*=(T a)
202  {
203  x *= a;
204  y *= a;
205  }
206  /*json serialize() {
207  return {x,y};
208  }*/
209  std::array<T, 2> as_array() const
210  {
211  return {x, y};
212  }
213 };
214 
215 
216 typedef Coord<float> Coordf;
217 typedef Coord<int64_t> Coordi;
218 typedef Coord<double> Coordd;
219 
220 class Color {
221 public:
222  float r;
223  float g;
224  float b;
225  Color(double ir, double ig, double ib) : r(ir), g(ig), b(ib)
226  {
227  }
228  // Color(unsigned int ir, unsigned ig, unsigned ib): r(ir/255.), g(ig/255.),
229  // b(ib/255.) {}
230  static Color new_from_int(unsigned int ir, unsigned ig, unsigned ib)
231  {
232  return Color(ir / 255.0, ig / 255.0, ib / 255.0);
233  }
234  Color() : r(0), g(0), b(0)
235  {
236  }
237 };
238 
239 struct ColorI {
240  uint8_t r;
241  uint8_t g;
242  uint8_t b;
243 
244  bool operator<(const ColorI &other) const
245  {
246  return hashify() < other.hashify();
247  }
248 
249  Color to_color() const
250  {
251  return Color::new_from_int(r, g, b);
252  }
253 
254 private:
255  uint32_t hashify() const
256  {
257  return r | (g << 8) | (b << 16);
258  }
259 };
260 
261 constexpr int64_t operator"" _mm(long double i)
262 {
263  return i * 1e6;
264 }
265 constexpr int64_t operator"" _mm(unsigned long long int i)
266 {
267  return i * 1000000;
268 }
269 
271  explicit shallow_copy_t() = default;
272 };
273 
274 constexpr shallow_copy_t shallow_copy = shallow_copy_t();
275 
276 enum class CopyMode { DEEP, SHALLOW };
277 
278 } // namespace horizon
horizon::Coord::dot
T dot(const Coord< T > &a) const
Definition: common.hpp:173
libzip::uint8_t
zip_uint8_t uint8_t
zip_uint8_t typedef.
Definition: zip.hpp:78
horizon::Coord::min
static Coord< T > min(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:146
libzip::uint32_t
zip_uint32_t uint32_t
zip_uint32_t typedef.
Definition: zip.hpp:98
horizon::shallow_copy_t
Definition: common.hpp:270
horizon::Color
Definition: common.hpp:220
horizon::Coord
Your typical coordinate class.
Definition: common.hpp:78
libzip::int64_t
zip_int64_t int64_t
zip_int64_t typedef.
Definition: zip.hpp:103
horizon::ColorI
Definition: common.hpp:239
horizon::Coord::euler
static Coord< float > euler(float r, float phi)
Definition: common.hpp:164
horizon::Coord::mag_sq
T mag_sq() const
Definition: common.hpp:181
horizon::Coord::max
static Coord< T > max(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:154
SHAPE
Class SHAPE.
Definition: shape.h:59