]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/track.cpp
Move double-address logic to drivers
[r2c2.git] / source / libr2c2 / track.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include "block.h"
10 #include "catalogue.h"
11 #include "driver.h"
12 #include "layout.h"
13 #include "track.h"
14 #include "tracktype.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 namespace R2C2 {
20
21 Track::Track(Layout &l, const TrackType &t):
22         layout(l),
23         type(t),
24         block(0),
25         rot(0),
26         slope(0),
27         flex(false),
28         turnout_id(0),
29         sensor_id(0),
30         links(type.get_endpoints().size()),
31         active_path(0)
32 {
33         if(type.is_turnout())
34                 turnout_id = layout.allocate_turnout_id();
35
36         layout.add_track(*this);
37
38         if(layout.has_driver())
39                 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
40
41         for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
42 }
43
44 Track::~Track()
45 {
46         break_links();
47         layout.remove_track(*this);
48 }
49
50 void Track::set_block(Block *b)
51 {
52         if(b && !b->has_track(*this))
53                 throw InvalidParameterValue("Track is not in the Block");
54         if(!b && block && block->has_track(*this))
55                 throw InvalidState("Track is still in a Block");
56
57         block = b;
58 }
59
60 Block &Track::get_block() const
61 {
62         if(!block)
63                 throw InvalidState("No Block");
64
65         return *block;
66 }
67
68 void Track::set_position(const Point &p)
69 {
70         pos = p;
71 }
72
73 void Track::set_rotation(float r)
74 {
75         rot = r;
76         while(rot<0)
77                 rot += M_PI*2;
78         while(rot>M_PI*2)
79                 rot -= M_PI*2;
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                 Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
103                 Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
104                 pos.z = epp0.z;
105                 slope = epp1.z-pos.z;
106         }
107         else
108         {
109                 slope = 0;
110                 if(links[0])
111                 {
112                         Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
113                         pos.z = epp.z;
114                 }
115                 else if(links[1])
116                 {
117                         Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
118                         pos.z = epp.z;
119                 }
120         }
121 }
122
123 void Track::set_turnout_id(unsigned i)
124 {
125         if(!type.is_turnout())
126                 throw InvalidState("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 InvalidState("Can't set sensor on 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 InvalidState("Not a turnout");
150         if(!(type.get_paths()&(1<<p)))
151                 throw InvalidParameterValue("Invalid path");
152
153         layout.get_driver().set_turnout(turnout_id, p);
154 }
155
156 int Track::get_endpoint_by_link(Track &other) const
157 {
158         for(unsigned i=0; i<links.size(); ++i)
159                 if(links[i]==&other)
160                         return i;
161
162         return -1;
163 }
164
165 Point Track::get_endpoint_position(unsigned epi) const
166 {
167         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
168         if(epi>=eps.size())
169                 throw InvalidParameterValue("TrackType::Endpoint index out of range");
170
171         const TrackType::Endpoint &ep = eps[epi];
172
173         float c = cos(rot);
174         float s = sin(rot);
175
176         Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
177         if(eps.size()==2 && epi==1)
178                 p.z += slope;
179         return p;
180 }
181
182 float Track::get_endpoint_direction(unsigned epi) const
183 {
184         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
185         if(epi>=eps.size())
186                 throw InvalidParameterValue("TrackType::Endpoint index out of range");
187
188         const TrackType::Endpoint &ep = eps[epi];
189
190         return rot+ep.dir;
191 }
192
193 bool Track::snap_to(Track &other, bool link, float limit)
194 {
195         if(!limit || link)
196         {
197                 limit = layout.get_catalogue().get_gauge();
198                 if(link && !flex && !other.get_flex())
199                         limit /= 10;
200         }
201         limit *= limit;
202
203         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
204         const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
205
206         for(unsigned i=0; i<eps.size(); ++i)
207         {
208                 Point epp = get_endpoint_position(i);
209
210                 for(unsigned j=0; j<other_eps.size(); ++j)
211                 {
212                         if(other.get_link(j))
213                                 continue;
214
215                         Point epp2 = other.get_endpoint_position(j);
216                         float dx = epp2.x-epp.x;
217                         float dy = epp2.y-epp.y;
218                         float dz = epp2.z-epp.z;
219                         if(dx*dx+dy*dy<limit && dz*dz<limit)
220                         {
221                                 if(!link || (!flex && !other.get_flex()))
222                                 {
223                                         set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
224                                         Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
225                                                 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
226                                                 epp2.z);
227                                         if(eps.size()==2 && i==1)
228                                                 p.z -= slope;
229                                         set_position(p);
230                                 }
231
232                                 if(link)
233                                 {
234                                         if(links[i])
235                                                 break_link(*links[i]);
236                                         links[i] = &other;
237                                         other.links[j] = this;
238                                         layout.create_blocks(*this);
239                                 }
240
241                                 return true;
242                         }
243                 }
244         }
245
246         return false;
247 }
248
249 bool Track::snap(Point &pt, float &d) const
250 {
251         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
252
253         for(unsigned i=0; i<eps.size(); ++i)
254         {
255                 Point epp = get_endpoint_position(i);
256                 float dx = pt.x-epp.x;
257                 float dy = pt.y-epp.y;
258                 if(dx*dx+dy*dy<1e-4)
259                 {
260                         pt = epp;
261                         d = rot+eps[i].dir;
262                         return true;
263                 }
264         }
265
266         return false;
267 }
268
269 void Track::break_link(Track &trk)
270 {
271         for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
272                 if(*i==&trk)
273                 {
274                         *i = 0;
275                         trk.break_link(*this);
276                         // XXX Creates the blocks twice
277                         layout.create_blocks(*this);
278                         return;
279                 }
280 }
281
282 void Track::break_links()
283 {
284         for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
285                 if(Track *trk=*i)
286                 {
287                         *i = 0;
288                         trk->break_link(*this);
289                 }
290 }
291
292 Track *Track::get_link(unsigned i) const
293 {
294         if(i>links.size())
295                 throw InvalidParameterValue("Link index out of range");
296
297         return links[i];
298 }
299
300 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
301 {
302         TrackPoint p = type.get_point(epi, path, d);
303         float c = cos(rot);
304         float s = sin(rot);
305
306         p.pos = Point(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, pos.z);
307         p.dir += rot;
308         if(type.get_endpoints().size()==2)
309         {
310                 float len = type.get_path_length(path);
311                 float grade = slope/len;
312                 if(epi==0)
313                 {
314                         p.pos.z += grade*d;
315                         p.grade = grade;
316                 }
317                 else
318                 {
319                         p.pos.z += slope-grade*d;
320                         p.grade = -grade;
321                 }
322         }
323
324         return p;
325 }
326
327 TrackPoint Track::get_point(unsigned epi, float d) const
328 {
329         return get_point(epi, active_path, d);
330 }
331
332 void Track::save(list<DataFile::Statement> &st) const
333 {
334         st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
335         st.push_back((DataFile::Statement("rotation"), rot));
336         st.push_back((DataFile::Statement("slope"), slope));
337         if(turnout_id)
338                 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
339         if(sensor_id)
340                 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
341         if(flex)
342                 st.push_back((DataFile::Statement("flex"), true));
343 }
344
345 void Track::turnout_event(unsigned addr, unsigned state)
346 {
347         if(!turnout_id)
348                 return;
349
350         if(addr==turnout_id)
351         {
352                 active_path = state;
353                 signal_path_changed.emit(active_path);
354         }
355 }
356
357
358 Track::Loader::Loader(Track &t):
359         DataFile::BasicLoader<Track>(t)
360 {
361         add("position",   &Loader::position);
362         add("rotation",   &Track::rot);
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.pos = Point(x, y, z);
372 }
373
374 void Track::Loader::sensor_id(unsigned id)
375 {
376         obj.set_sensor_id(id);
377 }
378
379 void Track::Loader::turnout_id(unsigned id)
380 {
381         obj.set_turnout_id(id);
382 }
383
384 } // namespace R2C2