14 Track::Track(Layout &l, const TrackType &t):
22 links(type.get_endpoints().size()),
27 turnout_id = layout.allocate_turnout_id();
29 layout.add_track(*this);
31 if(layout.has_driver())
32 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
34 for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
40 layout.remove_track(*this);
43 Track *Track::clone(Layout *to_layout) const
45 Track *track = new Track((to_layout ? *to_layout : layout), type);
46 track->set_position(position);
47 track->set_rotation(rotation);
51 void Track::set_block(Block *b)
53 if(b && !b->has_track(*this))
54 throw logic_error("track not in block");
55 if(!b && block && block->has_track(*this))
56 throw logic_error("track still in block");
61 Block &Track::get_block() const
64 throw logic_error("!block");
69 void Track::set_position(const Vector &p)
74 void Track::set_rotation(float r)
79 while(rotation>M_PI*2)
83 void Track::set_slope(float s)
91 void Track::set_flex(bool f)
96 void Track::check_slope()
101 if(links[0] && links[1])
103 Vector epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
104 Vector epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
106 slope = epp1.z-position.z;
113 Vector epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
118 Vector epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
124 void Track::set_turnout_id(unsigned i)
126 if(!type.is_turnout())
127 throw logic_error("not a turnout");
130 layout.create_blocks(*this);
131 layout.update_routes();
132 if(layout.has_driver() && turnout_id)
133 layout.get_driver().add_turnout(turnout_id, type);
136 void Track::set_sensor_id(unsigned i)
138 if(type.is_turnout())
139 throw logic_error("is a turnout");
142 layout.create_blocks(*this);
143 if(layout.has_driver() && sensor_id)
144 layout.get_driver().add_sensor(sensor_id);
147 void Track::set_active_path(unsigned p)
150 throw logic_error("not a turnout");
151 if(!(type.get_paths()&(1<<p)))
152 throw invalid_argument("Track::set_active_path");
154 path_changing = true;
155 layout.get_driver().set_turnout(turnout_id, p);
158 int Track::get_endpoint_by_link(Track &other) const
160 for(unsigned i=0; i<links.size(); ++i)
167 Vector Track::get_endpoint_position(unsigned epi) const
169 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
171 throw out_of_range("Track::get_endpoint_position");
173 const TrackType::Endpoint &ep = eps[epi];
175 float c = cos(rotation);
176 float s = sin(rotation);
178 Vector p(position.x+c*ep.pos.x-s*ep.pos.y, position.y+s*ep.pos.x+c*ep.pos.y, position.z);
179 if(eps.size()==2 && epi==1)
184 float Track::get_endpoint_direction(unsigned epi) const
186 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
188 throw out_of_range("Track::get_endpoint_direction");
190 const TrackType::Endpoint &ep = eps[epi];
192 return rotation+ep.dir;
195 bool Track::snap_to(Track &other, bool link, float limit)
199 limit = layout.get_catalogue().get_gauge();
200 if(link && !flex && !other.get_flex())
205 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
206 const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
208 for(unsigned i=0; i<eps.size(); ++i)
210 Vector epp = get_endpoint_position(i);
212 for(unsigned j=0; j<other_eps.size(); ++j)
214 if(other.get_link(j))
217 Vector epp2 = other.get_endpoint_position(j);
218 float dx = epp2.x-epp.x;
219 float dy = epp2.y-epp.y;
220 float dz = epp2.z-epp.z;
221 if(dx*dx+dy*dy<limit && dz*dz<limit)
223 if(!link || (!flex && !other.get_flex()))
225 set_rotation(other.rotation+other_eps[j].dir-eps[i].dir+M_PI);
226 Vector p(epp2.x-(eps[i].pos.x*cos(rotation)-eps[i].pos.y*sin(rotation)),
227 epp2.y-(eps[i].pos.y*cos(rotation)+eps[i].pos.x*sin(rotation)),
229 if(eps.size()==2 && i==1)
237 break_link(*links[i]);
239 other.links[j] = this;
240 layout.create_blocks(*this);
242 signal_link_changed.emit(i, &other);
243 other.signal_link_changed.emit(j, this);
254 bool Track::snap(Vector &pt, float &d) const
256 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
258 for(unsigned i=0; i<eps.size(); ++i)
260 Vector epp = get_endpoint_position(i);
261 float dx = pt.x-epp.x;
262 float dy = pt.y-epp.y;
266 d = rotation+eps[i].dir;
274 void Track::break_link(Track &trk)
276 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
280 trk.break_link(*this);
281 // XXX Creates the blocks twice
282 layout.create_blocks(*this);
283 signal_link_changed.emit(i-links.begin(), 0);
288 void Track::break_links()
290 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
294 trk->break_link(*this);
298 Track *Track::get_link(unsigned i) const
301 throw out_of_range("Track::get_link");
306 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
308 TrackPoint p = type.get_point(epi, path, d);
309 float c = cos(rotation);
310 float s = sin(rotation);
312 p.pos = Vector(position.x+c*p.pos.x-s*p.pos.y, position.y+s*p.pos.x+c*p.pos.y, position.z);
314 if(type.get_endpoints().size()==2)
316 float len = type.get_path_length(path);
317 float grade = slope/len;
325 p.pos.z += slope-grade*d;
333 TrackPoint Track::get_point(unsigned epi, float d) const
335 return get_point(epi, active_path, d);
338 TrackPoint Track::get_nearest_point(const Vector &p) const
340 Vector local(p.x-position.x, p.y-position.y, p.z-position.z);
341 float c = cos(rotation);
342 float s = sin(rotation);
343 local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z);
345 TrackPoint tp = type.get_nearest_point(local);
346 tp.pos = Vector(position.x+tp.pos.x*c-tp.pos.y*s, position.y+tp.pos.y*c+tp.pos.x*s, position.z+tp.pos.z);
351 bool Track::collide_ray(const Vector &start, const Vector &ray) const
353 Vector local_start(start.x-position.x, start.y-position.y, start.z-position.z);
354 float c = cos(rotation);
355 float s = sin(rotation);
356 local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
357 Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
359 float width = layout.get_catalogue().get_ballast_profile().get_width();
361 return type.collide_ray(local_start, local_ray, width);
364 void Track::save(list<DataFile::Statement> &st) const
366 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
367 st.push_back((DataFile::Statement("rotation"), rotation));
368 st.push_back((DataFile::Statement("slope"), slope));
370 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
372 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
374 st.push_back((DataFile::Statement("flex"), true));
377 void Track::turnout_event(unsigned addr, unsigned state)
385 path_changing = false;
386 signal_path_changed.emit(active_path);
391 Track::Loader::Loader(Track &t):
392 DataFile::ObjectLoader<Track>(t)
394 add("position", &Loader::position);
395 add("rotation", &Loader::rotation);
396 add("slope", &Track::slope);
397 add("turnout_id", &Loader::turnout_id);
398 add("sensor_id", &Loader::sensor_id);
399 add("flex", &Track::flex);
402 void Track::Loader::position(float x, float y, float z)
404 obj.position = Vector(x, y, z);
407 void Track::Loader::rotation(float r)
412 void Track::Loader::sensor_id(unsigned id)
414 obj.set_sensor_id(id);
417 void Track::Loader::turnout_id(unsigned id)
419 obj.set_turnout_id(id);