]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/track.cpp
Rename Point to Vector
[r2c2.git] / source / libr2c2 / track.cpp
index 0592731c6bb24aab7cae2b44cbca29437328502f..0d4052ea999a83b6394bb79f7d03f9216604fc39 100644 (file)
@@ -25,11 +25,14 @@ 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)
 {
+       if(type.is_turnout())
+               turnout_id = layout.allocate_turnout_id();
+
        layout.add_track(*this);
 
        if(layout.has_driver())
@@ -62,7 +65,7 @@ Block &Track::get_block() const
        return *block;
 }
 
-void Track::set_position(const Point &p)
+void Track::set_position(const Vector &p)
 {
        pos = p;
 }
@@ -96,8 +99,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 +109,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;
                }
        }
@@ -126,11 +129,7 @@ void Track::set_turnout_id(unsigned 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)
@@ -151,11 +150,7 @@ void Track::set_active_path(unsigned p)
        if(!(type.get_paths()&(1<<p)))
                throw InvalidParameterValue("Invalid path");
 
-       layout.get_driver().set_turnout(turnout_id, p&1);
-       if(type.is_double_address())
-               layout.get_driver().set_turnout(turnout_id+1, p&2);
-       else if(type.get_n_paths()>2)
-               active_path = (active_path&1) | (p&2);
+       layout.get_driver().set_turnout(turnout_id, p);
 }
 
 int Track::get_endpoint_by_link(Track &other) const
@@ -167,7 +162,7 @@ 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<TrackType::Endpoint> &eps = type.get_endpoints();
        if(epi>=eps.size())
@@ -178,7 +173,7 @@ Point Track::get_endpoint_position(unsigned epi) const
        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;
@@ -210,14 +205,14 @@ bool Track::snap_to(Track &other, bool link, float limit)
 
        for(unsigned i=0; i<eps.size(); ++i)
        {
-               Point epp = get_endpoint_position(i);
+               Vector epp = get_endpoint_position(i);
 
                for(unsigned j=0; j<other_eps.size(); ++j)
                {
                        if(other.get_link(j))
                                continue;
 
-                       Point epp2 = other.get_endpoint_position(j);
+                       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;
@@ -226,7 +221,7 @@ bool Track::snap_to(Track &other, bool link, float limit)
                                if(!link || (!flex && !other.get_flex()))
                                {
                                        set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
-                                       Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
+                                       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)
@@ -241,6 +236,9 @@ bool Track::snap_to(Track &other, bool link, float limit)
                                        links[i] = &other;
                                        other.links[j] = this;
                                        layout.create_blocks(*this);
+
+                                       signal_link_changed.emit(i, &other);
+                                       other.signal_link_changed.emit(j, this);
                                }
 
                                return true;
@@ -251,13 +249,13 @@ bool Track::snap_to(Track &other, bool link, float limit)
        return false;
 }
 
-bool Track::snap(Point &pt, float &d) const
+bool Track::snap(Vector &pt, float &d) const
 {
        const vector<TrackType::Endpoint> &eps = type.get_endpoints();
 
        for(unsigned i=0; i<eps.size(); ++i)
        {
-               Point epp = get_endpoint_position(i);
+               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)
@@ -280,6 +278,7 @@ void Track::break_link(Track &trk)
                        trk.break_link(*this);
                        // XXX Creates the blocks twice
                        layout.create_blocks(*this);
+                       signal_link_changed.emit(i-links.begin(), 0);
                        return;
                }
 }
@@ -308,7 +307,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)
        {
@@ -347,19 +346,16 @@ void Track::save(list<DataFile::Statement> &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;
+               signal_path_changed.emit(active_path);
+       }
 }
 
 
@@ -376,7 +372,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)