]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - incl/bcar.hh
Change generate plot for parking slot
[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         /*! Generate output for plotting with gnuplot. */
90         void gen_gnuplot_to(std::ostream& out);
91
92         bool operator==(Point const& p);
93         friend std::ostream& operator<<(std::ostream& out, Point const& p);
94 };
95
96 class Line {
97 private:
98         Point _b;
99         Point _e;
100         Point _i1;
101         Point _i2;
102 public:
103         Line(Point const& fp, Point const& lp);
104
105         /*! Get beginning point. */
106         Point b() const&;
107
108         /*! Get end point. */
109         Point e() const&;
110
111         /*! Get middle point. */
112         Point m() const;
113
114         /*! Get intersection point. */
115         Point i1() const&;
116
117         /*! Get intersection point. */
118         Point i2() const&;
119
120         /*! \brief Return if `this` line intersects with line `li`.
121          *
122          * If the method returns `true`, the intersection `Point` is available
123          * in `this->i1()`.
124          *
125          * \see https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection
126          *
127          * \param li The line to check the intersection with.
128          */
129         bool intersects_with(Line const& li);
130
131         /*! \brief Return intersections of `this` (infinite) line and circle.
132          *
133          * If the method returns `true`, the intersection `Point`s are available
134          * in `this->i1()` and `this->i2()`.
135          *
136          * \see https://mathworld.wolfram.com/Circle-LineIntersection.html
137          *
138          * \param c Circle center.
139          * \param r Circle radius.
140          */
141         bool intersects_with(Point const& c, double const r);
142
143         double len() const;
144
145         double h() const;
146
147         /*! Generate output for plotting with gnuplot. */
148         void gen_gnuplot_to(std::ostream& out);
149
150         friend std::ostream& operator<<(std::ostream& out, Line const& li);
151 };
152
153 /*! Store coordinates `x`, `y`, and heading `h`. */
154 class Pose : public virtual Point {
155 private:
156         double _h = 0.0;
157 public:
158         using Point::Point;
159         Pose(double x, double y, double h);
160
161         /*! Get heading in the interval [-pi, +pi] radians. */
162         double h() const;
163
164         /*! Set heading in radians. It's recomputed to [-pi, +pi]. */
165         void h(double h);
166
167         /*! Set pose (`x`, `y`, and `h`.) */
168         void set_pose(Pose const& p);
169
170         void rotate(Point const& c, double const angl);
171
172         void reflect(Line const& li);
173
174         bool operator==(Pose const& p);
175         friend std::ostream& operator<<(std::ostream& out, Pose const& p);
176 };
177
178 class PoseRange : public virtual Pose {
179 private:
180         Pose _bp;
181         Pose _ep;
182         void set_xyh();
183 public:
184         PoseRange(Pose bp, Pose ep);
185         PoseRange(double x, double y, double b, double e);
186
187         Pose bp() const;
188         Pose ep() const;
189
190         /*! Get heading's begin in the interval [-pi, +pi] radians. */
191         double b() const;
192
193         /*! Get heading's end in the interval [-pi, +pi] radians. */
194         double e() const;
195
196         void translate(Point const& p);
197         void rotate(Point const& c, double const angl);
198         void reflect(Line const& li);
199
200         friend std::ostream& operator<<(std::ostream& out, PoseRange const& p);
201 };
202
203 /*! \brief Store car size.
204  *
205  * - The default is Renault ZOE (www.car.info)
206  */
207 class CarSize {
208 private:
209         double _curb_to_curb = 10.802166641822163;
210         double _width_with_mirrors = 1.945;
211         double _width = 1.771;
212         double _wheelbase = 2.588;
213         double _distance_to_front = 3.427;
214         double _length = 4.084;
215         double _front_track = 1.511;
216 public:
217         /*! Get curb-to-curb distance. */
218         double ctc() const;
219
220         /*! Set curb-to-curb distance. */
221         void ctc(double ctc);
222
223         /*! Get wheelbase. */
224         double wb() const;
225
226         /*! Set wheelbase. */
227         void wb(double wb);
228
229         /*! Get width. */
230         double w() const;
231
232         /*! Set width. */
233         void w(double w);
234
235         /*! Get width with mirrors. */
236         double wwm() const;
237
238         /*! Set width with mirrors. */
239         void wwm(double w);
240
241         /*! Get length. */
242         double len() const;
243
244         /*! Set length. */
245         void len(double len);
246
247         /*! Get distance from rear axle to front. */
248         double df() const;
249
250         /*! Set distance from rear axle to front. */
251         void df(double df);
252
253         /*! Get distance from rear axle to rear. */
254         double dr() const;
255
256         /*! Set front track. */
257         void ft(double ft);
258
259         /*! Get front track. */
260         double ft() const;
261
262         /*! \brief Get minimum turning radius.
263          *
264          * Please, note that the method returns really _minimum turning radius_,
265          * which is the distance from the rear axle center to the center of
266          * left or right rotation given by the kinematics constrants, i.e.
267          * _wheelbase and _curb-to-curb_ distance.
268          *
269          * Sometimes _minimum turning _radius is not radius, not minimum, or not
270          * turning. In this method, _minimum turning _radius is minimum turning
271          * radius.
272          */
273         double mtr() const;
274
275         /*! \brief Return inner radius.
276          *
277          * The inner radius is the distance from minimum turning radius circle
278          * center to the nearest point on the car. In this case, the nearest
279          * points on the car are rear axle endpoints.
280          */
281         double iradi() const;
282
283         /*! \brief Return outer front radius.
284          *
285          * The outer front radius is the distance from minimum turning radius
286          * circle center to the farthest point on the front (from the rear axle
287          * view) part of the car.
288          */
289         double ofradi() const;
290
291         /*! \brief Return outer rear radius.
292          *
293          * The outer rear radius is the distance from minimum turning radius
294          * circle center to the farthest point on the rear (from the rear axle
295          * view) part of the car.
296          */
297         double orradi() const;
298
299         /*! \brief Return inner mirror radius.
300          *
301          * The inner mirror radius is the distance from minimum turning radius
302          * circle center to the farther mirror (from the rear axle view).
303          */
304         double imradi() const;
305
306         /*! \brief Return outer mirror radius.
307          *
308          * The outer mirror radius is the distance from minimum turning radius
309          * circle center to the farther mirror (from the rear axle view).
310          */
311         double omradi() const;
312
313         /*! \brief Return length of perfect parking slot.
314          *
315          * The width of the slot is the same as the width of the car.
316          *
317          * \see Simon R. Blackburn *The Geometry of Perfect Parking*
318          * \see https://www.ma.rhul.ac.uk/SRBparking
319          */
320         double perfect_parking_slot_len() const;
321 };
322
323 /*! Store car motion. */
324 class CarMove {
325 private:
326         double _speed = 0.0;
327         double _steer = 0.0;
328 public:
329         /*! Get speed. */
330         double sp() const;
331
332         /*! Set speed. */
333         void sp(double sp);
334
335         /*! Get steer. */
336         double st() const;
337
338         /*! Set steer. */
339         void st(double st);
340 };
341
342 /*! \brief Geometrical computations of a bicycle car.
343  *
344  * - `x()` and `y()` methods returns coordinates of rear axle center.
345  */
346 class BicycleCar : public virtual Pose, public virtual CarSize,
347                 public virtual CarMove {
348 private:
349 public:
350         /*! \brief Return `true` if `this` can drive to `p` trivially.
351          *
352          * Trivially means that `this` can drive to `p` by line segment - circle
353          * arc - line segment.
354          *
355          * \param p `PoseRange` (resp. `Pose`) to achieve.
356          */
357         bool drivable(PoseRange const& p) const;
358         bool drivable(Pose const& p) const;
359
360         /*! Set maximum steering angle. */
361         void set_max_steer();
362
363         /*! Get frame's left front x coordinate. */
364         double lfx() const;
365
366         /*! Get frame's left front y coordinate. */
367         double lfy() const;
368
369         /*! Get frame's left rear x coordinate. */
370         double lrx() const;
371
372         /*! Get frame's left rear y coordinate. */
373         double lry() const;
374
375         /*! Get frame's right rear x coordinate. */
376         double rrx() const;
377
378         /*! Get frame's right rear y coordinate. */
379         double rry() const;
380
381         /*! Get frame's right front x coordinate. */
382         double rfx() const;
383
384         /*! Get frame's right front y coordinate. */
385         double rfy() const;
386
387         /*! Get frame's left front point. */
388         Point lf() const;
389
390         /*! Get frame's left rear point. */
391         Point lr() const;
392
393         /*! Get frame's right rear point. */
394         Point rr() const;
395
396         /*! Get frame's right front point. */
397         Point rf() const;
398
399         /*! Get frame's left side. */
400         Line left() const;
401
402         /*! Get frame's rear side. */
403         Line rear() const;
404
405         /*! Get frame's right side. */
406         Line right() const;
407
408         /*! Get frame's front side. */
409         Line front() const;
410
411         /*! Get frame's left rear axle x coordinate. */
412         double lrax() const;
413
414         /*! Get frame's left rear axle y coordinate. */
415         double lray() const;
416
417         /*! Get frame's right rear axle x coordinate. */
418         double rrax() const;
419
420         /*! Get frame's right rear axle y coordinate. */
421         double rray() const;
422
423         /*! Get frame's left rear axle point. */
424         Point lra() const;
425
426         /*! Get frame's right rear axle point. */
427         Point rra() const;
428
429         /*! Get frame's left front axle x coordinate. */
430         double lfax() const;
431
432         /*! Get frame's left front axle y coordinate. */
433         double lfay() const;
434
435         /*! Get frame's right front axle x coordinate. */
436         double rfax() const;
437
438         /*! Get frame's right front axle y coordinate. */
439         double rfay() const;
440
441         /*! Get iframe's left front axle point. */
442         Point lfa() const;
443
444         /*! Get frame's right front axle point. */
445         Point rfa() const;
446
447         /*! Get frame's left front mirror x coordinate. */
448         double lfmx() const;
449
450         /*! Get frame's left front mirror y coordinate. */
451         double lfmy() const;
452
453         /*! Get frame's right front mirror x coordinate. */
454         double rfmx() const;
455
456         /*! Get frame's right front mirror y coordinate. */
457         double rfmy() const;
458
459         /*! Get iframe's left front mirror point. */
460         Point lfm() const;
461
462         /*! Get frame's right front mirror point. */
463         Point rfm() const;
464
465         /*! Get frame's center front x coordinate. */
466         double cfx() const;
467
468         /*! Get frame's center front y coordinate. */
469         double cfy() const;
470
471         /*! Get frame's center front point. */
472         Point cf() const;
473
474         /*! Min. turning radius circle center on left. */
475         Point ccl() const;
476
477         /*! Min. turning radius circle center on rigth. */
478         Point ccr() const;
479
480         /*! Next car position based on speed `sp` and steer `st`. */
481         void next();
482
483         /*! Options for generating output for gnuplot. */
484         class GenPlotOpts {
485         public:
486                 bool LF_POINT = false;
487                 bool LR_POINT = false;
488                 bool RR_POINT = false;
489                 bool RF_POINT = false;
490                 bool LFM_POINT = false;
491                 bool RFM_POINT = false;
492                 bool CRA_POINT = false;
493                 bool CAR_POINT = false;
494                 bool LRA_POINT = false;
495                 bool RRA_POINT = false;
496
497                 bool LEFT = false;
498                 bool RIGHT = false;
499                 bool REAR = false;
500                 bool FRONT = false;
501                 bool FRAME = false; // LEFT, RIGHT, REAR, FRONT
502                 bool ARROW = false;
503                 bool CROSS = false;
504                 bool CAR = false; // CROSS, ARROW, FRAME
505                 bool LEFT_MIRROR = false;
506                 bool RIGHT_MIRROR = false;
507                 bool MIRRORS = false; // RIGHT_MIRROR, LEFT_MIRROR
508                 bool ALL = true; // MIRRORS, CAR
509         };
510
511         /*! Generate output for plotting with gnuplot. */
512         void gen_gnuplot_to(std::ostream& out, GenPlotOpts opts);
513 };
514
515 } // namespace bcar
516 #endif /* BCAR_BCAR_H */