]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/track.cpp
Remove diversion logic
[r2c2.git] / source / libr2c2 / track.cpp
index cf496c41ddae554b98da55d62e67bc2b3e0179b4..d4e9d12d8ef7b08682f0f9c97d5f4cb36a3cce38 100644 (file)
@@ -21,7 +21,8 @@ Track::Track(Layout &l, const TrackType &t):
        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();
@@ -43,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;
 }
@@ -53,7 +54,7 @@ void Track::set_block(Block *b)
 Block &Track::get_block() const
 {
        if(!block)
-               throw InvalidState("No Block");
+               throw logic_error("!block");
 
        return *block;
 }
@@ -116,7 +117,7 @@ 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);
@@ -128,7 +129,7 @@ void Track::set_turnout_id(unsigned i)
 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);
@@ -139,10 +140,11 @@ 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<<p)))
-               throw InvalidParameterValue("Invalid path");
+               throw invalid_argument("Track::set_active_path");
 
+       path_changing = true;
        layout.get_driver().set_turnout(turnout_id, p);
 }
 
@@ -159,7 +161,7 @@ Vector Track::get_endpoint_position(unsigned epi) const
 {
        const vector<TrackType::Endpoint> &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];
 
@@ -176,7 +178,7 @@ float Track::get_endpoint_direction(unsigned epi) const
 {
        const vector<TrackType::Endpoint> &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];
 
@@ -288,8 +290,8 @@ void Track::break_links()
 
 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];
 }
@@ -326,6 +328,19 @@ 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);
@@ -360,13 +375,14 @@ void Track::turnout_event(unsigned addr, unsigned state)
        if(addr==turnout_id)
        {
                active_path = state;
+               path_changing = false;
                signal_path_changed.emit(active_path);
        }
 }
 
 
 Track::Loader::Loader(Track &t):
-       DataFile::BasicLoader<Track>(t)
+       DataFile::ObjectLoader<Track>(t)
 {
        add("position",   &Loader::position);
        add("rotation",   &Track::rot);