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");
109 float gauge_ratio = t->get_type().get_gauge()/type.get_gauge();
110 if(gauge_ratio<0.99 || gauge_ratio>1.01)
111 throw logic_error("Incompatible gauge");
113 placement.place(t, a);
116 propagate_position();
119 void Vehicle::unplace()
121 if(!placement.is_placed())
132 void Vehicle::advance(float d)
134 placement.advance(d);
136 update_position(d<0 ? -1 : 1);
137 propagate_position();
140 const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
143 throw out_of_range("Vehicle::get_axle");
147 const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const
149 if(i>=fixed_axles.size())
150 throw out_of_range("Vehicle::get_fixed_axle");
151 return *fixed_axles[i];
154 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
157 throw out_of_range("Vehicle::get_bogie");
161 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
164 throw out_of_range("Vehicle::get_bogie_axle");
165 if(j>=bogies[i].axles.size())
166 throw out_of_range("Vehicle::get_bogie_axle");
167 return *bogies[i].axles[j];
170 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
173 throw out_of_range("Vehicle::get_rod");
177 void Vehicle::update_position(int sign)
179 OrientedPoint p = placement.get_point();
180 position = p.position;
181 // TODO Move the z adjustment to VehiclePlacement
182 position.z += placement.get_position(VehiclePlacement::FRONT_AXLE)->get_type().get_appearance().get_rail_elevation();
183 rotation = p.rotation;
188 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
189 bogies.front().direction = front_point.rotation-p.rotation;
191 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
192 bogies.back().direction = back_point.rotation-p.rotation;
196 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
198 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
203 void Vehicle::update_position_from(const Vehicle &veh)
205 int sign = (&veh==prev ? -1 : 1);
207 float tdist = (type.get_length()+veh.type.get_length())/2;
208 float margin = layout.get_catalogue().get_scale();
210 float dist = distance(veh.position, position);
211 if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
214 placement.place_after(veh.placement);
216 placement.place_before(veh.placement);
219 dist = distance(veh.position, position);
222 float d = sign*(tdist-dist);
223 placement.advance(d);
224 update_position(d<0 ? -1 : 1);
228 void Vehicle::propagate_position()
233 propagate_backward();
236 void Vehicle::propagate_forward()
238 prev->update_position_from(*this);
241 prev->propagate_forward();
244 void Vehicle::propagate_backward()
246 next->update_position_from(*this);
249 next->propagate_backward();
252 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
254 unsigned s = t->get_sensor_address();
257 unsigned old = sensor;
260 layout.get_driver().set_sensor(old, false);
262 layout.get_driver().set_sensor(sensor, true);
266 void Vehicle::turn_axles(float d)
268 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
269 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
274 void Vehicle::update_rods()
279 for(unsigned n=0; n<10; ++n)
282 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
284 const vector<VehicleType::RodConstraint> &constraints = i->type->constraints;
285 for(vector<VehicleType::RodConstraint>::const_iterator j=constraints.begin(); j!=constraints.end(); ++j)
287 float d = resolve_rod_constraint(*i, *j);
288 max_d = max(d, max_d);
297 float Vehicle::resolve_rod_constraint(Rod &rod, const VehicleType::RodConstraint &cns)
300 if(cns.target==VehicleType::RodConstraint::BODY)
301 target = cns.target_position;
302 else if(cns.target==VehicleType::RodConstraint::BOGIE)
303 ; // TODO currently rods must lie in the xz plane of the body
304 else if(cns.target==VehicleType::RodConstraint::AXLE)
306 const Axle &axle = get_axle(cns.target_index);
307 target = Vector(axle.type->position, 0, axle.type->wheel_dia/2);
308 target += Transform::rotation(axle.angle, Vector(0, 1, 0)).transform(cns.target_position);
310 else if(cns.target==VehicleType::RodConstraint::ROD)
312 const Rod &trod = get_rod(cns.target_index);
313 target = trod.position;
314 target += Transform::rotation(trod.angle, Vector(0, -1, 0)).transform(cns.target_position);
317 Vector old_position = rod.position;
318 if(cns.type==VehicleType::RodConstraint::MOVE)
319 rod.position = target-Transform::rotation(rod.angle, Vector(0, -1, 0)).transform(cns.local_position);
320 else if(cns.type==VehicleType::RodConstraint::SLIDE)
322 Vector d = rod.position-target;
323 rod.position = target+cns.axis*dot(d, cns.axis);
324 rod.angle = Angle::zero();
326 else if(cns.type==VehicleType::RodConstraint::ROTATE)
328 Angle old_angle = rod.angle;
329 Vector d = target-rod.position;
330 rod.angle = Geometry::atan2<float>(d.z, d.x);
331 if(cns.local_position.x || cns.local_position.z)
332 rod.angle -= Geometry::atan2<float>(cns.local_position.z, cns.local_position.x);
333 return abs(rod.angle-old_angle).radians()*cns.local_position.norm();
336 return distance(old_position, rod.position);
339 unsigned Vehicle::get_n_link_slots() const
344 Vehicle *Vehicle::get_link(unsigned i) const
347 throw out_of_range("Vehicle::get_link");
349 return (i==0 ? prev : next);
352 int Vehicle::get_link_slot(const Object &other) const
356 else if(&other==next)
362 bool Vehicle::collide_ray(const Ray &ray, float *d) const
365 return Object::collide_ray(ray, d);
371 Vehicle::Axle::Axle(const VehicleType::Axle &t):
376 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
378 axles(t.axles.size())
382 Vehicle::Rod::Rod(const VehicleType::Rod &t):
384 position(t.initial_position)