]> git.tdb.fi Git - r2c2.git/blob - libr2c2/vehicle.cpp
Don't crash if a train has no router
[r2c2.git] / libr2c2 / vehicle.cpp
1 #include <cmath>
2 #include "catalogue.h"
3 #include "driver.h"
4 #include "layout.h"
5 #include "track.h"
6 #include "trackiter.h"
7 #include "tracktype.h"
8 #include "vehicle.h"
9 #include "vehicletype.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace R2C2 {
15
16 Vehicle::Vehicle(Layout &l, const VehicleType &t):
17         Object(l),
18         type(t),
19         train(0),
20         next(0),
21         prev(0),
22         placement(type),
23         front_sensor(0),
24         back_sensor(0)
25 {
26         axles.assign(type.get_axles().begin(), type.get_axles().end());
27         for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
28                 if(!i->type->bogie)
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];
35
36         update_rods();
37
38         layout.add(*this);
39 }
40
41 Vehicle::~Vehicle()
42 {
43         if(next)
44                 detach_back();
45         if(prev)
46                 detach_front();
47         layout.remove(*this);
48 }
49
50 Vehicle *Vehicle::clone(Layout *to_layout) const
51 {
52         Vehicle *veh = new Vehicle((to_layout ? *to_layout : layout), type);
53         veh->set_position(position);
54         veh->set_rotation(rotation);
55         return veh;
56 }
57
58 void Vehicle::set_train(Train *t)
59 {
60         train = t;
61 }
62
63 void Vehicle::attach_back(Vehicle &veh)
64 {
65         if(next || veh.prev)
66                 throw attachment_error("already attached");
67
68         next = &veh;
69         veh.prev = this;
70
71         if(is_placed())
72                 propagate_backward();
73 }
74
75 void Vehicle::attach_front(Vehicle &veh)
76 {
77         if(prev || veh.next)
78                 throw attachment_error("already attached");
79
80         prev = &veh;
81         veh.next = this;
82
83         if(prev->is_placed())
84                 prev->propagate_backward();
85 }
86
87 void Vehicle::detach_back()
88 {
89         if(!next)
90                 throw attachment_error("not attached");
91
92         next->prev = 0;
93         next = 0;
94 }
95
96 void Vehicle::detach_front()
97 {
98         if(!prev)
99                 throw attachment_error("not attached");
100
101         prev->next = 0;
102         prev = 0;
103 }
104
105 void Vehicle::place(const TrackOffsetIter &t, VehiclePlacement::Anchor a)
106 {
107         if(!t)
108                 throw invalid_argument("Vehicle::place");
109
110         placement.place(t, a);
111
112         update_position(0);
113         propagate_position();
114 }
115
116 void Vehicle::unplace()
117 {
118         if(!placement.is_placed())
119                 return;
120
121         placement.unplace();
122
123         if(prev)
124                 prev->unplace();
125         if(next)
126                 next->unplace();
127 }
128
129 void Vehicle::advance(float d)
130 {
131         placement.advance(d);
132         turn_axles(d);
133         update_position(d<0 ? -1 : 1);
134         propagate_position();
135 }
136
137 const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
138 {
139         if(i>=axles.size())
140                 throw out_of_range("Vehicle::get_axle");
141         return axles[i];
142 }
143
144 const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const
145 {
146         if(i>=fixed_axles.size())
147                 throw out_of_range("Vehicle::get_fixed_axle");
148         return *fixed_axles[i];
149 }
150
151 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
152 {
153         if(i>=bogies.size())
154                 throw out_of_range("Vehicle::get_bogie");
155         return bogies[i];
156 }
157
158 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
159 {
160         if(i>=bogies.size())
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];
165 }
166
167 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
168 {
169         if(i>=rods.size())
170                 throw out_of_range("Vehicle::get_rod");
171         return rods[i];
172 }
173
174 void Vehicle::update_position(int sign)
175 {
176         OrientedPoint p = placement.get_point();
177         position = p.position;
178         position.z += layout.get_catalogue().get_rail_elevation();
179         rotation = p.rotation;
180         tilt = p.tilt;
181
182         if(bogies.size()>=2)
183         {
184                 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
185                 bogies.front().direction = front_point.rotation-p.rotation;
186
187                 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
188                 bogies.back().direction = back_point.rotation-p.rotation;
189         }
190
191         if(!prev)
192                 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
193         if(!next)
194                 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
195
196         signal_moved.emit();
197 }
198
199 void Vehicle::update_position_from(const Vehicle &veh)
200 {
201         int sign = (&veh==prev ? -1 : 1);
202
203         float tdist = (type.get_length()+veh.type.get_length())/2;
204         float margin = layout.get_catalogue().get_scale();
205
206         float dist = distance(veh.position, position);
207         if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
208         {
209                 if(sign<0)
210                         placement.place_after(veh.placement);
211                 else
212                         placement.place_before(veh.placement);
213                 update_position(0);
214
215                 dist = distance(veh.position, position);
216         }
217
218         float d = sign*(tdist-dist);
219         placement.advance(d);
220         update_position(d<0 ? -1 : 1);
221         turn_axles(d);
222 }
223
224 void Vehicle::propagate_position()
225 {
226         if(prev)
227                 propagate_forward();
228         if(next)
229                 propagate_backward();
230 }
231
232 void Vehicle::propagate_forward()
233 {
234         prev->update_position_from(*this);
235
236         if(prev->prev)
237                 prev->propagate_forward();
238 }
239
240 void Vehicle::propagate_backward()
241 {
242         next->update_position_from(*this);
243
244         if(next->next)
245                 next->propagate_backward();
246 }
247
248 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
249 {
250         unsigned s = t->get_sensor_address();
251         if(s!=sensor)
252         {
253                 unsigned old = sensor;
254                 sensor = s;
255                 if(release)
256                         layout.get_driver().set_sensor(old, false);
257                 else
258                         layout.get_driver().set_sensor(sensor, true);
259         }
260 }
261
262 void Vehicle::turn_axles(float d)
263 {
264         for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
265                 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
266
267         update_rods();
268 }
269
270 void Vehicle::update_rods()
271 {
272         if(rods.empty())
273                 return;
274
275         for(unsigned n=0; n<10; ++n)
276         {
277                 float max_d = 0;
278                 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
279                 {
280                         const vector<VehicleType::RodConstraint> &constraints = i->type->constraints;
281                         for(vector<VehicleType::RodConstraint>::const_iterator j=constraints.begin(); j!=constraints.end(); ++j)
282                         {
283                                 float d = resolve_rod_constraint(*i, *j);
284                                 max_d = max(d, max_d);
285                         }
286                 }
287
288                 if(max_d<0.0001)
289                         break;
290         }
291 }
292
293 float Vehicle::resolve_rod_constraint(Rod &rod, const VehicleType::RodConstraint &cns)
294 {
295         Vector target;
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)
301         {
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);
305         }
306         else if(cns.target==VehicleType::RodConstraint::ROD)
307         {
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);
311         }
312
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)
317         {
318                 Vector d = rod.position-target;
319                 rod.position = target+cns.axis*dot(d, cns.axis);
320                 rod.angle = Angle::zero();
321         }
322         else if(cns.type==VehicleType::RodConstraint::ROTATE)
323         {
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();
330         }
331
332         return distance(old_position, rod.position);
333 }
334
335 unsigned Vehicle::get_n_link_slots() const
336 {
337         return 2;
338 }
339
340 Vehicle *Vehicle::get_link(unsigned i) const
341 {
342         if(i>=2)
343                 throw out_of_range("Vehicle::get_link");
344
345         return (i==0 ? prev : next);
346 }
347
348 int Vehicle::get_link_slot(const Object &other) const
349 {
350         if(&other==prev)
351                 return 0;
352         else if(&other==next)
353                 return 1;
354         else
355                 return -1;
356 }
357
358 bool Vehicle::collide_ray(const Ray &ray) const
359 {
360         if(is_placed())
361                 return Object::collide_ray(ray);
362         else
363                 return false;
364 }
365
366
367 Vehicle::Axle::Axle(const VehicleType::Axle &t):
368         type(&t)
369 { }
370
371
372 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
373         type(&t),
374         axles(t.axles.size())
375 { }
376
377
378 Vehicle::Rod::Rod(const VehicleType::Rod &t):
379         type(&t),
380         position(t.initial_position)
381 { }
382
383 } // namespace R2C2