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