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):
18 double_address(false),
22 float TrackType::get_total_length() const
24 return get_path_length(-1);
27 float TrackType::get_path_length(int p) const
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();
36 unsigned TrackType::get_paths() const
39 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
40 mask |= 1<<i->get_path();
44 unsigned TrackType::get_n_paths() const
47 for(unsigned mask = get_paths(); mask; ++n)
52 bool TrackType::is_turnout() const
54 return endpoints.size()>2;
57 bool TrackType::is_dead_end() const
59 return endpoints.size()<2;
62 TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
64 if(epi>=endpoints.size())
65 throw InvalidParameterValue("Endpoint index out of range");
67 const TrackPart *part = 0;
69 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
71 if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
74 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
75 for(unsigned j=0; j<n_part_eps; ++j)
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;
89 throw Exception("Internal error (endpoint does not match any part)");
93 float plen = part->get_length();
98 TrackPoint p = part->get_point(d);
106 TrackPart *next = part->get_link(1-part_ep);
108 throw InvalidParameterValue("Distance out of range");
109 part_ep = (next->get_link(0)==part ? 0 : 1);
115 void TrackType::collect_endpoints()
119 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
121 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
124 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
125 for(unsigned j=0; j<n_part_eps; ++j)
128 TrackPoint p = i->get_point(j ? i->get_length() : 0);
133 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
135 float dx = k->pos.x-p.pos.x;
136 float dy = k->pos.y-p.pos.y;
138 float da = k->dir-p.dir;
144 if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01)
146 k->paths |= 1<<i->get_path();
153 endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<<i->get_path()));
158 TrackType::Endpoint::Endpoint(float x, float y, float d, unsigned p):
165 TrackType::Loader::Loader(TrackType &t):
166 Msp::DataFile::BasicLoader<TrackType>(t)
168 add("autofit_preference", &TrackType::autofit_preference);
169 add("description", &TrackType::description);
170 add("double_address", &TrackType::double_address);
171 add("part", &Loader::part);
174 void TrackType::Loader::finish()
176 obj.collect_endpoints();
179 void TrackType::Loader::part()
183 obj.parts.push_back(p);
186 } // namespace Marklin