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