2 #include <msp/core/maputils.h>
15 Track::Track(Layout &l, const TrackType &t):
23 links(type.get_endpoints().size()),
28 turnout_id = layout.allocate_turnout_id();
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) ;
44 Track *Track::clone(Layout *to_layout) const
46 Track *track = new Track((to_layout ? *to_layout : layout), type);
47 track->set_position(position);
48 track->set_rotation(rotation);
52 void Track::set_block(Block *b)
54 if(b && !b->has_track(*this))
55 throw logic_error("track not in block");
56 if(!b && block && block->has_track(*this))
57 throw logic_error("track still in block");
62 Block &Track::get_block() const
65 throw logic_error("!block");
70 void Track::set_position(const Vector &p)
77 void Track::set_rotation(const Angle &r)
79 rotation = wrap_positive(r);
83 void Track::set_tilt(const Angle &t)
89 slope = tan(tilt)*type.get_path_length(0);
94 void Track::set_flex(bool f)
99 void Track::propagate_slope()
101 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
106 void Track::check_slope()
111 if(links[0] && links[1])
113 Vector epp0 = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
114 Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
116 slope = epp1.z-position.z;
117 tilt = Geometry::atan(slope/type.get_path_length(0));
123 Vector epp = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
128 Vector epp = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
129 position.z = epp.z-slope;
136 void Track::set_turnout_id(unsigned i)
138 if(!type.is_turnout())
139 throw logic_error("not a turnout");
141 throw invalid_argument("Track::set_turnout_id");
144 layout.create_blocks(*this);
145 layout.update_routes();
146 if(layout.has_driver() && turnout_id)
147 layout.get_driver().add_turnout(turnout_id, type);
150 void Track::set_sensor_id(unsigned i)
152 if(type.is_turnout())
153 throw logic_error("is a turnout");
156 layout.create_blocks(*this);
157 if(layout.has_driver() && sensor_id)
158 layout.get_driver().add_sensor(sensor_id);
161 void Track::set_active_path(unsigned p)
164 throw logic_error("not a turnout");
165 if(!(type.get_paths()&(1<<p)))
166 throw invalid_argument("Track::set_active_path");
168 signal_path_changing(p);
169 path_changing = true;
170 layout.get_driver().set_turnout(turnout_id, p);
173 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
175 TrackPoint p = type.get_point(epi, path, d);
177 p.pos = position+rotated_vector(p.pos, rotation);
179 if(type.get_endpoints().size()==2)
181 float grade = tan(tilt);
189 p.pos.z += slope-grade*d;
197 TrackPoint Track::get_point(unsigned epi, float d) const
199 return get_point(epi, active_path, d);
202 unsigned Track::get_n_snap_nodes() const
204 return type.get_endpoints().size();
207 Snap Track::get_snap_node(unsigned i) const
209 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
211 throw out_of_range("Track::get_snap_node");
214 const TrackType::Endpoint &ep = eps[i];
216 result.position = position+rotated_vector(ep.pos, rotation);
217 if(eps.size()==2 && i==1)
218 result.position.z += slope;
220 result.rotation = rotation+ep.dir;
225 bool Track::snap(Snap &sn, float limit, SnapType what) const
227 if(Object::snap(sn, limit, what))
230 if(what&SNAP_SEGMENT)
232 Vector local = rotated_vector(sn.position-position, -rotation);
234 TrackPoint tp = type.get_nearest_point(local);
235 Vector span = local-tp.pos;
236 if(dot(span, span)<=limit*limit)
238 sn.position = position+rotated_vector(tp.pos, rotation);
239 sn.rotation = tp.dir+rotation;
247 SnapType Track::get_default_snap_type_to(const Object &other) const
249 if(dynamic_cast<const Track *>(&other))
255 unsigned Track::get_n_link_slots() const
260 Track *Track::get_link(unsigned i) const
263 throw out_of_range("Track::get_link");
268 int Track::get_link_slot(const Object &other) const
270 for(unsigned i=0; i<links.size(); ++i)
277 bool Track::link_to(Object &other)
279 Track *otrack = dynamic_cast<Track *>(&other);
283 float limit = layout.get_catalogue().get_gauge();
284 if(!flex && !otrack->get_flex())
288 unsigned nsn = get_n_snap_nodes();
289 unsigned other_nsn = other.get_n_snap_nodes();
290 for(unsigned i=0; i<nsn; ++i)
292 Snap sn = get_snap_node(i);
293 for(unsigned j=0; j<other_nsn; ++j)
295 Snap osn = other.get_snap_node(j);
296 Vector span = osn.position-sn.position;
297 Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
299 if(dot(span, span)<limit && abs(da).radians()<0.01)
303 otrack->links[j] = this;
305 layout.create_blocks(*this);
307 signal_link_changed.emit(i, otrack);
308 otrack->signal_link_changed.emit(j, this);
317 bool Track::break_link(unsigned i)
320 throw out_of_range("Track::break_link");
322 Track *other = links[i];
327 other->break_link(*this);
328 // XXX Creates the blocks twice, because the other track calls this too
329 layout.create_blocks(*this);
330 signal_link_changed.emit(i, 0);
335 void Track::add_attachment(TrackAttachment &a)
337 if(find(attachments.begin(), attachments.end(), &a)!=attachments.end())
339 attachments.push_back(&a);
342 void Track::remove_attachment(TrackAttachment &a)
344 AttachmentList::iterator i = find(attachments.begin(), attachments.end(), &a);
345 if(i==attachments.end())
347 attachments.erase(i);
350 void Track::save(list<DataFile::Statement> &st) const
352 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
353 st.push_back((DataFile::Statement("rotation"), rotation.radians()));
354 st.push_back((DataFile::Statement("tilt"), tilt.radians()));
356 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
358 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
360 st.push_back((DataFile::Statement("flex"), true));
363 void Track::turnout_event(unsigned addr, unsigned state)
371 path_changing = false;
372 signal_path_changed.emit(active_path);
377 Track::Loader::Loader(Track &t):
378 DataFile::ObjectLoader<Track>(t)
380 add("position", &Loader::position);
381 add("rotation", &Loader::rotation);
382 add("tilt", &Loader::tilt);
383 add("turnout_id", &Loader::turnout_id);
384 add("sensor_id", &Loader::sensor_id);
385 add("flex", &Track::flex);
388 add("slope", &Loader::slope);
391 void Track::Loader::position(float x, float y, float z)
393 obj.set_position(Vector(x, y, z));
396 void Track::Loader::rotation(float r)
398 obj.set_rotation(Angle::from_radians(r));
401 void Track::Loader::sensor_id(unsigned id)
403 obj.set_sensor_id(id);
406 void Track::Loader::slope(float s)
408 obj.set_tilt(Geometry::atan(s/obj.type.get_path_length(0)));
411 void Track::Loader::tilt(float t)
413 obj.set_tilt(Angle::from_radians(t));
416 void Track::Loader::turnout_id(unsigned id)
418 obj.set_turnout_id(id);