2 #include <msp/geometry/union.h>
10 TrackType::TrackType(const ArticleNumber &an, const TrackAppearance &ta):
17 float TrackType::get_total_length() const
19 return get_path_length(-1);
22 float TrackType::get_path_length(int p) const
25 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
26 if(p<0 || i->get_path()==static_cast<unsigned>(p))
27 len += i->get_length();
31 unsigned TrackType::get_paths() const
34 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
35 mask |= 1<<i->get_path();
39 unsigned TrackType::get_n_paths() const
42 for(unsigned mask = get_paths(); mask; ++n)
47 unsigned TrackType::coerce_path(unsigned entry, unsigned path) const
49 const Endpoint &ep = get_endpoint(entry);
53 unsigned paths = get_paths();
54 if(paths>>(1<<state_bits))
56 /* There are more paths than can be expressed with state_bits, so
57 multiple paths are set at once. See if one of the alternatives fits. */
58 unsigned step = 1<<state_bits;
59 for(unsigned p=path+step; paths>>p; p+=step)
64 // Find an endpoint that's connected to the entry and has the requested path
65 for(vector<Endpoint>::const_iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
66 if(i->has_path(path) && i->has_common_paths(ep))
69 for(unsigned m=i->paths&ep.paths; m>>p; ++p) ;
73 // TODO crossings fall here
74 throw logic_error("TrackType::coerce_path");
77 bool TrackType::is_turnout() const
79 return endpoints.size()>2;
82 bool TrackType::is_dead_end() const
84 return endpoints.size()<2;
87 const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const
89 if(i>=endpoints.size())
90 throw out_of_range("TrackType::get_endpoint");
95 OrientedPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
97 if(epi>=endpoints.size())
98 throw out_of_range("TrackType::get_point");
100 const TrackPart *part = 0;
101 unsigned part_ep = 0;
102 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
104 if(endpoints[epi].has_path(path) && i->get_path()!=path)
107 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
108 for(unsigned j=0; j<n_part_eps; ++j)
110 OrientedPoint p = i->get_point(j ? i->get_length() : 0);
111 Vector span = p.position-endpoints[epi].pos;
112 if(dot(span, span)<1e-6)
121 throw logic_error("internal error (endpoint does not match any part)");
125 float plen = part->get_length();
130 OrientedPoint p = part->get_point(d);
132 p.rotation += Angle::half_turn();
138 TrackPart *next = part->get_link(1-part_ep);
140 throw invalid_argument("TrackType::get_point");
141 part_ep = (next->get_link(0)==part ? 0 : 1);
147 OrientedPoint TrackType::get_nearest_point(const Vector &p) const
149 OrientedPoint result;
152 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
154 OrientedPoint n = i->get_nearest_point(p);
155 float d = distance(n.position, p);
166 void TrackType::collect_endpoints()
170 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
172 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
175 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
176 for(unsigned j=0; j<n_part_eps; ++j)
179 OrientedPoint p = i->get_point(j ? i->get_length() : 0);
181 p.rotation += Angle::half_turn();
184 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
186 Vector d = k->pos-p.position;
188 Angle da = wrap_balanced(k->dir-p.rotation);
190 if(dot(d, d)<1e-6 && abs(da).radians()<0.01)
192 k->paths |= 1<<i->get_path();
199 endpoints.push_back(Endpoint(p.position.x, p.position.y, p.rotation, 1<<i->get_path()));
204 TrackType::Endpoint::Endpoint(float x, float y, const Angle &d, unsigned p):
211 TrackType::Loader::Loader(TrackType &t):
212 DataFile::DerivedObjectLoader<TrackType, ObjectType::Loader>(t),
213 state_bits_set(false)
215 add("autofit_preference", &TrackType::autofit_preference);
216 add("object", &TrackType::object);
217 add("state_bits", &Loader::state_bits);
218 add("part", &Loader::part);
221 void TrackType::Loader::finish()
223 obj.collect_endpoints();
224 vector<const Shape *> shapes;
225 for(vector<TrackPart>::iterator i=obj.parts.begin(); i!=obj.parts.end(); ++i)
228 shapes.push_back(&i->get_shape());
230 obj.shape = Geometry::Union<float, 3>::from_iterator_range(shapes.begin(), shapes.end()).clone();
233 void TrackType::Loader::part()
237 obj.parts.push_back(p);
238 if(!state_bits_set && p.get_path())
239 while(p.get_path()>=(1U<<obj.state_bits))
243 void TrackType::Loader::state_bits(unsigned b)
246 state_bits_set = true;