#include <cmath>
+#include <msp/core/maputils.h>
+#include <msp/strings/format.h>
#include "block.h"
#include "catalogue.h"
#include "driver.h"
#include "layout.h"
#include "track.h"
+#include "trackattachment.h"
#include "tracktype.h"
using namespace std;
using namespace Msp;
+namespace {
+
+struct AttachmentCompare
+{
+ unsigned entry;
+
+ AttachmentCompare(unsigned e): entry(e) { }
+
+ bool operator()(const R2C2::TrackAttachment *a1, const R2C2::TrackAttachment *a2) const
+ { return a1->get_offset_from_endpoint(entry)<a2->get_offset_from_endpoint(entry); }
+};
+
+}
+
namespace R2C2 {
Track::Track(Layout &l, const TrackType &t):
- layout(l),
+ Object(l),
type(t),
block(0),
- rot(0),
slope(0),
flex(false),
- turnout_id(0),
- sensor_id(0),
+ turnout_addr(0),
+ sensor_addr(0),
links(type.get_endpoints().size()),
- active_path(0)
+ active_path(0),
+ path_changing(false)
{
if(type.is_turnout())
- turnout_id = layout.allocate_turnout_id();
+ {
+ turnout_addr = layout.allocate_turnout_address();
- layout.add_track(*this);
+ if(layout.has_driver())
+ {
+ Driver &driver = layout.get_driver();
+ turnout_id = driver.add_turnout(turnout_addr, type);
+ driver.signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
+ driver.signal_turnout_failed.connect(sigc::mem_fun(this, &Track::turnout_failed));
+ }
+ }
+
+ layout.add(*this);
- if(layout.has_driver())
- layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
}
Track::~Track()
{
break_links();
- layout.remove_track(*this);
+ if(layout.has_driver() && turnout_id)
+ layout.get_driver().remove_turnout(turnout_id);
+ layout.remove(*this);
+}
+
+Track *Track::clone(Layout *to_layout) const
+{
+ Track *track = new Track((to_layout ? *to_layout : layout), type);
+ track->set_position(position);
+ track->set_rotation(rotation);
+ return track;
}
void Track::set_block(Block *b)
void Track::set_position(const Vector &p)
{
- pos = p;
+ position = p;
+ signal_moved.emit();
+ propagate_slope();
}
-void Track::set_rotation(float r)
+void Track::set_rotation(const Angle &r)
{
- rot = r;
- while(rot<0)
- rot += M_PI*2;
- while(rot>M_PI*2)
- rot -= M_PI*2;
+ rotation = wrap_positive(r);
+ signal_moved.emit();
}
-void Track::set_slope(float s)
+void Track::set_tilt(const Angle &t)
{
if(links.size()!=2)
return;
- slope = s;
+ tilt = t;
+ slope = tan(tilt)*type.get_path_length(0);
+ signal_moved.emit();
+ propagate_slope();
}
void Track::set_flex(bool f)
flex = f;
}
+void Track::propagate_slope()
+{
+ for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
+ if(*i)
+ (*i)->check_slope();
+}
+
void Track::check_slope()
{
if(links.size()!=2)
if(links[0] && links[1])
{
- Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
- Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
- pos.z = epp0.z;
- slope = epp1.z-pos.z;
+ Vector epp0 = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
+ Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
+ position.z = epp0.z;
+ slope = epp1.z-position.z;
+ tilt = Geometry::atan(slope/type.get_path_length(0));
}
else
{
- slope = 0;
if(links[0])
{
- Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
- pos.z = epp.z;
+ Vector epp = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
+ position.z = epp.z;
}
else if(links[1])
{
- Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
- pos.z = epp.z;
+ Vector epp = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
+ position.z = epp.z-slope;
}
}
+
+ signal_moved.emit();
}
-void Track::set_turnout_id(unsigned i)
+void Track::set_turnout_address(unsigned a)
{
if(!type.is_turnout())
throw logic_error("not a turnout");
+ if(!a)
+ throw invalid_argument("Track::set_turnout_id");
+
+ Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0);
- turnout_id = i;
+ if(driver && turnout_id)
+ driver->remove_turnout(turnout_id);
+ turnout_addr = a;
layout.create_blocks(*this);
layout.update_routes();
- if(layout.has_driver() && turnout_id)
- layout.get_driver().add_turnout(turnout_id, type);
+ if(driver && turnout_addr)
+ turnout_id = driver->add_turnout(turnout_addr, type);
+ else
+ turnout_id = 0;
}
-void Track::set_sensor_id(unsigned i)
+void Track::set_sensor_address(unsigned a)
{
if(type.is_turnout())
throw logic_error("is a turnout");
- sensor_id = i;
+ sensor_addr = a;
layout.create_blocks(*this);
- if(layout.has_driver() && sensor_id)
- layout.get_driver().add_sensor(sensor_id);
}
void Track::set_active_path(unsigned p)
{
- if(!turnout_id)
+ if(!type.is_turnout())
throw logic_error("not a turnout");
if(!(type.get_paths()&(1<<p)))
throw invalid_argument("Track::set_active_path");
+ if(active_path==p)
+ return;
+
+ signal_path_changing(p);
+ path_changing = true;
layout.get_driver().set_turnout(turnout_id, p);
}
-int Track::get_endpoint_by_link(Track &other) const
+float Track::get_path_length(int p) const
{
- for(unsigned i=0; i<links.size(); ++i)
- if(links[i]==&other)
- return i;
-
- return -1;
+ if(p<0)
+ p = active_path;
+ return type.get_path_length(p);
}
-Vector Track::get_endpoint_position(unsigned epi) const
+OrientedPoint Track::get_point(unsigned epi, unsigned path, float d) const
{
- const vector<TrackType::Endpoint> &eps = type.get_endpoints();
- if(epi>=eps.size())
- throw out_of_range("Track::get_endpoint_position");
+ OrientedPoint p = type.get_point(epi, path, d);
- const TrackType::Endpoint &ep = eps[epi];
-
- float c = cos(rot);
- float s = sin(rot);
+ p.position = position+rotated_vector(p.position, rotation);
+ p.rotation += rotation;
+ if(type.get_endpoints().size()==2)
+ {
+ float dz = tan(tilt)*d;
+ if(epi==0)
+ {
+ p.position.z += dz;
+ p.tilt = tilt;
+ }
+ else
+ {
+ p.position.z += slope-dz;
+ p.tilt = -tilt;
+ }
+ }
- Vector p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
- if(eps.size()==2 && epi==1)
- p.z += slope;
return p;
}
-float Track::get_endpoint_direction(unsigned epi) const
+OrientedPoint Track::get_point(unsigned epi, float d) const
{
- const vector<TrackType::Endpoint> &eps = type.get_endpoints();
- if(epi>=eps.size())
- throw out_of_range("Track::get_endpoint_direction");
-
- const TrackType::Endpoint &ep = eps[epi];
-
- return rot+ep.dir;
+ return get_point(epi, active_path, d);
}
-bool Track::snap_to(Track &other, bool link, float limit)
+unsigned Track::get_n_snap_nodes() const
{
- if(!limit || link)
- {
- limit = layout.get_catalogue().get_gauge();
- if(link && !flex && !other.get_flex())
- limit /= 10;
- }
- limit *= limit;
+ return type.get_endpoints().size();
+}
+Snap Track::get_snap_node(unsigned i) const
+{
const vector<TrackType::Endpoint> &eps = type.get_endpoints();
- const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
+ if(i>=eps.size())
+ throw out_of_range("Track::get_snap_node");
- for(unsigned i=0; i<eps.size(); ++i)
- {
- Vector epp = get_endpoint_position(i);
+ Snap result;
+ const TrackType::Endpoint &ep = eps[i];
- for(unsigned j=0; j<other_eps.size(); ++j)
- {
- if(other.get_link(j))
- continue;
-
- Vector epp2 = other.get_endpoint_position(j);
- float dx = epp2.x-epp.x;
- float dy = epp2.y-epp.y;
- float dz = epp2.z-epp.z;
- if(dx*dx+dy*dy<limit && dz*dz<limit)
- {
- if(!link || (!flex && !other.get_flex()))
- {
- set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
- Vector p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
- epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
- epp2.z);
- if(eps.size()==2 && i==1)
- p.z -= slope;
- set_position(p);
- }
-
- if(link)
- {
- if(links[i])
- break_link(*links[i]);
- links[i] = &other;
- other.links[j] = this;
- layout.create_blocks(*this);
-
- signal_link_changed.emit(i, &other);
- other.signal_link_changed.emit(j, this);
- }
+ result.position = position+rotated_vector(ep.pos, rotation);
+ if(eps.size()==2 && i==1)
+ result.position.z += slope;
- return true;
- }
- }
- }
+ result.rotation = rotation+ep.dir;
- return false;
+ return result;
}
-bool Track::snap(Vector &pt, float &d) const
+bool Track::snap(Snap &sn, float limit, SnapType what) const
{
- const vector<TrackType::Endpoint> &eps = type.get_endpoints();
+ if(Object::snap(sn, limit, what))
+ return true;
- for(unsigned i=0; i<eps.size(); ++i)
+ if(what&SNAP_SEGMENT)
{
- Vector epp = get_endpoint_position(i);
- float dx = pt.x-epp.x;
- float dy = pt.y-epp.y;
- if(dx*dx+dy*dy<1e-4)
+ Vector local = rotated_vector(sn.position-position, -rotation);
+
+ OrientedPoint np = type.get_nearest_point(local);
+ Vector span = local-np.position;
+ if(dot(span, span)<=limit*limit)
{
- pt = epp;
- d = rot+eps[i].dir;
+ sn.position = position+rotated_vector(np.position, rotation);
+ sn.rotation = np.rotation+rotation;
return true;
}
}
return false;
}
-void Track::break_link(Track &trk)
+SnapType Track::get_default_snap_type_to(const Object &other) const
{
- for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
- if(*i==&trk)
- {
- *i = 0;
- trk.break_link(*this);
- // XXX Creates the blocks twice
- layout.create_blocks(*this);
- signal_link_changed.emit(i-links.begin(), 0);
- return;
- }
+ if(dynamic_cast<const Track *>(&other))
+ return SNAP_NODE;
+
+ return NO_SNAP;
}
-void Track::break_links()
+unsigned Track::get_n_link_slots() const
{
- for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
- if(Track *trk=*i)
- {
- *i = 0;
- trk->break_link(*this);
- }
+ return links.size();
}
Track *Track::get_link(unsigned i) const
return links[i];
}
-TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
+int Track::get_link_slot(const Object &other) const
{
- TrackPoint p = type.get_point(epi, path, d);
- float c = cos(rot);
- float s = sin(rot);
+ for(unsigned i=0; i<links.size(); ++i)
+ if(links[i]==&other)
+ return i;
- 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);
- p.dir += rot;
- if(type.get_endpoints().size()==2)
+ return -1;
+}
+
+bool Track::link_to(Object &other)
+{
+ Track *otrack = dynamic_cast<Track *>(&other);
+ if(!otrack)
+ return false;
+
+ float gauge_ratio = otrack->get_type().get_gauge()/type.get_gauge();
+ if(gauge_ratio<0.99 || gauge_ratio>1.01)
+ return false;
+
+ float limit = type.get_gauge();
+ if(!flex && !otrack->get_flex())
+ limit /= 10;
+ limit *= limit;
+
+ unsigned nsn = get_n_snap_nodes();
+ unsigned other_nsn = other.get_n_snap_nodes();
+ for(unsigned i=0; i<nsn; ++i)
{
- float len = type.get_path_length(path);
- float grade = slope/len;
- if(epi==0)
- {
- p.pos.z += grade*d;
- p.grade = grade;
- }
- else
+ Snap sn = get_snap_node(i);
+ for(unsigned j=0; j<other_nsn; ++j)
{
- p.pos.z += slope-grade*d;
- p.grade = -grade;
+ Snap osn = other.get_snap_node(j);
+ Vector span = osn.position-sn.position;
+ Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
+
+ if(dot(span, span)<limit && abs(da).radians()<0.01)
+ {
+ break_link(i);
+ otrack->break_link(j);
+ links[i] = otrack;
+ otrack->links[j] = this;
+ check_slope();
+ layout.create_blocks(*this);
+
+ signal_link_changed.emit(i, otrack);
+ otrack->signal_link_changed.emit(j, this);
+ return true;
+ }
}
}
- return p;
+ return false;
}
-TrackPoint Track::get_point(unsigned epi, float d) const
+bool Track::break_link(unsigned i)
{
- return get_point(epi, active_path, d);
-}
+ if(i>=links.size())
+ throw out_of_range("Track::break_link");
-TrackPoint Track::get_nearest_point(const Vector &p) const
-{
- Vector local(p.x-pos.x, p.y-pos.y, p.z-pos.z);
- float c = cos(rot);
- float s = sin(rot);
- local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z);
+ Track *other = links[i];
+ if(!other)
+ return false;
+
+ links[i] = 0;
+ if(!other->break_link(*this))
+ {
+ /* If the call doesn't succeed, it means that the other track already
+ broke the link and is calling us right now. Recreate blocks in the inner
+ call so it occurs before any signals are emitted. */
+ layout.create_blocks(*this);
+ }
+
+ signal_link_changed.emit(i, 0);
- TrackPoint tp = type.get_nearest_point(local);
- tp.pos = Vector(pos.x+tp.pos.x*c-tp.pos.y*s, pos.y+tp.pos.y*c+tp.pos.x*s, pos.z+tp.pos.z);
- tp.dir += rot;
- return tp;
+ return true;
}
-bool Track::collide_ray(const Vector &start, const Vector &ray)
+void Track::add_attachment(TrackAttachment &a)
{
- Vector local_start(start.x-pos.x, start.y-pos.y, start.z-pos.z);
- float c = cos(rot);
- float s = sin(rot);
- local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
- Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
+ if(find(attachments.begin(), attachments.end(), &a)!=attachments.end())
+ throw key_error(&a);
+ attachments.push_back(&a);
+}
- float width = layout.get_catalogue().get_ballast_profile().get_width();
+void Track::remove_attachment(TrackAttachment &a)
+{
+ AttachmentList::iterator i = find(attachments.begin(), attachments.end(), &a);
+ if(i==attachments.end())
+ throw key_error(&a);
+ attachments.erase(i);
+}
- return type.collide_ray(local_start, local_ray, width);
+Track::AttachmentList Track::get_attachments_ordered(unsigned epi) const
+{
+ AttachmentList result = attachments;
+ result.sort(AttachmentCompare(epi));
+ return result;
}
void Track::save(list<DataFile::Statement> &st) const
{
- st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
- st.push_back((DataFile::Statement("rotation"), rot));
- st.push_back((DataFile::Statement("slope"), slope));
- if(turnout_id)
- st.push_back((DataFile::Statement("turnout_id"), turnout_id));
- if(sensor_id)
- st.push_back((DataFile::Statement("sensor_id"), sensor_id));
+ st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
+ st.push_back((DataFile::Statement("rotation"), rotation.radians()));
+ st.push_back((DataFile::Statement("tilt"), tilt.radians()));
+ if(turnout_addr)
+ st.push_back((DataFile::Statement("turnout_address"), turnout_addr));
+ if(sensor_addr)
+ st.push_back((DataFile::Statement("sensor_address"), sensor_addr));
if(flex)
st.push_back((DataFile::Statement("flex"), true));
}
-void Track::turnout_event(unsigned addr, unsigned state)
+void Track::turnout_event(unsigned id, unsigned state)
{
- if(!turnout_id)
- return;
-
- if(addr==turnout_id)
+ if(id==turnout_id)
{
active_path = state;
+ path_changing = false;
signal_path_changed.emit(active_path);
}
}
+void Track::turnout_failed(unsigned id)
+{
+ if(id==turnout_id)
+ {
+ path_changing = false;
+ layout.emergency(block, "Turnout failed");
+ }
+}
+
Track::Loader::Loader(Track &t):
DataFile::ObjectLoader<Track>(t)
{
add("position", &Loader::position);
- add("rotation", &Track::rot);
- add("slope", &Track::slope);
- add("turnout_id", &Loader::turnout_id);
- add("sensor_id", &Loader::sensor_id);
+ add("rotation", &Loader::rotation);
+ add("tilt", &Loader::tilt);
+ add("turnout_id", &Loader::turnout_address);
+ add("turnout_address", &Loader::turnout_address);
+ add("sensor_id", &Loader::sensor_address);
+ add("sensor_address", &Loader::sensor_address);
add("flex", &Track::flex);
+
+ // deprecated
+ add("slope", &Loader::slope);
}
void Track::Loader::position(float x, float y, float z)
{
- obj.pos = Vector(x, y, z);
+ obj.set_position(Vector(x, y, z));
+}
+
+void Track::Loader::rotation(float r)
+{
+ obj.set_rotation(Angle::from_radians(r));
+}
+
+void Track::Loader::sensor_address(unsigned addr)
+{
+ obj.set_sensor_address(addr);
+}
+
+void Track::Loader::slope(float s)
+{
+ obj.set_tilt(Geometry::atan(s/obj.type.get_path_length(0)));
}
-void Track::Loader::sensor_id(unsigned id)
+void Track::Loader::tilt(float t)
{
- obj.set_sensor_id(id);
+ obj.set_tilt(Angle::from_radians(t));
}
-void Track::Loader::turnout_id(unsigned id)
+void Track::Loader::turnout_address(unsigned addr)
{
- obj.set_turnout_id(id);
+ obj.set_turnout_address(addr);
}
} // namespace R2C2