3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
14 #include <msp/io/print.h>
18 TrackPart::TrackPart():
29 float TrackPart::get_length() const
32 return abs(radius)*length;
37 TrackPoint TrackPart::get_point(float d) const
46 float rx = radius*sin(dir);
47 float ry = -radius*cos(dir);
48 result.pos = Point(pos.x+c*rx-s*ry-rx, pos.y+c*ry+s*rx-ry);
53 result.pos = Point(pos.x+cos(dir)*d, pos.y+sin(dir)*d);
60 void TrackPart::check_link(TrackPart &other)
62 unsigned n_eps = (dead_end ? 1 : 2);
63 unsigned n_other_eps = (other.is_dead_end() ? 1 : 2);
64 for(unsigned i=0; i<n_eps; ++i)
66 TrackPoint p1 = get_point(i ? get_length() : 0);
67 for(unsigned j=0; j<n_other_eps; ++j)
69 TrackPoint p2 = other.get_point(j ? other.get_length() : 0);
71 float dx = p2.pos.x-p1.pos.x;
72 float dy = p2.pos.y-p1.pos.y;
74 float da = p2.dir-p1.dir+M_PI*((i+j+1)%2);
80 if(dx*dx+dy*dy<1e-6 && da>=-0.01 && da<=0.01)
82 IO::print("TrackParts linked!\n");
84 other.links[j] = this;
91 TrackPart *TrackPart::get_link(unsigned i) const
94 throw InvalidParameterValue("Index out of range");
99 TrackPart::Loader::Loader(TrackPart &p):
100 Msp::DataFile::BasicLoader<TrackPart>(p)
102 add("start", &Loader::start);
103 add("length", &TrackPart::length);
104 add("radius", &TrackPart::radius);
105 add("path", &TrackPart::path);
106 add("dead_end", &TrackPart::dead_end);
109 void TrackPart::Loader::finish()
113 obj.length *= M_PI/180;
124 void TrackPart::Loader::start(float x, float y, float d)
126 obj.pos = Point(x, y);
130 } // namespace Marklin