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