]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - incl/bcar.hh
9e709862d99495139a56debba759425175f76225
[hubacji1/bcar.git] / incl / bcar.hh
1 /*
2  * SPDX-FileCopyrightText: 2021 Jiri Vlasak <jiri.vlasak.2@cvut.cz>
3  *
4  * SPDX-License-Identifier: GPL-3.0-only
5  */
6
7 /*! \file */
8 #ifndef BCAR_BCAR_H
9 #define BCAR_BCAR_H
10
11 #include <ostream>
12 #include <vector>
13
14 namespace bcar {
15
16 template <typename T> int sgn(T val) {
17         return (T(0) < val) - (val < T(0));
18 }
19
20 class Line;
21
22 class Point {
23 private:
24         double _x = 0.0;
25         double _y = 0.0;
26 public:
27         Point();
28         Point(double x, double y);
29
30         /*! Get horizontal coordinate. */
31         double x() const;
32
33         /*! Set horizontal coordinate. */
34         void x(double x);
35
36         /*! Get vertical coordinate. */
37         double y() const;
38
39         /*! Set vertical coordinate. */
40         void y(double y);
41
42         /*! \brief Return the smallest angle between three points.
43
44         \see https://math.stackexchange.com/questions/361412/finding-the-angle-between-three-points
45         */
46         double min_angle_between(Point const& p1, Point const& p2) const;
47
48         /*! \brief Return `true` if `this` point is inside of polygon `poly`.
49          *
50          * The polygon is given by the vector of `Point`s.
51          *
52          * \see https://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
53          *
54          * \param poly Polygon to consider.
55          */
56         bool inside_of(std::vector<Point> const& poly) const;
57
58         /*! \brief Return `true` if on the right side of the plane.
59          *
60          * The plane is given by the line `li`, where `li->b()` is the base
61          * point and the direction is given by `li->e() - li->b()`.
62          *
63          * \param li The plane to consider is given by `li`.
64          */
65         bool on_right_side_of(Line const& li) const;
66
67         /*! \brief Translate self.
68          *
69          * \param p `Point` offset to translate by.
70          */
71         void translate(Point const& p);
72
73         /*! \brief Rotate self around the point.
74
75         \param c Rotation center `Point`.
76         \param angl Angle of rotation.
77         */
78         void rotate(Point const& c, double const angl);
79
80         /*! \brief Compute reflection of `this` around the `Line`.
81          *
82          * \param li The plane to reflect around is given by `li`.
83          */
84         void reflect(Line const& li);
85
86         /*! Return Euclidean distance to `p`. */
87         double edist(Point const& p) const;
88
89         bool operator==(Point const& p);
90         friend std::ostream& operator<<(std::ostream& out, Point const& p);
91 };
92
93 class Line {
94 private:
95         Point _b;
96         Point _e;
97         Point _i1;
98         Point _i2;
99 public:
100         Line(Point const& fp, Point const& lp);
101
102         /*! Get beginning point. */
103         Point b() const&;
104
105         /*! Get end point. */
106         Point e() const&;
107
108         /*! Get middle point. */
109         Point m() const;
110
111         /*! Get intersection point. */
112         Point i1() const&;
113
114         /*! Get intersection point. */
115         Point i2() const&;
116
117         /*! \brief Return if `this` line intersects with line `li`.
118          *
119          * If the method returns `true`, the intersection `Point` is available
120          * in `this->i1()`.
121          *
122          * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
123          *
124          * \param li The line to check the intersection with.
125          */
126         bool intersects_with(Line const& li);
127
128         /*! \brief Return intersections of `this` (infinite) line and circle.
129          *
130          * If the method returns `true`, the intersection `Point`s are available
131          * in `this->i1()` and `this->i2()`.
132          *
133          * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
134          *
135          * \param c Circle center.
136          * \param r Circle radius.
137          */
138         bool intersects_with(Point const& c, double const r);
139
140         double len() const;
141
142         double h() const;
143
144         friend std::ostream& operator<<(std::ostream& out, Line const& li);
145 };
146
147 /*! Store coordinates `x`, `y`, and heading `h`. */
148 class Pose : public virtual Point {
149 private:
150         double _h = 0.0;
151 public:
152         using Point::Point;
153         Pose(double x, double y, double h);
154
155         /*! Get heading in the interval [-pi, +pi] radians. */
156         double h() const;
157
158         /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
159         void h(double h);
160
161         /*! Set pose (`x`, `y`, and `h`.) */
162         void set_pose(Pose const& p);
163
164         void rotate(Point const& c, double const angl);
165
166         void reflect(Line const& li);
167
168         bool operator==(Pose const& p);
169         friend std::ostream& operator<<(std::ostream& out, Pose const& p);
170 };
171
172 class PoseRange : public virtual Pose {
173 private:
174         Pose _bp;
175         Pose _ep;
176         void set_xyh();
177 public:
178         PoseRange(Pose bp, Pose ep);
179         PoseRange(double x, double y, double b, double e);
180
181         Pose bp() const;
182         Pose ep() const;
183
184         /*! Get heading's begin in the interval [-pi, +pi] radians. */
185         double b() const;
186
187         /*! Get heading's end in the interval [-pi, +pi] radians. */
188         double e() const;
189
190         void translate(Point const& p);
191         void rotate(Point const& c, double const angl);
192         void reflect(Line const& li);
193
194         friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
195 };
196
197 /*! \brief Store car size.
198  *
199  * - The default is Renault ZOE (www.car.info)
200  */
201 class CarSize {
202 private:
203         double _curb_to_curb = 10.802166641822163;
204         double _width = 1.945; // including mirrors
205         double _wheelbase = 2.588;
206         double _distance_to_front = 3.427;
207         double _length = 4.084;
208         double _front_track = 1.511;
209 public:
210         /*! Get curb-to-curb distance. */
211         double ctc() const;
212
213         /*! Set curb-to-curb distance. */
214         void ctc(double ctc);
215
216         /*! Get wheelbase. */
217         double wb() const;
218
219         /*! Set wheelbase. */
220         void wb(double wb);
221
222         /*! Get width. */
223         double w() const;
224
225         /*! Set width. */
226         void w(double w);
227
228         /*! Get length. */
229         double len() const;
230
231         /*! Set length. */
232         void len(double len);
233
234         /*! Get distance from rear axle to front. */
235         double df() const;
236
237         /*! Set distance from rear axle to front. */
238         void df(double df);
239
240         /*! Get distance from rear axle to rear. */
241         double dr() const;
242
243         /*! Set front track. */
244         void ft(double ft);
245
246         /*! Get front track. */
247         double ft() const;
248
249         /*! \brief Get minimum turning radius.
250          *
251          * Please, note that the method returns really _minimum turning radius_,
252          * which is the distance from the rear axle center to the center of
253          * left or right rotation given by the kinematics constrants, i.e.
254          * _wheelbase and _curb-to-curb_ distance.
255          *
256          * Sometimes _minimum turning _radius is not radius, not minimum, or not
257          * turning. In this method, _minimum turning _radius is minimum turning
258          * radius.
259          */
260         double mtr() const;
261
262         /*! \brief Return inner radius.
263          *
264          * The inner radius is the distance from minimum turning radius circle
265          * center to the nearest point on the car. In this case, the nearest
266          * points on the car are rear axle endpoints.
267          */
268         double iradi() const;
269
270         /*! \brief Return outer front radius.
271          *
272          * The outer front radius is the distance from minimum turning radius
273          * circle center to the farthest point on the front (from the rear axle
274          * view) part of the car.
275          */
276         double ofradi() const;
277
278         /*! \brief Return outer rear radius.
279          *
280          * The outer rear radius is the distance from minimum turning radius
281          * circle center to the farthest point on the rear (from the rear axle
282          * view) part of the car.
283          */
284         double orradi() const;
285
286         /*! \brief Return length of perfect parking slot.
287          *
288          * The width of the slot is the same as the width of the car.
289          *
290          * \see Simon R. Blackburn *The Geometry of Perfect Parking*
291          * \see https://www.ma.rhul.ac.uk/SRBparking
292          */
293         double perfect_parking_slot_len() const;
294 };
295
296 /*! Store car motion. */
297 class CarMove {
298 private:
299         double _speed = 0.0;
300         double _steer = 0.0;
301 public:
302         /*! Get speed. */
303         double sp() const;
304
305         /*! Set speed. */
306         void sp(double sp);
307
308         /*! Get steer. */
309         double st() const;
310
311         /*! Set steer. */
312         void st(double st);
313 };
314
315 /*! \brief Geometrical computations of a bicycle car.
316  *
317  * - `x()` and `y()` methods returns coordinates of rear axle center.
318  */
319 class BicycleCar : public virtual Pose, public virtual CarSize,
320                 public virtual CarMove {
321 private:
322 public:
323         /*! \brief Return `true` if `this` can drive to `p` trivially.
324          *
325          * Trivially means that `this` can drive to `p` by line segment - circle
326          * arc - line segment.
327          *
328          * \param p `PoseRange` (resp. `Pose`) to achieve.
329          */
330         bool drivable(PoseRange const& p) const;
331         bool drivable(Pose const& p) const;
332
333         /*! Set maximum steering angle. */
334         void set_max_steer();
335
336         /*! Get frame's left front x coordinate. */
337         double lfx() const;
338
339         /*! Get frame's left front y coordinate. */
340         double lfy() const;
341
342         /*! Get frame's left rear x coordinate. */
343         double lrx() const;
344
345         /*! Get frame's left rear y coordinate. */
346         double lry() const;
347
348         /*! Get frame's right rear x coordinate. */
349         double rrx() const;
350
351         /*! Get frame's right rear y coordinate. */
352         double rry() const;
353
354         /*! Get frame's right front x coordinate. */
355         double rfx() const;
356
357         /*! Get frame's right front y coordinate. */
358         double rfy() const;
359
360         /*! Get frame's left front point. */
361         Point lf() const;
362
363         /*! Get frame's left rear point. */
364         Point lr() const;
365
366         /*! Get frame's right rear point. */
367         Point rr() const;
368
369         /*! Get frame's right front point. */
370         Point rf() const;
371
372         /*! Get frame's left side. */
373         Line left() const;
374
375         /*! Get frame's rear side. */
376         Line rear() const;
377
378         /*! Get frame's right side. */
379         Line right() const;
380
381         /*! Get frame's front side. */
382         Line front() const;
383
384         /*! Get frame's left rear axle x coordinate. */
385         double lrax() const;
386
387         /*! Get frame's left rear axle y coordinate. */
388         double lray() const;
389
390         /*! Get frame's right rear axle x coordinate. */
391         double rrax() const;
392
393         /*! Get frame's right rear axle y coordinate. */
394         double rray() const;
395
396         /*! Get frame's left rear axle point. */
397         Point lra() const;
398
399         /*! Get frame's right rear axle point. */
400         Point rra() const;
401
402         /*! Get frame's left front axle x coordinate. */
403         double lfax() const;
404
405         /*! Get frame's left front axle y coordinate. */
406         double lfay() const;
407
408         /*! Get frame's right front axle x coordinate. */
409         double rfax() const;
410
411         /*! Get frame's right front axle y coordinate. */
412         double rfay() const;
413
414         /*! Get iframe's left front axle point. */
415         Point lfa() const;
416
417         /*! Get frame's right front axle point. */
418         Point rfa() const;
419
420         /*! Get frame's center front x coordinate. */
421         double cfx() const;
422
423         /*! Get frame's center front y coordinate. */
424         double cfy() const;
425
426         /*! Get frame's center front point. */
427         Point cf() const;
428
429         /*! Min. turning radius circle center on left. */
430         Point ccl() const;
431
432         /*! Min. turning radius circle center on rigth. */
433         Point ccr() const;
434
435         /*! Next car position based on speed `sp` and steer `st`. */
436         void next();
437 };
438
439 } // namespace bcar
440 #endif /* BCAR_BCAR_H */