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