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