2 #include <msp/datafile/collection.h>
3 #include <msp/geometry/union.h>
4 #include "trackappearance.h"
12 TrackType::TrackType():
18 const TrackAppearance &TrackType::get_appearance() const
21 throw logic_error("no appearance");
25 float TrackType::get_gauge() const
27 return get_appearance().get_gauge();
30 float TrackType::get_total_length() const
32 return get_path_length(-1);
35 float TrackType::get_path_length(int p) const
38 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
39 if(p<0 || i->get_path()==static_cast<unsigned>(p))
40 len += i->get_length();
44 unsigned TrackType::get_paths() const
47 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
48 mask |= 1<<i->get_path();
52 unsigned TrackType::get_n_paths() const
55 for(unsigned mask = get_paths(); mask; ++n)
60 unsigned TrackType::coerce_path(unsigned entry, unsigned path) const
62 const Endpoint &ep = get_endpoint(entry);
66 unsigned paths = get_paths();
67 if(paths>>(1<<state_bits))
69 /* There are more paths than can be expressed with state_bits, so
70 multiple paths are set at once. See if one of the alternatives fits. */
71 unsigned step = 1<<state_bits;
72 for(unsigned p=path+step; paths>>p; p+=step)
77 // Find an endpoint that's connected to the entry and has the requested path
78 for(vector<Endpoint>::const_iterator i=endpoints.begin(); i!=endpoints.end(); ++i)
79 if(i->has_path(path) && i->has_common_paths(ep))
82 for(unsigned m=i->paths&ep.paths; m>>p; ++p) ;
86 // TODO crossings fall here
87 throw logic_error("TrackType::coerce_path");
90 bool TrackType::is_turnout() const
92 return endpoints.size()>2;
95 bool TrackType::is_dead_end() const
97 return endpoints.size()<2;
100 const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const
102 if(i>=endpoints.size())
103 throw out_of_range("TrackType::get_endpoint");
108 OrientedPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
110 if(epi>=endpoints.size())
111 throw out_of_range("TrackType::get_point");
113 const TrackPart *part = 0;
114 unsigned part_ep = 0;
115 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
117 if(endpoints[epi].has_path(path) && i->get_path()!=path)
120 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
121 for(unsigned j=0; j<n_part_eps; ++j)
123 OrientedPoint p = i->get_point(j ? i->get_length() : 0);
124 Vector span = p.position-endpoints[epi].pos;
125 if(dot(span, span)<1e-6)
134 throw logic_error("internal error (endpoint does not match any part)");
138 float plen = part->get_length();
143 OrientedPoint p = part->get_point(d);
145 p.rotation += Angle::half_turn();
151 TrackPart *next = part->get_link(1-part_ep);
153 throw invalid_argument("TrackType::get_point");
154 part_ep = (next->get_link(0)==part ? 0 : 1);
160 OrientedPoint TrackType::get_nearest_point(const Vector &p) const
162 OrientedPoint result;
165 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
167 OrientedPoint n = i->get_nearest_point(p);
168 float d = distance(n.position, p);
179 void TrackType::collect_endpoints()
183 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
185 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
188 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
189 for(unsigned j=0; j<n_part_eps; ++j)
192 OrientedPoint p = i->get_point(j ? i->get_length() : 0);
194 p.rotation += Angle::half_turn();
197 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
199 Vector d = k->pos-p.position;
201 Angle da = wrap_balanced(k->dir-p.rotation);
203 if(dot(d, d)<1e-6 && abs(da).radians()<0.01)
205 k->paths |= 1<<i->get_path();
212 endpoints.push_back(Endpoint(p.position.x, p.position.y, p.rotation, 1<<i->get_path()));
217 TrackType::Endpoint::Endpoint(float x, float y, const Angle &d, unsigned p):
224 TrackType::Loader::Loader(TrackType &t, Collection &c):
225 DataFile::DerivedObjectLoader<TrackType, ObjectType::Loader>(t),
227 state_bits_set(false)
229 add("appearance", &TrackType::appearance);
230 add("autofit_preference", &TrackType::autofit_preference);
231 add("object", &TrackType::object);
232 add("state_bits", &Loader::state_bits);
233 add("part", &Loader::part);
236 void TrackType::Loader::finish()
238 obj.collect_endpoints();
239 vector<const Shape *> shapes;
240 for(vector<TrackPart>::iterator i=obj.parts.begin(); i!=obj.parts.end(); ++i)
243 shapes.push_back(&i->get_shape());
245 obj.shape = Geometry::Union<float, 3>::from_iterator_range(shapes.begin(), shapes.end()).clone();
248 void TrackType::Loader::part()
252 obj.parts.push_back(p);
253 if(!state_bits_set && p.get_path())
254 while(p.get_path()>=(1U<<obj.state_bits))
258 void TrackType::Loader::state_bits(unsigned b)
261 state_bits_set = true;