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 path) 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.paths&(1<<path))
236 // Find the other endpoint for this path
237 for(unsigned j=0; j<eps.size(); ++j)
238 if((eps[j].paths&(1<<path)) && j!=i)
243 // Find an endpoint that's connected to this one and has the requested path
244 for(unsigned j=0; j<eps.size(); ++j)
245 if((eps[j].paths&(1<<path)) && (eps[j].paths&ep.paths))
252 Point Track::get_point(unsigned epi, unsigned path, 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 float slope_norm = 0;
264 slope_norm = slope/type.get_total_length();
268 slope_norm = -slope_norm;
272 const vector<TrackPart> &parts = type.get_parts();
273 const TrackPart *last_part = 0;
276 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
278 if((eps[epi].paths&(1<<path)) && i->path!=path)
283 vector<Endpoint> part_eps;
284 i->collect_endpoints(part_eps);
285 for(unsigned j=0; j<part_eps.size(); ++j)
287 float dx = part_eps[j].pos.x-x;
288 float dy = part_eps[j].pos.y-y;
291 float plen = i->length;
293 plen *= abs(i->radius);
299 Point p = i->get_point(d);
302 return Point(pos.x+c*p.x-s*p.y, pos.y+c*p.y+s*p.x, pos.z+z);
304 else if(part_eps.size()>1)
307 x = part_eps[1-j].pos.x;
308 y = part_eps[1-j].pos.y;
309 z += plen*slope_norm;
321 throw Exception("Internal error (Endpoint does not match any part)");
327 Track *Track::copy() const
329 Track *trk = new Track(type);
330 trk->set_position(pos);
331 trk->set_rotation(rot);
332 trk->set_slope(slope);
338 void Track::save(list<DataFile::Statement> &st) const
340 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
341 st.push_back((DataFile::Statement("rotation"), rot));
342 st.push_back((DataFile::Statement("slope"), slope));
344 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
346 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
348 st.push_back((DataFile::Statement("flex"), true));
352 Track::Loader::Loader(Track &t):
353 DataFile::BasicLoader<Track>(t)
355 add("position", &Loader::position);
356 add("rotation", &Track::rot);
357 add("slope", &Track::slope);
358 add("turnout_id", &Track::turnout_id);
359 add("sensor_id", &Track::sensor_id);
360 add("flex", &Track::flex);
363 void Track::Loader::position(float x, float y, float z)
365 obj.pos = Point(x, y, z);
368 } // namespace Marklin