]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/track.cpp
Add a flag to Track to indicate that its path is changing
[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         layout(l),
16         type(t),
17         block(0),
18         rot(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_track(*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_track(*this);
42 }
43
44 void Track::set_block(Block *b)
45 {
46         if(b && !b->has_track(*this))
47                 throw logic_error("track not in block");
48         if(!b && block && block->has_track(*this))
49                 throw logic_error("track still in block");
50
51         block = b;
52 }
53
54 Block &Track::get_block() const
55 {
56         if(!block)
57                 throw logic_error("!block");
58
59         return *block;
60 }
61
62 void Track::set_position(const Vector &p)
63 {
64         pos = p;
65 }
66
67 void Track::set_rotation(float r)
68 {
69         rot = r;
70         while(rot<0)
71                 rot += M_PI*2;
72         while(rot>M_PI*2)
73                 rot -= M_PI*2;
74 }
75
76 void Track::set_slope(float s)
77 {
78         if(links.size()!=2)
79                 return;
80
81         slope = s;
82 }
83
84 void Track::set_flex(bool f)
85 {
86         flex = f;
87 }
88
89 void Track::check_slope()
90 {
91         if(links.size()!=2)
92                 return;
93
94         if(links[0] && links[1])
95         {
96                 Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
97                 Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
98                 pos.z = epp0.z;
99                 slope = epp1.z-pos.z;
100         }
101         else
102         {
103                 slope = 0;
104                 if(links[0])
105                 {
106                         Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
107                         pos.z = epp.z;
108                 }
109                 else if(links[1])
110                 {
111                         Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
112                         pos.z = epp.z;
113                 }
114         }
115 }
116
117 void Track::set_turnout_id(unsigned i)
118 {
119         if(!type.is_turnout())
120                 throw logic_error("not a turnout");
121
122         turnout_id = i;
123         layout.create_blocks(*this);
124         layout.update_routes();
125         if(layout.has_driver() && turnout_id)
126                 layout.get_driver().add_turnout(turnout_id, type);
127 }
128
129 void Track::set_sensor_id(unsigned i)
130 {
131         if(type.is_turnout())
132                 throw logic_error("is a turnout");
133
134         sensor_id = i;
135         layout.create_blocks(*this);
136         if(layout.has_driver() && sensor_id)
137                 layout.get_driver().add_sensor(sensor_id);
138 }
139
140 void Track::set_active_path(unsigned p)
141 {
142         if(!turnout_id)
143                 throw logic_error("not a turnout");
144         if(!(type.get_paths()&(1<<p)))
145                 throw invalid_argument("Track::set_active_path");
146
147         path_changing = true;
148         layout.get_driver().set_turnout(turnout_id, p);
149 }
150
151 int Track::get_endpoint_by_link(Track &other) const
152 {
153         for(unsigned i=0; i<links.size(); ++i)
154                 if(links[i]==&other)
155                         return i;
156
157         return -1;
158 }
159
160 Vector Track::get_endpoint_position(unsigned epi) const
161 {
162         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
163         if(epi>=eps.size())
164                 throw out_of_range("Track::get_endpoint_position");
165
166         const TrackType::Endpoint &ep = eps[epi];
167
168         float c = cos(rot);
169         float s = sin(rot);
170
171         Vector p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
172         if(eps.size()==2 && epi==1)
173                 p.z += slope;
174         return p;
175 }
176
177 float Track::get_endpoint_direction(unsigned epi) const
178 {
179         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
180         if(epi>=eps.size())
181                 throw out_of_range("Track::get_endpoint_direction");
182
183         const TrackType::Endpoint &ep = eps[epi];
184
185         return rot+ep.dir;
186 }
187
188 bool Track::snap_to(Track &other, bool link, float limit)
189 {
190         if(!limit || link)
191         {
192                 limit = layout.get_catalogue().get_gauge();
193                 if(link && !flex && !other.get_flex())
194                         limit /= 10;
195         }
196         limit *= limit;
197
198         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
199         const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
200
201         for(unsigned i=0; i<eps.size(); ++i)
202         {
203                 Vector epp = get_endpoint_position(i);
204
205                 for(unsigned j=0; j<other_eps.size(); ++j)
206                 {
207                         if(other.get_link(j))
208                                 continue;
209
210                         Vector epp2 = other.get_endpoint_position(j);
211                         float dx = epp2.x-epp.x;
212                         float dy = epp2.y-epp.y;
213                         float dz = epp2.z-epp.z;
214                         if(dx*dx+dy*dy<limit && dz*dz<limit)
215                         {
216                                 if(!link || (!flex && !other.get_flex()))
217                                 {
218                                         set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
219                                         Vector p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
220                                                 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
221                                                 epp2.z);
222                                         if(eps.size()==2 && i==1)
223                                                 p.z -= slope;
224                                         set_position(p);
225                                 }
226
227                                 if(link)
228                                 {
229                                         if(links[i])
230                                                 break_link(*links[i]);
231                                         links[i] = &other;
232                                         other.links[j] = this;
233                                         layout.create_blocks(*this);
234
235                                         signal_link_changed.emit(i, &other);
236                                         other.signal_link_changed.emit(j, this);
237                                 }
238
239                                 return true;
240                         }
241                 }
242         }
243
244         return false;
245 }
246
247 bool Track::snap(Vector &pt, float &d) const
248 {
249         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
250
251         for(unsigned i=0; i<eps.size(); ++i)
252         {
253                 Vector epp = get_endpoint_position(i);
254                 float dx = pt.x-epp.x;
255                 float dy = pt.y-epp.y;
256                 if(dx*dx+dy*dy<1e-4)
257                 {
258                         pt = epp;
259                         d = rot+eps[i].dir;
260                         return true;
261                 }
262         }
263
264         return false;
265 }
266
267 void Track::break_link(Track &trk)
268 {
269         for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
270                 if(*i==&trk)
271                 {
272                         *i = 0;
273                         trk.break_link(*this);
274                         // XXX Creates the blocks twice
275                         layout.create_blocks(*this);
276                         signal_link_changed.emit(i-links.begin(), 0);
277                         return;
278                 }
279 }
280
281 void Track::break_links()
282 {
283         for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
284                 if(Track *trk=*i)
285                 {
286                         *i = 0;
287                         trk->break_link(*this);
288                 }
289 }
290
291 Track *Track::get_link(unsigned i) const
292 {
293         if(i>=links.size())
294                 throw out_of_range("Track::get_link");
295
296         return links[i];
297 }
298
299 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
300 {
301         TrackPoint p = type.get_point(epi, path, d);
302         float c = cos(rot);
303         float s = sin(rot);
304
305         p.pos = Vector(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, pos.z);
306         p.dir += rot;
307         if(type.get_endpoints().size()==2)
308         {
309                 float len = type.get_path_length(path);
310                 float grade = slope/len;
311                 if(epi==0)
312                 {
313                         p.pos.z += grade*d;
314                         p.grade = grade;
315                 }
316                 else
317                 {
318                         p.pos.z += slope-grade*d;
319                         p.grade = -grade;
320                 }
321         }
322
323         return p;
324 }
325
326 TrackPoint Track::get_point(unsigned epi, float d) const
327 {
328         return get_point(epi, active_path, d);
329 }
330
331 TrackPoint Track::get_nearest_point(const Vector &p) const
332 {
333         Vector local(p.x-pos.x, p.y-pos.y, p.z-pos.z);
334         float c = cos(rot);
335         float s = sin(rot);
336         local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z);
337
338         TrackPoint tp = type.get_nearest_point(local);
339         tp.pos = Vector(pos.x+tp.pos.x*c-tp.pos.y*s, pos.y+tp.pos.y*c+tp.pos.x*s, pos.z+tp.pos.z);
340         tp.dir += rot;
341         return tp;
342 }
343
344 bool Track::collide_ray(const Vector &start, const Vector &ray)
345 {
346         Vector local_start(start.x-pos.x, start.y-pos.y, start.z-pos.z);
347         float c = cos(rot);
348         float s = sin(rot);
349         local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
350         Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
351
352         float width = layout.get_catalogue().get_ballast_profile().get_width();
353
354         return type.collide_ray(local_start, local_ray, width);
355 }
356
357 void Track::save(list<DataFile::Statement> &st) const
358 {
359         st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
360         st.push_back((DataFile::Statement("rotation"), rot));
361         st.push_back((DataFile::Statement("slope"), slope));
362         if(turnout_id)
363                 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
364         if(sensor_id)
365                 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
366         if(flex)
367                 st.push_back((DataFile::Statement("flex"), true));
368 }
369
370 void Track::turnout_event(unsigned addr, unsigned state)
371 {
372         if(!turnout_id)
373                 return;
374
375         if(addr==turnout_id)
376         {
377                 active_path = state;
378                 path_changing = false;
379                 signal_path_changed.emit(active_path);
380         }
381 }
382
383
384 Track::Loader::Loader(Track &t):
385         DataFile::ObjectLoader<Track>(t)
386 {
387         add("position",   &Loader::position);
388         add("rotation",   &Track::rot);
389         add("slope",      &Track::slope);
390         add("turnout_id", &Loader::turnout_id);
391         add("sensor_id",  &Loader::sensor_id);
392         add("flex",       &Track::flex);
393 }
394
395 void Track::Loader::position(float x, float y, float z)
396 {
397         obj.pos = Vector(x, y, z);
398 }
399
400 void Track::Loader::sensor_id(unsigned id)
401 {
402         obj.set_sensor_id(id);
403 }
404
405 void Track::Loader::turnout_id(unsigned id)
406 {
407         obj.set_turnout_id(id);
408 }
409
410 } // namespace R2C2