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