3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2008 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.x-s*ep.y, pos.y+s*ep.x+c*ep.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].x*cos(rot)-eps[i].y*sin(rot)), epp2.y-(eps[i].y*cos(rot)+eps[i].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 Track *Track::copy() const
251 Track *trk=new Track(type);
252 trk->set_position(pos);
253 trk->set_rotation(rot);
254 trk->set_slope(slope);
264 Track::Loader::Loader(Track &t):
267 add("position", &Loader::position);
268 add("rotation", &Track::rot);
269 add("slope", &Track::slope);
270 add("turnout_id", &Track::turnout_id);
271 add("sensor_id", &Track::sensor_id);
272 add("flex", &Track::flex);
275 void Track::Loader::position(float x, float y, float z)
277 track.pos=Point(x, y, z);
280 } // namespace Marklin