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)
58 if(links.size()!=2) return;
63 void Track::set_flex(bool f)
68 void Track::check_slope()
73 if(links[0] && links[1])
75 Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
76 Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
85 Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
90 Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
96 void Track::set_turnout_id(unsigned i)
98 if(!type.is_turnout())
99 throw InvalidState("Not a turnout");
102 layout.create_blocks(*this);
103 if(layout.has_driver() && turnout_id)
104 layout.get_driver().add_turnout(turnout_id);
107 void Track::set_sensor_id(unsigned i)
109 if(type.is_turnout())
110 throw InvalidState("Can't set sensor on a turnout");
113 layout.create_blocks(*this);
114 if(layout.has_driver() && sensor_id)
115 layout.get_driver().add_sensor(sensor_id);
118 void Track::set_active_path(unsigned p)
121 throw InvalidState("Not a turnout");
122 if(!(type.get_paths()&(1<<p)))
123 throw InvalidParameterValue("Invalid path");
125 layout.get_driver().set_turnout(turnout_id, p&1);
126 if(type.get_n_paths()>2)
127 layout.get_driver().set_turnout(turnout_id+1, p&2);
130 int Track::get_endpoint_by_link(const Track &other) const
132 for(unsigned i=0; i<links.size(); ++i)
139 Point Track::get_endpoint_position(unsigned epi) const
141 const vector<Endpoint> &eps = type.get_endpoints();
143 throw InvalidParameterValue("Endpoint index out of range");
145 const Endpoint &ep = eps[epi];
150 Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
151 if(eps.size()==2 && epi==1)
156 float Track::get_endpoint_direction(unsigned epi) const
158 const vector<Endpoint> &eps = type.get_endpoints();
160 throw InvalidParameterValue("Endpoint index out of range");
162 const Endpoint &ep = eps[epi];
167 bool Track::snap_to(Track &other, bool link)
169 float limit = (link && !flex) ? 1e-6 : 1e-4;
170 const vector<Endpoint> &eps = type.get_endpoints();
171 const vector<Endpoint> &other_eps = other.get_type().get_endpoints();
173 for(unsigned i=0; i<eps.size(); ++i)
175 Point epp = get_endpoint_position(i);
177 for(unsigned j=0; j<other_eps.size(); ++j)
179 if(other.get_link(j))
182 Point epp2 = other.get_endpoint_position(j);
183 float dx = epp2.x-epp.x;
184 float dy = epp2.y-epp.y;
185 if(dx*dx+dy*dy<limit)
187 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
188 Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
189 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
191 if(eps.size()==2 && i==1)
198 break_link(*links[i]);
200 other.links[j] = this;
201 layout.create_blocks(*this);
212 bool Track::snap(Point &pt, float &d) const
214 const vector<Endpoint> &eps = type.get_endpoints();
216 for(unsigned i=0; i<eps.size(); ++i)
218 Point epp = get_endpoint_position(i);
219 float dx = pt.x-epp.x;
220 float dy = pt.y-epp.y;
232 void Track::break_link(Track &trk)
234 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
238 trk.break_link(*this);
239 // XXX Creates the blocks twice
240 layout.create_blocks(*this);
245 void Track::break_links()
247 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
251 trk->break_link(*this);
255 Track *Track::get_link(unsigned i) const
258 throw InvalidParameterValue("Link index out of range");
263 unsigned Track::traverse(unsigned i, unsigned path) const
265 const vector<Endpoint> &eps = type.get_endpoints();
267 throw InvalidParameterValue("Endpoint index out of range");
269 const Endpoint &ep = eps[i];
271 if(ep.paths&(1<<path))
273 // Find the other endpoint for this path
274 for(unsigned j=0; j<eps.size(); ++j)
275 if((eps[j].paths&(1<<path)) && j!=i)
280 // Find an endpoint that's connected to this one and has the requested path
281 for(unsigned j=0; j<eps.size(); ++j)
282 if((eps[j].paths&(1<<path)) && (eps[j].paths&ep.paths))
286 throw Exception("Track endpoint did not have a counterpart");
289 Point Track::get_point(unsigned epi, unsigned path, float d) const
291 const vector<Endpoint> &eps = type.get_endpoints();
293 throw InvalidParameterValue("Endpoint index out of range");
295 float x = eps[epi].pos.x;
296 float y = eps[epi].pos.y;
298 float slope_norm = 0;
301 slope_norm = slope/type.get_total_length();
305 slope_norm = -slope_norm;
309 const vector<TrackPart> &parts = type.get_parts();
310 const TrackPart *last_part = 0;
313 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
315 if((eps[epi].paths&(1<<path)) && i->path!=path)
320 vector<Endpoint> part_eps;
321 i->collect_endpoints(part_eps);
322 for(unsigned j=0; j<part_eps.size(); ++j)
324 float dx = part_eps[j].pos.x-x;
325 float dy = part_eps[j].pos.y-y;
328 float plen = i->length;
330 plen *= abs(i->radius);
336 Point p = i->get_point(d);
339 return Point(pos.x+c*p.x-s*p.y, pos.y+c*p.y+s*p.x, pos.z+z);
341 else if(part_eps.size()>1)
344 x = part_eps[1-j].pos.x;
345 y = part_eps[1-j].pos.y;
346 z += plen*slope_norm;
358 throw Exception("Internal error (Endpoint does not match any part)");
364 void Track::save(list<DataFile::Statement> &st) const
366 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
367 st.push_back((DataFile::Statement("rotation"), rot));
368 st.push_back((DataFile::Statement("slope"), slope));
370 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
372 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
374 st.push_back((DataFile::Statement("flex"), true));
377 void Track::turnout_event(unsigned addr, bool state)
383 active_path = (active_path&2) | (state ? 1 : 0);
384 else if(type.is_double_address() && addr==turnout_id+1)
385 active_path = (active_path&1) | (state ? 2 : 0);
389 Track::Loader::Loader(Track &t):
390 DataFile::BasicLoader<Track>(t)
392 add("position", &Loader::position);
393 add("rotation", &Track::rot);
394 add("slope", &Track::slope);
395 add("turnout_id", &Loader::turnout_id);
396 add("sensor_id", &Loader::sensor_id);
397 add("flex", &Track::flex);
400 void Track::Loader::position(float x, float y, float z)
402 obj.pos = Point(x, y, z);
405 void Track::Loader::sensor_id(unsigned id)
407 obj.set_sensor_id(id);
410 void Track::Loader::turnout_id(unsigned id)
412 obj.set_turnout_id(id);
415 } // namespace Marklin