]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/vehicle.cpp
Simplify some interfaces by using track and block iterators
[r2c2.git] / source / libr2c2 / vehicle.cpp
index 6b89fa8743b604036ccab17c7a42a48e94e8c208..1c4a5b0f441ec9d5e6b40f711cc54afd176cbf08 100644 (file)
@@ -14,11 +14,10 @@ using namespace Msp;
 namespace R2C2 {
 
 Vehicle::Vehicle(Layout &l, const VehicleType &t):
-       layout(l),
+       Object(l),
        type(t),
        next(0),
        prev(0),
-       direction(0),
        front_sensor(0),
        back_sensor(0)
 {
@@ -38,10 +37,18 @@ Vehicle::~Vehicle()
        layout.remove_vehicle(*this);
 }
 
+Vehicle *Vehicle::clone(Layout *to_layout) const
+{
+       Vehicle *veh = new Vehicle((to_layout ? *to_layout : layout), type);
+       veh->set_position(position);
+       veh->set_rotation(rotation);
+       return veh;
+}
+
 void Vehicle::attach_back(Vehicle &veh)
 {
        if(next || veh.prev)
-               throw InvalidState("Already attached");
+               throw attachment_error("already attached");
 
        next = &veh;
        veh.prev = this;
@@ -53,7 +60,7 @@ void Vehicle::attach_back(Vehicle &veh)
 void Vehicle::attach_front(Vehicle &veh)
 {
        if(prev || veh.next)
-               throw InvalidState("Already attached");
+               throw attachment_error("already attached");
 
        prev = &veh;
        veh.next = this;
@@ -65,7 +72,7 @@ void Vehicle::attach_front(Vehicle &veh)
 void Vehicle::detach_back()
 {
        if(!next)
-               throw InvalidState("Not attached");
+               throw attachment_error("not attached");
 
        next->prev = 0;
        next = 0;
@@ -74,15 +81,18 @@ void Vehicle::detach_back()
 void Vehicle::detach_front()
 {
        if(!prev)
-               throw InvalidState("Not attached");
+               throw attachment_error("not attached");
 
        prev->next = 0;
        prev = 0;
 }
 
-void Vehicle::place(Track &t, unsigned e, float o, PlaceMode m)
+void Vehicle::place(const TrackIter &t, float o, PlaceMode m)
 {
-       track_pos = TrackPosition(&t, e, o);
+       if(!t)
+               throw invalid_argument("Vehicle::place");
+
+       track_pos = TrackPosition(t, o);
 
        if(m==FRONT_AXLE)
                track_pos.advance(-type.get_front_axle_offset());
@@ -121,30 +131,30 @@ void Vehicle::advance(float d)
 const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const
 {
        if(i>=axles.size())
-               throw InvalidParameterValue("Axle index out of range");
+               throw out_of_range("Vehicle::get_fixed_axle");
        return axles[i];
 }
 
 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
 {
        if(i>=bogies.size())
-               throw InvalidParameterValue("Bogie index out of range");
+               throw out_of_range("Vehicle::get_bogie");
        return bogies[i];
 }
 
 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
 {
        if(i>=bogies.size())
-               throw InvalidParameterValue("Bogie index out of range");
+               throw out_of_range("Vehicle::get_bogie_axle");
        if(j>=bogies[i].axles.size())
-               throw InvalidParameterValue("Axle index out of range");
+               throw out_of_range("Vehicle::get_bogie_axle");
        return bogies[i].axles[j];
 }
 
 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
 {
        if(i>=rods.size())
-               throw InvalidParameterValue("Rod index out of range");
+               throw out_of_range("Vehicle::get_rod");
        return rods[i];
 }
 
@@ -189,7 +199,7 @@ void Vehicle::update_position()
 
        position = tp.pos;
        position.z += layout.get_catalogue().get_rail_elevation();
-       direction = tp.dir;
+       rotation = tp.dir;
 }
 
 void Vehicle::update_position_from(const Vehicle &veh)
@@ -200,7 +210,7 @@ void Vehicle::update_position_from(const Vehicle &veh)
        float margin = layout.get_catalogue().get_scale();
 
        float dist = distance(veh.position, position);
-       if(dist<tdist-margin || dist>tdist+margin)
+       if(!track_pos.track || dist<tdist-margin || dist>tdist+margin)
        {
                track_pos = veh.track_pos;
                track_pos.advance(sign*tdist);
@@ -267,10 +277,10 @@ void Vehicle::check_sensor(float offset, unsigned &sensor)
 void Vehicle::turn_axles(float d)
 {
        for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
-               i->angle += d*2/i->type->wheel_dia;
+               i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
        for(vector<Bogie>::iterator i=bogies.begin(); i!=bogies.end(); ++i)
                for(vector<Axle>::iterator j=i->axles.begin(); j!=i->axles.end(); ++j)
-                       j->angle += d*2/j->type->wheel_dia;
+                       j->angle += Angle::from_radians(d*2/j->type->wheel_dia);
 
        update_rods();
 }
@@ -284,19 +294,17 @@ void Vehicle::update_rods()
                else if(i->type->pivot==VehicleType::Rod::AXLE)
                {
                        const Axle &axle = get_fixed_axle(i->type->pivot_index);
-                       float c = cos(axle.angle);
-                       float s = sin(axle.angle);
                        const Vector &pp = i->type->pivot_point;
-                       i->position = Vector(axle.type->position+pp.x*c+pp.z*s, pp.y, axle.type->wheel_dia/2+pp.z*c-pp.x*s);
+                       Transform trans = Transform::rotation(axle.angle, Vector(0, -1, 0));
+                       i->position = Vector(axle.type->position, 0, axle.type->wheel_dia/2)+trans.transform(pp);
                }
                else if(i->type->pivot==VehicleType::Rod::ROD)
                {
                        const Rod &prod = get_rod(i->type->pivot_index);
-                       float c = cos(prod.angle);
-                       float s = sin(prod.angle);
                        const Vector &pos = prod.position;
                        const Vector &off = i->type->pivot_point;
-                       i->position = Vector(pos.x+off.x*c-off.z*s, pos.y+off.y, pos.z+off.z*c+off.x*s);
+                       Transform trans = Transform::rotation(prod.angle, Vector(0, 1, 0));
+                       i->position = pos+trans.transform(off);
                }
 
                if(i->type->connect_index>=0)
@@ -304,30 +312,27 @@ void Vehicle::update_rods()
                        Rod &crod = rods[i->type->connect_index];
                        if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::SLIDE_X)
                        {
-                               float dx = (crod.position.x+i->type->connect_offset.x)-i->position.x;
-                               float dz = (crod.position.z+i->type->connect_offset.z)-i->position.z;
-                               float cd = sqrt(i->type->connect_point.x*i->type->connect_point.x+i->type->connect_point.z*i->type->connect_point.z);
-                               float ca = atan2(i->type->connect_point.z, i->type->connect_point.x);
-                               dx = sqrt(cd*cd-dz*dz)*(dx>0 ? 1 : -1);
-                               i->angle = atan2(dz, dx)-ca;
-                               crod.position.x = i->position.x+dx-i->type->connect_offset.x;
+                               Vector span = crod.position+i->type->connect_offset-i->position;
+                               float cd = i->type->connect_point.norm();
+                               Angle ca = Geometry::atan2(i->type->connect_point.z, i->type->connect_point.x);
+                               span.x = sqrt(cd*cd-span.z*span.z)*(span.x>0 ? 1 : -1);
+                               i->angle = Geometry::atan2(span.z, span.x)-ca;
+                               crod.position.x = i->position.x+span.x-i->type->connect_offset.x;
                        }
                        else if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::ROTATE)
                        {
-                               float dx = crod.position.x-i->position.x;
-                               float dz = crod.position.z-i->position.z;
-                               float d = sqrt(dx*dx+dz*dz);
-                               float cd1 = sqrt(i->type->connect_point.x*i->type->connect_point.x+i->type->connect_point.z*i->type->connect_point.z);
-                               float cd2 = sqrt(i->type->connect_offset.x*i->type->connect_offset.x+i->type->connect_offset.z*i->type->connect_offset.z);
+                               Vector span = crod.position-i->position;
+                               float d = span.norm();
+                               float cd1 = i->type->connect_point.norm();
+                               float cd2 = i->type->connect_offset.norm();
                                float a = (d*d+cd1*cd1-cd2*cd2)/(2*d);
                                float b = sqrt(cd1*cd1-a*a);
-                               float sign = (dx*i->type->connect_point.z-dz*i->type->connect_point.x>0 ? 1 : -1);
-                               float cx = (dx*a-dz*b*sign)/d;
-                               float cz = (dz*a+dx*b*sign)/d;
-                               float ca1 = atan2(i->type->connect_point.z, i->type->connect_point.x);
-                               float ca2 = atan2(i->type->connect_offset.z, i->type->connect_offset.x);
-                               i->angle = atan2(cz, cx)-ca1;
-                               crod.angle = atan2(cz-dz, cx-dx)-ca2;
+                               float sign = (cross(i->type->connect_point, span).y>0 ? 1 : -1);
+                               Vector conn = Vector(span.x*a-span.z*b, 0, span.z*a+span.x*b)/(d*sign);
+                               Angle ca1 = Geometry::atan2(i->type->connect_point.z, i->type->connect_point.x);
+                               Angle ca2 = Geometry::atan2(i->type->connect_offset.z, i->type->connect_offset.x);
+                               i->angle = Geometry::atan2(conn.z, conn.x)-ca1;
+                               crod.angle = Geometry::atan2(conn.z-span.z, conn.x-span.x)-ca2;
                        }
                }
        }
@@ -342,10 +347,7 @@ void Vehicle::adjust_for_distance(TrackPosition &front, TrackPosition &back, flo
                Vector front_point = front.get_point().pos;
                Vector back_point = back.get_point().pos;
 
-               float dx = front_point.x-back_point.x;
-               float dy = front_point.y-back_point.y;
-               float dz = front_point.z-back_point.z;
-               float dist = sqrt(dx*dx+dy*dy+dz*dz);
+               float dist = distance(front_point, back_point);
 
                float diff = tdist-dist;
                if(diff<-margin && adjust_dir<=0)
@@ -368,13 +370,11 @@ void Vehicle::adjust_for_distance(TrackPosition &front, TrackPosition &back, flo
 
 TrackPoint Vehicle::get_point(const Vector &front, const Vector &back, float ratio) const
 {
-       float dx = front.x-back.x;
-       float dy = front.y-back.y;
-       float dz = front.z-back.z;
+       Vector span = front-back;
 
        TrackPoint tp;
-       tp.pos = Vector(back.x+dx*ratio, back.y+dy*ratio, back.z+dz*ratio);
-       tp.dir = atan2(dy, dx);
+       tp.pos = back+span*ratio;
+       tp.dir = Geometry::atan2(span.y, span.x);
 
        return tp;
 }
@@ -391,16 +391,37 @@ TrackPoint Vehicle::get_point(const TrackPosition &pos, float tdist, float ratio
        return get_point(front.get_point().pos, back.get_point().pos, ratio);
 }
 
+unsigned Vehicle::get_n_link_slots() const
+{
+       return 2;
+}
+
+Vehicle *Vehicle::get_link(unsigned i) const
+{
+       if(i>=2)
+               throw out_of_range("Vehicle::get_link");
+
+       return (i==0 ? prev : next);
+}
+
+int Vehicle::get_link_slot(const Object &other) const
+{
+       if(&other==prev)
+               return 0;
+       else if(&other==next)
+               return 1;
+       else
+               return -1;
+}
+
 
 Vehicle::Axle::Axle(const VehicleType::Axle &t):
-       type(&t),
-       angle(0)
+       type(&t)
 { }
 
 
 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
-       type(&t),
-       direction(0)
+       type(&t)
 {
        for(VehicleType::AxleArray::const_iterator i=type->axles.begin(); i!=type->axles.end(); ++i)
                axles.push_back(*i);
@@ -408,20 +429,16 @@ Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
 
 
 Vehicle::Rod::Rod(const VehicleType::Rod &t):
-       type(&t),
-       angle(0)
+       type(&t)
 { }
 
 
 Vehicle::TrackPosition::TrackPosition():
-       track(0),
-       ep(0),
        offs(0)
 { }
 
-Vehicle::TrackPosition::TrackPosition(Track *t, unsigned e, float o):
+Vehicle::TrackPosition::TrackPosition(const TrackIter &t, float o):
        track(t),
-       ep(e),
        offs(o)
 { }
 
@@ -431,33 +448,30 @@ void Vehicle::TrackPosition::advance(float d)
                return;
 
        offs += d;
-       TrackIter iter(track, ep);
-       while(iter)
+       while(track)
        {
-               float path_len = iter->get_type().get_path_length(iter->get_active_path());
+               float path_len = track->get_type().get_path_length(track->get_active_path());
 
                if(offs>path_len)
                {
                        offs -= path_len;
-                       iter = iter.next();
+                       track = track.next();
                }
                else
                        break;
        }
 
-       while(iter && offs<0)
+       while(track && offs<0)
        {
-               iter = iter.flip().reverse();
+               track = track.flip().reverse();
 
-               if(iter)
+               if(track)
                {
-                       float path_len = iter->get_type().get_path_length(iter->get_active_path());
+                       float path_len = track->get_type().get_path_length(track->get_active_path());
                        offs += path_len;
                }
        }
 
-       track = iter.track();
-       ep = iter.entry();
        if(!track)
                offs = 0;
 }
@@ -465,7 +479,7 @@ void Vehicle::TrackPosition::advance(float d)
 TrackPoint Vehicle::TrackPosition::get_point() const
 {
        if(track)
-               return track->get_point(ep, offs);
+               return track->get_point(track.entry(), offs);
        else
                return TrackPoint();
 }