Horizon
placement.hpp
1 #pragma once
2 #include "common/common.hpp"
3 #include "nlohmann/json_fwd.hpp"
4 
5 namespace horizon {
6 using json = nlohmann::json;
7 
8 class Placement {
9 public:
10  Placement(const Coordi &sh = {0, 0}, int a = 0, bool m = false) : shift(sh), mirror(m), angle(a)
11  {
12  set_angle(angle);
13  }
14  Placement(const json &j);
15  template <typename T> Coord<T> transform(const Coord<T> &c) const
16  {
17  Coord<T> r = c;
18  int a = angle;
19  while (a < 0) {
20  a += 65536;
21  }
22  if (angle == 0) {
23  // nop
24  }
25  if (angle == 16384) {
26  r.y = c.x;
27  r.x = -c.y;
28  }
29  else if (angle == 32768) {
30  r.y = -c.y;
31  r.x = -c.x;
32  }
33  else if (angle == 49152) {
34  r.y = -c.x;
35  r.x = c.y;
36  }
37  else {
38  double af = (angle / 65536.0) * 2 * M_PI;
39  r.x = c.x * cos(af) - c.y * sin(af);
40  r.y = c.x * sin(af) + c.y * cos(af);
41  }
42  if (mirror) {
43  r.x = -r.x;
44  }
45 
46  r += shift;
47  return r;
48  }
49 
50  template <typename T> std::pair<Coord<T>, Coord<T>> transform_bb(const std::pair<Coord<T>, Coord<T>> &bb) const
51  {
52  int64_t xa = std::min(bb.first.x, bb.second.x);
53  int64_t xb = std::max(bb.first.x, bb.second.x);
54  int64_t ya = std::min(bb.first.y, bb.second.y);
55  int64_t yb = std::max(bb.first.y, bb.second.y);
56 
57  auto a = transform(Coord<T>(xa, ya));
58  auto b = transform(Coord<T>(xa, yb));
59  auto c = transform(Coord<T>(xb, ya));
60  auto d = transform(Coord<T>(xb, yb));
61 
62  auto pa = Coord<T>::min(a, Coord<T>::min(b, Coord<T>::min(c, d)));
63  auto pb = Coord<T>::max(a, Coord<T>::max(b, Coord<T>::max(c, d)));
64  return {pa, pb};
65  }
66 
67  void reset()
68  {
69  shift = {0, 0}, angle = 0, mirror = false;
70  }
71  void accumulate(const Placement &p);
72  void invert_angle();
73  void set_angle(int a);
74  void inc_angle(int a);
75  void inc_angle_deg(int a);
76  void set_angle_deg(int a);
77  int get_angle() const;
78  int get_angle_deg() const;
79  Coordi shift;
80 
81  bool mirror = false;
82  json serialize() const;
83 
84 private:
85  int angle = 0;
86 };
87 } // namespace horizon
nlohmann::json
basic_json<> json
default JSON class
Definition: json_fwd.hpp:61
horizon::Coord::min
static Coord< T > min(const Coord< T > &a, const Coord< T > &b)
Definition: common.hpp:140
horizon::Coord< int64_t >
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:161
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
horizon::Placement
Definition: placement.hpp:8