X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Flibr2c2%2Ftrack.cpp;h=d4e9d12d8ef7b08682f0f9c97d5f4cb36a3cce38;hb=66c2c7ca5a4bd369293959bc211b040834343381;hp=0592731c6bb24aab7cae2b44cbca29437328502f;hpb=1ff06c5bc46a677fa389ef86c6b26664368f1653;p=r2c2.git diff --git a/source/libr2c2/track.cpp b/source/libr2c2/track.cpp index 0592731..d4e9d12 100644 --- a/source/libr2c2/track.cpp +++ b/source/libr2c2/track.cpp @@ -1,10 +1,3 @@ -/* $Id$ - -This file is part of R²C² -Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa -Distributed under the GPL -*/ - #include #include "block.h" #include "catalogue.h" @@ -25,11 +18,15 @@ Track::Track(Layout &l, const TrackType &t): rot(0), slope(0), flex(false), - turnout_id(type.is_turnout() ? layout.allocate_turnout_id(type.is_double_address()) : 0), + turnout_id(0), sensor_id(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(); + layout.add_track(*this); if(layout.has_driver()) @@ -47,9 +44,9 @@ Track::~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; } @@ -57,12 +54,12 @@ 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 Point &p) +void Track::set_position(const Vector &p) { pos = p; } @@ -96,8 +93,8 @@ void Track::check_slope() if(links[0] && links[1]) { - Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this)); - Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this)); + 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; } @@ -106,12 +103,12 @@ void Track::check_slope() slope = 0; if(links[0]) { - Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this)); + Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this)); pos.z = epp.z; } else if(links[1]) { - Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this)); + Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this)); pos.z = epp.z; } } @@ -120,23 +117,19 @@ void Track::check_slope() void Track::set_turnout_id(unsigned i) { if(!type.is_turnout()) - throw InvalidState("Not a turnout"); + throw logic_error("not a turnout"); turnout_id = i; layout.create_blocks(*this); layout.update_routes(); if(layout.has_driver() && turnout_id) - { - layout.get_driver().add_turnout(turnout_id); - if(type.is_double_address()) - layout.get_driver().add_turnout(turnout_id+1); - } + layout.get_driver().add_turnout(turnout_id, type); } void Track::set_sensor_id(unsigned i) { if(type.is_turnout()) - throw InvalidState("Can't set sensor on a turnout"); + throw logic_error("is a turnout"); sensor_id = i; layout.create_blocks(*this); @@ -147,15 +140,12 @@ void Track::set_sensor_id(unsigned i) void Track::set_active_path(unsigned p) { if(!turnout_id) - throw InvalidState("Not a turnout"); + throw logic_error("not a turnout"); if(!(type.get_paths()&(1<2) - active_path = (active_path&1) | (p&2); + path_changing = true; + layout.get_driver().set_turnout(turnout_id, p); } int Track::get_endpoint_by_link(Track &other) const @@ -167,18 +157,18 @@ int Track::get_endpoint_by_link(Track &other) const return -1; } -Point Track::get_endpoint_position(unsigned epi) const +Vector Track::get_endpoint_position(unsigned epi) const { const vector &eps = type.get_endpoints(); if(epi>=eps.size()) - throw InvalidParameterValue("TrackType::Endpoint index out of range"); + throw out_of_range("Track::get_endpoint_position"); const TrackType::Endpoint &ep = eps[epi]; float c = cos(rot); float s = sin(rot); - Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z); + 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; @@ -188,7 +178,7 @@ float Track::get_endpoint_direction(unsigned epi) const { const vector &eps = type.get_endpoints(); if(epi>=eps.size()) - throw InvalidParameterValue("TrackType::Endpoint index out of range"); + throw out_of_range("Track::get_endpoint_direction"); const TrackType::Endpoint &ep = eps[epi]; @@ -210,14 +200,14 @@ bool Track::snap_to(Track &other, bool link, float limit) for(unsigned i=0; i &eps = type.get_endpoints(); for(unsigned i=0; ilinks.size()) - throw InvalidParameterValue("Link index out of range"); + if(i>=links.size()) + throw out_of_range("Track::get_link"); return links[i]; } @@ -308,7 +302,7 @@ TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const float c = cos(rot); float s = sin(rot); - p.pos = Point(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, pos.z); + 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) { @@ -334,6 +328,32 @@ TrackPoint Track::get_point(unsigned epi, float d) const return get_point(epi, active_path, d); } +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); + + 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; +} + +bool Track::collide_ray(const Vector &start, const Vector &ray) +{ + 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); + + float width = layout.get_catalogue().get_ballast_profile().get_width(); + + return type.collide_ray(local_start, local_ray, width); +} + void Track::save(list &st) const { st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z)); @@ -347,24 +367,22 @@ void Track::save(list &st) const st.push_back((DataFile::Statement("flex"), true)); } -void Track::turnout_event(unsigned addr, bool state) +void Track::turnout_event(unsigned addr, unsigned state) { if(!turnout_id) return; if(addr==turnout_id) - active_path = (active_path&2) | (state ? 1 : 0); - else if(type.is_double_address() && addr==turnout_id+1) - active_path = (active_path&1) | (state ? 2 : 0); - else - return; - - signal_path_changed.emit(active_path); + { + active_path = state; + path_changing = false; + signal_path_changed.emit(active_path); + } } Track::Loader::Loader(Track &t): - DataFile::BasicLoader(t) + DataFile::ObjectLoader(t) { add("position", &Loader::position); add("rotation", &Track::rot); @@ -376,7 +394,7 @@ Track::Loader::Loader(Track &t): void Track::Loader::position(float x, float y, float z) { - obj.pos = Point(x, y, z); + obj.pos = Vector(x, y, z); } void Track::Loader::sensor_id(unsigned id)