9 #include "vehicletype.h"
16 Vehicle::Vehicle(Layout &l, const VehicleType &t):
26 axles.assign(type.get_axles().begin(), type.get_axles().end());
27 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
29 fixed_axles.push_back(&*i);
30 bogies.assign(type.get_bogies().begin(), type.get_bogies().end());
31 rods.assign(type.get_rods().begin(), type.get_rods().end());
32 for(vector<Bogie>::iterator i=bogies.begin(); i!=bogies.end(); ++i)
33 for(unsigned j=0; j<i->axles.size(); ++j)
34 i->axles[j] = &axles[i->type->first_axle+j];
48 Vehicle *Vehicle::clone(Layout *to_layout) const
50 Vehicle *veh = new Vehicle((to_layout ? *to_layout : layout), type);
51 veh->set_position(position);
52 veh->set_rotation(rotation);
56 void Vehicle::set_train(Train *t)
61 void Vehicle::attach_back(Vehicle &veh)
64 throw attachment_error("already attached");
73 void Vehicle::attach_front(Vehicle &veh)
76 throw attachment_error("already attached");
82 prev->propagate_backward();
85 void Vehicle::detach_back()
88 throw attachment_error("not attached");
94 void Vehicle::detach_front()
97 throw attachment_error("not attached");
103 void Vehicle::place(const TrackOffsetIter &t, VehiclePlacement::Anchor a)
106 throw invalid_argument("Vehicle::place");
108 placement.place(t, a);
111 propagate_position();
114 void Vehicle::unplace()
116 if(!placement.is_placed())
127 void Vehicle::advance(float d)
129 placement.advance(d);
131 update_position(d<0 ? -1 : 1);
132 propagate_position();
135 const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
138 throw out_of_range("Vehicle::get_axle");
142 const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const
144 if(i>=fixed_axles.size())
145 throw out_of_range("Vehicle::get_fixed_axle");
146 return *fixed_axles[i];
149 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
152 throw out_of_range("Vehicle::get_bogie");
156 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
159 throw out_of_range("Vehicle::get_bogie_axle");
160 if(j>=bogies[i].axles.size())
161 throw out_of_range("Vehicle::get_bogie_axle");
162 return *bogies[i].axles[j];
165 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
168 throw out_of_range("Vehicle::get_rod");
172 void Vehicle::update_position(int sign)
174 OrientedPoint p = placement.get_point();
175 position = p.position;
176 position.z += layout.get_catalogue().get_rail_elevation();
177 rotation = p.rotation;
182 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
183 bogies.front().direction = front_point.rotation-p.rotation;
185 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
186 bogies.back().direction = back_point.rotation-p.rotation;
190 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
192 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
197 void Vehicle::update_position_from(const Vehicle &veh)
199 int sign = (&veh==prev ? -1 : 1);
201 float tdist = (type.get_length()+veh.type.get_length())/2;
202 float margin = layout.get_catalogue().get_scale();
204 float dist = distance(veh.position, position);
205 if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
208 placement.place_after(veh.placement);
210 placement.place_before(veh.placement);
213 dist = distance(veh.position, position);
216 float d = sign*(tdist-dist);
217 placement.advance(d);
218 update_position(d<0 ? -1 : 1);
222 void Vehicle::propagate_position()
227 propagate_backward();
230 void Vehicle::propagate_forward()
232 prev->update_position_from(*this);
235 prev->propagate_forward();
238 void Vehicle::propagate_backward()
240 next->update_position_from(*this);
243 next->propagate_backward();
246 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
248 unsigned s = t->get_sensor_id();
251 unsigned old = sensor;
254 layout.get_driver().set_sensor(old, false);
256 layout.get_driver().set_sensor(sensor, true);
260 void Vehicle::turn_axles(float d)
262 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
263 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
268 void Vehicle::update_rods()
273 for(unsigned n=0; n<10; ++n)
276 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
278 const vector<VehicleType::RodConstraint> &constraints = i->type->constraints;
279 for(vector<VehicleType::RodConstraint>::const_iterator j=constraints.begin(); j!=constraints.end(); ++j)
281 float d = resolve_rod_constraint(*i, *j);
282 max_d = max(d, max_d);
291 float Vehicle::resolve_rod_constraint(Rod &rod, const VehicleType::RodConstraint &cns)
294 if(cns.target==VehicleType::RodConstraint::BODY)
295 target = cns.target_position;
296 else if(cns.target==VehicleType::RodConstraint::BOGIE)
297 ; // TODO currently rods must lie in the xz plane of the body
298 else if(cns.target==VehicleType::RodConstraint::AXLE)
300 const Axle &axle = get_axle(cns.target_index);
301 target = Vector(axle.type->position, 0, axle.type->wheel_dia/2);
302 target += Transform::rotation(axle.angle, Vector(0, 1, 0)).transform(cns.target_position);
304 else if(cns.target==VehicleType::RodConstraint::ROD)
306 const Rod &trod = get_rod(cns.target_index);
307 target = trod.position;
308 target += Transform::rotation(trod.angle, Vector(0, -1, 0)).transform(cns.target_position);
311 Vector old_position = rod.position;
312 if(cns.type==VehicleType::RodConstraint::MOVE)
313 rod.position = target-Transform::rotation(rod.angle, Vector(0, -1, 0)).transform(cns.local_position);
314 else if(cns.type==VehicleType::RodConstraint::SLIDE)
316 Vector d = rod.position-target;
317 rod.position = target+cns.axis*dot(d, cns.axis);
318 rod.angle = Angle::zero();
320 else if(cns.type==VehicleType::RodConstraint::ROTATE)
322 Angle old_angle = rod.angle;
323 Vector d = target-rod.position;
324 rod.angle = Geometry::atan2<float>(d.z, d.x);
325 if(cns.local_position.x || cns.local_position.z)
326 rod.angle -= Geometry::atan2<float>(cns.local_position.z, cns.local_position.x);
327 return abs(rod.angle-old_angle).radians()*cns.local_position.norm();
330 return distance(old_position, rod.position);
333 unsigned Vehicle::get_n_link_slots() const
338 Vehicle *Vehicle::get_link(unsigned i) const
341 throw out_of_range("Vehicle::get_link");
343 return (i==0 ? prev : next);
346 int Vehicle::get_link_slot(const Object &other) const
350 else if(&other==next)
357 Vehicle::Axle::Axle(const VehicleType::Axle &t):
362 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
364 axles(t.axles.size())
368 Vehicle::Rod::Rod(const VehicleType::Rod &t):
370 position(t.initial_position)