14 Track::Track(Layout &l, const TrackType &t):
22 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 Track *Track::clone(Layout *to_layout) const
45 Track *track = new Track((to_layout ? *to_layout : layout), type);
46 track->set_position(position);
47 track->set_rotation(rotation);
51 void Track::set_block(Block *b)
53 if(b && !b->has_track(*this))
54 throw logic_error("track not in block");
55 if(!b && block && block->has_track(*this))
56 throw logic_error("track still in block");
61 Block &Track::get_block() const
64 throw logic_error("!block");
69 void Track::set_position(const Vector &p)
72 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
77 void Track::set_rotation(const Angle &r)
79 rotation = wrap_positive(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_snap_node(links[0]->get_link_slot(*this)).position;
103 Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
105 slope = epp1.z-position.z;
111 Vector epp = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
116 Vector epp = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
117 position.z = epp.z-slope;
122 void Track::set_turnout_id(unsigned i)
124 if(!type.is_turnout())
125 throw logic_error("not a turnout");
128 layout.create_blocks(*this);
129 layout.update_routes();
130 if(layout.has_driver() && turnout_id)
131 layout.get_driver().add_turnout(turnout_id, type);
134 void Track::set_sensor_id(unsigned i)
136 if(type.is_turnout())
137 throw logic_error("is a turnout");
140 layout.create_blocks(*this);
141 if(layout.has_driver() && sensor_id)
142 layout.get_driver().add_sensor(sensor_id);
145 void Track::set_active_path(unsigned p)
148 throw logic_error("not a turnout");
149 if(!(type.get_paths()&(1<<p)))
150 throw invalid_argument("Track::set_active_path");
152 path_changing = true;
153 layout.get_driver().set_turnout(turnout_id, p);
156 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
158 TrackPoint p = type.get_point(epi, path, d);
160 p.pos = position+rotated_vector(p.pos, rotation);
162 if(type.get_endpoints().size()==2)
164 float len = type.get_path_length(path);
165 float grade = slope/len;
173 p.pos.z += slope-grade*d;
181 TrackPoint Track::get_point(unsigned epi, float d) const
183 return get_point(epi, active_path, d);
186 unsigned Track::get_n_snap_nodes() const
188 return type.get_endpoints().size();
191 Snap Track::get_snap_node(unsigned i) const
193 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
195 throw out_of_range("Track::get_snap_node");
198 const TrackType::Endpoint &ep = eps[i];
200 result.position = position+rotated_vector(ep.pos, rotation);
201 if(eps.size()==2 && i==1)
202 result.position.z += slope;
204 result.rotation = rotation+ep.dir;
209 bool Track::snap(Snap &sn, float limit, SnapType what) const
211 if(Object::snap(sn, limit, what))
214 if(what&SNAP_SEGMENT)
216 Vector local = rotated_vector(sn.position-position, -rotation);
218 TrackPoint tp = type.get_nearest_point(local);
219 Vector span = local-tp.pos;
220 if(dot(span, span)<=limit*limit)
222 sn.position = position+rotated_vector(tp.pos, rotation);
223 sn.rotation = tp.dir+rotation;
231 SnapType Track::get_default_snap_type_to(const Object &other) const
233 if(dynamic_cast<const Track *>(&other))
239 unsigned Track::get_n_link_slots() const
244 Track *Track::get_link(unsigned i) const
247 throw out_of_range("Track::get_link");
252 int Track::get_link_slot(const Object &other) const
254 for(unsigned i=0; i<links.size(); ++i)
261 bool Track::link_to(Object &other)
263 Track *otrack = dynamic_cast<Track *>(&other);
267 float limit = layout.get_catalogue().get_gauge();
268 if(!flex && !otrack->get_flex())
272 unsigned nsn = get_n_snap_nodes();
273 unsigned other_nsn = other.get_n_snap_nodes();
274 for(unsigned i=0; i<nsn; ++i)
276 Snap sn = get_snap_node(i);
277 for(unsigned j=0; j<other_nsn; ++j)
279 Snap osn = other.get_snap_node(j);
280 Vector span = osn.position-sn.position;
281 Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
283 if(dot(span, span)<limit && abs(da).radians()<0.01)
287 otrack->links[j] = this;
289 layout.create_blocks(*this);
291 signal_link_changed.emit(i, otrack);
292 otrack->signal_link_changed.emit(j, this);
301 bool Track::break_link(unsigned i)
304 throw out_of_range("Track::break_link");
306 Track *other = links[i];
311 other->break_link(*this);
312 // XXX Creates the blocks twice, because the other track calls this too
313 layout.create_blocks(*this);
314 signal_link_changed.emit(i, 0);
319 bool Track::collide_ray(const Vector &start, const Vector &ray) const
321 Transform trans = Transform::rotation(-rotation, Vector(0, 0, 1));
322 Vector local_start = trans.transform(start-position);
323 Vector local_ray = trans.transform(ray);
325 float width = layout.get_catalogue().get_ballast_profile().get_width();
327 return type.collide_ray(local_start, local_ray, width);
330 void Track::save(list<DataFile::Statement> &st) const
332 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
333 st.push_back((DataFile::Statement("rotation"), rotation.radians()));
334 st.push_back((DataFile::Statement("slope"), slope));
336 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
338 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
340 st.push_back((DataFile::Statement("flex"), true));
343 void Track::turnout_event(unsigned addr, unsigned state)
351 path_changing = false;
352 signal_path_changed.emit(active_path);
357 Track::Loader::Loader(Track &t):
358 DataFile::ObjectLoader<Track>(t)
360 add("position", &Loader::position);
361 add("rotation", &Loader::rotation);
362 add("slope", &Track::slope);
363 add("turnout_id", &Loader::turnout_id);
364 add("sensor_id", &Loader::sensor_id);
365 add("flex", &Track::flex);
368 void Track::Loader::position(float x, float y, float z)
370 obj.position = Vector(x, y, z);
373 void Track::Loader::rotation(float r)
375 obj.rotation = Angle::from_radians(r);
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);