From: Mikko Rasa Date: Thu, 25 Jul 2013 14:48:34 +0000 (+0300) Subject: Store all axles in a single array X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=bc9e941447da48905295192289bc09483a678180;p=r2c2.git Store all axles in a single array --- diff --git a/source/3d/axle.cpp b/source/3d/axle.cpp index 04c7668..4344eea 100644 --- a/source/3d/axle.cpp +++ b/source/3d/axle.cpp @@ -7,16 +7,13 @@ using namespace Msp; namespace R2C2 { Axle3D::Axle3D(const Vehicle3D &v, unsigned a): - VehiclePart3D(v, *v.get_type().get_fixed_axle_object(a)), + VehiclePart3D(v, *v.get_type().get_axle_object(a)), bogie(0), - axle(vehicle.get_vehicle().get_fixed_axle(a)) -{ } - -Axle3D::Axle3D(const Vehicle3D &v, unsigned b, unsigned a): - VehiclePart3D(v, *v.get_type().get_bogie_axle_object(b, a)), - bogie(&vehicle.get_vehicle().get_bogie(b)), - axle(bogie->axles[a]) -{ } + axle(vehicle.get_vehicle().get_axle(a)) +{ + if(axle.type->bogie) + bogie = &vehicle.get_vehicle().get_bogie(axle.type->bogie->index); +} void Axle3D::update_matrix() { @@ -28,7 +25,7 @@ void Axle3D::update_matrix() matrix.rotate(bogie->direction, 0, 0, 1); } - matrix.translate(axle.type->position, 0, axle.type->wheel_dia/2); + matrix.translate(axle.type->local_position, 0, axle.type->wheel_dia/2); matrix.rotate(axle.angle, 0, 1, 0); } diff --git a/source/3d/axle.h b/source/3d/axle.h index 86bb752..6335b9f 100644 --- a/source/3d/axle.h +++ b/source/3d/axle.h @@ -15,7 +15,6 @@ private: public: Axle3D(const Vehicle3D &, unsigned); - Axle3D(const Vehicle3D &, unsigned, unsigned); virtual void update_matrix(); }; diff --git a/source/3d/vehicle.cpp b/source/3d/vehicle.cpp index b7cec44..9d0f3e8 100644 --- a/source/3d/vehicle.cpp +++ b/source/3d/vehicle.cpp @@ -19,23 +19,16 @@ Vehicle3D::Vehicle3D(Layout3D &l, Vehicle &v): vehicle(v), type(layout.get_catalogue().get_vehicle(vehicle.get_type())) { - unsigned n_axles = vehicle.get_type().get_fixed_axles().size(); + unsigned n_axles = vehicle.get_type().get_axles().size(); for(unsigned i=0; i &axles = type.get_fixed_axles(); + const vector &axles = type.get_axles(); for(vector::const_iterator i=axles.begin(); i!=axles.end(); ++i) - axle_objects[0].push_back(get_object(i->object)); + axle_objects.push_back(get_object(i->object)); const vector &bogies = type.get_bogies(); for(vector::const_iterator i=bogies.begin(); i!=bogies.end(); ++i) - { bogie_objects.push_back(get_object(i->object)); - axle_objects.push_back(vector()); - for(vector::const_iterator j=i->axles.begin(); j!=i->axles.end(); ++j) - axle_objects.back().push_back(get_object(j->object)); - } const vector &rods = type.get_rods(); for(vector::const_iterator i=rods.begin(); i!=rods.end(); ++i) @@ -56,11 +50,11 @@ VehicleType3D::~VehicleType3D() delete i->second; } -const GL::Object *VehicleType3D::get_fixed_axle_object(unsigned i) const +const GL::Object *VehicleType3D::get_axle_object(unsigned i) const { - if(i>=axle_objects[0].size()) + if(i>=axle_objects.size()) throw out_of_range("VehicleType3D::get_fixed_axle_object"); - return axle_objects[0][i]; + return axle_objects[i]; } const GL::Object *VehicleType3D::get_bogie_object(unsigned i) const @@ -70,15 +64,6 @@ const GL::Object *VehicleType3D::get_bogie_object(unsigned i) const return bogie_objects[i]; } -const GL::Object *VehicleType3D::get_bogie_axle_object(unsigned i, unsigned j) const -{ - if(i>=bogie_objects.size()) - throw out_of_range("VehicleType3D::get_bogie_axle_object"); - if(j>=axle_objects[i+1].size()) - throw out_of_range("VehicleType3D::get_bogie_axle_object"); - return axle_objects[i+1][j]; -} - const GL::Object *VehicleType3D::get_rod_object(unsigned i) const { if(i>=rod_objects.size()) diff --git a/source/3d/vehicletype.h b/source/3d/vehicletype.h index b779fa4..81a6468 100644 --- a/source/3d/vehicletype.h +++ b/source/3d/vehicletype.h @@ -17,7 +17,7 @@ private: std::map objects; Msp::GL::Object *body_object; std::vector bogie_objects; - std::vector > axle_objects; + std::vector axle_objects; std::vector rod_objects; public: @@ -25,9 +25,8 @@ public: ~VehicleType3D(); const Msp::GL::Object *get_body_object() const { return body_object; } - const Msp::GL::Object *get_fixed_axle_object(unsigned) const; + const Msp::GL::Object *get_axle_object(unsigned) const; const Msp::GL::Object *get_bogie_object(unsigned) const; - const Msp::GL::Object *get_bogie_axle_object(unsigned, unsigned) const; const Msp::GL::Object *get_rod_object(unsigned) const; private: Msp::GL::Object *get_object(const std::string &); diff --git a/source/libr2c2/vehicle.cpp b/source/libr2c2/vehicle.cpp index 17d67b6..96ad879 100644 --- a/source/libr2c2/vehicle.cpp +++ b/source/libr2c2/vehicle.cpp @@ -22,9 +22,15 @@ Vehicle::Vehicle(Layout &l, const VehicleType &t): front_sensor(0), back_sensor(0) { - axles.assign(type.get_fixed_axles().begin(), type.get_fixed_axles().end()); + axles.assign(type.get_axles().begin(), type.get_axles().end()); + for(vector::iterator i=axles.begin(); i!=axles.end(); ++i) + if(!i->type->bogie) + fixed_axles.push_back(&*i); bogies.assign(type.get_bogies().begin(), type.get_bogies().end()); rods.assign(type.get_rods().begin(), type.get_rods().end()); + for(vector::iterator i=bogies.begin(); i!=bogies.end(); ++i) + for(unsigned j=0; jaxles.size(); ++j) + i->axles[j] = &axles[i->type->first_axle+j]; layout.add(*this); } @@ -134,13 +140,20 @@ void Vehicle::advance(float d) propagate_position(); } -const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const +const Vehicle::Axle &Vehicle::get_axle(unsigned i) const { if(i>=axles.size()) - throw out_of_range("Vehicle::get_fixed_axle"); + throw out_of_range("Vehicle::get_axle"); return axles[i]; } +const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const +{ + if(i>=fixed_axles.size()) + throw out_of_range("Vehicle::get_fixed_axle"); + return *fixed_axles[i]; +} + const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const { if(i>=bogies.size()) @@ -154,7 +167,7 @@ const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const throw out_of_range("Vehicle::get_bogie_axle"); if(j>=bogies[i].axles.size()) throw out_of_range("Vehicle::get_bogie_axle"); - return bogies[i].axles[j]; + return *bogies[i].axles[j]; } const Vehicle::Rod &Vehicle::get_rod(unsigned i) const @@ -168,10 +181,10 @@ void Vehicle::update_position() { OrientedPoint p; - if(axles.size()>=2) + if(fixed_axles.size()>=2) { - float wheelbase = axles.front().type->position-axles.back().type->position; - p = get_point(track, wheelbase, -axles.back().type->position/wheelbase); + float wheelbase = fixed_axles.front()->type->position-fixed_axles.back()->type->position; + p = get_point(track, wheelbase, -fixed_axles.back()->type->position/wheelbase); } else if(bogies.size()>=2) { @@ -180,13 +193,13 @@ void Vehicle::update_position() float bogie_spacing = bogies.front().type->position-bogies.back().type->position; adjust_for_distance(front, back, bogie_spacing); - const vector &front_axles = bogies.front().axles; - float wheelbase = front_axles.front().type->position-front_axles.back().type->position; - OrientedPoint front_point = get_point(front, wheelbase, -front_axles.back().type->position/wheelbase); + const vector &front_axles = bogies.front().axles; + float wheelbase = front_axles.front()->type->position-front_axles.back()->type->position; + OrientedPoint front_point = get_point(front, wheelbase, -front_axles.back()->type->position/wheelbase); - const vector &back_axles = bogies.back().axles; - wheelbase = back_axles.front().type->position-back_axles.back().type->position; - OrientedPoint back_point = get_point(back, wheelbase, -back_axles.back().type->position/wheelbase); + const vector &back_axles = bogies.back().axles; + wheelbase = back_axles.front()->type->position-back_axles.back()->type->position; + OrientedPoint back_point = get_point(back, wheelbase, -back_axles.back()->type->position/wheelbase); p = get_point(front_point.position, back_point.position, -bogies.back().type->position/bogie_spacing); @@ -282,9 +295,6 @@ void Vehicle::turn_axles(float d) { for(vector::iterator i=axles.begin(); i!=axles.end(); ++i) i->angle += Angle::from_radians(d*2/i->type->wheel_dia); - for(vector::iterator i=bogies.begin(); i!=bogies.end(); ++i) - for(vector::iterator j=i->axles.begin(); j!=i->axles.end(); ++j) - j->angle += Angle::from_radians(d*2/j->type->wheel_dia); update_rods(); } @@ -423,11 +433,9 @@ Vehicle::Axle::Axle(const VehicleType::Axle &t): Vehicle::Bogie::Bogie(const VehicleType::Bogie &t): - type(&t) -{ - for(VehicleType::AxleArray::const_iterator i=type->axles.begin(); i!=type->axles.end(); ++i) - axles.push_back(*i); -} + type(&t), + axles(t.axles.size()) +{ } Vehicle::Rod::Rod(const VehicleType::Rod &t): diff --git a/source/libr2c2/vehicle.h b/source/libr2c2/vehicle.h index df251c1..1e9454f 100644 --- a/source/libr2c2/vehicle.h +++ b/source/libr2c2/vehicle.h @@ -43,7 +43,7 @@ public: { const VehicleType::Bogie *type; Angle direction; - std::vector axles; + std::vector axles; Bogie(const VehicleType::Bogie &); }; @@ -64,6 +64,7 @@ private: Vehicle *prev; TrackOffsetIter track; std::vector axles; + std::vector fixed_axles; std::vector bogies; std::vector rods; unsigned front_sensor; @@ -96,6 +97,7 @@ public: Track *get_track() const { return track.track(); } unsigned get_entry() const { return track.entry(); } float get_offset() const { return track.offset(); } + const Axle &get_axle(unsigned) const; const Axle &get_fixed_axle(unsigned) const; const Bogie &get_bogie(unsigned) const; const Axle &get_bogie_axle(unsigned, unsigned) const; diff --git a/source/libr2c2/vehicletype.cpp b/source/libr2c2/vehicletype.cpp index 854308c..9d929b3 100644 --- a/source/libr2c2/vehicletype.cpp +++ b/source/libr2c2/vehicletype.cpp @@ -26,13 +26,20 @@ unsigned VehicleType::get_max_function() const return (--functions.end())->first; } -const VehicleType::Axle &VehicleType::get_fixed_axle(unsigned i) const +const VehicleType::Axle &VehicleType::get_axle(unsigned i) const { if(i>=axles.size()) - throw out_of_range("VehicleType::get_fixed_axle"); + throw out_of_range("VehicleType::get_axle"); return axles[i]; } +const VehicleType::Axle &VehicleType::get_fixed_axle(unsigned i) const +{ + if(i>=fixed_axles.size()) + throw out_of_range("VehicleType::get_fixed_axle"); + return *fixed_axles[i]; +} + const VehicleType::Bogie &VehicleType::get_bogie(unsigned i) const { if(i>=bogies.size()) @@ -46,7 +53,7 @@ const VehicleType::Axle &VehicleType::get_bogie_axle(unsigned i, unsigned j) con throw out_of_range("VehicleType::get_bogie_axle"); if(j>=bogies[i].axles.size()) throw out_of_range("VehicleType::get_bogie_axle"); - return bogies[i].axles[j]; + return *bogies[i].axles[j]; } const VehicleType::Rod &VehicleType::get_rod(unsigned i) const @@ -58,32 +65,23 @@ const VehicleType::Rod &VehicleType::get_rod(unsigned i) const float VehicleType::get_front_axle_offset() const { - float front = length/2; if(!axles.empty()) - front = axles.front().position; - if(!bogies.empty()) - { - const Bogie &bogie = bogies.front(); - front = max(front, bogie.position+bogie.axles.front().position); - } - return front; + return axles.front().position; + return length/2; } float VehicleType::get_back_axle_offset() const { - float back = -length/2; if(!axles.empty()) - back = axles.back().position; - if(!bogies.empty()) - { - const Bogie &bogie = bogies.back(); - back = min(back, bogie.position+bogie.axles.back().position); - } - return back; + return axles.back().position; + return -length/2; } VehicleType::Axle::Axle(): + index(0), + bogie(0), + local_position(0), position(0), wheel_dia(0), powered(false) @@ -124,6 +122,24 @@ VehicleType::Loader::Loader(VehicleType &vt): void VehicleType::Loader::finish() { + for(unsigned i=0; ibogie = &obj.bogies[i]; + obj.bogies[i].axles[j]->position += obj.bogies[i].position; + } + } + + for(unsigned i=0; i( Geometry::Box(obj.length, obj.width, obj.height), Transform::translation(Vector(0, 0, obj.height/2))); @@ -139,7 +155,8 @@ void VehicleType::Loader::axle() void VehicleType::Loader::bogie() { Bogie bog; - load_sub(bog); + Bogie::Loader ldr(obj, bog); + load_sub_with(ldr); obj.bogies.push_back(bog); } @@ -186,7 +203,8 @@ VehicleType::Axle::Loader::Loader(Axle &a): void VehicleType::Axle::Loader::position(float p) { - obj.position = p/1000; + obj.local_position = p/1000; + obj.position = obj.local_position; } void VehicleType::Axle::Loader::wheel_diameter(float d) @@ -195,8 +213,9 @@ void VehicleType::Axle::Loader::wheel_diameter(float d) } -VehicleType::Bogie::Loader::Loader(Bogie &b): - DataFile::ObjectLoader(b) +VehicleType::Bogie::Loader::Loader(VehicleType &t, Bogie &b): + DataFile::ObjectLoader(b), + parent(t) { add("axle", &Loader::axle); add("object", &Bogie::object); @@ -208,7 +227,11 @@ void VehicleType::Bogie::Loader::axle() { Axle axl; load_sub(axl); - obj.axles.push_back(axl); + if(obj.axles.empty()) + obj.first_axle = parent.axles.size(); + parent.axles.push_back(axl); + // Actual pointers will be filled after everything is loaded + obj.axles.push_back(0); } void VehicleType::Bogie::Loader::position(float p) diff --git a/source/libr2c2/vehicletype.h b/source/libr2c2/vehicletype.h index 145a08d..31b6992 100644 --- a/source/libr2c2/vehicletype.h +++ b/source/libr2c2/vehicletype.h @@ -28,6 +28,8 @@ public: void width(float); }; + struct Bogie; + struct Axle { class Loader: public Msp::DataFile::ObjectLoader @@ -39,6 +41,9 @@ public: void wheel_diameter(float); }; + unsigned index; + Bogie *bogie; + float local_position; float position; float wheel_dia; bool powered; @@ -47,29 +52,30 @@ public: Axle(); }; - typedef std::vector AxleArray; - struct Bogie { class Loader: public Msp::DataFile::ObjectLoader { + private: + VehicleType &parent; + public: - Loader(Bogie &); + Loader(VehicleType &, Bogie &); private: void axle(); void position(float); }; + unsigned index; float position; - AxleArray axles; + unsigned first_axle; + std::vector axles; std::string object; bool rotate_object; Bogie(); }; - typedef std::vector BogieArray; - struct Rod { enum Anchor @@ -121,8 +127,9 @@ public: Rod(); }; + typedef std::vector AxleArray; + typedef std::vector BogieArray; typedef std::vector RodArray; - typedef std::map FunctionMap; private: @@ -133,6 +140,7 @@ private: float width; float height; AxleArray axles; + std::vector fixed_axles; BogieArray bogies; RodArray rods; std::string object; @@ -148,7 +156,8 @@ public: float get_length() const { return length; } float get_width() const { return width; } float get_height() const { return height; } - const AxleArray &get_fixed_axles() const { return axles; } + const AxleArray &get_axles() const { return axles; } + const Axle &get_axle(unsigned) const; const Axle &get_fixed_axle(unsigned) const; const BogieArray &get_bogies() const { return bogies; } const Bogie &get_bogie(unsigned) const;