]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - incl/bcar.hh
Add pose range
[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 {
107 private:
108         double x_ = 0.0;
109         double y_ = 0.0;
110         double h_ = 0.0;
111 public:
112         /*! Get horizontal coordinate. */
113         double x() const;
114
115         /*! Set horizontal coordinate. */
116         void x(double x);
117
118         /*! Get vertical coordinate. */
119         double y() const;
120
121         /*! Set vertical coordinate. */
122         void y(double y);
123
124         /*! Get heading in the interval [-pi, +pi] radians. */
125         double h() const;
126
127         /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
128         void h(double h);
129
130         /*! \brief Rotate self around the point.
131
132         \param c Rotation center `Point`.
133         \param angl Angle of rotation.
134         */
135         void rotate(Point const& c, double const angl);
136
137         friend std::ostream& operator<<(std::ostream& out, Pose const& p);
138 };
139
140 class PoseRange : public Pose {
141 private:
142         double e_ = 0.0;
143         using Pose::h;
144 public:
145         /*! Get heading's begin in the interval [-pi, +pi] radians. */
146         double b() const;
147
148         /*! Set heading's begin in radians. It's recomputed to [-pi, +pi]. */
149         void b(double b);
150
151         /*! Get heading's end in the interval [-pi, +pi] radians. */
152         double e() const;
153
154         /*! Set heading's end in radians. It's recomputed to [-pi, +pi]. */
155         void e(double e);
156
157         void rotate(Point const& c, double const angl);
158
159         friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
160 };
161
162 /*! \brief Store car size.
163  *
164  * - Default is https://en.wikipedia.org/wiki/Fiat_Punto
165  */
166 class CarSize {
167 private:
168         double curb_to_curb = 10.820;
169         double width = 1.625;
170         double wheelbase = 2.450;
171         double distance_to_front = 3.105;
172         double length = 3.760;
173 public:
174         /*! Get curb-to-curb distance. */
175         double ctc() const;
176
177         /*! Set curb-to-curb distance. */
178         void ctc(double ctc);
179
180         /*! Get wheelbase. */
181         double wb() const;
182
183         /*! Set wheelbase. */
184         void wb(double wb);
185
186         /*! Get width. */
187         double w() const;
188
189         /*! Set width. */
190         void w(double w);
191
192         /*! Get length. */
193         double len() const;
194
195         /*! Set length. */
196         void len(double len);
197
198         /*! Get distance from rear axle to front. */
199         double df() const;
200
201         /*! Set distance from rear axle to front. */
202         void df(double df);
203
204         /*! Get distance from rear axle to rear. */
205         double dr() const;
206
207         /*! \brief Get minimum turning radius.
208          *
209          * Please, note that the method returns really _minimum turning radius_,
210          * which is the distance from the reare axle center to the center of
211          * left or right rotation given by the kinematics constrants, i.e.
212          * _wheelbase_ and _curb-to-curb_ distance.
213          *
214          * Sometimes _minimum turning radius_ is not radius, not minimum, or not
215          * turning. In this method, _minimum turning radius_ is minimum turning
216          * radius.
217          */
218         double mtr() const;
219 };
220
221 /*! Store car motion. */
222 class CarMove {
223 private:
224         double speed = 0.0;
225         double steer = 0.0;
226 public:
227         /*! Get speed. */
228         double sp() const;
229
230         /*! Set speed. */
231         void sp(double sp);
232
233         /*! Get steer. */
234         double st() const;
235
236         /*! Set steer. */
237         void st(double st);
238 };
239
240 /*! \brief Geometrical computations of a bicycle car.
241  *
242  * - `x()` and `y()` methods returns coordinates of rear axle center.
243  */
244 class BicycleCar : public Pose, public CarSize, public CarMove {
245 private:
246 public:
247         /*! \brief Return `false` if `bc` is not achievable.
248          *
249          * When `false` is returned the `bc` may still be drivable, but not
250          * trivially, i.e. by "line segment - circle arc - line segment".
251          *
252          * \param bc The bicycle car to achieve.
253          * \param b The beginning of the heading range.
254          * \param e The end of the heading range.
255          */
256         bool drivable(Pose const& p, double b, double e) const;
257         bool drivable(Pose const& p) const;
258
259         /*! \brief Return inner radius.
260          *
261          * The inner radius is the distance from minimum turning radius circle
262          * center to the nearest point on the car. In this case, the nearest
263          * points on the car are rear axle endpoints.
264          */
265         double iradi() const;
266
267         /*! \brief Return outer front radius.
268          *
269          * The outer front radius is the distance from minimum turning radius
270          * circle center to the farthest point on the front (from the rear axle
271          * view) part of the car.
272          */
273         double ofradi() const;
274
275         /*! \brief Return outer rear radius.
276          *
277          * The outer rear radius is the distance from minimum turning radius
278          * circle center to the farthest point on the rear (from the rear axle
279          * view) part of the car.
280          */
281         double orradi() const;
282
283         /*! \brief Return length of perfect parking slot.
284          *
285          * The width of the slot is the same as the width of the car.
286          *
287          * \see Simon R. Blackburn *The Geometry of Perfect Parking*
288          * \see https://www.ma.rhul.ac.uk/SRBparking
289          */
290         double perfect_parking_slot_len() const;
291
292         /*! Set maximum steering angle. */
293         void set_max_steer();
294
295         /*! Get frame's left front x coordinate. */
296         double lfx() const;
297
298         /*! Get frame's left front y coordinate. */
299         double lfy() const;
300
301         /*! Get frame's left rear x coordinate. */
302         double lrx() const;
303
304         /*! Get frame's left rear y coordinate. */
305         double lry() const;
306
307         /*! Get frame's right rear x coordinate. */
308         double rrx() const;
309
310         /*! Get frame's right rear y coordinate. */
311         double rry() const;
312
313         /*! Get frame's right front x coordinate. */
314         double rfx() const;
315
316         /*! Get frame's right front y coordinate. */
317         double rfy() const;
318
319         /*! Get rear axle's left x coordinate. */
320         double ralx() const;
321
322         /*! Get rear axle's left y coordinate. */
323         double raly() const;
324
325         /*! Get rear axle's right x coordinate. */
326         double rarx() const;
327
328         /*! Get rear axle's right y coordinate. */
329         double rary() const;
330
331         /*! Min. turning radius circle center on left. */
332         Point ccl() const;
333
334         /*! Min. turning radius circle center on rigth. */
335         Point ccr() const;
336
337         /*! Next car position based on speed `sp` and steer `st`. */
338         void next();
339 };
340
341 } // namespace bcar
342 #endif /* BCAR_BCAR_H */