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