]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - incl/bcar.hh
1e0b61bf2c1de59076729a28dac3324a26f7eaf2
[hubacji1/bcar.git] / incl / bcar.hh
1 /*! \file */
2 #ifndef BCAR_BCAR_H
3 #define BCAR_BCAR_H
4
5 #include <ostream>
6 #include <vector>
7
8 namespace bcar {
9
10 template <typename T> int sgn(T val) {
11         return (T(0) < val) - (val < T(0));
12 }
13
14 class Point {
15 private:
16         double x_ = 0.0;
17         double y_ = 0.0;
18 public:
19         Point(double x, double y);
20         Point();
21
22         /*! Get horizontal coordinate. */
23         double x() const;
24
25         /*! Set horizontal coordinate. */
26         void x(double x);
27
28         /*! Get vertical coordinate. */
29         double y() const;
30
31         /*! Set vertical coordinate. */
32         void y(double y);
33
34         /*! \brief Return the smallest angle between three points.
35
36         \see https://math.stackexchange.com/questions/361412/finding-the-angle-between-three-points
37         */
38         double min_angle_between(Point const& p1, Point const& p2) const;
39
40         /*! \brief Return `true` if `this` point is inside of polygon `poly`.
41          *
42          * The polygon is given by the vector of `Point`s.
43          *
44          * \see https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
45          *
46          * \param poly Polygon to consider.
47          */
48         bool inside_of(std::vector<Point> const& poly) const;
49 };
50
51 class Line {
52 private:
53         Point first;
54         Point last;
55         Point intersection1;
56         Point intersection2;
57 public:
58         Line(Point const& fp, Point const& lp);
59
60         /*! Get first point. */
61         Point fp() const&;
62
63         /*! Get last point. */
64         Point lp() const&;
65
66         /*! Get intersection point. */
67         Point in1() const&;
68
69         /*! Get intersection point. */
70         Point in2() const&;
71
72         /*! \brief Return if `this` line intersects with line `li`.
73          *
74          * If the method returns `true`, the intersection `Point` is available
75          * in `this->in1()`.
76          *
77          * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
78          *
79          * \param li The line to check the intersection with.
80          */
81         bool intersects_with(Line const& li);
82
83         /*! \brief Return intersections of `this` (infinite) line and circle.
84          *
85          * If the method returns `true`, the intersection `Point`s are available
86          * in `this->in1()` and `this->in2()`.
87          *
88          * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
89          *
90          * \param c Circle center.
91          * \param r Circle radius.
92          */
93         bool intersects_with(Point const& c, double const r);
94
95         /*! \brief Return if point `p` is on the right side of the plane.
96          *
97          * The plane is given by the line `this`, where `this->fp()` is the base
98          * point and the direction is given by `this->lp() - this->fp()`.
99          *
100          * \param p The point to consider.
101          */
102         bool is_on_right_side(Point const& p) const;
103 };
104
105 /*! Store coordinates `x`, `y`, and heading `h`. */
106 class Pose : public Point {
107 private:
108         double h_ = 0.0;
109 public:
110         Pose(double x, double y, double h);
111         Pose();
112
113         /*! Get heading in the interval [-pi, +pi] radians. */
114         double h() const;
115
116         /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
117         void h(double h);
118
119         /*! Set pose (`x`, `y`, and `h`.) */
120         void set_pose(Pose const& p);
121
122         /*! \brief Rotate self around the point.
123
124         \param c Rotation center `Point`.
125         \param angl Angle of rotation.
126         */
127         void rotate(Point const& c, double const angl);
128
129         friend std::ostream& operator<<(std::ostream& out, Pose const& p);
130 };
131
132 class PoseRange : public Pose {
133 private:
134         double e_ = 0.0;
135         using Pose::h;
136 public:
137         /*! Get heading's begin in the interval [-pi, +pi] radians. */
138         double b() const;
139
140         /*! Set heading's begin in radians. It's recomputed to [-pi, +pi]. */
141         void b(double b);
142
143         /*! Get heading's end in the interval [-pi, +pi] radians. */
144         double e() const;
145
146         /*! Set heading's end in radians. It's recomputed to [-pi, +pi]. */
147         void e(double e);
148
149         void rotate(Point const& c, double const angl);
150
151         friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
152 };
153
154 /*! \brief Store car size.
155  *
156  * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
157  */
158 class CarSize {
159 private:
160         double curb_to_curb = 10.820;
161         double width = 1.625;
162         double wheelbase = 2.450;
163         double distance_to_front = 3.105;
164         double length = 3.760;
165 public:
166         /*! Get curb-to-curb distance. */
167         double ctc() const;
168
169         /*! Set curb-to-curb distance. */
170         void ctc(double ctc);
171
172         /*! Get wheelbase. */
173         double wb() const;
174
175         /*! Set wheelbase. */
176         void wb(double wb);
177
178         /*! Get width. */
179         double w() const;
180
181         /*! Set width. */
182         void w(double w);
183
184         /*! Get length. */
185         double len() const;
186
187         /*! Set length. */
188         void len(double len);
189
190         /*! Get distance from rear axle to front. */
191         double df() const;
192
193         /*! Set distance from rear axle to front. */
194         void df(double df);
195
196         /*! Get distance from rear axle to rear. */
197         double dr() const;
198
199         /*! \brief Get minimum turning radius.
200          *
201          * Please, note that the method returns really _minimum turning radius_,
202          * which is the distance from the reare axle center to the center of
203          * left or right rotation given by the kinematics constrants, i.e.
204          * _wheelbase_ and _curb-to-curb_ distance.
205          *
206          * Sometimes _minimum turning radius_ is not radius, not minimum, or not
207          * turning. In this method, _minimum turning radius_ is minimum turning
208          * radius.
209          */
210         double mtr() const;
211
212         /*! \brief Return inner radius.
213          *
214          * The inner radius is the distance from minimum turning radius circle
215          * center to the nearest point on the car. In this case, the nearest
216          * points on the car are rear axle endpoints.
217          */
218         double iradi() const;
219
220         /*! \brief Return outer front radius.
221          *
222          * The outer front radius is the distance from minimum turning radius
223          * circle center to the farthest point on the front (from the rear axle
224          * view) part of the car.
225          */
226         double ofradi() const;
227
228         /*! \brief Return outer rear radius.
229          *
230          * The outer rear radius is the distance from minimum turning radius
231          * circle center to the farthest point on the rear (from the rear axle
232          * view) part of the car.
233          */
234         double orradi() const;
235
236         /*! \brief Return length of perfect parking slot.
237          *
238          * The width of the slot is the same as the width of the car.
239          *
240          * \see Simon R. Blackburn *The Geometry of Perfect Parking*
241          * \see https://www.ma.rhul.ac.uk/SRBparking
242          */
243         double perfect_parking_slot_len() const;
244 };
245
246 /*! Store car motion. */
247 class CarMove {
248 private:
249         double speed = 0.0;
250         double steer = 0.0;
251 public:
252         /*! Get speed. */
253         double sp() const;
254
255         /*! Set speed. */
256         void sp(double sp);
257
258         /*! Get steer. */
259         double st() const;
260
261         /*! Set steer. */
262         void st(double st);
263 };
264
265 /*! \brief Geometrical computations of a bicycle car.
266  *
267  * - `x()` and `y()` methods returns coordinates of rear axle center.
268  */
269 class BicycleCar : public Pose, public CarSize, public CarMove {
270 private:
271 public:
272         /*! \brief Return `false` if `bc` is not achievable.
273          *
274          * When `false` is returned the `bc` may still be drivable, but not
275          * trivially, i.e. by "line segment - circle arc - line segment".
276          *
277          * \param p `PoseRange` (resp. `Pose`) to achieve.
278          */
279         bool drivable(PoseRange const& p) const;
280         bool drivable(Pose const& p) const;
281
282         /*! Set maximum steering angle. */
283         void set_max_steer();
284
285         /*! Get frame's left front x coordinate. */
286         double lfx() const;
287
288         /*! Get frame's left front y coordinate. */
289         double lfy() const;
290
291         /*! Get frame's left rear x coordinate. */
292         double lrx() const;
293
294         /*! Get frame's left rear y coordinate. */
295         double lry() const;
296
297         /*! Get frame's right rear x coordinate. */
298         double rrx() const;
299
300         /*! Get frame's right rear y coordinate. */
301         double rry() const;
302
303         /*! Get frame's right front x coordinate. */
304         double rfx() const;
305
306         /*! Get frame's right front y coordinate. */
307         double rfy() const;
308
309         /*! Get rear axle's left x coordinate. */
310         double ralx() const;
311
312         /*! Get rear axle's left y coordinate. */
313         double raly() const;
314
315         /*! Get rear axle's right x coordinate. */
316         double rarx() const;
317
318         /*! Get rear axle's right y coordinate. */
319         double rary() const;
320
321         /*! Min. turning radius circle center on left. */
322         Point ccl() const;
323
324         /*! Min. turning radius circle center on rigth. */
325         Point ccr() const;
326
327         /*! Next car position based on speed `sp` and steer `st`. */
328         void next();
329 };
330
331 } // namespace bcar
332 #endif /* BCAR_BCAR_H */