3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
12 #include "tracktype.h"
19 Track::Track(Layout &l, const TrackType &t):
27 links(t.get_endpoints().size()),
30 layout.add_track(*this);
32 if(layout.has_driver())
33 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
39 layout.remove_track(*this);
42 void Track::set_position(const Point &p)
47 void Track::set_rotation(float r)
56 void Track::set_slope(float s)
64 void Track::set_flex(bool f)
69 void Track::check_slope()
74 if(links[0] && links[1])
76 Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
77 Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
86 Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
91 Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
97 void Track::set_turnout_id(unsigned i)
99 if(!type.is_turnout())
100 throw InvalidState("Not a turnout");
103 layout.create_blocks(*this);
104 if(layout.has_driver() && turnout_id)
105 layout.get_driver().add_turnout(turnout_id);
108 void Track::set_sensor_id(unsigned i)
110 if(type.is_turnout())
111 throw InvalidState("Can't set sensor on a turnout");
114 layout.create_blocks(*this);
115 if(layout.has_driver() && sensor_id)
116 layout.get_driver().add_sensor(sensor_id);
119 void Track::set_active_path(unsigned p)
122 throw InvalidState("Not a turnout");
123 if(!(type.get_paths()&(1<<p)))
124 throw InvalidParameterValue("Invalid path");
126 layout.get_driver().set_turnout(turnout_id, p&1);
127 if(type.get_n_paths()>2)
128 layout.get_driver().set_turnout(turnout_id+1, p&2);
131 int Track::get_endpoint_by_link(const Track &other) const
133 for(unsigned i=0; i<links.size(); ++i)
140 Point Track::get_endpoint_position(unsigned epi) const
142 const vector<Endpoint> &eps = type.get_endpoints();
144 throw InvalidParameterValue("Endpoint index out of range");
146 const Endpoint &ep = eps[epi];
151 Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
152 if(eps.size()==2 && epi==1)
157 float Track::get_endpoint_direction(unsigned epi) const
159 const vector<Endpoint> &eps = type.get_endpoints();
161 throw InvalidParameterValue("Endpoint index out of range");
163 const Endpoint &ep = eps[epi];
168 bool Track::snap_to(Track &other, bool link)
170 float limit = (link && !flex) ? 1e-6 : 1e-4;
171 const vector<Endpoint> &eps = type.get_endpoints();
172 const vector<Endpoint> &other_eps = other.get_type().get_endpoints();
174 for(unsigned i=0; i<eps.size(); ++i)
176 Point epp = get_endpoint_position(i);
178 for(unsigned j=0; j<other_eps.size(); ++j)
180 if(other.get_link(j))
183 Point epp2 = other.get_endpoint_position(j);
184 float dx = epp2.x-epp.x;
185 float dy = epp2.y-epp.y;
186 if(dx*dx+dy*dy<limit)
188 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
189 Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
190 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
192 if(eps.size()==2 && i==1)
199 break_link(*links[i]);
201 other.links[j] = this;
202 layout.create_blocks(*this);
213 bool Track::snap(Point &pt, float &d) const
215 const vector<Endpoint> &eps = type.get_endpoints();
217 for(unsigned i=0; i<eps.size(); ++i)
219 Point epp = get_endpoint_position(i);
220 float dx = pt.x-epp.x;
221 float dy = pt.y-epp.y;
233 void Track::break_link(Track &trk)
235 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
239 trk.break_link(*this);
240 // XXX Creates the blocks twice
241 layout.create_blocks(*this);
246 void Track::break_links()
248 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
252 trk->break_link(*this);
256 Track *Track::get_link(unsigned i) const
259 throw InvalidParameterValue("Link index out of range");
264 unsigned Track::traverse(unsigned i, unsigned path) const
266 const vector<Endpoint> &eps = type.get_endpoints();
268 throw InvalidParameterValue("Endpoint index out of range");
270 const Endpoint &ep = eps[i];
272 if(ep.paths&(1<<path))
274 // Find the other endpoint for this path
275 for(unsigned j=0; j<eps.size(); ++j)
276 if((eps[j].paths&(1<<path)) && j!=i)
281 // Find an endpoint that's connected to this one and has the requested path
282 for(unsigned j=0; j<eps.size(); ++j)
283 if((eps[j].paths&(1<<path)) && (eps[j].paths&ep.paths))
287 throw Exception("Track endpoint did not have a counterpart");
290 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
292 TrackPoint p = type.get_point(epi, path, d);
296 p.pos = Point(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, 0);
297 if(type.get_endpoints().size()==2)
299 float len = type.get_path_length(path);
300 float grade = slope/len;
303 p.pos.z = pos.z+grade*d;
308 p.pos.z = pos.z+slope-grade*d;
316 void Track::save(list<DataFile::Statement> &st) const
318 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
319 st.push_back((DataFile::Statement("rotation"), rot));
320 st.push_back((DataFile::Statement("slope"), slope));
322 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
324 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
326 st.push_back((DataFile::Statement("flex"), true));
329 void Track::turnout_event(unsigned addr, bool state)
335 active_path = (active_path&2) | (state ? 1 : 0);
336 else if(type.is_double_address() && addr==turnout_id+1)
337 active_path = (active_path&1) | (state ? 2 : 0);
341 Track::Loader::Loader(Track &t):
342 DataFile::BasicLoader<Track>(t)
344 add("position", &Loader::position);
345 add("rotation", &Track::rot);
346 add("slope", &Track::slope);
347 add("turnout_id", &Loader::turnout_id);
348 add("sensor_id", &Loader::sensor_id);
349 add("flex", &Track::flex);
352 void Track::Loader::position(float x, float y, float z)
354 obj.pos = Point(x, y, z);
357 void Track::Loader::sensor_id(unsigned id)
359 obj.set_sensor_id(id);
362 void Track::Loader::turnout_id(unsigned id)
364 obj.set_turnout_id(id);
367 } // namespace Marklin