6 #include "trackappearance.h"
10 #include "vehicletype.h"
17 Vehicle::Vehicle(Layout &l, const VehicleType &t):
27 axles.assign(type.get_axles().begin(), type.get_axles().end());
28 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
30 fixed_axles.push_back(&*i);
31 bogies.assign(type.get_bogies().begin(), type.get_bogies().end());
32 rods.assign(type.get_rods().begin(), type.get_rods().end());
33 for(vector<Bogie>::iterator i=bogies.begin(); i!=bogies.end(); ++i)
34 for(unsigned j=0; j<i->axles.size(); ++j)
35 i->axles[j] = &axles[i->type->first_axle+j];
51 Vehicle *Vehicle::clone(Layout *to_layout) const
53 Vehicle *veh = new Vehicle((to_layout ? *to_layout : layout), type);
54 veh->set_position(position);
55 veh->set_rotation(rotation);
59 void Vehicle::set_train(Train *t)
64 void Vehicle::attach_back(Vehicle &veh)
67 throw attachment_error("already attached");
76 void Vehicle::attach_front(Vehicle &veh)
79 throw attachment_error("already attached");
85 prev->propagate_backward();
88 void Vehicle::detach_back()
91 throw attachment_error("not attached");
97 void Vehicle::detach_front()
100 throw attachment_error("not attached");
106 void Vehicle::place(const TrackOffsetIter &t, VehiclePlacement::Anchor a)
109 throw invalid_argument("Vehicle::place");
110 float gauge_ratio = t->get_type().get_gauge()/type.get_gauge();
111 if(gauge_ratio<0.99 || gauge_ratio>1.01)
112 throw logic_error("Incompatible gauge");
114 placement.place(t, a);
117 propagate_position();
120 void Vehicle::unplace()
122 if(!placement.is_placed())
133 void Vehicle::advance(float d)
135 placement.advance(d);
137 update_position(d<0 ? -1 : 1);
138 propagate_position();
141 const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
144 throw out_of_range("Vehicle::get_axle");
148 const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const
150 if(i>=fixed_axles.size())
151 throw out_of_range("Vehicle::get_fixed_axle");
152 return *fixed_axles[i];
155 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
158 throw out_of_range("Vehicle::get_bogie");
162 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
165 throw out_of_range("Vehicle::get_bogie_axle");
166 if(j>=bogies[i].axles.size())
167 throw out_of_range("Vehicle::get_bogie_axle");
168 return *bogies[i].axles[j];
171 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
174 throw out_of_range("Vehicle::get_rod");
178 void Vehicle::update_position(int sign)
180 OrientedPoint p = placement.get_point();
181 position = p.position;
182 // TODO Move the z adjustment to VehiclePlacement
183 position.z += placement.get_position(VehiclePlacement::FRONT_AXLE)->get_type().get_appearance().get_rail_elevation();
184 rotation = p.rotation;
189 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
190 bogies.front().direction = front_point.rotation-p.rotation;
192 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
193 bogies.back().direction = back_point.rotation-p.rotation;
197 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
199 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
204 void Vehicle::update_position_from(const Vehicle &veh)
206 int sign = (&veh==prev ? -1 : 1);
208 float tdist = (type.get_length()+veh.type.get_length())/2;
209 float margin = layout.get_catalogue().get_scale();
211 float dist = distance(veh.position, position);
212 if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
215 placement.place_after(veh.placement);
217 placement.place_before(veh.placement);
220 dist = distance(veh.position, position);
223 float d = sign*(tdist-dist);
224 placement.advance(d);
225 update_position(d<0 ? -1 : 1);
229 void Vehicle::propagate_position()
234 propagate_backward();
237 void Vehicle::propagate_forward()
239 prev->update_position_from(*this);
242 prev->propagate_forward();
245 void Vehicle::propagate_backward()
247 next->update_position_from(*this);
250 next->propagate_backward();
253 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
255 unsigned s = t->get_sensor_address();
258 unsigned old = sensor;
261 layout.get_driver().set_sensor(old, false);
263 layout.get_driver().set_sensor(sensor, true);
267 void Vehicle::turn_axles(float d)
269 for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
270 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
275 void Vehicle::update_rods()
280 for(unsigned n=0; n<10; ++n)
283 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
285 const vector<VehicleType::RodConstraint> &constraints = i->type->constraints;
286 for(vector<VehicleType::RodConstraint>::const_iterator j=constraints.begin(); j!=constraints.end(); ++j)
288 float d = resolve_rod_constraint(*i, *j);
289 max_d = max(d, max_d);
298 float Vehicle::resolve_rod_constraint(Rod &rod, const VehicleType::RodConstraint &cns)
301 if(cns.target==VehicleType::RodConstraint::BODY)
302 target = cns.target_position;
303 else if(cns.target==VehicleType::RodConstraint::BOGIE)
304 ; // TODO currently rods must lie in the xz plane of the body
305 else if(cns.target==VehicleType::RodConstraint::AXLE)
307 const Axle &axle = get_axle(cns.target_index);
308 target = Vector(axle.type->position, 0, axle.type->wheel_dia/2);
309 target += Transform::rotation(axle.angle, Vector(0, 1, 0)).transform(cns.target_position);
311 else if(cns.target==VehicleType::RodConstraint::ROD)
313 const Rod &trod = get_rod(cns.target_index);
314 target = trod.position;
315 target += Transform::rotation(trod.angle, Vector(0, -1, 0)).transform(cns.target_position);
318 Vector old_position = rod.position;
319 if(cns.type==VehicleType::RodConstraint::MOVE)
320 rod.position = target-Transform::rotation(rod.angle, Vector(0, -1, 0)).transform(cns.local_position);
321 else if(cns.type==VehicleType::RodConstraint::SLIDE)
323 Vector d = rod.position-target;
324 rod.position = target+cns.axis*dot(d, cns.axis);
325 rod.angle = Angle::zero();
327 else if(cns.type==VehicleType::RodConstraint::ROTATE)
329 Angle old_angle = rod.angle;
330 Vector d = target-rod.position;
331 rod.angle = Geometry::atan2<float>(d.z, d.x);
332 if(cns.local_position.x || cns.local_position.z)
333 rod.angle -= Geometry::atan2<float>(cns.local_position.z, cns.local_position.x);
334 return abs(rod.angle-old_angle).radians()*cns.local_position.norm();
337 return distance(old_position, rod.position);
340 unsigned Vehicle::get_n_link_slots() const
345 Vehicle *Vehicle::get_link(unsigned i) const
348 throw out_of_range("Vehicle::get_link");
350 return (i==0 ? prev : next);
353 int Vehicle::get_link_slot(const Object &other) const
357 else if(&other==next)
363 bool Vehicle::collide_ray(const Ray &ray, float *d) const
366 return Object::collide_ray(ray, d);
372 Vehicle::Axle::Axle(const VehicleType::Axle &t):
377 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
379 axles(t.axles.size())
383 Vehicle::Rod::Rod(const VehicleType::Rod &t):
385 position(t.initial_position)