2 #include <msp/core/maputils.h>
8 #include "trackattachment.h"
16 struct AttachmentCompare
20 AttachmentCompare(unsigned e): entry(e) { }
22 bool operator()(const R2C2::TrackAttachment *a1, const R2C2::TrackAttachment *a2) const
23 { return a1->get_offset_from_endpoint(entry)<a2->get_offset_from_endpoint(entry); }
30 Track::Track(Layout &l, const TrackType &t):
38 links(type.get_endpoints().size()),
43 turnout_id = layout.allocate_turnout_id();
47 if(layout.has_driver())
48 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
50 for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
59 Track *Track::clone(Layout *to_layout) const
61 Track *track = new Track((to_layout ? *to_layout : layout), type);
62 track->set_position(position);
63 track->set_rotation(rotation);
67 void Track::set_block(Block *b)
69 if(b && !b->has_track(*this))
70 throw logic_error("track not in block");
71 if(!b && block && block->has_track(*this))
72 throw logic_error("track still in block");
77 Block &Track::get_block() const
80 throw logic_error("!block");
85 void Track::set_position(const Vector &p)
92 void Track::set_rotation(const Angle &r)
94 rotation = wrap_positive(r);
98 void Track::set_tilt(const Angle &t)
104 slope = tan(tilt)*type.get_path_length(0);
109 void Track::set_flex(bool f)
114 void Track::propagate_slope()
116 for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
121 void Track::check_slope()
126 if(links[0] && links[1])
128 Vector epp0 = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
129 Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
131 slope = epp1.z-position.z;
132 tilt = Geometry::atan(slope/type.get_path_length(0));
138 Vector epp = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
143 Vector epp = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
144 position.z = epp.z-slope;
151 void Track::set_turnout_id(unsigned i)
153 if(!type.is_turnout())
154 throw logic_error("not a turnout");
156 throw invalid_argument("Track::set_turnout_id");
159 layout.create_blocks(*this);
160 layout.update_routes();
161 if(layout.has_driver() && turnout_id)
162 layout.get_driver().add_turnout(turnout_id, type);
165 void Track::set_sensor_id(unsigned i)
167 if(type.is_turnout())
168 throw logic_error("is a turnout");
171 layout.create_blocks(*this);
172 if(layout.has_driver() && sensor_id)
173 layout.get_driver().add_sensor(sensor_id);
176 void Track::set_active_path(unsigned p)
179 throw logic_error("not a turnout");
180 if(!(type.get_paths()&(1<<p)))
181 throw invalid_argument("Track::set_active_path");
183 signal_path_changing(p);
184 path_changing = true;
185 layout.get_driver().set_turnout(turnout_id, p);
188 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
190 TrackPoint p = type.get_point(epi, path, d);
192 p.pos = position+rotated_vector(p.pos, rotation);
194 if(type.get_endpoints().size()==2)
196 float grade = tan(tilt);
204 p.pos.z += slope-grade*d;
212 TrackPoint Track::get_point(unsigned epi, float d) const
214 return get_point(epi, active_path, d);
217 unsigned Track::get_n_snap_nodes() const
219 return type.get_endpoints().size();
222 Snap Track::get_snap_node(unsigned i) const
224 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
226 throw out_of_range("Track::get_snap_node");
229 const TrackType::Endpoint &ep = eps[i];
231 result.position = position+rotated_vector(ep.pos, rotation);
232 if(eps.size()==2 && i==1)
233 result.position.z += slope;
235 result.rotation = rotation+ep.dir;
240 bool Track::snap(Snap &sn, float limit, SnapType what) const
242 if(Object::snap(sn, limit, what))
245 if(what&SNAP_SEGMENT)
247 Vector local = rotated_vector(sn.position-position, -rotation);
249 TrackPoint tp = type.get_nearest_point(local);
250 Vector span = local-tp.pos;
251 if(dot(span, span)<=limit*limit)
253 sn.position = position+rotated_vector(tp.pos, rotation);
254 sn.rotation = tp.dir+rotation;
262 SnapType Track::get_default_snap_type_to(const Object &other) const
264 if(dynamic_cast<const Track *>(&other))
270 unsigned Track::get_n_link_slots() const
275 Track *Track::get_link(unsigned i) const
278 throw out_of_range("Track::get_link");
283 int Track::get_link_slot(const Object &other) const
285 for(unsigned i=0; i<links.size(); ++i)
292 bool Track::link_to(Object &other)
294 Track *otrack = dynamic_cast<Track *>(&other);
298 float limit = layout.get_catalogue().get_gauge();
299 if(!flex && !otrack->get_flex())
303 unsigned nsn = get_n_snap_nodes();
304 unsigned other_nsn = other.get_n_snap_nodes();
305 for(unsigned i=0; i<nsn; ++i)
307 Snap sn = get_snap_node(i);
308 for(unsigned j=0; j<other_nsn; ++j)
310 Snap osn = other.get_snap_node(j);
311 Vector span = osn.position-sn.position;
312 Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
314 if(dot(span, span)<limit && abs(da).radians()<0.01)
318 otrack->links[j] = this;
320 layout.create_blocks(*this);
322 signal_link_changed.emit(i, otrack);
323 otrack->signal_link_changed.emit(j, this);
332 bool Track::break_link(unsigned i)
335 throw out_of_range("Track::break_link");
337 Track *other = links[i];
342 other->break_link(*this);
343 // XXX Creates the blocks twice, because the other track calls this too
344 layout.create_blocks(*this);
345 signal_link_changed.emit(i, 0);
350 void Track::add_attachment(TrackAttachment &a)
352 if(find(attachments.begin(), attachments.end(), &a)!=attachments.end())
354 attachments.push_back(&a);
357 void Track::remove_attachment(TrackAttachment &a)
359 AttachmentList::iterator i = find(attachments.begin(), attachments.end(), &a);
360 if(i==attachments.end())
362 attachments.erase(i);
365 Track::AttachmentList Track::get_attachments_ordered(unsigned epi) const
367 AttachmentList result = attachments;
368 result.sort(AttachmentCompare(epi));
372 void Track::save(list<DataFile::Statement> &st) const
374 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
375 st.push_back((DataFile::Statement("rotation"), rotation.radians()));
376 st.push_back((DataFile::Statement("tilt"), tilt.radians()));
378 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
380 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
382 st.push_back((DataFile::Statement("flex"), true));
385 void Track::turnout_event(unsigned addr, unsigned state)
393 path_changing = false;
394 signal_path_changed.emit(active_path);
399 Track::Loader::Loader(Track &t):
400 DataFile::ObjectLoader<Track>(t)
402 add("position", &Loader::position);
403 add("rotation", &Loader::rotation);
404 add("tilt", &Loader::tilt);
405 add("turnout_id", &Loader::turnout_id);
406 add("sensor_id", &Loader::sensor_id);
407 add("flex", &Track::flex);
410 add("slope", &Loader::slope);
413 void Track::Loader::position(float x, float y, float z)
415 obj.set_position(Vector(x, y, z));
418 void Track::Loader::rotation(float r)
420 obj.set_rotation(Angle::from_radians(r));
423 void Track::Loader::sensor_id(unsigned id)
425 obj.set_sensor_id(id);
428 void Track::Loader::slope(float s)
430 obj.set_tilt(Geometry::atan(s/obj.type.get_path_length(0)));
433 void Track::Loader::tilt(float t)
435 obj.set_tilt(Angle::from_radians(t));
438 void Track::Loader::turnout_id(unsigned id)
440 obj.set_turnout_id(id);