]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/track.cpp
Ensure that turnout tracks have a turnout id
[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         path_changing = true;
155         layout.get_driver().set_turnout(turnout_id, p);
156 }
157
158 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
159 {
160         TrackPoint p = type.get_point(epi, path, d);
161
162         p.pos = position+rotated_vector(p.pos, rotation);
163         p.dir += rotation;
164         if(type.get_endpoints().size()==2)
165         {
166                 float len = type.get_path_length(path);
167                 float grade = slope/len;
168                 if(epi==0)
169                 {
170                         p.pos.z += grade*d;
171                         p.grade = grade;
172                 }
173                 else
174                 {
175                         p.pos.z += slope-grade*d;
176                         p.grade = -grade;
177                 }
178         }
179
180         return p;
181 }
182
183 TrackPoint Track::get_point(unsigned epi, float d) const
184 {
185         return get_point(epi, active_path, d);
186 }
187
188 unsigned Track::get_n_snap_nodes() const
189 {
190         return type.get_endpoints().size();
191 }
192
193 Snap Track::get_snap_node(unsigned i) const
194 {
195         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
196         if(i>=eps.size())
197                 throw out_of_range("Track::get_snap_node");
198
199         Snap result;
200         const TrackType::Endpoint &ep = eps[i];
201
202         result.position = position+rotated_vector(ep.pos, rotation);
203         if(eps.size()==2 && i==1)
204                 result.position.z += slope;
205
206         result.rotation = rotation+ep.dir;
207
208         return result;
209 }
210
211 bool Track::snap(Snap &sn, float limit, SnapType what) const
212 {
213         if(Object::snap(sn, limit, what))
214                 return true;
215
216         if(what&SNAP_SEGMENT)
217         {
218                 Vector local = rotated_vector(sn.position-position, -rotation);
219
220                 TrackPoint tp = type.get_nearest_point(local);
221                 Vector span = local-tp.pos;
222                 if(dot(span, span)<=limit*limit)
223                 {
224                         sn.position = position+rotated_vector(tp.pos, rotation);
225                         sn.rotation = tp.dir+rotation;
226                         return true;
227                 }
228         }
229
230         return false;
231 }
232
233 SnapType Track::get_default_snap_type_to(const Object &other) const
234 {
235         if(dynamic_cast<const Track *>(&other))
236                 return SNAP_NODE;
237
238         return NO_SNAP;
239 }
240
241 unsigned Track::get_n_link_slots() const
242 {
243         return links.size();
244 }
245
246 Track *Track::get_link(unsigned i) const
247 {
248         if(i>=links.size())
249                 throw out_of_range("Track::get_link");
250
251         return links[i];
252 }
253
254 int Track::get_link_slot(const Object &other) const
255 {
256         for(unsigned i=0; i<links.size(); ++i)
257                 if(links[i]==&other)
258                         return i;
259
260         return -1;
261 }
262
263 bool Track::link_to(Object &other)
264 {
265         Track *otrack = dynamic_cast<Track *>(&other);
266         if(!otrack)
267                 return false;
268
269         float limit = layout.get_catalogue().get_gauge();
270         if(!flex && !otrack->get_flex())
271                 limit /= 10;
272         limit *= limit;
273
274         unsigned nsn = get_n_snap_nodes();
275         unsigned other_nsn = other.get_n_snap_nodes();
276         for(unsigned i=0; i<nsn; ++i)
277         {
278                 Snap sn = get_snap_node(i);
279                 for(unsigned j=0; j<other_nsn; ++j)
280                 {
281                         Snap osn = other.get_snap_node(j);
282                         Vector span = osn.position-sn.position;
283                         Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
284
285                         if(dot(span, span)<limit && abs(da).radians()<0.01)
286                         {
287                                 break_link(i);
288                                 links[i] = otrack;
289                                 otrack->links[j] = this;
290                                 check_slope();
291                                 layout.create_blocks(*this);
292
293                                 signal_link_changed.emit(i, otrack);
294                                 otrack->signal_link_changed.emit(j, this);
295                                 return true;
296                         }
297                 }
298         }
299
300         return false;
301 }
302
303 bool Track::break_link(unsigned i)
304 {
305         if(i>=links.size())
306                 throw out_of_range("Track::break_link");
307
308         Track *other = links[i];
309         if(!other)
310                 return false;
311
312         links[i] = 0;
313         other->break_link(*this);
314         // XXX Creates the blocks twice, because the other track calls this too
315         layout.create_blocks(*this);
316         signal_link_changed.emit(i, 0);
317
318         return true;
319 }
320
321 void Track::save(list<DataFile::Statement> &st) const
322 {
323         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
324         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
325         st.push_back((DataFile::Statement("slope"), slope));
326         if(turnout_id)
327                 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
328         if(sensor_id)
329                 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
330         if(flex)
331                 st.push_back((DataFile::Statement("flex"), true));
332 }
333
334 void Track::turnout_event(unsigned addr, unsigned state)
335 {
336         if(!turnout_id)
337                 return;
338
339         if(addr==turnout_id)
340         {
341                 active_path = state;
342                 path_changing = false;
343                 signal_path_changed.emit(active_path);
344         }
345 }
346
347
348 Track::Loader::Loader(Track &t):
349         DataFile::ObjectLoader<Track>(t)
350 {
351         add("position",   &Loader::position);
352         add("rotation",   &Loader::rotation);
353         add("slope",      &Track::slope);
354         add("turnout_id", &Loader::turnout_id);
355         add("sensor_id",  &Loader::sensor_id);
356         add("flex",       &Track::flex);
357 }
358
359 void Track::Loader::position(float x, float y, float z)
360 {
361         obj.position = Vector(x, y, z);
362 }
363
364 void Track::Loader::rotation(float r)
365 {
366         obj.rotation = Angle::from_radians(r);
367 }
368
369 void Track::Loader::sensor_id(unsigned id)
370 {
371         obj.set_sensor_id(id);
372 }
373
374 void Track::Loader::turnout_id(unsigned id)
375 {
376         obj.set_turnout_id(id);
377 }
378
379 } // namespace R2C2