14 Track::Track(Layout &l, const TrackType &t):
23 links(type.get_endpoints().size()),
27 turnout_id = layout.allocate_turnout_id();
29 layout.add_track(*this);
31 if(layout.has_driver())
32 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
34 for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
40 layout.remove_track(*this);
43 void Track::set_block(Block *b)
45 if(b && !b->has_track(*this))
46 throw logic_error("track not in block");
47 if(!b && block && block->has_track(*this))
48 throw logic_error("track still in block");
53 Block &Track::get_block() const
56 throw logic_error("!block");
61 void Track::set_position(const Vector &p)
66 void Track::set_rotation(float r)
75 void Track::set_slope(float s)
83 void Track::set_flex(bool f)
88 void Track::check_slope()
93 if(links[0] && links[1])
95 Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
96 Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
105 Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
110 Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
116 void Track::set_turnout_id(unsigned i)
118 if(!type.is_turnout())
119 throw logic_error("not a turnout");
122 layout.create_blocks(*this);
123 layout.update_routes();
124 if(layout.has_driver() && turnout_id)
125 layout.get_driver().add_turnout(turnout_id, type);
128 void Track::set_sensor_id(unsigned i)
130 if(type.is_turnout())
131 throw logic_error("is a turnout");
134 layout.create_blocks(*this);
135 if(layout.has_driver() && sensor_id)
136 layout.get_driver().add_sensor(sensor_id);
139 void Track::set_active_path(unsigned p)
142 throw logic_error("not a turnout");
143 if(!(type.get_paths()&(1<<p)))
144 throw invalid_argument("Track::set_active_path");
146 layout.get_driver().set_turnout(turnout_id, p);
149 int Track::get_endpoint_by_link(Track &other) const
151 for(unsigned i=0; i<links.size(); ++i)
158 Vector Track::get_endpoint_position(unsigned epi) const
160 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
162 throw out_of_range("Track::get_endpoint_position");
164 const TrackType::Endpoint &ep = eps[epi];
169 Vector p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
170 if(eps.size()==2 && epi==1)
175 float Track::get_endpoint_direction(unsigned epi) const
177 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
179 throw out_of_range("Track::get_endpoint_direction");
181 const TrackType::Endpoint &ep = eps[epi];
186 bool Track::snap_to(Track &other, bool link, float limit)
190 limit = layout.get_catalogue().get_gauge();
191 if(link && !flex && !other.get_flex())
196 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
197 const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
199 for(unsigned i=0; i<eps.size(); ++i)
201 Vector epp = get_endpoint_position(i);
203 for(unsigned j=0; j<other_eps.size(); ++j)
205 if(other.get_link(j))
208 Vector epp2 = other.get_endpoint_position(j);
209 float dx = epp2.x-epp.x;
210 float dy = epp2.y-epp.y;
211 float dz = epp2.z-epp.z;
212 if(dx*dx+dy*dy<limit && dz*dz<limit)
214 if(!link || (!flex && !other.get_flex()))
216 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
217 Vector p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
218 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
220 if(eps.size()==2 && i==1)
228 break_link(*links[i]);
230 other.links[j] = this;
231 layout.create_blocks(*this);
233 signal_link_changed.emit(i, &other);
234 other.signal_link_changed.emit(j, this);
245 bool Track::snap(Vector &pt, float &d) const
247 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
249 for(unsigned i=0; i<eps.size(); ++i)
251 Vector epp = get_endpoint_position(i);
252 float dx = pt.x-epp.x;
253 float dy = pt.y-epp.y;
265 void Track::break_link(Track &trk)
267 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
271 trk.break_link(*this);
272 // XXX Creates the blocks twice
273 layout.create_blocks(*this);
274 signal_link_changed.emit(i-links.begin(), 0);
279 void Track::break_links()
281 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
285 trk->break_link(*this);
289 Track *Track::get_link(unsigned i) const
292 throw out_of_range("Track::get_link");
297 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
299 TrackPoint p = type.get_point(epi, path, d);
303 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);
305 if(type.get_endpoints().size()==2)
307 float len = type.get_path_length(path);
308 float grade = slope/len;
316 p.pos.z += slope-grade*d;
324 TrackPoint Track::get_point(unsigned epi, float d) const
326 return get_point(epi, active_path, d);
329 bool Track::collide_ray(const Vector &start, const Vector &ray)
331 Vector local_start(start.x-pos.x, start.y-pos.y, start.z-pos.z);
334 local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
335 Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
337 float width = layout.get_catalogue().get_ballast_profile().get_width();
339 return type.collide_ray(local_start, local_ray, width);
342 void Track::save(list<DataFile::Statement> &st) const
344 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
345 st.push_back((DataFile::Statement("rotation"), rot));
346 st.push_back((DataFile::Statement("slope"), slope));
348 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
350 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
352 st.push_back((DataFile::Statement("flex"), true));
355 void Track::turnout_event(unsigned addr, unsigned state)
363 signal_path_changed.emit(active_path);
368 Track::Loader::Loader(Track &t):
369 DataFile::ObjectLoader<Track>(t)
371 add("position", &Loader::position);
372 add("rotation", &Track::rot);
373 add("slope", &Track::slope);
374 add("turnout_id", &Loader::turnout_id);
375 add("sensor_id", &Loader::sensor_id);
376 add("flex", &Track::flex);
379 void Track::Loader::position(float x, float y, float z)
381 obj.pos = Vector(x, y, z);
384 void Track::Loader::sensor_id(unsigned id)
386 obj.set_sensor_id(id);
389 void Track::Loader::turnout_id(unsigned id)
391 obj.set_turnout_id(id);