]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/vehicle.cpp
Things with different gauges don't go together
[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         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");
112
113         placement.place(t, a);
114
115         update_position(0);
116         propagate_position();
117 }
118
119 void Vehicle::unplace()
120 {
121         if(!placement.is_placed())
122                 return;
123
124         placement.unplace();
125
126         if(prev)
127                 prev->unplace();
128         if(next)
129                 next->unplace();
130 }
131
132 void Vehicle::advance(float d)
133 {
134         placement.advance(d);
135         turn_axles(d);
136         update_position(d<0 ? -1 : 1);
137         propagate_position();
138 }
139
140 const Vehicle::Axle &Vehicle::get_axle(unsigned i) const
141 {
142         if(i>=axles.size())
143                 throw out_of_range("Vehicle::get_axle");
144         return axles[i];
145 }
146
147 const Vehicle::Axle &Vehicle::get_fixed_axle(unsigned i) const
148 {
149         if(i>=fixed_axles.size())
150                 throw out_of_range("Vehicle::get_fixed_axle");
151         return *fixed_axles[i];
152 }
153
154 const Vehicle::Bogie &Vehicle::get_bogie(unsigned i) const
155 {
156         if(i>=bogies.size())
157                 throw out_of_range("Vehicle::get_bogie");
158         return bogies[i];
159 }
160
161 const Vehicle::Axle &Vehicle::get_bogie_axle(unsigned i, unsigned j) const
162 {
163         if(i>=bogies.size())
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];
168 }
169
170 const Vehicle::Rod &Vehicle::get_rod(unsigned i) const
171 {
172         if(i>=rods.size())
173                 throw out_of_range("Vehicle::get_rod");
174         return rods[i];
175 }
176
177 void Vehicle::update_position(int sign)
178 {
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;
184         tilt = p.tilt;
185
186         if(bogies.size()>=2)
187         {
188                 OrientedPoint front_point = placement.get_bogie_point(bogies.front().type->index);
189                 bogies.front().direction = front_point.rotation-p.rotation;
190
191                 OrientedPoint back_point = placement.get_bogie_point(bogies.back().type->index);
192                 bogies.back().direction = back_point.rotation-p.rotation;
193         }
194
195         if(!prev)
196                 check_sensor(placement.get_position(VehiclePlacement::FRONT_AXLE), front_sensor, sign<0);
197         if(!next)
198                 check_sensor(placement.get_position(VehiclePlacement::BACK_AXLE), back_sensor, sign>0);
199
200         signal_moved.emit();
201 }
202
203 void Vehicle::update_position_from(const Vehicle &veh)
204 {
205         int sign = (&veh==prev ? -1 : 1);
206
207         float tdist = (type.get_length()+veh.type.get_length())/2;
208         float margin = layout.get_catalogue().get_scale();
209
210         float dist = distance(veh.position, position);
211         if(!is_placed() || dist<tdist-margin || dist>tdist+margin)
212         {
213                 if(sign<0)
214                         placement.place_after(veh.placement);
215                 else
216                         placement.place_before(veh.placement);
217                 update_position(0);
218
219                 dist = distance(veh.position, position);
220         }
221
222         float d = sign*(tdist-dist);
223         placement.advance(d);
224         update_position(d<0 ? -1 : 1);
225         turn_axles(d);
226 }
227
228 void Vehicle::propagate_position()
229 {
230         if(prev)
231                 propagate_forward();
232         if(next)
233                 propagate_backward();
234 }
235
236 void Vehicle::propagate_forward()
237 {
238         prev->update_position_from(*this);
239
240         if(prev->prev)
241                 prev->propagate_forward();
242 }
243
244 void Vehicle::propagate_backward()
245 {
246         next->update_position_from(*this);
247
248         if(next->next)
249                 next->propagate_backward();
250 }
251
252 void Vehicle::check_sensor(const TrackOffsetIter &t, unsigned &sensor, bool release)
253 {
254         unsigned s = t->get_sensor_address();
255         if(s!=sensor)
256         {
257                 unsigned old = sensor;
258                 sensor = s;
259                 if(release)
260                         layout.get_driver().set_sensor(old, false);
261                 else
262                         layout.get_driver().set_sensor(sensor, true);
263         }
264 }
265
266 void Vehicle::turn_axles(float d)
267 {
268         for(vector<Axle>::iterator i=axles.begin(); i!=axles.end(); ++i)
269                 i->angle += Angle::from_radians(d*2/i->type->wheel_dia);
270
271         update_rods();
272 }
273
274 void Vehicle::update_rods()
275 {
276         if(rods.empty())
277                 return;
278
279         for(unsigned n=0; n<10; ++n)
280         {
281                 float max_d = 0;
282                 for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
283                 {
284                         const vector<VehicleType::RodConstraint> &constraints = i->type->constraints;
285                         for(vector<VehicleType::RodConstraint>::const_iterator j=constraints.begin(); j!=constraints.end(); ++j)
286                         {
287                                 float d = resolve_rod_constraint(*i, *j);
288                                 max_d = max(d, max_d);
289                         }
290                 }
291
292                 if(max_d<0.0001)
293                         break;
294         }
295 }
296
297 float Vehicle::resolve_rod_constraint(Rod &rod, const VehicleType::RodConstraint &cns)
298 {
299         Vector target;
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)
305         {
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);
309         }
310         else if(cns.target==VehicleType::RodConstraint::ROD)
311         {
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);
315         }
316
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)
321         {
322                 Vector d = rod.position-target;
323                 rod.position = target+cns.axis*dot(d, cns.axis);
324                 rod.angle = Angle::zero();
325         }
326         else if(cns.type==VehicleType::RodConstraint::ROTATE)
327         {
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();
334         }
335
336         return distance(old_position, rod.position);
337 }
338
339 unsigned Vehicle::get_n_link_slots() const
340 {
341         return 2;
342 }
343
344 Vehicle *Vehicle::get_link(unsigned i) const
345 {
346         if(i>=2)
347                 throw out_of_range("Vehicle::get_link");
348
349         return (i==0 ? prev : next);
350 }
351
352 int Vehicle::get_link_slot(const Object &other) const
353 {
354         if(&other==prev)
355                 return 0;
356         else if(&other==next)
357                 return 1;
358         else
359                 return -1;
360 }
361
362 bool Vehicle::collide_ray(const Ray &ray, float *d) const
363 {
364         if(is_placed())
365                 return Object::collide_ray(ray, d);
366         else
367                 return false;
368 }
369
370
371 Vehicle::Axle::Axle(const VehicleType::Axle &t):
372         type(&t)
373 { }
374
375
376 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
377         type(&t),
378         axles(t.axles.size())
379 { }
380
381
382 Vehicle::Rod::Rod(const VehicleType::Rod &t):
383         type(&t),
384         position(t.initial_position)
385 { }
386
387 } // namespace R2C2