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