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