3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
13 #include "trackiter.h"
14 #include "tracktype.h"
16 #include "vehicletype.h"
23 Vehicle::Vehicle(Layout &l, const VehicleType &t):
29 bogie_dirs(type.get_bogies().size()),
33 layout.add_vehicle(*this);
42 layout.remove_vehicle(*this);
45 void Vehicle::attach_back(Vehicle &veh)
48 throw InvalidState("Already attached");
57 void Vehicle::attach_front(Vehicle &veh)
60 throw InvalidState("Already attached");
66 prev->propagate_backward();
69 void Vehicle::detach_back()
72 throw InvalidState("Not attached");
78 void Vehicle::detach_front()
81 throw InvalidState("Not attached");
87 void Vehicle::place(Track &t, unsigned e, float o, PlaceMode m)
89 track_pos = TrackPosition(&t, e, o);
92 track_pos.advance(-type.get_front_axle_offset());
93 else if(m==FRONT_BUFFER)
94 track_pos.advance(-type.get_length()/2);
96 track_pos.advance(-type.get_back_axle_offset());
97 else if(m==BACK_BUFFER)
98 track_pos.advance(type.get_length()/2);
101 propagate_position();
104 void Vehicle::unplace()
109 track_pos = TrackPosition();
117 void Vehicle::advance(float d)
119 track_pos.advance(d);
121 propagate_position();
124 float Vehicle::get_bogie_direction(unsigned i) const
126 if(i>=bogie_dirs.size())
127 throw InvalidParameterValue("Bogie index out of range");
128 return bogie_dirs[i];
131 void Vehicle::update_position()
135 const vector<VehicleType::Axle> &axles = type.get_axles();
136 const vector<VehicleType::Bogie> &bogies = type.get_bogies();
139 float wheelbase = axles.front().position-axles.back().position;
140 tp = get_point(track_pos, wheelbase, -axles.back().position/wheelbase);
142 else if(bogies.size()>=2)
144 TrackPosition front = track_pos;
145 front.advance(bogies.front().position);
146 TrackPosition back = track_pos;
147 back.advance(bogies.back().position);
148 float bogie_spacing = bogies.front().position-bogies.back().position;
149 adjust_for_distance(front, back, bogie_spacing);
151 const vector<VehicleType::Axle> &front_axles = bogies.front().axles;
152 float wheelbase = front_axles.front().position-front_axles.back().position;
153 TrackPoint front_point = get_point(front, wheelbase, -front_axles.back().position/wheelbase);
155 const vector<VehicleType::Axle> &back_axles = bogies.back().axles;
156 wheelbase = back_axles.front().position-back_axles.back().position;
157 TrackPoint back_point = get_point(back, wheelbase, -back_axles.back().position/wheelbase);
159 tp = get_point(front_point.pos, back_point.pos, -bogies.back().position/bogie_spacing);
161 bogie_dirs.front() = front_point.dir-tp.dir;
162 bogie_dirs.back() = back_point.dir-tp.dir;
165 tp = track_pos.get_point();
168 check_sensor(type.get_front_axle_offset(), front_sensor);
170 check_sensor(type.get_back_axle_offset(), back_sensor);
173 position.z += layout.get_catalogue().get_rail_elevation();
177 void Vehicle::update_position_from(const Vehicle &veh)
179 int sign = (&veh==prev ? -1 : 1);
181 float tdist = (type.get_length()+veh.type.get_length())/2;
182 float margin = layout.get_catalogue().get_scale();
184 float dist = distance(veh.position, position);
185 if(dist<tdist-margin || dist>tdist+margin)
187 track_pos = veh.track_pos;
188 track_pos.advance(sign*tdist);
191 dist = distance(veh.position, position);
194 track_pos.advance(sign*(tdist-dist));
198 void Vehicle::propagate_position()
203 propagate_backward();
206 void Vehicle::propagate_forward()
208 prev->update_position_from(*this);
211 prev->propagate_forward();
214 void Vehicle::propagate_backward()
216 next->update_position_from(*this);
219 next->propagate_backward();
222 void Vehicle::check_sensor(float offset, unsigned &sensor)
224 TrackPosition pos = track_pos;
226 unsigned s = pos.track->get_sensor_id();
229 /* Sensor ID under axle has changed. Deduce movement direction by using
230 the sensor ID under the midpoint of the vehicle. */
231 /* XXX This depends on the simulation running fast enough. Something
232 more robust would be preferable. */
233 unsigned old = sensor;
235 unsigned mid = track_pos.track->get_sensor_id();
238 /* There's a sensor and it's different from mid. We've just entered
240 layout.get_driver().set_sensor(sensor, true);
242 /* A sensor was under the axle and it was different from mid. We've
243 just left that sensor. */
244 layout.get_driver().set_sensor(old, false);
248 void Vehicle::adjust_for_distance(TrackPosition &front, TrackPosition &back, float tdist, float ratio) const
250 float margin = 0.01*layout.get_catalogue().get_scale();
254 Point front_point = front.get_point().pos;
255 Point back_point = back.get_point().pos;
257 float dx = front_point.x-back_point.x;
258 float dy = front_point.y-back_point.y;
259 float dz = front_point.z-back_point.z;
260 float dist = sqrt(dx*dx+dy*dy+dz*dz);
262 float diff = tdist-dist;
263 if(diff<-margin && adjust_dir<=0)
268 else if(diff>margin && adjust_dir>=0)
276 front.advance(diff*(1-ratio));
277 back.advance(-diff*ratio);
281 TrackPoint Vehicle::get_point(const Point &front, const Point &back, float ratio) const
283 float dx = front.x-back.x;
284 float dy = front.y-back.y;
285 float dz = front.z-back.z;
288 tp.pos = Point(back.x+dx*ratio, back.y+dy*ratio, back.z+dz*ratio);
289 tp.dir = atan2(dy, dx);
294 TrackPoint Vehicle::get_point(const TrackPosition &pos, float tdist, float ratio) const
296 TrackPosition front = pos;
297 front.advance(tdist*(1-ratio));
299 TrackPosition back = pos;
300 back.advance(-tdist*ratio);
302 adjust_for_distance(front, back, tdist, ratio);
303 return get_point(front.get_point().pos, back.get_point().pos, ratio);
307 Vehicle::TrackPosition::TrackPosition():
313 Vehicle::TrackPosition::TrackPosition(Track *t, unsigned e, float o):
319 void Vehicle::TrackPosition::advance(float d)
325 TrackIter iter(track, ep);
328 float path_len = iter->get_type().get_path_length(iter->get_active_path());
339 while(iter && offs<0)
341 iter = iter.flip().reverse();
345 float path_len = iter->get_type().get_path_length(iter->get_active_path());
350 track = iter.track();
356 TrackPoint Vehicle::TrackPosition::get_point() const
359 return track->get_point(ep, offs);
364 } // namespace Marklin