]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/vehicle.h
Provide some telemetry values from ArduControl
[r2c2.git] / source / libr2c2 / vehicle.h
1 #ifndef LIBR2C2_VEHICLE_H_
2 #define LIBR2C2_VEHICLE_H_
3
4 #include "geometry.h"
5 #include "object.h"
6 #include "trackoffsetiter.h"
7 #include "vehicleplacement.h"
8 #include "vehicletype.h"
9
10 namespace R2C2 {
11
12 class Layout;
13 class Train;
14
15 class attachment_error: public std::logic_error
16 {
17 public:
18         attachment_error(const std::string &w): std::logic_error(w) { }
19         virtual ~attachment_error() throw() { }
20 };
21
22
23 class Vehicle: public Object
24 {
25 public:
26         struct Axle
27         {
28                 const VehicleType::Axle *type;
29                 Angle angle;
30
31                 Axle(const VehicleType::Axle &);
32         };
33
34         struct Bogie
35         {
36                 const VehicleType::Bogie *type;
37                 Angle direction;
38                 std::vector<Axle *> axles;
39
40                 Bogie(const VehicleType::Bogie &);
41         };
42
43         struct Rod
44         {
45                 const VehicleType::Rod *type;
46                 Vector position;
47                 Angle angle;
48
49                 Rod(const VehicleType::Rod &);
50         };
51
52 private:
53         const VehicleType &type;
54         Train *train;
55         Vehicle *next;
56         Vehicle *prev;
57         VehiclePlacement placement;
58         std::vector<Axle> axles;
59         std::vector<Axle *> fixed_axles;
60         std::vector<Bogie> bogies;
61         std::vector<Rod> rods;
62         unsigned front_sensor;
63         unsigned back_sensor;
64
65 public:
66         Vehicle(Layout &, const VehicleType &);
67         ~Vehicle();
68
69         virtual Vehicle *clone(Layout * = 0) const;
70         virtual const VehicleType &get_type() const { return type; }
71
72         void set_train(Train *);
73         Train *get_train() const { return train; }
74         void attach_back(Vehicle &);
75         void attach_front(Vehicle &);
76         void detach_back();
77         void detach_front();
78         Vehicle *get_next() const { return next; }
79         Vehicle *get_previous() const { return prev; }
80
81         // TODO implement these - should call place() with suitable parameters
82         virtual void set_position(const Vector &) { }
83         virtual void set_rotation(const Angle &) { }
84         virtual void set_tilt(const Angle &) { }
85         void place(const TrackOffsetIter &, VehiclePlacement::Anchor = VehiclePlacement::CENTER);
86         void unplace();
87         void advance(float);
88         const VehiclePlacement &get_placement() const { return placement; }
89         bool is_placed() const { return placement.is_placed(); }
90         const Axle &get_axle(unsigned) const;
91         const Axle &get_fixed_axle(unsigned) const;
92         const Bogie &get_bogie(unsigned) const;
93         const Axle &get_bogie_axle(unsigned, unsigned) const;
94         const Rod &get_rod(unsigned) const;
95 private:
96         void update_position(int);
97         void update_position_from(const Vehicle &);
98         void propagate_position();
99         void propagate_forward();
100         void propagate_backward();
101         void check_sensor(const TrackOffsetIter &, unsigned &, bool);
102         void turn_axles(float);
103         void update_rods();
104         float resolve_rod_constraint(Rod &, const VehicleType::RodConstraint &);
105
106 public:
107         virtual unsigned get_n_link_slots() const;
108         virtual Vehicle *get_link(unsigned) const;
109         virtual int get_link_slot(const Object &) const;
110
111         virtual bool collide_ray(const Ray &, float * = 0) const;
112 };
113
114 } // namespace R2C2
115
116 #endif