]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/beamgate.cpp
Basic support for beam gate sensors
[r2c2.git] / source / libr2c2 / beamgate.cpp
1 #include "beamgate.h"
2 #include "catalogue.h"
3 #include "layout.h"
4
5 using namespace std;
6 using namespace Msp;
7
8 namespace R2C2 {
9
10 BeamGate::BeamGate(Layout &l):
11         TrackAttachment(l),
12         Sensor(l)
13 {
14         invert = true;
15
16         layout.add(*this);
17 }
18
19 BeamGate::~BeamGate()
20 {
21         layout.remove(*this);
22 }
23
24 BeamGate *BeamGate::clone(Layout *to_layout) const
25 {
26         BeamGate *gate = new BeamGate(to_layout ? *to_layout : layout);
27         gate->set_position(position);
28         gate->set_rotation(rotation);
29         return gate;
30 }
31
32 const BeamGateType &BeamGate::get_type() const
33 {
34         return BeamGateType::instance();
35 }
36
37 void BeamGate::set_address(unsigned a)
38 {
39         address = a;
40 }
41
42 void BeamGate::set_position(const Vector &p)
43 {
44         position = p;
45         update_attachment();
46         signal_moved.emit();
47 }
48
49 void BeamGate::set_rotation(const Angle &r)
50 {
51         rotation = r;
52         update_attachment();
53         signal_moved.emit();
54 }
55
56 void BeamGate::update_attachment()
57 {
58         attach_to_closest(100*layout.get_catalogue().get_gauge());
59
60         if(track)
61         {
62                 TrackPoint tp = track->get_point(track.entry(), offset);
63                 position = tp.pos;
64                 rotation = tp.dir;
65         }
66 }
67
68 Block *BeamGate::get_block() const
69 {
70         if(track)
71                 return &track->get_block();
72         else
73                 return 0;
74 }
75
76 void BeamGate::save(list<DataFile::Statement> &st) const
77 {
78         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
79         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
80         if(address)
81                 st.push_back((DataFile::Statement("address"), address));
82 }
83
84
85 BeamGate::Loader::Loader(BeamGate &g):
86         DataFile::ObjectLoader<BeamGate>(g)
87 {
88         add("address",  &Loader::address);
89         add("position", &Loader::position);
90         add("rotation", &Loader::rotation);
91 }
92
93 void BeamGate::Loader::address(unsigned a)
94 {
95         obj.set_address(a);
96 }
97
98 void BeamGate::Loader::position(float x, float y, float z)
99 {
100         obj.set_position(Vector(x, y, z));
101 }
102
103 void BeamGate::Loader::rotation(float r)
104 {
105         obj.set_rotation(Angle::from_radians(r));
106 }
107
108 } // namespace R2C2