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 };
58 enum class PatchType { OTHER, TRACK, PAD, PAD_TH, VIA, PLANE, HOLE_PTH, HOLE_NPTH, BOARD_EDGE, TEXT };
59 
60 extern const LutEnumStr<PatchType> patch_type_lut;
61 extern const LutEnumStr<ObjectType> object_type_lut;
62 extern const LutEnumStr<Orientation> orientation_lut;
63 
72 template <typename T> class Coord {
73 public:
74  T x;
75  T y;
76 
77  // WTF, but works
78  // template<typename U = T>
79  // Coord(double ix, double iy, typename std::enable_if<std::is_same<U,
80  // float>::value>::type* = 0) : x((float)ix), y((float)iy) { }
81 
82 
83  Coord(T ix, T iy) : x(ix), y(iy)
84  {
85  }
86  Coord() : x(0), y(0)
87  {
88  }
89  Coord(std::vector<T> v) : x(v.at(0)), y(v.at(1))
90  {
91  }
92  operator Coord<float>() const
93  {
94  return Coord<float>(x, y);
95  }
96  operator Coord<double>() const
97  {
98  return Coord<double>(x, y);
99  }
100  Coord<T> operator+(const Coord<T> &a) const
101  {
102  return Coord<T>(x + a.x, y + a.y);
103  }
104  Coord<T> operator-(const Coord<T> &a) const
105  {
106  return Coord<T>(x - a.x, y - a.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*(T r) const
113  {
114  return Coord<T>(x * r, y * r);
115  }
116  Coord<T> operator/(T r) const
117  {
118  return Coord<T>(x / r, y / r);
119  }
120  bool operator==(const Coord<T> &a) const
121  {
122  return a.x == x && a.y == y;
123  }
124  bool operator!=(const Coord<T> &a) const
125  {
126  return !(a == *this);
127  }
128  bool operator<(const Coord<T> &a) const
129  {
130  if (x < a.x)
131  return true;
132  if (x > a.x)
133  return false;
134  return y < a.y;
135  }
136 
140  static Coord<T> min(const Coord<T> &a, const Coord<T> &b)
141  {
142  return Coord<T>(std::min(a.x, b.x), std::min(a.y, b.y));
143  }
144 
148  static Coord<T> max(const Coord<T> &a, const Coord<T> &b)
149  {
150  return Coord<T>(std::max(a.x, b.x), std::max(a.y, b.y));
151  }
152 
158  static Coord<float> euler(float r, float phi)
159  {
160  return Coord<float>(r * cos(phi), r * sin(phi));
161  }
162 
167  T dot(const Coord<T> &a) const
168  {
169  return x * a.x + y * a.y;
170  }
171 
175  T mag_sq() const
176  {
177  return x * x + y * y;
178  }
179 
180  bool in_range(const Coord<T> &a, const Coord<T> &b) const
181  {
182  return x > a.x && y > a.y && x < b.x && y < b.y;
183  }
184 
185  void operator+=(const Coord<T> a)
186  {
187  x += a.x;
188  y += a.y;
189  }
190  void operator-=(const Coord<T> a)
191  {
192  x -= a.x;
193  y -= a.y;
194  }
195  void operator*=(T a)
196  {
197  x *= a;
198  y *= a;
199  }
200  /*json serialize() {
201  return {x,y};
202  }*/
203  std::array<T, 2> as_array() const
204  {
205  return {x, y};
206  }
207 };
208 
209 
210 typedef Coord<float> Coordf;
211 typedef Coord<int64_t> Coordi;
212 
213 class Color {
214 public:
215  float r;
216  float g;
217  float b;
218  Color(double ir, double ig, double ib) : r(ir), g(ig), b(ib)
219  {
220  }
221  // Color(unsigned int ir, unsigned ig, unsigned ib): r(ir/255.), g(ig/255.),
222  // b(ib/255.) {}
223  static Color new_from_int(unsigned int ir, unsigned ig, unsigned ib)
224  {
225  return Color(ir / 255.0, ig / 255.0, ib / 255.0);
226  }
227  Color() : r(0), g(0), b(0)
228  {
229  }
230 };
231 
232 constexpr int64_t operator"" _mm(long double i)
233 {
234  return i * 1e6;
235 }
236 constexpr int64_t operator"" _mm(unsigned long long int i)
237 {
238  return i * 1000000;
239 }
240 } // namespace horizon
horizon::Coord::dot
T dot(const Coord< T > &a) const
Definition: common.hpp:167
horizon::Coord::min
static Coord< T > min(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:140
horizon::Color
Definition: common.hpp:213
horizon::Coord
Your typical coordinate class.
Definition: common.hpp:72
horizon::Coord::euler
static Coord< float > euler(float r, float phi)
Definition: common.hpp:158
horizon::Coord::mag_sq
T mag_sq() const
Definition: common.hpp:175
horizon::Coord::max
static Coord< T > max(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:148
libzip::int64_t
zip_int64_t int64_t
zip_int64_t typedef.
Definition: zip.hpp:103
SHAPE
Class SHAPE.
Definition: shape.h:58