X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=aeccaa697e4bc29a3e8b341737401200bd5bd82e;hb=9a2fd67cec715e371e293be638b126e0d1b2148d;hp=b679af783867f72746793f205179f004a9e02baa;hpb=2910db1364914c0ab98a0f80250cc39137821577;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index b679af7..aeccaa6 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -1,14 +1,31 @@ #include +#include +#include #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)get_offset_from_endpoint(entry); } +}; + +} + namespace R2C2 { Track::Track(Layout &l, const TrackType &t): @@ -17,19 +34,27 @@ Track::Track(Layout &l, const TrackType &t): block(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), - path_changing(false) + path_changing(false), + preferred_exit(-1) { 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)); + } + } - if(layout.has_driver()) - layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event)); + layout.add(*this); for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ; } @@ -37,7 +62,9 @@ Track::Track(Layout &l, const TrackType &t): 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 @@ -69,23 +96,25 @@ Block &Track::get_block() const void Track::set_position(const Vector &p) { position = p; + signal_moved.emit(); + propagate_slope(); } -void Track::set_rotation(float r) +void Track::set_rotation(const Angle &r) { - rotation = r; - while(rotation<0) - rotation += M_PI*2; - while(rotation>M_PI*2) - rotation -= 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) @@ -93,6 +122,13 @@ void Track::set_flex(bool f) flex = f; } +void Track::propagate_slope() +{ + for(vector::const_iterator i=links.begin(); i!=links.end(); ++i) + if(*i) + (*i)->check_slope(); +} + void Track::check_slope() { if(links.size()!=2) @@ -100,170 +136,152 @@ void Track::check_slope() 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)); + 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)); + 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)); - position.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_preferred_exit(int e) +{ + preferred_exit = e; } 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< &eps = type.get_endpoints(); - if(epi>=eps.size()) - throw out_of_range("Track::get_endpoint_position"); - - const TrackType::Endpoint &ep = eps[epi]; + OrientedPoint p = type.get_point(epi, path, d); - float c = cos(rotation); - float s = sin(rotation); + 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(position.x+c*ep.pos.x-s*ep.pos.y, position.y+s*ep.pos.x+c*ep.pos.y, position.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 &eps = type.get_endpoints(); - if(epi>=eps.size()) - throw out_of_range("Track::get_endpoint_direction"); - - const TrackType::Endpoint &ep = eps[epi]; - - return rotation+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 &eps = type.get_endpoints(); - const vector &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 = type.get_endpoints(); + if(Object::snap(sn, limit, what)) + return true; - for(unsigned i=0; i::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(&other)) + return SNAP_NODE; + + return NO_SNAP; } -void Track::break_links() +unsigned Track::get_n_link_slots() const { - for(vector::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 @@ -303,83 +310,127 @@ 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(rotation); - float s = sin(rotation); + for(unsigned i=0; i(&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; ibreak_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-position.x, p.y-position.y, p.z-position.z); - float c = cos(rotation); - float s = sin(rotation); - 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); + } - TrackPoint tp = type.get_nearest_point(local); - tp.pos = Vector(position.x+tp.pos.x*c-tp.pos.y*s, position.y+tp.pos.y*c+tp.pos.x*s, position.z+tp.pos.z); - tp.dir += rotation; - return tp; + signal_link_changed.emit(i, 0); + + return true; } -bool Track::collide_ray(const Vector &start, const Vector &ray) const +void Track::add_attachment(TrackAttachment &a) { - Vector local_start(start.x-position.x, start.y-position.y, start.z-position.z); - float c = cos(rotation); - float s = sin(rotation); - 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 &st) const { st.push_back((DataFile::Statement("position"), position.x, position.y, position.z)); - st.push_back((DataFile::Statement("rotation"), rotation)); - 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("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::save_dynamic(list &st) const { - if(!turnout_id) - return; + if(turnout_addr) + st.push_back((DataFile::Statement("path"), active_path)); +} - if(addr==turnout_id) +void Track::turnout_event(unsigned id, unsigned state) +{ + if(id==turnout_id) { active_path = state; path_changing = false; @@ -387,36 +438,71 @@ void Track::turnout_event(unsigned addr, unsigned state) } } +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(t) { + add("path", &Loader::path); add("position", &Loader::position); add("rotation", &Loader::rotation); - add("slope", &Track::slope); - add("turnout_id", &Loader::turnout_id); - add("sensor_id", &Loader::sensor_id); + 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::path(unsigned p) +{ + obj.set_active_path(p); + if(obj.path_changing) + { + obj.active_path = p; + obj.signal_path_changed.emit(p); + } } void Track::Loader::position(float x, float y, float z) { - obj.position = Vector(x, y, z); + obj.set_position(Vector(x, y, z)); } void Track::Loader::rotation(float r) { - obj.rotation = 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