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_snap_node(links[0]->get_endpoint_by_link(*this)).position;
104 Vector epp1 = links[1]->get_snap_node(links[1]->get_endpoint_by_link(*this)).position;
106 slope = epp1.z-position.z;
113 Vector epp = links[0]->get_snap_node(links[0]->get_endpoint_by_link(*this)).position;
118 Vector epp = links[1]->get_snap_node(links[1]->get_endpoint_by_link(*this)).position;
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 bool Track::snap_to(Track &other, bool link, float limit)
171 limit = layout.get_catalogue().get_gauge();
172 if(link && !flex && !other.get_flex())
177 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
178 const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
180 for(unsigned i=0; i<eps.size(); ++i)
182 Vector epp = get_snap_node(i).position;
184 for(unsigned j=0; j<other_eps.size(); ++j)
186 if(other.get_link(j))
189 Vector epp2 = other.get_snap_node(j).position;
190 float dx = epp2.x-epp.x;
191 float dy = epp2.y-epp.y;
192 float dz = epp2.z-epp.z;
193 if(dx*dx+dy*dy<limit && dz*dz<limit)
195 if(!link || (!flex && !other.get_flex()))
197 set_rotation(other.rotation+other_eps[j].dir-eps[i].dir+M_PI);
198 Vector p(epp2.x-(eps[i].pos.x*cos(rotation)-eps[i].pos.y*sin(rotation)),
199 epp2.y-(eps[i].pos.y*cos(rotation)+eps[i].pos.x*sin(rotation)),
201 if(eps.size()==2 && i==1)
209 break_link(*links[i]);
211 other.links[j] = this;
212 layout.create_blocks(*this);
214 signal_link_changed.emit(i, &other);
215 other.signal_link_changed.emit(j, this);
226 void Track::break_link(Track &trk)
228 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
232 trk.break_link(*this);
233 // XXX Creates the blocks twice
234 layout.create_blocks(*this);
235 signal_link_changed.emit(i-links.begin(), 0);
240 void Track::break_links()
242 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
246 trk->break_link(*this);
250 Track *Track::get_link(unsigned i) const
253 throw out_of_range("Track::get_link");
258 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
260 TrackPoint p = type.get_point(epi, path, d);
261 float c = cos(rotation);
262 float s = sin(rotation);
264 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);
266 if(type.get_endpoints().size()==2)
268 float len = type.get_path_length(path);
269 float grade = slope/len;
277 p.pos.z += slope-grade*d;
285 TrackPoint Track::get_point(unsigned epi, float d) const
287 return get_point(epi, active_path, d);
290 unsigned Track::get_n_snap_nodes() const
292 return type.get_endpoints().size();
295 Snap Track::get_snap_node(unsigned i) const
297 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
299 throw out_of_range("Track::get_snap_node");
302 const TrackType::Endpoint &ep = eps[i];
304 float c = cos(rotation);
305 float s = sin(rotation);
307 result.position = Vector(position.x+c*ep.pos.x-s*ep.pos.y, position.y+s*ep.pos.x+c*ep.pos.y, position.z);
308 if(eps.size()==2 && i==1)
309 result.position.z += slope;
311 result.rotation = rotation+ep.dir;
316 bool Track::snap(Snap &sn, float limit, SnapType what) const
318 if(Object::snap(sn, limit, what))
321 if(what&SNAP_SEGMENT)
323 Vector local(sn.position.x-position.x, sn.position.y-position.y, sn.position.z-position.z);
324 float c = cos(rotation);
325 float s = sin(rotation);
326 local = Vector(c*local.x+s*local.y, c*local.y-s*local.x, local.z);
328 TrackPoint tp = type.get_nearest_point(local);
329 Vector d(local.x-tp.pos.x, local.y-tp.pos.y, local.z-tp.pos.z);
330 if(d.x*d.x+d.y*d.y+d.z*d.z<=limit*limit)
332 sn.position = 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);
333 sn.rotation = tp.dir+rotation;
341 SnapType Track::get_default_snap_type_to(const Object &other) const
343 if(dynamic_cast<const Track *>(&other))
349 bool Track::collide_ray(const Vector &start, const Vector &ray) const
351 Vector local_start(start.x-position.x, start.y-position.y, start.z-position.z);
352 float c = cos(rotation);
353 float s = sin(rotation);
354 local_start = Vector(c*local_start.x+s*local_start.y, c*local_start.y-s*local_start.x, local_start.z);
355 Vector local_ray(c*ray.x+s*ray.y, c*ray.y-s*ray.x, ray.z);
357 float width = layout.get_catalogue().get_ballast_profile().get_width();
359 return type.collide_ray(local_start, local_ray, width);
362 void Track::save(list<DataFile::Statement> &st) const
364 st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
365 st.push_back((DataFile::Statement("rotation"), rotation));
366 st.push_back((DataFile::Statement("slope"), slope));
368 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
370 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
372 st.push_back((DataFile::Statement("flex"), true));
375 void Track::turnout_event(unsigned addr, unsigned state)
383 path_changing = false;
384 signal_path_changed.emit(active_path);
389 Track::Loader::Loader(Track &t):
390 DataFile::ObjectLoader<Track>(t)
392 add("position", &Loader::position);
393 add("rotation", &Loader::rotation);
394 add("slope", &Track::slope);
395 add("turnout_id", &Loader::turnout_id);
396 add("sensor_id", &Loader::sensor_id);
397 add("flex", &Track::flex);
400 void Track::Loader::position(float x, float y, float z)
402 obj.position = Vector(x, y, z);
405 void Track::Loader::rotation(float r)
410 void Track::Loader::sensor_id(unsigned id)
412 obj.set_sensor_id(id);
415 void Track::Loader::turnout_id(unsigned id)
417 obj.set_turnout_id(id);