3 This file is part of R²C²
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
10 #include "catalogue.h"
14 #include "tracktype.h"
21 Track::Track(Layout &l, const TrackType &t):
28 turnout_id(type.is_turnout() ? layout.allocate_turnout_id(type.is_double_address()) : 0),
30 links(type.get_endpoints().size()),
33 layout.add_track(*this);
35 if(layout.has_driver())
36 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
38 for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
44 layout.remove_track(*this);
47 void Track::set_block(Block *b)
49 if(b && !b->has_track(*this))
50 throw InvalidParameterValue("Track is not in the Block");
51 if(!b && block && block->has_track(*this))
52 throw InvalidState("Track is still in a Block");
57 Block &Track::get_block() const
60 throw InvalidState("No Block");
65 void Track::set_position(const Point &p)
70 void Track::set_rotation(float r)
79 void Track::set_slope(float s)
87 void Track::set_flex(bool f)
92 void Track::check_slope()
97 if(links[0] && links[1])
99 Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
100 Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
102 slope = epp1.z-pos.z;
109 Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
114 Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
120 void Track::set_turnout_id(unsigned i)
122 if(!type.is_turnout())
123 throw InvalidState("Not a turnout");
126 layout.create_blocks(*this);
127 layout.update_routes();
128 if(layout.has_driver() && turnout_id)
130 layout.get_driver().add_turnout(turnout_id);
131 if(type.is_double_address())
132 layout.get_driver().add_turnout(turnout_id+1);
136 void Track::set_sensor_id(unsigned i)
138 if(type.is_turnout())
139 throw InvalidState("Can't set sensor on 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 InvalidState("Not a turnout");
151 if(!(type.get_paths()&(1<<p)))
152 throw InvalidParameterValue("Invalid path");
154 layout.get_driver().set_turnout(turnout_id, p&1);
155 if(type.is_double_address())
156 layout.get_driver().set_turnout(turnout_id+1, p&2);
157 else if(type.get_n_paths()>2)
158 active_path = (active_path&1) | (p&2);
161 int Track::get_endpoint_by_link(Track &other) const
163 for(unsigned i=0; i<links.size(); ++i)
170 Point Track::get_endpoint_position(unsigned epi) const
172 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
174 throw InvalidParameterValue("TrackType::Endpoint index out of range");
176 const TrackType::Endpoint &ep = eps[epi];
181 Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
182 if(eps.size()==2 && epi==1)
187 float Track::get_endpoint_direction(unsigned epi) const
189 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
191 throw InvalidParameterValue("TrackType::Endpoint index out of range");
193 const TrackType::Endpoint &ep = eps[epi];
198 bool Track::snap_to(Track &other, bool link, float limit)
202 limit = layout.get_catalogue().get_gauge();
203 if(link && !flex && !other.get_flex())
208 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
209 const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
211 for(unsigned i=0; i<eps.size(); ++i)
213 Point epp = get_endpoint_position(i);
215 for(unsigned j=0; j<other_eps.size(); ++j)
217 if(other.get_link(j))
220 Point epp2 = other.get_endpoint_position(j);
221 float dx = epp2.x-epp.x;
222 float dy = epp2.y-epp.y;
223 float dz = epp2.z-epp.z;
224 if(dx*dx+dy*dy<limit && dz*dz<limit)
226 if(!link || (!flex && !other.get_flex()))
228 set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
229 Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
230 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
232 if(eps.size()==2 && i==1)
240 break_link(*links[i]);
242 other.links[j] = this;
243 layout.create_blocks(*this);
254 bool Track::snap(Point &pt, float &d) const
256 const vector<TrackType::Endpoint> &eps = type.get_endpoints();
258 for(unsigned i=0; i<eps.size(); ++i)
260 Point epp = get_endpoint_position(i);
261 float dx = pt.x-epp.x;
262 float dy = pt.y-epp.y;
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);
287 void Track::break_links()
289 for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
293 trk->break_link(*this);
297 Track *Track::get_link(unsigned i) const
300 throw InvalidParameterValue("Link index out of range");
305 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
307 TrackPoint p = type.get_point(epi, path, d);
311 p.pos = Point(pos.x+c*p.pos.x-s*p.pos.y, pos.y+s*p.pos.x+c*p.pos.y, pos.z);
313 if(type.get_endpoints().size()==2)
315 float len = type.get_path_length(path);
316 float grade = slope/len;
324 p.pos.z += slope-grade*d;
332 TrackPoint Track::get_point(unsigned epi, float d) const
334 return get_point(epi, active_path, d);
337 void Track::save(list<DataFile::Statement> &st) const
339 st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
340 st.push_back((DataFile::Statement("rotation"), rot));
341 st.push_back((DataFile::Statement("slope"), slope));
343 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
345 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
347 st.push_back((DataFile::Statement("flex"), true));
350 void Track::turnout_event(unsigned addr, bool state)
356 active_path = (active_path&2) | (state ? 1 : 0);
357 else if(type.is_double_address() && addr==turnout_id+1)
358 active_path = (active_path&1) | (state ? 2 : 0);
362 signal_path_changed.emit(active_path);
366 Track::Loader::Loader(Track &t):
367 DataFile::BasicLoader<Track>(t)
369 add("position", &Loader::position);
370 add("rotation", &Track::rot);
371 add("slope", &Track::slope);
372 add("turnout_id", &Loader::turnout_id);
373 add("sensor_id", &Loader::sensor_id);
374 add("flex", &Track::flex);
377 void Track::Loader::position(float x, float y, float z)
379 obj.pos = Point(x, y, z);
382 void Track::Loader::sensor_id(unsigned id)
384 obj.set_sensor_id(id);
387 void Track::Loader::turnout_id(unsigned id)
389 obj.set_turnout_id(id);