]> rtime.felk.cvut.cz Git - hubacji1/bcar.git/blob - incl/bcar.hh
Add width with mirrors
[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_with_mirrors = 1.945;
205         double _width = 1.771;
206         double _wheelbase = 2.588;
207         double _distance_to_front = 3.427;
208         double _length = 4.084;
209         double _front_track = 1.511;
210 public:
211         /*! Get curb-to-curb distance. */
212         double ctc() const;
213
214         /*! Set curb-to-curb distance. */
215         void ctc(double ctc);
216
217         /*! Get wheelbase. */
218         double wb() const;
219
220         /*! Set wheelbase. */
221         void wb(double wb);
222
223         /*! Get width. */
224         double w() const;
225
226         /*! Set width. */
227         void w(double w);
228
229         /*! Get width with mirrors. */
230         double wwm() const;
231
232         /*! Set width with mirrors. */
233         void wwm(double w);
234
235         /*! Get length. */
236         double len() const;
237
238         /*! Set length. */
239         void len(double len);
240
241         /*! Get distance from rear axle to front. */
242         double df() const;
243
244         /*! Set distance from rear axle to front. */
245         void df(double df);
246
247         /*! Get distance from rear axle to rear. */
248         double dr() const;
249
250         /*! Set front track. */
251         void ft(double ft);
252
253         /*! Get front track. */
254         double ft() const;
255
256         /*! \brief Get minimum turning radius.
257          *
258          * Please, note that the method returns really _minimum turning radius_,
259          * which is the distance from the rear axle center to the center of
260          * left or right rotation given by the kinematics constrants, i.e.
261          * _wheelbase and _curb-to-curb_ distance.
262          *
263          * Sometimes _minimum turning _radius is not radius, not minimum, or not
264          * turning. In this method, _minimum turning _radius is minimum turning
265          * radius.
266          */
267         double mtr() const;
268
269         /*! \brief Return inner radius.
270          *
271          * The inner radius is the distance from minimum turning radius circle
272          * center to the nearest point on the car. In this case, the nearest
273          * points on the car are rear axle endpoints.
274          */
275         double iradi() const;
276
277         /*! \brief Return outer front radius.
278          *
279          * The outer front radius is the distance from minimum turning radius
280          * circle center to the farthest point on the front (from the rear axle
281          * view) part of the car.
282          */
283         double ofradi() const;
284
285         /*! \brief Return outer rear radius.
286          *
287          * The outer rear radius is the distance from minimum turning radius
288          * circle center to the farthest point on the rear (from the rear axle
289          * view) part of the car.
290          */
291         double orradi() const;
292
293         /*! \brief Return length of perfect parking slot.
294          *
295          * The width of the slot is the same as the width of the car.
296          *
297          * \see Simon R. Blackburn *The Geometry of Perfect Parking*
298          * \see https://www.ma.rhul.ac.uk/SRBparking
299          */
300         double perfect_parking_slot_len() const;
301 };
302
303 /*! Store car motion. */
304 class CarMove {
305 private:
306         double _speed = 0.0;
307         double _steer = 0.0;
308 public:
309         /*! Get speed. */
310         double sp() const;
311
312         /*! Set speed. */
313         void sp(double sp);
314
315         /*! Get steer. */
316         double st() const;
317
318         /*! Set steer. */
319         void st(double st);
320 };
321
322 /*! \brief Geometrical computations of a bicycle car.
323  *
324  * - `x()` and `y()` methods returns coordinates of rear axle center.
325  */
326 class BicycleCar : public virtual Pose, public virtual CarSize,
327                 public virtual CarMove {
328 private:
329 public:
330         /*! \brief Return `true` if `this` can drive to `p` trivially.
331          *
332          * Trivially means that `this` can drive to `p` by line segment - circle
333          * arc - line segment.
334          *
335          * \param p `PoseRange` (resp. `Pose`) to achieve.
336          */
337         bool drivable(PoseRange const& p) const;
338         bool drivable(Pose const& p) const;
339
340         /*! Set maximum steering angle. */
341         void set_max_steer();
342
343         /*! Get frame's left front x coordinate. */
344         double lfx() const;
345
346         /*! Get frame's left front y coordinate. */
347         double lfy() const;
348
349         /*! Get frame's left rear x coordinate. */
350         double lrx() const;
351
352         /*! Get frame's left rear y coordinate. */
353         double lry() const;
354
355         /*! Get frame's right rear x coordinate. */
356         double rrx() const;
357
358         /*! Get frame's right rear y coordinate. */
359         double rry() const;
360
361         /*! Get frame's right front x coordinate. */
362         double rfx() const;
363
364         /*! Get frame's right front y coordinate. */
365         double rfy() const;
366
367         /*! Get frame's left front point. */
368         Point lf() const;
369
370         /*! Get frame's left rear point. */
371         Point lr() const;
372
373         /*! Get frame's right rear point. */
374         Point rr() const;
375
376         /*! Get frame's right front point. */
377         Point rf() const;
378
379         /*! Get frame's left side. */
380         Line left() const;
381
382         /*! Get frame's rear side. */
383         Line rear() const;
384
385         /*! Get frame's right side. */
386         Line right() const;
387
388         /*! Get frame's front side. */
389         Line front() const;
390
391         /*! Get frame's left rear axle x coordinate. */
392         double lrax() const;
393
394         /*! Get frame's left rear axle y coordinate. */
395         double lray() const;
396
397         /*! Get frame's right rear axle x coordinate. */
398         double rrax() const;
399
400         /*! Get frame's right rear axle y coordinate. */
401         double rray() const;
402
403         /*! Get frame's left rear axle point. */
404         Point lra() const;
405
406         /*! Get frame's right rear axle point. */
407         Point rra() const;
408
409         /*! Get frame's left front axle x coordinate. */
410         double lfax() const;
411
412         /*! Get frame's left front axle y coordinate. */
413         double lfay() const;
414
415         /*! Get frame's right front axle x coordinate. */
416         double rfax() const;
417
418         /*! Get frame's right front axle y coordinate. */
419         double rfay() const;
420
421         /*! Get iframe's left front axle point. */
422         Point lfa() const;
423
424         /*! Get frame's right front axle point. */
425         Point rfa() const;
426
427         /*! Get frame's left front mirror x coordinate. */
428         double lfmx() const;
429
430         /*! Get frame's left front mirror y coordinate. */
431         double lfmy() const;
432
433         /*! Get frame's right front mirror x coordinate. */
434         double rfmx() const;
435
436         /*! Get frame's right front mirror y coordinate. */
437         double rfmy() const;
438
439         /*! Get iframe's left front mirror point. */
440         Point lfm() const;
441
442         /*! Get frame's right front mirror point. */
443         Point rfm() const;
444
445         /*! Get frame's center front x coordinate. */
446         double cfx() const;
447
448         /*! Get frame's center front y coordinate. */
449         double cfy() const;
450
451         /*! Get frame's center front point. */
452         Point cf() const;
453
454         /*! Min. turning radius circle center on left. */
455         Point ccl() const;
456
457         /*! Min. turning radius circle center on rigth. */
458         Point ccr() const;
459
460         /*! Next car position based on speed `sp` and steer `st`. */
461         void next();
462 };
463
464 } // namespace bcar
465 #endif /* BCAR_BCAR_H */