14 Track::Track(Layout &l, const TrackType &t):
23 links(type.get_endpoints().size()),
28 turnout_id = layout.allocate_turnout_id();
30 layout.add_track(*this);
32 if(layout.has_driver())
33 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
35 for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
41 layout.remove_track(*this);
44 void Track::set_block(Block *b)
46 if(b && !b->has_track(*this))
47 throw logic_error("track not in block");
48 if(!b && block && block->has_track(*this))
49 throw logic_error("track still in block");
54 Block &Track::get_block() const
57 throw logic_error("!block");
62 void Track::set_position(const Vector &p)
67 void Track::set_rotation(float r)
76 void Track::set_slope(float s)
84 void Track::set_flex(bool f)
89 void Track::check_slope()
94 if(links[0] && links[1])
96 Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
97 Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
106 Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
111 Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
117 void Track::set_turnout_id(unsigned i)
119 if(!type.is_turnout())
120 throw logic_error("not a turnout");
123 layout.create_blocks(*this);
124 layout.update_routes();
125 if(layout.has_driver() && turnout_id)
126 layout.get_driver().add_turnout(turnout_id, type);
129 void Track::set_sensor_id(unsigned i)
131 if(type.is_turnout())
132 throw logic_error("is a turnout");
135 layout.create_blocks(*this);
136 if(layout.has_driver() && sensor_id)
137 layout.get_driver().add_sensor(sensor_id);
140 void Track::set_active_path(unsigned p)
143 throw logic_error("not a turnout");
144 if(!(type.get_paths()&(1<<p)))
145 throw invalid_argument("Track::set_active_path");
147 path_changing = true;
148 layout.get_driver().set_turnout(turnout_id, p);
151 int Track::get_endpoint_by_link(Track &other) const
153 for(unsigned i=0; i<links.size(); ++i)
160 Vector Track::get_endpoint_position(unsigned epi) const
162 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
164 throw out_of_range("Track::get_endpoint_position");
166 const TrackType::Endpoint &ep = eps[epi];
171 Vector p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
172 if(eps.size()==2 && epi==1)
177 float Track::get_endpoint_direction(unsigned epi) const
179 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
181 throw out_of_range("Track::get_endpoint_direction");
183 const TrackType::Endpoint &ep = eps[epi];
188 bool Track::snap_to(Track &other, bool link, float limit)
192 limit = layout.get_catalogue().get_gauge();
193 if(link && !flex && !other.get_flex())
198 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
199 const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
201 for(unsigned i=0; i<eps.size(); ++i)
203 Vector epp = get_endpoint_position(i);
205 for(unsigned j=0; j<other_eps.size(); ++j)
207 if(other.get_link(j))
210 Vector epp2 = other.get_endpoint_position(j);
211 float dx = epp2.x-epp.x;
212 float dy = epp2.y-epp.y;
213 float dz = epp2.z-epp.z;
214 if(dx*dx+dy*dy<limit && dz*dz<limit)
216 if(!link || (!flex && !other.get_flex()))
218 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
219 Vector p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
220 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
222 if(eps.size()==2 && i==1)
230 break_link(*links[i]);
232 other.links[j] = this;
233 layout.create_blocks(*this);
235 signal_link_changed.emit(i, &other);
236 other.signal_link_changed.emit(j, this);
247 bool Track::snap(Vector &pt, float &d) const
249 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
251 for(unsigned i=0; i<eps.size(); ++i)
253 Vector epp = get_endpoint_position(i);
254 float dx = pt.x-epp.x;
255 float dy = pt.y-epp.y;
267 void Track::break_link(Track &trk)
269 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
273 trk.break_link(*this);
274 // XXX Creates the blocks twice
275 layout.create_blocks(*this);
276 signal_link_changed.emit(i-links.begin(), 0);
281 void Track::break_links()
283 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
287 trk->break_link(*this);
291 Track *Track::get_link(unsigned i) const
294 throw out_of_range("Track::get_link");
299 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
301 TrackPoint p = type.get_point(epi, path, d);
305 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);
307 if(type.get_endpoints().size()==2)
309 float len = type.get_path_length(path);
310 float grade = slope/len;
318 p.pos.z += slope-grade*d;
326 TrackPoint Track::get_point(unsigned epi, float d) const
328 return get_point(epi, active_path, d);
331 TrackPoint Track::get_nearest_point(const Vector &p) const
333 Vector local(p.x-pos.x, p.y-pos.y, p.z-pos.z);
336 local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z);
338 TrackPoint tp = type.get_nearest_point(local);
339 tp.pos = Vector(pos.x+tp.pos.x*c-tp.pos.y*s, pos.y+tp.pos.y*c+tp.pos.x*s, pos.z+tp.pos.z);
344 bool Track::collide_ray(const Vector &start, const Vector &ray)
346 Vector local_start(start.x-pos.x, start.y-pos.y, start.z-pos.z);
349 local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
350 Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
352 float width = layout.get_catalogue().get_ballast_profile().get_width();
354 return type.collide_ray(local_start, local_ray, width);
357 void Track::save(list<DataFile::Statement> &st) const
359 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
360 st.push_back((DataFile::Statement("rotation"), rot));
361 st.push_back((DataFile::Statement("slope"), slope));
363 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
365 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
367 st.push_back((DataFile::Statement("flex"), true));
370 void Track::turnout_event(unsigned addr, unsigned state)
378 path_changing = false;
379 signal_path_changed.emit(active_path);
384 Track::Loader::Loader(Track &t):
385 DataFile::ObjectLoader<Track>(t)
387 add("position", &Loader::position);
388 add("rotation", &Track::rot);
389 add("slope", &Track::slope);
390 add("turnout_id", &Loader::turnout_id);
391 add("sensor_id", &Loader::sensor_id);
392 add("flex", &Track::flex);
395 void Track::Loader::position(float x, float y, float z)
397 obj.pos = Vector(x, y, z);
400 void Track::Loader::sensor_id(unsigned id)
402 obj.set_sensor_id(id);
405 void Track::Loader::turnout_id(unsigned id)
407 obj.set_turnout_id(id);