]> git.tdb.fi Git - r2c2.git/blob - source/libmarklin/track.h
Initial revision
[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         const std::string  &get_description() const   { return description; }
79         float              get_slope() const          { return slope; }
80         bool               get_flex() const           { return flex; }
81         float              get_length() const;
82         float              get_total_length() const;
83         unsigned           get_turnout_id() const     { return turnout_id; }
84         unsigned           get_sensor_id() const      { return sensor_id; }
85         unsigned           get_n_routes() const;
86         bool               snap_to(Track &, bool);
87         bool               snap(Point &, float &) const;
88         void               break_link(Track &);
89         void               break_links();
90         void               check_slope();
91         const Endpoint     *traverse(const Endpoint *, unsigned) const;
92
93         /**
94         Creates a copy of the track.  The new track will be almost identical, but
95         won't have any links to other tracks, nor a turnout or sensor id.
96         */
97         Track *copy() const;
98 private:
99         unsigned     art_nr;
100         std::string  description;
101         PartSeq      parts;
102         EndpointSeq  endpoints;
103         Point        pos;
104         float        rot;
105         float        slope;
106         bool         flex;
107         unsigned     turnout_id;
108         unsigned     sensor_id;
109
110         // Direct copying not allowed due to links.  See the copy() function.
111         //Track(const Track &);
112         Track &operator=(const Track &);
113
114         void  collect_endpoints();
115 };
116 typedef std::list<Track *> TrackSeq;
117 typedef std::set<Track *> TrackSet;
118
119 } // namespace Marklin
120
121 #endif