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