]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/tracktype.cpp
53c2bc47844c862e0a3b07db63f94aa71a37e041
[r2c2.git] / source / libmarklin / tracktype.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include "tracktype.h"
10
11 using namespace std;
12 using namespace Msp;
13
14 namespace Marklin {
15
16 TrackType::TrackType(unsigned a):
17         art_nr(a),
18         double_address(false),
19         autofit_preference(1)
20 { }
21
22 float TrackType::get_total_length() const
23 {
24         return get_path_length(-1);
25 }
26
27 float TrackType::get_path_length(int p) const
28 {
29         float len = 0;
30         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
31                 if(p<0 || i->get_path()==static_cast<unsigned>(p))
32                         len += i->get_length();
33         return len;
34 }
35
36 unsigned TrackType::get_paths() const
37 {
38         unsigned mask = 0;
39         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
40                 mask |= 1<<i->get_path();
41         return mask;
42 }
43
44 unsigned TrackType::get_n_paths() const
45 {
46         unsigned n = 0;
47         for(unsigned mask = get_paths(); mask; ++n)
48                 mask &= mask-1;
49         return n;
50 }
51
52 bool TrackType::is_turnout() const
53 {
54         return endpoints.size()>2;
55 }
56
57 bool TrackType::is_dead_end() const
58 {
59         return endpoints.size()<2;
60 }
61
62 TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
63 {
64         if(epi>=endpoints.size())
65                 throw InvalidParameterValue("Endpoint index out of range");
66
67         const TrackPart *part = 0;
68         unsigned part_ep = 0;
69         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
70         {
71                 if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
72                         continue;
73
74                 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
75                 for(unsigned j=0; j<n_part_eps; ++j)
76                 {
77                         TrackPoint p = i->get_point(j ? i->get_length() : 0);
78                         float dx = p.pos.x-endpoints[epi].pos.x;
79                         float dy = p.pos.y-endpoints[epi].pos.y;
80                         if(dx*dx+dy*dy<1e-6)
81                         {
82                                 part = &*i;
83                                 part_ep = j;
84                         }
85                 }
86         }
87
88         if(!part)
89                 throw Exception("Internal error (endpoint does not match any part)");
90
91         while(1)
92         {
93                 float plen = part->get_length();
94                 if(d<=plen)
95                 {
96                         if(part_ep==1)
97                                 d = plen-d;
98                         TrackPoint p = part->get_point(d);
99                         if(part_ep==1)
100                                 p.dir += M_PI;
101                         return p;
102                 }
103                 else
104                 {
105                         d -= plen;
106                         TrackPart *next = part->get_link(1-part_ep);
107                         if(!next)
108                                 throw InvalidParameterValue("Distance out of range");
109                         part_ep = (next->get_link(0)==part ? 0 : 1);
110                         part = next;
111                 }
112         }
113 }
114
115 void TrackType::collect_endpoints()
116 {
117         endpoints.clear();
118
119         for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
120         {
121                 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
122                         i->check_link(*j);
123
124                 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
125                 for(unsigned j=0; j<n_part_eps; ++j)
126                         if(!i->get_link(j))
127                         {
128                                 TrackPoint p = i->get_point(j ? i->get_length() : 0);
129                                 if(j==0)
130                                         p.dir += M_PI;
131
132                                 bool found = false;
133                                 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
134                                 {
135                                         float dx = k->pos.x-p.pos.x;
136                                         float dy = k->pos.y-p.pos.y;
137
138                                         float da = k->dir-p.dir;
139                                         while(da>M_PI)
140                                                 da -= M_PI*2;
141                                         while(da<-M_PI)
142                                                 da += M_PI*2;
143
144                                         if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01)
145                                         {
146                                                 k->paths |= 1<<i->get_path();
147                                                 found = true;
148                                                 break;
149                                         }
150                                 }
151
152                                 if(!found)
153                                         endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<<i->get_path()));
154                         }
155         }
156 }
157
158 TrackType::Endpoint::Endpoint(float x, float y, float d, unsigned p):
159         pos(x, y),
160         dir(d),
161         paths(p)
162 { }
163
164
165 TrackType::Loader::Loader(TrackType &t):
166         Msp::DataFile::BasicLoader<TrackType>(t)
167 {
168         add("autofit_preference", &TrackType::autofit_preference);
169         add("description", &TrackType::description);
170         add("double_address", &TrackType::double_address);
171         add("part",        &Loader::part);
172 }
173
174 void TrackType::Loader::finish()
175 {
176         obj.collect_endpoints();
177 }
178
179 void TrackType::Loader::part()
180 {
181         TrackPart p;
182         load_sub(p);
183         obj.parts.push_back(p);
184 }
185
186 } // namespace Marklin