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