]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/vehicle.cpp
cb776f413daacb20f82889076aeab17e451cc842
[r2c2.git] / source / 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         // 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;
181         tilt = p.tilt;
182
183         if(bogies.size()>=2)
184         {
185                 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
186                 bogies.front().direction = front_point.rotation-p.rotation;
187
188                 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
189                 bogies.back().direction = back_point.rotation-p.rotation;
190         }
191
192         if(!prev)
193                 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
194         if(!next)
195                 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
196
197         signal_moved.emit();
198 }
199
200 void Vehicle::update_position_from(const Vehicle &veh)
201 {
202         int sign = (&veh==prev ? -1 : 1);
203
204         float tdist = (type.get_length()+veh.type.get_length())/2;
205         float margin = layout.get_catalogue().get_scale();
206
207         float dist = distance(veh.position, position);
208         if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
209         {
210                 if(sign<0)
211                         placement.place_after(veh.placement);
212                 else
213                         placement.place_before(veh.placement);
214                 update_position(0);
215
216                 dist = distance(veh.position, position);
217         }
218
219         float d = sign*(tdist-dist);
220         placement.advance(d);
221         update_position(d<0 ? -1 : 1);
222         turn_axles(d);
223 }
224
225 void Vehicle::propagate_position()
226 {
227         if(prev)
228                 propagate_forward();
229         if(next)
230                 propagate_backward();
231 }
232
233 void Vehicle::propagate_forward()
234 {
235         prev->update_position_from(*this);
236
237         if(prev->prev)
238                 prev->propagate_forward();
239 }
240
241 void Vehicle::propagate_backward()
242 {
243         next->update_position_from(*this);
244
245         if(next->next)
246                 next->propagate_backward();
247 }
248
249 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
250 {
251         unsigned s = t->get_sensor_address();
252         if(s!=sensor)
253         {
254                 unsigned old = sensor;
255                 sensor = s;
256                 if(release)
257                         layout.get_driver().set_sensor(old, false);
258                 else
259                         layout.get_driver().set_sensor(sensor, true);
260         }
261 }
262
263 void Vehicle::turn_axles(float d)
264 {
265         for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
266                 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
267
268         update_rods();
269 }
270
271 void Vehicle::update_rods()
272 {
273         if(rods.empty())
274                 return;
275
276         for(unsigned n=0; n<10; ++n)
277         {
278                 float max_d = 0;
279                 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
280                 {
281                         const vector<VehicleType::RodConstraint> &constraints = i->type->constraints;
282                         for(vector<VehicleType::RodConstraint>::const_iterator j=constraints.begin(); j!=constraints.end(); ++j)
283                         {
284                                 float d = resolve_rod_constraint(*i, *j);
285                                 max_d = max(d, max_d);
286                         }
287                 }
288
289                 if(max_d<0.0001)
290                         break;
291         }
292 }
293
294 float Vehicle::resolve_rod_constraint(Rod &rod, const VehicleType::RodConstraint &cns)
295 {
296         Vector target;
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)
302         {
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);
306         }
307         else if(cns.target==VehicleType::RodConstraint::ROD)
308         {
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);
312         }
313
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)
318         {
319                 Vector d = rod.position-target;
320                 rod.position = target+cns.axis*dot(d, cns.axis);
321                 rod.angle = Angle::zero();
322         }
323         else if(cns.type==VehicleType::RodConstraint::ROTATE)
324         {
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();
331         }
332
333         return distance(old_position, rod.position);
334 }
335
336 unsigned Vehicle::get_n_link_slots() const
337 {
338         return 2;
339 }
340
341 Vehicle *Vehicle::get_link(unsigned i) const
342 {
343         if(i>=2)
344                 throw out_of_range("Vehicle::get_link");
345
346         return (i==0 ? prev : next);
347 }
348
349 int Vehicle::get_link_slot(const Object &other) const
350 {
351         if(&other==prev)
352                 return 0;
353         else if(&other==next)
354                 return 1;
355         else
356                 return -1;
357 }
358
359 bool Vehicle::collide_ray(const Ray &ray, float *d) const
360 {
361         if(is_placed())
362                 return Object::collide_ray(ray, d);
363         else
364                 return false;
365 }
366
367
368 Vehicle::Axle::Axle(const VehicleType::Axle &t):
369         type(&t)
370 { }
371
372
373 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
374         type(&t),
375         axles(t.axles.size())
376 { }
377
378
379 Vehicle::Rod::Rod(const VehicleType::Rod &t):
380         type(&t),
381         position(t.initial_position)
382 { }
383
384 } // namespace R2C2