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"
19 Track::Track(const TrackType &t):
26 links(t.get_endpoints().size())
34 void Track::set_position(const Point &p)
39 void Track::set_rotation(float r)
48 void Track::set_slope(float s)
50 if(links.size()!=2) return;
55 void Track::set_flex(bool f)
60 void Track::set_turnout_id(unsigned i)
65 void Track::set_sensor_id(unsigned i)
70 int Track::get_endpoint_by_link(const Track &other) const
72 for(unsigned i=0; i<links.size(); ++i)
79 Point Track::get_endpoint_position(unsigned epi) const
81 const vector<Endpoint> &eps=type.get_endpoints();
83 throw InvalidParameterValue("Endpoint index out of range");
85 const Endpoint &ep=eps[epi];
90 Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
91 if(eps.size()==2 && epi==1)
96 float Track::get_endpoint_direction(unsigned epi) const
98 const vector<Endpoint> &eps=type.get_endpoints();
100 throw InvalidParameterValue("Endpoint index out of range");
102 const Endpoint &ep=eps[epi];
107 bool Track::snap_to(Track &other, bool link)
109 float limit=(link && !flex) ? 1e-6 : 1e-4;
110 const vector<Endpoint> &eps=type.get_endpoints();
111 const vector<Endpoint> &other_eps=other.get_type().get_endpoints();
113 for(unsigned i=0; i<eps.size(); ++i)
115 Point epp=get_endpoint_position(i);
117 for(unsigned j=0; j<other_eps.size(); ++j)
119 if(other.get_link(j))
122 Point epp2=other.get_endpoint_position(j);
123 float dx=epp2.x-epp.x;
124 float dy=epp2.y-epp.y;
125 if(dx*dx+dy*dy<limit)
127 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
128 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));
133 break_link(*links[i]);
146 bool Track::snap(Point &pt, float &d) const
148 const vector<Endpoint> &eps=type.get_endpoints();
150 for(unsigned i=0; i<eps.size(); ++i)
152 Point epp=get_endpoint_position(i);
166 void Track::break_link(Track &trk)
168 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
172 trk.break_link(*this);
177 void Track::break_links()
179 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
183 trk->break_link(*this);
187 Track *Track::get_link(unsigned i) const
190 throw InvalidParameterValue("Link index out of range");
195 void Track::check_slope()
200 if(links[0] && links[1])
202 Point epp0=links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
203 Point epp1=links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
212 Point epp=links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
217 Point epp=links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
223 int Track::traverse(unsigned i, unsigned route) const
225 const vector<Endpoint> &eps=type.get_endpoints();
227 throw InvalidParameterValue("Endpoint index out of range");
229 const Endpoint &ep=eps[i];
231 if(ep.routes&(1<<route))
233 // Find the other endpoint for this route
234 for(unsigned j=0; j<eps.size(); ++j)
235 if((eps[j].routes&(1<<route)) && j!=i)
240 // Find an endpoint that's connected to this one and has the requested route
241 for(unsigned j=0; j<eps.size(); ++j)
242 if((eps[j].routes&(1<<route)) && (eps[j].routes&ep.routes))
249 Point Track::get_point(unsigned epi, unsigned route, float d) const
251 const vector<Endpoint> &eps=type.get_endpoints();
253 throw InvalidParameterValue("Endpoint index out of range");
255 float x=eps[epi].pos.x;
256 float y=eps[epi].pos.y;
258 const vector<TrackPart> &parts=type.get_parts();
259 const TrackPart *last_part=0;
262 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
264 if((eps[epi].routes&(1<<route)) && i->route!=route)
269 vector<Endpoint> part_eps;
270 i->collect_endpoints(part_eps);
271 for(unsigned j=0; j<part_eps.size(); ++j)
273 float dx=part_eps[j].pos.x-x;
274 float dy=part_eps[j].pos.y-y;
277 float plen=i->length;
279 plen*=abs(i->radius);
284 Point p=i->get_point(d);
287 return Point(pos.x+c*p.x-s*p.y, pos.y+c*p.y+s*p.x);
289 else if(part_eps.size()>1)
292 x=part_eps[1-j].pos.x;
293 y=part_eps[1-j].pos.y;
305 throw Exception("Internal error (Endpoint does not match any part)");
311 Track *Track::copy() const
313 Track *trk=new Track(type);
314 trk->set_position(pos);
315 trk->set_rotation(rot);
316 trk->set_slope(slope);
326 Track::Loader::Loader(Track &t):
329 add("position", &Loader::position);
330 add("rotation", &Track::rot);
331 add("slope", &Track::slope);
332 add("turnout_id", &Track::turnout_id);
333 add("sensor_id", &Track::sensor_id);
334 add("flex", &Track::flex);
337 void Track::Loader::position(float x, float y, float z)
339 track.pos=Point(x, y, z);
342 } // namespace Marklin