Horizon
Loading...
Searching...
No Matches
placement.hpp
1#pragma once
2#include "common/common.hpp"
3#include "nlohmann/json_fwd.hpp"
4
5namespace horizon {
6using json = nlohmann::json;
7
8class Placement {
9public:
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 else 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 = get_angle_rad();
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
72 bool is_identity() const
73 {
74 return shift.x == 0 && shift.y == 0 && angle == 0 && mirror == false;
75 }
76
77 void make_relative(const Placement &to);
78 void accumulate(const Placement &p);
79 void invert_angle();
80 void set_angle(int a);
81 void inc_angle(int a);
82 void inc_angle_deg(int a);
83 void set_angle_deg(int a);
84 void set_angle_rad(double a);
85 int get_angle() const;
86 int get_angle_deg() const;
87 double get_angle_rad() const;
88 Coordi shift;
89
90 bool mirror = false;
91 json serialize() const;
92
93private:
94 int angle = 0;
95};
96} // namespace horizon
static Coord< T > min(const Coord< T > &a, const Coord< T > &b)
Definition common.hpp:158
static Coord< T > max(const Coord< T > &a, const Coord< T > &b)
Definition common.hpp:166
Definition placement.hpp:8
a class to store JSON values
Definition json.hpp:177
basic_json<> json
default JSON class
Definition json_fwd.hpp:62