9 TrackType::TrackType(const ArticleNumber &an):
15 float TrackType::get_total_length() const
17 return get_path_length(-1);
20 float TrackType::get_path_length(int p) const
23 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
24 if(p<0 || i->get_path()==static_cast<unsigned>(p))
25 len += i->get_length();
29 unsigned TrackType::get_paths() const
32 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
33 mask |= 1<<i->get_path();
37 unsigned TrackType::get_n_paths() const
40 for(unsigned mask = get_paths(); mask; ++n)
45 bool TrackType::is_turnout() const
47 return endpoints.size()>2;
50 bool TrackType::is_dead_end() const
52 return endpoints.size()<2;
55 const TrackType::Endpoint &TrackType::get_endpoint(unsigned i) const
57 if(i>=endpoints.size())
58 throw out_of_range("TrackType::get_endpoint");
63 TrackPoint TrackType::get_point(unsigned epi, unsigned path, float d) const
65 if(epi>=endpoints.size())
66 throw out_of_range("TrackType::get_point");
68 const TrackPart *part = 0;
70 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
72 if((endpoints[epi].paths&(1<<path)) && i->get_path()!=path)
75 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
76 for(unsigned j=0; j<n_part_eps; ++j)
78 TrackPoint p = i->get_point(j ? i->get_length() : 0);
79 float dx = p.pos.x-endpoints[epi].pos.x;
80 float dy = p.pos.y-endpoints[epi].pos.y;
90 throw logic_error("internal error (endpoint does not match any part)");
94 float plen = part->get_length();
99 TrackPoint p = part->get_point(d);
107 TrackPart *next = part->get_link(1-part_ep);
109 throw invalid_argument("TrackType::get_point");
110 part_ep = (next->get_link(0)==part ? 0 : 1);
116 bool TrackType::collide_ray(const Vector &start, const Vector &dir, float width) const
118 for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
119 if(i->collide_ray(start, dir, width))
125 void TrackType::collect_endpoints()
129 for(vector<TrackPart>::iterator i=parts.begin(); i!=parts.end(); ++i)
131 for(vector<TrackPart>::iterator j=i; ++j!=parts.end();)
134 unsigned n_part_eps = (i->is_dead_end() ? 1 : 2);
135 for(unsigned j=0; j<n_part_eps; ++j)
138 TrackPoint p = i->get_point(j ? i->get_length() : 0);
143 for(vector<Endpoint>::iterator k=endpoints.begin(); k!=endpoints.end(); ++k)
145 float dx = k->pos.x-p.pos.x;
146 float dy = k->pos.y-p.pos.y;
148 float da = k->dir-p.dir;
154 if(dx*dx+dy*dy<1e-6 && da>-0.01 && da<0.01)
156 k->paths |= 1<<i->get_path();
163 endpoints.push_back(Endpoint(p.pos.x, p.pos.y, p.dir, 1<<i->get_path()));
168 TrackType::Endpoint::Endpoint(float x, float y, float d, unsigned p):
175 TrackType::Loader::Loader(TrackType &t):
176 Msp::DataFile::ObjectLoader<TrackType>(t),
177 state_bits_set(false)
179 add("autofit_preference", &TrackType::autofit_preference);
180 add("description", &TrackType::description);
181 add("object", &TrackType::object);
182 add("state_bits", &Loader::state_bits);
183 add("part", &Loader::part);
186 void TrackType::Loader::finish()
188 obj.collect_endpoints();
191 void TrackType::Loader::part()
195 obj.parts.push_back(p);
196 if(!state_bits_set && p.get_path())
197 while(p.get_path()>=(1U<<obj.state_bits))
201 void TrackType::Loader::state_bits(unsigned b)
204 state_bits_set = true;