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):
21 float TrackType::get_total_length() const
23 return get_path_length(-1);
26 float TrackType::get_path_length(int p) const
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();
35 unsigned TrackType::get_paths() const
38 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
39 mask |= 1<<i->get_path();
43 unsigned TrackType::get_n_paths() const
46 for(unsigned mask = get_paths(); mask; ++n)
51 bool TrackType::is_turnout() const
53 return endpoints.size()>2;
56 TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
58 if(epi>=endpoints.size())
59 throw InvalidParameterValue("Endpoint index out of range");
61 const TrackPart *part = 0;
63 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
65 if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
68 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
69 for(unsigned j=0; j<n_part_eps; ++j)
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;
83 throw Exception("Internal error (endpoint does not match any part)");
87 float plen = part->get_length();
92 TrackPoint p = part->get_point(d);
100 TrackPart *next = part->get_link(1-part_ep);
102 throw InvalidParameterValue("Distance out of range");
103 part_ep = (next->get_link(0)==part ? 0 : 1);
109 void TrackType::collect_endpoints()
113 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
115 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
118 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
119 for(unsigned j=0; j<n_part_eps; ++j)
122 TrackPoint p = i->get_point(j ? i->get_length() : 0);
127 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
129 float dx = k->pos.x-p.pos.x;
130 float dy = k->pos.y-p.pos.y;
132 float da = k->dir-p.dir;
138 if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01)
140 k->paths |= 1<<i->get_path();
147 endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<<i->get_path()));
153 TrackType::Loader::Loader(TrackType &t):
154 Msp::DataFile::BasicLoader<TrackType>(t)
156 add("description", &TrackType::description);
157 add("double_address", &TrackType::double_address);
158 add("part", &Loader::part);
161 void TrackType::Loader::finish()
163 obj.collect_endpoints();
166 void TrackType::Loader::part()
170 obj.parts.push_back(p);
173 } // namespace Marklin