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 position.z += layout.get_catalogue().get_rail_elevation();
179 rotation = p.rotation;
184 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
185 bogies.front().direction = front_point.rotation-p.rotation;
187 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
188 bogies.back().direction = back_point.rotation-p.rotation;
192 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
194 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
199 void Vehicle::update_position_from(const Vehicle &veh)
201 int sign = (&veh==prev ? -1 : 1);
203 float tdist = (type.get_length()+veh.type.get_length())/2;
204 float margin = layout.get_catalogue().get_scale();
206 float dist = distance(veh.position, position);
207 if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
210 placement.place_after(veh.placement);
212 placement.place_before(veh.placement);
215 dist = distance(veh.position, position);
218 float d = sign*(tdist-dist);
219 placement.advance(d);
220 update_position(d<0 ? -1 : 1);
224 void Vehicle::propagate_position()
229 propagate_backward();
232 void Vehicle::propagate_forward()
234 prev->update_position_from(*this);
237 prev->propagate_forward();
240 void Vehicle::propagate_backward()
242 next->update_position_from(*this);
245 next->propagate_backward();
248 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
250 unsigned s = t->get_sensor_address();
253 unsigned old = sensor;
256 layout.get_driver().set_sensor(old, false);
258 layout.get_driver().set_sensor(sensor, true);
262 void Vehicle::turn_axles(float d)
264 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
265 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
270 void Vehicle::update_rods()
275 for(unsigned n=0; n<10; ++n)
278 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
280 const vector<VehicleType::RodConstraint> &constraints = i->type->constraints;
281 for(vector<VehicleType::RodConstraint>::const_iterator j=constraints.begin(); j!=constraints.end(); ++j)
283 float d = resolve_rod_constraint(*i, *j);
284 max_d = max(d, max_d);
293 float Vehicle::resolve_rod_constraint(Rod &rod, const VehicleType::RodConstraint &cns)
296 if(cns.target==VehicleType::RodConstraint::BODY)
297 target = cns.target_position;
298 else if(cns.target==VehicleType::RodConstraint::BOGIE)
299 ; // TODO currently rods must lie in the xz plane of the body
300 else if(cns.target==VehicleType::RodConstraint::AXLE)
302 const Axle &axle = get_axle(cns.target_index);
303 target = Vector(axle.type->position, 0, axle.type->wheel_dia/2);
304 target += Transform::rotation(axle.angle, Vector(0, 1, 0)).transform(cns.target_position);
306 else if(cns.target==VehicleType::RodConstraint::ROD)
308 const Rod &trod = get_rod(cns.target_index);
309 target = trod.position;
310 target += Transform::rotation(trod.angle, Vector(0, -1, 0)).transform(cns.target_position);
313 Vector old_position = rod.position;
314 if(cns.type==VehicleType::RodConstraint::MOVE)
315 rod.position = target-Transform::rotation(rod.angle, Vector(0, -1, 0)).transform(cns.local_position);
316 else if(cns.type==VehicleType::RodConstraint::SLIDE)
318 Vector d = rod.position-target;
319 rod.position = target+cns.axis*dot(d, cns.axis);
320 rod.angle = Angle::zero();
322 else if(cns.type==VehicleType::RodConstraint::ROTATE)
324 Angle old_angle = rod.angle;
325 Vector d = target-rod.position;
326 rod.angle = Geometry::atan2<float>(d.z, d.x);
327 if(cns.local_position.x || cns.local_position.z)
328 rod.angle -= Geometry::atan2<float>(cns.local_position.z, cns.local_position.x);
329 return abs(rod.angle-old_angle).radians()*cns.local_position.norm();
332 return distance(old_position, rod.position);
335 unsigned Vehicle::get_n_link_slots() const
340 Vehicle *Vehicle::get_link(unsigned i) const
343 throw out_of_range("Vehicle::get_link");
345 return (i==0 ? prev : next);
348 int Vehicle::get_link_slot(const Object &other) const
352 else if(&other==next)
358 bool Vehicle::collide_ray(const Ray &ray) const
361 return Object::collide_ray(ray);
367 Vehicle::Axle::Axle(const VehicleType::Axle &t):
372 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
374 axles(t.axles.size())
378 Vehicle::Rod::Rod(const VehicleType::Rod &t):
380 position(t.initial_position)