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));
35 for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
41 layout.remove_track(*this);
44 void Track::set_position(const Point &p)
49 void Track::set_rotation(float r)
58 void Track::set_slope(float s)
66 void Track::set_flex(bool f)
71 void Track::check_slope()
76 if(links[0] && links[1])
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));
88 Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
93 Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
99 void Track::set_turnout_id(unsigned i)
101 if(!type.is_turnout())
102 throw InvalidState("Not a turnout");
105 layout.create_blocks(*this);
106 if(layout.has_driver() && turnout_id)
108 layout.get_driver().add_turnout(turnout_id);
109 if(type.is_double_address())
110 layout.get_driver().add_turnout(turnout_id+1);
114 void Track::set_sensor_id(unsigned i)
116 if(type.is_turnout())
117 throw InvalidState("Can't set sensor on a turnout");
120 layout.create_blocks(*this);
121 if(layout.has_driver() && sensor_id)
122 layout.get_driver().add_sensor(sensor_id);
125 void Track::set_active_path(unsigned p)
128 throw InvalidState("Not a turnout");
129 if(!(type.get_paths()&(1<<p)))
130 throw InvalidParameterValue("Invalid path");
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);
137 int Track::get_endpoint_by_link(const Track &other) const
139 for(unsigned i=0; i<links.size(); ++i)
146 Point Track::get_endpoint_position(unsigned epi) const
148 const vector<Endpoint> &eps = type.get_endpoints();
150 throw InvalidParameterValue("Endpoint index out of range");
152 const Endpoint &ep = eps[epi];
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)
163 float Track::get_endpoint_direction(unsigned epi) const
165 const vector<Endpoint> &eps = type.get_endpoints();
167 throw InvalidParameterValue("Endpoint index out of range");
169 const Endpoint &ep = eps[epi];
174 bool Track::snap_to(Track &other, bool link)
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();
180 for(unsigned i=0; i<eps.size(); ++i)
182 Point epp = get_endpoint_position(i);
184 for(unsigned j=0; j<other_eps.size(); ++j)
186 if(other.get_link(j))
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)
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)),
198 if(eps.size()==2 && i==1)
205 break_link(*links[i]);
207 other.links[j] = this;
208 layout.create_blocks(*this);
219 bool Track::snap(Point &pt, float &d) const
221 const vector<Endpoint> &eps = type.get_endpoints();
223 for(unsigned i=0; i<eps.size(); ++i)
225 Point epp = get_endpoint_position(i);
226 float dx = pt.x-epp.x;
227 float dy = pt.y-epp.y;
239 void Track::break_link(Track &trk)
241 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
245 trk.break_link(*this);
246 // XXX Creates the blocks twice
247 layout.create_blocks(*this);
252 void Track::break_links()
254 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
258 trk->break_link(*this);
262 Track *Track::get_link(unsigned i) const
265 throw InvalidParameterValue("Link index out of range");
270 unsigned Track::traverse(unsigned i, unsigned path) const
272 const vector<Endpoint> &eps = type.get_endpoints();
274 throw InvalidParameterValue("Endpoint index out of range");
276 const Endpoint &ep = eps[i];
278 if(ep.paths&(1<<path))
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)
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))
293 throw Exception("Track endpoint did not have a counterpart");
296 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
298 TrackPoint p = type.get_point(epi, path, d);
302 p.pos = Point(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, 0);
303 if(type.get_endpoints().size()==2)
305 float len = type.get_path_length(path);
306 float grade = slope/len;
309 p.pos.z = pos.z+grade*d;
314 p.pos.z = pos.z+slope-grade*d;
322 void Track::save(list<DataFile::Statement> &st) const
324 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
325 st.push_back((DataFile::Statement("rotation"), rot));
326 st.push_back((DataFile::Statement("slope"), slope));
328 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
330 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
332 st.push_back((DataFile::Statement("flex"), true));
335 void Track::turnout_event(unsigned addr, bool state)
341 active_path = (active_path&2) | (state ? 1 : 0);
342 else if(type.is_double_address() && addr==turnout_id+1)
343 active_path = (active_path&1) | (state ? 2 : 0);
347 Track::Loader::Loader(Track &t):
348 DataFile::BasicLoader<Track>(t)
350 add("position", &Loader::position);
351 add("rotation", &Track::rot);
352 add("slope", &Track::slope);
353 add("turnout_id", &Loader::turnout_id);
354 add("sensor_id", &Loader::sensor_id);
355 add("flex", &Track::flex);
358 void Track::Loader::position(float x, float y, float z)
360 obj.pos = Point(x, y, z);
363 void Track::Loader::sensor_id(unsigned id)
365 obj.set_sensor_id(id);
368 void Track::Loader::turnout_id(unsigned id)
370 obj.set_turnout_id(id);
373 } // namespace Marklin