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 Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
155 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
157 if(eps.size()==2 && i==1)
164 break_link(*links[i]);
166 other.links[j] = this;
177 bool Track::snap(Point &pt, float &d) const
179 const vector<Endpoint> &eps = type.get_endpoints();
181 for(unsigned i=0; i<eps.size(); ++i)
183 Point epp = get_endpoint_position(i);
184 float dx = pt.x-epp.x;
185 float dy = pt.y-epp.y;
197 void Track::break_link(Track &trk)
199 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
203 trk.break_link(*this);
208 void Track::break_links()
210 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
214 trk->break_link(*this);
218 Track *Track::get_link(unsigned i) const
221 throw InvalidParameterValue("Link index out of range");
226 int Track::traverse(unsigned i, unsigned route) const
228 const vector<Endpoint> &eps = type.get_endpoints();
230 throw InvalidParameterValue("Endpoint index out of range");
232 const Endpoint &ep = eps[i];
234 if(ep.routes&(1<<route))
236 // Find the other endpoint for this route
237 for(unsigned j=0; j<eps.size(); ++j)
238 if((eps[j].routes&(1<<route)) && j!=i)
243 // Find an endpoint that's connected to this one and has the requested route
244 for(unsigned j=0; j<eps.size(); ++j)
245 if((eps[j].routes&(1<<route)) && (eps[j].routes&ep.routes))
252 Point Track::get_point(unsigned epi, unsigned route, float d) const
254 const vector<Endpoint> &eps = type.get_endpoints();
256 throw InvalidParameterValue("Endpoint index out of range");
258 float x = eps[epi].pos.x;
259 float y = eps[epi].pos.y;
261 const vector<TrackPart> &parts = type.get_parts();
262 const TrackPart *last_part = 0;
265 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
267 if((eps[epi].routes&(1<<route)) && i->route!=route)
272 vector<Endpoint> part_eps;
273 i->collect_endpoints(part_eps);
274 for(unsigned j=0; j<part_eps.size(); ++j)
276 float dx = part_eps[j].pos.x-x;
277 float dy = part_eps[j].pos.y-y;
280 float plen = i->length;
282 plen *= abs(i->radius);
287 Point p = i->get_point(d);
290 return Point(pos.x+c*p.x-s*p.y, pos.y+c*p.y+s*p.x);
292 else if(part_eps.size()>1)
295 x = part_eps[1-j].pos.x;
296 y = part_eps[1-j].pos.y;
308 throw Exception("Internal error (Endpoint does not match any part)");
314 Track *Track::copy() const
316 Track *trk = new Track(type);
317 trk->set_position(pos);
318 trk->set_rotation(rot);
319 trk->set_slope(slope);
325 void Track::save(list<DataFile::Statement> &st) const
327 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
328 st.push_back((DataFile::Statement("rotation"), rot));
329 st.push_back((DataFile::Statement("slope"), slope));
331 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
333 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
335 st.push_back((DataFile::Statement("flex"), true));
339 Track::Loader::Loader(Track &t):
340 DataFile::BasicLoader<Track>(t)
342 add("position", &Loader::position);
343 add("rotation", &Track::rot);
344 add("slope", &Track::slope);
345 add("turnout_id", &Track::turnout_id);
346 add("sensor_id", &Track::sensor_id);
347 add("flex", &Track::flex);
350 void Track::Loader::position(float x, float y, float z)
352 obj.pos = Point(x, y, z);
355 } // namespace Marklin