]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/track.cpp
Emit a signal before changing the path of a turnout
[r2c2.git] / source / libr2c2 / track.cpp
1 #include <cmath>
2 #include "block.h"
3 #include "catalogue.h"
4 #include "driver.h"
5 #include "layout.h"
6 #include "track.h"
7 #include "tracktype.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 namespace R2C2 {
13
14 Track::Track(Layout &l, const TrackType &t):
15         Object(l),
16         type(t),
17         block(0),
18         slope(0),
19         flex(false),
20         turnout_id(0),
21         sensor_id(0),
22         links(type.get_endpoints().size()),
23         active_path(0),
24         path_changing(false)
25 {
26         if(type.is_turnout())
27                 turnout_id = layout.allocate_turnout_id();
28
29         layout.add(*this);
30
31         if(layout.has_driver())
32                 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
33
34         for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
35 }
36
37 Track::~Track()
38 {
39         break_links();
40         layout.remove(*this);
41 }
42
43 Track *Track::clone(Layout *to_layout) const
44 {
45         Track *track = new Track((to_layout ? *to_layout : layout), type);
46         track->set_position(position);
47         track->set_rotation(rotation);
48         return track;
49 }
50
51 void Track::set_block(Block *b)
52 {
53         if(b && !b->has_track(*this))
54                 throw logic_error("track not in block");
55         if(!b && block && block->has_track(*this))
56                 throw logic_error("track still in block");
57
58         block = b;
59 }
60
61 Block &Track::get_block() const
62 {
63         if(!block)
64                 throw logic_error("!block");
65
66         return *block;
67 }
68
69 void Track::set_position(const Vector &p)
70 {
71         position = p;
72         for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
73                 if(*i)
74                         (*i)->check_slope();
75 }
76
77 void Track::set_rotation(const Angle &r)
78 {
79         rotation = wrap_positive(r);
80 }
81
82 void Track::set_slope(float s)
83 {
84         if(links.size()!=2)
85                 return;
86
87         slope = s;
88 }
89
90 void Track::set_flex(bool f)
91 {
92         flex = f;
93 }
94
95 void Track::check_slope()
96 {
97         if(links.size()!=2)
98                 return;
99
100         if(links[0] && links[1])
101         {
102                 Vector epp0 = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
103                 Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
104                 position.z = epp0.z;
105                 slope = epp1.z-position.z;
106         }
107         else
108         {
109                 if(links[0])
110                 {
111                         Vector epp = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
112                         position.z = epp.z;
113                 }
114                 else if(links[1])
115                 {
116                         Vector epp = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
117                         position.z = epp.z-slope;
118                 }
119         }
120 }
121
122 void Track::set_turnout_id(unsigned i)
123 {
124         if(!type.is_turnout())
125                 throw logic_error("not a turnout");
126         if(!i)
127                 throw invalid_argument("Track::set_turnout_id");
128
129         turnout_id = i;
130         layout.create_blocks(*this);
131         layout.update_routes();
132         if(layout.has_driver() && turnout_id)
133                 layout.get_driver().add_turnout(turnout_id, type);
134 }
135
136 void Track::set_sensor_id(unsigned i)
137 {
138         if(type.is_turnout())
139                 throw logic_error("is a turnout");
140
141         sensor_id = i;
142         layout.create_blocks(*this);
143         if(layout.has_driver() && sensor_id)
144                 layout.get_driver().add_sensor(sensor_id);
145 }
146
147 void Track::set_active_path(unsigned p)
148 {
149         if(!turnout_id)
150                 throw logic_error("not a turnout");
151         if(!(type.get_paths()&(1<<p)))
152                 throw invalid_argument("Track::set_active_path");
153
154         signal_path_changing(p);
155         path_changing = true;
156         layout.get_driver().set_turnout(turnout_id, p);
157 }
158
159 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
160 {
161         TrackPoint p = type.get_point(epi, path, d);
162
163         p.pos = position+rotated_vector(p.pos, rotation);
164         p.dir += rotation;
165         if(type.get_endpoints().size()==2)
166         {
167                 float len = type.get_path_length(path);
168                 float grade = slope/len;
169                 if(epi==0)
170                 {
171                         p.pos.z += grade*d;
172                         p.grade = grade;
173                 }
174                 else
175                 {
176                         p.pos.z += slope-grade*d;
177                         p.grade = -grade;
178                 }
179         }
180
181         return p;
182 }
183
184 TrackPoint Track::get_point(unsigned epi, float d) const
185 {
186         return get_point(epi, active_path, d);
187 }
188
189 unsigned Track::get_n_snap_nodes() const
190 {
191         return type.get_endpoints().size();
192 }
193
194 Snap Track::get_snap_node(unsigned i) const
195 {
196         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
197         if(i>=eps.size())
198                 throw out_of_range("Track::get_snap_node");
199
200         Snap result;
201         const TrackType::Endpoint &ep = eps[i];
202
203         result.position = position+rotated_vector(ep.pos, rotation);
204         if(eps.size()==2 && i==1)
205                 result.position.z += slope;
206
207         result.rotation = rotation+ep.dir;
208
209         return result;
210 }
211
212 bool Track::snap(Snap &sn, float limit, SnapType what) const
213 {
214         if(Object::snap(sn, limit, what))
215                 return true;
216
217         if(what&SNAP_SEGMENT)
218         {
219                 Vector local = rotated_vector(sn.position-position, -rotation);
220
221                 TrackPoint tp = type.get_nearest_point(local);
222                 Vector span = local-tp.pos;
223                 if(dot(span, span)<=limit*limit)
224                 {
225                         sn.position = position+rotated_vector(tp.pos, rotation);
226                         sn.rotation = tp.dir+rotation;
227                         return true;
228                 }
229         }
230
231         return false;
232 }
233
234 SnapType Track::get_default_snap_type_to(const Object &other) const
235 {
236         if(dynamic_cast<const Track *>(&other))
237                 return SNAP_NODE;
238
239         return NO_SNAP;
240 }
241
242 unsigned Track::get_n_link_slots() const
243 {
244         return links.size();
245 }
246
247 Track *Track::get_link(unsigned i) const
248 {
249         if(i>=links.size())
250                 throw out_of_range("Track::get_link");
251
252         return links[i];
253 }
254
255 int Track::get_link_slot(const Object &other) const
256 {
257         for(unsigned i=0; i<links.size(); ++i)
258                 if(links[i]==&other)
259                         return i;
260
261         return -1;
262 }
263
264 bool Track::link_to(Object &other)
265 {
266         Track *otrack = dynamic_cast<Track *>(&other);
267         if(!otrack)
268                 return false;
269
270         float limit = layout.get_catalogue().get_gauge();
271         if(!flex && !otrack->get_flex())
272                 limit /= 10;
273         limit *= limit;
274
275         unsigned nsn = get_n_snap_nodes();
276         unsigned other_nsn = other.get_n_snap_nodes();
277         for(unsigned i=0; i<nsn; ++i)
278         {
279                 Snap sn = get_snap_node(i);
280                 for(unsigned j=0; j<other_nsn; ++j)
281                 {
282                         Snap osn = other.get_snap_node(j);
283                         Vector span = osn.position-sn.position;
284                         Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
285
286                         if(dot(span, span)<limit && abs(da).radians()<0.01)
287                         {
288                                 break_link(i);
289                                 links[i] = otrack;
290                                 otrack->links[j] = this;
291                                 check_slope();
292                                 layout.create_blocks(*this);
293
294                                 signal_link_changed.emit(i, otrack);
295                                 otrack->signal_link_changed.emit(j, this);
296                                 return true;
297                         }
298                 }
299         }
300
301         return false;
302 }
303
304 bool Track::break_link(unsigned i)
305 {
306         if(i>=links.size())
307                 throw out_of_range("Track::break_link");
308
309         Track *other = links[i];
310         if(!other)
311                 return false;
312
313         links[i] = 0;
314         other->break_link(*this);
315         // XXX Creates the blocks twice, because the other track calls this too
316         layout.create_blocks(*this);
317         signal_link_changed.emit(i, 0);
318
319         return true;
320 }
321
322 void Track::save(list<DataFile::Statement> &st) const
323 {
324         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
325         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
326         st.push_back((DataFile::Statement("slope"), slope));
327         if(turnout_id)
328                 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
329         if(sensor_id)
330                 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
331         if(flex)
332                 st.push_back((DataFile::Statement("flex"), true));
333 }
334
335 void Track::turnout_event(unsigned addr, unsigned state)
336 {
337         if(!turnout_id)
338                 return;
339
340         if(addr==turnout_id)
341         {
342                 active_path = state;
343                 path_changing = false;
344                 signal_path_changed.emit(active_path);
345         }
346 }
347
348
349 Track::Loader::Loader(Track &t):
350         DataFile::ObjectLoader<Track>(t)
351 {
352         add("position",   &Loader::position);
353         add("rotation",   &Loader::rotation);
354         add("slope",      &Track::slope);
355         add("turnout_id", &Loader::turnout_id);
356         add("sensor_id",  &Loader::sensor_id);
357         add("flex",       &Track::flex);
358 }
359
360 void Track::Loader::position(float x, float y, float z)
361 {
362         obj.position = Vector(x, y, z);
363 }
364
365 void Track::Loader::rotation(float r)
366 {
367         obj.rotation = Angle::from_radians(r);
368 }
369
370 void Track::Loader::sensor_id(unsigned id)
371 {
372         obj.set_sensor_id(id);
373 }
374
375 void Track::Loader::turnout_id(unsigned id)
376 {
377         obj.set_turnout_id(id);
378 }
379
380 } // namespace R2C2