3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
16 TrackType::TrackType(unsigned a):
20 float TrackType::get_total_length() const
22 return get_path_length(-1);
25 float TrackType::get_path_length(int p) const
28 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
29 if(p<0 || i->get_path()==static_cast<unsigned>(p))
30 len += i->get_length();
34 unsigned TrackType::get_paths() const
37 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
38 mask |= 1<<i->get_path();
42 unsigned TrackType::get_n_paths() const
45 for(unsigned mask = get_paths(); mask; ++n)
50 bool TrackType::is_turnout() const
52 return endpoints.size()>2;
55 bool TrackType::is_double_address() const
57 return get_n_paths()>2;
60 TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
62 if(epi>=endpoints.size())
63 throw InvalidParameterValue("Endpoint index out of range");
65 const TrackPart *part = 0;
67 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
69 if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
72 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
73 for(unsigned j=0; j<n_part_eps; ++j)
75 TrackPoint p = i->get_point(j ? i->get_length() : 0);
76 float dx = p.pos.x-endpoints[epi].pos.x;
77 float dy = p.pos.y-endpoints[epi].pos.y;
87 throw Exception("Internal error (endpoint does not match any part)");
91 float plen = part->get_length();
96 TrackPoint p = part->get_point(d);
104 TrackPart *next = part->get_link(1-part_ep);
106 throw InvalidParameterValue("Distance out of range");
107 part_ep = (next->get_link(0)==part ? 0 : 1);
113 void TrackType::collect_endpoints()
117 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
119 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
122 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
123 for(unsigned j=0; j<n_part_eps; ++j)
126 TrackPoint p = i->get_point(j ? i->get_length() : 0);
131 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
133 float dx = k->pos.x-p.pos.x;
134 float dy = k->pos.y-p.pos.y;
136 float da = k->dir-p.dir;
142 if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01)
144 k->paths |= 1<<i->get_path();
151 endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<<i->get_path()));
157 TrackType::Loader::Loader(TrackType &t):
158 Msp::DataFile::BasicLoader<TrackType>(t)
160 add("description", &TrackType::description);
161 add("part", &Loader::part);
164 void TrackType::Loader::finish()
166 obj.collect_endpoints();
169 void TrackType::Loader::part()
173 obj.parts.push_back(p);
176 } // namespace Marklin