]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/trackpart.cpp
Code reformatting: add spaces around assignment operators
[r2c2.git] / source / libmarklin / trackpart.cpp
1 /* $Id$
2
3 This file is part of the MSP Märklin suite
4 Copyright © 2006-2009 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <cmath>
9 #include "trackpart.h"
10
11 using namespace std;
12
13 namespace Marklin {
14
15 TrackPart::TrackPart():
16         dir(0),
17         length(0),
18         radius(0),
19         route(0),
20         dead_end(false)
21 { }
22
23 void TrackPart::collect_endpoints(vector<Endpoint> &eps) const
24 {
25         eps.push_back(Endpoint(pos.x, pos.y, dir+M_PI, 1<<route));
26
27         if(dead_end)
28                 ;
29         else if(radius)
30         {
31                 float a = ((radius<0) ? -length : length);
32                 Point p = get_point(length*abs(radius));
33                 eps.push_back(Endpoint(p.x, p.y, dir+a, 1<<route));
34         }
35         else
36                 eps.push_back(Endpoint(pos.x+cos(dir)*length, pos.y+sin(dir)*length, dir, 1<<route));
37 }
38
39 Point TrackPart::get_point(float d) const
40 {
41         if(radius)
42         {
43                 float a = d/radius;
44                 float c = cos(a);
45                 float s = sin(a);
46                 float rx = radius*sin(dir);
47                 float ry = -radius*cos(dir);
48                 return Point(pos.x+c*rx-s*ry-rx, pos.y+c*ry+s*rx-ry);
49         }
50         else
51                 return Point(pos.x+cos(dir)*d, pos.y+sin(dir)*d);
52 }
53
54
55 TrackPart::Loader::Loader(TrackPart &p):
56         Msp::DataFile::BasicLoader<TrackPart>(p)
57 {
58         add("start",    &Loader::start);
59         add("length",   &TrackPart::length);
60         add("radius",   &TrackPart::radius);
61         add("route",    &TrackPart::route);
62         add("dead_end", &TrackPart::dead_end);
63 }
64
65 void TrackPart::Loader::finish()
66 {
67         if(obj.radius)
68         {
69                 obj.length *= M_PI/180;
70                 obj.radius /= 1000;
71         }
72         else
73                 obj.length /= 1000;
74
75         obj.pos.x /= 1000;
76         obj.pos.y /= 1000;
77         obj.dir *= M_PI/180;
78 }
79
80 void TrackPart::Loader::start(float x, float y, float d)
81 {
82         obj.pos = Point(x, y);
83         obj.dir = d;
84 }
85
86 } // namespace Marklin