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::check_slope()
63 if(links[0] && links[1])
65 Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
66 Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
75 Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
80 Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
86 void Track::set_turnout_id(unsigned i)
91 void Track::set_sensor_id(unsigned i)
96 int Track::get_endpoint_by_link(const Track &other) const
98 for(unsigned i=0; i<links.size(); ++i)
105 Point Track::get_endpoint_position(unsigned epi) const
107 const vector<Endpoint> &eps = type.get_endpoints();
109 throw InvalidParameterValue("Endpoint index out of range");
111 const Endpoint &ep = eps[epi];
116 Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
117 if(eps.size()==2 && epi==1)
122 float Track::get_endpoint_direction(unsigned epi) const
124 const vector<Endpoint> &eps = type.get_endpoints();
126 throw InvalidParameterValue("Endpoint index out of range");
128 const Endpoint &ep = eps[epi];
133 bool Track::snap_to(Track &other, bool link)
135 float limit = (link && !flex) ? 1e-6 : 1e-4;
136 const vector<Endpoint> &eps = type.get_endpoints();
137 const vector<Endpoint> &other_eps = other.get_type().get_endpoints();
139 for(unsigned i=0; i<eps.size(); ++i)
141 Point epp = get_endpoint_position(i);
143 for(unsigned j=0; j<other_eps.size(); ++j)
145 if(other.get_link(j))
148 Point epp2 = other.get_endpoint_position(j);
149 float dx = epp2.x-epp.x;
150 float dy = epp2.y-epp.y;
151 if(dx*dx+dy*dy<limit)
153 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
154 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));
159 break_link(*links[i]);
161 other.links[j] = this;
172 bool Track::snap(Point &pt, float &d) const
174 const vector<Endpoint> &eps = type.get_endpoints();
176 for(unsigned i=0; i<eps.size(); ++i)
178 Point epp = get_endpoint_position(i);
179 float dx = pt.x-epp.x;
180 float dy = pt.y-epp.y;
192 void Track::break_link(Track &trk)
194 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
198 trk.break_link(*this);
203 void Track::break_links()
205 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
209 trk->break_link(*this);
213 Track *Track::get_link(unsigned i) const
216 throw InvalidParameterValue("Link index out of range");
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