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);
174 void Track::set_active_path(unsigned p)
177 throw logic_error("not a turnout");
178 if(!(type.get_paths()&(1<<p)))
179 throw invalid_argument("Track::set_active_path");
181 signal_path_changing(p);
182 path_changing = true;
183 layout.get_driver().set_turnout(turnout_id, p);
186 OrientedPoint Track::get_point(unsigned epi, unsigned path, float d) const
188 OrientedPoint p = type.get_point(epi, path, d);
190 p.position = position+rotated_vector(p.position, rotation);
191 p.rotation += rotation;
192 if(type.get_endpoints().size()==2)
194 float dz = tan(tilt)*d;
202 p.position.z += slope-dz;
210 OrientedPoint Track::get_point(unsigned epi, float d) const
212 return get_point(epi, active_path, d);
215 unsigned Track::get_n_snap_nodes() const
217 return type.get_endpoints().size();
220 Snap Track::get_snap_node(unsigned i) const
222 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
224 throw out_of_range("Track::get_snap_node");
227 const TrackType::Endpoint &ep = eps[i];
229 result.position = position+rotated_vector(ep.pos, rotation);
230 if(eps.size()==2 && i==1)
231 result.position.z += slope;
233 result.rotation = rotation+ep.dir;
238 bool Track::snap(Snap &sn, float limit, SnapType what) const
240 if(Object::snap(sn, limit, what))
243 if(what&SNAP_SEGMENT)
245 Vector local = rotated_vector(sn.position-position, -rotation);
247 OrientedPoint np = type.get_nearest_point(local);
248 Vector span = local-np.position;
249 if(dot(span, span)<=limit*limit)
251 sn.position = position+rotated_vector(np.position, rotation);
252 sn.rotation = np.rotation+rotation;
260 SnapType Track::get_default_snap_type_to(const Object &other) const
262 if(dynamic_cast<const Track *>(&other))
268 unsigned Track::get_n_link_slots() const
273 Track *Track::get_link(unsigned i) const
276 throw out_of_range("Track::get_link");
281 int Track::get_link_slot(const Object &other) const
283 for(unsigned i=0; i<links.size(); ++i)
290 bool Track::link_to(Object &other)
292 Track *otrack = dynamic_cast<Track *>(&other);
296 float limit = layout.get_catalogue().get_gauge();
297 if(!flex && !otrack->get_flex())
301 unsigned nsn = get_n_snap_nodes();
302 unsigned other_nsn = other.get_n_snap_nodes();
303 for(unsigned i=0; i<nsn; ++i)
305 Snap sn = get_snap_node(i);
306 for(unsigned j=0; j<other_nsn; ++j)
308 Snap osn = other.get_snap_node(j);
309 Vector span = osn.position-sn.position;
310 Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
312 if(dot(span, span)<limit && abs(da).radians()<0.01)
316 otrack->links[j] = this;
318 layout.create_blocks(*this);
320 signal_link_changed.emit(i, otrack);
321 otrack->signal_link_changed.emit(j, this);
330 bool Track::break_link(unsigned i)
333 throw out_of_range("Track::break_link");
335 Track *other = links[i];
340 other->break_link(*this);
341 // XXX Creates the blocks twice, because the other track calls this too
342 layout.create_blocks(*this);
343 signal_link_changed.emit(i, 0);
348 void Track::add_attachment(TrackAttachment &a)
350 if(find(attachments.begin(), attachments.end(), &a)!=attachments.end())
352 attachments.push_back(&a);
355 void Track::remove_attachment(TrackAttachment &a)
357 AttachmentList::iterator i = find(attachments.begin(), attachments.end(), &a);
358 if(i==attachments.end())
360 attachments.erase(i);
363 Track::AttachmentList Track::get_attachments_ordered(unsigned epi) const
365 AttachmentList result = attachments;
366 result.sort(AttachmentCompare(epi));
370 void Track::save(list<DataFile::Statement> &st) const
372 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
373 st.push_back((DataFile::Statement("rotation"), rotation.radians()));
374 st.push_back((DataFile::Statement("tilt"), tilt.radians()));
376 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
378 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
380 st.push_back((DataFile::Statement("flex"), true));
383 void Track::turnout_event(unsigned addr, unsigned state)
391 path_changing = false;
392 signal_path_changed.emit(active_path);
397 Track::Loader::Loader(Track &t):
398 DataFile::ObjectLoader<Track>(t)
400 add("position", &Loader::position);
401 add("rotation", &Loader::rotation);
402 add("tilt", &Loader::tilt);
403 add("turnout_id", &Loader::turnout_id);
404 add("sensor_id", &Loader::sensor_id);
405 add("flex", &Track::flex);
408 add("slope", &Loader::slope);
411 void Track::Loader::position(float x, float y, float z)
413 obj.set_position(Vector(x, y, z));
416 void Track::Loader::rotation(float r)
418 obj.set_rotation(Angle::from_radians(r));
421 void Track::Loader::sensor_id(unsigned id)
423 obj.set_sensor_id(id);
426 void Track::Loader::slope(float s)
428 obj.set_tilt(Geometry::atan(s/obj.type.get_path_length(0)));
431 void Track::Loader::tilt(float t)
433 obj.set_tilt(Angle::from_radians(t));
436 void Track::Loader::turnout_id(unsigned id)
438 obj.set_turnout_id(id);