]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/track.cpp
Foundations of using physics simulation for trains
[r2c2.git] / source / libmarklin / track.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include "driver.h"
10 #include "layout.h"
11 #include "track.h"
12 #include "tracktype.h"
13
14 using namespace std;
15 using namespace Msp;
16
17 namespace Marklin {
18
19 Track::Track(Layout &l, const TrackType &t):
20         layout(l),
21         type(t),
22         rot(0),
23         slope(0),
24         flex(false),
25         turnout_id(0),
26         sensor_id(0),
27         links(t.get_endpoints().size()),
28         active_path(0)
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_position(const Point &p)
45 {
46         pos = p;
47 }
48
49 void Track::set_rotation(float r)
50 {
51         rot = r;
52         while(rot<0)
53                 rot += M_PI*2;
54         while(rot>M_PI*2)
55                 rot -= M_PI*2;
56 }
57
58 void Track::set_slope(float s)
59 {
60         if(links.size()!=2)
61                 return;
62
63         slope = s;
64 }
65
66 void Track::set_flex(bool f)
67 {
68         flex = f;
69 }
70
71 void Track::check_slope()
72 {
73         if(links.size()!=2)
74                 return;
75
76         if(links[0] && links[1])
77         {
78                 Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
79                 Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
80                 pos.z = epp0.z;
81                 slope = epp1.z-pos.z;
82         }
83         else
84         {
85                 slope = 0;
86                 if(links[0])
87                 {
88                         Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
89                         pos.z = epp.z;
90                 }
91                 else if(links[1])
92                 {
93                         Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
94                         pos.z = epp.z;
95                 }
96         }
97 }
98
99 void Track::set_turnout_id(unsigned i)
100 {
101         if(!type.is_turnout())
102                 throw InvalidState("Not a turnout");
103
104         turnout_id = i;
105         layout.create_blocks(*this);
106         if(layout.has_driver() && turnout_id)
107         {
108                 layout.get_driver().add_turnout(turnout_id);
109                 if(type.is_double_address())
110                         layout.get_driver().add_turnout(turnout_id+1);
111         }
112 }
113
114 void Track::set_sensor_id(unsigned i)
115 {
116         if(type.is_turnout())
117                 throw InvalidState("Can't set sensor on a turnout");
118
119         sensor_id = i;
120         layout.create_blocks(*this);
121         if(layout.has_driver() && sensor_id)
122                 layout.get_driver().add_sensor(sensor_id);
123 }
124
125 void Track::set_active_path(unsigned p)
126 {
127         if(!turnout_id)
128                 throw InvalidState("Not a turnout");
129         if(!(type.get_paths()&(1<<p)))
130                 throw InvalidParameterValue("Invalid path");
131
132         layout.get_driver().set_turnout(turnout_id, p&1);
133         if(type.get_n_paths()>2)
134                 layout.get_driver().set_turnout(turnout_id+1, p&2);
135 }
136
137 int Track::get_endpoint_by_link(const Track &other) const
138 {
139         for(unsigned i=0; i<links.size(); ++i)
140                 if(links[i]==&other)
141                         return i;
142
143         return -1;
144 }
145
146 Point Track::get_endpoint_position(unsigned epi) const
147 {
148         const vector<Endpoint> &eps = type.get_endpoints();
149         if(epi>=eps.size())
150                 throw InvalidParameterValue("Endpoint index out of range");
151
152         const Endpoint &ep = eps[epi];
153
154         float c = cos(rot);
155         float s = sin(rot);
156
157         Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
158         if(eps.size()==2 && epi==1)
159                 p.z += slope;
160         return p;
161 }
162
163 float Track::get_endpoint_direction(unsigned epi) const
164 {
165         const vector<Endpoint> &eps = type.get_endpoints();
166         if(epi>=eps.size())
167                 throw InvalidParameterValue("Endpoint index out of range");
168
169         const Endpoint &ep = eps[epi];
170
171         return rot+ep.dir;
172 }
173
174 bool Track::snap_to(Track &other, bool link)
175 {
176         float limit = (link && !flex) ? 1e-6 : 1e-4;
177         const vector<Endpoint> &eps = type.get_endpoints();
178         const vector<Endpoint> &other_eps = other.get_type().get_endpoints();
179
180         for(unsigned i=0; i<eps.size(); ++i)
181         {
182                 Point epp = get_endpoint_position(i);
183
184                 for(unsigned j=0; j<other_eps.size(); ++j)
185                 {
186                         if(other.get_link(j))
187                                 continue;
188
189                         Point epp2 = other.get_endpoint_position(j);
190                         float dx = epp2.x-epp.x;
191                         float dy = epp2.y-epp.y;
192                         if(dx*dx+dy*dy<limit)
193                         {
194                                 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
195                                 Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
196                                         epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
197                                         epp2.z);
198                                 if(eps.size()==2 && i==1)
199                                         p.z -= slope;
200                                 set_position(p);
201
202                                 if(link)
203                                 {
204                                         if(links[i])
205                                                 break_link(*links[i]);
206                                         links[i] = &other;
207                                         other.links[j] = this;
208                                         layout.create_blocks(*this);
209                                 }
210
211                                 return true;
212                         }
213                 }
214         }
215
216         return false;
217 }
218
219 bool Track::snap(Point &pt, float &d) const
220 {
221         const vector<Endpoint> &eps = type.get_endpoints();
222
223         for(unsigned i=0; i<eps.size(); ++i)
224         {
225                 Point epp = get_endpoint_position(i);
226                 float dx = pt.x-epp.x;
227                 float dy = pt.y-epp.y;
228                 if(dx*dx+dy*dy<1e-4)
229                 {
230                         pt = epp;
231                         d = rot+eps[i].dir;
232                         return true;
233                 }
234         }
235
236         return false;
237 }
238
239 void Track::break_link(Track &trk)
240 {
241         for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
242                 if(*i==&trk)
243                 {
244                         *i = 0;
245                         trk.break_link(*this);
246                         // XXX Creates the blocks twice
247                         layout.create_blocks(*this);
248                         return;
249                 }
250 }
251
252 void Track::break_links()
253 {
254         for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
255                 if(Track *trk=*i)
256                 {
257                         *i = 0;
258                         trk->break_link(*this);
259                 }
260 }
261
262 Track *Track::get_link(unsigned i) const
263 {
264         if(i>links.size())
265                 throw InvalidParameterValue("Link index out of range");
266
267         return links[i];
268 }
269
270 unsigned Track::traverse(unsigned i, unsigned path) const
271 {
272         const vector<Endpoint> &eps = type.get_endpoints();
273         if(i>=eps.size())
274                 throw InvalidParameterValue("Endpoint index out of range");
275
276         const Endpoint &ep = eps[i];
277         
278         if(ep.paths&(1<<path))
279         {
280                 // Find the other endpoint for this path
281                 for(unsigned j=0; j<eps.size(); ++j)
282                         if((eps[j].paths&(1<<path)) && j!=i)
283                                 return j;
284         }
285         else
286         {
287                 // Find an endpoint that's connected to this one and has the requested path
288                 for(unsigned j=0; j<eps.size(); ++j)
289                         if((eps[j].paths&(1<<path)) && (eps[j].paths&ep.paths))
290                                 return j;
291         }
292
293         throw Exception("Track endpoint did not have a counterpart");
294 }
295
296 unsigned Track::traverse(unsigned i) const
297 {
298         return traverse(i, active_path);
299 }
300
301 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
302 {
303         TrackPoint p = type.get_point(epi, path, d);
304         float c = cos(rot);
305         float s = sin(rot);
306
307         p.pos = Point(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, 0);
308         p.dir += rot;
309         if(type.get_endpoints().size()==2)
310         {
311                 float len = type.get_path_length(path);
312                 float grade = slope/len;
313                 if(epi==0)
314                 {
315                         p.pos.z = pos.z+grade*d;
316                         p.grade = grade;
317                 }
318                 else
319                 {
320                         p.pos.z = pos.z+slope-grade*d;
321                         p.grade = -grade;
322                 }
323         }
324
325         return p;
326 }
327
328 TrackPoint Track::get_point(unsigned epi, float d) const
329 {
330         return get_point(epi, active_path, d);
331 }
332
333 void Track::save(list<DataFile::Statement> &st) const
334 {
335         st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
336         st.push_back((DataFile::Statement("rotation"), rot));
337         st.push_back((DataFile::Statement("slope"), slope));
338         if(turnout_id)
339                 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
340         if(sensor_id)
341                 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
342         if(flex)
343                 st.push_back((DataFile::Statement("flex"), true));
344 }
345
346 void Track::turnout_event(unsigned addr, bool state)
347 {
348         if(!turnout_id)
349                 return;
350
351         if(addr==turnout_id)
352                 active_path = (active_path&2) | (state ? 1 : 0);
353         else if(type.is_double_address() && addr==turnout_id+1)
354                 active_path = (active_path&1) | (state ? 2 : 0);
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 Marklin