]> git.tdb.fi Git - r2c2.git/commitdiff
Add single get_stuff methods with index to VehicleType
authorMikko Rasa <tdb@tdb.fi>
Tue, 14 Dec 2010 21:30:16 +0000 (21:30 +0000)
committerMikko Rasa <tdb@tdb.fi>
Tue, 14 Dec 2010 21:30:16 +0000 (21:30 +0000)
Change how stuff is stored in and given out of Vehicle

source/3d/vehicle.cpp
source/libr2c2/vehicle.cpp
source/libr2c2/vehicle.h
source/libr2c2/vehicletype.cpp
source/libr2c2/vehicletype.h

index 50dbcb0d693134d969964ba31249bf71e3acc9c7..8027a1999d3827ab14dad36ced4816703f070a34 100644 (file)
@@ -64,7 +64,7 @@ void Vehicle3D::render(const GL::Tag &tag) const
                        {
                                GL::PushMatrix push_mat2;
                                GL::translate(axles[i].position, 0, axles[i].wheel_dia/2);
-                               GL::rotate(vehicle.get_axle_angle(i)*180/M_PI, 0, 1, 0);
+                               GL::rotate(vehicle.get_axle(i).angle*180/M_PI, 0, 1, 0);
                                obj->render(tag);
                        }
 
@@ -73,7 +73,7 @@ void Vehicle3D::render(const GL::Tag &tag) const
                {
                        GL::PushMatrix push_mat2;
                        GL::translate(bogies[i].position, 0, 0);
-                       float angle = vehicle.get_bogie_direction(i)*180/M_PI;
+                       float angle = vehicle.get_bogie(i).direction*180/M_PI;
                        GL::rotate(angle, 0, 0, 1);
 
                        for(unsigned j=0; j<bogies[i].axles.size(); ++j)
@@ -81,7 +81,7 @@ void Vehicle3D::render(const GL::Tag &tag) const
                                {
                                        GL::PushMatrix push_mat3;
                                        GL::translate(bogies[i].axles[j].position, 0, bogies[i].axles[j].wheel_dia/2);
-                                       GL::rotate(vehicle.get_bogie_axle_angle(i, j)*180/M_PI, 0, 1, 0);
+                                       GL::rotate(vehicle.get_bogie_axle(i, j).angle*180/M_PI, 0, 1, 0);
                                        obj->render(tag);
                                }
 
@@ -96,8 +96,8 @@ void Vehicle3D::render(const GL::Tag &tag) const
                        if(const GL::Object *obj = type.get_rod_object(i))
                        {
                                GL::PushMatrix push_mat2;
-                               const Point &rpos = vehicle.get_rod_position(i);
-                               float angle = vehicle.get_rod_angle(i);
+                               const Point &rpos = vehicle.get_rod(i).position;
+                               float angle = vehicle.get_rod(i).angle;
                                GL::translate(rpos.x, rpos.y, rpos.z);
                                if(rods[i].mirror_object)
                                        GL::scale(1, -1, 1);
index 578707a63ef5c47eab1bef45104a0c269fe60a6c..d52417a5c8ba0e99606b7ee754ca43cd5a2b2515 100644 (file)
@@ -26,18 +26,14 @@ Vehicle::Vehicle(Layout &l, const VehicleType &t):
        next(0),
        prev(0),
        direction(0),
-       bogie_dirs(type.get_bogies().size()),
-       axle_angles(1),
-       rods(type.get_rods().size()),
        front_sensor(0),
        back_sensor(0)
 {
        layout.add_vehicle(*this);
 
-       axle_angles.front().resize(type.get_axles().size(), 0.0f);
-       const vector<VehicleType::Bogie> &bogies = type.get_bogies();
-       for(vector<VehicleType::Bogie>::const_iterator i=bogies.begin(); i!=bogies.end(); ++i)
-               axle_angles.push_back(vector<float>(i->axles.size(), 0.0f));
+       axles.assign(type.get_axles().begin(), type.get_axles().end());
+       bogies.assign(type.get_bogies().begin(), type.get_bogies().end());
+       rods.assign(type.get_rods().begin(), type.get_rods().end());
 }
 
 Vehicle::~Vehicle()
@@ -129,75 +125,66 @@ void Vehicle::advance(float d)
        propagate_position();
 }
 
-float Vehicle::get_bogie_direction(unsigned i) const
+const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
 {
-       if(i>=bogie_dirs.size())
+       if(i>=bogies.size())
                throw InvalidParameterValue("Bogie index out of range");
-       return bogie_dirs[i];
+       return bogies[i];
 }
 
-float Vehicle::get_axle_angle(unsigned i) const
+const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
 {
-       if(i>=axle_angles[0].size())
+       if(i>=axles.size())
                throw InvalidParameterValue("Axle index out of range");
-       return axle_angles[0][i];
+       return axles[i];
 }
 
-float Vehicle::get_bogie_axle_angle(unsigned i, unsigned j) const
+const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
 {
-       if(i+1>=axle_angles.size())
+       if(i>=bogies.size())
                throw InvalidParameterValue("Bogie index out of range");
-       if(j>=axle_angles[i+1].size())
+       if(j>=bogies[i].axles.size())
                throw InvalidParameterValue("Axle index out of range");
-       return axle_angles[i+1][j];
+       return bogies[i].axles[j];
 }
 
-const Point &Vehicle::get_rod_position(unsigned i) const
+const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
 {
        if(i>=rods.size())
                throw InvalidParameterValue("Rod index out of range");
-       return rods[i].position;
-}
-
-float Vehicle::get_rod_angle(unsigned i) const
-{
-       if(i>=rods.size())
-               throw InvalidParameterValue("Rod index out of range");
-       return rods[i].angle;
+       return rods[i];
 }
 
 void Vehicle::update_position()
 {
        TrackPoint tp;
 
-       const vector<VehicleType::Axle> &axles = type.get_axles();
-       const vector<VehicleType::Bogie> &bogies = type.get_bogies();
        if(axles.size()>=2)
        {
-               float wheelbase = axles.front().position-axles.back().position;
-               tp = get_point(track_pos, wheelbase, -axles.back().position/wheelbase);
+               float wheelbase = axles.front().type->position-axles.back().type->position;
+               tp = get_point(track_pos, wheelbase, -axles.back().type->position/wheelbase);
        }
        else if(bogies.size()>=2)
        {
                TrackPosition front = track_pos;
-               front.advance(bogies.front().position);
+               front.advance(bogies.front().type->position);
                TrackPosition back = track_pos;
-               back.advance(bogies.back().position);
-               float bogie_spacing = bogies.front().position-bogies.back().position;
+               back.advance(bogies.back().type->position);
+               float bogie_spacing = bogies.front().type->position-bogies.back().type->position;
                adjust_for_distance(front, back, bogie_spacing);
 
-               const vector<VehicleType::Axle> &front_axles = bogies.front().axles;
-               float wheelbase = front_axles.front().position-front_axles.back().position;
-               TrackPoint front_point = get_point(front, wheelbase, -front_axles.back().position/wheelbase);
+               const vector<Axle> &front_axles = bogies.front().axles;
+               float wheelbase = front_axles.front().type->position-front_axles.back().type->position;
+               TrackPoint front_point = get_point(front, wheelbase, -front_axles.back().type->position/wheelbase);
 
-               const vector<VehicleType::Axle> &back_axles = bogies.back().axles;
-               wheelbase = back_axles.front().position-back_axles.back().position;
-               TrackPoint back_point = get_point(back, wheelbase, -back_axles.back().position/wheelbase);
+               const vector<Axle> &back_axles = bogies.back().axles;
+               wheelbase = back_axles.front().type->position-back_axles.back().type->position;
+               TrackPoint back_point = get_point(back, wheelbase, -back_axles.back().type->position/wheelbase);
 
-               tp = get_point(front_point.pos, back_point.pos, -bogies.back().position/bogie_spacing);
+               tp = get_point(front_point.pos, back_point.pos, -bogies.back().type->position/bogie_spacing);
 
-               bogie_dirs.front() = front_point.dir-tp.dir;
-               bogie_dirs.back() = back_point.dir-tp.dir;
+               bogies.front().direction = front_point.dir-tp.dir;
+               bogies.back().direction = back_point.dir-tp.dir;
        }
        else
                tp = track_pos.get_point();
@@ -286,74 +273,68 @@ void Vehicle::check_sensor(float offset, unsigned &sensor)
 
 void Vehicle::turn_axles(float d)
 {
-       const vector<VehicleType::Axle> &axles = type.get_axles();
-       const vector<VehicleType::Bogie> &bogies = type.get_bogies();
-       for(unsigned i=0; i<axle_angles.size(); ++i)
-               for(unsigned j=0; j<axle_angles[i].size(); ++j)
-               {
-                       const VehicleType::Axle &axle = (i==0 ? axles[j] : bogies[i-1].axles[j]);
-                       axle_angles[i][j] += d*2/axle.wheel_dia;
-               }
+       for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
+               i->angle += 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;
 
        update_rods();
 }
 
 void Vehicle::update_rods()
 {
-       const vector<VehicleType::Rod> &trods = type.get_rods();
-       for(unsigned i=0; i<trods.size(); ++i)
+       for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
        {
-               const VehicleType::Rod &rod = trods[i];
-               if(rod.pivot==VehicleType::Rod::BODY)
-                       rods[i].position = rod.pivot_point;
-               else if(rod.pivot==VehicleType::Rod::AXLE)
+               if(i->type->pivot==VehicleType::Rod::BODY)
+                       i->position = i->type->pivot_point;
+               else if(i->type->pivot==VehicleType::Rod::AXLE)
                {
-                       const VehicleType::Axle &axle = type.get_axles()[rod.pivot_index];
-                       float angle = axle_angles[0][rod.pivot_index];
-                       float c = cos(angle);
-                       float s = sin(angle);
-                       const Point &pp = rod.pivot_point;
-                       rods[i].position = Point(axle.position+pp.x*c+pp.z*s, pp.y, axle.wheel_dia/2+pp.z*c-pp.x*s);
+                       const Axle &axle = get_axle(i->type->pivot_index);
+                       float c = cos(axle.angle);
+                       float s = sin(axle.angle);
+                       const Point &pp = i->type->pivot_point;
+                       i->position = Point(axle.type->position+pp.x*c+pp.z*s, pp.y, axle.type->wheel_dia/2+pp.z*c-pp.x*s);
                }
-               else if(rod.pivot==VehicleType::Rod::ROD)
+               else if(i->type->pivot==VehicleType::Rod::ROD)
                {
-                       float angle = rods[rod.pivot_index].angle;
-                       float c = cos(angle);
-                       float s = sin(angle);
-                       const Point &pos = rods[rod.pivot_index].position;
-                       const Point &off = rod.pivot_point;
-                       rods[i].position = Point(pos.x+off.x*c-off.z*s, pos.y+off.y, pos.z+off.z*c+off.x*s);
+                       const Rod &prod = get_rod(i->type->pivot_index);
+                       float c = cos(prod.angle);
+                       float s = sin(prod.angle);
+                       const Point &pos = prod.position;
+                       const Point &off = i->type->pivot_point;
+                       i->position = Point(pos.x+off.x*c-off.z*s, pos.y+off.y, pos.z+off.z*c+off.x*s);
                }
 
-               if(rod.connect_index>=0)
+               if(i->type->connect_index>=0)
                {
-                       const VehicleType::Rod &crod = trods[rod.connect_index];
-                       if(rod.limit==VehicleType::Rod::ROTATE && crod.limit==VehicleType::Rod::SLIDE_X)
+                       Rod &crod = rods[i->type->connect_index];
+                       if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::SLIDE_X)
                        {
-                               float dx = (rods[rod.connect_index].position.x+rod.connect_offset.x)-rods[i].position.x;
-                               float dz = (rods[rod.connect_index].position.z+rod.connect_offset.z)-rods[i].position.z;
-                               float cd = sqrt(rod.connect_point.x*rod.connect_point.x+rod.connect_point.z*rod.connect_point.z);
-                               float ca = atan2(rod.connect_point.z, rod.connect_point.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);
-                               rods[i].angle = atan2(dz, dx)-ca;
-                               rods[rod.connect_index].position.x = rods[i].position.x+dx-rod.connect_offset.x;
+                               i->angle = atan2(dz, dx)-ca;
+                               crod.position.x = i->position.x+dx-i->type->connect_offset.x;
                        }
-                       else if(rod.limit==VehicleType::Rod::ROTATE && crod.limit==VehicleType::Rod::ROTATE)
+                       else if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::ROTATE)
                        {
-                               float dx = rods[rod.connect_index].position.x-rods[i].position.x;
-                               float dz = rods[rod.connect_index].position.z-rods[i].position.z;
+                               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(rod.connect_point.x*rod.connect_point.x+rod.connect_point.z*rod.connect_point.z);
-                               float cd2 = sqrt(rod.connect_offset.x*rod.connect_offset.x+rod.connect_offset.z*rod.connect_offset.z);
+                               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);
                                float a = (d*d+cd1*cd1-cd2*cd2)/(2*d);
                                float b = sqrt(cd1*cd1-a*a);
-                               float sign = (dx*rod.connect_point.z-dz*rod.connect_point.x>0 ? 1 : -1);
+                               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(rod.connect_point.z, rod.connect_point.x);
-                               float ca2 = atan2(rod.connect_offset.z, rod.connect_offset.x);
-                               rods[i].angle = atan2(cz, cx)-ca1;
-                               rods[rod.connect_index].angle = atan2(cz-dz, cx-dx)-ca2;
+                               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;
                        }
                }
        }
@@ -418,6 +399,27 @@ TrackPoint Vehicle::get_point(const TrackPosition &pos, float tdist, float ratio
 }
 
 
+Vehicle::Axle::Axle(const VehicleType::Axle &t):
+       type(&t),
+       angle(0)
+{ }
+
+
+Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
+       type(&t),
+       direction(0)
+{
+       for(VehicleType::AxleArray::const_iterator i=type->axles.begin(); i!=type->axles.end(); ++i)
+               axles.push_back(*i);
+}
+
+
+Vehicle::Rod::Rod(const VehicleType::Rod &t):
+       type(&t),
+       angle(0)
+{ }
+
+
 Vehicle::TrackPosition::TrackPosition():
        track(0),
        ep(0),
@@ -475,9 +477,4 @@ TrackPoint Vehicle::TrackPosition::get_point() const
                return TrackPoint();
 }
 
-
-Vehicle::Rod::Rod():
-       angle(0)
-{ }
-
 } // namespace R2C2
index 2e91962842f2d319153ea4e8a2592f9f7af2275b..1f118cc579b15c76a90b9eb86c686e7f82d5c0a9 100644 (file)
@@ -9,12 +9,12 @@ Distributed under the GPL
 #define LIBR2C2_VEHICLE_H_
 
 #include "geometry.h"
+#include "vehicletype.h"
 
 namespace R2C2 {
 
 class Layout;
 class Track;
-class VehicleType;
 
 class Vehicle
 {
@@ -28,6 +28,32 @@ public:
                BACK_BUFFER
        };
 
+       struct Axle
+       {
+               const VehicleType::Axle *type;
+               float angle;
+
+               Axle(const VehicleType::Axle &);
+       };
+
+       struct Bogie
+       {
+               const VehicleType::Bogie *type;
+               float direction;
+               std::vector<Axle> axles;
+
+               Bogie(const VehicleType::Bogie &);
+       };
+
+       struct Rod
+       {
+               const VehicleType::Rod *type;
+               Point position;
+               float angle;
+
+               Rod(const VehicleType::Rod &);
+       };
+
 private:
        struct TrackPosition
        {
@@ -41,14 +67,6 @@ private:
                TrackPoint get_point() const;
        };
 
-       struct Rod
-       {
-               Point position;
-               float angle;
-
-               Rod();
-       };
-
        Layout &layout;
        const VehicleType &type;
        Vehicle *next;
@@ -56,8 +74,8 @@ private:
        TrackPosition track_pos;
        Point position;
        float direction;
-       std::vector<float> bogie_dirs;
-       std::vector<std::vector<float> > axle_angles;
+       std::vector<Axle> axles;
+       std::vector<Bogie> bogies;
        std::vector<Rod> rods;
        unsigned front_sensor;
        unsigned back_sensor;
@@ -83,11 +101,10 @@ public:
        float get_offset() const { return track_pos.offs; }
        const Point &get_position() const { return position; }
        float get_direction() const { return direction; }
-       float get_axle_angle(unsigned) const;
-       float get_bogie_direction(unsigned) const;
-       float get_bogie_axle_angle(unsigned, unsigned) const;
-       const Point &get_rod_position(unsigned) const;
-       float get_rod_angle(unsigned) const;
+       const Axle &get_axle(unsigned) const;
+       const Bogie &get_bogie(unsigned) const;
+       const Axle &get_bogie_axle(unsigned, unsigned) const;
+       const Rod &get_rod(unsigned) const;
 private:
        void update_position();
        void update_position_from(const Vehicle &);
index b9c6ed2f72fe093c37043d3504bc226b225abe84..4f640193d025f433a86c81a2d5813c112200890d 100644 (file)
@@ -27,6 +27,36 @@ unsigned VehicleType::get_max_function() const
        return (--functions.end())->first;
 }
 
+const VehicleType::Axle &VehicleType::get_axle(unsigned i) const
+{
+       if(i>=axles.size())
+               throw InvalidParameterValue("Axle index out of range");
+       return axles[i];
+}
+
+const VehicleType::Bogie &VehicleType::get_bogie(unsigned i) const
+{
+       if(i>=bogies.size())
+               throw InvalidParameterValue("Axle index out of range");
+       return bogies[i];
+}
+
+const VehicleType::Axle &VehicleType::get_bogie_axle(unsigned i, unsigned j) const
+{
+       if(i>=bogies.size())
+               throw InvalidParameterValue("Axle index out of range");
+       if(j>=bogies[i].axles.size())
+               throw InvalidParameterValue("Axle index out of range");
+       return bogies[i].axles[j];
+}
+
+const VehicleType::Rod &VehicleType::get_rod(unsigned i) const
+{
+       if(i>=rods.size())
+               throw InvalidParameterValue("Rod index out of range");
+       return rods[i];
+}
+
 float VehicleType::get_front_axle_offset() const
 {
        float front = length/2;
index c0fe0ea5136f3c56bf7a94a6089fe2cc7db64a88..a12d3f4f1ea1b4cc513cad45ecbcbfe64cb9e586 100644 (file)
@@ -53,6 +53,8 @@ public:
                Axle();
        };
 
+       typedef std::vector<Axle> AxleArray;
+
        struct Bogie
        {
                class Loader: public Msp::DataFile::ObjectLoader<Bogie>
@@ -65,13 +67,15 @@ public:
                };
 
                float position;
-               std::vector<Axle> axles;
+               AxleArray axles;
                std::string object;
                bool rotate_object;
 
                Bogie();
        };
 
+       typedef std::vector<Bogie> BogieArray;
+
        struct Rod
        {
                enum Anchor
@@ -123,6 +127,8 @@ public:
                Rod();
        };
 
+       typedef std::vector<Rod> RodArray;
+
 private:
        ArticleNumber art_nr;
        std::string name;
@@ -131,9 +137,9 @@ private:
        float length;
        float width;
        float height;
-       std::vector<Axle> axles;
-       std::vector<Bogie> bogies;
-       std::vector<Rod> rods;
+       AxleArray axles;
+       BogieArray bogies;
+       RodArray rods;
        std::string object;
 
 public:
@@ -147,9 +153,13 @@ public:
        float get_length() const { return length; }
        float get_width() const { return width; }
        float get_height() const { return height; }
-       const std::vector<Axle> &get_axles() const { return axles; }
-       const std::vector<Bogie> &get_bogies() const { return bogies; }
-       const std::vector<Rod> &get_rods() const { return rods; }
+       const AxleArray &get_axles() const { return axles; }
+       const Axle &get_axle(unsigned) const;
+       const BogieArray &get_bogies() const { return bogies; }
+       const Bogie &get_bogie(unsigned) const;
+       const Axle &get_bogie_axle(unsigned, unsigned) const;
+       const RodArray &get_rods() const { return rods; }
+       const Rod &get_rod(unsigned) const;
        float get_front_axle_offset() const;
        float get_back_axle_offset() const;
        const std::string &get_object() const { return object; }