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()
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();
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;
}
}
}
}
+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),
return TrackPoint();
}
-
-Vehicle::Rod::Rod():
- angle(0)
-{ }
-
} // namespace R2C2