1 #include "hole.hpp"
2 #include "lut.hpp"
3 #include "nlohmann/json.hpp"
4 
5 namespace horizon {
6 using json = nlohmann::json;
7 
8 static const LutEnumStr<Hole::Shape> shape_lut = {
9         {"round", Hole::Shape::ROUND},
10         {"slot", Hole::Shape::SLOT},
11 };
12 
13 
Hole(const UUID & uu,const json & j)14 Hole::Hole(const UUID &uu, const json &j)
15     : uuid(uu), placement(j.at("placement")), diameter(j.at("diameter").get<uint64_t>()),
16       length(j.at("length").get<uint64_t>()), parameter_class(j.value("parameter_class", "")),
17       plated(j.at("plated").get<bool>()), shape(shape_lut.lookup(j.value("shape", "round")))
18 {
19 }
20 
get_uuid() const21 UUID Hole::get_uuid() const
22 {
23     return uuid;
24 }
25 
to_polygon() const26 Polygon Hole::to_polygon() const
27 {
28     auto uu = UUID();
29     Polygon poly(uu);
30     poly.layer = 10000;
31     switch (shape) {
32     case Shape::ROUND: {
33         int64_t r = diameter / 2;
34         Polygon::Vertex *v;
35         v = poly.append_vertex({r, 0});
36         v->type = Polygon::Vertex::Type::ARC;
37         v->arc_reverse = true;
38         poly.append_vertex({r, 0});
39     } break;
40 
41     case Shape::SLOT: {
42         int64_t h = diameter / 2;
43         int64_t w = length / 2;
44         w = std::max(w - h, (int64_t)0);
45 
46         Polygon::Vertex *v;
47         v = poly.append_vertex({w, h});
48         v->type = Polygon::Vertex::Type::ARC;
49         v->arc_center = {w, 0};
50         v->arc_reverse = true;
51         poly.append_vertex({w, -h});
52         v = poly.append_vertex({-w, -h});
53         v->type = Polygon::Vertex::Type::ARC;
54         v->arc_center = {-w, 0};
55         v->arc_reverse = true;
56         poly.append_vertex({-w, h});
57 
58     } break;
59     }
60     for (auto &it : poly.vertices) {
61         it.position = placement.transform(it.position);
62         it.arc_center = placement.transform(it.arc_center);
63     }
64     return poly;
65 }
66 
Hole(const UUID & uu)67 Hole::Hole(const UUID &uu) : uuid(uu)
68 {
69 }
70 
serialize() const71 json Hole::serialize() const
72 {
73     json j;
74     j["placement"] = placement.serialize();
75     j["diameter"] = diameter;
76     j["length"] = length;
77     j["shape"] = shape_lut.lookup_reverse(shape);
78     j["plated"] = plated;
79     j["parameter_class"] = parameter_class;
80     return j;
81 }
82 } // namespace horizon
83