]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/vehicle.cpp
Split vehicle placement code to a separate class
[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         for(vector<Rod>::iterator i=rods.begin(); i!=rods.end(); ++i)
271         {
272                 if(i->type->pivot==VehicleType::Rod::BODY)
273                         i->position = i->type->pivot_point;
274                 else if(i->type->pivot==VehicleType::Rod::AXLE)
275                 {
276                         const Axle &axle = get_fixed_axle(i->type->pivot_index);
277                         const Vector &pp = i->type->pivot_point;
278                         Transform trans = Transform::rotation(axle.angle, Vector(0, -1, 0));
279                         i->position = Vector(axle.type->position, 0, axle.type->wheel_dia/2)+trans.transform(pp);
280                 }
281                 else if(i->type->pivot==VehicleType::Rod::ROD)
282                 {
283                         const Rod &prod = get_rod(i->type->pivot_index);
284                         const Vector &pos = prod.position;
285                         const Vector &off = i->type->pivot_point;
286                         Transform trans = Transform::rotation(prod.angle, Vector(0, 1, 0));
287                         i->position = pos+trans.transform(off);
288                 }
289
290                 if(i->type->connect_index>=0)
291                 {
292                         Rod &crod = rods[i->type->connect_index];
293                         if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::SLIDE_X)
294                         {
295                                 Vector span = crod.position+i->type->connect_offset-i->position;
296                                 float cd = i->type->connect_point.norm();
297                                 Angle ca = Geometry::atan2(i->type->connect_point.z, i->type->connect_point.x);
298                                 span.x = sqrt(cd*cd-span.z*span.z)*(span.x>0 ? 1 : -1);
299                                 i->angle = Geometry::atan2(span.z, span.x)-ca;
300                                 crod.position.x = i->position.x+span.x-i->type->connect_offset.x;
301                         }
302                         else if(i->type->limit==VehicleType::Rod::ROTATE && crod.type->limit==VehicleType::Rod::ROTATE)
303                         {
304                                 Vector span = crod.position-i->position;
305                                 float d = span.norm();
306                                 float cd1 = i->type->connect_point.norm();
307                                 float cd2 = i->type->connect_offset.norm();
308                                 float a = (d*d+cd1*cd1-cd2*cd2)/(2*d);
309                                 float b = sqrt(cd1*cd1-a*a);
310                                 float sign = (cross(i->type->connect_point, span).y>0 ? 1 : -1);
311                                 Vector conn = Vector(span.x*a-span.z*b, 0, span.z*a+span.x*b)/(d*sign);
312                                 Angle ca1 = Geometry::atan2(i->type->connect_point.z, i->type->connect_point.x);
313                                 Angle ca2 = Geometry::atan2(i->type->connect_offset.z, i->type->connect_offset.x);
314                                 i->angle = Geometry::atan2(conn.z, conn.x)-ca1;
315                                 crod.angle = Geometry::atan2(conn.z-span.z, conn.x-span.x)-ca2;
316                         }
317                 }
318         }
319 }
320
321 unsigned Vehicle::get_n_link_slots() const
322 {
323         return 2;
324 }
325
326 Vehicle *Vehicle::get_link(unsigned i) const
327 {
328         if(i>=2)
329                 throw out_of_range("Vehicle::get_link");
330
331         return (i==0 ? prev : next);
332 }
333
334 int Vehicle::get_link_slot(const Object &other) const
335 {
336         if(&other==prev)
337                 return 0;
338         else if(&other==next)
339                 return 1;
340         else
341                 return -1;
342 }
343
344
345 Vehicle::Axle::Axle(const VehicleType::Axle &t):
346         type(&t)
347 { }
348
349
350 Vehicle::Bogie::Bogie(const VehicleType::Bogie &t):
351         type(&t),
352         axles(t.axles.size())
353 { }
354
355
356 Vehicle::Rod::Rod(const VehicleType::Rod &t):
357         type(&t)
358 { }
359
360 } // namespace R2C2