]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/track.cpp
Enable Renderer for Endpoint3D and Path3D
[r2c2.git] / source / libr2c2 / track.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include "block.h"
10 #include "catalogue.h"
11 #include "driver.h"
12 #include "layout.h"
13 #include "track.h"
14 #include "tracktype.h"
15
16 using namespace std;
17 using namespace Msp;
18
19 namespace R2C2 {
20
21 Track::Track(Layout &l, const TrackType &t):
22         layout(l),
23         type(t),
24         block(0),
25         rot(0),
26         slope(0),
27         flex(false),
28         turnout_id(0),
29         sensor_id(0),
30         links(type.get_endpoints().size()),
31         active_path(0)
32 {
33         if(type.is_turnout())
34                 turnout_id = layout.allocate_turnout_id();
35
36         layout.add_track(*this);
37
38         if(layout.has_driver())
39                 layout.get_driver().signal_turnout.connect(sigc::mem_fun(this, &Track::turnout_event));
40
41         for(unsigned paths = type.get_paths(); !(paths&1); ++active_path, paths>>=1) ;
42 }
43
44 Track::~Track()
45 {
46         break_links();
47         layout.remove_track(*this);
48 }
49
50 void Track::set_block(Block *b)
51 {
52         if(b && !b->has_track(*this))
53                 throw InvalidParameterValue("Track is not in the Block");
54         if(!b && block && block->has_track(*this))
55                 throw InvalidState("Track is still in a Block");
56
57         block = b;
58 }
59
60 Block &Track::get_block() const
61 {
62         if(!block)
63                 throw InvalidState("No Block");
64
65         return *block;
66 }
67
68 void Track::set_position(const Point &p)
69 {
70         pos = p;
71 }
72
73 void Track::set_rotation(float r)
74 {
75         rot = r;
76         while(rot<0)
77                 rot += M_PI*2;
78         while(rot>M_PI*2)
79                 rot -= M_PI*2;
80 }
81
82 void Track::set_slope(float s)
83 {
84         if(links.size()!=2)
85                 return;
86
87         slope = s;
88 }
89
90 void Track::set_flex(bool f)
91 {
92         flex = f;
93 }
94
95 void Track::check_slope()
96 {
97         if(links.size()!=2)
98                 return;
99
100         if(links[0] && links[1])
101         {
102                 Point epp0 = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
103                 Point epp1 = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
104                 pos.z = epp0.z;
105                 slope = epp1.z-pos.z;
106         }
107         else
108         {
109                 slope = 0;
110                 if(links[0])
111                 {
112                         Point epp = links[0]->get_endpoint_position(links[0]->get_endpoint_by_link(*this));
113                         pos.z = epp.z;
114                 }
115                 else if(links[1])
116                 {
117                         Point epp = links[1]->get_endpoint_position(links[1]->get_endpoint_by_link(*this));
118                         pos.z = epp.z;
119                 }
120         }
121 }
122
123 void Track::set_turnout_id(unsigned i)
124 {
125         if(!type.is_turnout())
126                 throw InvalidState("Not a turnout");
127
128         turnout_id = i;
129         layout.create_blocks(*this);
130         layout.update_routes();
131         if(layout.has_driver() && turnout_id)
132                 layout.get_driver().add_turnout(turnout_id, type);
133 }
134
135 void Track::set_sensor_id(unsigned i)
136 {
137         if(type.is_turnout())
138                 throw InvalidState("Can't set sensor on a turnout");
139
140         sensor_id = i;
141         layout.create_blocks(*this);
142         if(layout.has_driver() && sensor_id)
143                 layout.get_driver().add_sensor(sensor_id);
144 }
145
146 void Track::set_active_path(unsigned p)
147 {
148         if(!turnout_id)
149                 throw InvalidState("Not a turnout");
150         if(!(type.get_paths()&(1<<p)))
151                 throw InvalidParameterValue("Invalid path");
152
153         layout.get_driver().set_turnout(turnout_id, p);
154 }
155
156 int Track::get_endpoint_by_link(Track &other) const
157 {
158         for(unsigned i=0; i<links.size(); ++i)
159                 if(links[i]==&other)
160                         return i;
161
162         return -1;
163 }
164
165 Point Track::get_endpoint_position(unsigned epi) const
166 {
167         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
168         if(epi>=eps.size())
169                 throw InvalidParameterValue("TrackType::Endpoint index out of range");
170
171         const TrackType::Endpoint &ep = eps[epi];
172
173         float c = cos(rot);
174         float s = sin(rot);
175
176         Point p(pos.x+c*ep.pos.x-s*ep.pos.y, pos.y+s*ep.pos.x+c*ep.pos.y, pos.z);
177         if(eps.size()==2 && epi==1)
178                 p.z += slope;
179         return p;
180 }
181
182 float Track::get_endpoint_direction(unsigned epi) const
183 {
184         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
185         if(epi>=eps.size())
186                 throw InvalidParameterValue("TrackType::Endpoint index out of range");
187
188         const TrackType::Endpoint &ep = eps[epi];
189
190         return rot+ep.dir;
191 }
192
193 bool Track::snap_to(Track &other, bool link, float limit)
194 {
195         if(!limit || link)
196         {
197                 limit = layout.get_catalogue().get_gauge();
198                 if(link && !flex && !other.get_flex())
199                         limit /= 10;
200         }
201         limit *= limit;
202
203         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
204         const vector<TrackType::Endpoint> &other_eps = other.get_type().get_endpoints();
205
206         for(unsigned i=0; i<eps.size(); ++i)
207         {
208                 Point epp = get_endpoint_position(i);
209
210                 for(unsigned j=0; j<other_eps.size(); ++j)
211                 {
212                         if(other.get_link(j))
213                                 continue;
214
215                         Point epp2 = other.get_endpoint_position(j);
216                         float dx = epp2.x-epp.x;
217                         float dy = epp2.y-epp.y;
218                         float dz = epp2.z-epp.z;
219                         if(dx*dx+dy*dy<limit && dz*dz<limit)
220                         {
221                                 if(!link || (!flex && !other.get_flex()))
222                                 {
223                                         set_rotation(other.rot+other_eps[j].dir-eps[i].dir+M_PI);
224                                         Point p(epp2.x-(eps[i].pos.x*cos(rot)-eps[i].pos.y*sin(rot)),
225                                                 epp2.y-(eps[i].pos.y*cos(rot)+eps[i].pos.x*sin(rot)),
226                                                 epp2.z);
227                                         if(eps.size()==2 && i==1)
228                                                 p.z -= slope;
229                                         set_position(p);
230                                 }
231
232                                 if(link)
233                                 {
234                                         if(links[i])
235                                                 break_link(*links[i]);
236                                         links[i] = &other;
237                                         other.links[j] = this;
238                                         layout.create_blocks(*this);
239
240                                         signal_link_changed.emit(i, &other);
241                                         other.signal_link_changed.emit(j, this);
242                                 }
243
244                                 return true;
245                         }
246                 }
247         }
248
249         return false;
250 }
251
252 bool Track::snap(Point &pt, float &d) const
253 {
254         const vector<TrackType::Endpoint> &eps = type.get_endpoints();
255
256         for(unsigned i=0; i<eps.size(); ++i)
257         {
258                 Point epp = get_endpoint_position(i);
259                 float dx = pt.x-epp.x;
260                 float dy = pt.y-epp.y;
261                 if(dx*dx+dy*dy<1e-4)
262                 {
263                         pt = epp;
264                         d = rot+eps[i].dir;
265                         return true;
266                 }
267         }
268
269         return false;
270 }
271
272 void Track::break_link(Track &trk)
273 {
274         for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
275                 if(*i==&trk)
276                 {
277                         *i = 0;
278                         trk.break_link(*this);
279                         // XXX Creates the blocks twice
280                         layout.create_blocks(*this);
281                         signal_link_changed.emit(i-links.begin(), 0);
282                         return;
283                 }
284 }
285
286 void Track::break_links()
287 {
288         for(vector<Track *>::iterator i=links.begin(); i!=links.end(); ++i)
289                 if(Track *trk=*i)
290                 {
291                         *i = 0;
292                         trk->break_link(*this);
293                 }
294 }
295
296 Track *Track::get_link(unsigned i) const
297 {
298         if(i>links.size())
299                 throw InvalidParameterValue("Link index out of range");
300
301         return links[i];
302 }
303
304 TrackPoint Track::get_point(unsigned epi, unsigned path, float d) const
305 {
306         TrackPoint p = type.get_point(epi, path, d);
307         float c = cos(rot);
308         float s = sin(rot);
309
310         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);
311         p.dir += rot;
312         if(type.get_endpoints().size()==2)
313         {
314                 float len = type.get_path_length(path);
315                 float grade = slope/len;
316                 if(epi==0)
317                 {
318                         p.pos.z += grade*d;
319                         p.grade = grade;
320                 }
321                 else
322                 {
323                         p.pos.z += slope-grade*d;
324                         p.grade = -grade;
325                 }
326         }
327
328         return p;
329 }
330
331 TrackPoint Track::get_point(unsigned epi, float d) const
332 {
333         return get_point(epi, active_path, d);
334 }
335
336 void Track::save(list<DataFile::Statement> &st) const
337 {
338         st.push_back((DataFile::Statement("position"), pos.x, pos.y, pos.z));
339         st.push_back((DataFile::Statement("rotation"), rot));
340         st.push_back((DataFile::Statement("slope"), slope));
341         if(turnout_id)
342                 st.push_back((DataFile::Statement("turnout_id"), turnout_id));
343         if(sensor_id)
344                 st.push_back((DataFile::Statement("sensor_id"), sensor_id));
345         if(flex)
346                 st.push_back((DataFile::Statement("flex"), true));
347 }
348
349 void Track::turnout_event(unsigned addr, unsigned state)
350 {
351         if(!turnout_id)
352                 return;
353
354         if(addr==turnout_id)
355         {
356                 active_path = state;
357                 signal_path_changed.emit(active_path);
358         }
359 }
360
361
362 Track::Loader::Loader(Track &t):
363         DataFile::BasicLoader<Track>(t)
364 {
365         add("position",   &Loader::position);
366         add("rotation",   &Track::rot);
367         add("slope",      &Track::slope);
368         add("turnout_id", &Loader::turnout_id);
369         add("sensor_id",  &Loader::sensor_id);
370         add("flex",       &Track::flex);
371 }
372
373 void Track::Loader::position(float x, float y, float z)
374 {
375         obj.pos = Point(x, y, z);
376 }
377
378 void Track::Loader::sensor_id(unsigned id)
379 {
380         obj.set_sensor_id(id);
381 }
382
383 void Track::Loader::turnout_id(unsigned id)
384 {
385         obj.set_turnout_id(id);
386 }
387
388 } // namespace R2C2