2 #include <msp/geometry/union.h>
3 #include "trackappearance.h"
11 TrackType::TrackType(const ArticleNumber &an, const TrackAppearance &ta):
18 float TrackType::get_gauge() const
20 return appearance.get_gauge();
23 float TrackType::get_total_length() const
25 return get_path_length(-1);
28 float TrackType::get_path_length(int p) const
31 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
32 if(p<0 || i->get_path()==static_cast<unsigned>(p))
33 len += i->get_length();
37 unsigned TrackType::get_paths() const
40 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
41 mask |= 1<<i->get_path();
45 unsigned TrackType::get_n_paths() const
48 for(unsigned mask = get_paths(); mask; ++n)
53 unsigned TrackType::coerce_path(unsigned entry, unsigned path) const
55 const Endpoint &ep = get_endpoint(entry);
59 unsigned paths = get_paths();
60 if(paths>>(1<<state_bits))
62 /* There are more paths than can be expressed with state_bits, so
63 multiple paths are set at once. See if one of the alternatives fits. */
64 unsigned step = 1<<state_bits;
65 for(unsigned p=path+step; paths>>p; p+=step)
70 // Find an endpoint that's connected to the entry and has the requested path
71 for(vector<Endpoint>::const_iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
72 if(i->has_path(path) && i->has_common_paths(ep))
75 for(unsigned m=i->paths&ep.paths; m>>p; ++p) ;
79 // TODO crossings fall here
80 throw logic_error("TrackType::coerce_path");
83 bool TrackType::is_turnout() const
85 return endpoints.size()>2;
88 bool TrackType::is_dead_end() const
90 return endpoints.size()<2;
93 const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const
95 if(i>=endpoints.size())
96 throw out_of_range("TrackType::get_endpoint");
101 OrientedPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
103 if(epi>=endpoints.size())
104 throw out_of_range("TrackType::get_point");
106 const TrackPart *part = 0;
107 unsigned part_ep = 0;
108 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
110 if(endpoints[epi].has_path(path) && i->get_path()!=path)
113 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
114 for(unsigned j=0; j<n_part_eps; ++j)
116 OrientedPoint p = i->get_point(j ? i->get_length() : 0);
117 Vector span = p.position-endpoints[epi].pos;
118 if(dot(span, span)<1e-6)
127 throw logic_error("internal error (endpoint does not match any part)");
131 float plen = part->get_length();
136 OrientedPoint p = part->get_point(d);
138 p.rotation += Angle::half_turn();
144 TrackPart *next = part->get_link(1-part_ep);
146 throw invalid_argument("TrackType::get_point");
147 part_ep = (next->get_link(0)==part ? 0 : 1);
153 OrientedPoint TrackType::get_nearest_point(const Vector &p) const
155 OrientedPoint result;
158 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
160 OrientedPoint n = i->get_nearest_point(p);
161 float d = distance(n.position, p);
172 void TrackType::collect_endpoints()
176 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
178 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
181 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
182 for(unsigned j=0; j<n_part_eps; ++j)
185 OrientedPoint p = i->get_point(j ? i->get_length() : 0);
187 p.rotation += Angle::half_turn();
190 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
192 Vector d = k->pos-p.position;
194 Angle da = wrap_balanced(k->dir-p.rotation);
196 if(dot(d, d)<1e-6 && abs(da).radians()<0.01)
198 k->paths |= 1<<i->get_path();
205 endpoints.push_back(Endpoint(p.position.x, p.position.y, p.rotation, 1<<i->get_path()));
210 TrackType::Endpoint::Endpoint(float x, float y, const Angle &d, unsigned p):
217 TrackType::Loader::Loader(TrackType &t):
218 DataFile::DerivedObjectLoader<TrackType, ObjectType::Loader>(t),
219 state_bits_set(false)
221 add("autofit_preference", &TrackType::autofit_preference);
222 add("object", &TrackType::object);
223 add("state_bits", &Loader::state_bits);
224 add("part", &Loader::part);
227 void TrackType::Loader::finish()
229 obj.collect_endpoints();
230 vector<const Shape *> shapes;
231 for(vector<TrackPart>::iterator i=obj.parts.begin(); i!=obj.parts.end(); ++i)
234 shapes.push_back(&i->get_shape());
236 obj.shape = Geometry::Union<float, 3>::from_iterator_range(shapes.begin(), shapes.end()).clone();
239 void TrackType::Loader::part()
243 obj.parts.push_back(p);
244 if(!state_bits_set && p.get_path())
245 while(p.get_path()>=(1U<<obj.state_bits))
249 void TrackType::Loader::state_bits(unsigned b)
252 state_bits_set = true;