]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/track.h
6bddd4dad01518676062db707caab0b173d19ff9
[r2c2.git] / source / libmarklin / track.h
1 #ifndef LIBMARKLIN_TRACK_H_
2 #define LIBMARKLIN_TRACK_H_
3
4 #include <list>
5 #include <set>
6 #include <msp/parser/loader.h>
7 #include "geometry.h"
8
9 namespace Marklin {
10
11 class Track
12 {
13 public:
14         class Loader: public Msp::Parser::Loader
15         {
16         public:
17                 Loader(Track &);
18                 Track &get_object() { return track; }
19                 ~Loader();
20         private:
21                 Track &track;
22
23                 void part();
24                 void position(float, float, float);
25         };
26
27         struct Endpoint
28         {
29                 Point  pos;
30                 float  rot;
31                 Track *link;
32                 unsigned routes;
33
34                 Endpoint(const Point &p, float r, unsigned o): pos(p), rot(r), link(0), routes(o) { }
35         };
36         typedef std::list<Endpoint> EndpointSeq;
37
38         struct Part
39         {
40                 class Loader: public Msp::Parser::Loader
41                 {
42                 public:
43                         Loader(Part &);
44                         Part &get_object() { return part; }
45                         ~Loader();
46                 private:
47                         Part &part;
48
49                         void start(float, float, float);
50                 };
51
52                 float    x,y;
53                 float    dir;
54                 float    length;
55                 float    radius;
56                 unsigned route;
57                 bool     dead_end;
58
59                 Part();
60                 void collect_endpoints(EndpointSeq &);
61         };
62         typedef std::list<Part> PartSeq;
63
64         Track(unsigned);
65         ~Track();
66         void               set_position(const Point &);
67         void               set_rotation(float);
68         void               set_slope(float);
69         void               set_flex(bool f)           { flex=f; }
70         void               set_turnout_id(unsigned i) { turnout_id=i; }
71         void               set_sensor_id(unsigned i)  { sensor_id=i; }
72         const Point        &get_position() const      { return pos; }
73         float              get_rotation() const       { return rot; }
74         unsigned           get_article_number() const { return art_nr; }
75         const PartSeq      &get_parts() const         { return parts; }
76         const EndpointSeq  &get_endpoints() const     { return endpoints; }
77         const Endpoint     *get_endpoint_by_link(Track *) const;
78         Point              get_endpoint_position(const Endpoint &) const;
79         const std::string  &get_description() const   { return description; }
80         float              get_slope() const          { return slope; }
81         bool               get_flex() const           { return flex; }
82         float              get_length() const;
83         float              get_total_length() const;
84         unsigned           get_turnout_id() const     { return turnout_id; }
85         unsigned           get_sensor_id() const      { return sensor_id; }
86         unsigned           get_n_routes() const;
87         bool               snap_to(Track &, bool);
88         bool               snap(Point &, float &) const;
89         void               break_link(Track &);
90         void               break_links();
91         void               check_slope();
92         const Endpoint     *traverse(const Endpoint *, unsigned) const;
93
94         /**
95         Creates a copy of the track.  The new track will be almost identical, but
96         won't have any links to other tracks, nor a turnout or sensor id.
97         */
98         Track *copy() const;
99 private:
100         unsigned     art_nr;
101         std::string  description;
102         PartSeq      parts;
103         EndpointSeq  endpoints;
104         Point        pos;
105         float        rot;
106         float        slope;
107         bool         flex;
108         unsigned     turnout_id;
109         unsigned     sensor_id;
110
111         // Direct copying not allowed due to links.  See the copy() function.
112         //Track(const Track &);
113         Track &operator=(const Track &);
114
115         void  collect_endpoints();
116 };
117 typedef std::list<Track *> TrackSeq;
118 typedef std::set<Track *> TrackSet;
119
120 } // namespace Marklin
121
122 #endif