X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=aeccaa697e4bc29a3e8b341737401200bd5bd82e;hb=9a2fd67cec715e371e293be638b126e0d1b2148d;hp=0d4052ea999a83b6394bb79f7d03f9216604fc39;hpb=f8a7788cee0261babfc4c804a58515aad6dfbc3d;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 0d4052e..aeccaa6 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -1,42 +1,60 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - #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): - 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), + 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) ; } @@ -44,15 +62,25 @@ 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 +{ + 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) { if(b && !b->has_track(*this)) - throw InvalidParameterValue("Track is not in the Block"); + throw logic_error("track not in block"); if(!b && block && block->has_track(*this)) - throw InvalidState("Track is still in a Block"); + throw logic_error("track still in block"); block = b; } @@ -60,31 +88,33 @@ void Track::set_block(Block *b) Block &Track::get_block() const { if(!block) - throw InvalidState("No Block"); + throw logic_error("!block"); return *block; } 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) @@ -92,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) @@ -99,169 +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)); - 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 InvalidState("Not a turnout"); + throw logic_error("not a turnout"); + if(!a) + throw invalid_argument("Track::set_turnout_id"); - turnout_id = i; + Driver *driver = (layout.has_driver() ? &layout.get_driver() : 0); + + 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 InvalidState("Can't set sensor on a 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) - throw InvalidState("Not a turnout"); + if(!type.is_turnout()) + throw logic_error("not a turnout"); if(!(type.get_paths()&(1< &eps = type.get_endpoints(); - if(epi>=eps.size()) - throw InvalidParameterValue("TrackType::Endpoint index out of range"); + 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 &eps = type.get_endpoints(); - if(epi>=eps.size()) - throw InvalidParameterValue("TrackType::Endpoint index out of range"); - - 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 &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 { - if(i>links.size()) - throw InvalidParameterValue("Link index out of range"); + if(i>=links.size()) + throw out_of_range("Track::get_link"); 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(&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"); + + 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); + + return true; +} + +void Track::add_attachment(TrackAttachment &a) +{ + if(find(attachments.begin(), attachments.end(), &a)!=attachments.end()) + throw key_error(&a); + attachments.push_back(&a); +} + +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); +} + +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"), 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::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; 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::BasicLoader(t) + DataFile::ObjectLoader(t) { + add("path", &Loader::path); 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::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.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