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