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 bool TrackType::is_turnout() const
49 return endpoints.size()>2;
52 bool TrackType::is_dead_end() const
54 return endpoints.size()<2;
57 const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const
59 if(i>=endpoints.size())
60 throw out_of_range("TrackType::get_endpoint");
65 OrientedPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
67 if(epi>=endpoints.size())
68 throw out_of_range("TrackType::get_point");
70 const TrackPart *part = 0;
72 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
74 if(endpoints[epi].has_path(path) && i->get_path()!=path)
77 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
78 for(unsigned j=0; j<n_part_eps; ++j)
80 OrientedPoint p = i->get_point(j ? i->get_length() : 0);
81 Vector span = p.position-endpoints[epi].pos;
82 if(dot(span, span)<1e-6)
91 throw logic_error("internal error (endpoint does not match any part)");
95 float plen = part->get_length();
100 OrientedPoint p = part->get_point(d);
102 p.rotation += Angle::half_turn();
108 TrackPart *next = part->get_link(1-part_ep);
110 throw invalid_argument("TrackType::get_point");
111 part_ep = (next->get_link(0)==part ? 0 : 1);
117 OrientedPoint TrackType::get_nearest_point(const Vector &p) const
119 OrientedPoint result;
122 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
124 OrientedPoint n = i->get_nearest_point(p);
125 float d = distance(n.position, p);
136 void TrackType::collect_endpoints()
140 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
142 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
145 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
146 for(unsigned j=0; j<n_part_eps; ++j)
149 OrientedPoint p = i->get_point(j ? i->get_length() : 0);
151 p.rotation += Angle::half_turn();
154 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
156 Vector d = k->pos-p.position;
158 Angle da = wrap_balanced(k->dir-p.rotation);
160 if(dot(d, d)<1e-6 && abs(da).radians()<0.01)
162 k->paths |= 1<<i->get_path();
169 endpoints.push_back(Endpoint(p.position.x, p.position.y, p.rotation, 1<<i->get_path()));
174 TrackType::Endpoint::Endpoint(float x, float y, const Angle &d, unsigned p):
181 TrackType::Loader::Loader(TrackType &t):
182 DataFile::DerivedObjectLoader<TrackType, ObjectType::Loader>(t),
183 state_bits_set(false)
185 add("autofit_preference", &TrackType::autofit_preference);
186 add("object", &TrackType::object);
187 add("state_bits", &Loader::state_bits);
188 add("part", &Loader::part);
191 void TrackType::Loader::finish()
193 obj.collect_endpoints();
194 vector<const Shape *> shapes;
195 for(vector<TrackPart>::iterator i=obj.parts.begin(); i!=obj.parts.end(); ++i)
198 shapes.push_back(&i->get_shape());
200 obj.shape = Geometry::Union<float, 3>::from_iterator_range(shapes.begin(), shapes.end()).clone();
203 void TrackType::Loader::part()
207 obj.parts.push_back(p);
208 if(!state_bits_set && p.get_path())
209 while(p.get_path()>=(1U<<obj.state_bits))
213 void TrackType::Loader::state_bits(unsigned b)
216 state_bits_set = true;