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];
50 Vehicle *Vehicle::clone(Layout *to_layout) const
52 Vehicle *veh = new Vehicle((to_layout ? *to_layout : layout), type);
53 veh->set_position(position);
54 veh->set_rotation(rotation);
58 void Vehicle::set_train(Train *t)
63 void Vehicle::attach_back(Vehicle &veh)
66 throw attachment_error("already attached");
75 void Vehicle::attach_front(Vehicle &veh)
78 throw attachment_error("already attached");
84 prev->propagate_backward();
87 void Vehicle::detach_back()
90 throw attachment_error("not attached");
96 void Vehicle::detach_front()
99 throw attachment_error("not attached");
105 void Vehicle::place(const TrackOffsetIter &t, VehiclePlacement::Anchor a)
108 throw invalid_argument("Vehicle::place");
110 placement.place(t, a);
113 propagate_position();
116 void Vehicle::unplace()
118 if(!placement.is_placed())
129 void Vehicle::advance(float d)
131 placement.advance(d);
133 update_position(d<0 ? -1 : 1);
134 propagate_position();
137 const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
140 throw out_of_range("Vehicle::get_axle");
144 const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const
146 if(i>=fixed_axles.size())
147 throw out_of_range("Vehicle::get_fixed_axle");
148 return *fixed_axles[i];
151 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
154 throw out_of_range("Vehicle::get_bogie");
158 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
161 throw out_of_range("Vehicle::get_bogie_axle");
162 if(j>=bogies[i].axles.size())
163 throw out_of_range("Vehicle::get_bogie_axle");
164 return *bogies[i].axles[j];
167 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
170 throw out_of_range("Vehicle::get_rod");
174 void Vehicle::update_position(int sign)
176 OrientedPoint p = placement.get_point();
177 position = p.position;
178 // TODO Move the z adjustment to VehiclePlacement
179 position.z += placement.get_position(VehiclePlacement::FRONT_AXLE)->get_type().get_appearance().get_rail_elevation();
180 rotation = p.rotation;
185 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
186 bogies.front().direction = front_point.rotation-p.rotation;
188 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
189 bogies.back().direction = back_point.rotation-p.rotation;
193 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
195 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
200 void Vehicle::update_position_from(const Vehicle &veh)
202 int sign = (&veh==prev ? -1 : 1);
204 float tdist = (type.get_length()+veh.type.get_length())/2;
205 float margin = layout.get_catalogue().get_scale();
207 float dist = distance(veh.position, position);
208 if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
211 placement.place_after(veh.placement);
213 placement.place_before(veh.placement);
216 dist = distance(veh.position, position);
219 float d = sign*(tdist-dist);
220 placement.advance(d);
221 update_position(d<0 ? -1 : 1);
225 void Vehicle::propagate_position()
230 propagate_backward();
233 void Vehicle::propagate_forward()
235 prev->update_position_from(*this);
238 prev->propagate_forward();
241 void Vehicle::propagate_backward()
243 next->update_position_from(*this);
246 next->propagate_backward();
249 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
251 unsigned s = t->get_sensor_address();
254 unsigned old = sensor;
257 layout.get_driver().set_sensor(old, false);
259 layout.get_driver().set_sensor(sensor, true);
263 void Vehicle::turn_axles(float d)
265 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
266 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
271 void Vehicle::update_rods()
276 for(unsigned n=0; n<10; ++n)
279 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
281 const vector<VehicleType::RodConstraint> &constraints = i->type->constraints;
282 for(vector<VehicleType::RodConstraint>::const_iterator j=constraints.begin(); j!=constraints.end(); ++j)
284 float d = resolve_rod_constraint(*i, *j);
285 max_d = max(d, max_d);
294 float Vehicle::resolve_rod_constraint(Rod &rod, const VehicleType::RodConstraint &cns)
297 if(cns.target==VehicleType::RodConstraint::BODY)
298 target = cns.target_position;
299 else if(cns.target==VehicleType::RodConstraint::BOGIE)
300 ; // TODO currently rods must lie in the xz plane of the body
301 else if(cns.target==VehicleType::RodConstraint::AXLE)
303 const Axle &axle = get_axle(cns.target_index);
304 target = Vector(axle.type->position, 0, axle.type->wheel_dia/2);
305 target += Transform::rotation(axle.angle, Vector(0, 1, 0)).transform(cns.target_position);
307 else if(cns.target==VehicleType::RodConstraint::ROD)
309 const Rod &trod = get_rod(cns.target_index);
310 target = trod.position;
311 target += Transform::rotation(trod.angle, Vector(0, -1, 0)).transform(cns.target_position);
314 Vector old_position = rod.position;
315 if(cns.type==VehicleType::RodConstraint::MOVE)
316 rod.position = target-Transform::rotation(rod.angle, Vector(0, -1, 0)).transform(cns.local_position);
317 else if(cns.type==VehicleType::RodConstraint::SLIDE)
319 Vector d = rod.position-target;
320 rod.position = target+cns.axis*dot(d, cns.axis);
321 rod.angle = Angle::zero();
323 else if(cns.type==VehicleType::RodConstraint::ROTATE)
325 Angle old_angle = rod.angle;
326 Vector d = target-rod.position;
327 rod.angle = Geometry::atan2<float>(d.z, d.x);
328 if(cns.local_position.x || cns.local_position.z)
329 rod.angle -= Geometry::atan2<float>(cns.local_position.z, cns.local_position.x);
330 return abs(rod.angle-old_angle).radians()*cns.local_position.norm();
333 return distance(old_position, rod.position);
336 unsigned Vehicle::get_n_link_slots() const
341 Vehicle *Vehicle::get_link(unsigned i) const
344 throw out_of_range("Vehicle::get_link");
346 return (i==0 ? prev : next);
349 int Vehicle::get_link_slot(const Object &other) const
353 else if(&other==next)
359 bool Vehicle::collide_ray(const Ray &ray) const
362 return Object::collide_ray(ray);
368 Vehicle::Axle::Axle(const VehicleType::Axle &t):
373 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
375 axles(t.axles.size())
379 Vehicle::Rod::Rod(const VehicleType::Rod &t):
381 position(t.initial_position)