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