From: Mikko Rasa Date: Tue, 14 Dec 2010 21:30:16 +0000 (+0000) Subject: Add single get_stuff methods with index to VehicleType X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=ce8be590703fdc0709863f0ee9695f40b6adb200;p=r2c2.git Add single get_stuff methods with index to VehicleType Change how stuff is stored in and given out of Vehicle --- diff --git a/source/3d/vehicle.cpp b/source/3d/vehicle.cpp index 50dbcb0..8027a19 100644 --- a/source/3d/vehicle.cpp +++ b/source/3d/vehicle.cpp @@ -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; jrender(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); diff --git a/source/libr2c2/vehicle.cpp b/source/libr2c2/vehicle.cpp index 578707a..d52417a 100644 --- a/source/libr2c2/vehicle.cpp +++ b/source/libr2c2/vehicle.cpp @@ -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 &bogies = type.get_bogies(); - for(vector::const_iterator i=bogies.begin(); i!=bogies.end(); ++i) - axle_angles.push_back(vector(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 &axles = type.get_axles(); - const vector &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 &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 &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 &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 &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 &axles = type.get_axles(); - const vector &bogies = type.get_bogies(); - for(unsigned i=0; i::iterator i=axles.begin(); i!=axles.end(); ++i) + i->angle += 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 += d*2/j->type->wheel_dia; update_rods(); } void Vehicle::update_rods() { - const vector &trods = type.get_rods(); - for(unsigned i=0; i::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 diff --git a/source/libr2c2/vehicle.h b/source/libr2c2/vehicle.h index 2e91962..1f118cc 100644 --- a/source/libr2c2/vehicle.h +++ b/source/libr2c2/vehicle.h @@ -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 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 bogie_dirs; - std::vector > axle_angles; + std::vector axles; + std::vector bogies; std::vector 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 &); diff --git a/source/libr2c2/vehicletype.cpp b/source/libr2c2/vehicletype.cpp index b9c6ed2..4f64019 100644 --- a/source/libr2c2/vehicletype.cpp +++ b/source/libr2c2/vehicletype.cpp @@ -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; diff --git a/source/libr2c2/vehicletype.h b/source/libr2c2/vehicletype.h index c0fe0ea..a12d3f4 100644 --- a/source/libr2c2/vehicletype.h +++ b/source/libr2c2/vehicletype.h @@ -53,6 +53,8 @@ public: Axle(); }; + typedef std::vector AxleArray; + struct Bogie { class Loader: public Msp::DataFile::ObjectLoader @@ -65,13 +67,15 @@ public: }; float position; - std::vector axles; + AxleArray axles; std::string object; bool rotate_object; Bogie(); }; + typedef std::vector BogieArray; + struct Rod { enum Anchor @@ -123,6 +127,8 @@ public: Rod(); }; + typedef std::vector RodArray; + private: ArticleNumber art_nr; std::string name; @@ -131,9 +137,9 @@ private: float length; float width; float height; - std::vector axles; - std::vector bogies; - std::vector 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 &get_axles() const { return axles; } - const std::vector &get_bogies() const { return bogies; } - const std::vector &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; }