/* $Id$
This file is part of the MSP Märklin suite
-Copyright © 2006-2008 Mikkosoft Productions, Mikko Rasa
+Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
Distributed under the GPL
*/
namespace Marklin {
TrackPart::TrackPart():
- x(0),
- y(0),
dir(0),
length(0),
radius(0),
- route(0),
+ path(0),
dead_end(false)
{ }
-void TrackPart::collect_endpoints(vector<Endpoint> &eps)
+void TrackPart::collect_endpoints(vector<Endpoint> &eps) const
{
- eps.push_back(Endpoint(x, y, dir+M_PI, 1<<route));
+ eps.push_back(Endpoint(pos.x, pos.y, dir+M_PI, 1<<path));
if(dead_end)
;
else if(radius)
{
- float a=((radius<0) ? -length : length);
- float c=cos(a);
- float s=sin(a);
- float rx=radius*sin(dir);
- float ry=-radius*cos(dir);
- eps.push_back(Endpoint(x+c*rx-s*ry-rx, y+c*ry+s*rx-ry, dir+a, 1<<route));
+ float a = ((radius<0) ? -length : length);
+ Point p = get_point(length*abs(radius));
+ eps.push_back(Endpoint(p.x, p.y, dir+a, 1<<path));
}
else
- eps.push_back(Endpoint(x+cos(dir)*length, y+sin(dir)*length, dir, 1<<route));
+ eps.push_back(Endpoint(pos.x+cos(dir)*length, pos.y+sin(dir)*length, dir, 1<<path));
+}
+
+Point TrackPart::get_point(float d) const
+{
+ if(radius)
+ {
+ float a = d/radius;
+ float c = cos(a);
+ float s = sin(a);
+ float rx = radius*sin(dir);
+ float ry = -radius*cos(dir);
+ return Point(pos.x+c*rx-s*ry-rx, pos.y+c*ry+s*rx-ry);
+ }
+ else
+ return Point(pos.x+cos(dir)*d, pos.y+sin(dir)*d);
}
TrackPart::Loader::Loader(TrackPart &p):
- part(p)
+ Msp::DataFile::BasicLoader<TrackPart>(p)
{
add("start", &Loader::start);
add("length", &TrackPart::length);
add("radius", &TrackPart::radius);
- add("route", &TrackPart::route);
+ add("path", &TrackPart::path);
add("dead_end", &TrackPart::dead_end);
}
void TrackPart::Loader::finish()
{
- if(part.radius)
+ if(obj.radius)
{
- part.length*=M_PI/180;
- part.radius/=1000;
+ obj.length *= M_PI/180;
+ obj.radius /= 1000;
}
else
- part.length/=1000;
+ obj.length /= 1000;
- part.x/=1000;
- part.y/=1000;
- part.dir*=M_PI/180;
+ obj.pos.x /= 1000;
+ obj.pos.y /= 1000;
+ obj.dir *= M_PI/180;
}
void TrackPart::Loader::start(float x, float y, float d)
{
- part.x=x;
- part.y=y;
- part.dir=d;
+ obj.pos = Point(x, y);
+ obj.dir = d;
}
} // namespace Marklin