3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include "catalogue.h"
14 #include "tracktype.h"
21 Track::Track(Layout &l, const TrackType &t):
30 links(type.get_endpoints().size()),
34 turnout_id = layout.allocate_turnout_id();
36 layout.add_track(*this);
38 if(layout.has_driver())
39 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
41 for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
47 layout.remove_track(*this);
50 void Track::set_block(Block *b)
52 if(b && !b->has_track(*this))
53 throw InvalidParameterValue("Track is not in the Block");
54 if(!b && block && block->has_track(*this))
55 throw InvalidState("Track is still in a Block");
60 Block &Track::get_block() const
63 throw InvalidState("No Block");
68 void Track::set_position(const Point &p)
73 void Track::set_rotation(float r)
82 void Track::set_slope(float s)
90 void Track::set_flex(bool f)
95 void Track::check_slope()
100 if(links[0] && links[1])
102 Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
103 Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
105 slope = epp1.z-pos.z;
112 Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
117 Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
123 void Track::set_turnout_id(unsigned i)
125 if(!type.is_turnout())
126 throw InvalidState("Not a turnout");
129 layout.create_blocks(*this);
130 layout.update_routes();
131 if(layout.has_driver() && turnout_id)
132 layout.get_driver().add_turnout(turnout_id, type);
135 void Track::set_sensor_id(unsigned i)
137 if(type.is_turnout())
138 throw InvalidState("Can't set sensor on a turnout");
141 layout.create_blocks(*this);
142 if(layout.has_driver() && sensor_id)
143 layout.get_driver().add_sensor(sensor_id);
146 void Track::set_active_path(unsigned p)
149 throw InvalidState("Not a turnout");
150 if(!(type.get_paths()&(1<<p)))
151 throw InvalidParameterValue("Invalid path");
153 layout.get_driver().set_turnout(turnout_id, p);
156 int Track::get_endpoint_by_link(Track &other) const
158 for(unsigned i=0; i<links.size(); ++i)
165 Point Track::get_endpoint_position(unsigned epi) const
167 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
169 throw InvalidParameterValue("TrackType::Endpoint index out of range");
171 const TrackType::Endpoint &ep = eps[epi];
176 Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
177 if(eps.size()==2 && epi==1)
182 float Track::get_endpoint_direction(unsigned epi) const
184 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
186 throw InvalidParameterValue("TrackType::Endpoint index out of range");
188 const TrackType::Endpoint &ep = eps[epi];
193 bool Track::snap_to(Track &other, bool link, float limit)
197 limit = layout.get_catalogue().get_gauge();
198 if(link && !flex && !other.get_flex())
203 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
204 const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
206 for(unsigned i=0; i<eps.size(); ++i)
208 Point epp = get_endpoint_position(i);
210 for(unsigned j=0; j<other_eps.size(); ++j)
212 if(other.get_link(j))
215 Point epp2 = other.get_endpoint_position(j);
216 float dx = epp2.x-epp.x;
217 float dy = epp2.y-epp.y;
218 float dz = epp2.z-epp.z;
219 if(dx*dx+dy*dy<limit && dz*dz<limit)
221 if(!link || (!flex && !other.get_flex()))
223 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
224 Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
225 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
227 if(eps.size()==2 && i==1)
235 break_link(*links[i]);
237 other.links[j] = this;
238 layout.create_blocks(*this);
249 bool Track::snap(Point &pt, float &d) const
251 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
253 for(unsigned i=0; i<eps.size(); ++i)
255 Point epp = get_endpoint_position(i);
256 float dx = pt.x-epp.x;
257 float dy = pt.y-epp.y;
269 void Track::break_link(Track &trk)
271 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
275 trk.break_link(*this);
276 // XXX Creates the blocks twice
277 layout.create_blocks(*this);
282 void Track::break_links()
284 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
288 trk->break_link(*this);
292 Track *Track::get_link(unsigned i) const
295 throw InvalidParameterValue("Link index out of range");
300 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
302 TrackPoint p = type.get_point(epi, path, d);
306 p.pos = Point(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, pos.z);
308 if(type.get_endpoints().size()==2)
310 float len = type.get_path_length(path);
311 float grade = slope/len;
319 p.pos.z += slope-grade*d;
327 TrackPoint Track::get_point(unsigned epi, float d) const
329 return get_point(epi, active_path, d);
332 void Track::save(list<DataFile::Statement> &st) const
334 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
335 st.push_back((DataFile::Statement("rotation"), rot));
336 st.push_back((DataFile::Statement("slope"), slope));
338 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
340 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
342 st.push_back((DataFile::Statement("flex"), true));
345 void Track::turnout_event(unsigned addr, unsigned state)
353 signal_path_changed.emit(active_path);
358 Track::Loader::Loader(Track &t):
359 DataFile::BasicLoader<Track>(t)
361 add("position", &Loader::position);
362 add("rotation", &Track::rot);
363 add("slope", &Track::slope);
364 add("turnout_id", &Loader::turnout_id);
365 add("sensor_id", &Loader::sensor_id);
366 add("flex", &Track::flex);
369 void Track::Loader::position(float x, float y, float z)
371 obj.pos = Point(x, y, z);
374 void Track::Loader::sensor_id(unsigned id)
376 obj.set_sensor_id(id);
379 void Track::Loader::turnout_id(unsigned id)
381 obj.set_turnout_id(id);