]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/track.cpp
Use event-based matrix updates for objects
[r2c2.git] / source / libr2c2 / track.cpp
1 #include <cmath>
2 #include "block.h"
3 #include "catalogue.h"
4 #include "driver.h"
5 #include "layout.h"
6 #include "track.h"
7 #include "tracktype.h"
8
9 using namespace std;
10 using namespace Msp;
11
12 namespace R2C2 {
13
14 Track::Track(Layout &l, const TrackType &t):
15         Object(l),
16         type(t),
17         block(0),
18         slope(0),
19         flex(false),
20         turnout_id(0),
21         sensor_id(0),
22         links(type.get_endpoints().size()),
23         active_path(0),
24         path_changing(false)
25 {
26         if(type.is_turnout())
27                 turnout_id = layout.allocate_turnout_id();
28
29         layout.add(*this);
30
31         if(layout.has_driver())
32                 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
33
34         for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
35 }
36
37 Track::~Track()
38 {
39         break_links();
40         layout.remove(*this);
41 }
42
43 Track *Track::clone(Layout *to_layout) const
44 {
45         Track *track = new Track((to_layout ? *to_layout : layout), type);
46         track->set_position(position);
47         track->set_rotation(rotation);
48         return track;
49 }
50
51 void Track::set_block(Block *b)
52 {
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");
57
58         block = b;
59 }
60
61 Block &Track::get_block() const
62 {
63         if(!block)
64                 throw logic_error("!block");
65
66         return *block;
67 }
68
69 void Track::set_position(const Vector &p)
70 {
71         position = p;
72         signal_moved.emit();
73         propagate_slope();
74 }
75
76 void Track::set_rotation(const Angle &r)
77 {
78         rotation = wrap_positive(r);
79         signal_moved.emit();
80 }
81
82 void Track::set_tilt(const Angle &t)
83 {
84         if(links.size()!=2)
85                 return;
86
87         tilt = t;
88         slope = tan(tilt)*type.get_path_length(0);
89         signal_moved.emit();
90         propagate_slope();
91 }
92
93 void Track::set_flex(bool f)
94 {
95         flex = f;
96 }
97
98 void Track::propagate_slope()
99 {
100         for(vector<Track *>::const_iterator i=links.begin(); i!=links.end(); ++i)
101                 if(*i)
102                         (*i)->check_slope();
103 }
104
105 void Track::check_slope()
106 {
107         if(links.size()!=2)
108                 return;
109
110         if(links[0] && links[1])
111         {
112                 Vector epp0 = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
113                 Vector epp1 = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
114                 position.z = epp0.z;
115                 slope = epp1.z-position.z;
116                 tilt = Geometry::atan(slope/type.get_path_length(0));
117         }
118         else
119         {
120                 if(links[0])
121                 {
122                         Vector epp = links[0]->get_snap_node(links[0]->get_link_slot(*this)).position;
123                         position.z = epp.z;
124                 }
125                 else if(links[1])
126                 {
127                         Vector epp = links[1]->get_snap_node(links[1]->get_link_slot(*this)).position;
128                         position.z = epp.z-slope;
129                 }
130         }
131
132         signal_moved.emit();
133 }
134
135 void Track::set_turnout_id(unsigned i)
136 {
137         if(!type.is_turnout())
138                 throw logic_error("not a turnout");
139         if(!i)
140                 throw invalid_argument("Track::set_turnout_id");
141
142         turnout_id = i;
143         layout.create_blocks(*this);
144         layout.update_routes();
145         if(layout.has_driver() && turnout_id)
146                 layout.get_driver().add_turnout(turnout_id, type);
147 }
148
149 void Track::set_sensor_id(unsigned i)
150 {
151         if(type.is_turnout())
152                 throw logic_error("is a turnout");
153
154         sensor_id = i;
155         layout.create_blocks(*this);
156         if(layout.has_driver() && sensor_id)
157                 layout.get_driver().add_sensor(sensor_id);
158 }
159
160 void Track::set_active_path(unsigned p)
161 {
162         if(!turnout_id)
163                 throw logic_error("not a turnout");
164         if(!(type.get_paths()&(1<<p)))
165                 throw invalid_argument("Track::set_active_path");
166
167         signal_path_changing(p);
168         path_changing = true;
169         layout.get_driver().set_turnout(turnout_id, p);
170 }
171
172 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
173 {
174         TrackPoint p = type.get_point(epi, path, d);
175
176         p.pos = position+rotated_vector(p.pos, rotation);
177         p.dir += rotation;
178         if(type.get_endpoints().size()==2)
179         {
180                 float grade = tan(tilt);
181                 if(epi==0)
182                 {
183                         p.pos.z += grade*d;
184                         p.grade = grade;
185                 }
186                 else
187                 {
188                         p.pos.z += slope-grade*d;
189                         p.grade = -grade;
190                 }
191         }
192
193         return p;
194 }
195
196 TrackPoint Track::get_point(unsigned epi, float d) const
197 {
198         return get_point(epi, active_path, d);
199 }
200
201 unsigned Track::get_n_snap_nodes() const
202 {
203         return type.get_endpoints().size();
204 }
205
206 Snap Track::get_snap_node(unsigned i) const
207 {
208         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
209         if(i>=eps.size())
210                 throw out_of_range("Track::get_snap_node");
211
212         Snap result;
213         const TrackType::Endpoint &ep = eps[i];
214
215         result.position = position+rotated_vector(ep.pos, rotation);
216         if(eps.size()==2 && i==1)
217                 result.position.z += slope;
218
219         result.rotation = rotation+ep.dir;
220
221         return result;
222 }
223
224 bool Track::snap(Snap &sn, float limit, SnapType what) const
225 {
226         if(Object::snap(sn, limit, what))
227                 return true;
228
229         if(what&SNAP_SEGMENT)
230         {
231                 Vector local = rotated_vector(sn.position-position, -rotation);
232
233                 TrackPoint tp = type.get_nearest_point(local);
234                 Vector span = local-tp.pos;
235                 if(dot(span, span)<=limit*limit)
236                 {
237                         sn.position = position+rotated_vector(tp.pos, rotation);
238                         sn.rotation = tp.dir+rotation;
239                         return true;
240                 }
241         }
242
243         return false;
244 }
245
246 SnapType Track::get_default_snap_type_to(const Object &other) const
247 {
248         if(dynamic_cast<const Track *>(&other))
249                 return SNAP_NODE;
250
251         return NO_SNAP;
252 }
253
254 unsigned Track::get_n_link_slots() const
255 {
256         return links.size();
257 }
258
259 Track *Track::get_link(unsigned i) const
260 {
261         if(i>=links.size())
262                 throw out_of_range("Track::get_link");
263
264         return links[i];
265 }
266
267 int Track::get_link_slot(const Object &other) const
268 {
269         for(unsigned i=0; i<links.size(); ++i)
270                 if(links[i]==&other)
271                         return i;
272
273         return -1;
274 }
275
276 bool Track::link_to(Object &other)
277 {
278         Track *otrack = dynamic_cast<Track *>(&other);
279         if(!otrack)
280                 return false;
281
282         float limit = layout.get_catalogue().get_gauge();
283         if(!flex && !otrack->get_flex())
284                 limit /= 10;
285         limit *= limit;
286
287         unsigned nsn = get_n_snap_nodes();
288         unsigned other_nsn = other.get_n_snap_nodes();
289         for(unsigned i=0; i<nsn; ++i)
290         {
291                 Snap sn = get_snap_node(i);
292                 for(unsigned j=0; j<other_nsn; ++j)
293                 {
294                         Snap osn = other.get_snap_node(j);
295                         Vector span = osn.position-sn.position;
296                         Angle da = wrap_balanced(osn.rotation-sn.rotation-Angle::half_turn());
297
298                         if(dot(span, span)<limit && abs(da).radians()<0.01)
299                         {
300                                 break_link(i);
301                                 links[i] = otrack;
302                                 otrack->links[j] = this;
303                                 check_slope();
304                                 layout.create_blocks(*this);
305
306                                 signal_link_changed.emit(i, otrack);
307                                 otrack->signal_link_changed.emit(j, this);
308                                 return true;
309                         }
310                 }
311         }
312
313         return false;
314 }
315
316 bool Track::break_link(unsigned i)
317 {
318         if(i>=links.size())
319                 throw out_of_range("Track::break_link");
320
321         Track *other = links[i];
322         if(!other)
323                 return false;
324
325         links[i] = 0;
326         other->break_link(*this);
327         // XXX Creates the blocks twice, because the other track calls this too
328         layout.create_blocks(*this);
329         signal_link_changed.emit(i, 0);
330
331         return true;
332 }
333
334 void Track::save(list<DataFile::Statement> &st) const
335 {
336         st.push_back((DataFile::Statement("position"), position.x, position.y, position.z));
337         st.push_back((DataFile::Statement("rotation"), rotation.radians()));
338         st.push_back((DataFile::Statement("tilt"), tilt.radians()));
339         if(turnout_id)
340                 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
341         if(sensor_id)
342                 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
343         if(flex)
344                 st.push_back((DataFile::Statement("flex"), true));
345 }
346
347 void Track::turnout_event(unsigned addr, unsigned state)
348 {
349         if(!turnout_id)
350                 return;
351
352         if(addr==turnout_id)
353         {
354                 active_path = state;
355                 path_changing = false;
356                 signal_path_changed.emit(active_path);
357         }
358 }
359
360
361 Track::Loader::Loader(Track &t):
362         DataFile::ObjectLoader<Track>(t)
363 {
364         add("position",   &Loader::position);
365         add("rotation",   &Loader::rotation);
366         add("tilt",       &Loader::tilt);
367         add("turnout_id", &Loader::turnout_id);
368         add("sensor_id",  &Loader::sensor_id);
369         add("flex",       &Track::flex);
370
371         // deprecated
372         add("slope",      &Loader::slope);
373 }
374
375 void Track::Loader::position(float x, float y, float z)
376 {
377         obj.set_position(Vector(x, y, z));
378 }
379
380 void Track::Loader::rotation(float r)
381 {
382         obj.set_rotation(Angle::from_radians(r));
383 }
384
385 void Track::Loader::sensor_id(unsigned id)
386 {
387         obj.set_sensor_id(id);
388 }
389
390 void Track::Loader::slope(float s)
391 {
392         obj.set_tilt(Geometry::atan(s/obj.type.get_path_length(0)));
393 }
394
395 void Track::Loader::tilt(float t)
396 {
397         obj.set_tilt(Angle::from_radians(t));
398 }
399
400 void Track::Loader::turnout_id(unsigned id)
401 {
402         obj.set_turnout_id(id);
403 }
404
405 } // namespace R2C2