3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include "tracktype.h"
17 Track::Track(const TrackType &t):
24 links(t.get_endpoints().size())
32 void Track::set_position(const Point &p)
37 void Track::set_rotation(float r)
46 void Track::set_slope(float s)
48 if(links.size()!=2) return;
53 void Track::set_flex(bool f)
58 void Track::set_turnout_id(unsigned i)
63 void Track::set_sensor_id(unsigned i)
68 int Track::get_endpoint_by_link(const Track &other) const
70 for(unsigned i=0; i<links.size(); ++i)
77 Point Track::get_endpoint_position(unsigned epi) const
79 const vector<Endpoint> &eps=type.get_endpoints();
81 throw InvalidParameterValue("Endpoint index out of range");
83 const Endpoint &ep=eps[epi];
88 Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
89 if(eps.size()==2 && epi==1)
94 float Track::get_endpoint_direction(unsigned epi) const
96 const vector<Endpoint> &eps=type.get_endpoints();
98 throw InvalidParameterValue("Endpoint index out of range");
100 const Endpoint &ep=eps[epi];
105 bool Track::snap_to(Track &other, bool link)
107 float limit=(link && !flex) ? 1e-6 : 1e-4;
108 const vector<Endpoint> &eps=type.get_endpoints();
109 const vector<Endpoint> &other_eps=other.get_type().get_endpoints();
111 for(unsigned i=0; i<eps.size(); ++i)
113 Point epp=get_endpoint_position(i);
115 for(unsigned j=0; j<other_eps.size(); ++j)
117 if(other.get_link(j))
120 Point epp2=other.get_endpoint_position(j);
121 float dx=epp2.x-epp.x;
122 float dy=epp2.y-epp.y;
123 if(dx*dx+dy*dy<limit)
125 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
126 set_position(Point(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)), epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)), epp2.z));
131 break_link(*links[i]);
144 bool Track::snap(Point &pt, float &d) const
146 const vector<Endpoint> &eps=type.get_endpoints();
148 for(unsigned i=0; i<eps.size(); ++i)
150 Point epp=get_endpoint_position(i);
164 void Track::break_link(Track &trk)
166 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
170 trk.break_link(*this);
175 void Track::break_links()
177 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
181 trk->break_link(*this);
185 Track *Track::get_link(unsigned i) const
188 throw InvalidParameterValue("Link index out of range");
193 void Track::check_slope()
198 if(links[0] && links[1])
200 Point epp0=links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
201 Point epp1=links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
210 Point epp=links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
215 Point epp=links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
221 int Track::traverse(unsigned i, unsigned route) const
223 const vector<Endpoint> &eps=type.get_endpoints();
225 throw InvalidParameterValue("Endpoint index out of range");
227 const Endpoint &ep=eps[i];
229 if(ep.routes&(1<<route))
231 // Find the other endpoint for this route
232 for(unsigned j=0; j<eps.size(); ++j)
233 if((eps[j].routes&(1<<route)) && j!=i)
238 // Find an endpoint that's connected to this one and has the requested route
239 for(unsigned j=0; j<eps.size(); ++j)
240 if((eps[j].routes&(1<<route)) && (eps[j].routes&ep.routes))
247 Point Track::get_point(unsigned epi, unsigned route, float d) const
249 const vector<Endpoint> &eps=type.get_endpoints();
251 throw InvalidParameterValue("Endpoint index out of range");
253 float x=eps[epi].pos.x;
254 float y=eps[epi].pos.y;
256 const vector<TrackPart> &parts=type.get_parts();
257 const TrackPart *last_part=0;
260 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
262 if((eps[epi].routes&(1<<route)) && i->route!=route)
267 vector<Endpoint> part_eps;
268 i->collect_endpoints(part_eps);
269 for(unsigned j=0; j<part_eps.size(); ++j)
271 float dx=part_eps[j].pos.x-x;
272 float dy=part_eps[j].pos.y-y;
275 float plen=i->length;
277 plen*=abs(i->radius);
282 Point p=i->get_point(d);
285 return Point(pos.x+c*p.x-s*p.y, pos.y+c*p.y+s*p.x);
287 else if(part_eps.size()>1)
290 x=part_eps[1-j].pos.x;
291 y=part_eps[1-j].pos.y;
303 throw Exception("Internal error (Endpoint does not match any part)");
309 Track *Track::copy() const
311 Track *trk=new Track(type);
312 trk->set_position(pos);
313 trk->set_rotation(rot);
314 trk->set_slope(slope);
320 void Track::save(list<DataFile::Statement> &st) const
322 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
323 st.push_back((DataFile::Statement("rotation"), rot));
324 st.push_back((DataFile::Statement("slope"), slope));
326 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
328 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
330 st.push_back((DataFile::Statement("flex"), true));
334 Track::Loader::Loader(Track &t):
335 DataFile::BasicLoader<Track>(t)
337 add("position", &Loader::position);
338 add("rotation", &Track::rot);
339 add("slope", &Track::slope);
340 add("turnout_id", &Track::turnout_id);
341 add("sensor_id", &Track::sensor_id);
342 add("flex", &Track::flex);
345 void Track::Loader::position(float x, float y, float z)
347 obj.pos=Point(x, y, z);
350 } // namespace Marklin