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 Vector &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 Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
103 Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
105 slope = epp1.z-pos.z;
112 Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
117 Vector 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 Vector 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 Vector 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 Vector epp = get_endpoint_position(i);
210 for(unsigned j=0; j<other_eps.size(); ++j)
212 if(other.get_link(j))
215 Vector 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 Vector 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);
240 signal_link_changed.emit(i, &other);
241 other.signal_link_changed.emit(j, this);
252 bool Track::snap(Vector &pt, float &d) const
254 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
256 for(unsigned i=0; i<eps.size(); ++i)
258 Vector epp = get_endpoint_position(i);
259 float dx = pt.x-epp.x;
260 float dy = pt.y-epp.y;
272 void Track::break_link(Track &trk)
274 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
278 trk.break_link(*this);
279 // XXX Creates the blocks twice
280 layout.create_blocks(*this);
281 signal_link_changed.emit(i-links.begin(), 0);
286 void Track::break_links()
288 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
292 trk->break_link(*this);
296 Track *Track::get_link(unsigned i) const
299 throw InvalidParameterValue("Link index out of range");
304 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
306 TrackPoint p = type.get_point(epi, path, d);
310 p.pos = Vector(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, pos.z);
312 if(type.get_endpoints().size()==2)
314 float len = type.get_path_length(path);
315 float grade = slope/len;
323 p.pos.z += slope-grade*d;
331 TrackPoint Track::get_point(unsigned epi, float d) const
333 return get_point(epi, active_path, d);
336 void Track::save(list<DataFile::Statement> &st) const
338 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
339 st.push_back((DataFile::Statement("rotation"), rot));
340 st.push_back((DataFile::Statement("slope"), slope));
342 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
344 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
346 st.push_back((DataFile::Statement("flex"), true));
349 void Track::turnout_event(unsigned addr, unsigned state)
357 signal_path_changed.emit(active_path);
362 Track::Loader::Loader(Track &t):
363 DataFile::BasicLoader<Track>(t)
365 add("position", &Loader::position);
366 add("rotation", &Track::rot);
367 add("slope", &Track::slope);
368 add("turnout_id", &Loader::turnout_id);
369 add("sensor_id", &Loader::sensor_id);
370 add("flex", &Track::flex);
373 void Track::Loader::position(float x, float y, float z)
375 obj.pos = Vector(x, y, z);
378 void Track::Loader::sensor_id(unsigned id)
380 obj.set_sensor_id(id);
383 void Track::Loader::turnout_id(unsigned id)
385 obj.set_turnout_id(id);